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 | |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 27 | #include "compat.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 28 | #include "context.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 29 | #include "dict.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 30 | #include "hash_table.h" |
Michal Vasko | 8f702ee | 2024-02-20 15:44:24 +0100 | [diff] [blame] | 31 | #include "ly_common.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 */ |
Jan Kundrát | 3a7d5f7 | 2023-11-02 08:02:07 +0100 | [diff] [blame] | 239 | p = vsnprintf(*str ? *str + *used : NULL, *size - *used, format, ap); |
Michal Vasko | 1696178 | 2023-11-01 10:55:36 +0100 | [diff] [blame] | 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 | ae893ef | 2024-07-02 08:35:40 +0200 | [diff] [blame] | 333 | } else if (lyd_get_value(item->node)) { |
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); |
Michal Vasko | 01f1eb7 | 2024-02-20 15:49:49 +0100 | [diff] [blame] | 571 | LY_CHECK_RET(rc); |
Michal Vasko | 47da656 | 2022-07-14 15:43:15 +0200 | [diff] [blame] | 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); |
Michal Vasko | d470773 | 2024-06-19 07:56:28 +0200 | [diff] [blame] | 693 | if (errno || *ptr || (ptr == str)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 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 | b7ba2c9 | 2024-01-05 15:17:53 +0100 | [diff] [blame] | 902 | if (!set) { |
| 903 | return; |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 904 | } |
Michal Vasko | b7ba2c9 | 2024-01-05 15:17:53 +0100 | [diff] [blame] | 905 | |
| 906 | new->non_child_axis = set->non_child_axis; |
| 907 | new->not_found = set->not_found; |
| 908 | new->ctx = set->ctx; |
| 909 | new->cur_node = set->cur_node; |
| 910 | new->root_type = set->root_type; |
| 911 | new->context_op = set->context_op; |
| 912 | new->tree = set->tree; |
| 913 | new->cur_mod = set->cur_mod; |
| 914 | new->format = set->format; |
| 915 | new->prefix_data = set->prefix_data; |
| 916 | new->vars = set->vars; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 917 | } |
| 918 | |
| 919 | /** |
| 920 | * @brief Create a deep copy of a set. |
| 921 | * |
| 922 | * @param[in] set Set to copy. |
| 923 | * @return Copy of @p set. |
| 924 | */ |
| 925 | static struct lyxp_set * |
| 926 | set_copy(struct lyxp_set *set) |
| 927 | { |
| 928 | struct lyxp_set *ret; |
Michal Vasko | 1fdd8fa | 2021-01-08 09:21:45 +0100 | [diff] [blame] | 929 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 930 | |
| 931 | if (!set) { |
| 932 | return NULL; |
| 933 | } |
| 934 | |
| 935 | ret = malloc(sizeof *ret); |
| 936 | LY_CHECK_ERR_RET(!ret, LOGMEM(set->ctx), NULL); |
| 937 | set_init(ret, set); |
| 938 | |
| 939 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 940 | ret->type = set->type; |
| 941 | |
| 942 | for (i = 0; i < set->used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 943 | if ((set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) || |
| 944 | (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START)) { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 945 | uint32_t idx; |
Michal Vasko | 26bbb27 | 2022-08-02 14:54:33 +0200 | [diff] [blame] | 946 | |
Michal Vasko | e4a6d01 | 2023-05-22 14:34:52 +0200 | [diff] [blame] | 947 | 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] | 948 | set->val.scnodes[i].axis, &idx), lyxp_set_free(ret), NULL); |
Michal Vasko | 3f27c52 | 2020-01-06 08:37:49 +0100 | [diff] [blame] | 949 | /* coverity seems to think scnodes can be NULL */ |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 950 | if (!ret->val.scnodes) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 951 | lyxp_set_free(ret); |
| 952 | return NULL; |
| 953 | } |
Michal Vasko | ba71654 | 2019-12-16 10:01:58 +0100 | [diff] [blame] | 954 | ret->val.scnodes[idx].in_ctx = set->val.scnodes[i].in_ctx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 955 | } |
| 956 | } |
| 957 | } else if (set->type == LYXP_SET_NODE_SET) { |
| 958 | ret->type = set->type; |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 959 | if (set->used) { |
| 960 | ret->val.nodes = malloc(set->used * sizeof *ret->val.nodes); |
| 961 | LY_CHECK_ERR_RET(!ret->val.nodes, LOGMEM(set->ctx); free(ret), NULL); |
| 962 | memcpy(ret->val.nodes, set->val.nodes, set->used * sizeof *ret->val.nodes); |
| 963 | } else { |
| 964 | ret->val.nodes = NULL; |
| 965 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 966 | |
| 967 | ret->used = ret->size = set->used; |
| 968 | ret->ctx_pos = set->ctx_pos; |
| 969 | ret->ctx_size = set->ctx_size; |
Michal Vasko | 4a04e54 | 2021-02-01 08:58:30 +0100 | [diff] [blame] | 970 | if (set->ht) { |
| 971 | ret->ht = lyht_dup(set->ht); |
| 972 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 973 | } else { |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 974 | memcpy(ret, set, sizeof *ret); |
| 975 | if (set->type == LYXP_SET_STRING) { |
| 976 | ret->val.str = strdup(set->val.str); |
| 977 | LY_CHECK_ERR_RET(!ret->val.str, LOGMEM(set->ctx); free(ret), NULL); |
| 978 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 979 | } |
| 980 | |
| 981 | return ret; |
| 982 | } |
| 983 | |
| 984 | /** |
| 985 | * @brief Fill XPath set with a string. Any current data are disposed of. |
| 986 | * |
| 987 | * @param[in] set Set to fill. |
| 988 | * @param[in] string String to fill into \p set. |
| 989 | * @param[in] str_len Length of \p string. 0 is a valid value! |
| 990 | */ |
| 991 | static void |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 992 | 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] | 993 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 994 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 995 | |
| 996 | set->type = LYXP_SET_STRING; |
| 997 | if ((str_len == 0) && (string[0] != '\0')) { |
| 998 | string = ""; |
| 999 | } |
| 1000 | set->val.str = strndup(string, str_len); |
| 1001 | } |
| 1002 | |
| 1003 | /** |
| 1004 | * @brief Fill XPath set with a number. Any current data are disposed of. |
| 1005 | * |
| 1006 | * @param[in] set Set to fill. |
| 1007 | * @param[in] number Number to fill into \p set. |
| 1008 | */ |
| 1009 | static void |
| 1010 | set_fill_number(struct lyxp_set *set, long double number) |
| 1011 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1012 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1013 | |
| 1014 | set->type = LYXP_SET_NUMBER; |
| 1015 | set->val.num = number; |
| 1016 | } |
| 1017 | |
| 1018 | /** |
| 1019 | * @brief Fill XPath set with a boolean. Any current data are disposed of. |
| 1020 | * |
| 1021 | * @param[in] set Set to fill. |
| 1022 | * @param[in] boolean Boolean to fill into \p set. |
| 1023 | */ |
| 1024 | static void |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 1025 | set_fill_boolean(struct lyxp_set *set, ly_bool boolean) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1026 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1027 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1028 | |
| 1029 | set->type = LYXP_SET_BOOLEAN; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1030 | set->val.bln = boolean; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1031 | } |
| 1032 | |
| 1033 | /** |
| 1034 | * @brief Fill XPath set with the value from another set (deep assign). |
| 1035 | * Any current data are disposed of. |
| 1036 | * |
| 1037 | * @param[in] trg Set to fill. |
| 1038 | * @param[in] src Source set to copy into \p trg. |
| 1039 | */ |
| 1040 | static void |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1041 | set_fill_set(struct lyxp_set *trg, const struct lyxp_set *src) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1042 | { |
| 1043 | if (!trg || !src) { |
| 1044 | return; |
| 1045 | } |
| 1046 | |
Michal Vasko | f06d19d | 2023-08-04 14:04:08 +0200 | [diff] [blame] | 1047 | lyxp_set_free_content(trg); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1048 | set_init(trg, src); |
| 1049 | |
| 1050 | if (src->type == LYXP_SET_SCNODE_SET) { |
| 1051 | trg->type = LYXP_SET_SCNODE_SET; |
| 1052 | trg->used = src->used; |
| 1053 | trg->size = src->used; |
| 1054 | |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 1055 | if (trg->size) { |
| 1056 | trg->val.scnodes = ly_realloc(trg->val.scnodes, trg->size * sizeof *trg->val.scnodes); |
| 1057 | LY_CHECK_ERR_RET(!trg->val.scnodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), ); |
| 1058 | memcpy(trg->val.scnodes, src->val.scnodes, src->used * sizeof *src->val.scnodes); |
| 1059 | } else { |
| 1060 | trg->val.scnodes = NULL; |
| 1061 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1062 | } else if (src->type == LYXP_SET_BOOLEAN) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1063 | set_fill_boolean(trg, src->val.bln); |
Michal Vasko | 44f3d2c | 2020-08-24 09:49:38 +0200 | [diff] [blame] | 1064 | } else if (src->type == LYXP_SET_NUMBER) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1065 | set_fill_number(trg, src->val.num); |
| 1066 | } else if (src->type == LYXP_SET_STRING) { |
| 1067 | set_fill_string(trg, src->val.str, strlen(src->val.str)); |
| 1068 | } else { |
| 1069 | if (trg->type == LYXP_SET_NODE_SET) { |
| 1070 | free(trg->val.nodes); |
| 1071 | } else if (trg->type == LYXP_SET_STRING) { |
| 1072 | free(trg->val.str); |
| 1073 | } |
| 1074 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1075 | assert(src->type == LYXP_SET_NODE_SET); |
| 1076 | |
| 1077 | trg->type = LYXP_SET_NODE_SET; |
| 1078 | trg->used = src->used; |
| 1079 | trg->size = src->used; |
| 1080 | trg->ctx_pos = src->ctx_pos; |
| 1081 | trg->ctx_size = src->ctx_size; |
| 1082 | |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 1083 | if (trg->size) { |
| 1084 | trg->val.nodes = malloc(trg->size * sizeof *trg->val.nodes); |
| 1085 | LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), ); |
| 1086 | memcpy(trg->val.nodes, src->val.nodes, src->used * sizeof *src->val.nodes); |
| 1087 | } else { |
| 1088 | trg->val.nodes = NULL; |
| 1089 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1090 | if (src->ht) { |
| 1091 | trg->ht = lyht_dup(src->ht); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1092 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1093 | trg->ht = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1094 | } |
| 1095 | } |
| 1096 | } |
| 1097 | |
| 1098 | /** |
| 1099 | * @brief Clear context of all schema nodes. |
| 1100 | * |
| 1101 | * @param[in] set Set to clear. |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 1102 | * @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] | 1103 | */ |
| 1104 | static void |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 1105 | set_scnode_clear_ctx(struct lyxp_set *set, int32_t new_ctx) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1106 | { |
| 1107 | uint32_t i; |
| 1108 | |
| 1109 | for (i = 0; i < set->used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1110 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 1111 | set->val.scnodes[i].in_ctx = new_ctx; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1112 | } else if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START) { |
| 1113 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1114 | } |
| 1115 | } |
| 1116 | } |
| 1117 | |
| 1118 | /** |
| 1119 | * @brief Remove a node from a set. Removing last node changes |
| 1120 | * set into LYXP_SET_EMPTY. Context position aware. |
| 1121 | * |
| 1122 | * @param[in] set Set to use. |
| 1123 | * @param[in] idx Index from @p set of the node to be removed. |
| 1124 | */ |
| 1125 | static void |
| 1126 | set_remove_node(struct lyxp_set *set, uint32_t idx) |
| 1127 | { |
| 1128 | assert(set && (set->type == LYXP_SET_NODE_SET)); |
| 1129 | assert(idx < set->used); |
| 1130 | |
| 1131 | set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
| 1132 | |
| 1133 | --set->used; |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 1134 | if (idx < set->used) { |
| 1135 | memmove(&set->val.nodes[idx], &set->val.nodes[idx + 1], (set->used - idx) * sizeof *set->val.nodes); |
| 1136 | } else if (!set->used) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1137 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1138 | } |
| 1139 | } |
| 1140 | |
| 1141 | /** |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1142 | * @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] | 1143 | * |
| 1144 | * @param[in] set Set to use. |
| 1145 | * @param[in] idx Index from @p set of the node to be removed. |
| 1146 | */ |
| 1147 | static void |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1148 | set_remove_node_none(struct lyxp_set *set, uint32_t idx) |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1149 | { |
| 1150 | assert(set && (set->type == LYXP_SET_NODE_SET)); |
| 1151 | assert(idx < set->used); |
| 1152 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1153 | if (set->val.nodes[idx].type == LYXP_NODE_ELEM) { |
| 1154 | set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
| 1155 | } |
| 1156 | set->val.nodes[idx].type = LYXP_NODE_NONE; |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1157 | } |
| 1158 | |
| 1159 | /** |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1160 | * @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] | 1161 | * set into LYXP_SET_EMPTY. Context position aware. |
| 1162 | * |
| 1163 | * @param[in] set Set to consolidate. |
| 1164 | */ |
| 1165 | static void |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1166 | set_remove_nodes_none(struct lyxp_set *set) |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1167 | { |
Michal Vasko | 1fdd8fa | 2021-01-08 09:21:45 +0100 | [diff] [blame] | 1168 | uint32_t i, orig_used, end = 0; |
| 1169 | int64_t start; |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1170 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1171 | assert(set); |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1172 | |
| 1173 | orig_used = set->used; |
| 1174 | set->used = 0; |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 1175 | for (i = 0; i < orig_used; ) { |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1176 | start = -1; |
| 1177 | do { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1178 | if ((set->val.nodes[i].type != LYXP_NODE_NONE) && (start == -1)) { |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1179 | start = i; |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1180 | } else if ((start > -1) && (set->val.nodes[i].type == LYXP_NODE_NONE)) { |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1181 | end = i; |
| 1182 | ++i; |
| 1183 | break; |
| 1184 | } |
| 1185 | |
| 1186 | ++i; |
| 1187 | if (i == orig_used) { |
| 1188 | end = i; |
| 1189 | } |
| 1190 | } while (i < orig_used); |
| 1191 | |
| 1192 | if (start > -1) { |
| 1193 | /* move the whole chunk of valid nodes together */ |
| 1194 | if (set->used != (unsigned)start) { |
| 1195 | memmove(&set->val.nodes[set->used], &set->val.nodes[start], (end - start) * sizeof *set->val.nodes); |
| 1196 | } |
| 1197 | set->used += end - start; |
| 1198 | } |
| 1199 | } |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1200 | } |
| 1201 | |
| 1202 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1203 | * @brief Check for duplicates in a node set. |
| 1204 | * |
| 1205 | * @param[in] set Set to check. |
| 1206 | * @param[in] node Node to look for in @p set. |
| 1207 | * @param[in] node_type Type of @p node. |
| 1208 | * @param[in] skip_idx Index from @p set to skip. |
| 1209 | * @return LY_ERR |
| 1210 | */ |
| 1211 | static LY_ERR |
| 1212 | set_dup_node_check(const struct lyxp_set *set, const struct lyd_node *node, enum lyxp_node_type node_type, int skip_idx) |
| 1213 | { |
| 1214 | uint32_t i; |
| 1215 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1216 | if (set->ht && node) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1217 | return set_dup_node_hash_check(set, (struct lyd_node *)node, node_type, skip_idx); |
| 1218 | } |
| 1219 | |
| 1220 | for (i = 0; i < set->used; ++i) { |
| 1221 | if ((skip_idx > -1) && (i == (unsigned)skip_idx)) { |
| 1222 | continue; |
| 1223 | } |
| 1224 | |
| 1225 | if ((set->val.nodes[i].node == node) && (set->val.nodes[i].type == node_type)) { |
| 1226 | return LY_EEXIST; |
| 1227 | } |
| 1228 | } |
| 1229 | |
| 1230 | return LY_SUCCESS; |
| 1231 | } |
| 1232 | |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 1233 | ly_bool |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1234 | lyxp_set_scnode_contains(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, int skip_idx, |
| 1235 | uint32_t *index_p) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1236 | { |
| 1237 | uint32_t i; |
| 1238 | |
| 1239 | for (i = 0; i < set->used; ++i) { |
| 1240 | if ((skip_idx > -1) && (i == (unsigned)skip_idx)) { |
| 1241 | continue; |
| 1242 | } |
| 1243 | |
| 1244 | 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] | 1245 | if (index_p) { |
| 1246 | *index_p = i; |
| 1247 | } |
| 1248 | return 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1249 | } |
| 1250 | } |
| 1251 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1252 | return 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1253 | } |
| 1254 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 1255 | void |
| 1256 | lyxp_set_scnode_merge(struct lyxp_set *set1, struct lyxp_set *set2) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1257 | { |
| 1258 | uint32_t orig_used, i, j; |
| 1259 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1260 | 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] | 1261 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1262 | if (!set2->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1263 | return; |
| 1264 | } |
| 1265 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1266 | if (!set1->used) { |
aPiecek | adc1e4f | 2021-10-07 11:15:12 +0200 | [diff] [blame] | 1267 | /* release hidden allocated data (lyxp_set.size) */ |
| 1268 | lyxp_set_free_content(set1); |
| 1269 | /* direct copying of the entire structure */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1270 | memcpy(set1, set2, sizeof *set1); |
| 1271 | return; |
| 1272 | } |
| 1273 | |
| 1274 | if (set1->used + set2->used > set1->size) { |
| 1275 | set1->size = set1->used + set2->used; |
| 1276 | set1->val.scnodes = ly_realloc(set1->val.scnodes, set1->size * sizeof *set1->val.scnodes); |
| 1277 | LY_CHECK_ERR_RET(!set1->val.scnodes, LOGMEM(set1->ctx), ); |
| 1278 | } |
| 1279 | |
| 1280 | orig_used = set1->used; |
| 1281 | |
| 1282 | for (i = 0; i < set2->used; ++i) { |
| 1283 | for (j = 0; j < orig_used; ++j) { |
| 1284 | /* detect duplicities */ |
| 1285 | if (set1->val.scnodes[j].scnode == set2->val.scnodes[i].scnode) { |
| 1286 | break; |
| 1287 | } |
| 1288 | } |
| 1289 | |
Michal Vasko | 0587bce | 2020-12-10 12:19:21 +0100 | [diff] [blame] | 1290 | if (j < orig_used) { |
| 1291 | /* node is there, but update its status if needed */ |
| 1292 | if (set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_START_USED) { |
| 1293 | set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx; |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 1294 | } else if ((set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_ATOM_NODE) && |
| 1295 | (set2->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_VAL)) { |
| 1296 | set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx; |
Michal Vasko | 0587bce | 2020-12-10 12:19:21 +0100 | [diff] [blame] | 1297 | } |
| 1298 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1299 | memcpy(&set1->val.scnodes[set1->used], &set2->val.scnodes[i], sizeof *set2->val.scnodes); |
| 1300 | ++set1->used; |
| 1301 | } |
| 1302 | } |
| 1303 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1304 | lyxp_set_free_content(set2); |
| 1305 | set2->type = LYXP_SET_SCNODE_SET; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1306 | } |
| 1307 | |
| 1308 | /** |
| 1309 | * @brief Insert a node into a set. Context position aware. |
| 1310 | * |
| 1311 | * @param[in] set Set to use. |
| 1312 | * @param[in] node Node to insert to @p set. |
| 1313 | * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting. |
| 1314 | * @param[in] node_type Node type of @p node. |
| 1315 | * @param[in] idx Index in @p set to insert into. |
| 1316 | */ |
| 1317 | static void |
| 1318 | set_insert_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx) |
| 1319 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1320 | assert(set && (set->type == LYXP_SET_NODE_SET)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1321 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1322 | if (!set->size) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1323 | /* first item */ |
| 1324 | if (idx) { |
| 1325 | /* no real harm done, but it is a bug */ |
| 1326 | LOGINT(set->ctx); |
| 1327 | idx = 0; |
| 1328 | } |
| 1329 | set->val.nodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.nodes); |
| 1330 | LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), ); |
| 1331 | set->type = LYXP_SET_NODE_SET; |
| 1332 | set->used = 0; |
| 1333 | set->size = LYXP_SET_SIZE_START; |
| 1334 | set->ctx_pos = 1; |
| 1335 | set->ctx_size = 1; |
| 1336 | set->ht = NULL; |
| 1337 | } else { |
| 1338 | /* not an empty set */ |
| 1339 | if (set->used == set->size) { |
| 1340 | |
| 1341 | /* set is full */ |
Michal Vasko | 871df52 | 2022-04-06 12:14:41 +0200 | [diff] [blame] | 1342 | 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] | 1343 | LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), ); |
Michal Vasko | 871df52 | 2022-04-06 12:14:41 +0200 | [diff] [blame] | 1344 | set->size *= LYXP_SET_SIZE_MUL_STEP; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1345 | } |
| 1346 | |
| 1347 | if (idx > set->used) { |
| 1348 | LOGINT(set->ctx); |
| 1349 | idx = set->used; |
| 1350 | } |
| 1351 | |
| 1352 | /* make space for the new node */ |
| 1353 | if (idx < set->used) { |
| 1354 | memmove(&set->val.nodes[idx + 1], &set->val.nodes[idx], (set->used - idx) * sizeof *set->val.nodes); |
| 1355 | } |
| 1356 | } |
| 1357 | |
| 1358 | /* finally assign the value */ |
| 1359 | set->val.nodes[idx].node = (struct lyd_node *)node; |
| 1360 | set->val.nodes[idx].type = node_type; |
| 1361 | set->val.nodes[idx].pos = pos; |
| 1362 | ++set->used; |
| 1363 | |
Michal Vasko | 2416fdd | 2021-10-01 11:07:10 +0200 | [diff] [blame] | 1364 | /* add into hash table */ |
| 1365 | set_insert_node_hash(set, (struct lyd_node *)node, node_type); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1366 | } |
| 1367 | |
Michal Vasko | e4a6d01 | 2023-05-22 14:34:52 +0200 | [diff] [blame] | 1368 | LY_ERR |
| 1369 | 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] | 1370 | enum lyxp_axis axis, uint32_t *index_p) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1371 | { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1372 | uint32_t index; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1373 | |
| 1374 | assert(set->type == LYXP_SET_SCNODE_SET); |
| 1375 | |
Michal Vasko | 871df52 | 2022-04-06 12:14:41 +0200 | [diff] [blame] | 1376 | if (!set->size) { |
| 1377 | /* first item */ |
| 1378 | set->val.scnodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.scnodes); |
| 1379 | LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), LY_EMEM); |
| 1380 | set->type = LYXP_SET_SCNODE_SET; |
| 1381 | set->used = 0; |
| 1382 | set->size = LYXP_SET_SIZE_START; |
| 1383 | set->ctx_pos = 1; |
| 1384 | set->ctx_size = 1; |
| 1385 | set->ht = NULL; |
| 1386 | } |
| 1387 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1388 | if (lyxp_set_scnode_contains(set, node, node_type, -1, &index)) { |
Michal Vasko | e4a6d01 | 2023-05-22 14:34:52 +0200 | [diff] [blame] | 1389 | /* BUG if axes differ, this new one is thrown away */ |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1390 | set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1391 | } else { |
| 1392 | if (set->used == set->size) { |
Michal Vasko | 871df52 | 2022-04-06 12:14:41 +0200 | [diff] [blame] | 1393 | 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] | 1394 | LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), LY_EMEM); |
Michal Vasko | 871df52 | 2022-04-06 12:14:41 +0200 | [diff] [blame] | 1395 | set->size *= LYXP_SET_SIZE_MUL_STEP; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1396 | } |
| 1397 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1398 | index = set->used; |
| 1399 | set->val.scnodes[index].scnode = (struct lysc_node *)node; |
| 1400 | set->val.scnodes[index].type = node_type; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1401 | set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
Michal Vasko | 7333cb3 | 2022-07-29 16:30:29 +0200 | [diff] [blame] | 1402 | set->val.scnodes[index].axis = axis; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1403 | ++set->used; |
| 1404 | } |
| 1405 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1406 | if (index_p) { |
| 1407 | *index_p = index; |
| 1408 | } |
| 1409 | |
| 1410 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1411 | } |
| 1412 | |
| 1413 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1414 | * @brief Set all nodes with ctx 1 to a new unique context value. |
| 1415 | * |
| 1416 | * @param[in] set Set to modify. |
| 1417 | * @return New context value. |
| 1418 | */ |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 1419 | static int32_t |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1420 | set_scnode_new_in_ctx(struct lyxp_set *set) |
| 1421 | { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 1422 | uint32_t i; |
| 1423 | int32_t ret_ctx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1424 | |
| 1425 | assert(set->type == LYXP_SET_SCNODE_SET); |
| 1426 | |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1427 | ret_ctx = LYXP_SET_SCNODE_ATOM_PRED_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1428 | retry: |
| 1429 | for (i = 0; i < set->used; ++i) { |
| 1430 | if (set->val.scnodes[i].in_ctx >= ret_ctx) { |
| 1431 | ret_ctx = set->val.scnodes[i].in_ctx + 1; |
| 1432 | goto retry; |
| 1433 | } |
| 1434 | } |
| 1435 | for (i = 0; i < set->used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1436 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1437 | set->val.scnodes[i].in_ctx = ret_ctx; |
| 1438 | } |
| 1439 | } |
| 1440 | |
| 1441 | return ret_ctx; |
| 1442 | } |
| 1443 | |
| 1444 | /** |
| 1445 | * @brief Get unique @p node position in the data. |
| 1446 | * |
| 1447 | * @param[in] node Node to find. |
| 1448 | * @param[in] node_type Node type of @p node. |
| 1449 | * @param[in] root Root node. |
| 1450 | * @param[in] root_type Type of the XPath @p root node. |
| 1451 | * @param[in] prev Node that we think is before @p node in DFS from @p root. Can optionally |
| 1452 | * be used to increase efficiency and start the DFS from this node. |
| 1453 | * @param[in] prev_pos Node @p prev position. Optional, but must be set if @p prev is set. |
| 1454 | * @return Node position. |
| 1455 | */ |
| 1456 | static uint32_t |
| 1457 | 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] | 1458 | 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] | 1459 | { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1460 | const struct lyd_node *elem = NULL, *top_sibling; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1461 | uint32_t pos = 1; |
Michal Vasko | fb6c9dd | 2020-11-18 18:18:47 +0100 | [diff] [blame] | 1462 | ly_bool found = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1463 | |
| 1464 | assert(prev && prev_pos && !root->prev->next); |
| 1465 | |
| 1466 | if ((node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ROOT_CONFIG)) { |
| 1467 | return 0; |
| 1468 | } |
| 1469 | |
| 1470 | if (*prev) { |
| 1471 | /* start from the previous element instead from the root */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1472 | pos = *prev_pos; |
Michal Vasko | fb6c9dd | 2020-11-18 18:18:47 +0100 | [diff] [blame] | 1473 | 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] | 1474 | goto dfs_search; |
| 1475 | } |
| 1476 | |
Michal Vasko | fb6c9dd | 2020-11-18 18:18:47 +0100 | [diff] [blame] | 1477 | LY_LIST_FOR(root, top_sibling) { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1478 | LYD_TREE_DFS_BEGIN(top_sibling, elem) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1479 | dfs_search: |
Michal Vasko | a9309bb | 2021-07-09 09:31:55 +0200 | [diff] [blame] | 1480 | LYD_TREE_DFS_continue = 0; |
| 1481 | |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1482 | if (*prev && !elem) { |
| 1483 | /* resume previous DFS */ |
| 1484 | elem = LYD_TREE_DFS_next = (struct lyd_node *)*prev; |
| 1485 | LYD_TREE_DFS_continue = 0; |
| 1486 | } |
| 1487 | |
Michal Vasko | 1ef66d4 | 2023-12-14 11:41:18 +0100 | [diff] [blame] | 1488 | if ((root_type == LYXP_NODE_ROOT_CONFIG) && elem->schema && (elem->schema->flags & LYS_CONFIG_R)) { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1489 | /* skip */ |
| 1490 | LYD_TREE_DFS_continue = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1491 | } else { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1492 | if (elem == node) { |
Michal Vasko | fb6c9dd | 2020-11-18 18:18:47 +0100 | [diff] [blame] | 1493 | found = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1494 | break; |
| 1495 | } |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1496 | ++pos; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1497 | } |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1498 | |
| 1499 | LYD_TREE_DFS_END(top_sibling, elem); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1500 | } |
| 1501 | |
| 1502 | /* node found */ |
Michal Vasko | fb6c9dd | 2020-11-18 18:18:47 +0100 | [diff] [blame] | 1503 | if (found) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1504 | break; |
| 1505 | } |
| 1506 | } |
| 1507 | |
Michal Vasko | fb6c9dd | 2020-11-18 18:18:47 +0100 | [diff] [blame] | 1508 | if (!found) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1509 | if (!(*prev)) { |
| 1510 | /* we went from root and failed to find it, cannot be */ |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1511 | LOGINT(LYD_CTX(node)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1512 | return 0; |
| 1513 | } else { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1514 | /* start the search again from the beginning */ |
| 1515 | *prev = root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1516 | |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1517 | top_sibling = root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1518 | pos = 1; |
| 1519 | goto dfs_search; |
| 1520 | } |
| 1521 | } |
| 1522 | |
| 1523 | /* remember the last found node for next time */ |
| 1524 | *prev = node; |
| 1525 | *prev_pos = pos; |
| 1526 | |
| 1527 | return pos; |
| 1528 | } |
| 1529 | |
| 1530 | /** |
| 1531 | * @brief Assign (fill) missing node positions. |
| 1532 | * |
| 1533 | * @param[in] set Set to fill positions in. |
| 1534 | * @param[in] root Context root node. |
| 1535 | * @param[in] root_type Context root type. |
| 1536 | * @return LY_ERR |
| 1537 | */ |
| 1538 | static LY_ERR |
| 1539 | set_assign_pos(struct lyxp_set *set, const struct lyd_node *root, enum lyxp_node_type root_type) |
| 1540 | { |
| 1541 | const struct lyd_node *prev = NULL, *tmp_node; |
| 1542 | uint32_t i, tmp_pos = 0; |
| 1543 | |
| 1544 | for (i = 0; i < set->used; ++i) { |
| 1545 | if (!set->val.nodes[i].pos) { |
| 1546 | tmp_node = NULL; |
| 1547 | switch (set->val.nodes[i].type) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1548 | case LYXP_NODE_META: |
| 1549 | tmp_node = set->val.meta[i].meta->parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1550 | if (!tmp_node) { |
| 1551 | LOGINT_RET(root->schema->module->ctx); |
| 1552 | } |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1553 | /* fall through */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1554 | case LYXP_NODE_ELEM: |
| 1555 | case LYXP_NODE_TEXT: |
| 1556 | if (!tmp_node) { |
| 1557 | tmp_node = set->val.nodes[i].node; |
| 1558 | } |
| 1559 | set->val.nodes[i].pos = get_node_pos(tmp_node, set->val.nodes[i].type, root, root_type, &prev, &tmp_pos); |
| 1560 | break; |
| 1561 | default: |
| 1562 | /* all roots have position 0 */ |
| 1563 | break; |
| 1564 | } |
| 1565 | } |
| 1566 | } |
| 1567 | |
| 1568 | return LY_SUCCESS; |
| 1569 | } |
| 1570 | |
| 1571 | /** |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1572 | * @brief Get unique @p meta position in the parent metadata. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1573 | * |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1574 | * @param[in] meta Metadata to use. |
| 1575 | * @return Metadata position. |
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 | static uint32_t |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1578 | get_meta_pos(struct lyd_meta *meta) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1579 | { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 1580 | uint32_t pos = 0; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1581 | struct lyd_meta *meta2; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1582 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1583 | for (meta2 = meta->parent->meta; meta2 && (meta2 != meta); meta2 = meta2->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1584 | ++pos; |
| 1585 | } |
| 1586 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1587 | assert(meta2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1588 | return pos; |
| 1589 | } |
| 1590 | |
| 1591 | /** |
| 1592 | * @brief Compare 2 nodes in respect to XPath document order. |
| 1593 | * |
| 1594 | * @param[in] item1 1st node. |
| 1595 | * @param[in] item2 2nd node. |
| 1596 | * @return If 1st > 2nd returns 1, 1st == 2nd returns 0, and 1st < 2nd returns -1. |
| 1597 | */ |
| 1598 | static int |
| 1599 | set_sort_compare(struct lyxp_set_node *item1, struct lyxp_set_node *item2) |
| 1600 | { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1601 | uint32_t meta_pos1 = 0, meta_pos2 = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1602 | |
| 1603 | if (item1->pos < item2->pos) { |
| 1604 | return -1; |
| 1605 | } |
| 1606 | |
| 1607 | if (item1->pos > item2->pos) { |
| 1608 | return 1; |
| 1609 | } |
| 1610 | |
| 1611 | /* node positions are equal, the fun case */ |
| 1612 | |
| 1613 | /* 1st ELEM - == - 2nd TEXT, 1st TEXT - == - 2nd ELEM */ |
| 1614 | /* special case since text nodes are actually saved as their parents */ |
| 1615 | if ((item1->node == item2->node) && (item1->type != item2->type)) { |
| 1616 | if (item1->type == LYXP_NODE_ELEM) { |
| 1617 | assert(item2->type == LYXP_NODE_TEXT); |
| 1618 | return -1; |
| 1619 | } else { |
| 1620 | assert((item1->type == LYXP_NODE_TEXT) && (item2->type == LYXP_NODE_ELEM)); |
| 1621 | return 1; |
| 1622 | } |
| 1623 | } |
| 1624 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1625 | /* we need meta positions now */ |
| 1626 | if (item1->type == LYXP_NODE_META) { |
| 1627 | meta_pos1 = get_meta_pos((struct lyd_meta *)item1->node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1628 | } |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1629 | if (item2->type == LYXP_NODE_META) { |
| 1630 | meta_pos2 = get_meta_pos((struct lyd_meta *)item2->node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1631 | } |
| 1632 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1633 | /* 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] | 1634 | /* check for duplicates */ |
| 1635 | if (item1->node == item2->node) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1636 | 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] | 1637 | return 0; |
| 1638 | } |
| 1639 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1640 | /* 1st ELEM - 2nd TEXT, 1st ELEM - any pos - 2nd META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1641 | /* elem is always first, 2nd node is after it */ |
| 1642 | if (item1->type == LYXP_NODE_ELEM) { |
| 1643 | assert(item2->type != LYXP_NODE_ELEM); |
| 1644 | return -1; |
| 1645 | } |
| 1646 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1647 | /* 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] | 1648 | /* 2nd is before 1st */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1649 | if (((item1->type == LYXP_NODE_TEXT) && |
| 1650 | ((item2->type == LYXP_NODE_ELEM) || (item2->type == LYXP_NODE_META))) || |
| 1651 | ((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_ELEM)) || |
| 1652 | (((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_META)) && |
| 1653 | (meta_pos1 > meta_pos2))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1654 | return 1; |
| 1655 | } |
| 1656 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1657 | /* 1st META - any pos - 2nd TEXT, 1st META <pos< - 2nd META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1658 | /* 2nd is after 1st */ |
| 1659 | return -1; |
| 1660 | } |
| 1661 | |
| 1662 | /** |
| 1663 | * @brief Set cast for comparisons. |
| 1664 | * |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 1665 | * @param[in,out] trg Target set to cast source into. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1666 | * @param[in] src Source set. |
| 1667 | * @param[in] type Target set type. |
| 1668 | * @param[in] src_idx Source set node index. |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 1669 | * @return LY_SUCCESS on success. |
| 1670 | * @return LY_ERR value on error. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1671 | */ |
| 1672 | static LY_ERR |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 1673 | 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] | 1674 | { |
| 1675 | assert(src->type == LYXP_SET_NODE_SET); |
| 1676 | |
| 1677 | set_init(trg, src); |
| 1678 | |
| 1679 | /* insert node into target set */ |
| 1680 | set_insert_node(trg, src->val.nodes[src_idx].node, src->val.nodes[src_idx].pos, src->val.nodes[src_idx].type, 0); |
| 1681 | |
| 1682 | /* cast target set appropriately */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1683 | return lyxp_set_cast(trg, type); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1684 | } |
| 1685 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1686 | /** |
| 1687 | * @brief Set content canonization for comparisons. |
| 1688 | * |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 1689 | * @param[in,out] set Set to canonize. |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1690 | * @param[in] xp_node Source XPath node/meta to use for canonization. |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 1691 | * @return LY_SUCCESS on success. |
| 1692 | * @return LY_ERR value on error. |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1693 | */ |
| 1694 | static LY_ERR |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 1695 | 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] | 1696 | { |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 1697 | const struct lysc_type *type = NULL; |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1698 | struct lyd_value val; |
| 1699 | struct ly_err_item *err = NULL; |
Michal Vasko | ce46ced | 2024-01-25 15:28:41 +0100 | [diff] [blame] | 1700 | LY_ERR r; |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1701 | |
| 1702 | /* is there anything to canonize even? */ |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 1703 | if (set->type == LYXP_SET_STRING) { |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1704 | /* do we have a type to use for canonization? */ |
Michal Vasko | 222be68 | 2023-08-24 08:17:12 +0200 | [diff] [blame] | 1705 | 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] | 1706 | type = ((struct lyd_node_term *)xp_node->node)->value.realtype; |
| 1707 | } else if (xp_node->type == LYXP_NODE_META) { |
| 1708 | type = ((struct lyd_meta *)xp_node->node)->value.realtype; |
| 1709 | } |
| 1710 | } |
| 1711 | if (!type) { |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 1712 | /* no canonization needed/possible */ |
| 1713 | return LY_SUCCESS; |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1714 | } |
| 1715 | |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 1716 | /* check for built-in types without required canonization */ |
| 1717 | if ((type->basetype == LY_TYPE_STRING) && (type->plugin->store == lyplg_type_store_string)) { |
| 1718 | /* string */ |
| 1719 | return LY_SUCCESS; |
| 1720 | } |
| 1721 | if ((type->basetype == LY_TYPE_BOOL) && (type->plugin->store == lyplg_type_store_boolean)) { |
| 1722 | /* boolean */ |
| 1723 | return LY_SUCCESS; |
| 1724 | } |
| 1725 | if ((type->basetype == LY_TYPE_ENUM) && (type->plugin->store == lyplg_type_store_enum)) { |
| 1726 | /* enumeration */ |
| 1727 | return LY_SUCCESS; |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1728 | } |
| 1729 | |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 1730 | /* print canonized string, ignore errors, the value may not satisfy schema constraints */ |
Michal Vasko | ce46ced | 2024-01-25 15:28:41 +0100 | [diff] [blame] | 1731 | r = 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] | 1732 | LYD_HINT_DATA, xp_node->node->schema, &val, NULL, &err); |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1733 | ly_err_free(err); |
Michal Vasko | ce46ced | 2024-01-25 15:28:41 +0100 | [diff] [blame] | 1734 | if (r && (r != LY_EINCOMPLETE)) { |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 1735 | /* invalid value, function store automaticaly dealloc value when fail */ |
| 1736 | return LY_SUCCESS; |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1737 | } |
| 1738 | |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 1739 | /* use the canonized string value */ |
| 1740 | free(set->val.str); |
| 1741 | set->val.str = strdup(lyd_value_get_canonical(set->ctx, &val)); |
| 1742 | type->plugin->free(set->ctx, &val); |
| 1743 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM); |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1744 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1745 | return LY_SUCCESS; |
| 1746 | } |
| 1747 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1748 | /** |
| 1749 | * @brief Bubble sort @p set into XPath document order. |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 1750 | * Context position aware. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1751 | * |
| 1752 | * @param[in] set Set to sort. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1753 | * @return How many times the whole set was traversed - 1 (if set was sorted, returns 0). |
| 1754 | */ |
| 1755 | static int |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1756 | set_sort(struct lyxp_set *set) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1757 | { |
| 1758 | uint32_t i, j; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 1759 | int ret = 0, cmp; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 1760 | ly_bool inverted, change; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1761 | const struct lyd_node *root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1762 | struct lyxp_set_node item; |
| 1763 | struct lyxp_set_hash_node hnode; |
| 1764 | uint64_t hash; |
| 1765 | |
Michal Vasko | 3cf8fbf | 2020-05-27 15:21:21 +0200 | [diff] [blame] | 1766 | if ((set->type != LYXP_SET_NODE_SET) || (set->used < 2)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1767 | return 0; |
| 1768 | } |
| 1769 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1770 | /* find first top-level node to be used as anchor for positions */ |
Michal Vasko | 4a7d4d6 | 2021-12-13 17:05:06 +0100 | [diff] [blame] | 1771 | for (root = set->tree; root->parent; root = lyd_parent(root)) {} |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 1772 | for ( ; root->prev->next; root = root->prev) {} |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1773 | |
| 1774 | /* fill positions */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1775 | if (set_assign_pos(set, root, set->root_type)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1776 | return -1; |
| 1777 | } |
| 1778 | |
Michal Vasko | 88a9e80 | 2022-05-24 10:50:28 +0200 | [diff] [blame] | 1779 | #ifndef NDEBUG |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1780 | LOGDBG(LY_LDGXPATH, "SORT BEGIN"); |
| 1781 | print_set_debug(set); |
Michal Vasko | 88a9e80 | 2022-05-24 10:50:28 +0200 | [diff] [blame] | 1782 | #endif |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1783 | |
| 1784 | for (i = 0; i < set->used; ++i) { |
| 1785 | inverted = 0; |
| 1786 | change = 0; |
| 1787 | |
| 1788 | for (j = 1; j < set->used - i; ++j) { |
| 1789 | /* compare node positions */ |
| 1790 | if (inverted) { |
| 1791 | cmp = set_sort_compare(&set->val.nodes[j], &set->val.nodes[j - 1]); |
| 1792 | } else { |
| 1793 | cmp = set_sort_compare(&set->val.nodes[j - 1], &set->val.nodes[j]); |
| 1794 | } |
| 1795 | |
| 1796 | /* swap if needed */ |
| 1797 | if ((inverted && (cmp < 0)) || (!inverted && (cmp > 0))) { |
| 1798 | change = 1; |
| 1799 | |
| 1800 | item = set->val.nodes[j - 1]; |
| 1801 | set->val.nodes[j - 1] = set->val.nodes[j]; |
| 1802 | set->val.nodes[j] = item; |
| 1803 | } else { |
| 1804 | /* whether node_pos1 should be smaller than node_pos2 or the other way around */ |
| 1805 | inverted = !inverted; |
| 1806 | } |
| 1807 | } |
| 1808 | |
| 1809 | ++ret; |
| 1810 | |
| 1811 | if (!change) { |
| 1812 | break; |
| 1813 | } |
| 1814 | } |
| 1815 | |
Michal Vasko | 88a9e80 | 2022-05-24 10:50:28 +0200 | [diff] [blame] | 1816 | #ifndef NDEBUG |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1817 | LOGDBG(LY_LDGXPATH, "SORT END %d", ret); |
| 1818 | print_set_debug(set); |
Michal Vasko | 88a9e80 | 2022-05-24 10:50:28 +0200 | [diff] [blame] | 1819 | #endif |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1820 | |
| 1821 | /* check node hashes */ |
| 1822 | if (set->used >= LYD_HT_MIN_ITEMS) { |
| 1823 | assert(set->ht); |
| 1824 | for (i = 0; i < set->used; ++i) { |
| 1825 | hnode.node = set->val.nodes[i].node; |
| 1826 | hnode.type = set->val.nodes[i].type; |
| 1827 | |
Michal Vasko | ae130f5 | 2023-04-20 14:25:16 +0200 | [diff] [blame] | 1828 | hash = lyht_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 1829 | hash = lyht_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 1830 | hash = lyht_hash_multi(hash, NULL, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1831 | |
| 1832 | assert(!lyht_find(set->ht, &hnode, hash, NULL)); |
| 1833 | } |
| 1834 | } |
| 1835 | |
| 1836 | return ret - 1; |
| 1837 | } |
| 1838 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1839 | /** |
| 1840 | * @brief Merge 2 sorted sets into one. |
| 1841 | * |
| 1842 | * @param[in,out] trg Set to merge into. Duplicates are removed. |
| 1843 | * @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] | 1844 | * @return LY_ERR |
| 1845 | */ |
| 1846 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1847 | set_sorted_merge(struct lyxp_set *trg, struct lyxp_set *src) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1848 | { |
| 1849 | uint32_t i, j, k, count, dup_count; |
| 1850 | int cmp; |
| 1851 | const struct lyd_node *root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1852 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1853 | 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] | 1854 | return LY_EINVAL; |
| 1855 | } |
| 1856 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1857 | if (!src->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1858 | return LY_SUCCESS; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1859 | } else if (!trg->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1860 | set_fill_set(trg, src); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1861 | lyxp_set_free_content(src); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1862 | return LY_SUCCESS; |
| 1863 | } |
| 1864 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1865 | /* find first top-level node to be used as anchor for positions */ |
Michal Vasko | 0143b68 | 2021-12-13 17:13:09 +0100 | [diff] [blame] | 1866 | for (root = trg->tree; root->parent; root = lyd_parent(root)) {} |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 1867 | for ( ; root->prev->next; root = root->prev) {} |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1868 | |
| 1869 | /* fill positions */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1870 | 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] | 1871 | return LY_EINT; |
| 1872 | } |
| 1873 | |
| 1874 | #ifndef NDEBUG |
| 1875 | LOGDBG(LY_LDGXPATH, "MERGE target"); |
| 1876 | print_set_debug(trg); |
| 1877 | LOGDBG(LY_LDGXPATH, "MERGE source"); |
| 1878 | print_set_debug(src); |
| 1879 | #endif |
| 1880 | |
| 1881 | /* make memory for the merge (duplicates are not detected yet, so space |
| 1882 | * will likely be wasted on them, too bad) */ |
| 1883 | if (trg->size - trg->used < src->used) { |
| 1884 | trg->size = trg->used + src->used; |
| 1885 | |
| 1886 | trg->val.nodes = ly_realloc(trg->val.nodes, trg->size * sizeof *trg->val.nodes); |
| 1887 | LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx), LY_EMEM); |
| 1888 | } |
| 1889 | |
| 1890 | i = 0; |
| 1891 | j = 0; |
| 1892 | count = 0; |
| 1893 | dup_count = 0; |
| 1894 | do { |
| 1895 | cmp = set_sort_compare(&src->val.nodes[i], &trg->val.nodes[j]); |
| 1896 | if (!cmp) { |
| 1897 | if (!count) { |
| 1898 | /* duplicate, just skip it */ |
| 1899 | ++i; |
| 1900 | ++j; |
| 1901 | } else { |
| 1902 | /* we are copying something already, so let's copy the duplicate too, |
| 1903 | * we are hoping that afterwards there are some more nodes to |
| 1904 | * copy and this way we can copy them all together */ |
| 1905 | ++count; |
| 1906 | ++dup_count; |
| 1907 | ++i; |
| 1908 | ++j; |
| 1909 | } |
| 1910 | } else if (cmp < 0) { |
| 1911 | /* inserting src node into trg, just remember it for now */ |
| 1912 | ++count; |
| 1913 | ++i; |
| 1914 | |
| 1915 | /* insert the hash now */ |
| 1916 | set_insert_node_hash(trg, src->val.nodes[i - 1].node, src->val.nodes[i - 1].type); |
| 1917 | } else if (count) { |
| 1918 | copy_nodes: |
| 1919 | /* time to actually copy the nodes, we have found the largest block of nodes */ |
| 1920 | memmove(&trg->val.nodes[j + (count - dup_count)], |
| 1921 | &trg->val.nodes[j], |
| 1922 | (trg->used - j) * sizeof *trg->val.nodes); |
| 1923 | memcpy(&trg->val.nodes[j - dup_count], &src->val.nodes[i - count], count * sizeof *src->val.nodes); |
| 1924 | |
| 1925 | trg->used += count - dup_count; |
| 1926 | /* do not change i, except the copying above, we are basically doing exactly what is in the else branch below */ |
| 1927 | j += count - dup_count; |
| 1928 | |
| 1929 | count = 0; |
| 1930 | dup_count = 0; |
| 1931 | } else { |
| 1932 | ++j; |
| 1933 | } |
| 1934 | } while ((i < src->used) && (j < trg->used)); |
| 1935 | |
| 1936 | if ((i < src->used) || count) { |
| 1937 | /* insert all the hashes first */ |
| 1938 | for (k = i; k < src->used; ++k) { |
| 1939 | set_insert_node_hash(trg, src->val.nodes[k].node, src->val.nodes[k].type); |
| 1940 | } |
| 1941 | |
| 1942 | /* loop ended, but we need to copy something at trg end */ |
| 1943 | count += src->used - i; |
| 1944 | i = src->used; |
| 1945 | goto copy_nodes; |
| 1946 | } |
| 1947 | |
| 1948 | /* we are inserting hashes before the actual node insert, which causes |
| 1949 | * situations when there were initially not enough items for a hash table, |
| 1950 | * but even after some were inserted, hash table was not created (during |
| 1951 | * insertion the number of items is not updated yet) */ |
| 1952 | if (!trg->ht && (trg->used >= LYD_HT_MIN_ITEMS)) { |
| 1953 | set_insert_node_hash(trg, NULL, 0); |
| 1954 | } |
| 1955 | |
| 1956 | #ifndef NDEBUG |
| 1957 | LOGDBG(LY_LDGXPATH, "MERGE result"); |
| 1958 | print_set_debug(trg); |
| 1959 | #endif |
| 1960 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1961 | lyxp_set_free_content(src); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1962 | return LY_SUCCESS; |
| 1963 | } |
| 1964 | |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1965 | LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 1966 | 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] | 1967 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1968 | if (exp->used == tok_idx) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1969 | if (ctx) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1970 | LOGVAL(ctx, LY_VCODE_XP_EOF); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1971 | } |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1972 | return LY_EINCOMPLETE; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1973 | } |
| 1974 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1975 | if (want_tok && (exp->tokens[tok_idx] != want_tok)) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1976 | if (ctx) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 1977 | LOGVAL(ctx, LY_VCODE_XP_INTOK2, lyxp_token2str(exp->tokens[tok_idx]), |
| 1978 | &exp->expr[exp->tok_pos[tok_idx]], lyxp_token2str(want_tok)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1979 | } |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1980 | return LY_ENOT; |
| 1981 | } |
| 1982 | |
| 1983 | return LY_SUCCESS; |
| 1984 | } |
| 1985 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1986 | LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 1987 | 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] | 1988 | { |
| 1989 | LY_CHECK_RET(lyxp_check_token(ctx, exp, *tok_idx, want_tok)); |
| 1990 | |
| 1991 | /* skip the token */ |
| 1992 | ++(*tok_idx); |
| 1993 | |
| 1994 | return LY_SUCCESS; |
| 1995 | } |
| 1996 | |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1997 | /* just like lyxp_check_token() but tests for 2 tokens */ |
| 1998 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 1999 | 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] | 2000 | enum lyxp_token want_tok2) |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 2001 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2002 | if (exp->used == tok_idx) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 2003 | if (ctx) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2004 | LOGVAL(ctx, LY_VCODE_XP_EOF); |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 2005 | } |
| 2006 | return LY_EINCOMPLETE; |
| 2007 | } |
| 2008 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2009 | 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] | 2010 | if (ctx) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2011 | LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_token2str(exp->tokens[tok_idx]), |
Michal Vasko | 0b468e6 | 2020-10-19 09:33:04 +0200 | [diff] [blame] | 2012 | &exp->expr[exp->tok_pos[tok_idx]]); |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 2013 | } |
| 2014 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2015 | } |
| 2016 | |
| 2017 | return LY_SUCCESS; |
| 2018 | } |
| 2019 | |
Michal Vasko | 4911eeb | 2021-06-28 11:23:05 +0200 | [diff] [blame] | 2020 | LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2021 | 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] | 2022 | enum lyxp_token want_tok2) |
| 2023 | { |
| 2024 | LY_CHECK_RET(exp_check_token2(ctx, exp, *tok_idx, want_tok1, want_tok2)); |
| 2025 | |
| 2026 | /* skip the token */ |
| 2027 | ++(*tok_idx); |
| 2028 | |
| 2029 | return LY_SUCCESS; |
| 2030 | } |
| 2031 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2032 | /** |
| 2033 | * @brief Stack operation push on the repeat array. |
| 2034 | * |
| 2035 | * @param[in] exp Expression to use. |
Michal Vasko | 831e3bd | 2023-04-24 10:43:38 +0200 | [diff] [blame] | 2036 | * @param[in] tok_idx Position in the expresion @p exp. |
| 2037 | * @param[in] repeat_expr_type Repeated expression type, this value is pushed. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2038 | */ |
| 2039 | static void |
Michal Vasko | 831e3bd | 2023-04-24 10:43:38 +0200 | [diff] [blame] | 2040 | 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] | 2041 | { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2042 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2043 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2044 | if (exp->repeat[tok_idx]) { |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 2045 | for (i = 0; exp->repeat[tok_idx][i]; ++i) {} |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2046 | exp->repeat[tok_idx] = realloc(exp->repeat[tok_idx], (i + 2) * sizeof *exp->repeat[tok_idx]); |
| 2047 | LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), ); |
Michal Vasko | 831e3bd | 2023-04-24 10:43:38 +0200 | [diff] [blame] | 2048 | exp->repeat[tok_idx][i] = repeat_expr_type; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2049 | exp->repeat[tok_idx][i + 1] = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2050 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2051 | exp->repeat[tok_idx] = calloc(2, sizeof *exp->repeat[tok_idx]); |
| 2052 | LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), ); |
Michal Vasko | 831e3bd | 2023-04-24 10:43:38 +0200 | [diff] [blame] | 2053 | exp->repeat[tok_idx][0] = repeat_expr_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2054 | } |
| 2055 | } |
| 2056 | |
| 2057 | /** |
| 2058 | * @brief Reparse Predicate. Logs directly on error. |
| 2059 | * |
| 2060 | * [7] Predicate ::= '[' Expr ']' |
| 2061 | * |
| 2062 | * @param[in] ctx Context for logging. |
| 2063 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2064 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2065 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2066 | * @return LY_ERR |
| 2067 | */ |
| 2068 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2069 | 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] | 2070 | { |
| 2071 | LY_ERR rc; |
| 2072 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2073 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2074 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2075 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2076 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2077 | rc = reparse_or_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2078 | LY_CHECK_RET(rc); |
| 2079 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2080 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2081 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2082 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2083 | |
| 2084 | return LY_SUCCESS; |
| 2085 | } |
| 2086 | |
| 2087 | /** |
| 2088 | * @brief Reparse RelativeLocationPath. Logs directly on error. |
| 2089 | * |
| 2090 | * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step |
| 2091 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 2092 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 2093 | * |
| 2094 | * @param[in] ctx Context for logging. |
| 2095 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2096 | * @param[in] tok_idx Position in the expression \p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2097 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2098 | * @return LY_ERR (LY_EINCOMPLETE on forward reference) |
| 2099 | */ |
| 2100 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2101 | 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] | 2102 | { |
| 2103 | LY_ERR rc; |
| 2104 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2105 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2106 | LY_CHECK_RET(rc); |
| 2107 | |
| 2108 | goto step; |
| 2109 | do { |
| 2110 | /* '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2111 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2112 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2113 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2114 | LY_CHECK_RET(rc); |
| 2115 | step: |
| 2116 | /* Step */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2117 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2118 | case LYXP_TOKEN_DOT: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2119 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2120 | break; |
| 2121 | |
| 2122 | case LYXP_TOKEN_DDOT: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2123 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2124 | break; |
| 2125 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2126 | case LYXP_TOKEN_AXISNAME: |
| 2127 | ++(*tok_idx); |
| 2128 | |
| 2129 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_DCOLON); |
| 2130 | LY_CHECK_RET(rc); |
| 2131 | |
| 2132 | /* fall through */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2133 | case LYXP_TOKEN_AT: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2134 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2135 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2136 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2137 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2138 | 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] | 2139 | 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] | 2140 | return LY_EVALID; |
| 2141 | } |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2142 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_NODETYPE) { |
| 2143 | goto reparse_nodetype; |
| 2144 | } |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 2145 | /* fall through */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2146 | case LYXP_TOKEN_NAMETEST: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2147 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2148 | goto reparse_predicate; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2149 | |
| 2150 | case LYXP_TOKEN_NODETYPE: |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2151 | reparse_nodetype: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2152 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2153 | |
| 2154 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2155 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2156 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2157 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2158 | |
| 2159 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2160 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2161 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2162 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2163 | |
| 2164 | reparse_predicate: |
| 2165 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2166 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2167 | rc = reparse_predicate(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2168 | LY_CHECK_RET(rc); |
| 2169 | } |
| 2170 | break; |
| 2171 | default: |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2172 | 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] | 2173 | return LY_EVALID; |
| 2174 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2175 | } 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] | 2176 | |
| 2177 | return LY_SUCCESS; |
| 2178 | } |
| 2179 | |
| 2180 | /** |
| 2181 | * @brief Reparse AbsoluteLocationPath. Logs directly on error. |
| 2182 | * |
| 2183 | * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath |
| 2184 | * |
| 2185 | * @param[in] ctx Context for logging. |
| 2186 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2187 | * @param[in] tok_idx Position in the expression \p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2188 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2189 | * @return LY_ERR |
| 2190 | */ |
| 2191 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2192 | 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] | 2193 | { |
| 2194 | LY_ERR rc; |
| 2195 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2196 | 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] | 2197 | |
| 2198 | /* '/' RelativeLocationPath? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2199 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2200 | /* '/' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2201 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2202 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2203 | if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2204 | return LY_SUCCESS; |
| 2205 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2206 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2207 | case LYXP_TOKEN_DOT: |
| 2208 | case LYXP_TOKEN_DDOT: |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2209 | case LYXP_TOKEN_AXISNAME: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2210 | case LYXP_TOKEN_AT: |
| 2211 | case LYXP_TOKEN_NAMETEST: |
| 2212 | case LYXP_TOKEN_NODETYPE: |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2213 | rc = reparse_relative_location_path(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2214 | LY_CHECK_RET(rc); |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 2215 | /* fall through */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2216 | default: |
| 2217 | break; |
| 2218 | } |
| 2219 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2220 | } else { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 2221 | /* '//' RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2222 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2223 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2224 | rc = reparse_relative_location_path(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2225 | LY_CHECK_RET(rc); |
| 2226 | } |
| 2227 | |
| 2228 | return LY_SUCCESS; |
| 2229 | } |
| 2230 | |
| 2231 | /** |
| 2232 | * @brief Reparse FunctionCall. Logs directly on error. |
| 2233 | * |
| 2234 | * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')' |
| 2235 | * |
| 2236 | * @param[in] ctx Context for logging. |
| 2237 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2238 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2239 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2240 | * @return LY_ERR |
| 2241 | */ |
| 2242 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2243 | 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] | 2244 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2245 | int8_t min_arg_count = -1; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2246 | uint32_t arg_count, max_arg_count = 0, func_tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2247 | LY_ERR rc; |
| 2248 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2249 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2250 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2251 | func_tok_idx = *tok_idx; |
| 2252 | switch (exp->tok_len[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2253 | case 3: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2254 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2255 | min_arg_count = 1; |
| 2256 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2257 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2258 | min_arg_count = 1; |
| 2259 | max_arg_count = 1; |
| 2260 | } |
| 2261 | break; |
| 2262 | case 4: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2263 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2264 | min_arg_count = 1; |
| 2265 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2266 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2267 | min_arg_count = 0; |
| 2268 | max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2269 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2270 | min_arg_count = 0; |
| 2271 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2272 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2273 | min_arg_count = 0; |
| 2274 | max_arg_count = 0; |
| 2275 | } |
| 2276 | break; |
| 2277 | case 5: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2278 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2279 | min_arg_count = 1; |
| 2280 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2281 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2282 | min_arg_count = 0; |
| 2283 | max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2284 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 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]], "round", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2288 | min_arg_count = 1; |
| 2289 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2290 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2291 | min_arg_count = 1; |
| 2292 | max_arg_count = 1; |
| 2293 | } |
| 2294 | break; |
| 2295 | case 6: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2296 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2297 | min_arg_count = 2; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2298 | max_arg_count = UINT32_MAX; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2299 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2300 | min_arg_count = 0; |
| 2301 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2302 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2303 | min_arg_count = 0; |
| 2304 | max_arg_count = 1; |
| 2305 | } |
| 2306 | break; |
| 2307 | case 7: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2308 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 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]], "ceiling", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2312 | min_arg_count = 1; |
| 2313 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2314 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2315 | min_arg_count = 0; |
| 2316 | max_arg_count = 0; |
| 2317 | } |
| 2318 | break; |
| 2319 | case 8: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2320 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2321 | min_arg_count = 2; |
| 2322 | max_arg_count = 2; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2323 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2324 | min_arg_count = 0; |
| 2325 | max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2326 | } 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] | 2327 | min_arg_count = 2; |
| 2328 | max_arg_count = 2; |
| 2329 | } |
| 2330 | break; |
| 2331 | case 9: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2332 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2333 | min_arg_count = 2; |
| 2334 | max_arg_count = 3; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2335 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2336 | min_arg_count = 3; |
| 2337 | max_arg_count = 3; |
| 2338 | } |
| 2339 | break; |
| 2340 | case 10: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2341 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2342 | min_arg_count = 0; |
| 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]], "enum-value", 10)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2345 | min_arg_count = 1; |
| 2346 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2347 | } 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] | 2348 | min_arg_count = 2; |
| 2349 | max_arg_count = 2; |
| 2350 | } |
| 2351 | break; |
| 2352 | case 11: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2353 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2354 | min_arg_count = 2; |
| 2355 | max_arg_count = 2; |
| 2356 | } |
| 2357 | break; |
| 2358 | case 12: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2359 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2360 | min_arg_count = 2; |
| 2361 | max_arg_count = 2; |
| 2362 | } |
| 2363 | break; |
| 2364 | case 13: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2365 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2366 | min_arg_count = 0; |
| 2367 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2368 | } 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] | 2369 | min_arg_count = 0; |
| 2370 | max_arg_count = 1; |
| 2371 | } |
| 2372 | break; |
| 2373 | case 15: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2374 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2375 | min_arg_count = 0; |
| 2376 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2377 | } 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] | 2378 | min_arg_count = 2; |
| 2379 | max_arg_count = 2; |
| 2380 | } |
| 2381 | break; |
| 2382 | case 16: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2383 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2384 | min_arg_count = 2; |
| 2385 | max_arg_count = 2; |
| 2386 | } |
| 2387 | break; |
| 2388 | case 20: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2389 | 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] | 2390 | min_arg_count = 2; |
| 2391 | max_arg_count = 2; |
| 2392 | } |
| 2393 | break; |
| 2394 | } |
| 2395 | if (min_arg_count == -1) { |
Michal Vasko | 21eaa39 | 2024-02-20 15:48:42 +0100 | [diff] [blame] | 2396 | LOGVAL(ctx, LY_VCODE_XP_INFUNC, (int)exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2397 | return LY_EINVAL; |
| 2398 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2399 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2400 | |
| 2401 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2402 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2403 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2404 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2405 | |
| 2406 | /* ( Expr ( ',' Expr )* )? */ |
| 2407 | arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2408 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2409 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2410 | if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2411 | ++arg_count; |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2412 | rc = reparse_or_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2413 | LY_CHECK_RET(rc); |
| 2414 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2415 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) { |
| 2416 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2417 | |
| 2418 | ++arg_count; |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2419 | rc = reparse_or_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2420 | LY_CHECK_RET(rc); |
| 2421 | } |
| 2422 | |
| 2423 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2424 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2425 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2426 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2427 | |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 2428 | if ((arg_count < (uint32_t)min_arg_count) || (arg_count > max_arg_count)) { |
Michal Vasko | 21eaa39 | 2024-02-20 15:48:42 +0100 | [diff] [blame] | 2429 | LOGVAL(ctx, LY_VCODE_XP_INARGCOUNT, arg_count, (int)exp->tok_len[func_tok_idx], |
| 2430 | &exp->expr[exp->tok_pos[func_tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2431 | return LY_EVALID; |
| 2432 | } |
| 2433 | |
| 2434 | return LY_SUCCESS; |
| 2435 | } |
| 2436 | |
| 2437 | /** |
| 2438 | * @brief Reparse PathExpr. Logs directly on error. |
| 2439 | * |
| 2440 | * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate* |
| 2441 | * | PrimaryExpr Predicate* '/' RelativeLocationPath |
| 2442 | * | PrimaryExpr Predicate* '//' RelativeLocationPath |
| 2443 | * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 2444 | * [8] PrimaryExpr ::= VariableReference | '(' Expr ')' | Literal | Number | FunctionCall |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2445 | * |
| 2446 | * @param[in] ctx Context for logging. |
| 2447 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2448 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2449 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2450 | * @return LY_ERR |
| 2451 | */ |
| 2452 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2453 | 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] | 2454 | { |
| 2455 | LY_ERR rc; |
| 2456 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2457 | if (lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 2458 | return LY_EVALID; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2459 | } |
| 2460 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2461 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2462 | case LYXP_TOKEN_PAR1: |
| 2463 | /* '(' Expr ')' Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2464 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2465 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2466 | rc = reparse_or_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2467 | LY_CHECK_RET(rc); |
| 2468 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2469 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2470 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2471 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2472 | goto predicate; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2473 | case LYXP_TOKEN_DOT: |
| 2474 | case LYXP_TOKEN_DDOT: |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2475 | case LYXP_TOKEN_AXISNAME: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2476 | case LYXP_TOKEN_AT: |
| 2477 | case LYXP_TOKEN_NAMETEST: |
| 2478 | case LYXP_TOKEN_NODETYPE: |
| 2479 | /* RelativeLocationPath */ |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2480 | rc = reparse_relative_location_path(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2481 | LY_CHECK_RET(rc); |
| 2482 | break; |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 2483 | case LYXP_TOKEN_VARREF: |
| 2484 | /* VariableReference */ |
| 2485 | ++(*tok_idx); |
| 2486 | goto predicate; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2487 | case LYXP_TOKEN_FUNCNAME: |
| 2488 | /* FunctionCall */ |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2489 | rc = reparse_function_call(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2490 | LY_CHECK_RET(rc); |
| 2491 | goto predicate; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2492 | case LYXP_TOKEN_OPER_PATH: |
| 2493 | case LYXP_TOKEN_OPER_RPATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2494 | /* AbsoluteLocationPath */ |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2495 | rc = reparse_absolute_location_path(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2496 | LY_CHECK_RET(rc); |
| 2497 | break; |
| 2498 | case LYXP_TOKEN_LITERAL: |
| 2499 | /* Literal */ |
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 | case LYXP_TOKEN_NUMBER: |
| 2503 | /* Number */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2504 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2505 | goto predicate; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2506 | default: |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2507 | 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] | 2508 | return LY_EVALID; |
| 2509 | } |
| 2510 | |
| 2511 | return LY_SUCCESS; |
| 2512 | |
| 2513 | predicate: |
| 2514 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2515 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2516 | rc = reparse_predicate(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2517 | LY_CHECK_RET(rc); |
| 2518 | } |
| 2519 | |
| 2520 | /* ('/' or '//') RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2521 | 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] | 2522 | |
| 2523 | /* '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2524 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2525 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2526 | rc = reparse_relative_location_path(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2527 | LY_CHECK_RET(rc); |
| 2528 | } |
| 2529 | |
| 2530 | return LY_SUCCESS; |
| 2531 | } |
| 2532 | |
| 2533 | /** |
| 2534 | * @brief Reparse UnaryExpr. Logs directly on error. |
| 2535 | * |
| 2536 | * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr |
| 2537 | * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr |
| 2538 | * |
| 2539 | * @param[in] ctx Context for logging. |
| 2540 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2541 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2542 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2543 | * @return LY_ERR |
| 2544 | */ |
| 2545 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2546 | 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] | 2547 | { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2548 | uint32_t prev_exp; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2549 | LY_ERR rc; |
| 2550 | |
| 2551 | /* ('-')* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2552 | prev_exp = *tok_idx; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2553 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) && |
| 2554 | (exp->expr[exp->tok_pos[*tok_idx]] == '-')) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2555 | exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2556 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2557 | } |
| 2558 | |
| 2559 | /* PathExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2560 | prev_exp = *tok_idx; |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2561 | rc = reparse_path_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2562 | LY_CHECK_RET(rc); |
| 2563 | |
| 2564 | /* ('|' PathExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2565 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_UNI)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2566 | exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2567 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2568 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2569 | rc = reparse_path_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2570 | LY_CHECK_RET(rc); |
| 2571 | } |
| 2572 | |
| 2573 | return LY_SUCCESS; |
| 2574 | } |
| 2575 | |
| 2576 | /** |
| 2577 | * @brief Reparse AdditiveExpr. Logs directly on error. |
| 2578 | * |
| 2579 | * [15] AdditiveExpr ::= MultiplicativeExpr |
| 2580 | * | AdditiveExpr '+' MultiplicativeExpr |
| 2581 | * | AdditiveExpr '-' MultiplicativeExpr |
| 2582 | * [16] MultiplicativeExpr ::= UnaryExpr |
| 2583 | * | MultiplicativeExpr '*' UnaryExpr |
| 2584 | * | MultiplicativeExpr 'div' UnaryExpr |
| 2585 | * | MultiplicativeExpr 'mod' UnaryExpr |
| 2586 | * |
| 2587 | * @param[in] ctx Context for logging. |
| 2588 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2589 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2590 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2591 | * @return LY_ERR |
| 2592 | */ |
| 2593 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2594 | 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] | 2595 | { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2596 | uint32_t prev_add_exp, prev_mul_exp; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2597 | LY_ERR rc; |
| 2598 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2599 | prev_add_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2600 | goto reparse_multiplicative_expr; |
| 2601 | |
| 2602 | /* ('+' / '-' MultiplicativeExpr)* */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2603 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) && |
| 2604 | ((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] | 2605 | exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2606 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2607 | |
| 2608 | reparse_multiplicative_expr: |
| 2609 | /* UnaryExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2610 | prev_mul_exp = *tok_idx; |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2611 | rc = reparse_unary_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2612 | LY_CHECK_RET(rc); |
| 2613 | |
| 2614 | /* ('*' / 'div' / 'mod' UnaryExpr)* */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2615 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) && |
| 2616 | ((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] | 2617 | exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2618 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2619 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2620 | rc = reparse_unary_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2621 | LY_CHECK_RET(rc); |
| 2622 | } |
| 2623 | } |
| 2624 | |
| 2625 | return LY_SUCCESS; |
| 2626 | } |
| 2627 | |
| 2628 | /** |
| 2629 | * @brief Reparse EqualityExpr. Logs directly on error. |
| 2630 | * |
| 2631 | * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr |
| 2632 | * | EqualityExpr '!=' RelationalExpr |
| 2633 | * [14] RelationalExpr ::= AdditiveExpr |
| 2634 | * | RelationalExpr '<' AdditiveExpr |
| 2635 | * | RelationalExpr '>' AdditiveExpr |
| 2636 | * | RelationalExpr '<=' AdditiveExpr |
| 2637 | * | RelationalExpr '>=' AdditiveExpr |
| 2638 | * |
| 2639 | * @param[in] ctx Context for logging. |
| 2640 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2641 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2642 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2643 | * @return LY_ERR |
| 2644 | */ |
| 2645 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2646 | 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] | 2647 | { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2648 | uint32_t prev_eq_exp, prev_rel_exp; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2649 | LY_ERR rc; |
| 2650 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2651 | prev_eq_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2652 | goto reparse_additive_expr; |
| 2653 | |
| 2654 | /* ('=' / '!=' RelationalExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2655 | 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] | 2656 | exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2657 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2658 | |
| 2659 | reparse_additive_expr: |
| 2660 | /* AdditiveExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2661 | prev_rel_exp = *tok_idx; |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2662 | rc = reparse_additive_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2663 | LY_CHECK_RET(rc); |
| 2664 | |
| 2665 | /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2666 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_COMP)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2667 | exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2668 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2669 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2670 | rc = reparse_additive_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2671 | LY_CHECK_RET(rc); |
| 2672 | } |
| 2673 | } |
| 2674 | |
| 2675 | return LY_SUCCESS; |
| 2676 | } |
| 2677 | |
| 2678 | /** |
| 2679 | * @brief Reparse OrExpr. Logs directly on error. |
| 2680 | * |
| 2681 | * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr |
| 2682 | * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr |
| 2683 | * |
| 2684 | * @param[in] ctx Context for logging. |
| 2685 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2686 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2687 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2688 | * @return LY_ERR |
| 2689 | */ |
| 2690 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2691 | 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] | 2692 | { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2693 | uint32_t prev_or_exp, prev_and_exp; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2694 | LY_ERR rc; |
| 2695 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2696 | ++depth; |
| 2697 | LY_CHECK_ERR_RET(depth > LYXP_MAX_BLOCK_DEPTH, LOGVAL(ctx, LY_VCODE_XP_DEPTH), LY_EINVAL); |
| 2698 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2699 | prev_or_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2700 | goto reparse_equality_expr; |
| 2701 | |
| 2702 | /* ('or' AndExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2703 | 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] | 2704 | exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2705 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2706 | |
| 2707 | reparse_equality_expr: |
| 2708 | /* EqualityExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2709 | prev_and_exp = *tok_idx; |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2710 | rc = reparse_equality_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2711 | LY_CHECK_RET(rc); |
| 2712 | |
| 2713 | /* ('and' EqualityExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2714 | 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] | 2715 | exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2716 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2717 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2718 | rc = reparse_equality_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2719 | LY_CHECK_RET(rc); |
| 2720 | } |
| 2721 | } |
| 2722 | |
| 2723 | return LY_SUCCESS; |
| 2724 | } |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2725 | |
| 2726 | /** |
| 2727 | * @brief Parse NCName. |
| 2728 | * |
| 2729 | * @param[in] ncname Name to parse. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2730 | * @return Length of @p ncname valid bytes. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2731 | */ |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2732 | static ssize_t |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2733 | parse_ncname(const char *ncname) |
| 2734 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2735 | uint32_t uc; |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2736 | size_t size; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2737 | ssize_t len = 0; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2738 | |
| 2739 | LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0); |
| 2740 | if (!is_xmlqnamestartchar(uc) || (uc == ':')) { |
| 2741 | return len; |
| 2742 | } |
| 2743 | |
| 2744 | do { |
| 2745 | len += size; |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 2746 | if (!*ncname) { |
| 2747 | break; |
| 2748 | } |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2749 | LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2750 | } while (is_xmlqnamechar(uc) && (uc != ':')); |
| 2751 | |
| 2752 | return len; |
| 2753 | } |
| 2754 | |
| 2755 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2756 | * @brief Add @p token into the expression @p exp. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2757 | * |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2758 | * @param[in] ctx Context for logging. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2759 | * @param[in] exp Expression to use. |
| 2760 | * @param[in] token Token to add. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2761 | * @param[in] tok_pos Token position in the XPath expression. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2762 | * @param[in] tok_len Token length in the XPath expression. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2763 | * @return LY_ERR |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2764 | */ |
| 2765 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2766 | 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] | 2767 | { |
| 2768 | uint32_t prev; |
| 2769 | |
| 2770 | if (exp->used == exp->size) { |
| 2771 | prev = exp->size; |
| 2772 | exp->size += LYXP_EXPR_SIZE_STEP; |
| 2773 | if (prev > exp->size) { |
| 2774 | LOGINT(ctx); |
| 2775 | return LY_EINT; |
| 2776 | } |
| 2777 | |
| 2778 | exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens); |
| 2779 | LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM); |
| 2780 | exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos); |
| 2781 | LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM); |
| 2782 | exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len); |
| 2783 | LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM); |
| 2784 | } |
| 2785 | |
| 2786 | exp->tokens[exp->used] = token; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2787 | exp->tok_pos[exp->used] = tok_pos; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2788 | exp->tok_len[exp->used] = tok_len; |
| 2789 | ++exp->used; |
| 2790 | return LY_SUCCESS; |
| 2791 | } |
| 2792 | |
| 2793 | void |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 2794 | lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr) |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2795 | { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2796 | uint32_t i; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2797 | |
| 2798 | if (!expr) { |
| 2799 | return; |
| 2800 | } |
| 2801 | |
| 2802 | lydict_remove(ctx, expr->expr); |
| 2803 | free(expr->tokens); |
| 2804 | free(expr->tok_pos); |
| 2805 | free(expr->tok_len); |
| 2806 | if (expr->repeat) { |
| 2807 | for (i = 0; i < expr->used; ++i) { |
| 2808 | free(expr->repeat[i]); |
| 2809 | } |
| 2810 | } |
| 2811 | free(expr->repeat); |
| 2812 | free(expr); |
| 2813 | } |
| 2814 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2815 | /** |
| 2816 | * @brief Parse Axis name. |
| 2817 | * |
| 2818 | * @param[in] str String to parse. |
| 2819 | * @param[in] str_len Length of @p str. |
| 2820 | * @return LY_SUCCESS if an axis. |
| 2821 | * @return LY_ENOT otherwise. |
| 2822 | */ |
| 2823 | static LY_ERR |
| 2824 | expr_parse_axis(const char *str, size_t str_len) |
| 2825 | { |
| 2826 | switch (str_len) { |
| 2827 | case 4: |
| 2828 | if (!strncmp("self", str, str_len)) { |
| 2829 | return LY_SUCCESS; |
| 2830 | } |
| 2831 | break; |
| 2832 | case 5: |
| 2833 | if (!strncmp("child", str, str_len)) { |
| 2834 | return LY_SUCCESS; |
| 2835 | } |
| 2836 | break; |
| 2837 | case 6: |
| 2838 | if (!strncmp("parent", str, str_len)) { |
| 2839 | return LY_SUCCESS; |
| 2840 | } |
| 2841 | break; |
| 2842 | case 8: |
| 2843 | if (!strncmp("ancestor", str, str_len)) { |
| 2844 | return LY_SUCCESS; |
| 2845 | } |
| 2846 | break; |
| 2847 | case 9: |
| 2848 | if (!strncmp("attribute", str, str_len)) { |
| 2849 | return LY_SUCCESS; |
| 2850 | } else if (!strncmp("following", str, str_len)) { |
| 2851 | return LY_SUCCESS; |
| 2852 | } else if (!strncmp("namespace", str, str_len)) { |
| 2853 | LOGERR(NULL, LY_EINVAL, "Axis \"namespace\" not supported."); |
| 2854 | return LY_ENOT; |
| 2855 | } else if (!strncmp("preceding", str, str_len)) { |
| 2856 | return LY_SUCCESS; |
| 2857 | } |
| 2858 | break; |
| 2859 | case 10: |
| 2860 | if (!strncmp("descendant", str, str_len)) { |
| 2861 | return LY_SUCCESS; |
| 2862 | } |
| 2863 | break; |
| 2864 | case 16: |
| 2865 | if (!strncmp("ancestor-or-self", str, str_len)) { |
| 2866 | return LY_SUCCESS; |
| 2867 | } |
| 2868 | break; |
| 2869 | case 17: |
| 2870 | if (!strncmp("following-sibling", str, str_len)) { |
| 2871 | return LY_SUCCESS; |
| 2872 | } else if (!strncmp("preceding-sibling", str, str_len)) { |
| 2873 | return LY_SUCCESS; |
| 2874 | } |
| 2875 | break; |
| 2876 | case 18: |
| 2877 | if (!strncmp("descendant-or-self", str, str_len)) { |
| 2878 | return LY_SUCCESS; |
| 2879 | } |
| 2880 | break; |
| 2881 | } |
| 2882 | |
| 2883 | return LY_ENOT; |
| 2884 | } |
| 2885 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2886 | LY_ERR |
| 2887 | 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] | 2888 | { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2889 | LY_ERR ret = LY_SUCCESS; |
| 2890 | struct lyxp_expr *expr; |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2891 | size_t parsed = 0, tok_len; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2892 | enum lyxp_token tok_type; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2893 | ly_bool prev_func_check = 0, prev_ntype_check = 0, has_axis; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2894 | uint32_t tok_idx = 0; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2895 | ssize_t ncname_len; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2896 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2897 | assert(expr_p); |
| 2898 | |
| 2899 | if (!expr_str[0]) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2900 | LOGVAL(ctx, LY_VCODE_XP_EOF); |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2901 | return LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2902 | } |
| 2903 | |
| 2904 | if (!expr_len) { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2905 | expr_len = strlen(expr_str); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2906 | } |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 2907 | if (expr_len > UINT32_MAX) { |
| 2908 | 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] | 2909 | return LY_EVALID; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2910 | } |
| 2911 | |
| 2912 | /* init lyxp_expr structure */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2913 | expr = calloc(1, sizeof *expr); |
| 2914 | LY_CHECK_ERR_GOTO(!expr, LOGMEM(ctx); ret = LY_EMEM, error); |
| 2915 | LY_CHECK_GOTO(ret = lydict_insert(ctx, expr_str, expr_len, &expr->expr), error); |
| 2916 | expr->used = 0; |
| 2917 | expr->size = LYXP_EXPR_SIZE_START; |
| 2918 | expr->tokens = malloc(expr->size * sizeof *expr->tokens); |
| 2919 | LY_CHECK_ERR_GOTO(!expr->tokens, LOGMEM(ctx); ret = LY_EMEM, error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2920 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2921 | expr->tok_pos = malloc(expr->size * sizeof *expr->tok_pos); |
| 2922 | 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] | 2923 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2924 | expr->tok_len = malloc(expr->size * sizeof *expr->tok_len); |
| 2925 | 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] | 2926 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2927 | /* make expr 0-terminated */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2928 | expr_str = expr->expr; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2929 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2930 | while (is_xmlws(expr_str[parsed])) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2931 | ++parsed; |
| 2932 | } |
| 2933 | |
| 2934 | do { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2935 | if (expr_str[parsed] == '(') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2936 | |
| 2937 | /* '(' */ |
| 2938 | tok_len = 1; |
| 2939 | tok_type = LYXP_TOKEN_PAR1; |
| 2940 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 2941 | if (prev_ntype_check && expr->used && (expr->tokens[expr->used - 1] == LYXP_TOKEN_NAMETEST) && |
| 2942 | (((expr->tok_len[expr->used - 1] == 4) && |
| 2943 | (!strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "node", 4) || |
| 2944 | !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "text", 4))) || |
| 2945 | ((expr->tok_len[expr->used - 1] == 7) && |
| 2946 | !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "comment", 7)))) { |
| 2947 | /* it is NodeType after all */ |
| 2948 | expr->tokens[expr->used - 1] = LYXP_TOKEN_NODETYPE; |
| 2949 | |
| 2950 | prev_ntype_check = 0; |
| 2951 | prev_func_check = 0; |
| 2952 | } else if (prev_func_check && expr->used && (expr->tokens[expr->used - 1] == LYXP_TOKEN_NAMETEST)) { |
| 2953 | /* it is FunctionName after all */ |
| 2954 | expr->tokens[expr->used - 1] = LYXP_TOKEN_FUNCNAME; |
| 2955 | |
| 2956 | prev_ntype_check = 0; |
| 2957 | prev_func_check = 0; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2958 | } |
| 2959 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2960 | } else if (expr_str[parsed] == ')') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2961 | |
| 2962 | /* ')' */ |
| 2963 | tok_len = 1; |
| 2964 | tok_type = LYXP_TOKEN_PAR2; |
| 2965 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2966 | } else if (expr_str[parsed] == '[') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2967 | |
| 2968 | /* '[' */ |
| 2969 | tok_len = 1; |
| 2970 | tok_type = LYXP_TOKEN_BRACK1; |
| 2971 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2972 | } else if (expr_str[parsed] == ']') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2973 | |
| 2974 | /* ']' */ |
| 2975 | tok_len = 1; |
| 2976 | tok_type = LYXP_TOKEN_BRACK2; |
| 2977 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2978 | } else if (!strncmp(&expr_str[parsed], "..", 2)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2979 | |
| 2980 | /* '..' */ |
| 2981 | tok_len = 2; |
| 2982 | tok_type = LYXP_TOKEN_DDOT; |
| 2983 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2984 | } else if ((expr_str[parsed] == '.') && (!isdigit(expr_str[parsed + 1]))) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2985 | |
| 2986 | /* '.' */ |
| 2987 | tok_len = 1; |
| 2988 | tok_type = LYXP_TOKEN_DOT; |
| 2989 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2990 | } else if (expr_str[parsed] == '@') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2991 | |
| 2992 | /* '@' */ |
| 2993 | tok_len = 1; |
| 2994 | tok_type = LYXP_TOKEN_AT; |
| 2995 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2996 | } else if (expr_str[parsed] == ',') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2997 | |
| 2998 | /* ',' */ |
| 2999 | tok_len = 1; |
| 3000 | tok_type = LYXP_TOKEN_COMMA; |
| 3001 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3002 | } else if (expr_str[parsed] == '\'') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3003 | |
| 3004 | /* Literal with ' */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3005 | for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\''); ++tok_len) {} |
| 3006 | LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0', |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 3007 | 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] | 3008 | error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3009 | ++tok_len; |
| 3010 | tok_type = LYXP_TOKEN_LITERAL; |
| 3011 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3012 | } else if (expr_str[parsed] == '\"') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3013 | |
| 3014 | /* Literal with " */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3015 | for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\"'); ++tok_len) {} |
| 3016 | LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0', |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 3017 | 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] | 3018 | error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3019 | ++tok_len; |
| 3020 | tok_type = LYXP_TOKEN_LITERAL; |
| 3021 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3022 | } else if ((expr_str[parsed] == '.') || (isdigit(expr_str[parsed]))) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3023 | |
| 3024 | /* Number */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3025 | for (tok_len = 0; isdigit(expr_str[parsed + tok_len]); ++tok_len) {} |
| 3026 | if (expr_str[parsed + tok_len] == '.') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3027 | ++tok_len; |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3028 | for ( ; isdigit(expr_str[parsed + tok_len]); ++tok_len) {} |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3029 | } |
| 3030 | tok_type = LYXP_TOKEN_NUMBER; |
| 3031 | |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 3032 | } else if (expr_str[parsed] == '$') { |
| 3033 | |
| 3034 | /* VariableReference */ |
| 3035 | parsed++; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 3036 | ncname_len = parse_ncname(&expr_str[parsed]); |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 3037 | 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] | 3038 | (uint32_t)(parsed - ncname_len + 1), expr_str); ret = LY_EVALID, error); |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 3039 | tok_len = ncname_len; |
| 3040 | LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == ':', |
| 3041 | LOGVAL(ctx, LYVE_XPATH, "Variable with prefix is not supported."); ret = LY_EVALID, |
| 3042 | error); |
| 3043 | tok_type = LYXP_TOKEN_VARREF; |
| 3044 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3045 | } else if (expr_str[parsed] == '/') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3046 | |
| 3047 | /* Operator '/', '//' */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3048 | if (!strncmp(&expr_str[parsed], "//", 2)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3049 | tok_len = 2; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3050 | tok_type = LYXP_TOKEN_OPER_RPATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3051 | } else { |
| 3052 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3053 | tok_type = LYXP_TOKEN_OPER_PATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3054 | } |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3055 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3056 | } else if (!strncmp(&expr_str[parsed], "!=", 2)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3057 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3058 | /* Operator '!=' */ |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3059 | tok_len = 2; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3060 | tok_type = LYXP_TOKEN_OPER_NEQUAL; |
| 3061 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3062 | } else if (!strncmp(&expr_str[parsed], "<=", 2) || !strncmp(&expr_str[parsed], ">=", 2)) { |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3063 | |
| 3064 | /* Operator '<=', '>=' */ |
| 3065 | tok_len = 2; |
| 3066 | tok_type = LYXP_TOKEN_OPER_COMP; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3067 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3068 | } else if (expr_str[parsed] == '|') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3069 | |
| 3070 | /* Operator '|' */ |
| 3071 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3072 | tok_type = LYXP_TOKEN_OPER_UNI; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3073 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3074 | } else if ((expr_str[parsed] == '+') || (expr_str[parsed] == '-')) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3075 | |
| 3076 | /* Operator '+', '-' */ |
| 3077 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3078 | tok_type = LYXP_TOKEN_OPER_MATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3079 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3080 | } else if (expr_str[parsed] == '=') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3081 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3082 | /* Operator '=' */ |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3083 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3084 | tok_type = LYXP_TOKEN_OPER_EQUAL; |
| 3085 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3086 | } else if ((expr_str[parsed] == '<') || (expr_str[parsed] == '>')) { |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3087 | |
| 3088 | /* Operator '<', '>' */ |
| 3089 | tok_len = 1; |
| 3090 | tok_type = LYXP_TOKEN_OPER_COMP; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3091 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3092 | } else if (expr->used && (expr->tokens[expr->used - 1] != LYXP_TOKEN_AT) && |
| 3093 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_PAR1) && |
| 3094 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_BRACK1) && |
| 3095 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_COMMA) && |
| 3096 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_LOG) && |
| 3097 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_EQUAL) && |
| 3098 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_NEQUAL) && |
| 3099 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_COMP) && |
| 3100 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_MATH) && |
| 3101 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_UNI) && |
| 3102 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_PATH) && |
| 3103 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_RPATH)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3104 | |
| 3105 | /* Operator '*', 'or', 'and', 'mod', or 'div' */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3106 | if (expr_str[parsed] == '*') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3107 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3108 | tok_type = LYXP_TOKEN_OPER_MATH; |
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], "or", 2)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3111 | tok_len = 2; |
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], "and", 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_LOG; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3117 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3118 | } 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] | 3119 | tok_len = 3; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 3120 | tok_type = LYXP_TOKEN_OPER_MATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3121 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 3122 | } else if (prev_ntype_check || prev_func_check) { |
Michal Vasko | 21eaa39 | 2024-02-20 15:48:42 +0100 | [diff] [blame] | 3123 | LOGVAL(ctx, LYVE_XPATH, |
| 3124 | "Invalid character 0x%x ('%c'), perhaps \"%.*s\" is supposed to be a function call.", |
| 3125 | expr_str[parsed], expr_str[parsed], (int)expr->tok_len[expr->used - 1], |
| 3126 | &expr->expr[expr->tok_pos[expr->used - 1]]); |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3127 | ret = LY_EVALID; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3128 | goto error; |
| 3129 | } else { |
Michal Vasko | 7b3a00e | 2023-08-09 11:58:03 +0200 | [diff] [blame] | 3130 | 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] | 3131 | ret = LY_EVALID; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3132 | goto error; |
| 3133 | } |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3134 | } else { |
| 3135 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 3136 | /* (AxisName '::')? ((NCName ':')? '*' | QName) or NodeType/FunctionName */ |
| 3137 | if (expr_str[parsed] == '*') { |
| 3138 | ncname_len = 1; |
| 3139 | } else { |
| 3140 | ncname_len = parse_ncname(&expr_str[parsed]); |
| 3141 | 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] | 3142 | (uint32_t)(parsed - ncname_len + 1), expr_str); ret = LY_EVALID, error); |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 3143 | } |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3144 | tok_len = ncname_len; |
| 3145 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 3146 | has_axis = 0; |
| 3147 | if (!strncmp(&expr_str[parsed + tok_len], "::", 2)) { |
| 3148 | /* axis */ |
| 3149 | LY_CHECK_ERR_GOTO(expr_parse_axis(&expr_str[parsed], ncname_len), |
Michal Vasko | 7b3a00e | 2023-08-09 11:58:03 +0200 | [diff] [blame] | 3150 | LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed], (uint32_t)(parsed + 1), expr_str); ret = LY_EVALID, |
| 3151 | error); |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 3152 | tok_type = LYXP_TOKEN_AXISNAME; |
| 3153 | |
| 3154 | LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error); |
| 3155 | parsed += tok_len; |
| 3156 | |
| 3157 | /* '::' */ |
| 3158 | tok_len = 2; |
| 3159 | tok_type = LYXP_TOKEN_DCOLON; |
| 3160 | |
| 3161 | LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error); |
| 3162 | parsed += tok_len; |
| 3163 | |
| 3164 | if (expr_str[parsed] == '*') { |
| 3165 | ncname_len = 1; |
| 3166 | } else { |
| 3167 | ncname_len = parse_ncname(&expr_str[parsed]); |
| 3168 | 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] | 3169 | (uint32_t)(parsed - ncname_len + 1), expr_str); ret = LY_EVALID, error); |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 3170 | } |
| 3171 | tok_len = ncname_len; |
| 3172 | |
| 3173 | has_axis = 1; |
| 3174 | } |
| 3175 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3176 | if (expr_str[parsed + tok_len] == ':') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3177 | ++tok_len; |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3178 | if (expr_str[parsed + tok_len] == '*') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3179 | ++tok_len; |
| 3180 | } else { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3181 | ncname_len = parse_ncname(&expr_str[parsed + tok_len]); |
Michal Vasko | e2be546 | 2021-08-04 10:49:42 +0200 | [diff] [blame] | 3182 | 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] | 3183 | (uint32_t)(parsed - ncname_len + 1), expr_str); ret = LY_EVALID, error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3184 | tok_len += ncname_len; |
| 3185 | } |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 3186 | /* remove old flags to prevent ambiguities */ |
| 3187 | prev_ntype_check = 0; |
| 3188 | prev_func_check = 0; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3189 | tok_type = LYXP_TOKEN_NAMETEST; |
| 3190 | } else { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 3191 | /* if not '*', there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */ |
| 3192 | prev_ntype_check = (expr_str[parsed] == '*') ? 0 : 1; |
| 3193 | prev_func_check = (prev_ntype_check && !has_axis) ? 1 : 0; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3194 | tok_type = LYXP_TOKEN_NAMETEST; |
| 3195 | } |
| 3196 | } |
| 3197 | |
| 3198 | /* store the token, move on to the next one */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3199 | 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] | 3200 | parsed += tok_len; |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3201 | while (is_xmlws(expr_str[parsed])) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3202 | ++parsed; |
| 3203 | } |
| 3204 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3205 | } while (expr_str[parsed]); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3206 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3207 | if (reparse) { |
| 3208 | /* prealloc repeat */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3209 | expr->repeat = calloc(expr->size, sizeof *expr->repeat); |
| 3210 | LY_CHECK_ERR_GOTO(!expr->repeat, LOGMEM(ctx); ret = LY_EMEM, error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3211 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3212 | /* fill repeat */ |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 3213 | 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] | 3214 | if (expr->used > tok_idx) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 3215 | 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] | 3216 | &expr->expr[expr->tok_pos[tok_idx]]); |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3217 | ret = LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3218 | goto error; |
| 3219 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3220 | } |
| 3221 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3222 | print_expr_struct_debug(expr); |
| 3223 | *expr_p = expr; |
| 3224 | return LY_SUCCESS; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3225 | |
| 3226 | error: |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 3227 | lyxp_expr_free(ctx, expr); |
| 3228 | return ret; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 3229 | } |
| 3230 | |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 3231 | LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 3232 | 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] | 3233 | struct lyxp_expr **dup_p) |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3234 | { |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 3235 | LY_ERR ret = LY_SUCCESS; |
| 3236 | struct lyxp_expr *dup = NULL; |
Michal Vasko | d59039d | 2022-09-09 09:07:30 +0200 | [diff] [blame] | 3237 | uint32_t used = 0, i, j, expr_len; |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3238 | const char *expr_start; |
| 3239 | |
| 3240 | 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] | 3241 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 3242 | if (!exp) { |
| 3243 | goto cleanup; |
| 3244 | } |
| 3245 | |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3246 | if (!start_idx && !end_idx) { |
| 3247 | end_idx = exp->used - 1; |
| 3248 | } |
| 3249 | |
| 3250 | expr_start = exp->expr + exp->tok_pos[start_idx]; |
| 3251 | expr_len = (exp->tok_pos[end_idx] + exp->tok_len[end_idx]) - exp->tok_pos[start_idx]; |
| 3252 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3253 | dup = calloc(1, sizeof *dup); |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 3254 | LY_CHECK_ERR_GOTO(!dup, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3255 | |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 3256 | if (exp->used) { |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3257 | used = (end_idx - start_idx) + 1; |
| 3258 | |
| 3259 | dup->tokens = malloc(used * sizeof *dup->tokens); |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 3260 | LY_CHECK_ERR_GOTO(!dup->tokens, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3261 | memcpy(dup->tokens, exp->tokens + start_idx, used * sizeof *dup->tokens); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3262 | |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3263 | dup->tok_pos = malloc(used * sizeof *dup->tok_pos); |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 3264 | 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] | 3265 | 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] | 3266 | |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3267 | if (start_idx) { |
| 3268 | /* fix the indices in the expression */ |
| 3269 | for (i = 0; i < used; ++i) { |
| 3270 | dup->tok_pos[i] -= expr_start - exp->expr; |
| 3271 | } |
| 3272 | } |
| 3273 | |
| 3274 | dup->tok_len = malloc(used * sizeof *dup->tok_len); |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 3275 | 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] | 3276 | 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] | 3277 | |
Michal Vasko | 79a7a87 | 2022-06-17 09:00:48 +0200 | [diff] [blame] | 3278 | if (exp->repeat) { |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3279 | dup->repeat = malloc(used * sizeof *dup->repeat); |
Michal Vasko | 79a7a87 | 2022-06-17 09:00:48 +0200 | [diff] [blame] | 3280 | LY_CHECK_ERR_GOTO(!dup->repeat, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3281 | for (i = start_idx; i <= end_idx; ++i) { |
Michal Vasko | 79a7a87 | 2022-06-17 09:00:48 +0200 | [diff] [blame] | 3282 | if (!exp->repeat[i]) { |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3283 | dup->repeat[i - start_idx] = NULL; |
Michal Vasko | 79a7a87 | 2022-06-17 09:00:48 +0200 | [diff] [blame] | 3284 | } else { |
| 3285 | for (j = 0; exp->repeat[i][j]; ++j) {} |
| 3286 | /* the ending 0 as well */ |
| 3287 | ++j; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3288 | |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3289 | dup->repeat[i - start_idx] = malloc(j * sizeof **dup->repeat); |
| 3290 | LY_CHECK_ERR_GOTO(!dup->repeat[i - start_idx], LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 3291 | 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] | 3292 | } |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 3293 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3294 | } |
| 3295 | } |
| 3296 | |
Michal Vasko | e33134a | 2022-07-29 14:54:40 +0200 | [diff] [blame] | 3297 | dup->used = used; |
| 3298 | dup->size = used; |
| 3299 | |
| 3300 | /* copy only subexpression */ |
| 3301 | 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] | 3302 | |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 3303 | cleanup: |
| 3304 | if (ret) { |
| 3305 | lyxp_expr_free(ctx, dup); |
| 3306 | } else { |
| 3307 | *dup_p = dup; |
| 3308 | } |
| 3309 | return ret; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3310 | } |
| 3311 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3312 | /** |
| 3313 | * @brief Get the last-added schema node that is currently in the context. |
| 3314 | * |
| 3315 | * @param[in] set Set to search in. |
| 3316 | * @return Last-added schema context node, NULL if no node is in context. |
| 3317 | */ |
| 3318 | static struct lysc_node * |
| 3319 | warn_get_scnode_in_ctx(struct lyxp_set *set) |
| 3320 | { |
| 3321 | uint32_t i; |
| 3322 | |
| 3323 | if (!set || (set->type != LYXP_SET_SCNODE_SET)) { |
| 3324 | return NULL; |
| 3325 | } |
| 3326 | |
| 3327 | i = set->used; |
| 3328 | do { |
| 3329 | --i; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 3330 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3331 | /* if there are more, simply return the first found (last added) */ |
| 3332 | return set->val.scnodes[i].scnode; |
| 3333 | } |
| 3334 | } while (i); |
| 3335 | |
| 3336 | return NULL; |
| 3337 | } |
| 3338 | |
| 3339 | /** |
| 3340 | * @brief Test whether a type is numeric - integer type or decimal64. |
| 3341 | * |
| 3342 | * @param[in] type Type to test. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3343 | * @return Boolean value whether @p type is numeric type or not. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3344 | */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3345 | static ly_bool |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3346 | warn_is_numeric_type(struct lysc_type *type) |
| 3347 | { |
| 3348 | struct lysc_type_union *uni; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3349 | ly_bool ret; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3350 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3351 | |
| 3352 | switch (type->basetype) { |
| 3353 | case LY_TYPE_DEC64: |
| 3354 | case LY_TYPE_INT8: |
| 3355 | case LY_TYPE_UINT8: |
| 3356 | case LY_TYPE_INT16: |
| 3357 | case LY_TYPE_UINT16: |
| 3358 | case LY_TYPE_INT32: |
| 3359 | case LY_TYPE_UINT32: |
| 3360 | case LY_TYPE_INT64: |
| 3361 | case LY_TYPE_UINT64: |
| 3362 | return 1; |
| 3363 | case LY_TYPE_UNION: |
| 3364 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3365 | LY_ARRAY_FOR(uni->types, u) { |
| 3366 | ret = warn_is_numeric_type(uni->types[u]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3367 | if (ret) { |
| 3368 | /* found a suitable type */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3369 | return ret; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3370 | } |
| 3371 | } |
| 3372 | /* did not find any suitable type */ |
| 3373 | return 0; |
| 3374 | case LY_TYPE_LEAFREF: |
| 3375 | return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype); |
| 3376 | default: |
| 3377 | return 0; |
| 3378 | } |
| 3379 | } |
| 3380 | |
| 3381 | /** |
| 3382 | * @brief Test whether a type is string-like - no integers, decimal64 or binary. |
| 3383 | * |
| 3384 | * @param[in] type Type to test. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3385 | * @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] | 3386 | */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3387 | static ly_bool |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3388 | warn_is_string_type(struct lysc_type *type) |
| 3389 | { |
| 3390 | struct lysc_type_union *uni; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3391 | ly_bool ret; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3392 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3393 | |
| 3394 | switch (type->basetype) { |
| 3395 | case LY_TYPE_BITS: |
| 3396 | case LY_TYPE_ENUM: |
| 3397 | case LY_TYPE_IDENT: |
| 3398 | case LY_TYPE_INST: |
| 3399 | case LY_TYPE_STRING: |
| 3400 | return 1; |
| 3401 | case LY_TYPE_UNION: |
| 3402 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3403 | LY_ARRAY_FOR(uni->types, u) { |
| 3404 | ret = warn_is_string_type(uni->types[u]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3405 | if (ret) { |
| 3406 | /* found a suitable type */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3407 | return ret; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3408 | } |
| 3409 | } |
| 3410 | /* did not find any suitable type */ |
| 3411 | return 0; |
| 3412 | case LY_TYPE_LEAFREF: |
| 3413 | return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype); |
| 3414 | default: |
| 3415 | return 0; |
| 3416 | } |
| 3417 | } |
| 3418 | |
| 3419 | /** |
| 3420 | * @brief Test whether a type is one specific type. |
| 3421 | * |
| 3422 | * @param[in] type Type to test. |
| 3423 | * @param[in] base Expected type. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3424 | * @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] | 3425 | */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3426 | static ly_bool |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3427 | warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base) |
| 3428 | { |
| 3429 | struct lysc_type_union *uni; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3430 | ly_bool ret; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3431 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3432 | |
| 3433 | if (type->basetype == base) { |
| 3434 | return 1; |
| 3435 | } else if (type->basetype == LY_TYPE_UNION) { |
| 3436 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3437 | LY_ARRAY_FOR(uni->types, u) { |
| 3438 | ret = warn_is_specific_type(uni->types[u], base); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3439 | if (ret) { |
| 3440 | /* found a suitable type */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3441 | return ret; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3442 | } |
| 3443 | } |
| 3444 | /* did not find any suitable type */ |
| 3445 | return 0; |
| 3446 | } else if (type->basetype == LY_TYPE_LEAFREF) { |
| 3447 | return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base); |
| 3448 | } |
| 3449 | |
| 3450 | return 0; |
| 3451 | } |
| 3452 | |
| 3453 | /** |
| 3454 | * @brief Get next type of a (union) type. |
| 3455 | * |
| 3456 | * @param[in] type Base type. |
| 3457 | * @param[in] prev_type Previously returned type. |
Michal Vasko | e887774 | 2024-06-13 10:42:39 +0200 | [diff] [blame] | 3458 | * @param[in,out] found Whether @p prev_type has already been found or not. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3459 | * @return Next type or NULL. |
| 3460 | */ |
| 3461 | static struct lysc_type * |
Michal Vasko | e887774 | 2024-06-13 10:42:39 +0200 | [diff] [blame] | 3462 | warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type, ly_bool *found) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3463 | { |
Michal Vasko | e887774 | 2024-06-13 10:42:39 +0200 | [diff] [blame] | 3464 | struct lysc_type *next_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3465 | struct lysc_type_union *uni; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3466 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3467 | |
Michal Vasko | e887774 | 2024-06-13 10:42:39 +0200 | [diff] [blame] | 3468 | if (type->basetype == LY_TYPE_LEAFREF) { |
| 3469 | type = ((struct lysc_type_leafref *)type)->realtype; |
| 3470 | } |
| 3471 | |
Michal Vasko | 4e55f5a | 2022-12-14 12:15:00 +0100 | [diff] [blame] | 3472 | if (type->basetype == LY_TYPE_UNION) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3473 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3474 | LY_ARRAY_FOR(uni->types, u) { |
Michal Vasko | e887774 | 2024-06-13 10:42:39 +0200 | [diff] [blame] | 3475 | next_type = warn_is_equal_type_next_type(uni->types[u], prev_type, found); |
| 3476 | if (next_type) { |
| 3477 | return next_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3478 | } |
| 3479 | } |
Michal Vasko | 4e55f5a | 2022-12-14 12:15:00 +0100 | [diff] [blame] | 3480 | } else { |
Michal Vasko | e887774 | 2024-06-13 10:42:39 +0200 | [diff] [blame] | 3481 | if (*found) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3482 | return type; |
Michal Vasko | e887774 | 2024-06-13 10:42:39 +0200 | [diff] [blame] | 3483 | } else if (prev_type == type) { |
| 3484 | *found = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3485 | } |
| 3486 | } |
Michal Vasko | e887774 | 2024-06-13 10:42:39 +0200 | [diff] [blame] | 3487 | |
| 3488 | return NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3489 | } |
| 3490 | |
| 3491 | /** |
| 3492 | * @brief Test whether 2 types have a common type. |
| 3493 | * |
| 3494 | * @param[in] type1 First type. |
| 3495 | * @param[in] type2 Second type. |
| 3496 | * @return 1 if they do, 0 otherwise. |
| 3497 | */ |
| 3498 | static int |
| 3499 | warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2) |
| 3500 | { |
Michal Vasko | e887774 | 2024-06-13 10:42:39 +0200 | [diff] [blame] | 3501 | struct lysc_type *t1, *t2; |
| 3502 | ly_bool found1 = 1, found2 = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3503 | |
| 3504 | t1 = NULL; |
Michal Vasko | e887774 | 2024-06-13 10:42:39 +0200 | [diff] [blame] | 3505 | while ((t1 = warn_is_equal_type_next_type(type1, t1, &found1))) { |
| 3506 | found1 = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3507 | |
| 3508 | t2 = NULL; |
Michal Vasko | e887774 | 2024-06-13 10:42:39 +0200 | [diff] [blame] | 3509 | while ((t2 = warn_is_equal_type_next_type(type2, t2, &found2))) { |
| 3510 | found2 = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3511 | |
Michal Vasko | e887774 | 2024-06-13 10:42:39 +0200 | [diff] [blame] | 3512 | if (t2->basetype == t1->basetype) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3513 | /* match found */ |
| 3514 | return 1; |
| 3515 | } |
| 3516 | } |
| 3517 | } |
| 3518 | |
| 3519 | return 0; |
| 3520 | } |
| 3521 | |
| 3522 | /** |
Michal Vasko | aa95652 | 2021-11-11 10:45:34 +0100 | [diff] [blame] | 3523 | * @brief Print warning with information about the XPath subexpression that caused previous warning. |
| 3524 | * |
| 3525 | * @param[in] ctx Context for logging. |
| 3526 | * @param[in] tok_pos Index of the subexpression in the whole expression. |
| 3527 | * @param[in] subexpr Subexpression start. |
| 3528 | * @param[in] subexpr_len Length of @p subexpr to print. |
| 3529 | * @param[in] cur_scnode Expression context node. |
| 3530 | */ |
| 3531 | static void |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 3532 | 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] | 3533 | const struct lysc_node *cur_scnode) |
| 3534 | { |
| 3535 | char *path; |
| 3536 | |
| 3537 | path = lysc_path(cur_scnode, LYSC_PATH_LOG, NULL, 0); |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 3538 | 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] | 3539 | tok_pos, subexpr_len, subexpr, path); |
| 3540 | free(path); |
| 3541 | } |
| 3542 | |
| 3543 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3544 | * @brief Check both operands of comparison operators. |
| 3545 | * |
| 3546 | * @param[in] ctx Context for errors. |
| 3547 | * @param[in] set1 First operand set. |
| 3548 | * @param[in] set2 Second operand set. |
| 3549 | * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!='). |
| 3550 | * @param[in] expr Start of the expression to print with the warning. |
| 3551 | * @param[in] tok_pos Token position. |
| 3552 | */ |
| 3553 | static void |
Michal Vasko | aa95652 | 2021-11-11 10:45:34 +0100 | [diff] [blame] | 3554 | 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] | 3555 | uint32_t tok_pos) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3556 | { |
| 3557 | struct lysc_node_leaf *node1, *node2; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3558 | ly_bool leaves = 1, warning = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3559 | |
| 3560 | node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1); |
| 3561 | node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2); |
| 3562 | |
| 3563 | if (!node1 && !node2) { |
| 3564 | /* no node-sets involved, nothing to do */ |
| 3565 | return; |
| 3566 | } |
| 3567 | |
| 3568 | if (node1) { |
| 3569 | if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3570 | LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name); |
| 3571 | warning = 1; |
| 3572 | leaves = 0; |
| 3573 | } else if (numbers_only && !warn_is_numeric_type(node1->type)) { |
| 3574 | LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name); |
| 3575 | warning = 1; |
| 3576 | } |
| 3577 | } |
| 3578 | |
| 3579 | if (node2) { |
| 3580 | if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3581 | LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name); |
| 3582 | warning = 1; |
| 3583 | leaves = 0; |
| 3584 | } else if (numbers_only && !warn_is_numeric_type(node2->type)) { |
| 3585 | LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name); |
| 3586 | warning = 1; |
| 3587 | } |
| 3588 | } |
| 3589 | |
| 3590 | if (node1 && node2 && leaves && !numbers_only) { |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3591 | if ((warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type)) || |
| 3592 | (!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_equal_type(node1->type, node2->type))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3595 | LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name); |
| 3596 | warning = 1; |
| 3597 | } |
| 3598 | } |
| 3599 | |
| 3600 | if (warning) { |
Michal Vasko | aa95652 | 2021-11-11 10:45:34 +0100 | [diff] [blame] | 3601 | 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] | 3602 | } |
| 3603 | } |
| 3604 | |
| 3605 | /** |
| 3606 | * @brief Check that a value is valid for a leaf. If not applicable, does nothing. |
| 3607 | * |
| 3608 | * @param[in] exp Parsed XPath expression. |
| 3609 | * @param[in] set Set with the leaf/leaf-list. |
| 3610 | * @param[in] val_exp Index of the value (literal/number) in @p exp. |
| 3611 | * @param[in] equal_exp Index of the start of the equality expression in @p exp. |
| 3612 | * @param[in] last_equal_exp Index of the end of the equality expression in @p exp. |
| 3613 | */ |
| 3614 | static void |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 3615 | warn_equality_value(const struct lyxp_expr *exp, struct lyxp_set *set, uint32_t val_exp, uint32_t equal_exp, |
| 3616 | uint32_t last_equal_exp) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3617 | { |
| 3618 | struct lysc_node *scnode; |
| 3619 | struct lysc_type *type; |
| 3620 | char *value; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3621 | struct lyd_value storage; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3622 | LY_ERR rc; |
| 3623 | struct ly_err_item *err = NULL; |
| 3624 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3625 | if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && |
| 3626 | ((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] | 3627 | /* check that the node can have the specified value */ |
| 3628 | if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) { |
| 3629 | value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2); |
| 3630 | } else { |
| 3631 | value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]); |
| 3632 | } |
| 3633 | if (!value) { |
| 3634 | LOGMEM(set->ctx); |
| 3635 | return; |
| 3636 | } |
| 3637 | |
| 3638 | if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) { |
| 3639 | 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] | 3640 | " 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] | 3641 | 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] | 3642 | (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] | 3643 | set->cur_scnode); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3644 | } |
| 3645 | |
| 3646 | type = ((struct lysc_node_leaf *)scnode)->type; |
| 3647 | if (type->basetype != LY_TYPE_IDENT) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 3648 | 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] | 3649 | LYD_HINT_DATA, scnode, &storage, NULL, &err); |
Michal Vasko | bf42e83 | 2020-11-23 16:59:42 +0100 | [diff] [blame] | 3650 | if (rc == LY_EINCOMPLETE) { |
| 3651 | rc = LY_SUCCESS; |
| 3652 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3653 | |
| 3654 | if (err) { |
| 3655 | LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg); |
| 3656 | ly_err_free(err); |
| 3657 | } else if (rc != LY_SUCCESS) { |
| 3658 | LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value); |
| 3659 | } |
| 3660 | if (rc != LY_SUCCESS) { |
Michal Vasko | aa95652 | 2021-11-11 10:45:34 +0100 | [diff] [blame] | 3661 | 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] | 3662 | (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] | 3663 | set->cur_scnode); |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3664 | } else { |
| 3665 | type->plugin->free(set->ctx, &storage); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3666 | } |
| 3667 | } |
| 3668 | free(value); |
| 3669 | } |
| 3670 | } |
| 3671 | |
| 3672 | /* |
| 3673 | * XPath functions |
| 3674 | */ |
| 3675 | |
| 3676 | /** |
| 3677 | * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN |
| 3678 | * depending on whether the first node bit value from the second argument is set. |
| 3679 | * |
| 3680 | * @param[in] args Array of arguments. |
| 3681 | * @param[in] arg_count Count of elements in @p args. |
| 3682 | * @param[in,out] set Context and result set at the same time. |
| 3683 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3684 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3685 | */ |
| 3686 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 3687 | 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] | 3688 | { |
| 3689 | struct lyd_node_term *leaf; |
| 3690 | struct lysc_node_leaf *sleaf; |
Michal Vasko | 2588b95 | 2021-07-29 07:43:26 +0200 | [diff] [blame] | 3691 | struct lyd_value_bits *bits; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3692 | LY_ERR rc = LY_SUCCESS; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3693 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3694 | |
| 3695 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3696 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3697 | 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] | 3698 | } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3699 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3700 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 3701 | sleaf->name); |
| 3702 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) { |
| 3703 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"bits\".", __func__, sleaf->name); |
| 3704 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3705 | } |
| 3706 | |
| 3707 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 3708 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3709 | LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 3710 | sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3711 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3712 | 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] | 3713 | } |
| 3714 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3715 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3716 | return rc; |
| 3717 | } |
| 3718 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3719 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 3720 | 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] | 3721 | return LY_EVALID; |
| 3722 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3723 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3724 | LY_CHECK_RET(rc); |
| 3725 | |
| 3726 | set_fill_boolean(set, 0); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3727 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3728 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
Michal Vasko | 2588b95 | 2021-07-29 07:43:26 +0200 | [diff] [blame] | 3729 | if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (leaf->value.realtype->basetype == LY_TYPE_BITS)) { |
| 3730 | LYD_VALUE_GET(&leaf->value, bits); |
| 3731 | LY_ARRAY_FOR(bits->items, u) { |
| 3732 | if (!strcmp(bits->items[u]->name, args[1]->val.str)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3733 | set_fill_boolean(set, 1); |
| 3734 | break; |
| 3735 | } |
| 3736 | } |
| 3737 | } |
| 3738 | } |
| 3739 | |
| 3740 | return LY_SUCCESS; |
| 3741 | } |
| 3742 | |
| 3743 | /** |
| 3744 | * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN |
| 3745 | * with the argument converted to boolean. |
| 3746 | * |
| 3747 | * @param[in] args Array of arguments. |
| 3748 | * @param[in] arg_count Count of elements in @p args. |
| 3749 | * @param[in,out] set Context and result set at the same time. |
| 3750 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3751 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3752 | */ |
| 3753 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 3754 | 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] | 3755 | { |
| 3756 | LY_ERR rc; |
| 3757 | |
| 3758 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3759 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3760 | return LY_SUCCESS; |
| 3761 | } |
| 3762 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3763 | rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3764 | LY_CHECK_RET(rc); |
| 3765 | set_fill_set(set, args[0]); |
| 3766 | |
| 3767 | return LY_SUCCESS; |
| 3768 | } |
| 3769 | |
| 3770 | /** |
| 3771 | * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER |
| 3772 | * with the first argument rounded up to the nearest integer. |
| 3773 | * |
| 3774 | * @param[in] args Array of arguments. |
| 3775 | * @param[in] arg_count Count of elements in @p args. |
| 3776 | * @param[in,out] set Context and result set at the same time. |
| 3777 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3778 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3779 | */ |
| 3780 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 3781 | 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] | 3782 | { |
| 3783 | struct lysc_node_leaf *sleaf; |
| 3784 | LY_ERR rc = LY_SUCCESS; |
| 3785 | |
| 3786 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3787 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3788 | 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] | 3789 | } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3790 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3791 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 3792 | sleaf->name); |
| 3793 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) { |
| 3794 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name); |
| 3795 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3796 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3797 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3798 | return rc; |
| 3799 | } |
| 3800 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3801 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3802 | LY_CHECK_RET(rc); |
| 3803 | if ((long long)args[0]->val.num != args[0]->val.num) { |
| 3804 | set_fill_number(set, ((long long)args[0]->val.num) + 1); |
| 3805 | } else { |
| 3806 | set_fill_number(set, args[0]->val.num); |
| 3807 | } |
| 3808 | |
| 3809 | return LY_SUCCESS; |
| 3810 | } |
| 3811 | |
| 3812 | /** |
| 3813 | * @brief Execute the XPath concat(string, string, string*) function. |
| 3814 | * Returns LYXP_SET_STRING with the concatenation of all the arguments. |
| 3815 | * |
| 3816 | * @param[in] args Array of arguments. |
| 3817 | * @param[in] arg_count Count of elements in @p args. |
| 3818 | * @param[in,out] set Context and result set at the same time. |
| 3819 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3820 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3821 | */ |
| 3822 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 3823 | 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] | 3824 | { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 3825 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3826 | char *str = NULL; |
| 3827 | size_t used = 1; |
| 3828 | LY_ERR rc = LY_SUCCESS; |
| 3829 | struct lysc_node_leaf *sleaf; |
| 3830 | |
| 3831 | if (options & LYXP_SCNODE_ALL) { |
| 3832 | for (i = 0; i < arg_count; ++i) { |
| 3833 | if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) { |
| 3834 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Michal Vasko | 21eaa39 | 2024-02-20 15:48:42 +0100 | [diff] [blame] | 3835 | LOGWRN(set->ctx, "Argument #%" PRIu32 " of %s is a %s node \"%s\".", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3836 | i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3837 | } else if (!warn_is_string_type(sleaf->type)) { |
Michal Vasko | 21eaa39 | 2024-02-20 15:48:42 +0100 | [diff] [blame] | 3838 | LOGWRN(set->ctx, "Argument #%" PRIu32 " of %s is node \"%s\", not of string-type.", i + 1, __func__, |
| 3839 | 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; |
steweg | 6738895 | 2024-01-25 12:14:50 +0100 | [diff] [blame] | 4017 | LY_ERR ret = LY_SUCCESS; |
| 4018 | struct ly_set *targets = NULL; |
| 4019 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4020 | |
| 4021 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 4022 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4023 | 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] | 4024 | } 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] | 4025 | if (!(sleaf->nodetype & LYD_NODE_TERM)) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 4026 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 4027 | sleaf->name); |
Michal Vasko | ed725d7 | 2021-06-23 12:03:45 +0200 | [diff] [blame] | 4028 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) && |
| 4029 | !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 4030 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"leafref\" nor \"instance-identifier\".", |
| 4031 | __func__, sleaf->name); |
| 4032 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4033 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4034 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 423ba3f | 2021-07-19 13:08:50 +0200 | [diff] [blame] | 4035 | 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] | 4036 | lref = (struct lysc_type_leafref *)sleaf->type; |
Michal Vasko | d1e53b9 | 2021-01-28 13:11:06 +0100 | [diff] [blame] | 4037 | 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] | 4038 | |
| 4039 | /* it was already evaluated on schema, it must succeed */ |
Michal Vasko | 741bb56 | 2021-06-24 11:59:50 +0200 | [diff] [blame] | 4040 | 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] | 4041 | LY_VALUE_SCHEMA_RESOLVED, lref->prefixes, &p); |
Michal Vasko | 741bb56 | 2021-06-24 11:59:50 +0200 | [diff] [blame] | 4042 | if (!r) { |
| 4043 | /* get the target node */ |
| 4044 | target = p[LY_ARRAY_COUNT(p) - 1].node; |
| 4045 | ly_path_free(set->ctx, p); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4046 | |
Michal Vasko | e4a6d01 | 2023-05-22 14:34:52 +0200 | [diff] [blame] | 4047 | 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] | 4048 | } /* 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] | 4049 | } |
| 4050 | |
steweg | 6738895 | 2024-01-25 12:14:50 +0100 | [diff] [blame] | 4051 | ret = LY_SUCCESS; |
| 4052 | goto cleanup; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4053 | } |
| 4054 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4055 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4056 | LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "deref(node-set)"); |
steweg | 6738895 | 2024-01-25 12:14:50 +0100 | [diff] [blame] | 4057 | ret = LY_EVALID; |
| 4058 | goto cleanup; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4059 | } |
| 4060 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4061 | lyxp_set_free_content(set); |
| 4062 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4063 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
| 4064 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 4065 | if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 4066 | if (sleaf->type->basetype == LY_TYPE_LEAFREF) { |
| 4067 | /* find leafref target */ |
steweg | 6738895 | 2024-01-25 12:14:50 +0100 | [diff] [blame] | 4068 | r = lyplg_type_resolve_leafref((struct lysc_type_leafref *)sleaf->type, &leaf->node, &leaf->value, set->tree, |
| 4069 | &targets, &errmsg); |
| 4070 | if (r) { |
Michal Vasko | 56318fe | 2024-04-05 08:43:40 +0200 | [diff] [blame] | 4071 | LOGERR(set->ctx, LY_EINVAL, "%s", errmsg); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4072 | free(errmsg); |
Michal Vasko | 56318fe | 2024-04-05 08:43:40 +0200 | [diff] [blame] | 4073 | ret = LY_EINVAL; |
steweg | 6738895 | 2024-01-25 12:14:50 +0100 | [diff] [blame] | 4074 | goto cleanup; |
| 4075 | } |
| 4076 | |
| 4077 | /* insert nodes into set */ |
| 4078 | for (i = 0; i < targets->count; ++i) { |
| 4079 | set_insert_node(set, targets->dnodes[i], 0, LYXP_NODE_ELEM, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4080 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4081 | } else { |
| 4082 | assert(sleaf->type->basetype == LY_TYPE_INST); |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 4083 | if (ly_path_eval(leaf->value.target, set->tree, NULL, &node)) { |
Michal Vasko | 56318fe | 2024-04-05 08:43:40 +0200 | [diff] [blame] | 4084 | LOGERR(set->ctx, LY_EINVAL, "Invalid instance-identifier \"%s\" value - required instance not found.", |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 4085 | lyd_get_value(&leaf->node)); |
Michal Vasko | 56318fe | 2024-04-05 08:43:40 +0200 | [diff] [blame] | 4086 | ret = LY_EINVAL; |
steweg | 6738895 | 2024-01-25 12:14:50 +0100 | [diff] [blame] | 4087 | goto cleanup; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4088 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4089 | |
steweg | 6738895 | 2024-01-25 12:14:50 +0100 | [diff] [blame] | 4090 | /* insert it */ |
| 4091 | set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0); |
| 4092 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4093 | } |
| 4094 | } |
| 4095 | |
steweg | 6738895 | 2024-01-25 12:14:50 +0100 | [diff] [blame] | 4096 | cleanup: |
| 4097 | ly_set_free(targets, NULL); |
| 4098 | return ret; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4099 | } |
| 4100 | |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4101 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 4102 | 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] | 4103 | { |
Michal Vasko | fe1af04 | 2022-07-29 14:58:59 +0200 | [diff] [blame] | 4104 | uint32_t i, id_len; |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4105 | LY_ARRAY_COUNT_TYPE u; |
| 4106 | struct lyd_node_term *leaf; |
| 4107 | struct lysc_node_leaf *sleaf; |
| 4108 | struct lyd_meta *meta; |
Michal Vasko | 9392369 | 2021-05-07 15:28:02 +0200 | [diff] [blame] | 4109 | struct lyd_value *val; |
| 4110 | const struct lys_module *mod; |
| 4111 | const char *id_name; |
Michal Vasko | 9392369 | 2021-05-07 15:28:02 +0200 | [diff] [blame] | 4112 | struct lysc_ident *id; |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4113 | LY_ERR rc = LY_SUCCESS; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 4114 | ly_bool found; |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4115 | |
| 4116 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 4117 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4118 | 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] | 4119 | } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4120 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4121 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype), |
| 4122 | sleaf->name); |
| 4123 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) { |
| 4124 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", func, sleaf->name); |
| 4125 | } |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4126 | } |
| 4127 | |
| 4128 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4129 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4130 | 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] | 4131 | sleaf->name); |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4132 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4133 | LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", func, sleaf->name); |
| 4134 | } |
| 4135 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4136 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4137 | return rc; |
| 4138 | } |
| 4139 | |
| 4140 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4141 | 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] | 4142 | return LY_EVALID; |
| 4143 | } |
| 4144 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
| 4145 | LY_CHECK_RET(rc); |
| 4146 | |
Michal Vasko | 9392369 | 2021-05-07 15:28:02 +0200 | [diff] [blame] | 4147 | /* parse the identity */ |
| 4148 | id_name = args[1]->val.str; |
| 4149 | id_len = strlen(id_name); |
| 4150 | rc = moveto_resolve_model(&id_name, &id_len, set, set->cur_node ? set->cur_node->schema : NULL, &mod); |
| 4151 | LY_CHECK_RET(rc); |
| 4152 | if (!mod) { |
| 4153 | LOGVAL(set->ctx, LYVE_XPATH, "Identity \"%.*s\" without a prefix.", (int)id_len, id_name); |
| 4154 | return LY_EVALID; |
| 4155 | } |
| 4156 | |
| 4157 | /* find the identity */ |
| 4158 | found = 0; |
| 4159 | LY_ARRAY_FOR(mod->identities, u) { |
| 4160 | if (!ly_strncmp(mod->identities[u].name, id_name, id_len)) { |
| 4161 | /* we have match */ |
| 4162 | found = 1; |
| 4163 | break; |
| 4164 | } |
| 4165 | } |
| 4166 | if (!found) { |
| 4167 | LOGVAL(set->ctx, LYVE_XPATH, "Identity \"%.*s\" not found in module \"%s\".", (int)id_len, id_name, mod->name); |
| 4168 | return LY_EVALID; |
| 4169 | } |
| 4170 | id = &mod->identities[u]; |
| 4171 | |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4172 | set_fill_boolean(set, 0); |
| 4173 | found = 0; |
| 4174 | for (i = 0; i < args[0]->used; ++i) { |
| 4175 | if ((args[0]->val.nodes[i].type != LYXP_NODE_ELEM) && (args[0]->val.nodes[i].type != LYXP_NODE_META)) { |
| 4176 | continue; |
| 4177 | } |
| 4178 | |
| 4179 | if (args[0]->val.nodes[i].type == LYXP_NODE_ELEM) { |
| 4180 | leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node; |
| 4181 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 4182 | val = &leaf->value; |
zhangtaog | b3d079f | 2024-02-21 16:34:11 +0800 | [diff] [blame] | 4183 | if (!sleaf || !(sleaf->nodetype & LYD_NODE_TERM) || (leaf->value.realtype->basetype != LY_TYPE_IDENT)) { |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4184 | /* uninteresting */ |
| 4185 | continue; |
| 4186 | } |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4187 | } else { |
| 4188 | meta = args[0]->val.meta[i].meta; |
| 4189 | val = &meta->value; |
| 4190 | if (val->realtype->basetype != LY_TYPE_IDENT) { |
| 4191 | /* uninteresting */ |
| 4192 | continue; |
| 4193 | } |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4194 | } |
| 4195 | |
Michal Vasko | 9392369 | 2021-05-07 15:28:02 +0200 | [diff] [blame] | 4196 | /* check the identity itself */ |
| 4197 | if (self_match && (id == val->ident)) { |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4198 | set_fill_boolean(set, 1); |
| 4199 | found = 1; |
| 4200 | } |
Michal Vasko | 9392369 | 2021-05-07 15:28:02 +0200 | [diff] [blame] | 4201 | if (!found && !lyplg_type_identity_isderived(id, val->ident)) { |
| 4202 | set_fill_boolean(set, 1); |
| 4203 | found = 1; |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4204 | } |
| 4205 | |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4206 | if (found) { |
| 4207 | break; |
| 4208 | } |
| 4209 | } |
| 4210 | |
| 4211 | return LY_SUCCESS; |
| 4212 | } |
| 4213 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4214 | /** |
| 4215 | * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending |
| 4216 | * on whether the first argument nodes contain a node of an identity derived from the second |
| 4217 | * argument identity. |
| 4218 | * |
| 4219 | * @param[in] args Array of arguments. |
| 4220 | * @param[in] arg_count Count of elements in @p args. |
| 4221 | * @param[in,out] set Context and result set at the same time. |
| 4222 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4223 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4224 | */ |
| 4225 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4226 | 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] | 4227 | { |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4228 | return xpath_derived_(args, set, options, 0, __func__); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4229 | } |
| 4230 | |
| 4231 | /** |
| 4232 | * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending |
| 4233 | * on whether the first argument nodes contain a node of an identity that either is or is derived from |
| 4234 | * the second argument identity. |
| 4235 | * |
| 4236 | * @param[in] args Array of arguments. |
| 4237 | * @param[in] arg_count Count of elements in @p args. |
| 4238 | * @param[in,out] set Context and result set at the same time. |
| 4239 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4240 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4241 | */ |
| 4242 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4243 | xpath_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] | 4244 | { |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 4245 | return xpath_derived_(args, set, options, 1, __func__); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4246 | } |
| 4247 | |
| 4248 | /** |
| 4249 | * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER |
| 4250 | * with the integer value of the first node's enum value, otherwise NaN. |
| 4251 | * |
| 4252 | * @param[in] args Array of arguments. |
| 4253 | * @param[in] arg_count Count of elements in @p args. |
| 4254 | * @param[in,out] set Context and result set at the same time. |
| 4255 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4256 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4257 | */ |
| 4258 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4259 | 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] | 4260 | { |
| 4261 | struct lyd_node_term *leaf; |
| 4262 | struct lysc_node_leaf *sleaf; |
| 4263 | LY_ERR rc = LY_SUCCESS; |
| 4264 | |
| 4265 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 4266 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4267 | 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] | 4268 | } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4269 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4270 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 4271 | sleaf->name); |
| 4272 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) { |
| 4273 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"enumeration\".", __func__, sleaf->name); |
| 4274 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4275 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4276 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4277 | return rc; |
| 4278 | } |
| 4279 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4280 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4281 | 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] | 4282 | return LY_EVALID; |
| 4283 | } |
| 4284 | |
| 4285 | set_fill_number(set, NAN); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4286 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4287 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
| 4288 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 4289 | if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) { |
| 4290 | set_fill_number(set, leaf->value.enum_item->value); |
| 4291 | } |
| 4292 | } |
| 4293 | |
| 4294 | return LY_SUCCESS; |
| 4295 | } |
| 4296 | |
| 4297 | /** |
| 4298 | * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN |
| 4299 | * with false value. |
| 4300 | * |
| 4301 | * @param[in] args Array of arguments. |
| 4302 | * @param[in] arg_count Count of elements in @p args. |
| 4303 | * @param[in,out] set Context and result set at the same time. |
| 4304 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4305 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4306 | */ |
| 4307 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4308 | 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] | 4309 | { |
| 4310 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4311 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4312 | return LY_SUCCESS; |
| 4313 | } |
| 4314 | |
| 4315 | set_fill_boolean(set, 0); |
| 4316 | return LY_SUCCESS; |
| 4317 | } |
| 4318 | |
| 4319 | /** |
| 4320 | * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER |
| 4321 | * with the first argument floored (truncated). |
| 4322 | * |
| 4323 | * @param[in] args Array of arguments. |
| 4324 | * @param[in] arg_count Count of elements in @p args. |
| 4325 | * @param[in,out] set Context and result set at the same time. |
| 4326 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4327 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4328 | */ |
| 4329 | static LY_ERR |
Michal Vasko | 0ff8d63 | 2023-04-06 12:31:17 +0200 | [diff] [blame] | 4330 | 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] | 4331 | { |
Michal Vasko | 0ff8d63 | 2023-04-06 12:31:17 +0200 | [diff] [blame] | 4332 | struct lysc_node_leaf *sleaf; |
| 4333 | LY_ERR rc = LY_SUCCESS; |
| 4334 | |
| 4335 | if (options & LYXP_SCNODE_ALL) { |
| 4336 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
| 4337 | LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__); |
| 4338 | } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4339 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4340 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 4341 | sleaf->name); |
| 4342 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) { |
| 4343 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name); |
| 4344 | } |
| 4345 | } |
| 4346 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
| 4347 | return rc; |
| 4348 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4349 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4350 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4351 | LY_CHECK_RET(rc); |
| 4352 | if (isfinite(args[0]->val.num)) { |
| 4353 | set_fill_number(set, (long long)args[0]->val.num); |
| 4354 | } |
| 4355 | |
| 4356 | return LY_SUCCESS; |
| 4357 | } |
| 4358 | |
| 4359 | /** |
| 4360 | * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN |
| 4361 | * whether the language of the text matches the one from the argument. |
| 4362 | * |
| 4363 | * @param[in] args Array of arguments. |
| 4364 | * @param[in] arg_count Count of elements in @p args. |
| 4365 | * @param[in,out] set Context and result set at the same time. |
| 4366 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4367 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4368 | */ |
| 4369 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4370 | 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] | 4371 | { |
| 4372 | const struct lyd_node *node; |
| 4373 | struct lysc_node_leaf *sleaf; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4374 | struct lyd_meta *meta = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4375 | const char *val; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4376 | LY_ERR rc = LY_SUCCESS; |
| 4377 | |
| 4378 | if (options & LYXP_SCNODE_ALL) { |
| 4379 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4380 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4381 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 4382 | sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4383 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4384 | 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] | 4385 | } |
| 4386 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4387 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4388 | return rc; |
| 4389 | } |
| 4390 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4391 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4392 | LY_CHECK_RET(rc); |
| 4393 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4394 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4395 | 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] | 4396 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4397 | } else if (!set->used) { |
| 4398 | set_fill_boolean(set, 0); |
| 4399 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4400 | } |
| 4401 | |
| 4402 | switch (set->val.nodes[0].type) { |
| 4403 | case LYXP_NODE_ELEM: |
| 4404 | case LYXP_NODE_TEXT: |
| 4405 | node = set->val.nodes[0].node; |
| 4406 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4407 | case LYXP_NODE_META: |
| 4408 | node = set->val.meta[0].meta->parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4409 | break; |
| 4410 | default: |
| 4411 | /* nothing to do with roots */ |
| 4412 | set_fill_boolean(set, 0); |
| 4413 | return LY_SUCCESS; |
| 4414 | } |
| 4415 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4416 | /* find lang metadata */ |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 4417 | for ( ; node; node = lyd_parent(node)) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4418 | for (meta = node->meta; meta; meta = meta->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4419 | /* annotations */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4420 | 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] | 4421 | break; |
| 4422 | } |
| 4423 | } |
| 4424 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4425 | if (meta) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4426 | break; |
| 4427 | } |
| 4428 | } |
| 4429 | |
| 4430 | /* compare languages */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4431 | if (!meta) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4432 | set_fill_boolean(set, 0); |
| 4433 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4434 | uint64_t i; |
| 4435 | |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 4436 | val = lyd_get_meta_value(meta); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4437 | for (i = 0; args[0]->val.str[i]; ++i) { |
| 4438 | if (tolower(args[0]->val.str[i]) != tolower(val[i])) { |
| 4439 | set_fill_boolean(set, 0); |
| 4440 | break; |
| 4441 | } |
| 4442 | } |
| 4443 | if (!args[0]->val.str[i]) { |
| 4444 | if (!val[i] || (val[i] == '-')) { |
| 4445 | set_fill_boolean(set, 1); |
| 4446 | } else { |
| 4447 | set_fill_boolean(set, 0); |
| 4448 | } |
| 4449 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4450 | } |
| 4451 | |
| 4452 | return LY_SUCCESS; |
| 4453 | } |
| 4454 | |
| 4455 | /** |
| 4456 | * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER |
| 4457 | * with the context size. |
| 4458 | * |
| 4459 | * @param[in] args Array of arguments. |
| 4460 | * @param[in] arg_count Count of elements in @p args. |
| 4461 | * @param[in,out] set Context and result set at the same time. |
| 4462 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4463 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4464 | */ |
| 4465 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4466 | 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] | 4467 | { |
| 4468 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4469 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4470 | return LY_SUCCESS; |
| 4471 | } |
| 4472 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4473 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4474 | LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "last()"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4475 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4476 | } else if (!set->used) { |
| 4477 | set_fill_number(set, 0); |
| 4478 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4479 | } |
| 4480 | |
| 4481 | set_fill_number(set, set->ctx_size); |
| 4482 | return LY_SUCCESS; |
| 4483 | } |
| 4484 | |
| 4485 | /** |
| 4486 | * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING |
| 4487 | * with the node name without namespace from the argument or the context. |
| 4488 | * |
| 4489 | * @param[in] args Array of arguments. |
| 4490 | * @param[in] arg_count Count of elements in @p args. |
| 4491 | * @param[in,out] set Context and result set at the same time. |
| 4492 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4493 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4494 | */ |
| 4495 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4496 | 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] | 4497 | { |
| 4498 | struct lyxp_set_node *item; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 4499 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4500 | /* suppress unused variable warning */ |
| 4501 | (void)options; |
| 4502 | |
| 4503 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4504 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4505 | return LY_SUCCESS; |
| 4506 | } |
| 4507 | |
| 4508 | if (arg_count) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4509 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4510 | 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] | 4511 | "local-name(node-set?)"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4512 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4513 | } else if (!args[0]->used) { |
| 4514 | set_fill_string(set, "", 0); |
| 4515 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4516 | } |
| 4517 | |
| 4518 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4519 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4520 | |
| 4521 | item = &args[0]->val.nodes[0]; |
| 4522 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4523 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4524 | 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] | 4525 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4526 | } else if (!set->used) { |
| 4527 | set_fill_string(set, "", 0); |
| 4528 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4529 | } |
| 4530 | |
| 4531 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4532 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4533 | |
| 4534 | item = &set->val.nodes[0]; |
| 4535 | } |
| 4536 | |
| 4537 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4538 | case LYXP_NODE_NONE: |
| 4539 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4540 | case LYXP_NODE_ROOT: |
| 4541 | case LYXP_NODE_ROOT_CONFIG: |
| 4542 | case LYXP_NODE_TEXT: |
| 4543 | set_fill_string(set, "", 0); |
| 4544 | break; |
| 4545 | case LYXP_NODE_ELEM: |
Michal Vasko | 222be68 | 2023-08-24 08:17:12 +0200 | [diff] [blame] | 4546 | 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] | 4547 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4548 | case LYXP_NODE_META: |
| 4549 | 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] | 4550 | break; |
| 4551 | } |
| 4552 | |
| 4553 | return LY_SUCCESS; |
| 4554 | } |
| 4555 | |
| 4556 | /** |
| 4557 | * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING |
| 4558 | * with the node name fully qualified (with namespace) from the argument or the context. |
| 4559 | * |
| 4560 | * @param[in] args Array of arguments. |
| 4561 | * @param[in] arg_count Count of elements in @p args. |
| 4562 | * @param[in,out] set Context and result set at the same time. |
| 4563 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4564 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4565 | */ |
| 4566 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4567 | 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] | 4568 | { |
| 4569 | struct lyxp_set_node *item; |
Michal Vasko | 420cc25 | 2023-08-24 08:14:24 +0200 | [diff] [blame] | 4570 | const struct lys_module *mod = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4571 | char *str; |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 4572 | const char *name = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4573 | |
| 4574 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4575 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4576 | return LY_SUCCESS; |
| 4577 | } |
| 4578 | |
| 4579 | if (arg_count) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4580 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4581 | 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] | 4582 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4583 | } else if (!args[0]->used) { |
| 4584 | set_fill_string(set, "", 0); |
| 4585 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4586 | } |
| 4587 | |
| 4588 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4589 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4590 | |
| 4591 | item = &args[0]->val.nodes[0]; |
| 4592 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4593 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4594 | 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] | 4595 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4596 | } else if (!set->used) { |
| 4597 | set_fill_string(set, "", 0); |
| 4598 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4599 | } |
| 4600 | |
| 4601 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4602 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4603 | |
| 4604 | item = &set->val.nodes[0]; |
| 4605 | } |
| 4606 | |
| 4607 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4608 | case LYXP_NODE_NONE: |
| 4609 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4610 | case LYXP_NODE_ROOT: |
| 4611 | case LYXP_NODE_ROOT_CONFIG: |
| 4612 | case LYXP_NODE_TEXT: |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 4613 | /* keep NULL */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4614 | break; |
| 4615 | case LYXP_NODE_ELEM: |
Michal Vasko | 420cc25 | 2023-08-24 08:14:24 +0200 | [diff] [blame] | 4616 | mod = lyd_node_module(item->node); |
| 4617 | name = LYD_NAME(item->node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4618 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4619 | case LYXP_NODE_META: |
| 4620 | mod = ((struct lyd_meta *)item->node)->annotation->module; |
| 4621 | name = ((struct lyd_meta *)item->node)->name; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4622 | break; |
| 4623 | } |
| 4624 | |
| 4625 | if (mod && name) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 4626 | 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] | 4627 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4628 | LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM); |
| 4629 | set_fill_string(set, str, strlen(str)); |
| 4630 | free(str); |
| 4631 | } else { |
| 4632 | set_fill_string(set, "", 0); |
| 4633 | } |
| 4634 | |
| 4635 | return LY_SUCCESS; |
| 4636 | } |
| 4637 | |
| 4638 | /** |
| 4639 | * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING |
| 4640 | * with the namespace of the node from the argument or the context. |
| 4641 | * |
| 4642 | * @param[in] args Array of arguments. |
| 4643 | * @param[in] arg_count Count of elements in @p args. |
| 4644 | * @param[in,out] set Context and result set at the same time. |
| 4645 | * @param[in] options XPath options. |
| 4646 | * @return LY_ERR (LY_EINVAL for wrong arguments on schema) |
| 4647 | */ |
| 4648 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4649 | 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] | 4650 | { |
| 4651 | struct lyxp_set_node *item; |
Michal Vasko | 420cc25 | 2023-08-24 08:14:24 +0200 | [diff] [blame] | 4652 | const struct lys_module *mod; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 4653 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4654 | /* suppress unused variable warning */ |
| 4655 | (void)options; |
| 4656 | |
| 4657 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4658 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4659 | return LY_SUCCESS; |
| 4660 | } |
| 4661 | |
| 4662 | if (arg_count) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4663 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4664 | 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] | 4665 | "namespace-uri(node-set?)"); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4666 | return LY_EVALID; |
| 4667 | } else if (!args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4668 | set_fill_string(set, "", 0); |
| 4669 | return LY_SUCCESS; |
| 4670 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4671 | |
| 4672 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4673 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4674 | |
| 4675 | item = &args[0]->val.nodes[0]; |
| 4676 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4677 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4678 | 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] | 4679 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4680 | } else if (!set->used) { |
| 4681 | set_fill_string(set, "", 0); |
| 4682 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4683 | } |
| 4684 | |
| 4685 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4686 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4687 | |
| 4688 | item = &set->val.nodes[0]; |
| 4689 | } |
| 4690 | |
| 4691 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4692 | case LYXP_NODE_NONE: |
| 4693 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4694 | case LYXP_NODE_ROOT: |
| 4695 | case LYXP_NODE_ROOT_CONFIG: |
| 4696 | case LYXP_NODE_TEXT: |
| 4697 | set_fill_string(set, "", 0); |
| 4698 | break; |
| 4699 | case LYXP_NODE_ELEM: |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4700 | case LYXP_NODE_META: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4701 | if (item->type == LYXP_NODE_ELEM) { |
Michal Vasko | 420cc25 | 2023-08-24 08:14:24 +0200 | [diff] [blame] | 4702 | mod = lyd_node_module(item->node); |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4703 | } else { /* LYXP_NODE_META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4704 | /* annotations */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4705 | mod = ((struct lyd_meta *)item->node)->annotation->module; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4706 | } |
| 4707 | |
| 4708 | set_fill_string(set, mod->ns, strlen(mod->ns)); |
| 4709 | break; |
| 4710 | } |
| 4711 | |
| 4712 | return LY_SUCCESS; |
| 4713 | } |
| 4714 | |
| 4715 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4716 | * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING |
| 4717 | * with normalized value (no leading, trailing, double white spaces) of the node |
| 4718 | * from the argument or the context. |
| 4719 | * |
| 4720 | * @param[in] args Array of arguments. |
| 4721 | * @param[in] arg_count Count of elements in @p args. |
| 4722 | * @param[in,out] set Context and result set at the same time. |
| 4723 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4724 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4725 | */ |
| 4726 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4727 | 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] | 4728 | { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4729 | uint32_t i, new_used; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4730 | char *new; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 4731 | ly_bool have_spaces = 0, space_before = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4732 | struct lysc_node_leaf *sleaf; |
| 4733 | LY_ERR rc = LY_SUCCESS; |
| 4734 | |
| 4735 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4736 | if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && |
| 4737 | (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4738 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4739 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 4740 | sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4741 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4742 | 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] | 4743 | } |
| 4744 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4745 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4746 | return rc; |
| 4747 | } |
| 4748 | |
| 4749 | if (arg_count) { |
| 4750 | set_fill_set(set, args[0]); |
| 4751 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4752 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4753 | LY_CHECK_RET(rc); |
| 4754 | |
| 4755 | /* is there any normalization necessary? */ |
| 4756 | for (i = 0; set->val.str[i]; ++i) { |
| 4757 | if (is_xmlws(set->val.str[i])) { |
| 4758 | if ((i == 0) || space_before || (!set->val.str[i + 1])) { |
| 4759 | have_spaces = 1; |
| 4760 | break; |
| 4761 | } |
| 4762 | space_before = 1; |
| 4763 | } else { |
| 4764 | space_before = 0; |
| 4765 | } |
| 4766 | } |
| 4767 | |
| 4768 | /* yep, there is */ |
| 4769 | if (have_spaces) { |
| 4770 | /* it's enough, at least one character will go, makes space for ending '\0' */ |
| 4771 | new = malloc(strlen(set->val.str) * sizeof(char)); |
| 4772 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 4773 | new_used = 0; |
| 4774 | |
| 4775 | space_before = 0; |
| 4776 | for (i = 0; set->val.str[i]; ++i) { |
| 4777 | if (is_xmlws(set->val.str[i])) { |
| 4778 | if ((i == 0) || space_before) { |
| 4779 | space_before = 1; |
| 4780 | continue; |
| 4781 | } else { |
| 4782 | space_before = 1; |
| 4783 | } |
| 4784 | } else { |
| 4785 | space_before = 0; |
| 4786 | } |
| 4787 | |
| 4788 | new[new_used] = (space_before ? ' ' : set->val.str[i]); |
| 4789 | ++new_used; |
| 4790 | } |
| 4791 | |
| 4792 | /* at worst there is one trailing space now */ |
| 4793 | if (new_used && is_xmlws(new[new_used - 1])) { |
| 4794 | --new_used; |
| 4795 | } |
| 4796 | |
| 4797 | new = ly_realloc(new, (new_used + 1) * sizeof(char)); |
| 4798 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 4799 | new[new_used] = '\0'; |
| 4800 | |
| 4801 | free(set->val.str); |
| 4802 | set->val.str = new; |
| 4803 | } |
| 4804 | |
| 4805 | return LY_SUCCESS; |
| 4806 | } |
| 4807 | |
| 4808 | /** |
| 4809 | * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN |
| 4810 | * with the argument converted to boolean and logically inverted. |
| 4811 | * |
| 4812 | * @param[in] args Array of arguments. |
| 4813 | * @param[in] arg_count Count of elements in @p args. |
| 4814 | * @param[in,out] set Context and result set at the same time. |
| 4815 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4816 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4817 | */ |
| 4818 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4819 | 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] | 4820 | { |
| 4821 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4822 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4823 | return LY_SUCCESS; |
| 4824 | } |
| 4825 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4826 | lyxp_set_cast(args[0], LYXP_SET_BOOLEAN); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4827 | if (args[0]->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4828 | set_fill_boolean(set, 0); |
| 4829 | } else { |
| 4830 | set_fill_boolean(set, 1); |
| 4831 | } |
| 4832 | |
| 4833 | return LY_SUCCESS; |
| 4834 | } |
| 4835 | |
| 4836 | /** |
| 4837 | * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER |
| 4838 | * with the number representation of either the argument or the context. |
| 4839 | * |
| 4840 | * @param[in] args Array of arguments. |
| 4841 | * @param[in] arg_count Count of elements in @p args. |
| 4842 | * @param[in,out] set Context and result set at the same time. |
| 4843 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4844 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4845 | */ |
| 4846 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4847 | 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] | 4848 | { |
| 4849 | LY_ERR rc; |
| 4850 | |
| 4851 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4852 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4853 | return LY_SUCCESS; |
| 4854 | } |
| 4855 | |
| 4856 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4857 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4858 | LY_CHECK_RET(rc); |
| 4859 | set_fill_set(set, args[0]); |
| 4860 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4861 | rc = lyxp_set_cast(set, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4862 | LY_CHECK_RET(rc); |
| 4863 | } |
| 4864 | |
| 4865 | return LY_SUCCESS; |
| 4866 | } |
| 4867 | |
| 4868 | /** |
| 4869 | * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER |
| 4870 | * with the context position. |
| 4871 | * |
| 4872 | * @param[in] args Array of arguments. |
| 4873 | * @param[in] arg_count Count of elements in @p args. |
| 4874 | * @param[in,out] set Context and result set at the same time. |
| 4875 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4876 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4877 | */ |
| 4878 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4879 | 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] | 4880 | { |
| 4881 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4882 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4883 | return LY_SUCCESS; |
| 4884 | } |
| 4885 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4886 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4887 | LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "position()"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4888 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4889 | } else if (!set->used) { |
| 4890 | set_fill_number(set, 0); |
| 4891 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4892 | } |
| 4893 | |
| 4894 | set_fill_number(set, set->ctx_pos); |
| 4895 | |
| 4896 | /* UNUSED in 'Release' build type */ |
| 4897 | (void)options; |
| 4898 | return LY_SUCCESS; |
| 4899 | } |
| 4900 | |
| 4901 | /** |
| 4902 | * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN |
| 4903 | * depending on whether the second argument regex matches the first argument string. For details refer to |
| 4904 | * YANG 1.1 RFC section 10.2.1. |
| 4905 | * |
| 4906 | * @param[in] args Array of arguments. |
| 4907 | * @param[in] arg_count Count of elements in @p args. |
| 4908 | * @param[in,out] set Context and result set at the same time. |
| 4909 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4910 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4911 | */ |
| 4912 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4913 | 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] | 4914 | { |
| 4915 | struct lysc_pattern **patterns = NULL, **pattern; |
| 4916 | struct lysc_node_leaf *sleaf; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4917 | LY_ERR rc = LY_SUCCESS; |
| 4918 | struct ly_err_item *err; |
| 4919 | |
| 4920 | if (options & LYXP_SCNODE_ALL) { |
| 4921 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4922 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4923 | 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] | 4924 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4925 | 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] | 4926 | } |
| 4927 | } |
| 4928 | |
| 4929 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4930 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4931 | 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] | 4932 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4933 | 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] | 4934 | } |
| 4935 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4936 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4937 | return rc; |
| 4938 | } |
| 4939 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4940 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4941 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4942 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4943 | LY_CHECK_RET(rc); |
| 4944 | |
| 4945 | LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM); |
Radek Iša | 45802b5 | 2021-02-09 09:21:58 +0100 | [diff] [blame] | 4946 | *pattern = calloc(1, sizeof **pattern); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 4947 | if (set->cur_node) { |
| 4948 | LOG_LOCSET(NULL, set->cur_node); |
| 4949 | } |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4950 | 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] | 4951 | if (set->cur_node) { |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 4952 | LOG_LOCBACK(0, 1); |
Michal Vasko | 4a7d4d6 | 2021-12-13 17:05:06 +0100 | [diff] [blame] | 4953 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4954 | if (rc != LY_SUCCESS) { |
| 4955 | LY_ARRAY_FREE(patterns); |
| 4956 | return rc; |
| 4957 | } |
| 4958 | |
Radek Krejci | 0b01330 | 2021-03-29 15:22:32 +0200 | [diff] [blame] | 4959 | 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] | 4960 | pcre2_code_free((*pattern)->code); |
| 4961 | free(*pattern); |
| 4962 | LY_ARRAY_FREE(patterns); |
| 4963 | if (rc && (rc != LY_EVALID)) { |
Michal Vasko | 177d0ed | 2020-11-23 16:43:03 +0100 | [diff] [blame] | 4964 | ly_err_print(set->ctx, err); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4965 | ly_err_free(err); |
| 4966 | return rc; |
| 4967 | } |
| 4968 | |
| 4969 | if (rc == LY_EVALID) { |
| 4970 | ly_err_free(err); |
| 4971 | set_fill_boolean(set, 0); |
| 4972 | } else { |
| 4973 | set_fill_boolean(set, 1); |
| 4974 | } |
| 4975 | |
| 4976 | return LY_SUCCESS; |
| 4977 | } |
| 4978 | |
| 4979 | /** |
| 4980 | * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER |
| 4981 | * with the rounded first argument. For details refer to |
| 4982 | * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round. |
| 4983 | * |
| 4984 | * @param[in] args Array of arguments. |
| 4985 | * @param[in] arg_count Count of elements in @p args. |
| 4986 | * @param[in,out] set Context and result set at the same time. |
| 4987 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4988 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4989 | */ |
| 4990 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 4991 | 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] | 4992 | { |
| 4993 | struct lysc_node_leaf *sleaf; |
| 4994 | LY_ERR rc = LY_SUCCESS; |
| 4995 | |
| 4996 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 4997 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4998 | 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] | 4999 | } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 5000 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5001 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 5002 | sleaf->name); |
| 5003 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) { |
| 5004 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name); |
| 5005 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5006 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5007 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5008 | return rc; |
| 5009 | } |
| 5010 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5011 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5012 | LY_CHECK_RET(rc); |
| 5013 | |
| 5014 | /* cover only the cases where floor can't be used */ |
| 5015 | if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) { |
| 5016 | set_fill_number(set, -0.0f); |
| 5017 | } else { |
| 5018 | args[0]->val.num += 0.5; |
| 5019 | rc = xpath_floor(args, 1, args[0], options); |
| 5020 | LY_CHECK_RET(rc); |
| 5021 | set_fill_number(set, args[0]->val.num); |
| 5022 | } |
| 5023 | |
| 5024 | return LY_SUCCESS; |
| 5025 | } |
| 5026 | |
| 5027 | /** |
| 5028 | * @brief Execute the XPath starts-with(string, string) function. |
| 5029 | * Returns LYXP_SET_BOOLEAN whether the second argument is |
| 5030 | * the prefix of the first or not. |
| 5031 | * |
| 5032 | * @param[in] args Array of arguments. |
| 5033 | * @param[in] arg_count Count of elements in @p args. |
| 5034 | * @param[in,out] set Context and result set at the same time. |
| 5035 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5036 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5037 | */ |
| 5038 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 5039 | 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] | 5040 | { |
| 5041 | struct lysc_node_leaf *sleaf; |
| 5042 | LY_ERR rc = LY_SUCCESS; |
| 5043 | |
| 5044 | if (options & LYXP_SCNODE_ALL) { |
| 5045 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 5046 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5047 | 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] | 5048 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5049 | 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] | 5050 | } |
| 5051 | } |
| 5052 | |
| 5053 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 5054 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5055 | 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] | 5056 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5057 | 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] | 5058 | } |
| 5059 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5060 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5061 | return rc; |
| 5062 | } |
| 5063 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5064 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5065 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5066 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5067 | LY_CHECK_RET(rc); |
| 5068 | |
| 5069 | if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) { |
| 5070 | set_fill_boolean(set, 0); |
| 5071 | } else { |
| 5072 | set_fill_boolean(set, 1); |
| 5073 | } |
| 5074 | |
| 5075 | return LY_SUCCESS; |
| 5076 | } |
| 5077 | |
| 5078 | /** |
| 5079 | * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING |
| 5080 | * with the string representation of either the argument or the context. |
| 5081 | * |
| 5082 | * @param[in] args Array of arguments. |
| 5083 | * @param[in] arg_count Count of elements in @p args. |
| 5084 | * @param[in,out] set Context and result set at the same time. |
| 5085 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5086 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5087 | */ |
| 5088 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 5089 | 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] | 5090 | { |
| 5091 | LY_ERR rc; |
| 5092 | |
| 5093 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5094 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5095 | return LY_SUCCESS; |
| 5096 | } |
| 5097 | |
| 5098 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5099 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5100 | LY_CHECK_RET(rc); |
| 5101 | set_fill_set(set, args[0]); |
| 5102 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5103 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5104 | LY_CHECK_RET(rc); |
| 5105 | } |
| 5106 | |
| 5107 | return LY_SUCCESS; |
| 5108 | } |
| 5109 | |
| 5110 | /** |
| 5111 | * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER |
| 5112 | * with the length of the string in either the argument or the context. |
| 5113 | * |
| 5114 | * @param[in] args Array of arguments. |
| 5115 | * @param[in] arg_count Count of elements in @p args. |
| 5116 | * @param[in,out] set Context and result set at the same time. |
| 5117 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5118 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5119 | */ |
| 5120 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 5121 | 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] | 5122 | { |
| 5123 | struct lysc_node_leaf *sleaf; |
| 5124 | LY_ERR rc = LY_SUCCESS; |
| 5125 | |
| 5126 | if (options & LYXP_SCNODE_ALL) { |
| 5127 | if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 5128 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5129 | 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] | 5130 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5131 | 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] | 5132 | } |
| 5133 | } |
| 5134 | if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) { |
| 5135 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5136 | 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] | 5137 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5138 | 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] | 5139 | } |
| 5140 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5141 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5142 | return rc; |
| 5143 | } |
| 5144 | |
| 5145 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5146 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5147 | LY_CHECK_RET(rc); |
| 5148 | set_fill_number(set, strlen(args[0]->val.str)); |
| 5149 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5150 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5151 | LY_CHECK_RET(rc); |
| 5152 | set_fill_number(set, strlen(set->val.str)); |
| 5153 | } |
| 5154 | |
| 5155 | return LY_SUCCESS; |
| 5156 | } |
| 5157 | |
| 5158 | /** |
| 5159 | * @brief Execute the XPath substring(string, number, number?) function. |
| 5160 | * Returns LYXP_SET_STRING substring of the first argument starting |
| 5161 | * on the second argument index ending on the third argument index, |
| 5162 | * indexed from 1. For exact definition refer to |
| 5163 | * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring. |
| 5164 | * |
| 5165 | * @param[in] args Array of arguments. |
| 5166 | * @param[in] arg_count Count of elements in @p args. |
| 5167 | * @param[in,out] set Context and result set at the same time. |
| 5168 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5169 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5170 | */ |
| 5171 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 5172 | 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] | 5173 | { |
Michal Vasko | 6db996e | 2022-07-28 10:28:04 +0200 | [diff] [blame] | 5174 | int64_t start; |
| 5175 | int32_t len; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 5176 | uint32_t str_start, str_len, pos; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5177 | struct lysc_node_leaf *sleaf; |
| 5178 | LY_ERR rc = LY_SUCCESS; |
| 5179 | |
| 5180 | if (options & LYXP_SCNODE_ALL) { |
| 5181 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 5182 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5183 | 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] | 5184 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5185 | 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] | 5186 | } |
| 5187 | } |
| 5188 | |
| 5189 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 5190 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5191 | 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] | 5192 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 5193 | 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] | 5194 | } |
| 5195 | } |
| 5196 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 5197 | if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET) && |
| 5198 | (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5199 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5200 | 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] | 5201 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 5202 | 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] | 5203 | } |
| 5204 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5205 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5206 | return rc; |
| 5207 | } |
| 5208 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5209 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5210 | LY_CHECK_RET(rc); |
| 5211 | |
| 5212 | /* start */ |
| 5213 | if (xpath_round(&args[1], 1, args[1], options)) { |
| 5214 | return -1; |
| 5215 | } |
| 5216 | if (isfinite(args[1]->val.num)) { |
| 5217 | start = args[1]->val.num - 1; |
| 5218 | } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5219 | start = INT32_MIN; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5220 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5221 | start = INT32_MAX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5222 | } |
| 5223 | |
| 5224 | /* len */ |
| 5225 | if (arg_count == 3) { |
| 5226 | rc = xpath_round(&args[2], 1, args[2], options); |
| 5227 | LY_CHECK_RET(rc); |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5228 | if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5229 | len = 0; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5230 | } else if (isfinite(args[2]->val.num)) { |
| 5231 | len = args[2]->val.num; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5232 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5233 | len = INT32_MAX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5234 | } |
| 5235 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5236 | len = INT32_MAX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5237 | } |
| 5238 | |
| 5239 | /* find matching character positions */ |
| 5240 | str_start = 0; |
| 5241 | str_len = 0; |
| 5242 | for (pos = 0; args[0]->val.str[pos]; ++pos) { |
| 5243 | if (pos < start) { |
| 5244 | ++str_start; |
| 5245 | } else if (pos < start + len) { |
| 5246 | ++str_len; |
| 5247 | } else { |
| 5248 | break; |
| 5249 | } |
| 5250 | } |
| 5251 | |
| 5252 | set_fill_string(set, args[0]->val.str + str_start, str_len); |
| 5253 | return LY_SUCCESS; |
| 5254 | } |
| 5255 | |
| 5256 | /** |
| 5257 | * @brief Execute the XPath substring-after(string, string) function. |
| 5258 | * Returns LYXP_SET_STRING with the string succeeding the occurance |
| 5259 | * of the second argument in the first or an empty string. |
| 5260 | * |
| 5261 | * @param[in] args Array of arguments. |
| 5262 | * @param[in] arg_count Count of elements in @p args. |
| 5263 | * @param[in,out] set Context and result set at the same time. |
| 5264 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5265 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5266 | */ |
| 5267 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 5268 | 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] | 5269 | { |
| 5270 | char *ptr; |
| 5271 | struct lysc_node_leaf *sleaf; |
| 5272 | LY_ERR rc = LY_SUCCESS; |
| 5273 | |
| 5274 | if (options & LYXP_SCNODE_ALL) { |
| 5275 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 5276 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5277 | 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] | 5278 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5279 | 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] | 5280 | } |
| 5281 | } |
| 5282 | |
| 5283 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 5284 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5285 | 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] | 5286 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5287 | 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] | 5288 | } |
| 5289 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5290 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5291 | return rc; |
| 5292 | } |
| 5293 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5294 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5295 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5296 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5297 | LY_CHECK_RET(rc); |
| 5298 | |
| 5299 | ptr = strstr(args[0]->val.str, args[1]->val.str); |
| 5300 | if (ptr) { |
| 5301 | set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str))); |
| 5302 | } else { |
| 5303 | set_fill_string(set, "", 0); |
| 5304 | } |
| 5305 | |
| 5306 | return LY_SUCCESS; |
| 5307 | } |
| 5308 | |
| 5309 | /** |
| 5310 | * @brief Execute the XPath substring-before(string, string) function. |
| 5311 | * Returns LYXP_SET_STRING with the string preceding the occurance |
| 5312 | * of the second argument in the first or an empty string. |
| 5313 | * |
| 5314 | * @param[in] args Array of arguments. |
| 5315 | * @param[in] arg_count Count of elements in @p args. |
| 5316 | * @param[in,out] set Context and result set at the same time. |
| 5317 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5318 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5319 | */ |
| 5320 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 5321 | 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] | 5322 | { |
| 5323 | char *ptr; |
| 5324 | struct lysc_node_leaf *sleaf; |
| 5325 | LY_ERR rc = LY_SUCCESS; |
| 5326 | |
| 5327 | if (options & LYXP_SCNODE_ALL) { |
| 5328 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 5329 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5330 | 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] | 5331 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5332 | 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] | 5333 | } |
| 5334 | } |
| 5335 | |
| 5336 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 5337 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5338 | 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] | 5339 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5340 | 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] | 5341 | } |
| 5342 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5343 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5344 | return rc; |
| 5345 | } |
| 5346 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5347 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5348 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5349 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5350 | LY_CHECK_RET(rc); |
| 5351 | |
| 5352 | ptr = strstr(args[0]->val.str, args[1]->val.str); |
| 5353 | if (ptr) { |
| 5354 | set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str); |
| 5355 | } else { |
| 5356 | set_fill_string(set, "", 0); |
| 5357 | } |
| 5358 | |
| 5359 | return LY_SUCCESS; |
| 5360 | } |
| 5361 | |
| 5362 | /** |
| 5363 | * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER |
| 5364 | * with the sum of all the nodes in the context. |
| 5365 | * |
| 5366 | * @param[in] args Array of arguments. |
| 5367 | * @param[in] arg_count Count of elements in @p args. |
| 5368 | * @param[in,out] set Context and result set at the same time. |
| 5369 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5370 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5371 | */ |
| 5372 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 5373 | 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] | 5374 | { |
| 5375 | long double num; |
| 5376 | char *str; |
Michal Vasko | 1fdd8fa | 2021-01-08 09:21:45 +0100 | [diff] [blame] | 5377 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5378 | struct lyxp_set set_item; |
| 5379 | struct lysc_node_leaf *sleaf; |
| 5380 | LY_ERR rc = LY_SUCCESS; |
| 5381 | |
| 5382 | if (options & LYXP_SCNODE_ALL) { |
| 5383 | if (args[0]->type == LYXP_SET_SCNODE_SET) { |
| 5384 | for (i = 0; i < args[0]->used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 5385 | 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] | 5386 | sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode; |
| 5387 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5388 | 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] | 5389 | lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5390 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 5391 | 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] | 5392 | } |
| 5393 | } |
| 5394 | } |
| 5395 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5396 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5397 | return rc; |
| 5398 | } |
| 5399 | |
| 5400 | set_fill_number(set, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5401 | |
| 5402 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 5403 | 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] | 5404 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5405 | } else if (!args[0]->used) { |
| 5406 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5407 | } |
| 5408 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5409 | set_init(&set_item, set); |
| 5410 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5411 | set_item.type = LYXP_SET_NODE_SET; |
Michal Vasko | 41decbf | 2021-11-02 11:50:21 +0100 | [diff] [blame] | 5412 | set_item.val.nodes = calloc(1, sizeof *set_item.val.nodes); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5413 | LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM); |
| 5414 | |
| 5415 | set_item.used = 1; |
| 5416 | set_item.size = 1; |
| 5417 | |
| 5418 | for (i = 0; i < args[0]->used; ++i) { |
| 5419 | set_item.val.nodes[0] = args[0]->val.nodes[i]; |
| 5420 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5421 | rc = cast_node_set_to_string(&set_item, &str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5422 | LY_CHECK_RET(rc); |
| 5423 | num = cast_string_to_number(str); |
| 5424 | free(str); |
| 5425 | set->val.num += num; |
| 5426 | } |
| 5427 | |
| 5428 | free(set_item.val.nodes); |
| 5429 | |
| 5430 | return LY_SUCCESS; |
| 5431 | } |
| 5432 | |
| 5433 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5434 | * @brief Execute the XPath translate(string, string, string) function. |
| 5435 | * Returns LYXP_SET_STRING with the first argument with the characters |
| 5436 | * from the second argument replaced by those on the corresponding |
| 5437 | * positions in the third argument. |
| 5438 | * |
| 5439 | * @param[in] args Array of arguments. |
| 5440 | * @param[in] arg_count Count of elements in @p args. |
| 5441 | * @param[in,out] set Context and result set at the same time. |
| 5442 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5443 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5444 | */ |
| 5445 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 5446 | 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] | 5447 | { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 5448 | uint32_t i, j, new_used; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5449 | char *new; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 5450 | ly_bool have_removed; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5451 | struct lysc_node_leaf *sleaf; |
| 5452 | LY_ERR rc = LY_SUCCESS; |
| 5453 | |
| 5454 | if (options & LYXP_SCNODE_ALL) { |
| 5455 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 5456 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5457 | 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] | 5458 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5459 | 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] | 5460 | } |
| 5461 | } |
| 5462 | |
| 5463 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 5464 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5465 | 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] | 5466 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5467 | 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] | 5468 | } |
| 5469 | } |
| 5470 | |
| 5471 | if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) { |
| 5472 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5473 | 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] | 5474 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5475 | 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] | 5476 | } |
| 5477 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5478 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5479 | return rc; |
| 5480 | } |
| 5481 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5482 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5483 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5484 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5485 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5486 | rc = lyxp_set_cast(args[2], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5487 | LY_CHECK_RET(rc); |
| 5488 | |
| 5489 | new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char)); |
| 5490 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 5491 | new_used = 0; |
| 5492 | |
| 5493 | have_removed = 0; |
| 5494 | for (i = 0; args[0]->val.str[i]; ++i) { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 5495 | ly_bool found = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5496 | |
| 5497 | for (j = 0; args[1]->val.str[j]; ++j) { |
| 5498 | if (args[0]->val.str[i] == args[1]->val.str[j]) { |
| 5499 | /* removing this char */ |
| 5500 | if (j >= strlen(args[2]->val.str)) { |
| 5501 | have_removed = 1; |
| 5502 | found = 1; |
| 5503 | break; |
| 5504 | } |
| 5505 | /* replacing this char */ |
| 5506 | new[new_used] = args[2]->val.str[j]; |
| 5507 | ++new_used; |
| 5508 | found = 1; |
| 5509 | break; |
| 5510 | } |
| 5511 | } |
| 5512 | |
| 5513 | /* copying this char */ |
| 5514 | if (!found) { |
| 5515 | new[new_used] = args[0]->val.str[i]; |
| 5516 | ++new_used; |
| 5517 | } |
| 5518 | } |
| 5519 | |
| 5520 | if (have_removed) { |
| 5521 | new = ly_realloc(new, (new_used + 1) * sizeof(char)); |
| 5522 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 5523 | } |
| 5524 | new[new_used] = '\0'; |
| 5525 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5526 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5527 | set->type = LYXP_SET_STRING; |
| 5528 | set->val.str = new; |
| 5529 | |
| 5530 | return LY_SUCCESS; |
| 5531 | } |
| 5532 | |
| 5533 | /** |
| 5534 | * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN |
| 5535 | * with true value. |
| 5536 | * |
| 5537 | * @param[in] args Array of arguments. |
| 5538 | * @param[in] arg_count Count of elements in @p args. |
| 5539 | * @param[in,out] set Context and result set at the same time. |
| 5540 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5541 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5542 | */ |
| 5543 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 5544 | 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] | 5545 | { |
| 5546 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5547 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5548 | return LY_SUCCESS; |
| 5549 | } |
| 5550 | |
| 5551 | set_fill_boolean(set, 1); |
| 5552 | return LY_SUCCESS; |
| 5553 | } |
| 5554 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5555 | /** |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5556 | * @brief Execute the XPath node() processing instruction (node type). Returns LYXP_SET_NODE_SET |
| 5557 | * with only nodes from the context. |
| 5558 | * |
| 5559 | * @param[in,out] set Context and result set at the same time. |
| 5560 | * @param[in] axis Axis to search on. |
| 5561 | * @param[in] options XPath options. |
| 5562 | * @return LY_ERR |
| 5563 | */ |
| 5564 | static LY_ERR |
| 5565 | xpath_pi_node(struct lyxp_set *set, enum lyxp_axis axis, uint32_t options) |
| 5566 | { |
| 5567 | if (options & LYXP_SCNODE_ALL) { |
| 5568 | return moveto_scnode(set, NULL, NULL, axis, options); |
| 5569 | } |
| 5570 | |
| 5571 | if (set->type != LYXP_SET_NODE_SET) { |
| 5572 | lyxp_set_free_content(set); |
| 5573 | return LY_SUCCESS; |
| 5574 | } |
| 5575 | |
| 5576 | /* just like moving to a node with no restrictions */ |
| 5577 | return moveto_node(set, NULL, NULL, axis, options); |
| 5578 | } |
| 5579 | |
| 5580 | /** |
| 5581 | * @brief Execute the XPath text() processing instruction (node type). Returns LYXP_SET_NODE_SET |
| 5582 | * with the text content of the nodes in the context. |
| 5583 | * |
| 5584 | * @param[in,out] set Context and result set at the same time. |
| 5585 | * @param[in] axis Axis to search on. |
| 5586 | * @param[in] options XPath options. |
| 5587 | * @return LY_ERR |
| 5588 | */ |
| 5589 | static LY_ERR |
| 5590 | xpath_pi_text(struct lyxp_set *set, enum lyxp_axis axis, uint32_t options) |
| 5591 | { |
| 5592 | uint32_t i; |
| 5593 | |
| 5594 | if (options & LYXP_SCNODE_ALL) { |
| 5595 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
| 5596 | return LY_SUCCESS; |
| 5597 | } |
| 5598 | |
| 5599 | if (set->type != LYXP_SET_NODE_SET) { |
| 5600 | LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "text()"); |
| 5601 | return LY_EVALID; |
| 5602 | } |
| 5603 | |
| 5604 | if (axis != LYXP_AXIS_CHILD) { |
| 5605 | /* even following and preceding axescan return text nodes, but whatever */ |
| 5606 | lyxp_set_free_content(set); |
| 5607 | return LY_SUCCESS; |
| 5608 | } |
| 5609 | |
| 5610 | for (i = 0; i < set->used; ++i) { |
| 5611 | switch (set->val.nodes[i].type) { |
| 5612 | case LYXP_NODE_NONE: |
| 5613 | LOGINT_RET(set->ctx); |
| 5614 | case LYXP_NODE_ELEM: |
Michal Vasko | 222be68 | 2023-08-24 08:17:12 +0200 | [diff] [blame] | 5615 | 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] | 5616 | set->val.nodes[i].type = LYXP_NODE_TEXT; |
| 5617 | break; |
| 5618 | } |
| 5619 | /* fall through */ |
| 5620 | case LYXP_NODE_ROOT: |
| 5621 | case LYXP_NODE_ROOT_CONFIG: |
| 5622 | case LYXP_NODE_TEXT: |
| 5623 | case LYXP_NODE_META: |
| 5624 | set_remove_node_none(set, i); |
| 5625 | break; |
| 5626 | } |
| 5627 | } |
| 5628 | set_remove_nodes_none(set); |
| 5629 | |
| 5630 | return LY_SUCCESS; |
| 5631 | } |
| 5632 | |
| 5633 | /** |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5634 | * @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] | 5635 | * |
Michal Vasko | 2104e9f | 2020-03-06 08:23:25 +0100 | [diff] [blame] | 5636 | * XPath @p set is expected to be a (sc)node set! |
| 5637 | * |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5638 | * @param[in,out] qname Qualified node name. If includes prefix, it is skipped. |
| 5639 | * @param[in,out] qname_len Length of @p qname, is updated accordingly. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5640 | * @param[in] set Set with general XPath context. |
| 5641 | * @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] | 5642 | * @param[out] moveto_mod Expected module of a matching node. |
| 5643 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5644 | */ |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5645 | static LY_ERR |
Michal Vasko | fe1af04 | 2022-07-29 14:58:59 +0200 | [diff] [blame] | 5646 | 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] | 5647 | const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5648 | { |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 5649 | const struct lys_module *mod = NULL; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5650 | const char *ptr; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5651 | size_t pref_len; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5652 | |
Michal Vasko | 2104e9f | 2020-03-06 08:23:25 +0100 | [diff] [blame] | 5653 | assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET)); |
| 5654 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5655 | if ((ptr = ly_strnchr(*qname, ':', *qname_len))) { |
| 5656 | /* specific module */ |
| 5657 | pref_len = ptr - *qname; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5658 | 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] | 5659 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5660 | /* check for errors and non-implemented modules, as they are not valid */ |
Juraj Vijtiuk | d75faa6 | 2019-11-26 14:10:10 +0100 | [diff] [blame] | 5661 | if (!mod || !mod->implemented) { |
Michal Vasko | 7b3a00e | 2023-08-09 11:58:03 +0200 | [diff] [blame] | 5662 | LOGVAL(set->ctx, LY_VCODE_XP_INMOD, (int)pref_len, *qname); |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5663 | return LY_EVALID; |
| 5664 | } |
Juraj Vijtiuk | d75faa6 | 2019-11-26 14:10:10 +0100 | [diff] [blame] | 5665 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5666 | *qname += pref_len + 1; |
| 5667 | *qname_len -= pref_len + 1; |
| 5668 | } else if (((*qname)[0] == '*') && (*qname_len == 1)) { |
| 5669 | /* all modules - special case */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5670 | mod = NULL; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5671 | } else { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5672 | switch (set->format) { |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 5673 | case LY_VALUE_SCHEMA: |
| 5674 | case LY_VALUE_SCHEMA_RESOLVED: |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5675 | /* current module */ |
| 5676 | mod = set->cur_mod; |
| 5677 | break; |
Radek Krejci | 224d4b4 | 2021-04-23 13:54:59 +0200 | [diff] [blame] | 5678 | case LY_VALUE_CANON: |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 5679 | case LY_VALUE_JSON: |
Radek Krejci | f994364 | 2021-04-26 10:18:21 +0200 | [diff] [blame] | 5680 | case LY_VALUE_LYB: |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 5681 | case LY_VALUE_STR_NS: |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5682 | /* inherit parent (context node) module */ |
| 5683 | if (ctx_scnode) { |
| 5684 | mod = ctx_scnode->module; |
| 5685 | } else { |
| 5686 | mod = NULL; |
| 5687 | } |
| 5688 | break; |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 5689 | case LY_VALUE_XML: |
Michal Vasko | 52143d1 | 2021-04-14 15:36:39 +0200 | [diff] [blame] | 5690 | /* all nodes need to be prefixed */ |
Michal Vasko | 21eaa39 | 2024-02-20 15:48:42 +0100 | [diff] [blame] | 5691 | LOGVAL(set->ctx, LYVE_DATA, "Non-prefixed node \"%.*s\" in XML xpath found.", (int)*qname_len, *qname); |
Michal Vasko | 52143d1 | 2021-04-14 15:36:39 +0200 | [diff] [blame] | 5692 | return LY_EVALID; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5693 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5694 | } |
| 5695 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5696 | *moveto_mod = mod; |
| 5697 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5698 | } |
| 5699 | |
| 5700 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5701 | * @brief Move context @p set to the root. Handles absolute path. |
| 5702 | * Result is LYXP_SET_NODE_SET. |
| 5703 | * |
| 5704 | * @param[in,out] set Set to use. |
| 5705 | * @param[in] options Xpath options. |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 5706 | * @return LY_ERR value. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5707 | */ |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 5708 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5709 | moveto_root(struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5710 | { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 5711 | assert(!(options & LYXP_SKIP_EXPR)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5712 | |
| 5713 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5714 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | e4a6d01 | 2023-05-22 14:34:52 +0200 | [diff] [blame] | 5715 | 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] | 5716 | } else { |
Michal Vasko | ef71b39 | 2023-08-16 09:45:23 +0200 | [diff] [blame] | 5717 | lyxp_set_free_content(set); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5718 | set_insert_node(set, NULL, 0, set->root_type, 0); |
Michal Vasko | 306e283 | 2022-07-25 09:15:17 +0200 | [diff] [blame] | 5719 | set->non_child_axis = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5720 | } |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 5721 | |
| 5722 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5723 | } |
| 5724 | |
| 5725 | /** |
| 5726 | * @brief Check @p node as a part of NameTest processing. |
| 5727 | * |
| 5728 | * @param[in] node Node to check. |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5729 | * @param[in] node_type Node type of @p node. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5730 | * @param[in] set Set to read general context from. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5731 | * @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] | 5732 | * @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] | 5733 | * @param[in] options XPath options. |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5734 | * @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] | 5735 | * LY_EINVAL if neither node nor any children match) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5736 | */ |
| 5737 | static LY_ERR |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5738 | moveto_node_check(const struct lyd_node *node, enum lyxp_node_type node_type, const struct lyxp_set *set, |
| 5739 | 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] | 5740 | { |
Michal Vasko | fbd0da4 | 2023-08-17 14:57:44 +0200 | [diff] [blame] | 5741 | const struct lysc_node *schema; |
| 5742 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5743 | if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) { |
| 5744 | assert(node_type == set->root_type); |
| 5745 | |
| 5746 | if (node_name || moveto_mod) { |
| 5747 | /* root will not match a specific node */ |
| 5748 | return LY_ENOT; |
| 5749 | } |
| 5750 | return LY_SUCCESS; |
| 5751 | } else if (node_type != LYXP_NODE_ELEM) { |
| 5752 | /* other types will not match */ |
| 5753 | return LY_ENOT; |
| 5754 | } |
| 5755 | |
Michal Vasko | fbd0da4 | 2023-08-17 14:57:44 +0200 | [diff] [blame] | 5756 | /* get schema node even of an opaque node */ |
| 5757 | schema = lyd_node_schema(node); |
| 5758 | if (!schema) { |
| 5759 | /* unknown opaque node never matches */ |
Michal Vasko | dca9f12 | 2021-07-16 13:56:22 +0200 | [diff] [blame] | 5760 | return LY_ENOT; |
| 5761 | } |
| 5762 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5763 | /* module check */ |
Michal Vasko | 19089f0 | 2022-06-07 11:02:11 +0200 | [diff] [blame] | 5764 | if (moveto_mod) { |
Michal Vasko | fbd0da4 | 2023-08-17 14:57:44 +0200 | [diff] [blame] | 5765 | if ((set->ctx == LYD_CTX(node)) && (schema->module != moveto_mod)) { |
Michal Vasko | 19089f0 | 2022-06-07 11:02:11 +0200 | [diff] [blame] | 5766 | return LY_ENOT; |
Michal Vasko | fbd0da4 | 2023-08-17 14:57:44 +0200 | [diff] [blame] | 5767 | } 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] | 5768 | return LY_ENOT; |
| 5769 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5770 | } |
| 5771 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5772 | /* context check */ |
Michal Vasko | fbd0da4 | 2023-08-17 14:57:44 +0200 | [diff] [blame] | 5773 | 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] | 5774 | return LY_EINVAL; |
Michal Vasko | fbd0da4 | 2023-08-17 14:57:44 +0200 | [diff] [blame] | 5775 | } 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] | 5776 | return LY_EINVAL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5777 | } |
| 5778 | |
| 5779 | /* name check */ |
Michal Vasko | 19089f0 | 2022-06-07 11:02:11 +0200 | [diff] [blame] | 5780 | if (node_name) { |
Michal Vasko | fbd0da4 | 2023-08-17 14:57:44 +0200 | [diff] [blame] | 5781 | if ((set->ctx == LYD_CTX(node)) && (schema->name != node_name)) { |
Michal Vasko | 19089f0 | 2022-06-07 11:02:11 +0200 | [diff] [blame] | 5782 | return LY_ENOT; |
Michal Vasko | fbd0da4 | 2023-08-17 14:57:44 +0200 | [diff] [blame] | 5783 | } else if ((set->ctx != LYD_CTX(node)) && strcmp(schema->name, node_name)) { |
Michal Vasko | 19089f0 | 2022-06-07 11:02:11 +0200 | [diff] [blame] | 5784 | return LY_ENOT; |
| 5785 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5786 | } |
| 5787 | |
Michal Vasko | 6d65712 | 2022-10-19 14:38:35 +0200 | [diff] [blame] | 5788 | /* 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] | 5789 | 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] | 5790 | (node != set->cur_node)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5791 | return LY_EINCOMPLETE; |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 5792 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5793 | |
| 5794 | /* match */ |
| 5795 | return LY_SUCCESS; |
| 5796 | } |
| 5797 | |
| 5798 | /** |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5799 | * @brief Get the next node in a forward DFS. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5800 | * |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5801 | * @param[in] iter Last returned node. |
| 5802 | * @param[in] stop Node to stop the search on and not return. |
| 5803 | * @return Next node, NULL if there are no more. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5804 | */ |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5805 | static const struct lyd_node * |
| 5806 | 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] | 5807 | { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5808 | const struct lyd_node *next = NULL; |
| 5809 | |
| 5810 | /* 1) child */ |
| 5811 | next = lyd_child(iter); |
| 5812 | if (!next) { |
| 5813 | if (iter == stop) { |
| 5814 | /* reached stop, no more descendants */ |
| 5815 | return NULL; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5816 | } |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5817 | /* 2) child next sibling */ |
| 5818 | next = iter->next; |
| 5819 | } |
| 5820 | while (!next) { |
| 5821 | iter = lyd_parent(iter); |
| 5822 | if ((!stop && !iter) || (stop && (lyd_parent(iter) == lyd_parent(stop)))) { |
| 5823 | return NULL; |
| 5824 | } |
| 5825 | next = iter->next; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5826 | } |
| 5827 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5828 | return next; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5829 | } |
| 5830 | |
| 5831 | /** |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 5832 | * @brief Get the next node in a backward DFS. |
| 5833 | * |
| 5834 | * @param[in] iter Last returned node. |
| 5835 | * @param[in] stop Node to stop the search on and not return. |
| 5836 | * @return Next node, NULL if there are no more. |
| 5837 | */ |
| 5838 | static const struct lyd_node * |
| 5839 | moveto_axis_node_next_dfs_backward(const struct lyd_node *iter, const struct lyd_node *stop) |
| 5840 | { |
| 5841 | const struct lyd_node *next = NULL; |
| 5842 | |
| 5843 | /* 1) previous sibling innermost last child */ |
| 5844 | next = iter->prev->next ? iter->prev : NULL; |
| 5845 | while (next && lyd_child(next)) { |
| 5846 | next = lyd_child(next); |
| 5847 | next = next->prev; |
| 5848 | } |
| 5849 | |
| 5850 | if (!next) { |
| 5851 | /* 2) parent */ |
| 5852 | iter = lyd_parent(iter); |
| 5853 | if ((!stop && !iter) || (stop && (lyd_parent(iter) == lyd_parent(stop)))) { |
| 5854 | return NULL; |
| 5855 | } |
| 5856 | next = iter; |
| 5857 | } |
| 5858 | |
| 5859 | return next; |
| 5860 | } |
| 5861 | |
| 5862 | /** |
| 5863 | * @brief Get the first node on an axis for a context node. |
| 5864 | * |
| 5865 | * @param[in,out] iter NULL, updated to the next node. |
| 5866 | * @param[in,out] iter_type Node type 0 of @p iter, updated to the node type of the next node. |
| 5867 | * @param[in] node Context node. |
| 5868 | * @param[in] node_type Type of @p node. |
| 5869 | * @param[in] axis Axis to use. |
| 5870 | * @param[in] set XPath set with the general context. |
| 5871 | * @return LY_SUCCESS on success. |
| 5872 | * @return LY_ENOTFOUND if no next node found. |
| 5873 | */ |
| 5874 | static LY_ERR |
| 5875 | moveto_axis_node_next_first(const struct lyd_node **iter, enum lyxp_node_type *iter_type, const struct lyd_node *node, |
| 5876 | enum lyxp_node_type node_type, enum lyxp_axis axis, struct lyxp_set *set) |
| 5877 | { |
| 5878 | const struct lyd_node *next = NULL; |
| 5879 | enum lyxp_node_type next_type = 0; |
| 5880 | |
| 5881 | assert(!*iter); |
| 5882 | assert(!*iter_type); |
| 5883 | |
| 5884 | switch (axis) { |
| 5885 | case LYXP_AXIS_ANCESTOR_OR_SELF: |
| 5886 | case LYXP_AXIS_DESCENDANT_OR_SELF: |
| 5887 | case LYXP_AXIS_SELF: |
| 5888 | /* return the context node */ |
| 5889 | next = node; |
| 5890 | next_type = node_type; |
| 5891 | break; |
| 5892 | |
| 5893 | case LYXP_AXIS_ANCESTOR: |
| 5894 | case LYXP_AXIS_PARENT: |
| 5895 | if (node_type == LYXP_NODE_ELEM) { |
| 5896 | next = lyd_parent(node); |
| 5897 | next_type = next ? LYXP_NODE_ELEM : set->root_type; |
| 5898 | } else if (node_type == LYXP_NODE_TEXT) { |
| 5899 | next = node; |
| 5900 | next_type = LYXP_NODE_ELEM; |
| 5901 | } else if (node_type == LYXP_NODE_META) { |
| 5902 | next = ((struct lyd_meta *)node)->parent; |
| 5903 | next_type = LYXP_NODE_ELEM; |
| 5904 | } /* else root does not have a parent */ |
| 5905 | break; |
| 5906 | |
| 5907 | case LYXP_AXIS_CHILD: |
| 5908 | if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) { |
| 5909 | assert(!node); |
| 5910 | |
| 5911 | /* search in all the trees */ |
| 5912 | next = set->tree; |
| 5913 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5914 | } else { |
| 5915 | /* search in children */ |
| 5916 | next = lyd_child(node); |
| 5917 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5918 | } |
| 5919 | break; |
| 5920 | |
| 5921 | case LYXP_AXIS_DESCENDANT: |
| 5922 | if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) { |
| 5923 | /* top-level nodes */ |
| 5924 | next = set->tree; |
| 5925 | next_type = LYXP_NODE_ELEM; |
| 5926 | } else if (node_type == LYXP_NODE_ELEM) { |
| 5927 | /* start from the context node */ |
| 5928 | next = moveto_axis_node_next_dfs_forward(node, node); |
| 5929 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5930 | } /* else no children */ |
| 5931 | break; |
| 5932 | |
| 5933 | case LYXP_AXIS_FOLLOWING: |
| 5934 | case LYXP_AXIS_FOLLOWING_SIBLING: |
| 5935 | if (node_type == LYXP_NODE_ELEM) { |
| 5936 | /* first next sibling */ |
| 5937 | next = node->next; |
| 5938 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5939 | } /* else no sibling */ |
| 5940 | break; |
| 5941 | |
| 5942 | case LYXP_AXIS_PRECEDING: |
| 5943 | if ((node_type == LYXP_NODE_ELEM) && node->prev->next) { |
| 5944 | /* skip ancestors */ |
| 5945 | next = moveto_axis_node_next_dfs_backward(node, NULL); |
| 5946 | assert(next); |
| 5947 | next_type = LYXP_NODE_ELEM; |
| 5948 | } /* else no sibling */ |
| 5949 | break; |
| 5950 | |
| 5951 | case LYXP_AXIS_PRECEDING_SIBLING: |
| 5952 | if (node_type == LYXP_NODE_ELEM) { |
| 5953 | /* first previous sibling */ |
| 5954 | next = node->prev->next ? node->prev : NULL; |
| 5955 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 5956 | } /* else no sibling */ |
| 5957 | break; |
| 5958 | |
| 5959 | case LYXP_AXIS_ATTRIBUTE: |
| 5960 | /* handled specially */ |
| 5961 | assert(0); |
| 5962 | LOGINT(set->ctx); |
| 5963 | break; |
| 5964 | } |
| 5965 | |
| 5966 | *iter = next; |
| 5967 | *iter_type = next_type; |
| 5968 | return next_type ? LY_SUCCESS : LY_ENOTFOUND; |
| 5969 | } |
| 5970 | |
| 5971 | /** |
| 5972 | * @brief Iterate over all nodes on an axis for a context node. |
| 5973 | * |
| 5974 | * @param[in,out] iter Last returned node, start with NULL, updated to the next node. |
| 5975 | * @param[in,out] iter_type Node type of @p iter, start with 0, updated to the node type of the next node. |
| 5976 | * @param[in] node Context node. |
| 5977 | * @param[in] node_type Type of @p node. |
| 5978 | * @param[in] axis Axis to use. |
| 5979 | * @param[in] set XPath set with the general context. |
| 5980 | * @return LY_SUCCESS on success. |
| 5981 | * @return LY_ENOTFOUND if no next node found. |
| 5982 | */ |
| 5983 | static LY_ERR |
| 5984 | moveto_axis_node_next(const struct lyd_node **iter, enum lyxp_node_type *iter_type, const struct lyd_node *node, |
| 5985 | enum lyxp_node_type node_type, enum lyxp_axis axis, struct lyxp_set *set) |
| 5986 | { |
| 5987 | const struct lyd_node *next = NULL; |
| 5988 | enum lyxp_node_type next_type = 0; |
| 5989 | |
| 5990 | if (!*iter_type) { |
| 5991 | /* first returned node */ |
| 5992 | return moveto_axis_node_next_first(iter, iter_type, node, node_type, axis, set); |
| 5993 | } |
| 5994 | |
| 5995 | switch (axis) { |
| 5996 | case LYXP_AXIS_ANCESTOR_OR_SELF: |
| 5997 | if ((*iter == node) && (*iter_type == node_type)) { |
| 5998 | /* fake first ancestor, we returned self before */ |
| 5999 | *iter = NULL; |
| 6000 | *iter_type = 0; |
| 6001 | return moveto_axis_node_next_first(iter, iter_type, node, node_type, LYXP_AXIS_ANCESTOR, set); |
| 6002 | } /* else continue ancestor */ |
| 6003 | |
| 6004 | /* fallthrough */ |
| 6005 | case LYXP_AXIS_ANCESTOR: |
| 6006 | if (*iter_type == LYXP_NODE_ELEM) { |
| 6007 | /* iter parent */ |
| 6008 | next = lyd_parent(*iter); |
| 6009 | next_type = next ? LYXP_NODE_ELEM : set->root_type; |
| 6010 | } /* else root, no ancestors */ |
| 6011 | break; |
| 6012 | |
| 6013 | case LYXP_AXIS_CHILD: |
| 6014 | assert(*iter_type == LYXP_NODE_ELEM); |
| 6015 | |
| 6016 | /* next sibling (child) */ |
| 6017 | next = (*iter)->next; |
| 6018 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 6019 | break; |
| 6020 | |
| 6021 | case LYXP_AXIS_DESCENDANT_OR_SELF: |
| 6022 | if ((*iter == node) && (*iter_type == node_type)) { |
| 6023 | /* fake first descendant, we returned self before */ |
| 6024 | *iter = NULL; |
| 6025 | *iter_type = 0; |
| 6026 | return moveto_axis_node_next_first(iter, iter_type, node, node_type, LYXP_AXIS_DESCENDANT, set); |
| 6027 | } /* else continue descendant */ |
| 6028 | |
| 6029 | /* fallthrough */ |
| 6030 | case LYXP_AXIS_DESCENDANT: |
| 6031 | assert(*iter_type == LYXP_NODE_ELEM); |
| 6032 | next = moveto_axis_node_next_dfs_forward(*iter, node); |
| 6033 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 6034 | break; |
| 6035 | |
| 6036 | case LYXP_AXIS_FOLLOWING: |
| 6037 | assert(*iter_type == LYXP_NODE_ELEM); |
| 6038 | next = moveto_axis_node_next_dfs_forward(*iter, NULL); |
| 6039 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 6040 | break; |
| 6041 | |
| 6042 | case LYXP_AXIS_FOLLOWING_SIBLING: |
| 6043 | assert(*iter_type == LYXP_NODE_ELEM); |
| 6044 | |
| 6045 | /* next sibling */ |
| 6046 | next = (*iter)->next; |
| 6047 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 6048 | break; |
| 6049 | |
| 6050 | case LYXP_AXIS_PARENT: |
| 6051 | case LYXP_AXIS_SELF: |
| 6052 | /* parent/self was returned before */ |
| 6053 | break; |
| 6054 | |
| 6055 | case LYXP_AXIS_PRECEDING: |
| 6056 | assert(*iter_type == LYXP_NODE_ELEM); |
| 6057 | next = moveto_axis_node_next_dfs_backward(*iter, NULL); |
| 6058 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 6059 | break; |
| 6060 | |
| 6061 | case LYXP_AXIS_PRECEDING_SIBLING: |
| 6062 | assert(*iter_type == LYXP_NODE_ELEM); |
| 6063 | |
| 6064 | /* previous sibling */ |
| 6065 | next = (*iter)->prev->next ? (*iter)->prev : NULL; |
| 6066 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 6067 | break; |
| 6068 | |
| 6069 | case LYXP_AXIS_ATTRIBUTE: |
| 6070 | /* handled specially */ |
| 6071 | assert(0); |
| 6072 | LOGINT(set->ctx); |
| 6073 | break; |
| 6074 | } |
| 6075 | |
| 6076 | *iter = next; |
| 6077 | *iter_type = next_type; |
| 6078 | return next_type ? LY_SUCCESS : LY_ENOTFOUND; |
| 6079 | } |
| 6080 | |
| 6081 | /** |
| 6082 | * @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] | 6083 | * |
| 6084 | * @param[in,out] set Set to use. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6085 | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6086 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6087 | * @param[in] axis Axis to search on. |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 6088 | * @param[in] options XPath options. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6089 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6090 | */ |
| 6091 | static LY_ERR |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6092 | moveto_node(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, enum lyxp_axis axis, |
| 6093 | uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6094 | { |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6095 | LY_ERR r, rc = LY_SUCCESS; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6096 | const struct lyd_node *iter; |
| 6097 | enum lyxp_node_type iter_type; |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6098 | struct lyxp_set result; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6099 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6100 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6101 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6102 | return LY_SUCCESS; |
| 6103 | } |
| 6104 | |
| 6105 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 6106 | 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] | 6107 | return LY_EVALID; |
| 6108 | } |
| 6109 | |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6110 | /* init result set */ |
| 6111 | set_init(&result, set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6112 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6113 | for (i = 0; i < set->used; ++i) { |
| 6114 | /* iterate over all the nodes on the axis of the node */ |
| 6115 | iter = NULL; |
| 6116 | iter_type = 0; |
| 6117 | while (!moveto_axis_node_next(&iter, &iter_type, set->val.nodes[i].node, set->val.nodes[i].type, axis, set)) { |
| 6118 | r = moveto_node_check(iter, iter_type, set, ncname, moveto_mod, options); |
| 6119 | if (r == LY_EINCOMPLETE) { |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6120 | rc = r; |
| 6121 | goto cleanup; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6122 | } else if (r) { |
| 6123 | continue; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6124 | } |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6125 | |
| 6126 | /* check for duplicates if they are possible */ |
| 6127 | switch (axis) { |
| 6128 | case LYXP_AXIS_ANCESTOR: |
| 6129 | case LYXP_AXIS_ANCESTOR_OR_SELF: |
| 6130 | case LYXP_AXIS_DESCENDANT: |
| 6131 | case LYXP_AXIS_DESCENDANT_OR_SELF: |
| 6132 | case LYXP_AXIS_FOLLOWING: |
| 6133 | case LYXP_AXIS_FOLLOWING_SIBLING: |
| 6134 | case LYXP_AXIS_PARENT: |
| 6135 | case LYXP_AXIS_PRECEDING: |
| 6136 | case LYXP_AXIS_PRECEDING_SIBLING: |
Michal Vasko | 306e283 | 2022-07-25 09:15:17 +0200 | [diff] [blame] | 6137 | result.non_child_axis = 1; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6138 | if (set_dup_node_check(&result, iter, iter_type, -1)) { |
| 6139 | continue; |
| 6140 | } |
| 6141 | break; |
| 6142 | case LYXP_AXIS_CHILD: |
| 6143 | case LYXP_AXIS_SELF: |
| 6144 | break; |
| 6145 | case LYXP_AXIS_ATTRIBUTE: |
| 6146 | /* handled specially */ |
| 6147 | assert(0); |
| 6148 | LOGINT(set->ctx); |
| 6149 | break; |
| 6150 | } |
| 6151 | |
| 6152 | /* matching node */ |
| 6153 | set_insert_node(&result, iter, 0, iter_type, result.used); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6154 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6155 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6156 | |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6157 | /* move result to the set */ |
| 6158 | lyxp_set_free_content(set); |
| 6159 | *set = result; |
| 6160 | result.type = LYXP_SET_NUMBER; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6161 | |
Michal Vasko | 306e283 | 2022-07-25 09:15:17 +0200 | [diff] [blame] | 6162 | /* sort the final set if the document order could have been broken */ |
| 6163 | if (set->non_child_axis) { |
| 6164 | set_sort(set); |
| 6165 | } else { |
| 6166 | assert(!set_sort(set)); |
| 6167 | } |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6168 | |
| 6169 | cleanup: |
| 6170 | lyxp_set_free_content(&result); |
| 6171 | return rc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6172 | } |
| 6173 | |
| 6174 | /** |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6175 | * @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] | 6176 | * |
| 6177 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6178 | * @param[in] scnode Matching node schema. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6179 | * @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] | 6180 | * @param[in] options XPath options. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6181 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6182 | */ |
| 6183 | static LY_ERR |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6184 | 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] | 6185 | uint32_t options) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6186 | { |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 6187 | LY_ERR ret = LY_SUCCESS, r; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6188 | uint32_t i; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6189 | const struct lyd_node *siblings; |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6190 | struct lyxp_set result; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6191 | struct lyd_node *sub, *inst = NULL; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6192 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6193 | assert(scnode && (!(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) || predicates)); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6194 | |
Michal Vasko | 50aaa07 | 2021-12-02 13:11:56 +0100 | [diff] [blame] | 6195 | /* init result set */ |
| 6196 | set_init(&result, set); |
| 6197 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6198 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6199 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6200 | } |
| 6201 | |
| 6202 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 6203 | 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] | 6204 | ret = LY_EVALID; |
| 6205 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6206 | } |
| 6207 | |
| 6208 | /* context check for all the nodes since we have the schema node */ |
| 6209 | if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) { |
| 6210 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6211 | goto cleanup; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 6212 | } else if (set->context_op && (scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && |
| 6213 | (scnode != set->context_op)) { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 6214 | lyxp_set_free_content(set); |
| 6215 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6216 | } |
| 6217 | |
| 6218 | /* create specific data instance if needed */ |
| 6219 | if (scnode->nodetype == LYS_LIST) { |
steweg | d4cde64 | 2024-02-21 08:34:16 +0100 | [diff] [blame] | 6220 | LY_CHECK_GOTO(ret = lyd_create_list(scnode, predicates, NULL, 1, &inst), cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6221 | } else if (scnode->nodetype == LYS_LEAFLIST) { |
| 6222 | 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] | 6223 | } |
| 6224 | |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6225 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6226 | siblings = NULL; |
| 6227 | |
| 6228 | if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) { |
| 6229 | assert(!set->val.nodes[i].node); |
| 6230 | |
| 6231 | /* search in all the trees */ |
| 6232 | siblings = set->tree; |
| 6233 | } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
| 6234 | /* search in children */ |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 6235 | siblings = lyd_child(set->val.nodes[i].node); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6236 | } |
| 6237 | |
| 6238 | /* find the node using hashes */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6239 | if (inst) { |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 6240 | r = lyd_find_sibling_first(siblings, inst, &sub); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6241 | } else { |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 6242 | r = lyd_find_sibling_val(siblings, scnode, NULL, 0, &sub); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6243 | } |
Michal Vasko | d8a5201 | 2023-08-15 11:38:10 +0200 | [diff] [blame] | 6244 | if (r == LY_ENOTFOUND) { |
| 6245 | /* may still be an opaque node */ |
| 6246 | r = lyd_find_sibling_opaq_next(siblings, scnode->name, &sub); |
| 6247 | } |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 6248 | LY_CHECK_ERR_GOTO(r && (r != LY_ENOTFOUND), ret = r, cleanup); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6249 | |
| 6250 | /* when check */ |
Michal Vasko | d5cfa6e | 2020-11-23 16:56:08 +0100 | [diff] [blame] | 6251 | 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] | 6252 | ret = LY_EINCOMPLETE; |
| 6253 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6254 | } |
| 6255 | |
| 6256 | if (sub) { |
| 6257 | /* pos filled later */ |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6258 | set_insert_node(&result, sub, 0, LYXP_NODE_ELEM, result.used); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6259 | } |
| 6260 | } |
| 6261 | |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6262 | /* move result to the set */ |
| 6263 | lyxp_set_free_content(set); |
| 6264 | *set = result; |
| 6265 | result.type = LYXP_SET_NUMBER; |
| 6266 | assert(!set_sort(set)); |
| 6267 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6268 | cleanup: |
Michal Vasko | 230cf97 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6269 | lyxp_set_free_content(&result); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6270 | lyd_free_tree(inst); |
| 6271 | return ret; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6272 | } |
| 6273 | |
| 6274 | /** |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6275 | * @brief Check @p node as a part of schema NameTest processing. |
| 6276 | * |
| 6277 | * @param[in] node Schema node to check. |
| 6278 | * @param[in] ctx_scnode Context node. |
| 6279 | * @param[in] set Set to read general context from. |
| 6280 | * @param[in] node_name Node name in the dictionary to move to, NULL for any nodes. |
| 6281 | * @param[in] moveto_mod Expected module of the node, NULL for no prefix. |
| 6282 | * @return LY_ERR (LY_ENOT if node does not match, LY_EINVAL if neither node nor any children match) |
| 6283 | */ |
| 6284 | static LY_ERR |
| 6285 | moveto_scnode_check(const struct lysc_node *node, const struct lysc_node *ctx_scnode, const struct lyxp_set *set, |
| 6286 | const char *node_name, const struct lys_module *moveto_mod) |
| 6287 | { |
| 6288 | if (!moveto_mod && node_name) { |
| 6289 | switch (set->format) { |
| 6290 | case LY_VALUE_SCHEMA: |
| 6291 | case LY_VALUE_SCHEMA_RESOLVED: |
| 6292 | /* use current module */ |
| 6293 | moveto_mod = set->cur_mod; |
| 6294 | break; |
| 6295 | case LY_VALUE_JSON: |
| 6296 | case LY_VALUE_LYB: |
| 6297 | case LY_VALUE_STR_NS: |
| 6298 | /* inherit module of the context node, if any */ |
| 6299 | if (ctx_scnode) { |
| 6300 | moveto_mod = ctx_scnode->module; |
| 6301 | } |
| 6302 | break; |
| 6303 | case LY_VALUE_CANON: |
| 6304 | case LY_VALUE_XML: |
| 6305 | /* not defined */ |
| 6306 | LOGINT(set->ctx); |
| 6307 | return LY_EINVAL; |
| 6308 | } |
| 6309 | } |
| 6310 | |
| 6311 | if (!node) { |
| 6312 | /* root will not match a specific node */ |
| 6313 | if (node_name || moveto_mod) { |
| 6314 | return LY_ENOT; |
| 6315 | } |
| 6316 | return LY_SUCCESS; |
| 6317 | } |
| 6318 | |
| 6319 | /* module check */ |
| 6320 | if (moveto_mod && (node->module != moveto_mod)) { |
| 6321 | return LY_ENOT; |
| 6322 | } |
| 6323 | |
| 6324 | /* context check */ |
| 6325 | if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) { |
| 6326 | return LY_EINVAL; |
| 6327 | } else if (set->context_op && (node->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node != set->context_op)) { |
| 6328 | return LY_EINVAL; |
| 6329 | } |
| 6330 | |
| 6331 | /* name check */ |
| 6332 | if (node_name && (node->name != node_name)) { |
| 6333 | return LY_ENOT; |
| 6334 | } |
| 6335 | |
| 6336 | /* match */ |
| 6337 | return LY_SUCCESS; |
| 6338 | } |
| 6339 | |
| 6340 | /** |
| 6341 | * @brief Get the next node in a forward schema node DFS. |
| 6342 | * |
| 6343 | * @param[in] iter Last returned node. |
| 6344 | * @param[in] stop Node to stop the search on and not return. |
| 6345 | * @param[in] getnext_opts Options for ::lys_getnext(). |
| 6346 | * @return Next node, NULL if there are no more. |
| 6347 | */ |
| 6348 | static const struct lysc_node * |
| 6349 | moveto_axis_scnode_next_dfs_forward(const struct lysc_node *iter, const struct lysc_node *stop, uint32_t getnext_opts) |
| 6350 | { |
| 6351 | const struct lysc_node *next = NULL; |
| 6352 | |
| 6353 | next = lysc_node_child(iter); |
| 6354 | if (!next) { |
| 6355 | /* no children, try siblings */ |
Michal Vasko | 34a22fe | 2022-06-15 07:58:55 +0200 | [diff] [blame] | 6356 | if ((iter == stop) || !lysc_data_parent(iter)) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6357 | /* we are done, no next element to process */ |
| 6358 | return NULL; |
| 6359 | } |
| 6360 | |
| 6361 | next = lys_getnext(iter, lysc_data_parent(iter), NULL, getnext_opts); |
| 6362 | } |
| 6363 | while (!next && iter) { |
| 6364 | /* parent is already processed, go to its sibling */ |
| 6365 | iter = iter->parent; |
Michal Vasko | 34a22fe | 2022-06-15 07:58:55 +0200 | [diff] [blame] | 6366 | if ((iter == stop) || !lysc_data_parent(iter)) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6367 | /* we are done, no next element to process */ |
| 6368 | return NULL; |
| 6369 | } |
| 6370 | next = lys_getnext(iter, lysc_data_parent(iter), NULL, getnext_opts); |
| 6371 | } |
| 6372 | |
| 6373 | return next; |
| 6374 | } |
| 6375 | |
| 6376 | /** |
| 6377 | * @brief Consider schema node based on its in_ctx enum value. |
| 6378 | * |
| 6379 | * @param[in,out] in_ctx In_ctx enum of the schema node, may be updated. |
| 6380 | * @param[in] axis Axis to use. |
| 6381 | * @return LY_SUCCESS on success. |
| 6382 | * @return LY_ENOT if the node should not be returned. |
| 6383 | */ |
| 6384 | static LY_ERR |
| 6385 | moveto_axis_scnode_next_in_ctx(int32_t *in_ctx, enum lyxp_axis axis) |
| 6386 | { |
| 6387 | switch (axis) { |
| 6388 | case LYXP_AXIS_SELF: |
| 6389 | if ((*in_ctx == LYXP_SET_SCNODE_START) || (*in_ctx == LYXP_SET_SCNODE_ATOM_CTX)) { |
| 6390 | /* additionally put the start node into context */ |
| 6391 | *in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
| 6392 | return LY_SUCCESS; |
| 6393 | } |
| 6394 | break; |
| 6395 | case LYXP_AXIS_PARENT: |
| 6396 | case LYXP_AXIS_ANCESTOR_OR_SELF: |
| 6397 | case LYXP_AXIS_ANCESTOR: |
| 6398 | case LYXP_AXIS_DESCENDANT_OR_SELF: |
| 6399 | case LYXP_AXIS_DESCENDANT: |
| 6400 | case LYXP_AXIS_FOLLOWING: |
| 6401 | case LYXP_AXIS_FOLLOWING_SIBLING: |
| 6402 | case LYXP_AXIS_PRECEDING: |
| 6403 | case LYXP_AXIS_PRECEDING_SIBLING: |
| 6404 | case LYXP_AXIS_CHILD: |
| 6405 | if (*in_ctx == LYXP_SET_SCNODE_START) { |
| 6406 | /* remember that context node was used */ |
| 6407 | *in_ctx = LYXP_SET_SCNODE_START_USED; |
| 6408 | return LY_SUCCESS; |
| 6409 | } else if (*in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
| 6410 | /* traversed */ |
| 6411 | *in_ctx = LYXP_SET_SCNODE_ATOM_NODE; |
| 6412 | return LY_SUCCESS; |
| 6413 | } |
| 6414 | break; |
| 6415 | case LYXP_AXIS_ATTRIBUTE: |
| 6416 | /* unreachable */ |
| 6417 | assert(0); |
| 6418 | LOGINT(NULL); |
| 6419 | break; |
| 6420 | } |
| 6421 | |
| 6422 | return LY_ENOT; |
| 6423 | } |
| 6424 | |
| 6425 | /** |
| 6426 | * @brief Get previous sibling for a schema node. |
| 6427 | * |
| 6428 | * @param[in] scnode Schema node. |
| 6429 | * @param[in] getnext_opts Options for ::lys_getnext(). |
| 6430 | * @return Previous sibling, NULL if none. |
| 6431 | */ |
| 6432 | static const struct lysc_node * |
| 6433 | moveto_axis_scnode_preceding_sibling(const struct lysc_node *scnode, uint32_t getnext_opts) |
| 6434 | { |
| 6435 | const struct lysc_node *next = NULL, *prev = NULL; |
| 6436 | |
| 6437 | while ((next = lys_getnext(next, lysc_data_parent(scnode), scnode->module->compiled, getnext_opts))) { |
| 6438 | if (next == scnode) { |
| 6439 | break; |
| 6440 | } |
| 6441 | |
| 6442 | prev = next; |
| 6443 | } |
| 6444 | |
| 6445 | return prev; |
| 6446 | } |
| 6447 | |
| 6448 | /** |
| 6449 | * @brief Get the first schema node on an axis for a context node. |
| 6450 | * |
| 6451 | * @param[in,out] iter Last returned node, start with NULL, updated to the next node. |
| 6452 | * @param[in,out] iter_type Node type of @p iter, start with 0, updated to the node type of the next node. |
| 6453 | * @param[in,out] iter_mod Internal module iterator, do not change. |
| 6454 | * @param[in,out] iter_mod_idx Internal module index iterator, do not change. |
| 6455 | * @param[in] scnode Context node. |
| 6456 | * @param[in] node_type Type of @p scnode. |
| 6457 | * @param[in] in_ctx In_ctx enum of @p scnode. |
| 6458 | * @param[in] axis Axis to use. |
| 6459 | * @param[in] set XPath set with the general context. |
| 6460 | * @param[in] getnext_opts Options for ::lys_getnext(). |
| 6461 | * @return LY_SUCCESS on success. |
| 6462 | * @return LY_ENOTFOUND if no next node found. |
| 6463 | */ |
| 6464 | static LY_ERR |
| 6465 | moveto_axis_scnode_next_first(const struct lysc_node **iter, enum lyxp_node_type *iter_type, const struct lys_module **iter_mod, |
| 6466 | uint32_t *iter_mod_idx, const struct lysc_node *scnode, enum lyxp_node_type node_type, enum lyxp_axis axis, |
| 6467 | struct lyxp_set *set, uint32_t getnext_opts) |
| 6468 | { |
| 6469 | const struct lysc_node *next = NULL; |
| 6470 | enum lyxp_node_type next_type = 0; |
| 6471 | |
| 6472 | assert(!*iter); |
| 6473 | assert(!*iter_type); |
| 6474 | |
| 6475 | *iter_mod = NULL; |
| 6476 | *iter_mod_idx = 0; |
| 6477 | |
| 6478 | switch (axis) { |
| 6479 | case LYXP_AXIS_ANCESTOR_OR_SELF: |
| 6480 | case LYXP_AXIS_DESCENDANT_OR_SELF: |
| 6481 | case LYXP_AXIS_SELF: |
| 6482 | if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ELEM)) { |
| 6483 | /* just return the node */ |
| 6484 | next = scnode; |
| 6485 | next_type = node_type; |
| 6486 | } |
| 6487 | break; |
| 6488 | |
| 6489 | case LYXP_AXIS_ANCESTOR: |
| 6490 | case LYXP_AXIS_PARENT: |
| 6491 | if (node_type == LYXP_NODE_ELEM) { |
| 6492 | next = lysc_data_parent(scnode); |
| 6493 | next_type = next ? LYXP_NODE_ELEM : set->root_type; |
| 6494 | } /* else no parent */ |
| 6495 | break; |
| 6496 | |
| 6497 | case LYXP_AXIS_DESCENDANT: |
| 6498 | case LYXP_AXIS_CHILD: |
| 6499 | if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) { |
| 6500 | /* it can actually be in any module, it's all <running>, and even if it's moveto_mod (if set), |
| 6501 | * it can be in a top-level augment */ |
| 6502 | while ((*iter_mod = ly_ctx_get_module_iter(set->ctx, iter_mod_idx))) { |
| 6503 | /* module may not be implemented or not compiled yet */ |
| 6504 | if (!(*iter_mod)->compiled) { |
| 6505 | continue; |
| 6506 | } |
| 6507 | |
| 6508 | /* get next node */ |
| 6509 | if ((next = lys_getnext(NULL, NULL, (*iter_mod)->compiled, getnext_opts))) { |
| 6510 | next_type = LYXP_NODE_ELEM; |
| 6511 | break; |
| 6512 | } |
| 6513 | } |
| 6514 | } else if (node_type == LYXP_NODE_ELEM) { |
| 6515 | /* get next node */ |
| 6516 | next = lys_getnext(NULL, scnode, NULL, getnext_opts); |
| 6517 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 6518 | } |
| 6519 | break; |
| 6520 | |
| 6521 | case LYXP_AXIS_FOLLOWING: |
| 6522 | case LYXP_AXIS_FOLLOWING_SIBLING: |
| 6523 | if (node_type == LYXP_NODE_ELEM) { |
| 6524 | /* first next sibling */ |
| 6525 | next = lys_getnext(scnode, lysc_data_parent(scnode), scnode->module->compiled, getnext_opts); |
| 6526 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 6527 | } /* else no sibling */ |
| 6528 | break; |
| 6529 | |
| 6530 | case LYXP_AXIS_PRECEDING: |
| 6531 | case LYXP_AXIS_PRECEDING_SIBLING: |
| 6532 | if (node_type == LYXP_NODE_ELEM) { |
| 6533 | /* first parent sibling */ |
| 6534 | next = lys_getnext(NULL, lysc_data_parent(scnode), scnode->module->compiled, getnext_opts); |
| 6535 | if (next == scnode) { |
| 6536 | /* no preceding sibling */ |
| 6537 | next = NULL; |
| 6538 | } |
| 6539 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 6540 | } /* else no sibling */ |
| 6541 | break; |
| 6542 | |
| 6543 | case LYXP_AXIS_ATTRIBUTE: |
| 6544 | /* unreachable */ |
| 6545 | assert(0); |
| 6546 | LOGINT(set->ctx); |
| 6547 | break; |
| 6548 | } |
| 6549 | |
| 6550 | *iter = next; |
| 6551 | *iter_type = next_type; |
| 6552 | return next_type ? LY_SUCCESS : LY_ENOTFOUND; |
| 6553 | } |
| 6554 | |
| 6555 | /** |
| 6556 | * @brief Iterate over all schema nodes on an axis for a context node. |
| 6557 | * |
| 6558 | * @param[in,out] iter Last returned node, start with NULL, updated to the next node. |
| 6559 | * @param[in,out] iter_type Node type of @p iter, start with 0, updated to the node type of the next node. |
| 6560 | * @param[in,out] iter_mod Internal module iterator, do not change. |
| 6561 | * @param[in,out] iter_mod_idx Internal module index iterator, do not change. |
| 6562 | * @param[in] scnode Context node. |
| 6563 | * @param[in] node_type Type of @p scnode. |
| 6564 | * @param[in] axis Axis to use. |
| 6565 | * @param[in] set XPath set with the general context. |
| 6566 | * @param[in] getnext_opts Options for ::lys_getnext(). |
| 6567 | * @return LY_SUCCESS on success. |
| 6568 | * @return LY_ENOTFOUND if no next node found. |
| 6569 | */ |
| 6570 | static LY_ERR |
| 6571 | moveto_axis_scnode_next(const struct lysc_node **iter, enum lyxp_node_type *iter_type, const struct lys_module **iter_mod, |
| 6572 | uint32_t *iter_mod_idx, const struct lysc_node *scnode, enum lyxp_node_type node_type, enum lyxp_axis axis, |
| 6573 | struct lyxp_set *set, uint32_t getnext_opts) |
| 6574 | { |
| 6575 | const struct lysc_node *next = NULL, *dfs_stop; |
| 6576 | enum lyxp_node_type next_type = 0; |
| 6577 | |
| 6578 | if (!*iter_type) { |
| 6579 | /* first returned node */ |
| 6580 | return moveto_axis_scnode_next_first(iter, iter_type, iter_mod, iter_mod_idx, scnode, node_type, axis, set, |
| 6581 | getnext_opts); |
| 6582 | } |
| 6583 | |
| 6584 | switch (axis) { |
| 6585 | case LYXP_AXIS_PARENT: |
| 6586 | case LYXP_AXIS_SELF: |
| 6587 | /* parent/self was returned before */ |
| 6588 | break; |
| 6589 | |
| 6590 | case LYXP_AXIS_ANCESTOR_OR_SELF: |
| 6591 | if ((*iter == scnode) && (*iter_type == node_type)) { |
| 6592 | /* fake first ancestor, 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_ANCESTOR, set, getnext_opts); |
| 6597 | } /* else continue ancestor */ |
| 6598 | |
| 6599 | /* fallthrough */ |
| 6600 | case LYXP_AXIS_ANCESTOR: |
| 6601 | if (*iter_type == LYXP_NODE_ELEM) { |
| 6602 | next = lysc_data_parent(*iter); |
| 6603 | next_type = next ? LYXP_NODE_ELEM : set->root_type; |
| 6604 | } /* else no ancestor */ |
| 6605 | break; |
| 6606 | |
| 6607 | case LYXP_AXIS_DESCENDANT_OR_SELF: |
| 6608 | if ((*iter == scnode) && (*iter_type == node_type)) { |
| 6609 | /* fake first descendant, we returned self before */ |
| 6610 | *iter = NULL; |
| 6611 | *iter_type = 0; |
| 6612 | return moveto_axis_scnode_next_first(iter, iter_type, iter_mod, iter_mod_idx, scnode, node_type, |
| 6613 | LYXP_AXIS_DESCENDANT, set, getnext_opts); |
| 6614 | } /* else DFS until context node */ |
| 6615 | dfs_stop = scnode; |
| 6616 | |
| 6617 | /* fallthrough */ |
| 6618 | case LYXP_AXIS_DESCENDANT: |
| 6619 | if (axis == LYXP_AXIS_DESCENDANT) { |
| 6620 | /* DFS until the context node */ |
| 6621 | dfs_stop = scnode; |
| 6622 | } |
| 6623 | |
| 6624 | /* fallthrough */ |
| 6625 | case LYXP_AXIS_PRECEDING: |
| 6626 | if (axis == LYXP_AXIS_PRECEDING) { |
| 6627 | /* DFS until the previous sibling */ |
| 6628 | dfs_stop = moveto_axis_scnode_preceding_sibling(scnode, getnext_opts); |
| 6629 | assert(dfs_stop); |
| 6630 | |
| 6631 | if (*iter == dfs_stop) { |
| 6632 | /* we are done */ |
| 6633 | break; |
| 6634 | } |
| 6635 | } |
| 6636 | |
| 6637 | /* fallthrough */ |
| 6638 | case LYXP_AXIS_FOLLOWING: |
| 6639 | if (axis == LYXP_AXIS_FOLLOWING) { |
| 6640 | /* DFS through the whole module */ |
| 6641 | dfs_stop = NULL; |
| 6642 | } |
| 6643 | |
| 6644 | /* nested nodes */ |
| 6645 | assert(*iter); |
| 6646 | next = moveto_axis_scnode_next_dfs_forward(*iter, dfs_stop, getnext_opts); |
| 6647 | if (next) { |
| 6648 | next_type = LYXP_NODE_ELEM; |
| 6649 | break; |
| 6650 | } /* else get next top-level node just like a child */ |
| 6651 | |
| 6652 | /* fallthrough */ |
| 6653 | case LYXP_AXIS_CHILD: |
| 6654 | case LYXP_AXIS_FOLLOWING_SIBLING: |
| 6655 | if (!*iter_mod) { |
| 6656 | /* nodes from a single module */ |
| 6657 | if ((next = lys_getnext(*iter, lysc_data_parent(*iter), (*iter)->module->compiled, getnext_opts))) { |
| 6658 | next_type = LYXP_NODE_ELEM; |
| 6659 | break; |
| 6660 | } |
| 6661 | |
| 6662 | assert(scnode); |
Michal Vasko | a353cce | 2022-11-14 10:09:55 +0100 | [diff] [blame] | 6663 | if ((axis != LYXP_AXIS_CHILD) && !lysc_data_parent(scnode)) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6664 | /* iterating over top-level nodes, find next */ |
| 6665 | while (lysc_data_parent(*iter)) { |
| 6666 | *iter = lysc_data_parent(*iter); |
| 6667 | } |
| 6668 | if ((next = lys_getnext(*iter, NULL, (*iter)->module->compiled, getnext_opts))) { |
| 6669 | next_type = LYXP_NODE_ELEM; |
| 6670 | break; |
| 6671 | } |
| 6672 | } |
| 6673 | } |
| 6674 | |
| 6675 | while (*iter_mod) { |
| 6676 | /* module top-level nodes */ |
| 6677 | if ((next = lys_getnext(*iter, NULL, (*iter_mod)->compiled, getnext_opts))) { |
| 6678 | next_type = LYXP_NODE_ELEM; |
| 6679 | break; |
| 6680 | } |
| 6681 | |
| 6682 | /* get next module */ |
| 6683 | while ((*iter_mod = ly_ctx_get_module_iter(set->ctx, iter_mod_idx))) { |
| 6684 | /* module may not be implemented or not compiled yet */ |
| 6685 | if ((*iter_mod)->compiled) { |
| 6686 | break; |
| 6687 | } |
| 6688 | } |
| 6689 | |
| 6690 | /* new module, start over */ |
| 6691 | *iter = NULL; |
| 6692 | } |
| 6693 | break; |
| 6694 | |
| 6695 | case LYXP_AXIS_PRECEDING_SIBLING: |
| 6696 | assert(*iter); |
| 6697 | |
| 6698 | /* next parent sibling until scnode */ |
| 6699 | next = lys_getnext(*iter, lysc_data_parent(*iter), (*iter)->module->compiled, getnext_opts); |
| 6700 | if (next == scnode) { |
| 6701 | /* no previous sibling */ |
| 6702 | next = NULL; |
| 6703 | } |
| 6704 | next_type = next ? LYXP_NODE_ELEM : 0; |
| 6705 | break; |
| 6706 | |
| 6707 | case LYXP_AXIS_ATTRIBUTE: |
| 6708 | /* unreachable */ |
| 6709 | assert(0); |
| 6710 | LOGINT(set->ctx); |
| 6711 | break; |
| 6712 | } |
| 6713 | |
| 6714 | *iter = next; |
| 6715 | *iter_type = next_type; |
| 6716 | return next_type ? LY_SUCCESS : LY_ENOTFOUND; |
| 6717 | } |
| 6718 | |
| 6719 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6720 | * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY). |
| 6721 | * |
| 6722 | * @param[in,out] set Set to use. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6723 | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6724 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6725 | * @param[in] axis Axis to search on. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6726 | * @param[in] options XPath options. |
| 6727 | * @return LY_ERR |
| 6728 | */ |
| 6729 | static LY_ERR |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6730 | moveto_scnode(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, enum lyxp_axis axis, |
| 6731 | uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6732 | { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6733 | ly_bool temp_ctx = 0; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6734 | uint32_t getnext_opts, orig_used, i, mod_idx, idx; |
steweg | d8e2fc9 | 2023-05-31 09:52:56 +0200 | [diff] [blame] | 6735 | const struct lys_module *mod = NULL; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6736 | const struct lysc_node *iter; |
| 6737 | enum lyxp_node_type iter_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6738 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6739 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6740 | return LY_SUCCESS; |
| 6741 | } |
| 6742 | |
| 6743 | if (set->type != LYXP_SET_SCNODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 6744 | 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] | 6745 | return LY_EVALID; |
| 6746 | } |
| 6747 | |
Michal Vasko | cafad9d | 2019-11-07 15:20:03 +0100 | [diff] [blame] | 6748 | /* getnext opts */ |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 6749 | getnext_opts = 0; |
Michal Vasko | cafad9d | 2019-11-07 15:20:03 +0100 | [diff] [blame] | 6750 | if (options & LYXP_SCNODE_OUTPUT) { |
| 6751 | getnext_opts |= LYS_GETNEXT_OUTPUT; |
| 6752 | } |
Michal Vasko | e482bb9 | 2023-02-01 11:41:41 +0100 | [diff] [blame] | 6753 | if (options & LYXP_SCNODE_SCHEMAMOUNT) { |
| 6754 | getnext_opts |= LYS_GETNEXT_WITHSCHEMAMOUNT; |
| 6755 | } |
Michal Vasko | cafad9d | 2019-11-07 15:20:03 +0100 | [diff] [blame] | 6756 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6757 | orig_used = set->used; |
| 6758 | for (i = 0; i < orig_used; ++i) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6759 | /* update in_ctx first */ |
| 6760 | if (moveto_axis_scnode_next_in_ctx(&set->val.scnodes[i].in_ctx, axis)) { |
| 6761 | /* not usable, skip */ |
| 6762 | continue; |
| 6763 | } |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 6764 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6765 | iter = NULL; |
| 6766 | iter_type = 0; |
| 6767 | while (!moveto_axis_scnode_next(&iter, &iter_type, &mod, &mod_idx, set->val.scnodes[i].scnode, |
| 6768 | set->val.scnodes[i].type, axis, set, getnext_opts)) { |
| 6769 | if (moveto_scnode_check(iter, NULL, set, ncname, moveto_mod)) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6770 | continue; |
| 6771 | } |
| 6772 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6773 | /* insert */ |
Michal Vasko | e4a6d01 | 2023-05-22 14:34:52 +0200 | [diff] [blame] | 6774 | 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] | 6775 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6776 | /* we need to prevent these nodes from being considered in this moveto */ |
| 6777 | if ((idx < orig_used) && (idx > i)) { |
| 6778 | set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX; |
| 6779 | temp_ctx = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6780 | } |
| 6781 | } |
Michal Vasko | 66330fc | 2022-11-21 15:52:24 +0100 | [diff] [blame] | 6782 | |
| 6783 | if (moveto_mod && ncname && ((axis == LYXP_AXIS_DESCENDANT) || (axis == LYXP_AXIS_CHILD)) && |
| 6784 | (set->val.scnodes[i].type == LYXP_NODE_ELEM) && !ly_nested_ext_schema(NULL, set->val.scnodes[i].scnode, |
| 6785 | moveto_mod->name, strlen(moveto_mod->name), LY_VALUE_JSON, NULL, ncname, strlen(ncname), &iter, NULL)) { |
| 6786 | /* there is a matching node from an extension, use it */ |
Michal Vasko | e4a6d01 | 2023-05-22 14:34:52 +0200 | [diff] [blame] | 6787 | 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] | 6788 | if ((idx < orig_used) && (idx > i)) { |
| 6789 | set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX; |
| 6790 | temp_ctx = 1; |
| 6791 | } |
| 6792 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6793 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6794 | |
| 6795 | /* correct temporary in_ctx values */ |
| 6796 | if (temp_ctx) { |
| 6797 | for (i = 0; i < orig_used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 6798 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NEW_CTX) { |
| 6799 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6800 | } |
| 6801 | } |
| 6802 | } |
| 6803 | |
| 6804 | return LY_SUCCESS; |
| 6805 | } |
| 6806 | |
| 6807 | /** |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6808 | * @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] | 6809 | * Context position aware. |
| 6810 | * |
| 6811 | * @param[in] set Set to use. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6812 | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6813 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 6814 | * @param[in] options XPath options. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6815 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6816 | */ |
| 6817 | static LY_ERR |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6818 | 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] | 6819 | { |
| 6820 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6821 | const struct lyd_node *next, *elem, *start; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6822 | struct lyxp_set ret_set; |
| 6823 | LY_ERR rc; |
| 6824 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6825 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6826 | return LY_SUCCESS; |
| 6827 | } |
| 6828 | |
| 6829 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 6830 | 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] | 6831 | return LY_EVALID; |
| 6832 | } |
| 6833 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6834 | /* 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] | 6835 | rc = xpath_pi_node(set, LYXP_AXIS_CHILD, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6836 | LY_CHECK_RET(rc); |
| 6837 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 6838 | /* 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] | 6839 | set_init(&ret_set, set); |
| 6840 | for (i = 0; i < set->used; ++i) { |
| 6841 | |
| 6842 | /* TREE DFS */ |
| 6843 | start = set->val.nodes[i].node; |
| 6844 | for (elem = next = start; elem; elem = next) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6845 | 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] | 6846 | if (!rc) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6847 | /* add matching node into result set */ |
| 6848 | set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used); |
| 6849 | if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) { |
| 6850 | /* the node is a duplicate, we'll process it later in the set */ |
| 6851 | goto skip_children; |
| 6852 | } |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 6853 | } else if (rc == LY_EINCOMPLETE) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 6854 | return rc; |
| 6855 | } else if (rc == LY_EINVAL) { |
| 6856 | goto skip_children; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6857 | } |
| 6858 | |
| 6859 | /* TREE DFS NEXT ELEM */ |
| 6860 | /* select element for the next run - children first */ |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 6861 | next = lyd_child(elem); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6862 | if (!next) { |
| 6863 | skip_children: |
| 6864 | /* no children, so try siblings, but only if it's not the start, |
| 6865 | * that is considered to be the root and it's siblings are not traversed */ |
| 6866 | if (elem != start) { |
| 6867 | next = elem->next; |
| 6868 | } else { |
| 6869 | break; |
| 6870 | } |
| 6871 | } |
| 6872 | while (!next) { |
| 6873 | /* no siblings, go back through the parents */ |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 6874 | if (lyd_parent(elem) == start) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6875 | /* we are done, no next element to process */ |
| 6876 | break; |
| 6877 | } |
| 6878 | /* parent is already processed, go to its sibling */ |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 6879 | elem = lyd_parent(elem); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6880 | next = elem->next; |
| 6881 | } |
| 6882 | } |
| 6883 | } |
| 6884 | |
| 6885 | /* make the temporary set the current one */ |
| 6886 | ret_set.ctx_pos = set->ctx_pos; |
| 6887 | ret_set.ctx_size = set->ctx_size; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6888 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6889 | memcpy(set, &ret_set, sizeof *set); |
Michal Vasko | 306e283 | 2022-07-25 09:15:17 +0200 | [diff] [blame] | 6890 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6891 | |
| 6892 | return LY_SUCCESS; |
| 6893 | } |
| 6894 | |
| 6895 | /** |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6896 | * @brief Move context @p set to a child schema node and all its descendants starting from a node. |
| 6897 | * Result is LYXP_SET_NODE_SET. |
| 6898 | * |
| 6899 | * @param[in] set Set to use. |
| 6900 | * @param[in] start Start node whose subtree to add. |
| 6901 | * @param[in] start_idx Index of @p start in @p set. |
| 6902 | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
| 6903 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
| 6904 | * @param[in] options XPath options. |
| 6905 | * @return LY_ERR value. |
| 6906 | */ |
| 6907 | static LY_ERR |
| 6908 | moveto_scnode_dfs(struct lyxp_set *set, const struct lysc_node *start, uint32_t start_idx, |
| 6909 | const struct lys_module *moveto_mod, const char *ncname, uint32_t options) |
| 6910 | { |
| 6911 | const struct lysc_node *next, *elem; |
| 6912 | uint32_t idx; |
| 6913 | LY_ERR rc; |
| 6914 | |
| 6915 | /* TREE DFS */ |
| 6916 | for (elem = next = start; elem; elem = next) { |
| 6917 | if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) { |
| 6918 | /* schema-only nodes, skip root */ |
| 6919 | goto next_iter; |
| 6920 | } |
| 6921 | |
| 6922 | rc = moveto_scnode_check(elem, start, set, ncname, moveto_mod); |
| 6923 | if (!rc) { |
| 6924 | if (lyxp_set_scnode_contains(set, elem, LYXP_NODE_ELEM, start_idx, &idx)) { |
| 6925 | set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
| 6926 | if (idx > start_idx) { |
| 6927 | /* we will process it later in the set */ |
| 6928 | goto skip_children; |
| 6929 | } |
| 6930 | } else { |
Michal Vasko | e4a6d01 | 2023-05-22 14:34:52 +0200 | [diff] [blame] | 6931 | 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] | 6932 | } |
| 6933 | } else if (rc == LY_EINVAL) { |
| 6934 | goto skip_children; |
| 6935 | } |
| 6936 | |
| 6937 | next_iter: |
| 6938 | /* TREE DFS NEXT ELEM */ |
| 6939 | /* select element for the next run - children first */ |
| 6940 | next = lysc_node_child(elem); |
| 6941 | if (next && (next->nodetype == LYS_INPUT) && (options & LYXP_SCNODE_OUTPUT)) { |
| 6942 | next = next->next; |
| 6943 | } else if (next && (next->nodetype == LYS_OUTPUT) && !(options & LYXP_SCNODE_OUTPUT)) { |
| 6944 | next = next->next; |
| 6945 | } |
| 6946 | if (!next) { |
| 6947 | skip_children: |
| 6948 | /* no children, so try siblings, but only if it's not the start, |
| 6949 | * that is considered to be the root and it's siblings are not traversed */ |
| 6950 | if (elem != start) { |
| 6951 | next = elem->next; |
| 6952 | } else { |
| 6953 | break; |
| 6954 | } |
| 6955 | } |
| 6956 | while (!next) { |
| 6957 | /* no siblings, go back through the parents */ |
| 6958 | if (elem->parent == start) { |
| 6959 | /* we are done, no next element to process */ |
| 6960 | break; |
| 6961 | } |
| 6962 | /* parent is already processed, go to its sibling */ |
| 6963 | elem = elem->parent; |
| 6964 | next = elem->next; |
| 6965 | } |
| 6966 | } |
| 6967 | |
| 6968 | return LY_SUCCESS; |
| 6969 | } |
| 6970 | |
| 6971 | /** |
| 6972 | * @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] | 6973 | * |
| 6974 | * @param[in] set Set to use. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6975 | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6976 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6977 | * @param[in] options XPath options. |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6978 | * @return LY_ERR value. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6979 | */ |
| 6980 | static LY_ERR |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6981 | 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] | 6982 | { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 6983 | uint32_t i, orig_used, mod_idx; |
| 6984 | const struct lys_module *mod; |
| 6985 | const struct lysc_node *root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6986 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6987 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6988 | return LY_SUCCESS; |
| 6989 | } |
| 6990 | |
| 6991 | if (set->type != LYXP_SET_SCNODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 6992 | 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] | 6993 | return LY_EVALID; |
| 6994 | } |
| 6995 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6996 | orig_used = set->used; |
| 6997 | for (i = 0; i < orig_used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 6998 | if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) { |
| 6999 | if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 7000 | continue; |
| 7001 | } |
| 7002 | |
| 7003 | /* remember context node */ |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 7004 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED; |
Michal Vasko | ec4df48 | 2019-12-16 10:02:18 +0100 | [diff] [blame] | 7005 | } else { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 7006 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7007 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7008 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7009 | if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) { |
| 7010 | /* traverse all top-level nodes in all the modules */ |
| 7011 | mod_idx = 0; |
| 7012 | while ((mod = ly_ctx_get_module_iter(set->ctx, &mod_idx))) { |
| 7013 | /* module may not be implemented or not compiled yet */ |
| 7014 | if (!mod->compiled) { |
| 7015 | continue; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7016 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7017 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7018 | root = NULL; |
| 7019 | /* no getnext opts needed */ |
| 7020 | while ((root = lys_getnext(root, NULL, mod->compiled, 0))) { |
| 7021 | 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] | 7022 | } |
| 7023 | } |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7024 | |
| 7025 | } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) { |
| 7026 | /* add all the descendants recursively */ |
| 7027 | 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] | 7028 | } |
| 7029 | } |
| 7030 | |
| 7031 | return LY_SUCCESS; |
| 7032 | } |
| 7033 | |
| 7034 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 7035 | * @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] | 7036 | * Indirectly context position aware. |
| 7037 | * |
| 7038 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7039 | * @param[in] mod Matching metadata module, NULL for any. |
| 7040 | * @param[in] ncname Matching metadata name in the dictionary, NULL for any. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7041 | * @param[in] options XPath options. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7042 | * @return LY_ERR |
| 7043 | */ |
| 7044 | static LY_ERR |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7045 | 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] | 7046 | { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 7047 | struct lyd_meta *sub; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7048 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7049 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7050 | return LY_SUCCESS; |
| 7051 | } |
| 7052 | |
| 7053 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 7054 | 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] | 7055 | return LY_EVALID; |
| 7056 | } |
| 7057 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7058 | for (uint32_t i = 0; i < set->used; ) { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 7059 | ly_bool replaced = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7060 | |
| 7061 | /* only attributes of an elem (not dummy) can be in the result, skip all the rest; |
| 7062 | * our attributes are always qualified */ |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 7063 | if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 7064 | for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7065 | |
| 7066 | /* check "namespace" */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7067 | if (mod && (sub->annotation->module != mod)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7068 | continue; |
| 7069 | } |
| 7070 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7071 | if (!ncname || (sub->name == ncname)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7072 | /* match */ |
| 7073 | if (!replaced) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 7074 | set->val.meta[i].meta = sub; |
| 7075 | set->val.meta[i].type = LYXP_NODE_META; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7076 | /* pos does not change */ |
| 7077 | replaced = 1; |
| 7078 | } else { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 7079 | 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] | 7080 | } |
| 7081 | ++i; |
| 7082 | } |
| 7083 | } |
| 7084 | } |
| 7085 | |
| 7086 | if (!replaced) { |
| 7087 | /* no match */ |
| 7088 | set_remove_node(set, i); |
| 7089 | } |
| 7090 | } |
| 7091 | |
| 7092 | return LY_SUCCESS; |
| 7093 | } |
| 7094 | |
| 7095 | /** |
| 7096 | * @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] | 7097 | * Result is LYXP_SET_NODE_SET. Context position aware. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7098 | * |
| 7099 | * @param[in,out] set1 Set to use for the result. |
| 7100 | * @param[in] set2 Set that is copied to @p set1. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7101 | * @return LY_ERR |
| 7102 | */ |
| 7103 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7104 | moveto_union(struct lyxp_set *set1, struct lyxp_set *set2) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7105 | { |
| 7106 | LY_ERR rc; |
| 7107 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7108 | 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] | 7109 | 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] | 7110 | return LY_EVALID; |
| 7111 | } |
| 7112 | |
| 7113 | /* set2 is empty or both set1 and set2 */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7114 | if (!set2->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7115 | return LY_SUCCESS; |
| 7116 | } |
| 7117 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7118 | if (!set1->used) { |
aPiecek | adc1e4f | 2021-10-07 11:15:12 +0200 | [diff] [blame] | 7119 | /* release hidden allocated data (lyxp_set.size) */ |
| 7120 | lyxp_set_free_content(set1); |
| 7121 | /* direct copying of the entire structure */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7122 | memcpy(set1, set2, sizeof *set1); |
| 7123 | /* dynamic memory belongs to set1 now, do not free */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7124 | memset(set2, 0, sizeof *set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7125 | return LY_SUCCESS; |
| 7126 | } |
| 7127 | |
| 7128 | /* we assume sets are sorted */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7129 | assert(!set_sort(set1) && !set_sort(set2)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7130 | |
| 7131 | /* sort, remove duplicates */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7132 | rc = set_sorted_merge(set1, set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7133 | LY_CHECK_RET(rc); |
| 7134 | |
| 7135 | /* final set must be sorted */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7136 | assert(!set_sort(set1)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7137 | |
| 7138 | return LY_SUCCESS; |
| 7139 | } |
| 7140 | |
| 7141 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 7142 | * @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] | 7143 | * Context position aware. |
| 7144 | * |
| 7145 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7146 | * @param[in] mod Matching metadata module, NULL for any. |
| 7147 | * @param[in] ncname Matching metadata name in the dictionary, NULL for any. |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 7148 | * @param[in] options XPath options. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7149 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7150 | */ |
| 7151 | static int |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 7152 | 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] | 7153 | { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 7154 | struct lyd_meta *sub; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7155 | struct lyxp_set *set_all_desc = NULL; |
| 7156 | LY_ERR rc; |
| 7157 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7158 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7159 | return LY_SUCCESS; |
| 7160 | } |
| 7161 | |
| 7162 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 7163 | 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] | 7164 | return LY_EVALID; |
| 7165 | } |
| 7166 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7167 | /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory, |
| 7168 | * but it likely won't be used much, so it's a waste of time */ |
| 7169 | /* copy the context */ |
| 7170 | set_all_desc = set_copy(set); |
| 7171 | /* get all descendant nodes (the original context nodes are removed) */ |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7172 | rc = moveto_node_alldesc_child(set_all_desc, NULL, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7173 | if (rc != LY_SUCCESS) { |
| 7174 | lyxp_set_free(set_all_desc); |
| 7175 | return rc; |
| 7176 | } |
| 7177 | /* prepend the original context nodes */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7178 | rc = moveto_union(set, set_all_desc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7179 | if (rc != LY_SUCCESS) { |
| 7180 | lyxp_set_free(set_all_desc); |
| 7181 | return rc; |
| 7182 | } |
| 7183 | lyxp_set_free(set_all_desc); |
| 7184 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7185 | for (uint32_t i = 0; i < set->used; ) { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 7186 | ly_bool replaced = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7187 | |
| 7188 | /* only attributes of an elem can be in the result, skip all the rest, |
| 7189 | * we have all attributes qualified in lyd tree */ |
| 7190 | if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 7191 | for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7192 | /* check "namespace" */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7193 | if (mod && (sub->annotation->module != mod)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7194 | continue; |
| 7195 | } |
| 7196 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7197 | if (!ncname || (sub->name == ncname)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7198 | /* match */ |
| 7199 | if (!replaced) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 7200 | set->val.meta[i].meta = sub; |
| 7201 | set->val.meta[i].type = LYXP_NODE_META; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7202 | /* pos does not change */ |
| 7203 | replaced = 1; |
| 7204 | } else { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 7205 | 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] | 7206 | } |
| 7207 | ++i; |
| 7208 | } |
| 7209 | } |
| 7210 | } |
| 7211 | |
| 7212 | if (!replaced) { |
| 7213 | /* no match */ |
| 7214 | set_remove_node(set, i); |
| 7215 | } |
| 7216 | } |
| 7217 | |
| 7218 | return LY_SUCCESS; |
| 7219 | } |
| 7220 | |
| 7221 | /** |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 7222 | * @brief Move context @p set1 single item to the result of a comparison. |
| 7223 | * |
| 7224 | * @param[in] set1 First set with the item to compare. |
| 7225 | * @param[in] idx1 Index of the item in @p set1. |
| 7226 | * @param[in] set2 Second set. |
| 7227 | * @param[in] op Comparison operator to process. |
| 7228 | * @param[in] switch_operands Whether to switch sets as operands; whether it is `set1 op set2` or `set2 op set1`. |
| 7229 | * @param[out] result Result of the comparison. |
| 7230 | * @return LY_ERR value. |
| 7231 | */ |
| 7232 | static LY_ERR |
| 7233 | moveto_op_comp_item(const struct lyxp_set *set1, uint32_t idx1, struct lyxp_set *set2, const char *op, |
| 7234 | ly_bool switch_operands, ly_bool *result) |
| 7235 | { |
| 7236 | struct lyxp_set tmp1 = {0}; |
| 7237 | LY_ERR rc = LY_SUCCESS; |
| 7238 | |
| 7239 | assert(set1->type == LYXP_SET_NODE_SET); |
| 7240 | |
| 7241 | /* cast set1 */ |
| 7242 | switch (set2->type) { |
| 7243 | case LYXP_SET_NUMBER: |
| 7244 | rc = set_comp_cast(&tmp1, set1, LYXP_SET_NUMBER, idx1); |
| 7245 | break; |
| 7246 | case LYXP_SET_BOOLEAN: |
| 7247 | rc = set_comp_cast(&tmp1, set1, LYXP_SET_BOOLEAN, idx1); |
| 7248 | break; |
| 7249 | default: |
| 7250 | rc = set_comp_cast(&tmp1, set1, LYXP_SET_STRING, idx1); |
| 7251 | break; |
| 7252 | } |
| 7253 | LY_CHECK_GOTO(rc, cleanup); |
| 7254 | |
| 7255 | /* canonize set2 */ |
| 7256 | LY_CHECK_GOTO(rc = set_comp_canonize(set2, &set1->val.nodes[idx1]), cleanup); |
| 7257 | |
| 7258 | /* compare recursively and store the result */ |
| 7259 | if (switch_operands) { |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7260 | LY_CHECK_GOTO(rc = moveto_op_comp(set2, &tmp1, op, result), cleanup); |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 7261 | } else { |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7262 | LY_CHECK_GOTO(rc = moveto_op_comp(&tmp1, set2, op, result), cleanup); |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 7263 | } |
| 7264 | |
| 7265 | cleanup: |
| 7266 | lyxp_set_free_content(&tmp1); |
| 7267 | return rc; |
| 7268 | } |
| 7269 | |
| 7270 | /** |
| 7271 | * @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] | 7272 | * Result is LYXP_SET_BOOLEAN. Indirectly context position aware. |
| 7273 | * |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7274 | * @param[in] set1 Set acting as the first operand for @p op. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7275 | * @param[in] set2 Set acting as the second operand for @p op. |
| 7276 | * @param[in] op Comparison operator to process. |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7277 | * @param[out] result Result of the comparison. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7278 | * @return LY_ERR |
| 7279 | */ |
| 7280 | static LY_ERR |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7281 | 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] | 7282 | { |
| 7283 | /* |
| 7284 | * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING |
| 7285 | * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING) |
| 7286 | * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 7287 | * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 7288 | * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING |
| 7289 | * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 7290 | * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 7291 | * |
| 7292 | * '=' or '!=' |
| 7293 | * BOOLEAN + BOOLEAN |
| 7294 | * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 7295 | * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 7296 | * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 7297 | * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 7298 | * NUMBER + NUMBER |
| 7299 | * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 7300 | * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 7301 | * STRING + STRING |
| 7302 | * |
| 7303 | * '<=', '<', '>=', '>' |
| 7304 | * NUMBER + NUMBER |
| 7305 | * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 7306 | * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 7307 | * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 7308 | * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 7309 | * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 7310 | * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 7311 | * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 7312 | * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 7313 | */ |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 7314 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7315 | LY_ERR rc; |
| 7316 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7317 | /* iterative evaluation with node-sets */ |
| 7318 | if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) { |
| 7319 | if (set1->type == LYXP_SET_NODE_SET) { |
| 7320 | for (i = 0; i < set1->used; ++i) { |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 7321 | /* evaluate for the single item */ |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7322 | 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] | 7323 | |
| 7324 | /* lazy evaluation until true */ |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7325 | if (*result) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7326 | return LY_SUCCESS; |
| 7327 | } |
| 7328 | } |
| 7329 | } else { |
| 7330 | for (i = 0; i < set2->used; ++i) { |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 7331 | /* evaluate for the single item */ |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7332 | 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] | 7333 | |
| 7334 | /* lazy evaluation until true */ |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7335 | if (*result) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7336 | return LY_SUCCESS; |
| 7337 | } |
| 7338 | } |
| 7339 | } |
| 7340 | |
Michal Vasko | 8abcecc | 2022-07-28 09:55:01 +0200 | [diff] [blame] | 7341 | /* false for all the nodes */ |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7342 | *result = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7343 | return LY_SUCCESS; |
| 7344 | } |
| 7345 | |
| 7346 | /* first convert properly */ |
| 7347 | if ((op[0] == '=') || (op[0] == '!')) { |
| 7348 | if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7349 | lyxp_set_cast(set1, LYXP_SET_BOOLEAN); |
| 7350 | lyxp_set_cast(set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7351 | } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7352 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7353 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7354 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7355 | LY_CHECK_RET(rc); |
| 7356 | } /* else we have 2 strings */ |
| 7357 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7358 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7359 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7360 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7361 | LY_CHECK_RET(rc); |
| 7362 | } |
| 7363 | |
| 7364 | assert(set1->type == set2->type); |
| 7365 | |
| 7366 | /* compute result */ |
| 7367 | if (op[0] == '=') { |
| 7368 | if (set1->type == LYXP_SET_BOOLEAN) { |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7369 | *result = (set1->val.bln == set2->val.bln); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7370 | } else if (set1->type == LYXP_SET_NUMBER) { |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7371 | *result = (set1->val.num == set2->val.num); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7372 | } else { |
| 7373 | assert(set1->type == LYXP_SET_STRING); |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7374 | *result = strcmp(set1->val.str, set2->val.str) ? 0 : 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7375 | } |
| 7376 | } else if (op[0] == '!') { |
| 7377 | if (set1->type == LYXP_SET_BOOLEAN) { |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7378 | *result = (set1->val.bln != set2->val.bln); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7379 | } else if (set1->type == LYXP_SET_NUMBER) { |
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 | } else { |
| 7382 | assert(set1->type == LYXP_SET_STRING); |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7383 | *result = strcmp(set1->val.str, set2->val.str) ? 1 : 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7384 | } |
| 7385 | } else { |
| 7386 | assert(set1->type == LYXP_SET_NUMBER); |
| 7387 | if (op[0] == '<') { |
| 7388 | if (op[1] == '=') { |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7389 | *result = (set1->val.num <= set2->val.num); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7390 | } else { |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7391 | *result = (set1->val.num < set2->val.num); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7392 | } |
| 7393 | } else { |
| 7394 | if (op[1] == '=') { |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7395 | *result = (set1->val.num >= set2->val.num); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7396 | } else { |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 7397 | *result = (set1->val.num > set2->val.num); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7398 | } |
| 7399 | } |
| 7400 | } |
| 7401 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7402 | return LY_SUCCESS; |
| 7403 | } |
| 7404 | |
| 7405 | /** |
| 7406 | * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div', |
| 7407 | * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware. |
| 7408 | * |
| 7409 | * @param[in,out] set1 Set to use for the result. |
| 7410 | * @param[in] set2 Set acting as the second operand for @p op. |
| 7411 | * @param[in] op Operator to process. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7412 | * @return LY_ERR |
| 7413 | */ |
| 7414 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7415 | 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] | 7416 | { |
| 7417 | LY_ERR rc; |
| 7418 | |
| 7419 | /* unary '-' */ |
| 7420 | if (!set2 && (op[0] == '-')) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7421 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7422 | LY_CHECK_RET(rc); |
| 7423 | set1->val.num *= -1; |
| 7424 | lyxp_set_free(set2); |
| 7425 | return LY_SUCCESS; |
| 7426 | } |
| 7427 | |
| 7428 | assert(set1 && set2); |
| 7429 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7430 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7431 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7432 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7433 | LY_CHECK_RET(rc); |
| 7434 | |
| 7435 | switch (op[0]) { |
| 7436 | /* '+' */ |
| 7437 | case '+': |
| 7438 | set1->val.num += set2->val.num; |
| 7439 | break; |
| 7440 | |
| 7441 | /* '-' */ |
| 7442 | case '-': |
| 7443 | set1->val.num -= set2->val.num; |
| 7444 | break; |
| 7445 | |
| 7446 | /* '*' */ |
| 7447 | case '*': |
| 7448 | set1->val.num *= set2->val.num; |
| 7449 | break; |
| 7450 | |
| 7451 | /* 'div' */ |
| 7452 | case 'd': |
| 7453 | set1->val.num /= set2->val.num; |
| 7454 | break; |
| 7455 | |
| 7456 | /* 'mod' */ |
| 7457 | case 'm': |
| 7458 | set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num); |
| 7459 | break; |
| 7460 | |
| 7461 | default: |
| 7462 | LOGINT_RET(set1->ctx); |
| 7463 | } |
| 7464 | |
| 7465 | return LY_SUCCESS; |
| 7466 | } |
| 7467 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7468 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7469 | * @brief Evaluate Predicate. Logs directly on error. |
| 7470 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7471 | * [9] Predicate ::= '[' Expr ']' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7472 | * |
| 7473 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7474 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7475 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7476 | * @param[in] options XPath options. |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7477 | * @param[in] axis Axis to search on. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7478 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7479 | */ |
| 7480 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 7481 | 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] | 7482 | { |
| 7483 | LY_ERR rc; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 7484 | uint32_t i, orig_exp, orig_pos, orig_size; |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 7485 | int32_t pred_in_ctx; |
Michal Vasko | 88a9e80 | 2022-05-24 10:50:28 +0200 | [diff] [blame] | 7486 | ly_bool reverse_axis = 0; |
aPiecek | fff4dca | 2021-10-07 10:59:53 +0200 | [diff] [blame] | 7487 | struct lyxp_set set2 = {0}; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7488 | |
| 7489 | /* '[' */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7490 | 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] | 7491 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7492 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7493 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7494 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7495 | only_parse: |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7496 | 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] | 7497 | LY_CHECK_RET(rc); |
| 7498 | } else if (set->type == LYXP_SET_NODE_SET) { |
| 7499 | /* 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] | 7500 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7501 | |
| 7502 | /* empty set, nothing to evaluate */ |
| 7503 | if (!set->used) { |
| 7504 | goto only_parse; |
| 7505 | } |
| 7506 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7507 | /* decide forward or reverse axis */ |
| 7508 | switch (axis) { |
| 7509 | case LYXP_AXIS_ANCESTOR: |
| 7510 | case LYXP_AXIS_ANCESTOR_OR_SELF: |
| 7511 | case LYXP_AXIS_PRECEDING: |
| 7512 | case LYXP_AXIS_PRECEDING_SIBLING: |
| 7513 | reverse_axis = 1; |
| 7514 | break; |
| 7515 | case LYXP_AXIS_DESCENDANT: |
| 7516 | case LYXP_AXIS_DESCENDANT_OR_SELF: |
| 7517 | case LYXP_AXIS_FOLLOWING: |
| 7518 | case LYXP_AXIS_FOLLOWING_SIBLING: |
| 7519 | case LYXP_AXIS_PARENT: |
| 7520 | case LYXP_AXIS_CHILD: |
| 7521 | case LYXP_AXIS_SELF: |
| 7522 | case LYXP_AXIS_ATTRIBUTE: |
| 7523 | reverse_axis = 0; |
| 7524 | break; |
| 7525 | } |
| 7526 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7527 | orig_exp = *tok_idx; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7528 | orig_pos = reverse_axis ? set->used + 1 : 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7529 | orig_size = set->used; |
Michal Vasko | 39dbf35 | 2020-05-21 10:08:59 +0200 | [diff] [blame] | 7530 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7531 | set_init(&set2, set); |
| 7532 | 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] | 7533 | |
| 7534 | /* remember the node context position for position() and context size for last() */ |
| 7535 | orig_pos += reverse_axis ? -1 : 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7536 | |
| 7537 | set2.ctx_pos = orig_pos; |
| 7538 | set2.ctx_size = orig_size; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7539 | *tok_idx = orig_exp; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7540 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7541 | rc = eval_expr_select(exp, tok_idx, 0, &set2, options); |
Michal Vasko | b7ba2c9 | 2024-01-05 15:17:53 +0100 | [diff] [blame] | 7542 | if (!rc && set2.not_found) { |
| 7543 | set->not_found = 1; |
| 7544 | break; |
| 7545 | } |
| 7546 | if (rc) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7547 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7548 | return rc; |
| 7549 | } |
| 7550 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7551 | /* number is a proximity position */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7552 | if (set2.type == LYXP_SET_NUMBER) { |
| 7553 | if ((long long)set2.val.num == orig_pos) { |
| 7554 | set2.val.num = 1; |
| 7555 | } else { |
| 7556 | set2.val.num = 0; |
| 7557 | } |
| 7558 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7559 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7560 | |
| 7561 | /* predicate satisfied or not? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7562 | if (!set2.val.bln) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 7563 | set_remove_node_none(set, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7564 | } |
| 7565 | } |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 7566 | set_remove_nodes_none(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7567 | |
| 7568 | } else if (set->type == LYXP_SET_SCNODE_SET) { |
| 7569 | for (i = 0; i < set->used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 7570 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7571 | /* there is a currently-valid node */ |
| 7572 | break; |
| 7573 | } |
| 7574 | } |
| 7575 | /* empty set, nothing to evaluate */ |
| 7576 | if (i == set->used) { |
| 7577 | goto only_parse; |
| 7578 | } |
| 7579 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7580 | orig_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7581 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7582 | /* set special in_ctx to all the valid snodes */ |
| 7583 | pred_in_ctx = set_scnode_new_in_ctx(set); |
| 7584 | |
| 7585 | /* use the valid snodes one-by-one */ |
| 7586 | for (i = 0; i < set->used; ++i) { |
| 7587 | if (set->val.scnodes[i].in_ctx != pred_in_ctx) { |
| 7588 | continue; |
| 7589 | } |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 7590 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7591 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7592 | *tok_idx = orig_exp; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7593 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7594 | rc = eval_expr_select(exp, tok_idx, 0, set, options); |
Michal Vasko | b7ba2c9 | 2024-01-05 15:17:53 +0100 | [diff] [blame] | 7595 | if (!rc && set->not_found) { |
| 7596 | break; |
| 7597 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7598 | LY_CHECK_RET(rc); |
| 7599 | |
| 7600 | set->val.scnodes[i].in_ctx = pred_in_ctx; |
| 7601 | } |
| 7602 | |
| 7603 | /* restore the state as it was before the predicate */ |
| 7604 | for (i = 0; i < set->used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 7605 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 7606 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7607 | } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 7608 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7609 | } |
| 7610 | } |
| 7611 | |
| 7612 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7613 | set2.type = LYXP_SET_NODE_SET; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7614 | set_fill_set(&set2, set); |
| 7615 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7616 | rc = eval_expr_select(exp, tok_idx, 0, &set2, options); |
Michal Vasko | b7ba2c9 | 2024-01-05 15:17:53 +0100 | [diff] [blame] | 7617 | if (rc) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7618 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7619 | return rc; |
| 7620 | } |
| 7621 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7622 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7623 | if (!set2.val.bln) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7624 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7625 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7626 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7627 | } |
| 7628 | |
| 7629 | /* ']' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7630 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7631 | 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] | 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 | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7634 | |
| 7635 | return LY_SUCCESS; |
| 7636 | } |
| 7637 | |
| 7638 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7639 | * @brief Evaluate Literal. Logs directly on error. |
| 7640 | * |
| 7641 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7642 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7643 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7644 | */ |
| 7645 | static void |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 7646 | 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] | 7647 | { |
| 7648 | if (set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7649 | if (exp->tok_len[*tok_idx] == 2) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7650 | set_fill_string(set, "", 0); |
| 7651 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7652 | 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] | 7653 | } |
| 7654 | } |
| 7655 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 7656 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7657 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7658 | } |
| 7659 | |
| 7660 | /** |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7661 | * @brief Check that a nametest in a predicate matches a key node. |
| 7662 | * |
| 7663 | * @param[in] nametest Nametest to check. |
| 7664 | * @param[in] len Length of @p nametest. |
| 7665 | * @param[in] ctx_scnode Found schema node as the context for the predicate. |
| 7666 | * @param[in] set Context set. |
| 7667 | * @param[in] key Expected key node. |
| 7668 | * @return LY_SUCCESS on success, |
| 7669 | * @return LY_ENOT if a predicate could not be compiled. |
| 7670 | * @return LY_ERR on any error. |
| 7671 | */ |
| 7672 | static LY_ERR |
| 7673 | eval_name_test_try_compile_predicate_key(const char *nametest, uint32_t len, const struct lysc_node *ctx_scnode, |
| 7674 | const struct lyxp_set *set, const struct lysc_node *key) |
| 7675 | { |
| 7676 | const struct lys_module *mod; |
| 7677 | |
| 7678 | /* prefix (module) */ |
| 7679 | LY_CHECK_RET(moveto_resolve_model(&nametest, &len, set, ctx_scnode, &mod)); |
| 7680 | if (mod != key->module) { |
| 7681 | return LY_ENOT; |
| 7682 | } |
| 7683 | |
| 7684 | /* node name */ |
| 7685 | if (ly_strncmp(key->name, nametest, len)) { |
| 7686 | return LY_ENOT; |
| 7687 | } |
| 7688 | |
| 7689 | return LY_SUCCESS; |
| 7690 | } |
| 7691 | |
| 7692 | /** |
| 7693 | * @brief Append a simple predicate for the node. |
| 7694 | * |
| 7695 | * @param[in] exp Full parsed XPath expression. |
| 7696 | * @param[in] tok_idx Predicate start index in @p exp. |
| 7697 | * @param[in] end_tok_idx Predicate end index in @p exp. |
| 7698 | * @param[in] ctx_scnode Found schema node as the context for the predicate. |
| 7699 | * @param[in] set Context set. |
| 7700 | * @param[in] pred_node Node with the value referenced in the predicate. |
| 7701 | * @param[in,out] pred Predicate to append to. |
| 7702 | * @param[in,out] pred_len Length of @p pred, is updated. |
| 7703 | * @return LY_SUCCESS on success, |
| 7704 | * @return LY_ENOT if a predicate could not be compiled. |
| 7705 | * @return LY_ERR on any error. |
| 7706 | */ |
| 7707 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 7708 | 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] | 7709 | const struct lysc_node *ctx_scnode, const struct lyxp_set *set, const struct lysc_node *pred_node, char **pred, |
| 7710 | uint32_t *pred_len) |
| 7711 | { |
| 7712 | LY_ERR rc = LY_SUCCESS; |
| 7713 | uint32_t i; |
| 7714 | const struct lyd_node *siblings; |
| 7715 | struct lyd_node *ctx_node; |
Michal Vasko | 5fb92a2 | 2022-08-02 09:21:37 +0200 | [diff] [blame] | 7716 | const struct lysc_node *sparent, *cur_scnode; |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7717 | struct lyxp_expr *val_exp = NULL; |
| 7718 | struct lyxp_set set2 = {0}; |
| 7719 | char quot; |
| 7720 | |
| 7721 | /* duplicate the value expression */ |
| 7722 | LY_CHECK_GOTO(rc = lyxp_expr_dup(set->ctx, exp, tok_idx, end_tok_idx, &val_exp), cleanup); |
| 7723 | |
| 7724 | /* get its atoms */ |
Michal Vasko | 5fb92a2 | 2022-08-02 09:21:37 +0200 | [diff] [blame] | 7725 | cur_scnode = set->cur_node ? set->cur_node->schema : NULL; |
| 7726 | LY_CHECK_GOTO(rc = lyxp_atomize(set->ctx, val_exp, set->cur_mod, set->format, set->prefix_data, cur_scnode, |
| 7727 | ctx_scnode, &set2, LYXP_SCNODE), cleanup); |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7728 | |
| 7729 | /* check whether we can compile a single predicate (evaluation result value is always the same) */ |
| 7730 | for (i = 0; i < set2.used; ++i) { |
| 7731 | if ((set2.val.scnodes[i].type != LYXP_NODE_ELEM) || (set2.val.scnodes[i].in_ctx < LYXP_SET_SCNODE_ATOM_NODE)) { |
| 7732 | /* skip root and context node */ |
| 7733 | continue; |
| 7734 | } |
| 7735 | |
Michal Vasko | 5fb92a2 | 2022-08-02 09:21:37 +0200 | [diff] [blame] | 7736 | /* 1) context node descendants are traversed - do best-effort detection of the value dependency on the |
| 7737 | * context node instance */ |
| 7738 | if ((set2.val.scnodes[i].axis == LYXP_AXIS_CHILD) && (set2.val.scnodes[i].scnode->parent == ctx_scnode)) { |
| 7739 | /* 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] | 7740 | rc = LY_ENOT; |
| 7741 | goto cleanup; |
| 7742 | } |
Michal Vasko | 5fb92a2 | 2022-08-02 09:21:37 +0200 | [diff] [blame] | 7743 | if ((set2.val.scnodes[i].axis == LYXP_AXIS_DESCENDANT) || (set2.val.scnodes[i].axis == LYXP_AXIS_DESCENDANT_OR_SELF)) { |
| 7744 | for (sparent = set2.val.scnodes[i].scnode->parent; sparent && (sparent != ctx_scnode); sparent = sparent->parent) {} |
| 7745 | if (sparent) { |
| 7746 | /* 1.2) context node descendant was accessed on the descendant axis, probable dependency */ |
| 7747 | rc = LY_ENOT; |
| 7748 | goto cleanup; |
| 7749 | } |
| 7750 | } |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7751 | |
Michal Vasko | 5fb92a2 | 2022-08-02 09:21:37 +0200 | [diff] [blame] | 7752 | /* 2) multi-instance nodes (list or leaf-list) are traversed - all the instances need to be considered, |
| 7753 | * but the current node can be safely ignored, it is always the same data instance */ |
| 7754 | 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] | 7755 | rc = LY_ENOT; |
| 7756 | goto cleanup; |
| 7757 | } |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7758 | } |
| 7759 | |
| 7760 | /* get any data instance of the context node, we checked it makes no difference */ |
| 7761 | siblings = set->val.nodes[0].node ? lyd_child(set->val.nodes[0].node) : set->tree; |
| 7762 | LY_CHECK_GOTO(rc = lyd_find_sibling_schema(siblings, ctx_scnode, &ctx_node), cleanup); |
| 7763 | |
| 7764 | /* evaluate the value subexpression with the root context node */ |
| 7765 | lyxp_set_free_content(&set2); |
| 7766 | LY_CHECK_GOTO(rc = lyxp_eval(set->ctx, val_exp, set->cur_mod, set->format, set->prefix_data, set->cur_node, |
| 7767 | ctx_node, set->tree, NULL, &set2, 0), cleanup); |
| 7768 | |
| 7769 | /* cast it into a string */ |
| 7770 | LY_CHECK_GOTO(rc = lyxp_set_cast(&set2, LYXP_SET_STRING), cleanup); |
| 7771 | |
| 7772 | /* append the JSON predicate */ |
| 7773 | *pred = ly_realloc(*pred, *pred_len + 1 + strlen(pred_node->name) + 2 + strlen(set2.val.str) + 3); |
| 7774 | LY_CHECK_ERR_GOTO(!*pred, LOGMEM(set->ctx); rc = LY_EMEM, cleanup); |
| 7775 | quot = strchr(set2.val.str, '\'') ? '\"' : '\''; |
| 7776 | *pred_len += sprintf(*pred + *pred_len, "[%s=%c%s%c]", pred_node->name, quot, set2.val.str, quot); |
| 7777 | |
| 7778 | cleanup: |
| 7779 | lyxp_expr_free(set->ctx, val_exp); |
| 7780 | lyxp_set_free_content(&set2); |
| 7781 | return rc; |
| 7782 | } |
| 7783 | |
| 7784 | /** |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7785 | * @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] | 7786 | * |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7787 | * @param[in] exp Full parsed XPath expression. |
| 7788 | * @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] | 7789 | * @param[in] ctx_scnode Found schema node as the context for the predicate. |
| 7790 | * @param[in] set Context set. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7791 | * @param[out] predicates Parsed predicates. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7792 | * @return LY_SUCCESS on success, |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7793 | * @return LY_ENOT if a predicate could not be compiled. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7794 | * @return LY_ERR on any error. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7795 | */ |
| 7796 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 7797 | 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] | 7798 | const struct lyxp_set *set, struct ly_path_predicate **predicates) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7799 | { |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7800 | LY_ERR rc = LY_SUCCESS; |
Michal Vasko | 59004e3 | 2024-01-30 16:09:54 +0100 | [diff] [blame] | 7801 | uint32_t e_idx, val_start_idx, pred_idx = 0, *prev_lo, temp_lo = 0, pred_len = 0, nested_pred; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7802 | const struct lysc_node *key; |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7803 | char *pred = NULL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7804 | struct lyxp_expr *exp2 = NULL; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7805 | |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7806 | assert(ctx_scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7807 | |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7808 | /* turn logging off */ |
Michal Vasko | 59004e3 | 2024-01-30 16:09:54 +0100 | [diff] [blame] | 7809 | prev_lo = ly_temp_log_options(&temp_lo); |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7810 | |
| 7811 | if (ctx_scnode->nodetype == LYS_LIST) { |
| 7812 | /* check for predicates "[key1=...][key2=...]..." */ |
| 7813 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7814 | /* get key count */ |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7815 | if (ctx_scnode->flags & LYS_KEYLESS) { |
| 7816 | rc = LY_ENOT; |
| 7817 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7818 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7819 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7820 | /* learn where the predicates end */ |
| 7821 | e_idx = *tok_idx; |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7822 | 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] | 7823 | /* '[' */ |
| 7824 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) { |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7825 | rc = LY_ENOT; |
| 7826 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7827 | } |
| 7828 | ++e_idx; |
| 7829 | |
Michal Vasko | 3354d27 | 2021-04-06 09:40:06 +0200 | [diff] [blame] | 7830 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_NAMETEST)) { |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7831 | /* not a key */ |
| 7832 | rc = LY_ENOT; |
| 7833 | goto cleanup; |
Michal Vasko | 3354d27 | 2021-04-06 09:40:06 +0200 | [diff] [blame] | 7834 | } |
| 7835 | |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7836 | /* check key */ |
| 7837 | LY_CHECK_GOTO(rc = eval_name_test_try_compile_predicate_key(exp->expr + exp->tok_pos[e_idx], |
| 7838 | exp->tok_len[e_idx], ctx_scnode, set, key), cleanup); |
| 7839 | |
| 7840 | ++e_idx; |
| 7841 | |
| 7842 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_OPER_EQUAL)) { |
| 7843 | /* not '=' */ |
| 7844 | rc = LY_ENOT; |
| 7845 | goto cleanup; |
| 7846 | } |
| 7847 | ++e_idx; |
| 7848 | |
| 7849 | /* value start */ |
| 7850 | val_start_idx = e_idx; |
| 7851 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7852 | /* ']' */ |
Michal Vasko | 3ef7e25 | 2022-11-16 08:29:49 +0100 | [diff] [blame] | 7853 | nested_pred = 1; |
| 7854 | do { |
| 7855 | ++e_idx; |
| 7856 | |
| 7857 | 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] | 7858 | /* higher priority than '=' */ |
| 7859 | rc = LY_ENOT; |
| 7860 | goto cleanup; |
Michal Vasko | 3ef7e25 | 2022-11-16 08:29:49 +0100 | [diff] [blame] | 7861 | } else if (!lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) { |
| 7862 | /* nested predicate */ |
| 7863 | ++nested_pred; |
| 7864 | } else if (!lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) { |
| 7865 | /* predicate end */ |
| 7866 | --nested_pred; |
Michal Vasko | b8ee356 | 2022-08-02 10:43:17 +0200 | [diff] [blame] | 7867 | } |
Michal Vasko | 3ef7e25 | 2022-11-16 08:29:49 +0100 | [diff] [blame] | 7868 | } while (nested_pred); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7869 | |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7870 | /* try to evaluate the value */ |
| 7871 | LY_CHECK_GOTO(rc = eval_name_test_try_compile_predicate_append(exp, val_start_idx, e_idx - 1, ctx_scnode, |
| 7872 | set, key, &pred, &pred_len), cleanup); |
| 7873 | |
| 7874 | ++e_idx; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7875 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7876 | } else { |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7877 | /* check for predicate "[.=...]" */ |
| 7878 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7879 | /* learn just where this single predicate ends */ |
| 7880 | e_idx = *tok_idx; |
| 7881 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7882 | /* '[' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7883 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) { |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7884 | rc = LY_ENOT; |
| 7885 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7886 | } |
| 7887 | ++e_idx; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7888 | |
Michal Vasko | 3354d27 | 2021-04-06 09:40:06 +0200 | [diff] [blame] | 7889 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_DOT)) { |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7890 | /* not the node value */ |
| 7891 | rc = LY_ENOT; |
| 7892 | goto cleanup; |
Michal Vasko | 3354d27 | 2021-04-06 09:40:06 +0200 | [diff] [blame] | 7893 | } |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7894 | ++e_idx; |
| 7895 | |
| 7896 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_OPER_EQUAL)) { |
| 7897 | /* not '=' */ |
| 7898 | rc = LY_ENOT; |
| 7899 | goto cleanup; |
| 7900 | } |
| 7901 | ++e_idx; |
| 7902 | |
| 7903 | /* value start */ |
| 7904 | val_start_idx = e_idx; |
Michal Vasko | 3354d27 | 2021-04-06 09:40:06 +0200 | [diff] [blame] | 7905 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7906 | /* ']' */ |
Michal Vasko | 31f1963 | 2022-11-16 09:38:40 +0100 | [diff] [blame] | 7907 | nested_pred = 1; |
| 7908 | do { |
| 7909 | ++e_idx; |
| 7910 | |
| 7911 | 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] | 7912 | /* higher priority than '=' */ |
| 7913 | rc = LY_ENOT; |
| 7914 | goto cleanup; |
Michal Vasko | 31f1963 | 2022-11-16 09:38:40 +0100 | [diff] [blame] | 7915 | } else if (!lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) { |
| 7916 | /* nested predicate */ |
| 7917 | ++nested_pred; |
| 7918 | } else if (!lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) { |
| 7919 | /* predicate end */ |
| 7920 | --nested_pred; |
Michal Vasko | b8ee356 | 2022-08-02 10:43:17 +0200 | [diff] [blame] | 7921 | } |
Michal Vasko | 31f1963 | 2022-11-16 09:38:40 +0100 | [diff] [blame] | 7922 | } while (nested_pred); |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7923 | |
| 7924 | /* try to evaluate the value */ |
| 7925 | LY_CHECK_GOTO(rc = eval_name_test_try_compile_predicate_append(exp, val_start_idx, e_idx - 1, ctx_scnode, set, |
| 7926 | ctx_scnode, &pred, &pred_len), cleanup); |
| 7927 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7928 | ++e_idx; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7929 | } |
| 7930 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7931 | /* parse the predicate(s) */ |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7932 | LY_CHECK_GOTO(rc = ly_path_parse_predicate(set->ctx, ctx_scnode, pred, pred_len, LY_PATH_PREFIX_OPTIONAL, |
| 7933 | LY_PATH_PRED_SIMPLE, &exp2), cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7934 | |
| 7935 | /* compile */ |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7936 | 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] | 7937 | &pred_idx, LY_VALUE_JSON, NULL, predicates); |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7938 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7939 | |
| 7940 | /* success, the predicate must include all the needed information for hash-based search */ |
| 7941 | *tok_idx = e_idx; |
| 7942 | |
| 7943 | cleanup: |
Michal Vasko | 59004e3 | 2024-01-30 16:09:54 +0100 | [diff] [blame] | 7944 | ly_temp_log_options(prev_lo); |
Michal Vasko | 3d969ff | 2022-07-29 15:02:08 +0200 | [diff] [blame] | 7945 | lyxp_expr_free(set->ctx, exp2); |
| 7946 | free(pred); |
| 7947 | return rc; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7948 | } |
| 7949 | |
| 7950 | /** |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7951 | * @brief Search for/check the next schema node that could be the only matching schema node meaning the |
| 7952 | * data node(s) could be found using a single hash-based search. |
| 7953 | * |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7954 | * @param[in] ctx libyang context. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7955 | * @param[in] node Next context node to check. |
| 7956 | * @param[in] name Expected node name. |
| 7957 | * @param[in] name_len Length of @p name. |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7958 | * @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] | 7959 | * @param[in] root_type XPath root type. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7960 | * @param[in] format Prefix format. |
| 7961 | * @param[in,out] found Previously found node, is updated. |
| 7962 | * @return LY_SUCCESS on success, |
| 7963 | * @return LY_ENOT if the whole check failed and hashes cannot be used. |
| 7964 | */ |
| 7965 | static LY_ERR |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7966 | 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] | 7967 | 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] | 7968 | const struct lysc_node **found) |
| 7969 | { |
Michal Vasko | c730448 | 2023-11-30 15:59:30 +0100 | [diff] [blame] | 7970 | const struct lysc_node *scnode, *scnode2; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7971 | const struct lys_module *mod; |
| 7972 | uint32_t idx = 0; |
| 7973 | |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 7974 | assert((format == LY_VALUE_JSON) || moveto_mod); |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7975 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7976 | continue_search: |
Michal Vasko | 7d1d091 | 2020-10-16 08:38:30 +0200 | [diff] [blame] | 7977 | scnode = NULL; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7978 | if (!node) { |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 7979 | if ((format == LY_VALUE_JSON) && !moveto_mod) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7980 | /* search all modules for a single match */ |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7981 | while ((mod = ly_ctx_get_module_iter(ctx, &idx))) { |
Michal Vasko | 35a3b1d | 2021-07-14 09:34:16 +0200 | [diff] [blame] | 7982 | if (!mod->implemented) { |
| 7983 | continue; |
| 7984 | } |
| 7985 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7986 | scnode = lys_find_child(NULL, mod, name, name_len, 0, 0); |
| 7987 | if (scnode) { |
| 7988 | /* we have found a match */ |
| 7989 | break; |
| 7990 | } |
| 7991 | } |
| 7992 | |
Michal Vasko | 7d1d091 | 2020-10-16 08:38:30 +0200 | [diff] [blame] | 7993 | if (!scnode) { |
| 7994 | /* all modules searched */ |
| 7995 | idx = 0; |
| 7996 | } |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7997 | } else { |
| 7998 | /* search in top-level */ |
| 7999 | scnode = lys_find_child(NULL, moveto_mod, name, name_len, 0, 0); |
| 8000 | } |
Michal Vasko | 2b0e8a2 | 2024-03-08 14:10:48 +0100 | [diff] [blame] | 8001 | } else if (node->schema && (!*found || (lysc_data_parent(*found) != node->schema))) { |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 8002 | if ((format == LY_VALUE_JSON) && !moveto_mod) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8003 | /* we must adjust the module to inherit the one from the context node */ |
| 8004 | moveto_mod = node->schema->module; |
| 8005 | } |
| 8006 | |
| 8007 | /* search in children, do not repeat the same search */ |
Michal Vasko | c730448 | 2023-11-30 15:59:30 +0100 | [diff] [blame] | 8008 | if (node->schema->nodetype & (LYS_RPC | LYS_ACTION)) { |
| 8009 | /* make sure the node is unique, whether in input or output */ |
| 8010 | scnode = lys_find_child(node->schema, moveto_mod, name, name_len, 0, 0); |
| 8011 | scnode2 = lys_find_child(node->schema, moveto_mod, name, name_len, 0, LYS_GETNEXT_OUTPUT); |
| 8012 | if (scnode && scnode2) { |
| 8013 | /* conflict, do not use hashes */ |
| 8014 | scnode = NULL; |
| 8015 | } else if (scnode2) { |
| 8016 | scnode = scnode2; |
| 8017 | } |
| 8018 | } else { |
| 8019 | scnode = lys_find_child(node->schema, moveto_mod, name, name_len, 0, 0); |
| 8020 | } |
Michal Vasko | 7d1d091 | 2020-10-16 08:38:30 +0200 | [diff] [blame] | 8021 | } /* else skip redundant search */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8022 | |
| 8023 | /* additional context check */ |
| 8024 | if (scnode && (root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) { |
| 8025 | scnode = NULL; |
| 8026 | } |
| 8027 | |
| 8028 | if (scnode) { |
| 8029 | if (*found) { |
| 8030 | /* we found a schema node with the same name but at different level, give up, too complicated |
| 8031 | * (more hash-based searches would be required, not supported) */ |
| 8032 | return LY_ENOT; |
| 8033 | } else { |
| 8034 | /* remember the found schema node and continue to make sure it can be used */ |
| 8035 | *found = scnode; |
| 8036 | } |
| 8037 | } |
| 8038 | |
| 8039 | if (idx) { |
| 8040 | /* continue searching all the following models */ |
| 8041 | goto continue_search; |
| 8042 | } |
| 8043 | |
| 8044 | return LY_SUCCESS; |
| 8045 | } |
| 8046 | |
| 8047 | /** |
Michal Vasko | 4ad69e7 | 2021-10-26 16:25:55 +0200 | [diff] [blame] | 8048 | * @brief Generate message when no matching schema nodes were found for a path segment. |
| 8049 | * |
| 8050 | * @param[in] set XPath set. |
| 8051 | * @param[in] scparent Previous schema parent in the context, if only one. |
| 8052 | * @param[in] ncname XPath NCName being evaluated. |
| 8053 | * @param[in] ncname_len Length of @p ncname. |
| 8054 | * @param[in] expr Whole XPath expression. |
| 8055 | * @param[in] options XPath options. |
| 8056 | */ |
| 8057 | static void |
| 8058 | 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] | 8059 | uint32_t ncname_len, const char *expr, uint32_t options) |
Michal Vasko | 4ad69e7 | 2021-10-26 16:25:55 +0200 | [diff] [blame] | 8060 | { |
| 8061 | const char *format; |
| 8062 | char *path = NULL, *ppath = NULL; |
| 8063 | |
| 8064 | path = lysc_path(set->cur_scnode, LYSC_PATH_LOG, NULL, 0); |
| 8065 | if (scparent) { |
| 8066 | /* generate path for the parent */ |
| 8067 | if (scparent->type == LYXP_NODE_ELEM) { |
| 8068 | ppath = lysc_path(scparent->scnode, LYSC_PATH_LOG, NULL, 0); |
| 8069 | } else if (scparent->type == LYXP_NODE_ROOT) { |
| 8070 | ppath = strdup("<root>"); |
| 8071 | } else if (scparent->type == LYXP_NODE_ROOT_CONFIG) { |
| 8072 | ppath = strdup("<config-root>"); |
| 8073 | } |
| 8074 | } |
| 8075 | if (ppath) { |
| 8076 | format = "Schema node \"%.*s\" for parent \"%s\" not found; in expr \"%.*s\" with context node \"%s\"."; |
| 8077 | if (options & LYXP_SCNODE_ERROR) { |
Michal Vasko | 8d9889c | 2023-11-22 12:28:57 +0100 | [diff] [blame] | 8078 | LOGERR(set->ctx, LY_ENOTFOUND, format, ncname_len, ncname, ppath, (ncname - expr) + ncname_len, expr, path); |
Michal Vasko | 4ad69e7 | 2021-10-26 16:25:55 +0200 | [diff] [blame] | 8079 | } else { |
| 8080 | LOGWRN(set->ctx, format, ncname_len, ncname, ppath, (ncname - expr) + ncname_len, expr, path); |
| 8081 | } |
| 8082 | } else { |
| 8083 | format = "Schema node \"%.*s\" not found; in expr \"%.*s\" with context node \"%s\"."; |
| 8084 | if (options & LYXP_SCNODE_ERROR) { |
Michal Vasko | 8d9889c | 2023-11-22 12:28:57 +0100 | [diff] [blame] | 8085 | LOGERR(set->ctx, LY_ENOTFOUND, format, ncname_len, ncname, (ncname - expr) + ncname_len, expr, path); |
Michal Vasko | 4ad69e7 | 2021-10-26 16:25:55 +0200 | [diff] [blame] | 8086 | } else { |
| 8087 | LOGWRN(set->ctx, format, ncname_len, ncname, (ncname - expr) + ncname_len, expr, path); |
| 8088 | } |
| 8089 | } |
| 8090 | free(path); |
| 8091 | free(ppath); |
| 8092 | } |
| 8093 | |
| 8094 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8095 | * @brief Evaluate NameTest and any following Predicates. Logs directly on error. |
| 8096 | * |
| 8097 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 8098 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 8099 | * [7] NameTest ::= '*' | NCName ':' '*' | QName |
| 8100 | * |
| 8101 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8102 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8103 | * @param[in] axis What axis to search on. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8104 | * @param[in] all_desc Whether to search all the descendants or children only. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8105 | * @param[in,out] set Context and result set. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8106 | * @param[in] options XPath options. |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8107 | * @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] | 8108 | */ |
| 8109 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8110 | 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] | 8111 | struct lyxp_set *set, uint32_t options) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8112 | { |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8113 | LY_ERR rc = LY_SUCCESS, r; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8114 | const char *ncname, *ncname_dict = NULL; |
Michal Vasko | 56c008c | 2022-07-29 14:57:47 +0200 | [diff] [blame] | 8115 | uint32_t i, ncname_len; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8116 | const struct lys_module *moveto_mod = NULL; |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 8117 | const struct lysc_node *scnode = NULL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8118 | struct ly_path_predicate *predicates = NULL; |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8119 | int scnode_skip_pred = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8120 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8121 | 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] | 8122 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8123 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8124 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8125 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8126 | goto moveto; |
| 8127 | } |
| 8128 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8129 | ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]]; |
| 8130 | ncname_len = exp->tok_len[*tok_idx - 1]; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8131 | |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 8132 | if ((ncname[0] == '*') && (ncname_len == 1)) { |
| 8133 | /* all nodes will match */ |
| 8134 | goto moveto; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8135 | } |
| 8136 | |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 8137 | /* parse (and skip) module name */ |
| 8138 | rc = moveto_resolve_model(&ncname, &ncname_len, set, NULL, &moveto_mod); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8139 | LY_CHECK_GOTO(rc, cleanup); |
| 8140 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8141 | if ((ncname[0] == '*') && (ncname_len == 1)) { |
| 8142 | /* all nodes from the module will match */ |
| 8143 | goto moveto; |
| 8144 | } |
| 8145 | |
| 8146 | if (((set->format == LY_VALUE_JSON) || moveto_mod) && (axis == LYXP_AXIS_CHILD) && !all_desc && |
| 8147 | (set->type == LYXP_SET_NODE_SET)) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8148 | /* find the matching schema node in some parent in the context */ |
Michal Vasko | 56c008c | 2022-07-29 14:57:47 +0200 | [diff] [blame] | 8149 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 8150 | if (eval_name_test_with_predicate_get_scnode(set->ctx, set->val.nodes[i].node, ncname, ncname_len, |
| 8151 | moveto_mod, set->root_type, set->format, &scnode)) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8152 | /* check failed */ |
| 8153 | scnode = NULL; |
| 8154 | break; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8155 | } |
| 8156 | } |
| 8157 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8158 | if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) { |
| 8159 | /* try to create the predicates */ |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 8160 | 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] | 8161 | /* hashes cannot be used */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8162 | scnode = NULL; |
| 8163 | } |
| 8164 | } |
| 8165 | } |
| 8166 | |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 8167 | if (!scnode) { |
| 8168 | /* 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] | 8169 | * use dictionary for efficient comparison */ |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 8170 | 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] | 8171 | } |
| 8172 | |
| 8173 | moveto: |
| 8174 | /* move to the attribute(s), data node(s), or schema node(s) */ |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8175 | if (axis == LYXP_AXIS_ATTRIBUTE) { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8176 | if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 8177 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8178 | } else { |
| 8179 | if (all_desc) { |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 8180 | rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8181 | } else { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8182 | rc = moveto_attr(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8183 | } |
| 8184 | LY_CHECK_GOTO(rc, cleanup); |
| 8185 | } |
| 8186 | } else { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8187 | if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) { |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8188 | const struct lyxp_set_scnode *scparent = NULL; |
Michal Vasko | 56c008c | 2022-07-29 14:57:47 +0200 | [diff] [blame] | 8189 | ly_bool found = 0; |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8190 | |
| 8191 | /* remember parent if there is only one, to print in the warning */ |
| 8192 | for (i = 0; i < set->used; ++i) { |
| 8193 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
| 8194 | if (!scparent) { |
| 8195 | /* remember the context node */ |
| 8196 | scparent = &set->val.scnodes[i]; |
| 8197 | } else { |
| 8198 | /* several context nodes, no reasonable error possible */ |
| 8199 | scparent = NULL; |
| 8200 | break; |
| 8201 | } |
| 8202 | } |
| 8203 | } |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 8204 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8205 | if (all_desc && (axis == LYXP_AXIS_CHILD)) { |
| 8206 | /* efficient evaluation that does not add all the descendants into the set */ |
| 8207 | rc = moveto_scnode_alldesc_child(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8208 | } else { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8209 | if (all_desc) { |
| 8210 | /* "//" == "/descendant-or-self::node()/" */ |
| 8211 | rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options); |
| 8212 | LY_CHECK_GOTO(rc, cleanup); |
| 8213 | } |
| 8214 | rc = moveto_scnode(set, moveto_mod, ncname_dict, axis, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8215 | } |
| 8216 | LY_CHECK_GOTO(rc, cleanup); |
| 8217 | |
Michal Vasko | 56c008c | 2022-07-29 14:57:47 +0200 | [diff] [blame] | 8218 | i = set->used; |
| 8219 | do { |
| 8220 | --i; |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 8221 | if (set->val.scnodes[i].in_ctx > LYXP_SET_SCNODE_ATOM_NODE) { |
Michal Vasko | 56c008c | 2022-07-29 14:57:47 +0200 | [diff] [blame] | 8222 | found = 1; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8223 | break; |
| 8224 | } |
Michal Vasko | 56c008c | 2022-07-29 14:57:47 +0200 | [diff] [blame] | 8225 | } while (i); |
| 8226 | if (!found) { |
Michal Vasko | 4ad69e7 | 2021-10-26 16:25:55 +0200 | [diff] [blame] | 8227 | /* generate message */ |
| 8228 | eval_name_test_scnode_no_match_msg(set, scparent, ncname, ncname_len, exp->expr, options); |
| 8229 | |
| 8230 | if (options & LYXP_SCNODE_ERROR) { |
| 8231 | /* error */ |
Michal Vasko | b7ba2c9 | 2024-01-05 15:17:53 +0100 | [diff] [blame] | 8232 | set->not_found = 1; |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8233 | } |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8234 | |
| 8235 | /* skip the predicates and the rest of this path to not generate invalid warnings */ |
| 8236 | rc = LY_ENOT; |
| 8237 | scnode_skip_pred = 1; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8238 | } |
| 8239 | } else { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8240 | if (all_desc && (axis == LYXP_AXIS_CHILD)) { |
| 8241 | /* efficient evaluation */ |
| 8242 | rc = moveto_node_alldesc_child(set, moveto_mod, ncname_dict, options); |
| 8243 | } else if (scnode && (axis == LYXP_AXIS_CHILD)) { |
| 8244 | /* we can find the child nodes using hashes */ |
| 8245 | rc = moveto_node_hash_child(set, scnode, predicates, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8246 | } else { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8247 | if (all_desc) { |
| 8248 | /* "//" == "/descendant-or-self::node()/" */ |
| 8249 | rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options); |
| 8250 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8251 | } |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8252 | rc = moveto_node(set, moveto_mod, ncname_dict, axis, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8253 | } |
| 8254 | LY_CHECK_GOTO(rc, cleanup); |
| 8255 | } |
| 8256 | } |
| 8257 | |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8258 | if (scnode_skip_pred) { |
| 8259 | /* skip predicates */ |
| 8260 | options |= LYXP_SKIP_EXPR; |
| 8261 | } |
| 8262 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8263 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8264 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8265 | r = eval_predicate(exp, tok_idx, set, options, axis); |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8266 | LY_CHECK_ERR_GOTO(r, rc = r, cleanup); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8267 | } |
| 8268 | |
| 8269 | cleanup: |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8270 | if (scnode_skip_pred) { |
| 8271 | /* restore options */ |
| 8272 | options &= ~LYXP_SKIP_EXPR; |
| 8273 | } |
Michal Vasko | b7ba2c9 | 2024-01-05 15:17:53 +0100 | [diff] [blame] | 8274 | lydict_remove(set->ctx, ncname_dict); |
| 8275 | ly_path_predicates_free(set->ctx, predicates); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8276 | return rc; |
| 8277 | } |
| 8278 | |
| 8279 | /** |
| 8280 | * @brief Evaluate NodeType and any following Predicates. Logs directly on error. |
| 8281 | * |
| 8282 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 8283 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 8284 | * [8] NodeType ::= 'text' | 'node' |
| 8285 | * |
| 8286 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8287 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8288 | * @param[in] axis Axis to search on. |
| 8289 | * @param[in] all_desc Whether to search all the descendants or axis only. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8290 | * @param[in,out] set Context and result set. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8291 | * @param[in] options XPath options. |
| 8292 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8293 | */ |
| 8294 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8295 | 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] | 8296 | struct lyxp_set *set, uint32_t options) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8297 | { |
| 8298 | LY_ERR rc; |
| 8299 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8300 | (void)all_desc; |
| 8301 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8302 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8303 | assert(exp->tok_len[*tok_idx] == 4); |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8304 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) { |
| 8305 | rc = xpath_pi_node(set, axis, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8306 | } else { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8307 | assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4)); |
| 8308 | rc = xpath_pi_text(set, axis, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8309 | } |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8310 | LY_CHECK_RET(rc); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8311 | } |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8312 | 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] | 8313 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8314 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8315 | |
| 8316 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8317 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8318 | 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] | 8319 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8320 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8321 | |
| 8322 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8323 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8324 | 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] | 8325 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8326 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8327 | |
| 8328 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8329 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8330 | rc = eval_predicate(exp, tok_idx, set, options, axis); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8331 | LY_CHECK_RET(rc); |
| 8332 | } |
| 8333 | |
| 8334 | return LY_SUCCESS; |
| 8335 | } |
| 8336 | |
| 8337 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8338 | * @brief Evaluate RelativeLocationPath. Logs directly on error. |
| 8339 | * |
| 8340 | * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step |
| 8341 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8342 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8343 | * |
| 8344 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8345 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8346 | * @param[in] all_desc Whether to search all the descendants or children only. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8347 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8348 | * @param[in] options XPath options. |
| 8349 | * @return LY_ERR (YL_EINCOMPLETE on unresolved when) |
| 8350 | */ |
| 8351 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8352 | 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] | 8353 | uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8354 | { |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8355 | LY_ERR rc = LY_SUCCESS; |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8356 | enum lyxp_axis axis; |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8357 | int scnode_skip_path = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8358 | |
| 8359 | goto step; |
| 8360 | do { |
| 8361 | /* evaluate '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8362 | if (exp->tok_len[*tok_idx] == 1) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8363 | all_desc = 0; |
| 8364 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8365 | assert(exp->tok_len[*tok_idx] == 2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8366 | all_desc = 1; |
| 8367 | } |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8368 | 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] | 8369 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8370 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8371 | |
| 8372 | step: |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8373 | /* AxisSpecifier */ |
| 8374 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_AXISNAME) { |
| 8375 | 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] | 8376 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8377 | 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] | 8378 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 8379 | ++(*tok_idx); |
| 8380 | |
| 8381 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_DCOLON); |
| 8382 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
| 8383 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 8384 | ++(*tok_idx); |
| 8385 | } else if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) { |
| 8386 | axis = LYXP_AXIS_ATTRIBUTE; |
| 8387 | |
| 8388 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
| 8389 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8390 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8391 | } else { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8392 | /* default */ |
| 8393 | axis = LYXP_AXIS_CHILD; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8394 | } |
| 8395 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8396 | /* NodeTest Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8397 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8398 | case LYXP_TOKEN_DOT: |
| 8399 | /* evaluate '.' */ |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8400 | if (!(options & LYXP_SKIP_EXPR)) { |
| 8401 | if (((options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_SCNODE_SET)) || |
| 8402 | (!(options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_NODE_SET))) { |
| 8403 | LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 8404 | rc = LY_EVALID; |
| 8405 | goto cleanup; |
| 8406 | } |
| 8407 | |
| 8408 | if (all_desc) { |
| 8409 | rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options); |
| 8410 | LY_CHECK_GOTO(rc, cleanup); |
| 8411 | } |
| 8412 | rc = xpath_pi_node(set, LYXP_AXIS_SELF, options); |
| 8413 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8414 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8415 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8416 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8417 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8418 | break; |
| 8419 | |
| 8420 | case LYXP_TOKEN_DDOT: |
| 8421 | /* evaluate '..' */ |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8422 | if (!(options & LYXP_SKIP_EXPR)) { |
| 8423 | if (((options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_SCNODE_SET)) || |
| 8424 | (!(options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_NODE_SET))) { |
| 8425 | LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 8426 | rc = LY_EVALID; |
| 8427 | goto cleanup; |
| 8428 | } |
| 8429 | |
| 8430 | if (all_desc) { |
| 8431 | rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options); |
| 8432 | LY_CHECK_GOTO(rc, cleanup); |
| 8433 | } |
| 8434 | rc = xpath_pi_node(set, LYXP_AXIS_PARENT, options); |
| 8435 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8436 | } |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8437 | 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] | 8438 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8439 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8440 | break; |
| 8441 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8442 | case LYXP_TOKEN_NAMETEST: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8443 | /* evaluate NameTest Predicate* */ |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8444 | 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] | 8445 | if (rc == LY_ENOT) { |
| 8446 | assert(options & LYXP_SCNODE_ALL); |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8447 | rc = LY_SUCCESS; |
Michal Vasko | b7ba2c9 | 2024-01-05 15:17:53 +0100 | [diff] [blame] | 8448 | |
| 8449 | /* skip the rest of this path */ |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8450 | scnode_skip_path = 1; |
| 8451 | options |= LYXP_SKIP_EXPR; |
| 8452 | } |
| 8453 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8454 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8455 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8456 | case LYXP_TOKEN_NODETYPE: |
| 8457 | /* evaluate NodeType Predicate* */ |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8458 | 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] | 8459 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8460 | break; |
| 8461 | |
| 8462 | default: |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8463 | LOGINT(set->ctx); |
| 8464 | rc = LY_EINT; |
| 8465 | goto cleanup; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8466 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8467 | } 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] | 8468 | |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 8469 | cleanup: |
| 8470 | if (scnode_skip_path) { |
| 8471 | options &= ~LYXP_SKIP_EXPR; |
| 8472 | } |
| 8473 | return rc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8474 | } |
| 8475 | |
| 8476 | /** |
| 8477 | * @brief Evaluate AbsoluteLocationPath. Logs directly on error. |
| 8478 | * |
| 8479 | * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath |
| 8480 | * |
| 8481 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8482 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8483 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8484 | * @param[in] options XPath options. |
| 8485 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8486 | */ |
| 8487 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8488 | 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] | 8489 | { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 8490 | ly_bool all_desc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8491 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8492 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8493 | /* no matter what tokens follow, we need to be at the root */ |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 8494 | LY_CHECK_RET(moveto_root(set, options)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8495 | } |
| 8496 | |
| 8497 | /* '/' RelativeLocationPath? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8498 | if (exp->tok_len[*tok_idx] == 1) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8499 | /* evaluate '/' - deferred */ |
| 8500 | all_desc = 0; |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8501 | 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] | 8502 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8503 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8504 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8505 | if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8506 | return LY_SUCCESS; |
| 8507 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8508 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8509 | case LYXP_TOKEN_DOT: |
| 8510 | case LYXP_TOKEN_DDOT: |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8511 | case LYXP_TOKEN_AXISNAME: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8512 | case LYXP_TOKEN_AT: |
| 8513 | case LYXP_TOKEN_NAMETEST: |
| 8514 | case LYXP_TOKEN_NODETYPE: |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 8515 | 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] | 8516 | break; |
| 8517 | default: |
| 8518 | break; |
| 8519 | } |
| 8520 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8521 | } else { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 8522 | /* '//' RelativeLocationPath */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8523 | /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */ |
| 8524 | all_desc = 1; |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8525 | 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] | 8526 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8527 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8528 | |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 8529 | 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] | 8530 | } |
| 8531 | |
| 8532 | return LY_SUCCESS; |
| 8533 | } |
| 8534 | |
| 8535 | /** |
| 8536 | * @brief Evaluate FunctionCall. Logs directly on error. |
| 8537 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8538 | * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8539 | * |
| 8540 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8541 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8542 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8543 | * @param[in] options XPath options. |
| 8544 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8545 | */ |
| 8546 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8547 | 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] | 8548 | { |
| 8549 | LY_ERR rc; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 8550 | |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8551 | LY_ERR (*xpath_func)(struct lyxp_set **, uint32_t, struct lyxp_set *, uint32_t) = NULL; |
| 8552 | uint32_t arg_count = 0, i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8553 | struct lyxp_set **args = NULL, **args_aux; |
| 8554 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8555 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8556 | /* FunctionName */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8557 | switch (exp->tok_len[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8558 | case 3: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8559 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8560 | xpath_func = &xpath_not; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8561 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8562 | xpath_func = &xpath_sum; |
| 8563 | } |
| 8564 | break; |
| 8565 | case 4: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8566 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8567 | xpath_func = &xpath_lang; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8568 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8569 | xpath_func = &xpath_last; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8570 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8571 | xpath_func = &xpath_name; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8572 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8573 | xpath_func = &xpath_true; |
| 8574 | } |
| 8575 | break; |
| 8576 | case 5: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8577 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8578 | xpath_func = &xpath_count; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8579 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8580 | xpath_func = &xpath_false; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8581 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8582 | xpath_func = &xpath_floor; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8583 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8584 | xpath_func = &xpath_round; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8585 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8586 | xpath_func = &xpath_deref; |
| 8587 | } |
| 8588 | break; |
| 8589 | case 6: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8590 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8591 | xpath_func = &xpath_concat; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8592 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8593 | xpath_func = &xpath_number; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8594 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8595 | xpath_func = &xpath_string; |
| 8596 | } |
| 8597 | break; |
| 8598 | case 7: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8599 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8600 | xpath_func = &xpath_boolean; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8601 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8602 | xpath_func = &xpath_ceiling; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8603 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8604 | xpath_func = &xpath_current; |
| 8605 | } |
| 8606 | break; |
| 8607 | case 8: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8608 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8609 | xpath_func = &xpath_contains; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8610 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8611 | xpath_func = &xpath_position; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8612 | } 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] | 8613 | xpath_func = &xpath_re_match; |
| 8614 | } |
| 8615 | break; |
| 8616 | case 9: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8617 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8618 | xpath_func = &xpath_substring; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8619 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8620 | xpath_func = &xpath_translate; |
| 8621 | } |
| 8622 | break; |
| 8623 | case 10: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8624 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8625 | xpath_func = &xpath_local_name; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8626 | } 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] | 8627 | xpath_func = &xpath_enum_value; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8628 | } 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] | 8629 | xpath_func = &xpath_bit_is_set; |
| 8630 | } |
| 8631 | break; |
| 8632 | case 11: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8633 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8634 | xpath_func = &xpath_starts_with; |
| 8635 | } |
| 8636 | break; |
| 8637 | case 12: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8638 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8639 | xpath_func = &xpath_derived_from; |
| 8640 | } |
| 8641 | break; |
| 8642 | case 13: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8643 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8644 | xpath_func = &xpath_namespace_uri; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8645 | } 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] | 8646 | xpath_func = &xpath_string_length; |
| 8647 | } |
| 8648 | break; |
| 8649 | case 15: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8650 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8651 | xpath_func = &xpath_normalize_space; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8652 | } 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] | 8653 | xpath_func = &xpath_substring_after; |
| 8654 | } |
| 8655 | break; |
| 8656 | case 16: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8657 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8658 | xpath_func = &xpath_substring_before; |
| 8659 | } |
| 8660 | break; |
| 8661 | case 20: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8662 | 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] | 8663 | xpath_func = &xpath_derived_from_or_self; |
| 8664 | } |
| 8665 | break; |
| 8666 | } |
| 8667 | |
| 8668 | if (!xpath_func) { |
Michal Vasko | 21eaa39 | 2024-02-20 15:48:42 +0100 | [diff] [blame] | 8669 | LOGVAL(set->ctx, LY_VCODE_XP_INFUNC, (int)exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8670 | return LY_EVALID; |
| 8671 | } |
| 8672 | } |
| 8673 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8674 | 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] | 8675 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8676 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8677 | |
| 8678 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8679 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8680 | 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] | 8681 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8682 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8683 | |
| 8684 | /* ( Expr ( ',' Expr )* )? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8685 | if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8686 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8687 | args = malloc(sizeof *args); |
| 8688 | LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup); |
| 8689 | arg_count = 1; |
| 8690 | args[0] = set_copy(set); |
| 8691 | if (!args[0]) { |
| 8692 | rc = LY_EMEM; |
| 8693 | goto cleanup; |
| 8694 | } |
| 8695 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8696 | rc = eval_expr_select(exp, tok_idx, 0, args[0], options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8697 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | b7ba2c9 | 2024-01-05 15:17:53 +0100 | [diff] [blame] | 8698 | set->not_found = args[0]->not_found; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8699 | } else { |
Michal Vasko | b7ba2c9 | 2024-01-05 15:17:53 +0100 | [diff] [blame] | 8700 | rc = eval_expr_select(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8701 | LY_CHECK_GOTO(rc, cleanup); |
| 8702 | } |
| 8703 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8704 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8705 | 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] | 8706 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8707 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8708 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8709 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8710 | ++arg_count; |
| 8711 | args_aux = realloc(args, arg_count * sizeof *args); |
| 8712 | LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup); |
| 8713 | args = args_aux; |
| 8714 | args[arg_count - 1] = set_copy(set); |
| 8715 | if (!args[arg_count - 1]) { |
| 8716 | rc = LY_EMEM; |
| 8717 | goto cleanup; |
| 8718 | } |
| 8719 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8720 | 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] | 8721 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | b7ba2c9 | 2024-01-05 15:17:53 +0100 | [diff] [blame] | 8722 | if (args[arg_count - 1]->not_found) { |
| 8723 | set->not_found = 1; |
| 8724 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8725 | } else { |
Michal Vasko | b7ba2c9 | 2024-01-05 15:17:53 +0100 | [diff] [blame] | 8726 | rc = eval_expr_select(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8727 | LY_CHECK_GOTO(rc, cleanup); |
| 8728 | } |
| 8729 | } |
| 8730 | |
| 8731 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8732 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8733 | 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] | 8734 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8735 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8736 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8737 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8738 | /* evaluate function */ |
| 8739 | rc = xpath_func(args, arg_count, set, options); |
| 8740 | |
| 8741 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8742 | /* merge all nodes from arg evaluations */ |
| 8743 | for (i = 0; i < arg_count; ++i) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 8744 | set_scnode_clear_ctx(args[i], LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 8745 | lyxp_set_scnode_merge(set, args[i]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8746 | } |
| 8747 | } |
| 8748 | } else { |
| 8749 | rc = LY_SUCCESS; |
| 8750 | } |
| 8751 | |
| 8752 | cleanup: |
| 8753 | for (i = 0; i < arg_count; ++i) { |
| 8754 | lyxp_set_free(args[i]); |
| 8755 | } |
| 8756 | free(args); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8757 | return rc; |
| 8758 | } |
| 8759 | |
| 8760 | /** |
| 8761 | * @brief Evaluate Number. Logs directly on error. |
| 8762 | * |
| 8763 | * @param[in] ctx Context for errors. |
| 8764 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8765 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8766 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8767 | * @return LY_ERR |
| 8768 | */ |
| 8769 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8770 | 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] | 8771 | { |
| 8772 | long double num; |
| 8773 | char *endptr; |
| 8774 | |
| 8775 | if (set) { |
| 8776 | errno = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8777 | num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8778 | if (errno) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 8779 | LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]); |
| 8780 | LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double (%s).", |
Michal Vasko | 21eaa39 | 2024-02-20 15:48:42 +0100 | [diff] [blame] | 8781 | (int)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] | 8782 | return LY_EVALID; |
Michal Vasko | 21eaa39 | 2024-02-20 15:48:42 +0100 | [diff] [blame] | 8783 | } else if ((uint32_t)(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] | 8784 | LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]); |
| 8785 | LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double.", |
Michal Vasko | 21eaa39 | 2024-02-20 15:48:42 +0100 | [diff] [blame] | 8786 | (int)exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8787 | return LY_EVALID; |
| 8788 | } |
| 8789 | |
| 8790 | set_fill_number(set, num); |
| 8791 | } |
| 8792 | |
| 8793 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8794 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8795 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8796 | return LY_SUCCESS; |
| 8797 | } |
| 8798 | |
aPiecek | df23eee | 2021-10-07 12:21:50 +0200 | [diff] [blame] | 8799 | LY_ERR |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 8800 | lyxp_vars_find(const struct ly_ctx *ctx, const struct lyxp_var *vars, const char *name, size_t name_len, |
| 8801 | struct lyxp_var **var) |
aPiecek | df23eee | 2021-10-07 12:21:50 +0200 | [diff] [blame] | 8802 | { |
aPiecek | df23eee | 2021-10-07 12:21:50 +0200 | [diff] [blame] | 8803 | LY_ARRAY_COUNT_TYPE u; |
| 8804 | |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 8805 | assert(name); |
aPiecek | df23eee | 2021-10-07 12:21:50 +0200 | [diff] [blame] | 8806 | |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 8807 | if (!name_len) { |
| 8808 | name_len = strlen(name); |
| 8809 | } |
aPiecek | df23eee | 2021-10-07 12:21:50 +0200 | [diff] [blame] | 8810 | |
| 8811 | LY_ARRAY_FOR(vars, u) { |
| 8812 | if (!strncmp(vars[u].name, name, name_len)) { |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 8813 | if (var) { |
| 8814 | *var = (struct lyxp_var *)&vars[u]; |
| 8815 | } |
| 8816 | return LY_SUCCESS; |
aPiecek | df23eee | 2021-10-07 12:21:50 +0200 | [diff] [blame] | 8817 | } |
| 8818 | } |
| 8819 | |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 8820 | if (ctx) { |
| 8821 | LOGERR(ctx, LY_ENOTFOUND, "Variable \"%.*s\" not defined.", (int)name_len, name); |
aPiecek | df23eee | 2021-10-07 12:21:50 +0200 | [diff] [blame] | 8822 | } |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 8823 | return LY_ENOTFOUND; |
aPiecek | df23eee | 2021-10-07 12:21:50 +0200 | [diff] [blame] | 8824 | } |
| 8825 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8826 | /** |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8827 | * @brief Evaluate VariableReference. |
| 8828 | * |
| 8829 | * @param[in] exp Parsed XPath expression. |
| 8830 | * @param[in] tok_idx Position in the expression @p exp. |
| 8831 | * @param[in] vars [Sized array](@ref sizedarrays) of XPath variables. |
| 8832 | * @param[in,out] set Context and result set. |
| 8833 | * @param[in] options XPath options. |
| 8834 | * @return LY_ERR value. |
| 8835 | */ |
| 8836 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8837 | 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] | 8838 | { |
| 8839 | LY_ERR ret; |
| 8840 | const char *name; |
| 8841 | struct lyxp_var *var; |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8842 | struct lyxp_expr *tokens = NULL; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8843 | uint32_t token_index, name_len; |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8844 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8845 | /* find out the name and value of the variable */ |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8846 | name = &exp->expr[exp->tok_pos[*tok_idx]]; |
Michal Vasko | e68b540 | 2022-07-29 14:58:15 +0200 | [diff] [blame] | 8847 | name_len = exp->tok_len[*tok_idx]; |
Michal Vasko | 9018996 | 2023-02-28 12:10:34 +0100 | [diff] [blame] | 8848 | ret = lyxp_vars_find(set->ctx, set->vars, name, name_len, &var); |
| 8849 | LY_CHECK_RET(ret); |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8850 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8851 | /* parse value */ |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8852 | ret = lyxp_expr_parse(set->ctx, var->value, 0, 1, &tokens); |
| 8853 | LY_CHECK_GOTO(ret, cleanup); |
| 8854 | |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8855 | /* evaluate value */ |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8856 | token_index = 0; |
| 8857 | ret = eval_expr_select(tokens, &token_index, 0, set, options); |
| 8858 | LY_CHECK_GOTO(ret, cleanup); |
| 8859 | |
| 8860 | cleanup: |
| 8861 | lyxp_expr_free(set->ctx, tokens); |
| 8862 | |
| 8863 | return ret; |
| 8864 | } |
| 8865 | |
| 8866 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8867 | * @brief Evaluate PathExpr. Logs directly on error. |
| 8868 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8869 | * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate* |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8870 | * | PrimaryExpr Predicate* '/' RelativeLocationPath |
| 8871 | * | PrimaryExpr Predicate* '//' RelativeLocationPath |
| 8872 | * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8873 | * [10] PrimaryExpr ::= VariableReference | '(' Expr ')' | Literal | Number | FunctionCall |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8874 | * |
| 8875 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8876 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8877 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8878 | * @param[in] options XPath options. |
| 8879 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8880 | */ |
| 8881 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 8882 | 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] | 8883 | { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8884 | ly_bool all_desc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8885 | LY_ERR rc; |
| 8886 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8887 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8888 | case LYXP_TOKEN_PAR1: |
| 8889 | /* '(' Expr ')' */ |
| 8890 | |
| 8891 | /* '(' */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8892 | 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] | 8893 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8894 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8895 | |
| 8896 | /* Expr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8897 | rc = eval_expr_select(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8898 | LY_CHECK_RET(rc); |
| 8899 | |
| 8900 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8901 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8902 | 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] | 8903 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8904 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8905 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8906 | goto predicate; |
| 8907 | |
| 8908 | case LYXP_TOKEN_DOT: |
| 8909 | case LYXP_TOKEN_DDOT: |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8910 | case LYXP_TOKEN_AXISNAME: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8911 | case LYXP_TOKEN_AT: |
| 8912 | case LYXP_TOKEN_NAMETEST: |
| 8913 | case LYXP_TOKEN_NODETYPE: |
| 8914 | /* RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8915 | rc = eval_relative_location_path(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8916 | LY_CHECK_RET(rc); |
| 8917 | break; |
| 8918 | |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8919 | case LYXP_TOKEN_VARREF: |
| 8920 | /* VariableReference */ |
| 8921 | rc = eval_variable_reference(exp, tok_idx, set, options); |
| 8922 | LY_CHECK_RET(rc); |
| 8923 | ++(*tok_idx); |
| 8924 | |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8925 | goto predicate; |
| 8926 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8927 | case LYXP_TOKEN_FUNCNAME: |
| 8928 | /* FunctionCall */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8929 | rc = eval_function_call(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8930 | LY_CHECK_RET(rc); |
| 8931 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8932 | goto predicate; |
| 8933 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 8934 | case LYXP_TOKEN_OPER_PATH: |
| 8935 | case LYXP_TOKEN_OPER_RPATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8936 | /* AbsoluteLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8937 | rc = eval_absolute_location_path(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8938 | LY_CHECK_RET(rc); |
| 8939 | break; |
| 8940 | |
| 8941 | case LYXP_TOKEN_LITERAL: |
| 8942 | /* Literal */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8943 | if ((options & LYXP_SKIP_EXPR) || (options & LYXP_SCNODE_ALL)) { |
| 8944 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 8945 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8946 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8947 | eval_literal(exp, tok_idx, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8948 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8949 | eval_literal(exp, tok_idx, set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8950 | } |
| 8951 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8952 | goto predicate; |
| 8953 | |
| 8954 | case LYXP_TOKEN_NUMBER: |
| 8955 | /* Number */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8956 | if ((options & LYXP_SKIP_EXPR) || (options & LYXP_SCNODE_ALL)) { |
| 8957 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 8958 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8959 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8960 | rc = eval_number(NULL, exp, tok_idx, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8961 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8962 | rc = eval_number(set->ctx, exp, tok_idx, set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8963 | } |
| 8964 | LY_CHECK_RET(rc); |
| 8965 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8966 | goto predicate; |
| 8967 | |
| 8968 | default: |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8969 | 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] | 8970 | return LY_EVALID; |
| 8971 | } |
| 8972 | |
| 8973 | return LY_SUCCESS; |
| 8974 | |
| 8975 | predicate: |
| 8976 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8977 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 8978 | rc = eval_predicate(exp, tok_idx, set, options, LYXP_AXIS_CHILD); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8979 | LY_CHECK_RET(rc); |
| 8980 | } |
| 8981 | |
| 8982 | /* ('/' or '//') RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8983 | 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] | 8984 | |
| 8985 | /* evaluate '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8986 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8987 | all_desc = 0; |
| 8988 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8989 | all_desc = 1; |
| 8990 | } |
| 8991 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8992 | 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] | 8993 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8994 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8995 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8996 | rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8997 | LY_CHECK_RET(rc); |
| 8998 | } |
| 8999 | |
| 9000 | return LY_SUCCESS; |
| 9001 | } |
| 9002 | |
| 9003 | /** |
| 9004 | * @brief Evaluate UnionExpr. Logs directly on error. |
| 9005 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9006 | * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9007 | * |
| 9008 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9009 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9010 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9011 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9012 | * @param[in] options XPath options. |
| 9013 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 9014 | */ |
| 9015 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9016 | 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] | 9017 | { |
| 9018 | LY_ERR rc = LY_SUCCESS; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9019 | uint32_t i; |
Michal Vasko | b7ba2c9 | 2024-01-05 15:17:53 +0100 | [diff] [blame] | 9020 | struct lyxp_set orig_set, set2; |
| 9021 | ly_bool found = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9022 | |
| 9023 | assert(repeat); |
| 9024 | |
| 9025 | set_init(&orig_set, set); |
| 9026 | set_init(&set2, set); |
| 9027 | |
| 9028 | set_fill_set(&orig_set, set); |
| 9029 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9030 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9031 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | b7ba2c9 | 2024-01-05 15:17:53 +0100 | [diff] [blame] | 9032 | if (set->not_found) { |
| 9033 | set->not_found = 0; |
| 9034 | } else { |
| 9035 | found = 1; |
| 9036 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9037 | |
| 9038 | /* ('|' PathExpr)* */ |
| 9039 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9040 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9041 | 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] | 9042 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9043 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9044 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9045 | if (options & LYXP_SKIP_EXPR) { |
| 9046 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9047 | LY_CHECK_GOTO(rc, cleanup); |
| 9048 | continue; |
| 9049 | } |
| 9050 | |
| 9051 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9052 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9053 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | b7ba2c9 | 2024-01-05 15:17:53 +0100 | [diff] [blame] | 9054 | if (!set2.not_found) { |
| 9055 | found = 1; |
| 9056 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9057 | |
| 9058 | /* eval */ |
| 9059 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 9060 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9061 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9062 | rc = moveto_union(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9063 | LY_CHECK_GOTO(rc, cleanup); |
| 9064 | } |
| 9065 | } |
| 9066 | |
| 9067 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9068 | lyxp_set_free_content(&orig_set); |
| 9069 | lyxp_set_free_content(&set2); |
Michal Vasko | b7ba2c9 | 2024-01-05 15:17:53 +0100 | [diff] [blame] | 9070 | if (!found) { |
| 9071 | set->not_found = 1; |
| 9072 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9073 | return rc; |
| 9074 | } |
| 9075 | |
| 9076 | /** |
| 9077 | * @brief Evaluate UnaryExpr. Logs directly on error. |
| 9078 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9079 | * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9080 | * |
| 9081 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9082 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9083 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9084 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9085 | * @param[in] options XPath options. |
| 9086 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 9087 | */ |
| 9088 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9089 | 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] | 9090 | { |
| 9091 | LY_ERR rc; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9092 | uint32_t this_op, i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9093 | |
| 9094 | assert(repeat); |
| 9095 | |
| 9096 | /* ('-')+ */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9097 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9098 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9099 | 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] | 9100 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9101 | 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] | 9102 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9103 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9104 | } |
| 9105 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9106 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9107 | LY_CHECK_RET(rc); |
| 9108 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9109 | if (!(options & LYXP_SKIP_EXPR) && (repeat % 2)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9110 | if (options & LYXP_SCNODE_ALL) { |
| 9111 | warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]); |
| 9112 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9113 | 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] | 9114 | LY_CHECK_RET(rc); |
| 9115 | } |
| 9116 | } |
| 9117 | |
| 9118 | return LY_SUCCESS; |
| 9119 | } |
| 9120 | |
| 9121 | /** |
| 9122 | * @brief Evaluate MultiplicativeExpr. Logs directly on error. |
| 9123 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9124 | * [18] MultiplicativeExpr ::= UnaryExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9125 | * | MultiplicativeExpr '*' UnaryExpr |
| 9126 | * | MultiplicativeExpr 'div' UnaryExpr |
| 9127 | * | MultiplicativeExpr 'mod' UnaryExpr |
| 9128 | * |
| 9129 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9130 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9131 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9132 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9133 | * @param[in] options XPath options. |
| 9134 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 9135 | */ |
| 9136 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9137 | 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] | 9138 | uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9139 | { |
Michal Vasko | b7ba2c9 | 2024-01-05 15:17:53 +0100 | [diff] [blame] | 9140 | LY_ERR rc = LY_SUCCESS; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9141 | uint32_t i, this_op; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9142 | struct lyxp_set orig_set, set2; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9143 | |
| 9144 | assert(repeat); |
| 9145 | |
| 9146 | set_init(&orig_set, set); |
| 9147 | set_init(&set2, set); |
| 9148 | |
| 9149 | set_fill_set(&orig_set, set); |
| 9150 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9151 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9152 | LY_CHECK_GOTO(rc, cleanup); |
| 9153 | |
| 9154 | /* ('*' / 'div' / 'mod' UnaryExpr)* */ |
| 9155 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9156 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9157 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9158 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9159 | 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] | 9160 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9161 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9162 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9163 | if (options & LYXP_SKIP_EXPR) { |
| 9164 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9165 | LY_CHECK_GOTO(rc, cleanup); |
| 9166 | continue; |
| 9167 | } |
| 9168 | |
| 9169 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9170 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9171 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | b7ba2c9 | 2024-01-05 15:17:53 +0100 | [diff] [blame] | 9172 | if (set2.not_found) { |
| 9173 | set->not_found = 1; |
| 9174 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9175 | |
| 9176 | /* eval */ |
| 9177 | if (options & LYXP_SCNODE_ALL) { |
| 9178 | 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] | 9179 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 9180 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9181 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9182 | 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] | 9183 | LY_CHECK_GOTO(rc, cleanup); |
| 9184 | } |
| 9185 | } |
| 9186 | |
| 9187 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9188 | lyxp_set_free_content(&orig_set); |
| 9189 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9190 | return rc; |
| 9191 | } |
| 9192 | |
| 9193 | /** |
| 9194 | * @brief Evaluate AdditiveExpr. Logs directly on error. |
| 9195 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9196 | * [17] AdditiveExpr ::= MultiplicativeExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9197 | * | AdditiveExpr '+' MultiplicativeExpr |
| 9198 | * | AdditiveExpr '-' MultiplicativeExpr |
| 9199 | * |
| 9200 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9201 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9202 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9203 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9204 | * @param[in] options XPath options. |
| 9205 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 9206 | */ |
| 9207 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9208 | 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] | 9209 | { |
Michal Vasko | b7ba2c9 | 2024-01-05 15:17:53 +0100 | [diff] [blame] | 9210 | LY_ERR rc = LY_SUCCESS; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9211 | uint32_t i, this_op; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9212 | struct lyxp_set orig_set, set2; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9213 | |
| 9214 | assert(repeat); |
| 9215 | |
| 9216 | set_init(&orig_set, set); |
| 9217 | set_init(&set2, set); |
| 9218 | |
| 9219 | set_fill_set(&orig_set, set); |
| 9220 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9221 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9222 | LY_CHECK_GOTO(rc, cleanup); |
| 9223 | |
| 9224 | /* ('+' / '-' MultiplicativeExpr)* */ |
| 9225 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9226 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9227 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9228 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9229 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 49fec8e | 2022-05-24 10:28:33 +0200 | [diff] [blame] | 9230 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9231 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9232 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9233 | if (options & LYXP_SKIP_EXPR) { |
| 9234 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9235 | LY_CHECK_GOTO(rc, cleanup); |
| 9236 | continue; |
| 9237 | } |
| 9238 | |
| 9239 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9240 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9241 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | b7ba2c9 | 2024-01-05 15:17:53 +0100 | [diff] [blame] | 9242 | if (set2.not_found) { |
| 9243 | set->not_found = 1; |
| 9244 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9245 | |
| 9246 | /* eval */ |
| 9247 | if (options & LYXP_SCNODE_ALL) { |
| 9248 | 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] | 9249 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 9250 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9251 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9252 | 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] | 9253 | LY_CHECK_GOTO(rc, cleanup); |
| 9254 | } |
| 9255 | } |
| 9256 | |
| 9257 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9258 | lyxp_set_free_content(&orig_set); |
| 9259 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9260 | return rc; |
| 9261 | } |
| 9262 | |
| 9263 | /** |
| 9264 | * @brief Evaluate RelationalExpr. Logs directly on error. |
| 9265 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9266 | * [16] RelationalExpr ::= AdditiveExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9267 | * | RelationalExpr '<' AdditiveExpr |
| 9268 | * | RelationalExpr '>' AdditiveExpr |
| 9269 | * | RelationalExpr '<=' AdditiveExpr |
| 9270 | * | RelationalExpr '>=' AdditiveExpr |
| 9271 | * |
| 9272 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9273 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9274 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9275 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9276 | * @param[in] options XPath options. |
| 9277 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 9278 | */ |
| 9279 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9280 | 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] | 9281 | { |
Michal Vasko | b7ba2c9 | 2024-01-05 15:17:53 +0100 | [diff] [blame] | 9282 | LY_ERR rc = LY_SUCCESS; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9283 | uint32_t i, this_op; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9284 | struct lyxp_set orig_set, set2; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9285 | |
| 9286 | assert(repeat); |
| 9287 | |
| 9288 | set_init(&orig_set, set); |
| 9289 | set_init(&set2, set); |
| 9290 | |
| 9291 | set_fill_set(&orig_set, set); |
| 9292 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9293 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9294 | LY_CHECK_GOTO(rc, cleanup); |
| 9295 | |
| 9296 | /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */ |
| 9297 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9298 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9299 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9300 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9301 | 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] | 9302 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9303 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9304 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9305 | if (options & LYXP_SKIP_EXPR) { |
| 9306 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9307 | LY_CHECK_GOTO(rc, cleanup); |
| 9308 | continue; |
| 9309 | } |
| 9310 | |
| 9311 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9312 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9313 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | b7ba2c9 | 2024-01-05 15:17:53 +0100 | [diff] [blame] | 9314 | if (set2.not_found) { |
| 9315 | set->not_found = 1; |
| 9316 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9317 | |
| 9318 | /* eval */ |
| 9319 | if (options & LYXP_SCNODE_ALL) { |
| 9320 | 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] | 9321 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 9322 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9323 | } else { |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 9324 | ly_bool result; |
| 9325 | |
| 9326 | 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] | 9327 | LY_CHECK_GOTO(rc, cleanup); |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 9328 | set_fill_boolean(set, result); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9329 | } |
| 9330 | } |
| 9331 | |
| 9332 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9333 | lyxp_set_free_content(&orig_set); |
| 9334 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9335 | return rc; |
| 9336 | } |
| 9337 | |
| 9338 | /** |
| 9339 | * @brief Evaluate EqualityExpr. Logs directly on error. |
| 9340 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9341 | * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9342 | * | EqualityExpr '!=' RelationalExpr |
| 9343 | * |
| 9344 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9345 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9346 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9347 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9348 | * @param[in] options XPath options. |
| 9349 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 9350 | */ |
| 9351 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9352 | 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] | 9353 | { |
Michal Vasko | b7ba2c9 | 2024-01-05 15:17:53 +0100 | [diff] [blame] | 9354 | LY_ERR rc = LY_SUCCESS; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9355 | uint32_t i, this_op; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9356 | struct lyxp_set orig_set, set2; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9357 | |
| 9358 | assert(repeat); |
| 9359 | |
| 9360 | set_init(&orig_set, set); |
| 9361 | set_init(&set2, set); |
| 9362 | |
| 9363 | set_fill_set(&orig_set, set); |
| 9364 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9365 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9366 | LY_CHECK_GOTO(rc, cleanup); |
| 9367 | |
| 9368 | /* ('=' / '!=' RelationalExpr)* */ |
| 9369 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9370 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9371 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9372 | 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] | 9373 | 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] | 9374 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9375 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9376 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9377 | if (options & LYXP_SKIP_EXPR) { |
| 9378 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9379 | LY_CHECK_GOTO(rc, cleanup); |
| 9380 | continue; |
| 9381 | } |
| 9382 | |
| 9383 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9384 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9385 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | b7ba2c9 | 2024-01-05 15:17:53 +0100 | [diff] [blame] | 9386 | if (set2.not_found) { |
| 9387 | set->not_found = 1; |
| 9388 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9389 | |
| 9390 | /* eval */ |
| 9391 | if (options & LYXP_SCNODE_ALL) { |
| 9392 | 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] | 9393 | warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1); |
| 9394 | 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] | 9395 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 9396 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9397 | } else { |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 9398 | ly_bool result; |
| 9399 | |
| 9400 | 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] | 9401 | LY_CHECK_GOTO(rc, cleanup); |
Haithem Ben Ghorbal | 1ac219d | 2022-09-07 12:30:06 +0200 | [diff] [blame] | 9402 | set_fill_boolean(set, result); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9403 | } |
| 9404 | } |
| 9405 | |
| 9406 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9407 | lyxp_set_free_content(&orig_set); |
| 9408 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9409 | return rc; |
| 9410 | } |
| 9411 | |
| 9412 | /** |
| 9413 | * @brief Evaluate AndExpr. Logs directly on error. |
| 9414 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9415 | * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9416 | * |
| 9417 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9418 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9419 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9420 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9421 | * @param[in] options XPath options. |
| 9422 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 9423 | */ |
| 9424 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9425 | 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] | 9426 | { |
Michal Vasko | b7ba2c9 | 2024-01-05 15:17:53 +0100 | [diff] [blame] | 9427 | LY_ERR rc = LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9428 | struct lyxp_set orig_set, set2; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9429 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9430 | |
| 9431 | assert(repeat); |
| 9432 | |
| 9433 | set_init(&orig_set, set); |
| 9434 | set_init(&set2, set); |
| 9435 | |
| 9436 | set_fill_set(&orig_set, set); |
| 9437 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9438 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9439 | LY_CHECK_GOTO(rc, cleanup); |
| 9440 | |
Michal Vasko | 3ff82dd | 2023-05-09 14:09:21 +0200 | [diff] [blame] | 9441 | if (!(options & LYXP_SKIP_EXPR)) { |
| 9442 | if (options & LYXP_SCNODE_ALL) { |
| 9443 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
| 9444 | } else { |
| 9445 | /* cast to boolean, we know that will be the final result */ |
| 9446 | lyxp_set_cast(set, LYXP_SET_BOOLEAN); |
| 9447 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9448 | } |
| 9449 | |
| 9450 | /* ('and' EqualityExpr)* */ |
| 9451 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9452 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9453 | 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] | 9454 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9455 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9456 | |
| 9457 | /* lazy evaluation */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9458 | if ((options & LYXP_SKIP_EXPR) || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) { |
| 9459 | 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] | 9460 | LY_CHECK_GOTO(rc, cleanup); |
| 9461 | continue; |
| 9462 | } |
| 9463 | |
| 9464 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9465 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9466 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | b7ba2c9 | 2024-01-05 15:17:53 +0100 | [diff] [blame] | 9467 | if (set2.not_found) { |
| 9468 | set->not_found = 1; |
| 9469 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9470 | |
| 9471 | /* eval - just get boolean value actually */ |
| 9472 | if (set->type == LYXP_SET_SCNODE_SET) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 9473 | set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 9474 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9475 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9476 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9477 | set_fill_set(set, &set2); |
| 9478 | } |
| 9479 | } |
| 9480 | |
| 9481 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9482 | lyxp_set_free_content(&orig_set); |
| 9483 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9484 | return rc; |
| 9485 | } |
| 9486 | |
| 9487 | /** |
| 9488 | * @brief Evaluate OrExpr. Logs directly on error. |
| 9489 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9490 | * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9491 | * |
| 9492 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9493 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9494 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9495 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9496 | * @param[in] options XPath options. |
| 9497 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 9498 | */ |
| 9499 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9500 | 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] | 9501 | { |
Michal Vasko | b7ba2c9 | 2024-01-05 15:17:53 +0100 | [diff] [blame] | 9502 | LY_ERR rc = LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9503 | struct lyxp_set orig_set, set2; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9504 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9505 | |
| 9506 | assert(repeat); |
| 9507 | |
| 9508 | set_init(&orig_set, set); |
| 9509 | set_init(&set2, set); |
| 9510 | |
| 9511 | set_fill_set(&orig_set, set); |
| 9512 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9513 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9514 | LY_CHECK_GOTO(rc, cleanup); |
| 9515 | |
Michal Vasko | 3ff82dd | 2023-05-09 14:09:21 +0200 | [diff] [blame] | 9516 | if (!(options & LYXP_SKIP_EXPR)) { |
| 9517 | if (options & LYXP_SCNODE_ALL) { |
| 9518 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
| 9519 | } else { |
| 9520 | /* cast to boolean, we know that will be the final result */ |
| 9521 | lyxp_set_cast(set, LYXP_SET_BOOLEAN); |
| 9522 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9523 | } |
| 9524 | |
| 9525 | /* ('or' AndExpr)* */ |
| 9526 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9527 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9528 | 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] | 9529 | lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9530 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9531 | |
| 9532 | /* lazy evaluation */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9533 | if ((options & LYXP_SKIP_EXPR) || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) { |
| 9534 | 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] | 9535 | LY_CHECK_GOTO(rc, cleanup); |
| 9536 | continue; |
| 9537 | } |
| 9538 | |
| 9539 | set_fill_set(&set2, &orig_set); |
| 9540 | /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one), |
| 9541 | * but it does not matter */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9542 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9543 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | b7ba2c9 | 2024-01-05 15:17:53 +0100 | [diff] [blame] | 9544 | if (set2.not_found) { |
| 9545 | set->not_found = 1; |
| 9546 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9547 | |
| 9548 | /* eval - just get boolean value actually */ |
| 9549 | if (set->type == LYXP_SET_SCNODE_SET) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 9550 | set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 9551 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9552 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9553 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9554 | set_fill_set(set, &set2); |
| 9555 | } |
| 9556 | } |
| 9557 | |
| 9558 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9559 | lyxp_set_free_content(&orig_set); |
| 9560 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9561 | return rc; |
| 9562 | } |
| 9563 | |
| 9564 | /** |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9565 | * @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] | 9566 | * |
| 9567 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9568 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9569 | * @param[in] etype Expression type to evaluate. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 9570 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9571 | * @param[in] options XPath options. |
| 9572 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 9573 | */ |
| 9574 | static LY_ERR |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9575 | 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] | 9576 | uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9577 | { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9578 | uint32_t i, count; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9579 | enum lyxp_expr_type next_etype; |
| 9580 | LY_ERR rc; |
| 9581 | |
| 9582 | /* process operator repeats */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9583 | if (!exp->repeat[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9584 | next_etype = LYXP_EXPR_NONE; |
| 9585 | } else { |
| 9586 | /* find etype repeat */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 9587 | for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i) {} |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9588 | |
| 9589 | /* select one-priority lower because etype expression called us */ |
| 9590 | if (i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9591 | next_etype = exp->repeat[*tok_idx][i - 1]; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9592 | /* count repeats for that expression */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 9593 | 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] | 9594 | } else { |
| 9595 | next_etype = LYXP_EXPR_NONE; |
| 9596 | } |
| 9597 | } |
| 9598 | |
| 9599 | /* decide what expression are we parsing based on the repeat */ |
| 9600 | switch (next_etype) { |
| 9601 | case LYXP_EXPR_OR: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9602 | rc = eval_or_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9603 | break; |
| 9604 | case LYXP_EXPR_AND: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9605 | rc = eval_and_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9606 | break; |
| 9607 | case LYXP_EXPR_EQUALITY: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9608 | rc = eval_equality_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9609 | break; |
| 9610 | case LYXP_EXPR_RELATIONAL: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9611 | rc = eval_relational_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9612 | break; |
| 9613 | case LYXP_EXPR_ADDITIVE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9614 | rc = eval_additive_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9615 | break; |
| 9616 | case LYXP_EXPR_MULTIPLICATIVE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9617 | rc = eval_multiplicative_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9618 | break; |
| 9619 | case LYXP_EXPR_UNARY: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9620 | rc = eval_unary_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9621 | break; |
| 9622 | case LYXP_EXPR_UNION: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9623 | rc = eval_union_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9624 | break; |
| 9625 | case LYXP_EXPR_NONE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9626 | rc = eval_path_expr(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9627 | break; |
| 9628 | default: |
| 9629 | LOGINT_RET(set->ctx); |
| 9630 | } |
| 9631 | |
| 9632 | return rc; |
| 9633 | } |
| 9634 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9635 | /** |
| 9636 | * @brief Get root type. |
| 9637 | * |
| 9638 | * @param[in] ctx_node Context node. |
| 9639 | * @param[in] ctx_scnode Schema context node. |
| 9640 | * @param[in] options XPath options. |
| 9641 | * @return Root type. |
| 9642 | */ |
| 9643 | static enum lyxp_node_type |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 9644 | 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] | 9645 | { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 9646 | const struct lysc_node *op; |
| 9647 | |
Michal Vasko | a27245c | 2022-05-02 09:01:35 +0200 | [diff] [blame] | 9648 | /* explicit */ |
| 9649 | if (options & LYXP_ACCESS_TREE_ALL) { |
| 9650 | return LYXP_NODE_ROOT; |
| 9651 | } else if (options & LYXP_ACCESS_TREE_CONFIG) { |
| 9652 | return LYXP_NODE_ROOT_CONFIG; |
| 9653 | } |
| 9654 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9655 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9656 | /* schema */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 9657 | 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] | 9658 | |
Michal Vasko | 5357561 | 2023-12-15 16:09:11 +0100 | [diff] [blame] | 9659 | if (op || !(options & LYXP_SCNODE_SCHEMA)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9660 | /* general root that can access everything */ |
| 9661 | return LYXP_NODE_ROOT; |
| 9662 | } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) { |
| 9663 | /* root context node can access only config data (because we said so, it is unspecified) */ |
| 9664 | return LYXP_NODE_ROOT_CONFIG; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9665 | } |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 9666 | return LYXP_NODE_ROOT; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9667 | } |
| 9668 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9669 | /* data */ |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 9670 | op = ctx_node ? ctx_node->schema : NULL; |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 9671 | 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] | 9672 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9673 | if (op || !(options & LYXP_SCHEMA)) { |
| 9674 | /* general root that can access everything */ |
| 9675 | return LYXP_NODE_ROOT; |
Christian Hopps | b6ecaea | 2021-02-06 09:45:38 -0500 | [diff] [blame] | 9676 | } 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] | 9677 | /* root context node can access only config data (because we said so, it is unspecified) */ |
| 9678 | return LYXP_NODE_ROOT_CONFIG; |
| 9679 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9680 | return LYXP_NODE_ROOT; |
| 9681 | } |
| 9682 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9683 | LY_ERR |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 9684 | 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] | 9685 | LY_VALUE_FORMAT format, void *prefix_data, const struct lyd_node *cur_node, const struct lyd_node *ctx_node, |
| 9686 | 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] | 9687 | { |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9688 | uint32_t tok_idx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9689 | LY_ERR rc; |
| 9690 | |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 9691 | LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL); |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 9692 | if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) { |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 9693 | 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] | 9694 | return LY_EINVAL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9695 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9696 | |
Michal Vasko | d3bb12f | 2020-12-04 14:33:09 +0100 | [diff] [blame] | 9697 | if (tree) { |
| 9698 | /* adjust the pointer to be the first top-level sibling */ |
| 9699 | while (tree->parent) { |
| 9700 | tree = lyd_parent(tree); |
| 9701 | } |
| 9702 | tree = lyd_first_sibling(tree); |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 9703 | |
Michal Vasko | 6f78a77 | 2022-11-10 08:13:52 +0100 | [diff] [blame] | 9704 | if (lysc_data_parent(tree->schema)) { |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 9705 | /* unable to evaluate absolute paths */ |
| 9706 | LOGERR(ctx, LY_EINVAL, "Data node \"%s\" has no parent but is not instance of a top-level schema node.", |
| 9707 | LYD_NAME(tree)); |
| 9708 | return LY_EINVAL; |
| 9709 | } |
Michal Vasko | d3bb12f | 2020-12-04 14:33:09 +0100 | [diff] [blame] | 9710 | } |
| 9711 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9712 | /* prepare set for evaluation */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9713 | memset(set, 0, sizeof *set); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9714 | set->type = LYXP_SET_NODE_SET; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9715 | set->root_type = lyxp_get_root_type(ctx_node, NULL, options); |
| 9716 | set_insert_node(set, (struct lyd_node *)ctx_node, 0, ctx_node ? LYXP_NODE_ELEM : set->root_type, 0); |
| 9717 | |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 9718 | set->ctx = (struct ly_ctx *)ctx; |
Michal Vasko | a3e92bc | 2022-07-29 14:56:23 +0200 | [diff] [blame] | 9719 | set->cur_node = cur_node; |
| 9720 | for (set->context_op = cur_node ? cur_node->schema : NULL; |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 9721 | set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); |
| 9722 | set->context_op = set->context_op->parent) {} |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 9723 | set->tree = tree; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9724 | set->cur_mod = cur_mod; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9725 | set->format = format; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9726 | set->prefix_data = prefix_data; |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 9727 | set->vars = vars; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9728 | |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 9729 | if (set->cur_node) { |
| 9730 | LOG_LOCSET(NULL, set->cur_node); |
| 9731 | } |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 9732 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9733 | /* evaluate */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9734 | rc = eval_expr_select(exp, &tok_idx, 0, set, options); |
Michal Vasko | b7ba2c9 | 2024-01-05 15:17:53 +0100 | [diff] [blame] | 9735 | if (!rc && set->not_found) { |
| 9736 | rc = LY_ENOTFOUND; |
| 9737 | } |
| 9738 | if (rc) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9739 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9740 | } |
| 9741 | |
Michal Vasko | 4a7d4d6 | 2021-12-13 17:05:06 +0100 | [diff] [blame] | 9742 | if (set->cur_node) { |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 9743 | LOG_LOCBACK(0, 1); |
Michal Vasko | 4a7d4d6 | 2021-12-13 17:05:06 +0100 | [diff] [blame] | 9744 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9745 | return rc; |
| 9746 | } |
| 9747 | |
| 9748 | #if 0 |
| 9749 | |
| 9750 | /* full xml printing of set elements, not used currently */ |
| 9751 | |
| 9752 | void |
| 9753 | lyxp_set_print_xml(FILE *f, struct lyxp_set *set) |
| 9754 | { |
| 9755 | uint32_t i; |
| 9756 | char *str_num; |
| 9757 | struct lyout out; |
| 9758 | |
| 9759 | memset(&out, 0, sizeof out); |
| 9760 | |
| 9761 | out.type = LYOUT_STREAM; |
| 9762 | out.method.f = f; |
| 9763 | |
| 9764 | switch (set->type) { |
| 9765 | case LYXP_SET_EMPTY: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9766 | ly_print_(&out, "Empty XPath set\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9767 | break; |
| 9768 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9769 | ly_print_(&out, "Boolean XPath set:\n"); |
| 9770 | ly_print_(&out, "%s\n\n", set->value.bool ? "true" : "false"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9771 | break; |
| 9772 | case LYXP_SET_STRING: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9773 | ly_print_(&out, "String XPath set:\n"); |
| 9774 | ly_print_(&out, "\"%s\"\n\n", set->value.str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9775 | break; |
| 9776 | case LYXP_SET_NUMBER: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9777 | ly_print_(&out, "Number XPath set:\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9778 | |
| 9779 | if (isnan(set->value.num)) { |
| 9780 | str_num = strdup("NaN"); |
| 9781 | } else if ((set->value.num == 0) || (set->value.num == -0.0f)) { |
| 9782 | str_num = strdup("0"); |
| 9783 | } else if (isinf(set->value.num) && !signbit(set->value.num)) { |
| 9784 | str_num = strdup("Infinity"); |
| 9785 | } else if (isinf(set->value.num) && signbit(set->value.num)) { |
| 9786 | str_num = strdup("-Infinity"); |
| 9787 | } else if ((long long)set->value.num == set->value.num) { |
| 9788 | if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) { |
| 9789 | str_num = NULL; |
| 9790 | } |
| 9791 | } else { |
| 9792 | if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) { |
| 9793 | str_num = NULL; |
| 9794 | } |
| 9795 | } |
| 9796 | if (!str_num) { |
| 9797 | LOGMEM; |
| 9798 | return; |
| 9799 | } |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9800 | ly_print_(&out, "%s\n\n", str_num); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9801 | free(str_num); |
| 9802 | break; |
| 9803 | case LYXP_SET_NODE_SET: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9804 | ly_print_(&out, "Node XPath set:\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9805 | |
| 9806 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9807 | ly_print_(&out, "%d. ", i + 1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9808 | switch (set->node_type[i]) { |
| 9809 | case LYXP_NODE_ROOT_ALL: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9810 | ly_print_(&out, "ROOT all\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9811 | break; |
| 9812 | case LYXP_NODE_ROOT_CONFIG: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9813 | ly_print_(&out, "ROOT config\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9814 | break; |
| 9815 | case LYXP_NODE_ROOT_STATE: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9816 | ly_print_(&out, "ROOT state\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9817 | break; |
| 9818 | case LYXP_NODE_ROOT_NOTIF: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9819 | 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] | 9820 | break; |
| 9821 | case LYXP_NODE_ROOT_RPC: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9822 | 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] | 9823 | break; |
| 9824 | case LYXP_NODE_ROOT_OUTPUT: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9825 | 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] | 9826 | break; |
| 9827 | case LYXP_NODE_ELEM: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9828 | ly_print_(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9829 | xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT); |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9830 | ly_print_(&out, "\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9831 | break; |
| 9832 | case LYXP_NODE_TEXT: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9833 | 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] | 9834 | break; |
| 9835 | case LYXP_NODE_ATTR: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 9836 | 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] | 9837 | break; |
| 9838 | } |
| 9839 | } |
| 9840 | break; |
| 9841 | } |
| 9842 | } |
| 9843 | |
| 9844 | #endif |
| 9845 | |
| 9846 | LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9847 | lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9848 | { |
| 9849 | long double num; |
| 9850 | char *str; |
| 9851 | LY_ERR rc; |
| 9852 | |
| 9853 | if (!set || (set->type == target)) { |
| 9854 | return LY_SUCCESS; |
| 9855 | } |
| 9856 | |
| 9857 | /* it's not possible to convert anything into a node set */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9858 | assert(target != LYXP_SET_NODE_SET); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9859 | |
| 9860 | if (set->type == LYXP_SET_SCNODE_SET) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9861 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9862 | return LY_EINVAL; |
| 9863 | } |
| 9864 | |
| 9865 | /* to STRING */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9866 | 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] | 9867 | switch (set->type) { |
| 9868 | case LYXP_SET_NUMBER: |
| 9869 | if (isnan(set->val.num)) { |
| 9870 | set->val.str = strdup("NaN"); |
| 9871 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 9872 | } else if ((set->val.num == 0) || (set->val.num == -0.0f)) { |
| 9873 | set->val.str = strdup("0"); |
| 9874 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 9875 | } else if (isinf(set->val.num) && !signbit(set->val.num)) { |
| 9876 | set->val.str = strdup("Infinity"); |
| 9877 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 9878 | } else if (isinf(set->val.num) && signbit(set->val.num)) { |
| 9879 | set->val.str = strdup("-Infinity"); |
| 9880 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 9881 | } else if ((long long)set->val.num == set->val.num) { |
| 9882 | if (asprintf(&str, "%lld", (long long)set->val.num) == -1) { |
| 9883 | LOGMEM_RET(set->ctx); |
| 9884 | } |
| 9885 | set->val.str = str; |
| 9886 | } else { |
| 9887 | if (asprintf(&str, "%03.1Lf", set->val.num) == -1) { |
| 9888 | LOGMEM_RET(set->ctx); |
| 9889 | } |
| 9890 | set->val.str = str; |
| 9891 | } |
| 9892 | break; |
| 9893 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9894 | if (set->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9895 | set->val.str = strdup("true"); |
| 9896 | } else { |
| 9897 | set->val.str = strdup("false"); |
| 9898 | } |
| 9899 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM); |
| 9900 | break; |
| 9901 | case LYXP_SET_NODE_SET: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9902 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9903 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9904 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 9905 | rc = cast_node_set_to_string(set, &str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9906 | LY_CHECK_RET(rc); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9907 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9908 | set->val.str = str; |
| 9909 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9910 | default: |
| 9911 | LOGINT_RET(set->ctx); |
| 9912 | } |
| 9913 | set->type = LYXP_SET_STRING; |
| 9914 | } |
| 9915 | |
| 9916 | /* to NUMBER */ |
| 9917 | if (target == LYXP_SET_NUMBER) { |
| 9918 | switch (set->type) { |
| 9919 | case LYXP_SET_STRING: |
| 9920 | num = cast_string_to_number(set->val.str); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9921 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9922 | set->val.num = num; |
| 9923 | break; |
| 9924 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9925 | if (set->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9926 | set->val.num = 1; |
| 9927 | } else { |
| 9928 | set->val.num = 0; |
| 9929 | } |
| 9930 | break; |
| 9931 | default: |
| 9932 | LOGINT_RET(set->ctx); |
| 9933 | } |
| 9934 | set->type = LYXP_SET_NUMBER; |
| 9935 | } |
| 9936 | |
| 9937 | /* to BOOLEAN */ |
| 9938 | if (target == LYXP_SET_BOOLEAN) { |
| 9939 | switch (set->type) { |
| 9940 | case LYXP_SET_NUMBER: |
| 9941 | 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] | 9942 | set->val.bln = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9943 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9944 | set->val.bln = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9945 | } |
| 9946 | break; |
| 9947 | case LYXP_SET_STRING: |
| 9948 | if (set->val.str[0]) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9949 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9950 | set->val.bln = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9951 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9952 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9953 | set->val.bln = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9954 | } |
| 9955 | break; |
| 9956 | case LYXP_SET_NODE_SET: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9957 | if (set->used) { |
| 9958 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9959 | set->val.bln = 1; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9960 | } else { |
| 9961 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9962 | set->val.bln = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 9963 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9964 | break; |
| 9965 | default: |
| 9966 | LOGINT_RET(set->ctx); |
| 9967 | } |
| 9968 | set->type = LYXP_SET_BOOLEAN; |
| 9969 | } |
| 9970 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9971 | return LY_SUCCESS; |
| 9972 | } |
| 9973 | |
| 9974 | LY_ERR |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 9975 | 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] | 9976 | LY_VALUE_FORMAT format, void *prefix_data, const struct lysc_node *cur_scnode, |
| 9977 | 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] | 9978 | { |
Michal Vasko | b7ba2c9 | 2024-01-05 15:17:53 +0100 | [diff] [blame] | 9979 | LY_ERR rc; |
Michal Vasko | dd528af | 2022-08-08 14:35:07 +0200 | [diff] [blame] | 9980 | uint32_t tok_idx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9981 | |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 9982 | LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL); |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 9983 | if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9984 | LOGARG(NULL, "Current module must be set if schema format is used."); |
| 9985 | return LY_EINVAL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 9986 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9987 | |
| 9988 | /* prepare set for evaluation */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9989 | memset(set, 0, sizeof *set); |
| 9990 | set->type = LYXP_SET_SCNODE_SET; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9991 | set->root_type = lyxp_get_root_type(NULL, ctx_scnode, options); |
Michal Vasko | e4a6d01 | 2023-05-22 14:34:52 +0200 | [diff] [blame] | 9992 | 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] | 9993 | set->val.scnodes[0].in_ctx = LYXP_SET_SCNODE_START; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9994 | |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 9995 | set->ctx = (struct ly_ctx *)ctx; |
Michal Vasko | a3e92bc | 2022-07-29 14:56:23 +0200 | [diff] [blame] | 9996 | set->cur_scnode = cur_scnode; |
| 9997 | for (set->context_op = cur_scnode; |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 9998 | set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); |
| 9999 | set->context_op = set->context_op->parent) {} |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 10000 | set->cur_mod = cur_mod; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 10001 | set->format = format; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 10002 | set->prefix_data = prefix_data; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 10003 | |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 10004 | if (set->cur_scnode) { |
| 10005 | LOG_LOCSET(set->cur_scnode, NULL); |
| 10006 | } |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 10007 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 10008 | /* evaluate */ |
Michal Vasko | b7ba2c9 | 2024-01-05 15:17:53 +0100 | [diff] [blame] | 10009 | rc = eval_expr_select(exp, &tok_idx, 0, set, options); |
| 10010 | if (!rc && set->not_found) { |
| 10011 | rc = LY_ENOTFOUND; |
| 10012 | } |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 10013 | |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 10014 | if (set->cur_scnode) { |
| 10015 | LOG_LOCBACK(1, 0); |
| 10016 | } |
Michal Vasko | b7ba2c9 | 2024-01-05 15:17:53 +0100 | [diff] [blame] | 10017 | return rc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 10018 | } |
Michal Vasko | d43d71a | 2020-08-07 14:54:58 +0200 | [diff] [blame] | 10019 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 10020 | LIBYANG_API_DEF const char * |
Michal Vasko | d43d71a | 2020-08-07 14:54:58 +0200 | [diff] [blame] | 10021 | lyxp_get_expr(const struct lyxp_expr *path) |
| 10022 | { |
| 10023 | if (!path) { |
| 10024 | return NULL; |
| 10025 | } |
| 10026 | |
| 10027 | return path->expr; |
| 10028 | } |