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 | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6 | * Copyright (c) 2015 - 2020 CESNET, z.s.p.o. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 7 | * |
| 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * https://opensource.org/licenses/BSD-3-Clause |
| 13 | */ |
Christian Hopps | 32874e1 | 2021-05-01 09:43:54 -0400 | [diff] [blame] | 14 | #define _GNU_SOURCE /* asprintf, strdup */ |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 15 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 16 | #include "xpath.h" |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 17 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 18 | #include <assert.h> |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 19 | #include <ctype.h> |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 20 | #include <errno.h> |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 21 | #include <math.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 22 | #include <stdint.h> |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 25 | #include <string.h> |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 26 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 27 | #include "common.h" |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 28 | #include "compat.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 29 | #include "context.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 30 | #include "dict.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 31 | #include "hash_table.h" |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 32 | #include "out.h" |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 33 | #include "parser_data.h" |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 34 | #include "path.h" |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 35 | #include "plugins_types.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 36 | #include "printer_data.h" |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 37 | #include "schema_compile_node.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 38 | #include "tree.h" |
Radek Krejci | 7711410 | 2021-03-10 15:21:57 +0100 | [diff] [blame] | 39 | #include "tree_data.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 40 | #include "tree_data_internal.h" |
Radek Krejci | 859a15a | 2021-03-05 20:56:59 +0100 | [diff] [blame] | 41 | #include "tree_edit.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 42 | #include "tree_schema_internal.h" |
| 43 | #include "xml.h" |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 44 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 45 | static LY_ERR reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth); |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 46 | static LY_ERR eval_expr_select(const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype, |
| 47 | struct lyxp_set *set, uint32_t options); |
Michal Vasko | 9392369 | 2021-05-07 15:28:02 +0200 | [diff] [blame] | 48 | static LY_ERR moveto_resolve_model(const char **qname, uint16_t *qname_len, const struct lyxp_set *set, |
| 49 | const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 50 | |
aPiecek | 96dc1e3 | 2021-10-08 15:45:59 +0200 | [diff] [blame] | 51 | /* Functions are divided into the following basic classes: |
| 52 | * |
| 53 | * (re)parse functions: |
| 54 | * Parse functions parse the expression into |
| 55 | * tokens (syntactic analysis). |
| 56 | * Reparse functions perform semantic analysis |
| 57 | * (do not save the result, just a check) of |
| 58 | * the expression and fill repeat indices. |
| 59 | * |
| 60 | * warn functions: |
| 61 | * Warn functions check specific reasonable conditions for schema XPath |
| 62 | * and print a warning if they are not satisfied. |
| 63 | * |
| 64 | * moveto functions: |
| 65 | * They and only they actually change the context (set). |
| 66 | * |
| 67 | * eval functions: |
| 68 | * They execute a parsed XPath expression on some data subtree. |
| 69 | */ |
| 70 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 71 | /** |
| 72 | * @brief Print the type of an XPath \p set. |
| 73 | * |
| 74 | * @param[in] set Set to use. |
| 75 | * @return Set type string. |
| 76 | */ |
| 77 | static const char * |
| 78 | print_set_type(struct lyxp_set *set) |
| 79 | { |
| 80 | switch (set->type) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 81 | case LYXP_SET_NODE_SET: |
| 82 | return "node set"; |
| 83 | case LYXP_SET_SCNODE_SET: |
| 84 | return "schema node set"; |
| 85 | case LYXP_SET_BOOLEAN: |
| 86 | return "boolean"; |
| 87 | case LYXP_SET_NUMBER: |
| 88 | return "number"; |
| 89 | case LYXP_SET_STRING: |
| 90 | return "string"; |
| 91 | } |
| 92 | |
| 93 | return NULL; |
| 94 | } |
| 95 | |
Michal Vasko | 24cddf8 | 2020-06-01 08:17:01 +0200 | [diff] [blame] | 96 | const char * |
| 97 | lyxp_print_token(enum lyxp_token tok) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 98 | { |
| 99 | switch (tok) { |
| 100 | case LYXP_TOKEN_PAR1: |
| 101 | return "("; |
| 102 | case LYXP_TOKEN_PAR2: |
| 103 | return ")"; |
| 104 | case LYXP_TOKEN_BRACK1: |
| 105 | return "["; |
| 106 | case LYXP_TOKEN_BRACK2: |
| 107 | return "]"; |
| 108 | case LYXP_TOKEN_DOT: |
| 109 | return "."; |
| 110 | case LYXP_TOKEN_DDOT: |
| 111 | return ".."; |
| 112 | case LYXP_TOKEN_AT: |
| 113 | return "@"; |
| 114 | case LYXP_TOKEN_COMMA: |
| 115 | return ","; |
| 116 | case LYXP_TOKEN_NAMETEST: |
| 117 | return "NameTest"; |
| 118 | case LYXP_TOKEN_NODETYPE: |
| 119 | return "NodeType"; |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 120 | case LYXP_TOKEN_VARREF: |
| 121 | return "VariableReference"; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 122 | case LYXP_TOKEN_FUNCNAME: |
| 123 | return "FunctionName"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 124 | case LYXP_TOKEN_OPER_LOG: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 125 | return "Operator(Logic)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 126 | case LYXP_TOKEN_OPER_EQUAL: |
| 127 | return "Operator(Equal)"; |
| 128 | case LYXP_TOKEN_OPER_NEQUAL: |
| 129 | return "Operator(Non-equal)"; |
| 130 | case LYXP_TOKEN_OPER_COMP: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 131 | return "Operator(Comparison)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 132 | case LYXP_TOKEN_OPER_MATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 133 | return "Operator(Math)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 134 | case LYXP_TOKEN_OPER_UNI: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 135 | return "Operator(Union)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 136 | case LYXP_TOKEN_OPER_PATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 137 | return "Operator(Path)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 138 | case LYXP_TOKEN_OPER_RPATH: |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 139 | return "Operator(Recursive Path)"; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 140 | case LYXP_TOKEN_LITERAL: |
| 141 | return "Literal"; |
| 142 | case LYXP_TOKEN_NUMBER: |
| 143 | return "Number"; |
| 144 | default: |
| 145 | LOGINT(NULL); |
| 146 | return ""; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * @brief Print the whole expression \p exp to debug output. |
| 152 | * |
| 153 | * @param[in] exp Expression to use. |
| 154 | */ |
| 155 | static void |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 156 | print_expr_struct_debug(const struct lyxp_expr *exp) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 157 | { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 158 | #define MSG_BUFFER_SIZE 128 |
| 159 | char tmp[MSG_BUFFER_SIZE]; |
Michal Vasko | 1fdd8fa | 2021-01-08 09:21:45 +0100 | [diff] [blame] | 160 | uint32_t i, j; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 161 | |
Radek Krejci | 52b6d51 | 2020-10-12 12:33:17 +0200 | [diff] [blame] | 162 | if (!exp || (ly_ll < LY_LLDBG)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 163 | return; |
| 164 | } |
| 165 | |
| 166 | LOGDBG(LY_LDGXPATH, "expression \"%s\":", exp->expr); |
| 167 | for (i = 0; i < exp->used; ++i) { |
Michal Vasko | 24cddf8 | 2020-06-01 08:17:01 +0200 | [diff] [blame] | 168 | sprintf(tmp, "\ttoken %s, in expression \"%.*s\"", lyxp_print_token(exp->tokens[i]), exp->tok_len[i], |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 169 | &exp->expr[exp->tok_pos[i]]); |
Michal Vasko | 2304955 | 2021-03-04 15:57:44 +0100 | [diff] [blame] | 170 | if (exp->repeat && exp->repeat[i]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 171 | sprintf(tmp + strlen(tmp), " (repeat %d", exp->repeat[i][0]); |
| 172 | for (j = 1; exp->repeat[i][j]; ++j) { |
| 173 | sprintf(tmp + strlen(tmp), ", %d", exp->repeat[i][j]); |
| 174 | } |
| 175 | strcat(tmp, ")"); |
| 176 | } |
| 177 | LOGDBG(LY_LDGXPATH, tmp); |
| 178 | } |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 179 | #undef MSG_BUFFER_SIZE |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | #ifndef NDEBUG |
| 183 | |
| 184 | /** |
| 185 | * @brief Print XPath set content to debug output. |
| 186 | * |
| 187 | * @param[in] set Set to print. |
| 188 | */ |
| 189 | static void |
| 190 | print_set_debug(struct lyxp_set *set) |
| 191 | { |
| 192 | uint32_t i; |
| 193 | char *str; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 194 | struct lyxp_set_node *item; |
| 195 | struct lyxp_set_scnode *sitem; |
| 196 | |
Radek Krejci | 52b6d51 | 2020-10-12 12:33:17 +0200 | [diff] [blame] | 197 | if (ly_ll < LY_LLDBG) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 198 | return; |
| 199 | } |
| 200 | |
| 201 | switch (set->type) { |
| 202 | case LYXP_SET_NODE_SET: |
| 203 | LOGDBG(LY_LDGXPATH, "set NODE SET:"); |
| 204 | for (i = 0; i < set->used; ++i) { |
| 205 | item = &set->val.nodes[i]; |
| 206 | |
| 207 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 208 | case LYXP_NODE_NONE: |
| 209 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): NONE", i + 1, item->pos); |
| 210 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 211 | case LYXP_NODE_ROOT: |
| 212 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT", i + 1, item->pos); |
| 213 | break; |
| 214 | case LYXP_NODE_ROOT_CONFIG: |
| 215 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT CONFIG", i + 1, item->pos); |
| 216 | break; |
| 217 | case LYXP_NODE_ELEM: |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 218 | if ((item->node->schema->nodetype == LYS_LIST) && (lyd_child(item->node)->schema->nodetype == LYS_LEAF)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 219 | 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] | 220 | item->node->schema->name, lyd_get_value(lyd_child(item->node))); |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 221 | } else if (item->node->schema->nodetype == LYS_LEAFLIST) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 222 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (val: %s)", i + 1, item->pos, |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 223 | item->node->schema->name, lyd_get_value(item->node)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 224 | } else { |
| 225 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s", i + 1, item->pos, item->node->schema->name); |
| 226 | } |
| 227 | break; |
| 228 | case LYXP_NODE_TEXT: |
| 229 | if (item->node->schema->nodetype & LYS_ANYDATA) { |
| 230 | 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] | 231 | item->node->schema->nodetype == LYS_ANYXML ? "anyxml" : "anydata"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 232 | } else { |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 233 | 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] | 234 | } |
| 235 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 236 | case LYXP_NODE_META: |
| 237 | 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] | 238 | set->val.meta[i].meta->value); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 239 | break; |
| 240 | } |
| 241 | } |
| 242 | break; |
| 243 | |
| 244 | case LYXP_SET_SCNODE_SET: |
| 245 | LOGDBG(LY_LDGXPATH, "set SCNODE SET:"); |
| 246 | for (i = 0; i < set->used; ++i) { |
| 247 | sitem = &set->val.scnodes[i]; |
| 248 | |
| 249 | switch (sitem->type) { |
| 250 | case LYXP_NODE_ROOT: |
| 251 | LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT", i + 1, sitem->in_ctx); |
| 252 | break; |
| 253 | case LYXP_NODE_ROOT_CONFIG: |
| 254 | LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT CONFIG", i + 1, sitem->in_ctx); |
| 255 | break; |
| 256 | case LYXP_NODE_ELEM: |
| 257 | LOGDBG(LY_LDGXPATH, "\t%d (%u): ELEM %s", i + 1, sitem->in_ctx, sitem->scnode->name); |
| 258 | break; |
| 259 | default: |
| 260 | LOGINT(NULL); |
| 261 | break; |
| 262 | } |
| 263 | } |
| 264 | break; |
| 265 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 266 | case LYXP_SET_BOOLEAN: |
| 267 | LOGDBG(LY_LDGXPATH, "set BOOLEAN"); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 268 | LOGDBG(LY_LDGXPATH, "\t%s", (set->val.bln ? "true" : "false")); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 269 | break; |
| 270 | |
| 271 | case LYXP_SET_STRING: |
| 272 | LOGDBG(LY_LDGXPATH, "set STRING"); |
| 273 | LOGDBG(LY_LDGXPATH, "\t%s", set->val.str); |
| 274 | break; |
| 275 | |
| 276 | case LYXP_SET_NUMBER: |
| 277 | LOGDBG(LY_LDGXPATH, "set NUMBER"); |
| 278 | |
| 279 | if (isnan(set->val.num)) { |
| 280 | str = strdup("NaN"); |
| 281 | } else if ((set->val.num == 0) || (set->val.num == -0.0f)) { |
| 282 | str = strdup("0"); |
| 283 | } else if (isinf(set->val.num) && !signbit(set->val.num)) { |
| 284 | str = strdup("Infinity"); |
| 285 | } else if (isinf(set->val.num) && signbit(set->val.num)) { |
| 286 | str = strdup("-Infinity"); |
| 287 | } else if ((long long)set->val.num == set->val.num) { |
| 288 | if (asprintf(&str, "%lld", (long long)set->val.num) == -1) { |
| 289 | str = NULL; |
| 290 | } |
| 291 | } else { |
| 292 | if (asprintf(&str, "%03.1Lf", set->val.num) == -1) { |
| 293 | str = NULL; |
| 294 | } |
| 295 | } |
| 296 | LY_CHECK_ERR_RET(!str, LOGMEM(NULL), ); |
| 297 | |
| 298 | LOGDBG(LY_LDGXPATH, "\t%s", str); |
| 299 | free(str); |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | #endif |
| 304 | |
| 305 | /** |
| 306 | * @brief Realloc the string \p str. |
| 307 | * |
| 308 | * @param[in] ctx libyang context for logging. |
| 309 | * @param[in] needed How much free space is required. |
| 310 | * @param[in,out] str Pointer to the string to use. |
| 311 | * @param[in,out] used Used bytes in \p str. |
| 312 | * @param[in,out] size Allocated bytes in \p str. |
| 313 | * @return LY_ERR |
| 314 | */ |
| 315 | static LY_ERR |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 316 | cast_string_realloc(const struct ly_ctx *ctx, uint16_t needed, char **str, uint16_t *used, uint16_t *size) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 317 | { |
| 318 | if (*size - *used < needed) { |
| 319 | do { |
| 320 | if ((UINT16_MAX - *size) < LYXP_STRING_CAST_SIZE_STEP) { |
| 321 | LOGERR(ctx, LY_EINVAL, "XPath string length limit (%u) reached.", UINT16_MAX); |
| 322 | return LY_EINVAL; |
| 323 | } |
| 324 | *size += LYXP_STRING_CAST_SIZE_STEP; |
| 325 | } while (*size - *used < needed); |
| 326 | *str = ly_realloc(*str, *size * sizeof(char)); |
| 327 | LY_CHECK_ERR_RET(!(*str), LOGMEM(ctx), LY_EMEM); |
| 328 | } |
| 329 | |
| 330 | return LY_SUCCESS; |
| 331 | } |
| 332 | |
| 333 | /** |
| 334 | * @brief Cast nodes recursively to one string @p str. |
| 335 | * |
| 336 | * @param[in] node Node to cast. |
| 337 | * @param[in] fake_cont Whether to put the data into a "fake" container. |
| 338 | * @param[in] root_type Type of the XPath root. |
| 339 | * @param[in] indent Current indent. |
| 340 | * @param[in,out] str Resulting string. |
| 341 | * @param[in,out] used Used bytes in @p str. |
| 342 | * @param[in,out] size Allocated bytes in @p str. |
| 343 | * @return LY_ERR |
| 344 | */ |
| 345 | static LY_ERR |
Michal Vasko | aac267d | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 346 | cast_string_recursive(const struct lyd_node *node, ly_bool fake_cont, enum lyxp_node_type root_type, uint16_t indent, |
| 347 | char **str, uint16_t *used, uint16_t *size) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 348 | { |
Radek Krejci | 7f769d7 | 2020-07-11 23:13:56 +0200 | [diff] [blame] | 349 | char *buf, *line, *ptr = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 350 | const char *value_str; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 351 | const struct lyd_node *child; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 352 | struct lyd_node *tree; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 353 | struct lyd_node_any *any; |
| 354 | LY_ERR rc; |
| 355 | |
| 356 | if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) { |
| 357 | return LY_SUCCESS; |
| 358 | } |
| 359 | |
| 360 | if (fake_cont) { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 361 | rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 362 | LY_CHECK_RET(rc); |
| 363 | strcpy(*str + (*used - 1), "\n"); |
| 364 | ++(*used); |
| 365 | |
| 366 | ++indent; |
| 367 | } |
| 368 | |
| 369 | switch (node->schema->nodetype) { |
| 370 | case LYS_CONTAINER: |
| 371 | case LYS_LIST: |
| 372 | case LYS_RPC: |
| 373 | case LYS_NOTIF: |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 374 | rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 375 | LY_CHECK_RET(rc); |
| 376 | strcpy(*str + (*used - 1), "\n"); |
| 377 | ++(*used); |
| 378 | |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 379 | for (child = lyd_child(node); child; child = child->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 380 | rc = cast_string_recursive(child, 0, root_type, indent + 1, str, used, size); |
| 381 | LY_CHECK_RET(rc); |
| 382 | } |
| 383 | |
| 384 | break; |
| 385 | |
| 386 | case LYS_LEAF: |
| 387 | case LYS_LEAFLIST: |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 388 | value_str = lyd_get_value(node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 389 | |
| 390 | /* print indent */ |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 391 | LY_CHECK_RET(cast_string_realloc(LYD_CTX(node), indent * 2 + strlen(value_str) + 1, str, used, size)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 392 | memset(*str + (*used - 1), ' ', indent * 2); |
| 393 | *used += indent * 2; |
| 394 | |
| 395 | /* print value */ |
| 396 | if (*used == 1) { |
| 397 | sprintf(*str + (*used - 1), "%s", value_str); |
| 398 | *used += strlen(value_str); |
| 399 | } else { |
| 400 | sprintf(*str + (*used - 1), "%s\n", value_str); |
| 401 | *used += strlen(value_str) + 1; |
| 402 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 403 | |
| 404 | break; |
| 405 | |
| 406 | case LYS_ANYXML: |
| 407 | case LYS_ANYDATA: |
| 408 | any = (struct lyd_node_any *)node; |
| 409 | if (!(void *)any->value.tree) { |
| 410 | /* no content */ |
| 411 | buf = strdup(""); |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 412 | LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_CTX(node)), LY_EMEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 413 | } else { |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 414 | struct ly_out *out; |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 415 | |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 416 | if (any->value_type == LYD_ANYDATA_LYB) { |
| 417 | /* try to parse it into a data tree */ |
Michal Vasko | aac267d | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 418 | if (lyd_parse_data_mem((struct ly_ctx *)LYD_CTX(node), any->value.mem, LYD_LYB, |
| 419 | LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, &tree) == LY_SUCCESS) { |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 420 | /* successfully parsed */ |
| 421 | free(any->value.mem); |
| 422 | any->value.tree = tree; |
| 423 | any->value_type = LYD_ANYDATA_DATATREE; |
| 424 | } |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 425 | /* error is covered by the following switch where LYD_ANYDATA_LYB causes failure */ |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 426 | } |
| 427 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 428 | switch (any->value_type) { |
| 429 | case LYD_ANYDATA_STRING: |
| 430 | case LYD_ANYDATA_XML: |
| 431 | case LYD_ANYDATA_JSON: |
| 432 | buf = strdup(any->value.json); |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 433 | LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_CTX(node)), LY_EMEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 434 | break; |
| 435 | case LYD_ANYDATA_DATATREE: |
Radek Krejci | 84ce7b1 | 2020-06-11 17:28:25 +0200 | [diff] [blame] | 436 | LY_CHECK_RET(ly_out_new_memory(&buf, 0, &out)); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 437 | rc = lyd_print_all(out, any->value.tree, LYD_XML, 0); |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 438 | ly_out_free(out, NULL, 0); |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 439 | LY_CHECK_RET(rc < 0, -rc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 440 | break; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 441 | case LYD_ANYDATA_LYB: |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 442 | LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot convert LYB anydata into string."); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 443 | return LY_EINVAL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 444 | } |
| 445 | } |
| 446 | |
| 447 | line = strtok_r(buf, "\n", &ptr); |
| 448 | do { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 449 | rc = cast_string_realloc(LYD_CTX(node), indent * 2 + strlen(line) + 1, str, used, size); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 450 | if (rc != LY_SUCCESS) { |
| 451 | free(buf); |
| 452 | return rc; |
| 453 | } |
| 454 | memset(*str + (*used - 1), ' ', indent * 2); |
| 455 | *used += indent * 2; |
| 456 | |
| 457 | strcpy(*str + (*used - 1), line); |
| 458 | *used += strlen(line); |
| 459 | |
| 460 | strcpy(*str + (*used - 1), "\n"); |
| 461 | *used += 1; |
| 462 | } while ((line = strtok_r(NULL, "\n", &ptr))); |
| 463 | |
| 464 | free(buf); |
| 465 | break; |
| 466 | |
| 467 | default: |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 468 | LOGINT_RET(LYD_CTX(node)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | if (fake_cont) { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 472 | rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 473 | LY_CHECK_RET(rc); |
| 474 | strcpy(*str + (*used - 1), "\n"); |
| 475 | ++(*used); |
| 476 | |
| 477 | --indent; |
| 478 | } |
| 479 | |
| 480 | return LY_SUCCESS; |
| 481 | } |
| 482 | |
| 483 | /** |
| 484 | * @brief Cast an element into a string. |
| 485 | * |
| 486 | * @param[in] node Node to cast. |
| 487 | * @param[in] fake_cont Whether to put the data into a "fake" container. |
| 488 | * @param[in] root_type Type of the XPath root. |
| 489 | * @param[out] str Element cast to dynamically-allocated string. |
| 490 | * @return LY_ERR |
| 491 | */ |
| 492 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 493 | cast_string_elem(struct lyd_node *node, ly_bool fake_cont, enum lyxp_node_type root_type, char **str) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 494 | { |
| 495 | uint16_t used, size; |
| 496 | LY_ERR rc; |
| 497 | |
| 498 | *str = malloc(LYXP_STRING_CAST_SIZE_START * sizeof(char)); |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 499 | LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_CTX(node)), LY_EMEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 500 | (*str)[0] = '\0'; |
| 501 | used = 1; |
| 502 | size = LYXP_STRING_CAST_SIZE_START; |
| 503 | |
| 504 | rc = cast_string_recursive(node, fake_cont, root_type, 0, str, &used, &size); |
| 505 | if (rc != LY_SUCCESS) { |
| 506 | free(*str); |
| 507 | return rc; |
| 508 | } |
| 509 | |
| 510 | if (size > used) { |
| 511 | *str = ly_realloc(*str, used * sizeof(char)); |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 512 | LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_CTX(node)), LY_EMEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 513 | } |
| 514 | return LY_SUCCESS; |
| 515 | } |
| 516 | |
| 517 | /** |
| 518 | * @brief Cast a LYXP_SET_NODE_SET set into a string. |
| 519 | * Context position aware. |
| 520 | * |
| 521 | * @param[in] set Set to cast. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 522 | * @param[out] str Cast dynamically-allocated string. |
| 523 | * @return LY_ERR |
| 524 | */ |
| 525 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 526 | cast_node_set_to_string(struct lyxp_set *set, char **str) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 527 | { |
Michal Vasko | aa67706 | 2021-03-09 13:52:53 +0100 | [diff] [blame] | 528 | if (!set->used) { |
| 529 | *str = strdup(""); |
| 530 | if (!*str) { |
| 531 | LOGMEM_RET(set->ctx); |
| 532 | } |
| 533 | return LY_SUCCESS; |
| 534 | } |
| 535 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 536 | switch (set->val.nodes[0].type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 537 | case LYXP_NODE_NONE: |
| 538 | /* invalid */ |
| 539 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 540 | case LYXP_NODE_ROOT: |
| 541 | case LYXP_NODE_ROOT_CONFIG: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 542 | return cast_string_elem(set->val.nodes[0].node, 1, set->root_type, str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 543 | case LYXP_NODE_ELEM: |
| 544 | case LYXP_NODE_TEXT: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 545 | return cast_string_elem(set->val.nodes[0].node, 0, set->root_type, str); |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 546 | case LYXP_NODE_META: |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 547 | *str = strdup(lyd_get_meta_value(set->val.meta[0].meta)); |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 548 | if (!*str) { |
| 549 | LOGMEM_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 550 | } |
| 551 | return LY_SUCCESS; |
| 552 | } |
| 553 | |
| 554 | LOGINT_RET(set->ctx); |
| 555 | } |
| 556 | |
| 557 | /** |
| 558 | * @brief Cast a string into an XPath number. |
| 559 | * |
| 560 | * @param[in] str String to use. |
| 561 | * @return Cast number. |
| 562 | */ |
| 563 | static long double |
| 564 | cast_string_to_number(const char *str) |
| 565 | { |
| 566 | long double num; |
| 567 | char *ptr; |
| 568 | |
| 569 | errno = 0; |
| 570 | num = strtold(str, &ptr); |
| 571 | if (errno || *ptr) { |
| 572 | num = NAN; |
| 573 | } |
| 574 | return num; |
| 575 | } |
| 576 | |
| 577 | /** |
| 578 | * @brief Callback for checking value equality. |
| 579 | * |
Michal Vasko | 62524a9 | 2021-02-26 10:08:50 +0100 | [diff] [blame] | 580 | * Implementation of ::lyht_value_equal_cb. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 581 | * |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 582 | * @param[in] val1_p First value. |
| 583 | * @param[in] val2_p Second value. |
| 584 | * @param[in] mod Whether hash table is being modified. |
| 585 | * @param[in] cb_data Callback data. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 586 | * @return Boolean value whether values are equal or not. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 587 | */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 588 | static ly_bool |
| 589 | 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] | 590 | { |
| 591 | struct lyxp_set_hash_node *val1, *val2; |
| 592 | |
| 593 | val1 = (struct lyxp_set_hash_node *)val1_p; |
| 594 | val2 = (struct lyxp_set_hash_node *)val2_p; |
| 595 | |
| 596 | if ((val1->node == val2->node) && (val1->type == val2->type)) { |
| 597 | return 1; |
| 598 | } |
| 599 | |
| 600 | return 0; |
| 601 | } |
| 602 | |
| 603 | /** |
| 604 | * @brief Insert node and its hash into set. |
| 605 | * |
| 606 | * @param[in] set et to insert to. |
| 607 | * @param[in] node Node with hash. |
| 608 | * @param[in] type Node type. |
| 609 | */ |
| 610 | static void |
| 611 | set_insert_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type) |
| 612 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 613 | LY_ERR r; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 614 | uint32_t i, hash; |
| 615 | struct lyxp_set_hash_node hnode; |
| 616 | |
| 617 | if (!set->ht && (set->used >= LYD_HT_MIN_ITEMS)) { |
| 618 | /* create hash table and add all the nodes */ |
| 619 | set->ht = lyht_new(1, sizeof(struct lyxp_set_hash_node), set_values_equal_cb, NULL, 1); |
| 620 | for (i = 0; i < set->used; ++i) { |
| 621 | hnode.node = set->val.nodes[i].node; |
| 622 | hnode.type = set->val.nodes[i].type; |
| 623 | |
| 624 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 625 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 626 | hash = dict_hash_multi(hash, NULL, 0); |
| 627 | |
| 628 | r = lyht_insert(set->ht, &hnode, hash, NULL); |
| 629 | assert(!r); |
| 630 | (void)r; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 631 | |
Michal Vasko | 9d6befd | 2019-12-11 14:56:56 +0100 | [diff] [blame] | 632 | if (hnode.node == node) { |
| 633 | /* it was just added, do not add it twice */ |
| 634 | node = NULL; |
| 635 | } |
| 636 | } |
| 637 | } |
| 638 | |
| 639 | if (set->ht && node) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 640 | /* add the new node into hash table */ |
| 641 | hnode.node = node; |
| 642 | hnode.type = type; |
| 643 | |
| 644 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 645 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 646 | hash = dict_hash_multi(hash, NULL, 0); |
| 647 | |
| 648 | r = lyht_insert(set->ht, &hnode, hash, NULL); |
| 649 | assert(!r); |
| 650 | (void)r; |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | /** |
| 655 | * @brief Remove node and its hash from set. |
| 656 | * |
| 657 | * @param[in] set Set to remove from. |
| 658 | * @param[in] node Node to remove. |
| 659 | * @param[in] type Node type. |
| 660 | */ |
| 661 | static void |
| 662 | set_remove_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type) |
| 663 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 664 | LY_ERR r; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 665 | struct lyxp_set_hash_node hnode; |
| 666 | uint32_t hash; |
| 667 | |
| 668 | if (set->ht) { |
| 669 | hnode.node = node; |
| 670 | hnode.type = type; |
| 671 | |
| 672 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 673 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 674 | hash = dict_hash_multi(hash, NULL, 0); |
| 675 | |
| 676 | r = lyht_remove(set->ht, &hnode, hash); |
| 677 | assert(!r); |
| 678 | (void)r; |
| 679 | |
| 680 | if (!set->ht->used) { |
| 681 | lyht_free(set->ht); |
| 682 | set->ht = NULL; |
| 683 | } |
| 684 | } |
| 685 | } |
| 686 | |
| 687 | /** |
| 688 | * @brief Check whether node is in set based on its hash. |
| 689 | * |
| 690 | * @param[in] set Set to search in. |
| 691 | * @param[in] node Node to search for. |
| 692 | * @param[in] type Node type. |
| 693 | * @param[in] skip_idx Index in @p set to skip. |
| 694 | * @return LY_ERR |
| 695 | */ |
| 696 | static LY_ERR |
| 697 | set_dup_node_hash_check(const struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type, int skip_idx) |
| 698 | { |
| 699 | struct lyxp_set_hash_node hnode, *match_p; |
| 700 | uint32_t hash; |
| 701 | |
| 702 | hnode.node = node; |
| 703 | hnode.type = type; |
| 704 | |
| 705 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 706 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 707 | hash = dict_hash_multi(hash, NULL, 0); |
| 708 | |
| 709 | if (!lyht_find(set->ht, &hnode, hash, (void **)&match_p)) { |
| 710 | if ((skip_idx > -1) && (set->val.nodes[skip_idx].node == match_p->node) && (set->val.nodes[skip_idx].type == match_p->type)) { |
| 711 | /* we found it on the index that should be skipped, find another */ |
| 712 | hnode = *match_p; |
| 713 | if (lyht_find_next(set->ht, &hnode, hash, (void **)&match_p)) { |
| 714 | /* none other found */ |
| 715 | return LY_SUCCESS; |
| 716 | } |
| 717 | } |
| 718 | |
| 719 | return LY_EEXIST; |
| 720 | } |
| 721 | |
| 722 | /* not found */ |
| 723 | return LY_SUCCESS; |
| 724 | } |
| 725 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 726 | void |
| 727 | lyxp_set_free_content(struct lyxp_set *set) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 728 | { |
| 729 | if (!set) { |
| 730 | return; |
| 731 | } |
| 732 | |
| 733 | if (set->type == LYXP_SET_NODE_SET) { |
| 734 | free(set->val.nodes); |
| 735 | lyht_free(set->ht); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 736 | } else if (set->type == LYXP_SET_SCNODE_SET) { |
| 737 | free(set->val.scnodes); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 738 | lyht_free(set->ht); |
| 739 | } else { |
| 740 | if (set->type == LYXP_SET_STRING) { |
| 741 | free(set->val.str); |
| 742 | } |
| 743 | set->type = LYXP_SET_NODE_SET; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 744 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 745 | |
| 746 | set->val.nodes = NULL; |
| 747 | set->used = 0; |
| 748 | set->size = 0; |
| 749 | set->ht = NULL; |
| 750 | set->ctx_pos = 0; |
aPiecek | 748da73 | 2021-06-01 09:56:43 +0200 | [diff] [blame] | 751 | set->ctx_size = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 752 | } |
| 753 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 754 | /** |
| 755 | * @brief Free dynamically-allocated set. |
| 756 | * |
| 757 | * @param[in] set Set to free. |
| 758 | */ |
| 759 | static void |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 760 | lyxp_set_free(struct lyxp_set *set) |
| 761 | { |
| 762 | if (!set) { |
| 763 | return; |
| 764 | } |
| 765 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 766 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 767 | free(set); |
| 768 | } |
| 769 | |
| 770 | /** |
| 771 | * @brief Initialize set context. |
| 772 | * |
| 773 | * @param[in] new Set to initialize. |
| 774 | * @param[in] set Arbitrary initialized set. |
| 775 | */ |
| 776 | static void |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 777 | set_init(struct lyxp_set *new, const struct lyxp_set *set) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 778 | { |
| 779 | memset(new, 0, sizeof *new); |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 780 | if (set) { |
| 781 | new->ctx = set->ctx; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 782 | new->cur_node = set->cur_node; |
Michal Vasko | 588112f | 2019-12-10 14:51:53 +0100 | [diff] [blame] | 783 | new->root_type = set->root_type; |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 784 | new->context_op = set->context_op; |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 785 | new->tree = set->tree; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 786 | new->cur_mod = set->cur_mod; |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 787 | new->format = set->format; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 788 | new->prefix_data = set->prefix_data; |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 789 | new->vars = set->vars; |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 790 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 791 | } |
| 792 | |
| 793 | /** |
| 794 | * @brief Create a deep copy of a set. |
| 795 | * |
| 796 | * @param[in] set Set to copy. |
| 797 | * @return Copy of @p set. |
| 798 | */ |
| 799 | static struct lyxp_set * |
| 800 | set_copy(struct lyxp_set *set) |
| 801 | { |
| 802 | struct lyxp_set *ret; |
Michal Vasko | 1fdd8fa | 2021-01-08 09:21:45 +0100 | [diff] [blame] | 803 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 804 | |
| 805 | if (!set) { |
| 806 | return NULL; |
| 807 | } |
| 808 | |
| 809 | ret = malloc(sizeof *ret); |
| 810 | LY_CHECK_ERR_RET(!ret, LOGMEM(set->ctx), NULL); |
| 811 | set_init(ret, set); |
| 812 | |
| 813 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 814 | ret->type = set->type; |
| 815 | |
| 816 | for (i = 0; i < set->used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 817 | if ((set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) || |
| 818 | (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START)) { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 819 | uint32_t idx; |
| 820 | LY_CHECK_ERR_RET(lyxp_set_scnode_insert_node(ret, set->val.scnodes[i].scnode, set->val.scnodes[i].type, &idx), |
| 821 | lyxp_set_free(ret), NULL); |
Michal Vasko | 3f27c52 | 2020-01-06 08:37:49 +0100 | [diff] [blame] | 822 | /* coverity seems to think scnodes can be NULL */ |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 823 | if (!ret->val.scnodes) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 824 | lyxp_set_free(ret); |
| 825 | return NULL; |
| 826 | } |
Michal Vasko | ba71654 | 2019-12-16 10:01:58 +0100 | [diff] [blame] | 827 | ret->val.scnodes[idx].in_ctx = set->val.scnodes[i].in_ctx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 828 | } |
| 829 | } |
| 830 | } else if (set->type == LYXP_SET_NODE_SET) { |
| 831 | ret->type = set->type; |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 832 | if (set->used) { |
| 833 | ret->val.nodes = malloc(set->used * sizeof *ret->val.nodes); |
| 834 | LY_CHECK_ERR_RET(!ret->val.nodes, LOGMEM(set->ctx); free(ret), NULL); |
| 835 | memcpy(ret->val.nodes, set->val.nodes, set->used * sizeof *ret->val.nodes); |
| 836 | } else { |
| 837 | ret->val.nodes = NULL; |
| 838 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 839 | |
| 840 | ret->used = ret->size = set->used; |
| 841 | ret->ctx_pos = set->ctx_pos; |
| 842 | ret->ctx_size = set->ctx_size; |
Michal Vasko | 4a04e54 | 2021-02-01 08:58:30 +0100 | [diff] [blame] | 843 | if (set->ht) { |
| 844 | ret->ht = lyht_dup(set->ht); |
| 845 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 846 | } else { |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 847 | memcpy(ret, set, sizeof *ret); |
| 848 | if (set->type == LYXP_SET_STRING) { |
| 849 | ret->val.str = strdup(set->val.str); |
| 850 | LY_CHECK_ERR_RET(!ret->val.str, LOGMEM(set->ctx); free(ret), NULL); |
| 851 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 852 | } |
| 853 | |
| 854 | return ret; |
| 855 | } |
| 856 | |
| 857 | /** |
| 858 | * @brief Fill XPath set with a string. Any current data are disposed of. |
| 859 | * |
| 860 | * @param[in] set Set to fill. |
| 861 | * @param[in] string String to fill into \p set. |
| 862 | * @param[in] str_len Length of \p string. 0 is a valid value! |
| 863 | */ |
| 864 | static void |
| 865 | set_fill_string(struct lyxp_set *set, const char *string, uint16_t str_len) |
| 866 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 867 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 868 | |
| 869 | set->type = LYXP_SET_STRING; |
| 870 | if ((str_len == 0) && (string[0] != '\0')) { |
| 871 | string = ""; |
| 872 | } |
| 873 | set->val.str = strndup(string, str_len); |
| 874 | } |
| 875 | |
| 876 | /** |
| 877 | * @brief Fill XPath set with a number. Any current data are disposed of. |
| 878 | * |
| 879 | * @param[in] set Set to fill. |
| 880 | * @param[in] number Number to fill into \p set. |
| 881 | */ |
| 882 | static void |
| 883 | set_fill_number(struct lyxp_set *set, long double number) |
| 884 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 885 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 886 | |
| 887 | set->type = LYXP_SET_NUMBER; |
| 888 | set->val.num = number; |
| 889 | } |
| 890 | |
| 891 | /** |
| 892 | * @brief Fill XPath set with a boolean. Any current data are disposed of. |
| 893 | * |
| 894 | * @param[in] set Set to fill. |
| 895 | * @param[in] boolean Boolean to fill into \p set. |
| 896 | */ |
| 897 | static void |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 898 | set_fill_boolean(struct lyxp_set *set, ly_bool boolean) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 899 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 900 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 901 | |
| 902 | set->type = LYXP_SET_BOOLEAN; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 903 | set->val.bln = boolean; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 904 | } |
| 905 | |
| 906 | /** |
| 907 | * @brief Fill XPath set with the value from another set (deep assign). |
| 908 | * Any current data are disposed of. |
| 909 | * |
| 910 | * @param[in] trg Set to fill. |
| 911 | * @param[in] src Source set to copy into \p trg. |
| 912 | */ |
| 913 | static void |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 914 | set_fill_set(struct lyxp_set *trg, const struct lyxp_set *src) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 915 | { |
| 916 | if (!trg || !src) { |
| 917 | return; |
| 918 | } |
| 919 | |
| 920 | if (trg->type == LYXP_SET_NODE_SET) { |
| 921 | free(trg->val.nodes); |
| 922 | } else if (trg->type == LYXP_SET_STRING) { |
| 923 | free(trg->val.str); |
| 924 | } |
| 925 | set_init(trg, src); |
| 926 | |
| 927 | if (src->type == LYXP_SET_SCNODE_SET) { |
| 928 | trg->type = LYXP_SET_SCNODE_SET; |
| 929 | trg->used = src->used; |
| 930 | trg->size = src->used; |
| 931 | |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 932 | if (trg->size) { |
| 933 | trg->val.scnodes = ly_realloc(trg->val.scnodes, trg->size * sizeof *trg->val.scnodes); |
| 934 | LY_CHECK_ERR_RET(!trg->val.scnodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), ); |
| 935 | memcpy(trg->val.scnodes, src->val.scnodes, src->used * sizeof *src->val.scnodes); |
| 936 | } else { |
| 937 | trg->val.scnodes = NULL; |
| 938 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 939 | } else if (src->type == LYXP_SET_BOOLEAN) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 940 | set_fill_boolean(trg, src->val.bln); |
Michal Vasko | 44f3d2c | 2020-08-24 09:49:38 +0200 | [diff] [blame] | 941 | } else if (src->type == LYXP_SET_NUMBER) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 942 | set_fill_number(trg, src->val.num); |
| 943 | } else if (src->type == LYXP_SET_STRING) { |
| 944 | set_fill_string(trg, src->val.str, strlen(src->val.str)); |
| 945 | } else { |
| 946 | if (trg->type == LYXP_SET_NODE_SET) { |
| 947 | free(trg->val.nodes); |
| 948 | } else if (trg->type == LYXP_SET_STRING) { |
| 949 | free(trg->val.str); |
| 950 | } |
| 951 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 952 | assert(src->type == LYXP_SET_NODE_SET); |
| 953 | |
| 954 | trg->type = LYXP_SET_NODE_SET; |
| 955 | trg->used = src->used; |
| 956 | trg->size = src->used; |
| 957 | trg->ctx_pos = src->ctx_pos; |
| 958 | trg->ctx_size = src->ctx_size; |
| 959 | |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 960 | if (trg->size) { |
| 961 | trg->val.nodes = malloc(trg->size * sizeof *trg->val.nodes); |
| 962 | LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), ); |
| 963 | memcpy(trg->val.nodes, src->val.nodes, src->used * sizeof *src->val.nodes); |
| 964 | } else { |
| 965 | trg->val.nodes = NULL; |
| 966 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 967 | if (src->ht) { |
| 968 | trg->ht = lyht_dup(src->ht); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 969 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 970 | trg->ht = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 971 | } |
| 972 | } |
| 973 | } |
| 974 | |
| 975 | /** |
| 976 | * @brief Clear context of all schema nodes. |
| 977 | * |
| 978 | * @param[in] set Set to clear. |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 979 | * @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] | 980 | */ |
| 981 | static void |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 982 | set_scnode_clear_ctx(struct lyxp_set *set, int32_t new_ctx) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 983 | { |
| 984 | uint32_t i; |
| 985 | |
| 986 | for (i = 0; i < set->used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 987 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 988 | set->val.scnodes[i].in_ctx = new_ctx; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 989 | } else if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START) { |
| 990 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 991 | } |
| 992 | } |
| 993 | } |
| 994 | |
| 995 | /** |
| 996 | * @brief Remove a node from a set. Removing last node changes |
| 997 | * set into LYXP_SET_EMPTY. Context position aware. |
| 998 | * |
| 999 | * @param[in] set Set to use. |
| 1000 | * @param[in] idx Index from @p set of the node to be removed. |
| 1001 | */ |
| 1002 | static void |
| 1003 | set_remove_node(struct lyxp_set *set, uint32_t idx) |
| 1004 | { |
| 1005 | assert(set && (set->type == LYXP_SET_NODE_SET)); |
| 1006 | assert(idx < set->used); |
| 1007 | |
| 1008 | set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
| 1009 | |
| 1010 | --set->used; |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 1011 | if (idx < set->used) { |
| 1012 | memmove(&set->val.nodes[idx], &set->val.nodes[idx + 1], (set->used - idx) * sizeof *set->val.nodes); |
| 1013 | } else if (!set->used) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1014 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1015 | } |
| 1016 | } |
| 1017 | |
| 1018 | /** |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1019 | * @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] | 1020 | * |
| 1021 | * @param[in] set Set to use. |
| 1022 | * @param[in] idx Index from @p set of the node to be removed. |
| 1023 | */ |
| 1024 | static void |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1025 | set_remove_node_none(struct lyxp_set *set, uint32_t idx) |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1026 | { |
| 1027 | assert(set && (set->type == LYXP_SET_NODE_SET)); |
| 1028 | assert(idx < set->used); |
| 1029 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1030 | if (set->val.nodes[idx].type == LYXP_NODE_ELEM) { |
| 1031 | set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
| 1032 | } |
| 1033 | set->val.nodes[idx].type = LYXP_NODE_NONE; |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1034 | } |
| 1035 | |
| 1036 | /** |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1037 | * @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] | 1038 | * set into LYXP_SET_EMPTY. Context position aware. |
| 1039 | * |
| 1040 | * @param[in] set Set to consolidate. |
| 1041 | */ |
| 1042 | static void |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1043 | set_remove_nodes_none(struct lyxp_set *set) |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1044 | { |
Michal Vasko | 1fdd8fa | 2021-01-08 09:21:45 +0100 | [diff] [blame] | 1045 | uint32_t i, orig_used, end = 0; |
| 1046 | int64_t start; |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1047 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1048 | assert(set); |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1049 | |
| 1050 | orig_used = set->used; |
| 1051 | set->used = 0; |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 1052 | for (i = 0; i < orig_used; ) { |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1053 | start = -1; |
| 1054 | do { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1055 | if ((set->val.nodes[i].type != LYXP_NODE_NONE) && (start == -1)) { |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1056 | start = i; |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1057 | } else if ((start > -1) && (set->val.nodes[i].type == LYXP_NODE_NONE)) { |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1058 | end = i; |
| 1059 | ++i; |
| 1060 | break; |
| 1061 | } |
| 1062 | |
| 1063 | ++i; |
| 1064 | if (i == orig_used) { |
| 1065 | end = i; |
| 1066 | } |
| 1067 | } while (i < orig_used); |
| 1068 | |
| 1069 | if (start > -1) { |
| 1070 | /* move the whole chunk of valid nodes together */ |
| 1071 | if (set->used != (unsigned)start) { |
| 1072 | memmove(&set->val.nodes[set->used], &set->val.nodes[start], (end - start) * sizeof *set->val.nodes); |
| 1073 | } |
| 1074 | set->used += end - start; |
| 1075 | } |
| 1076 | } |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1077 | } |
| 1078 | |
| 1079 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1080 | * @brief Check for duplicates in a node set. |
| 1081 | * |
| 1082 | * @param[in] set Set to check. |
| 1083 | * @param[in] node Node to look for in @p set. |
| 1084 | * @param[in] node_type Type of @p node. |
| 1085 | * @param[in] skip_idx Index from @p set to skip. |
| 1086 | * @return LY_ERR |
| 1087 | */ |
| 1088 | static LY_ERR |
| 1089 | set_dup_node_check(const struct lyxp_set *set, const struct lyd_node *node, enum lyxp_node_type node_type, int skip_idx) |
| 1090 | { |
| 1091 | uint32_t i; |
| 1092 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1093 | if (set->ht && node) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1094 | return set_dup_node_hash_check(set, (struct lyd_node *)node, node_type, skip_idx); |
| 1095 | } |
| 1096 | |
| 1097 | for (i = 0; i < set->used; ++i) { |
| 1098 | if ((skip_idx > -1) && (i == (unsigned)skip_idx)) { |
| 1099 | continue; |
| 1100 | } |
| 1101 | |
| 1102 | if ((set->val.nodes[i].node == node) && (set->val.nodes[i].type == node_type)) { |
| 1103 | return LY_EEXIST; |
| 1104 | } |
| 1105 | } |
| 1106 | |
| 1107 | return LY_SUCCESS; |
| 1108 | } |
| 1109 | |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 1110 | ly_bool |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1111 | lyxp_set_scnode_contains(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, int skip_idx, |
| 1112 | uint32_t *index_p) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1113 | { |
| 1114 | uint32_t i; |
| 1115 | |
| 1116 | for (i = 0; i < set->used; ++i) { |
| 1117 | if ((skip_idx > -1) && (i == (unsigned)skip_idx)) { |
| 1118 | continue; |
| 1119 | } |
| 1120 | |
| 1121 | 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] | 1122 | if (index_p) { |
| 1123 | *index_p = i; |
| 1124 | } |
| 1125 | return 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1126 | } |
| 1127 | } |
| 1128 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1129 | return 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1130 | } |
| 1131 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 1132 | void |
| 1133 | lyxp_set_scnode_merge(struct lyxp_set *set1, struct lyxp_set *set2) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1134 | { |
| 1135 | uint32_t orig_used, i, j; |
| 1136 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1137 | 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] | 1138 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1139 | if (!set2->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1140 | return; |
| 1141 | } |
| 1142 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1143 | if (!set1->used) { |
aPiecek | adc1e4f | 2021-10-07 11:15:12 +0200 | [diff] [blame] | 1144 | /* release hidden allocated data (lyxp_set.size) */ |
| 1145 | lyxp_set_free_content(set1); |
| 1146 | /* direct copying of the entire structure */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1147 | memcpy(set1, set2, sizeof *set1); |
| 1148 | return; |
| 1149 | } |
| 1150 | |
| 1151 | if (set1->used + set2->used > set1->size) { |
| 1152 | set1->size = set1->used + set2->used; |
| 1153 | set1->val.scnodes = ly_realloc(set1->val.scnodes, set1->size * sizeof *set1->val.scnodes); |
| 1154 | LY_CHECK_ERR_RET(!set1->val.scnodes, LOGMEM(set1->ctx), ); |
| 1155 | } |
| 1156 | |
| 1157 | orig_used = set1->used; |
| 1158 | |
| 1159 | for (i = 0; i < set2->used; ++i) { |
| 1160 | for (j = 0; j < orig_used; ++j) { |
| 1161 | /* detect duplicities */ |
| 1162 | if (set1->val.scnodes[j].scnode == set2->val.scnodes[i].scnode) { |
| 1163 | break; |
| 1164 | } |
| 1165 | } |
| 1166 | |
Michal Vasko | 0587bce | 2020-12-10 12:19:21 +0100 | [diff] [blame] | 1167 | if (j < orig_used) { |
| 1168 | /* node is there, but update its status if needed */ |
| 1169 | if (set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_START_USED) { |
| 1170 | set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx; |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 1171 | } else if ((set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_ATOM_NODE) && |
| 1172 | (set2->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_VAL)) { |
| 1173 | set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx; |
Michal Vasko | 0587bce | 2020-12-10 12:19:21 +0100 | [diff] [blame] | 1174 | } |
| 1175 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1176 | memcpy(&set1->val.scnodes[set1->used], &set2->val.scnodes[i], sizeof *set2->val.scnodes); |
| 1177 | ++set1->used; |
| 1178 | } |
| 1179 | } |
| 1180 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1181 | lyxp_set_free_content(set2); |
| 1182 | set2->type = LYXP_SET_SCNODE_SET; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1183 | } |
| 1184 | |
| 1185 | /** |
| 1186 | * @brief Insert a node into a set. Context position aware. |
| 1187 | * |
| 1188 | * @param[in] set Set to use. |
| 1189 | * @param[in] node Node to insert to @p set. |
| 1190 | * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting. |
| 1191 | * @param[in] node_type Node type of @p node. |
| 1192 | * @param[in] idx Index in @p set to insert into. |
| 1193 | */ |
| 1194 | static void |
| 1195 | set_insert_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx) |
| 1196 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1197 | assert(set && (set->type == LYXP_SET_NODE_SET)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1198 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1199 | if (!set->size) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1200 | /* first item */ |
| 1201 | if (idx) { |
| 1202 | /* no real harm done, but it is a bug */ |
| 1203 | LOGINT(set->ctx); |
| 1204 | idx = 0; |
| 1205 | } |
| 1206 | set->val.nodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.nodes); |
| 1207 | LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), ); |
| 1208 | set->type = LYXP_SET_NODE_SET; |
| 1209 | set->used = 0; |
| 1210 | set->size = LYXP_SET_SIZE_START; |
| 1211 | set->ctx_pos = 1; |
| 1212 | set->ctx_size = 1; |
| 1213 | set->ht = NULL; |
| 1214 | } else { |
| 1215 | /* not an empty set */ |
| 1216 | if (set->used == set->size) { |
| 1217 | |
| 1218 | /* set is full */ |
| 1219 | set->val.nodes = ly_realloc(set->val.nodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.nodes); |
| 1220 | LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), ); |
| 1221 | set->size += LYXP_SET_SIZE_STEP; |
| 1222 | } |
| 1223 | |
| 1224 | if (idx > set->used) { |
| 1225 | LOGINT(set->ctx); |
| 1226 | idx = set->used; |
| 1227 | } |
| 1228 | |
| 1229 | /* make space for the new node */ |
| 1230 | if (idx < set->used) { |
| 1231 | memmove(&set->val.nodes[idx + 1], &set->val.nodes[idx], (set->used - idx) * sizeof *set->val.nodes); |
| 1232 | } |
| 1233 | } |
| 1234 | |
| 1235 | /* finally assign the value */ |
| 1236 | set->val.nodes[idx].node = (struct lyd_node *)node; |
| 1237 | set->val.nodes[idx].type = node_type; |
| 1238 | set->val.nodes[idx].pos = pos; |
| 1239 | ++set->used; |
| 1240 | |
Michal Vasko | 2416fdd | 2021-10-01 11:07:10 +0200 | [diff] [blame] | 1241 | /* add into hash table */ |
| 1242 | set_insert_node_hash(set, (struct lyd_node *)node, node_type); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1243 | } |
| 1244 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1245 | LY_ERR |
Michal Vasko | aac267d | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1246 | lyxp_set_scnode_insert_node(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, |
| 1247 | uint32_t *index_p) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1248 | { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1249 | uint32_t index; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1250 | |
| 1251 | assert(set->type == LYXP_SET_SCNODE_SET); |
| 1252 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1253 | if (lyxp_set_scnode_contains(set, node, node_type, -1, &index)) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1254 | set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1255 | } else { |
| 1256 | if (set->used == set->size) { |
| 1257 | set->val.scnodes = ly_realloc(set->val.scnodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.scnodes); |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1258 | LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), LY_EMEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1259 | set->size += LYXP_SET_SIZE_STEP; |
| 1260 | } |
| 1261 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1262 | index = set->used; |
| 1263 | set->val.scnodes[index].scnode = (struct lysc_node *)node; |
| 1264 | set->val.scnodes[index].type = node_type; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1265 | set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1266 | ++set->used; |
| 1267 | } |
| 1268 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1269 | if (index_p) { |
| 1270 | *index_p = index; |
| 1271 | } |
| 1272 | |
| 1273 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1274 | } |
| 1275 | |
| 1276 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1277 | * @brief Set all nodes with ctx 1 to a new unique context value. |
| 1278 | * |
| 1279 | * @param[in] set Set to modify. |
| 1280 | * @return New context value. |
| 1281 | */ |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 1282 | static int32_t |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1283 | set_scnode_new_in_ctx(struct lyxp_set *set) |
| 1284 | { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 1285 | uint32_t i; |
| 1286 | int32_t ret_ctx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1287 | |
| 1288 | assert(set->type == LYXP_SET_SCNODE_SET); |
| 1289 | |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1290 | ret_ctx = LYXP_SET_SCNODE_ATOM_PRED_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1291 | retry: |
| 1292 | for (i = 0; i < set->used; ++i) { |
| 1293 | if (set->val.scnodes[i].in_ctx >= ret_ctx) { |
| 1294 | ret_ctx = set->val.scnodes[i].in_ctx + 1; |
| 1295 | goto retry; |
| 1296 | } |
| 1297 | } |
| 1298 | for (i = 0; i < set->used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1299 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1300 | set->val.scnodes[i].in_ctx = ret_ctx; |
| 1301 | } |
| 1302 | } |
| 1303 | |
| 1304 | return ret_ctx; |
| 1305 | } |
| 1306 | |
| 1307 | /** |
| 1308 | * @brief Get unique @p node position in the data. |
| 1309 | * |
| 1310 | * @param[in] node Node to find. |
| 1311 | * @param[in] node_type Node type of @p node. |
| 1312 | * @param[in] root Root node. |
| 1313 | * @param[in] root_type Type of the XPath @p root node. |
| 1314 | * @param[in] prev Node that we think is before @p node in DFS from @p root. Can optionally |
| 1315 | * be used to increase efficiency and start the DFS from this node. |
| 1316 | * @param[in] prev_pos Node @p prev position. Optional, but must be set if @p prev is set. |
| 1317 | * @return Node position. |
| 1318 | */ |
| 1319 | static uint32_t |
| 1320 | 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] | 1321 | 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] | 1322 | { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1323 | const struct lyd_node *elem = NULL, *top_sibling; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1324 | uint32_t pos = 1; |
Michal Vasko | fb6c9dd | 2020-11-18 18:18:47 +0100 | [diff] [blame] | 1325 | ly_bool found = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1326 | |
| 1327 | assert(prev && prev_pos && !root->prev->next); |
| 1328 | |
| 1329 | if ((node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ROOT_CONFIG)) { |
| 1330 | return 0; |
| 1331 | } |
| 1332 | |
| 1333 | if (*prev) { |
| 1334 | /* start from the previous element instead from the root */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1335 | pos = *prev_pos; |
Michal Vasko | fb6c9dd | 2020-11-18 18:18:47 +0100 | [diff] [blame] | 1336 | 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] | 1337 | goto dfs_search; |
| 1338 | } |
| 1339 | |
Michal Vasko | fb6c9dd | 2020-11-18 18:18:47 +0100 | [diff] [blame] | 1340 | LY_LIST_FOR(root, top_sibling) { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1341 | LYD_TREE_DFS_BEGIN(top_sibling, elem) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1342 | dfs_search: |
Michal Vasko | a9309bb | 2021-07-09 09:31:55 +0200 | [diff] [blame] | 1343 | LYD_TREE_DFS_continue = 0; |
| 1344 | |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1345 | if (*prev && !elem) { |
| 1346 | /* resume previous DFS */ |
| 1347 | elem = LYD_TREE_DFS_next = (struct lyd_node *)*prev; |
| 1348 | LYD_TREE_DFS_continue = 0; |
| 1349 | } |
| 1350 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1351 | if ((root_type == LYXP_NODE_ROOT_CONFIG) && (elem->schema->flags & LYS_CONFIG_R)) { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1352 | /* skip */ |
| 1353 | LYD_TREE_DFS_continue = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1354 | } else { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1355 | if (elem == node) { |
Michal Vasko | fb6c9dd | 2020-11-18 18:18:47 +0100 | [diff] [blame] | 1356 | found = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1357 | break; |
| 1358 | } |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1359 | ++pos; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1360 | } |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1361 | |
| 1362 | LYD_TREE_DFS_END(top_sibling, elem); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1363 | } |
| 1364 | |
| 1365 | /* node found */ |
Michal Vasko | fb6c9dd | 2020-11-18 18:18:47 +0100 | [diff] [blame] | 1366 | if (found) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1367 | break; |
| 1368 | } |
| 1369 | } |
| 1370 | |
Michal Vasko | fb6c9dd | 2020-11-18 18:18:47 +0100 | [diff] [blame] | 1371 | if (!found) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1372 | if (!(*prev)) { |
| 1373 | /* we went from root and failed to find it, cannot be */ |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1374 | LOGINT(LYD_CTX(node)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1375 | return 0; |
| 1376 | } else { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1377 | /* start the search again from the beginning */ |
| 1378 | *prev = root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1379 | |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1380 | top_sibling = root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1381 | pos = 1; |
| 1382 | goto dfs_search; |
| 1383 | } |
| 1384 | } |
| 1385 | |
| 1386 | /* remember the last found node for next time */ |
| 1387 | *prev = node; |
| 1388 | *prev_pos = pos; |
| 1389 | |
| 1390 | return pos; |
| 1391 | } |
| 1392 | |
| 1393 | /** |
| 1394 | * @brief Assign (fill) missing node positions. |
| 1395 | * |
| 1396 | * @param[in] set Set to fill positions in. |
| 1397 | * @param[in] root Context root node. |
| 1398 | * @param[in] root_type Context root type. |
| 1399 | * @return LY_ERR |
| 1400 | */ |
| 1401 | static LY_ERR |
| 1402 | set_assign_pos(struct lyxp_set *set, const struct lyd_node *root, enum lyxp_node_type root_type) |
| 1403 | { |
| 1404 | const struct lyd_node *prev = NULL, *tmp_node; |
| 1405 | uint32_t i, tmp_pos = 0; |
| 1406 | |
| 1407 | for (i = 0; i < set->used; ++i) { |
| 1408 | if (!set->val.nodes[i].pos) { |
| 1409 | tmp_node = NULL; |
| 1410 | switch (set->val.nodes[i].type) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1411 | case LYXP_NODE_META: |
| 1412 | tmp_node = set->val.meta[i].meta->parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1413 | if (!tmp_node) { |
| 1414 | LOGINT_RET(root->schema->module->ctx); |
| 1415 | } |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1416 | /* fall through */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1417 | case LYXP_NODE_ELEM: |
| 1418 | case LYXP_NODE_TEXT: |
| 1419 | if (!tmp_node) { |
| 1420 | tmp_node = set->val.nodes[i].node; |
| 1421 | } |
| 1422 | set->val.nodes[i].pos = get_node_pos(tmp_node, set->val.nodes[i].type, root, root_type, &prev, &tmp_pos); |
| 1423 | break; |
| 1424 | default: |
| 1425 | /* all roots have position 0 */ |
| 1426 | break; |
| 1427 | } |
| 1428 | } |
| 1429 | } |
| 1430 | |
| 1431 | return LY_SUCCESS; |
| 1432 | } |
| 1433 | |
| 1434 | /** |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1435 | * @brief Get unique @p meta position in the parent metadata. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1436 | * |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1437 | * @param[in] meta Metadata to use. |
| 1438 | * @return Metadata position. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1439 | */ |
| 1440 | static uint16_t |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1441 | get_meta_pos(struct lyd_meta *meta) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1442 | { |
| 1443 | uint16_t pos = 0; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1444 | struct lyd_meta *meta2; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1445 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1446 | for (meta2 = meta->parent->meta; meta2 && (meta2 != meta); meta2 = meta2->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1447 | ++pos; |
| 1448 | } |
| 1449 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1450 | assert(meta2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1451 | return pos; |
| 1452 | } |
| 1453 | |
| 1454 | /** |
| 1455 | * @brief Compare 2 nodes in respect to XPath document order. |
| 1456 | * |
| 1457 | * @param[in] item1 1st node. |
| 1458 | * @param[in] item2 2nd node. |
| 1459 | * @return If 1st > 2nd returns 1, 1st == 2nd returns 0, and 1st < 2nd returns -1. |
| 1460 | */ |
| 1461 | static int |
| 1462 | set_sort_compare(struct lyxp_set_node *item1, struct lyxp_set_node *item2) |
| 1463 | { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1464 | uint32_t meta_pos1 = 0, meta_pos2 = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1465 | |
| 1466 | if (item1->pos < item2->pos) { |
| 1467 | return -1; |
| 1468 | } |
| 1469 | |
| 1470 | if (item1->pos > item2->pos) { |
| 1471 | return 1; |
| 1472 | } |
| 1473 | |
| 1474 | /* node positions are equal, the fun case */ |
| 1475 | |
| 1476 | /* 1st ELEM - == - 2nd TEXT, 1st TEXT - == - 2nd ELEM */ |
| 1477 | /* special case since text nodes are actually saved as their parents */ |
| 1478 | if ((item1->node == item2->node) && (item1->type != item2->type)) { |
| 1479 | if (item1->type == LYXP_NODE_ELEM) { |
| 1480 | assert(item2->type == LYXP_NODE_TEXT); |
| 1481 | return -1; |
| 1482 | } else { |
| 1483 | assert((item1->type == LYXP_NODE_TEXT) && (item2->type == LYXP_NODE_ELEM)); |
| 1484 | return 1; |
| 1485 | } |
| 1486 | } |
| 1487 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1488 | /* we need meta positions now */ |
| 1489 | if (item1->type == LYXP_NODE_META) { |
| 1490 | meta_pos1 = get_meta_pos((struct lyd_meta *)item1->node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1491 | } |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1492 | if (item2->type == LYXP_NODE_META) { |
| 1493 | meta_pos2 = get_meta_pos((struct lyd_meta *)item2->node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1494 | } |
| 1495 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1496 | /* 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] | 1497 | /* check for duplicates */ |
| 1498 | if (item1->node == item2->node) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1499 | 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] | 1500 | return 0; |
| 1501 | } |
| 1502 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1503 | /* 1st ELEM - 2nd TEXT, 1st ELEM - any pos - 2nd META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1504 | /* elem is always first, 2nd node is after it */ |
| 1505 | if (item1->type == LYXP_NODE_ELEM) { |
| 1506 | assert(item2->type != LYXP_NODE_ELEM); |
| 1507 | return -1; |
| 1508 | } |
| 1509 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1510 | /* 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] | 1511 | /* 2nd is before 1st */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1512 | if (((item1->type == LYXP_NODE_TEXT) && |
| 1513 | ((item2->type == LYXP_NODE_ELEM) || (item2->type == LYXP_NODE_META))) || |
| 1514 | ((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_ELEM)) || |
| 1515 | (((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_META)) && |
| 1516 | (meta_pos1 > meta_pos2))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1517 | return 1; |
| 1518 | } |
| 1519 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1520 | /* 1st META - any pos - 2nd TEXT, 1st META <pos< - 2nd META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1521 | /* 2nd is after 1st */ |
| 1522 | return -1; |
| 1523 | } |
| 1524 | |
| 1525 | /** |
| 1526 | * @brief Set cast for comparisons. |
| 1527 | * |
| 1528 | * @param[in] trg Target set to cast source into. |
| 1529 | * @param[in] src Source set. |
| 1530 | * @param[in] type Target set type. |
| 1531 | * @param[in] src_idx Source set node index. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1532 | * @return LY_ERR |
| 1533 | */ |
| 1534 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1535 | set_comp_cast(struct lyxp_set *trg, struct lyxp_set *src, enum lyxp_set_type type, uint32_t src_idx) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1536 | { |
| 1537 | assert(src->type == LYXP_SET_NODE_SET); |
| 1538 | |
| 1539 | set_init(trg, src); |
| 1540 | |
| 1541 | /* insert node into target set */ |
| 1542 | set_insert_node(trg, src->val.nodes[src_idx].node, src->val.nodes[src_idx].pos, src->val.nodes[src_idx].type, 0); |
| 1543 | |
| 1544 | /* cast target set appropriately */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1545 | return lyxp_set_cast(trg, type); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1546 | } |
| 1547 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1548 | /** |
| 1549 | * @brief Set content canonization for comparisons. |
| 1550 | * |
| 1551 | * @param[in] trg Target set to put the canononized source into. |
| 1552 | * @param[in] src Source set. |
| 1553 | * @param[in] xp_node Source XPath node/meta to use for canonization. |
| 1554 | * @return LY_ERR |
| 1555 | */ |
| 1556 | static LY_ERR |
| 1557 | set_comp_canonize(struct lyxp_set *trg, const struct lyxp_set *src, const struct lyxp_set_node *xp_node) |
| 1558 | { |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 1559 | const struct lysc_type *type = NULL; |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1560 | struct lyd_value val; |
| 1561 | struct ly_err_item *err = NULL; |
| 1562 | char *str, *ptr; |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1563 | LY_ERR rc; |
| 1564 | |
| 1565 | /* is there anything to canonize even? */ |
| 1566 | if ((src->type == LYXP_SET_NUMBER) || (src->type == LYXP_SET_STRING)) { |
| 1567 | /* do we have a type to use for canonization? */ |
| 1568 | if ((xp_node->type == LYXP_NODE_ELEM) && (xp_node->node->schema->nodetype & LYD_NODE_TERM)) { |
| 1569 | type = ((struct lyd_node_term *)xp_node->node)->value.realtype; |
| 1570 | } else if (xp_node->type == LYXP_NODE_META) { |
| 1571 | type = ((struct lyd_meta *)xp_node->node)->value.realtype; |
| 1572 | } |
| 1573 | } |
| 1574 | if (!type) { |
| 1575 | goto fill; |
| 1576 | } |
| 1577 | |
| 1578 | if (src->type == LYXP_SET_NUMBER) { |
| 1579 | /* canonize number */ |
| 1580 | if (asprintf(&str, "%Lf", src->val.num) == -1) { |
| 1581 | LOGMEM(src->ctx); |
| 1582 | return LY_EMEM; |
| 1583 | } |
| 1584 | } else { |
| 1585 | /* canonize string */ |
| 1586 | str = strdup(src->val.str); |
| 1587 | } |
| 1588 | |
| 1589 | /* ignore errors, the value may not satisfy schema constraints */ |
Radek Krejci | 0b01330 | 2021-03-29 15:22:32 +0200 | [diff] [blame] | 1590 | rc = type->plugin->store(src->ctx, type, str, strlen(str), LYPLG_TYPE_STORE_DYNAMIC, src->format, src->prefix_data, |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 1591 | LYD_HINT_DATA, xp_node->node->schema, &val, NULL, &err); |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1592 | ly_err_free(err); |
| 1593 | if (rc) { |
| 1594 | /* invalid value */ |
Radek Iša | 4846022 | 2021-03-02 15:18:32 +0100 | [diff] [blame] | 1595 | /* function store automaticaly dealloc value when fail */ |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1596 | goto fill; |
| 1597 | } |
| 1598 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1599 | /* use the canonized value */ |
| 1600 | set_init(trg, src); |
| 1601 | trg->type = src->type; |
| 1602 | if (src->type == LYXP_SET_NUMBER) { |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 1603 | trg->val.num = strtold(type->plugin->print(src->ctx, &val, LY_VALUE_CANON, NULL, NULL, NULL), &ptr); |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1604 | LY_CHECK_ERR_RET(ptr[0], LOGINT(src->ctx), LY_EINT); |
| 1605 | } else { |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 1606 | trg->val.str = strdup(type->plugin->print(src->ctx, &val, LY_VALUE_CANON, NULL, NULL, NULL)); |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1607 | } |
| 1608 | type->plugin->free(src->ctx, &val); |
| 1609 | return LY_SUCCESS; |
| 1610 | |
| 1611 | fill: |
| 1612 | /* no canonization needed/possible */ |
| 1613 | set_fill_set(trg, src); |
| 1614 | return LY_SUCCESS; |
| 1615 | } |
| 1616 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1617 | #ifndef NDEBUG |
| 1618 | |
| 1619 | /** |
| 1620 | * @brief Bubble sort @p set into XPath document order. |
| 1621 | * Context position aware. Unused in the 'Release' build target. |
| 1622 | * |
| 1623 | * @param[in] set Set to sort. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1624 | * @return How many times the whole set was traversed - 1 (if set was sorted, returns 0). |
| 1625 | */ |
| 1626 | static int |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1627 | set_sort(struct lyxp_set *set) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1628 | { |
| 1629 | uint32_t i, j; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 1630 | int ret = 0, cmp; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 1631 | ly_bool inverted, change; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1632 | const struct lyd_node *root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1633 | struct lyxp_set_node item; |
| 1634 | struct lyxp_set_hash_node hnode; |
| 1635 | uint64_t hash; |
| 1636 | |
Michal Vasko | 3cf8fbf | 2020-05-27 15:21:21 +0200 | [diff] [blame] | 1637 | if ((set->type != LYXP_SET_NODE_SET) || (set->used < 2)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1638 | return 0; |
| 1639 | } |
| 1640 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1641 | /* find first top-level node to be used as anchor for positions */ |
Michal Vasko | 0245d50 | 2021-12-13 17:05:06 +0100 | [diff] [blame] | 1642 | for (root = set->tree; root->parent; root = lyd_parent(root)) {} |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 1643 | for ( ; root->prev->next; root = root->prev) {} |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1644 | |
| 1645 | /* fill positions */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1646 | if (set_assign_pos(set, root, set->root_type)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1647 | return -1; |
| 1648 | } |
| 1649 | |
| 1650 | LOGDBG(LY_LDGXPATH, "SORT BEGIN"); |
| 1651 | print_set_debug(set); |
| 1652 | |
| 1653 | for (i = 0; i < set->used; ++i) { |
| 1654 | inverted = 0; |
| 1655 | change = 0; |
| 1656 | |
| 1657 | for (j = 1; j < set->used - i; ++j) { |
| 1658 | /* compare node positions */ |
| 1659 | if (inverted) { |
| 1660 | cmp = set_sort_compare(&set->val.nodes[j], &set->val.nodes[j - 1]); |
| 1661 | } else { |
| 1662 | cmp = set_sort_compare(&set->val.nodes[j - 1], &set->val.nodes[j]); |
| 1663 | } |
| 1664 | |
| 1665 | /* swap if needed */ |
| 1666 | if ((inverted && (cmp < 0)) || (!inverted && (cmp > 0))) { |
| 1667 | change = 1; |
| 1668 | |
| 1669 | item = set->val.nodes[j - 1]; |
| 1670 | set->val.nodes[j - 1] = set->val.nodes[j]; |
| 1671 | set->val.nodes[j] = item; |
| 1672 | } else { |
| 1673 | /* whether node_pos1 should be smaller than node_pos2 or the other way around */ |
| 1674 | inverted = !inverted; |
| 1675 | } |
| 1676 | } |
| 1677 | |
| 1678 | ++ret; |
| 1679 | |
| 1680 | if (!change) { |
| 1681 | break; |
| 1682 | } |
| 1683 | } |
| 1684 | |
| 1685 | LOGDBG(LY_LDGXPATH, "SORT END %d", ret); |
| 1686 | print_set_debug(set); |
| 1687 | |
| 1688 | /* check node hashes */ |
| 1689 | if (set->used >= LYD_HT_MIN_ITEMS) { |
| 1690 | assert(set->ht); |
| 1691 | for (i = 0; i < set->used; ++i) { |
| 1692 | hnode.node = set->val.nodes[i].node; |
| 1693 | hnode.type = set->val.nodes[i].type; |
| 1694 | |
| 1695 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 1696 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 1697 | hash = dict_hash_multi(hash, NULL, 0); |
| 1698 | |
| 1699 | assert(!lyht_find(set->ht, &hnode, hash, NULL)); |
| 1700 | } |
| 1701 | } |
| 1702 | |
| 1703 | return ret - 1; |
| 1704 | } |
| 1705 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1706 | #endif |
| 1707 | |
| 1708 | /** |
| 1709 | * @brief Merge 2 sorted sets into one. |
| 1710 | * |
| 1711 | * @param[in,out] trg Set to merge into. Duplicates are removed. |
| 1712 | * @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] | 1713 | * @return LY_ERR |
| 1714 | */ |
| 1715 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1716 | set_sorted_merge(struct lyxp_set *trg, struct lyxp_set *src) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1717 | { |
| 1718 | uint32_t i, j, k, count, dup_count; |
| 1719 | int cmp; |
| 1720 | const struct lyd_node *root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1721 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1722 | 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] | 1723 | return LY_EINVAL; |
| 1724 | } |
| 1725 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1726 | if (!src->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1727 | return LY_SUCCESS; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1728 | } else if (!trg->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1729 | set_fill_set(trg, src); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1730 | lyxp_set_free_content(src); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1731 | return LY_SUCCESS; |
| 1732 | } |
| 1733 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1734 | /* find first top-level node to be used as anchor for positions */ |
Michal Vasko | 80a5b0c | 2021-12-13 17:13:09 +0100 | [diff] [blame] | 1735 | for (root = trg->tree; root->parent; root = lyd_parent(root)) {} |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 1736 | for ( ; root->prev->next; root = root->prev) {} |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1737 | |
| 1738 | /* fill positions */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1739 | 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] | 1740 | return LY_EINT; |
| 1741 | } |
| 1742 | |
| 1743 | #ifndef NDEBUG |
| 1744 | LOGDBG(LY_LDGXPATH, "MERGE target"); |
| 1745 | print_set_debug(trg); |
| 1746 | LOGDBG(LY_LDGXPATH, "MERGE source"); |
| 1747 | print_set_debug(src); |
| 1748 | #endif |
| 1749 | |
| 1750 | /* make memory for the merge (duplicates are not detected yet, so space |
| 1751 | * will likely be wasted on them, too bad) */ |
| 1752 | if (trg->size - trg->used < src->used) { |
| 1753 | trg->size = trg->used + src->used; |
| 1754 | |
| 1755 | trg->val.nodes = ly_realloc(trg->val.nodes, trg->size * sizeof *trg->val.nodes); |
| 1756 | LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx), LY_EMEM); |
| 1757 | } |
| 1758 | |
| 1759 | i = 0; |
| 1760 | j = 0; |
| 1761 | count = 0; |
| 1762 | dup_count = 0; |
| 1763 | do { |
| 1764 | cmp = set_sort_compare(&src->val.nodes[i], &trg->val.nodes[j]); |
| 1765 | if (!cmp) { |
| 1766 | if (!count) { |
| 1767 | /* duplicate, just skip it */ |
| 1768 | ++i; |
| 1769 | ++j; |
| 1770 | } else { |
| 1771 | /* we are copying something already, so let's copy the duplicate too, |
| 1772 | * we are hoping that afterwards there are some more nodes to |
| 1773 | * copy and this way we can copy them all together */ |
| 1774 | ++count; |
| 1775 | ++dup_count; |
| 1776 | ++i; |
| 1777 | ++j; |
| 1778 | } |
| 1779 | } else if (cmp < 0) { |
| 1780 | /* inserting src node into trg, just remember it for now */ |
| 1781 | ++count; |
| 1782 | ++i; |
| 1783 | |
| 1784 | /* insert the hash now */ |
| 1785 | set_insert_node_hash(trg, src->val.nodes[i - 1].node, src->val.nodes[i - 1].type); |
| 1786 | } else if (count) { |
| 1787 | copy_nodes: |
| 1788 | /* time to actually copy the nodes, we have found the largest block of nodes */ |
| 1789 | memmove(&trg->val.nodes[j + (count - dup_count)], |
| 1790 | &trg->val.nodes[j], |
| 1791 | (trg->used - j) * sizeof *trg->val.nodes); |
| 1792 | memcpy(&trg->val.nodes[j - dup_count], &src->val.nodes[i - count], count * sizeof *src->val.nodes); |
| 1793 | |
| 1794 | trg->used += count - dup_count; |
| 1795 | /* do not change i, except the copying above, we are basically doing exactly what is in the else branch below */ |
| 1796 | j += count - dup_count; |
| 1797 | |
| 1798 | count = 0; |
| 1799 | dup_count = 0; |
| 1800 | } else { |
| 1801 | ++j; |
| 1802 | } |
| 1803 | } while ((i < src->used) && (j < trg->used)); |
| 1804 | |
| 1805 | if ((i < src->used) || count) { |
| 1806 | /* insert all the hashes first */ |
| 1807 | for (k = i; k < src->used; ++k) { |
| 1808 | set_insert_node_hash(trg, src->val.nodes[k].node, src->val.nodes[k].type); |
| 1809 | } |
| 1810 | |
| 1811 | /* loop ended, but we need to copy something at trg end */ |
| 1812 | count += src->used - i; |
| 1813 | i = src->used; |
| 1814 | goto copy_nodes; |
| 1815 | } |
| 1816 | |
| 1817 | /* we are inserting hashes before the actual node insert, which causes |
| 1818 | * situations when there were initially not enough items for a hash table, |
| 1819 | * but even after some were inserted, hash table was not created (during |
| 1820 | * insertion the number of items is not updated yet) */ |
| 1821 | if (!trg->ht && (trg->used >= LYD_HT_MIN_ITEMS)) { |
| 1822 | set_insert_node_hash(trg, NULL, 0); |
| 1823 | } |
| 1824 | |
| 1825 | #ifndef NDEBUG |
| 1826 | LOGDBG(LY_LDGXPATH, "MERGE result"); |
| 1827 | print_set_debug(trg); |
| 1828 | #endif |
| 1829 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1830 | lyxp_set_free_content(src); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1831 | return LY_SUCCESS; |
| 1832 | } |
| 1833 | |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1834 | LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1835 | lyxp_check_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t tok_idx, enum lyxp_token want_tok) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1836 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1837 | if (exp->used == tok_idx) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1838 | if (ctx) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1839 | LOGVAL(ctx, LY_VCODE_XP_EOF); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1840 | } |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1841 | return LY_EINCOMPLETE; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1842 | } |
| 1843 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1844 | if (want_tok && (exp->tokens[tok_idx] != want_tok)) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1845 | if (ctx) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1846 | LOGVAL(ctx, LY_VCODE_XP_INTOK2, lyxp_print_token(exp->tokens[tok_idx]), |
Michal Vasko | 0b468e6 | 2020-10-19 09:33:04 +0200 | [diff] [blame] | 1847 | &exp->expr[exp->tok_pos[tok_idx]], lyxp_print_token(want_tok)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1848 | } |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1849 | return LY_ENOT; |
| 1850 | } |
| 1851 | |
| 1852 | return LY_SUCCESS; |
| 1853 | } |
| 1854 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1855 | LY_ERR |
| 1856 | lyxp_next_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok) |
| 1857 | { |
| 1858 | LY_CHECK_RET(lyxp_check_token(ctx, exp, *tok_idx, want_tok)); |
| 1859 | |
| 1860 | /* skip the token */ |
| 1861 | ++(*tok_idx); |
| 1862 | |
| 1863 | return LY_SUCCESS; |
| 1864 | } |
| 1865 | |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1866 | /* just like lyxp_check_token() but tests for 2 tokens */ |
| 1867 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 1868 | exp_check_token2(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t tok_idx, enum lyxp_token want_tok1, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1869 | enum lyxp_token want_tok2) |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1870 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1871 | if (exp->used == tok_idx) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1872 | if (ctx) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1873 | LOGVAL(ctx, LY_VCODE_XP_EOF); |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1874 | } |
| 1875 | return LY_EINCOMPLETE; |
| 1876 | } |
| 1877 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1878 | 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] | 1879 | if (ctx) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1880 | LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[tok_idx]), |
Michal Vasko | 0b468e6 | 2020-10-19 09:33:04 +0200 | [diff] [blame] | 1881 | &exp->expr[exp->tok_pos[tok_idx]]); |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1882 | } |
| 1883 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1884 | } |
| 1885 | |
| 1886 | return LY_SUCCESS; |
| 1887 | } |
| 1888 | |
Michal Vasko | 4911eeb | 2021-06-28 11:23:05 +0200 | [diff] [blame] | 1889 | LY_ERR |
| 1890 | lyxp_next_token2(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok1, |
| 1891 | enum lyxp_token want_tok2) |
| 1892 | { |
| 1893 | LY_CHECK_RET(exp_check_token2(ctx, exp, *tok_idx, want_tok1, want_tok2)); |
| 1894 | |
| 1895 | /* skip the token */ |
| 1896 | ++(*tok_idx); |
| 1897 | |
| 1898 | return LY_SUCCESS; |
| 1899 | } |
| 1900 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1901 | /** |
| 1902 | * @brief Stack operation push on the repeat array. |
| 1903 | * |
| 1904 | * @param[in] exp Expression to use. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1905 | * @param[in] tok_idx Position in the expresion \p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1906 | * @param[in] repeat_op_idx Index from \p exp of the operator token. This value is pushed. |
| 1907 | */ |
| 1908 | static void |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1909 | exp_repeat_push(struct lyxp_expr *exp, uint16_t tok_idx, uint16_t repeat_op_idx) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1910 | { |
| 1911 | uint16_t i; |
| 1912 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1913 | if (exp->repeat[tok_idx]) { |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 1914 | for (i = 0; exp->repeat[tok_idx][i]; ++i) {} |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1915 | exp->repeat[tok_idx] = realloc(exp->repeat[tok_idx], (i + 2) * sizeof *exp->repeat[tok_idx]); |
| 1916 | LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), ); |
| 1917 | exp->repeat[tok_idx][i] = repeat_op_idx; |
| 1918 | exp->repeat[tok_idx][i + 1] = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1919 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1920 | exp->repeat[tok_idx] = calloc(2, sizeof *exp->repeat[tok_idx]); |
| 1921 | LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), ); |
| 1922 | exp->repeat[tok_idx][0] = repeat_op_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1923 | } |
| 1924 | } |
| 1925 | |
| 1926 | /** |
| 1927 | * @brief Reparse Predicate. Logs directly on error. |
| 1928 | * |
| 1929 | * [7] Predicate ::= '[' Expr ']' |
| 1930 | * |
| 1931 | * @param[in] ctx Context for logging. |
| 1932 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1933 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 1934 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1935 | * @return LY_ERR |
| 1936 | */ |
| 1937 | static LY_ERR |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 1938 | reparse_predicate(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1939 | { |
| 1940 | LY_ERR rc; |
| 1941 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1942 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1943 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1944 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1945 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 1946 | rc = reparse_or_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1947 | LY_CHECK_RET(rc); |
| 1948 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1949 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1950 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1951 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1952 | |
| 1953 | return LY_SUCCESS; |
| 1954 | } |
| 1955 | |
| 1956 | /** |
| 1957 | * @brief Reparse RelativeLocationPath. Logs directly on error. |
| 1958 | * |
| 1959 | * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step |
| 1960 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 1961 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 1962 | * |
| 1963 | * @param[in] ctx Context for logging. |
| 1964 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1965 | * @param[in] tok_idx Position in the expression \p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 1966 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1967 | * @return LY_ERR (LY_EINCOMPLETE on forward reference) |
| 1968 | */ |
| 1969 | static LY_ERR |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 1970 | reparse_relative_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1971 | { |
| 1972 | LY_ERR rc; |
| 1973 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1974 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1975 | LY_CHECK_RET(rc); |
| 1976 | |
| 1977 | goto step; |
| 1978 | do { |
| 1979 | /* '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1980 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1981 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1982 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1983 | LY_CHECK_RET(rc); |
| 1984 | step: |
| 1985 | /* Step */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1986 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1987 | case LYXP_TOKEN_DOT: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1988 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1989 | break; |
| 1990 | |
| 1991 | case LYXP_TOKEN_DDOT: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1992 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1993 | break; |
| 1994 | |
| 1995 | case LYXP_TOKEN_AT: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1996 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1997 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1998 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1999 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2000 | if ((exp->tokens[*tok_idx] != LYXP_TOKEN_NAMETEST) && (exp->tokens[*tok_idx] != LYXP_TOKEN_NODETYPE)) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2001 | LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2002 | return LY_EVALID; |
| 2003 | } |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 2004 | /* fall through */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2005 | case LYXP_TOKEN_NAMETEST: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2006 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2007 | goto reparse_predicate; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2008 | |
| 2009 | case LYXP_TOKEN_NODETYPE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2010 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2011 | |
| 2012 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2013 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2014 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2015 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2016 | |
| 2017 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2018 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2019 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2020 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2021 | |
| 2022 | reparse_predicate: |
| 2023 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2024 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2025 | rc = reparse_predicate(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2026 | LY_CHECK_RET(rc); |
| 2027 | } |
| 2028 | break; |
| 2029 | default: |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2030 | LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2031 | return LY_EVALID; |
| 2032 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2033 | } 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] | 2034 | |
| 2035 | return LY_SUCCESS; |
| 2036 | } |
| 2037 | |
| 2038 | /** |
| 2039 | * @brief Reparse AbsoluteLocationPath. Logs directly on error. |
| 2040 | * |
| 2041 | * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath |
| 2042 | * |
| 2043 | * @param[in] ctx Context for logging. |
| 2044 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2045 | * @param[in] tok_idx Position in the expression \p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2046 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2047 | * @return LY_ERR |
| 2048 | */ |
| 2049 | static LY_ERR |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2050 | reparse_absolute_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2051 | { |
| 2052 | LY_ERR rc; |
| 2053 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2054 | 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] | 2055 | |
| 2056 | /* '/' RelativeLocationPath? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2057 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2058 | /* '/' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2059 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2060 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2061 | if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2062 | return LY_SUCCESS; |
| 2063 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2064 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2065 | case LYXP_TOKEN_DOT: |
| 2066 | case LYXP_TOKEN_DDOT: |
| 2067 | case LYXP_TOKEN_AT: |
| 2068 | case LYXP_TOKEN_NAMETEST: |
| 2069 | case LYXP_TOKEN_NODETYPE: |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2070 | rc = reparse_relative_location_path(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2071 | LY_CHECK_RET(rc); |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 2072 | /* fall through */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2073 | default: |
| 2074 | break; |
| 2075 | } |
| 2076 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2077 | } else { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 2078 | /* '//' RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2079 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2080 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2081 | rc = reparse_relative_location_path(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2082 | LY_CHECK_RET(rc); |
| 2083 | } |
| 2084 | |
| 2085 | return LY_SUCCESS; |
| 2086 | } |
| 2087 | |
| 2088 | /** |
| 2089 | * @brief Reparse FunctionCall. Logs directly on error. |
| 2090 | * |
| 2091 | * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')' |
| 2092 | * |
| 2093 | * @param[in] ctx Context for logging. |
| 2094 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2095 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2096 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2097 | * @return LY_ERR |
| 2098 | */ |
| 2099 | static LY_ERR |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2100 | reparse_function_call(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2101 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2102 | int8_t min_arg_count = -1; |
| 2103 | uint32_t arg_count, max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2104 | uint16_t func_tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2105 | LY_ERR rc; |
| 2106 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2107 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2108 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2109 | func_tok_idx = *tok_idx; |
| 2110 | switch (exp->tok_len[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2111 | case 3: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2112 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2113 | min_arg_count = 1; |
| 2114 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2115 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2116 | min_arg_count = 1; |
| 2117 | max_arg_count = 1; |
| 2118 | } |
| 2119 | break; |
| 2120 | case 4: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2121 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2122 | min_arg_count = 1; |
| 2123 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2124 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2125 | min_arg_count = 0; |
| 2126 | max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2127 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2128 | min_arg_count = 0; |
| 2129 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2130 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2131 | min_arg_count = 0; |
| 2132 | max_arg_count = 0; |
| 2133 | } |
| 2134 | break; |
| 2135 | case 5: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2136 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2137 | min_arg_count = 1; |
| 2138 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2139 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2140 | min_arg_count = 0; |
| 2141 | max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2142 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2143 | min_arg_count = 1; |
| 2144 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2145 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2146 | min_arg_count = 1; |
| 2147 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2148 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2149 | min_arg_count = 1; |
| 2150 | max_arg_count = 1; |
| 2151 | } |
| 2152 | break; |
| 2153 | case 6: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2154 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2155 | min_arg_count = 2; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2156 | max_arg_count = UINT32_MAX; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2157 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2158 | min_arg_count = 0; |
| 2159 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2160 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2161 | min_arg_count = 0; |
| 2162 | max_arg_count = 1; |
| 2163 | } |
| 2164 | break; |
| 2165 | case 7: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2166 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2167 | min_arg_count = 1; |
| 2168 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2169 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2170 | min_arg_count = 1; |
| 2171 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2172 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2173 | min_arg_count = 0; |
| 2174 | max_arg_count = 0; |
| 2175 | } |
| 2176 | break; |
| 2177 | case 8: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2178 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2179 | min_arg_count = 2; |
| 2180 | max_arg_count = 2; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2181 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2182 | min_arg_count = 0; |
| 2183 | max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2184 | } 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] | 2185 | min_arg_count = 2; |
| 2186 | max_arg_count = 2; |
| 2187 | } |
| 2188 | break; |
| 2189 | case 9: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2190 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2191 | min_arg_count = 2; |
| 2192 | max_arg_count = 3; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2193 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2194 | min_arg_count = 3; |
| 2195 | max_arg_count = 3; |
| 2196 | } |
| 2197 | break; |
| 2198 | case 10: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2199 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2200 | min_arg_count = 0; |
| 2201 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2202 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2203 | min_arg_count = 1; |
| 2204 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2205 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2206 | min_arg_count = 2; |
| 2207 | max_arg_count = 2; |
| 2208 | } |
| 2209 | break; |
| 2210 | case 11: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2211 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2212 | min_arg_count = 2; |
| 2213 | max_arg_count = 2; |
| 2214 | } |
| 2215 | break; |
| 2216 | case 12: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2217 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2218 | min_arg_count = 2; |
| 2219 | max_arg_count = 2; |
| 2220 | } |
| 2221 | break; |
| 2222 | case 13: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2223 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2224 | min_arg_count = 0; |
| 2225 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2226 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2227 | min_arg_count = 0; |
| 2228 | max_arg_count = 1; |
| 2229 | } |
| 2230 | break; |
| 2231 | case 15: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2232 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2233 | min_arg_count = 0; |
| 2234 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2235 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2236 | min_arg_count = 2; |
| 2237 | max_arg_count = 2; |
| 2238 | } |
| 2239 | break; |
| 2240 | case 16: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2241 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2242 | min_arg_count = 2; |
| 2243 | max_arg_count = 2; |
| 2244 | } |
| 2245 | break; |
| 2246 | case 20: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2247 | 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] | 2248 | min_arg_count = 2; |
| 2249 | max_arg_count = 2; |
| 2250 | } |
| 2251 | break; |
| 2252 | } |
| 2253 | if (min_arg_count == -1) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2254 | LOGVAL(ctx, LY_VCODE_XP_INFUNC, exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2255 | return LY_EINVAL; |
| 2256 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2257 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2258 | |
| 2259 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2260 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2261 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2262 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2263 | |
| 2264 | /* ( Expr ( ',' Expr )* )? */ |
| 2265 | arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2266 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2267 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2268 | if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2269 | ++arg_count; |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2270 | rc = reparse_or_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2271 | LY_CHECK_RET(rc); |
| 2272 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2273 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) { |
| 2274 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2275 | |
| 2276 | ++arg_count; |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2277 | rc = reparse_or_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2278 | LY_CHECK_RET(rc); |
| 2279 | } |
| 2280 | |
| 2281 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2282 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2283 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2284 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2285 | |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 2286 | if ((arg_count < (uint32_t)min_arg_count) || (arg_count > max_arg_count)) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2287 | LOGVAL(ctx, LY_VCODE_XP_INARGCOUNT, arg_count, exp->tok_len[func_tok_idx], &exp->expr[exp->tok_pos[func_tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2288 | return LY_EVALID; |
| 2289 | } |
| 2290 | |
| 2291 | return LY_SUCCESS; |
| 2292 | } |
| 2293 | |
| 2294 | /** |
| 2295 | * @brief Reparse PathExpr. Logs directly on error. |
| 2296 | * |
| 2297 | * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate* |
| 2298 | * | PrimaryExpr Predicate* '/' RelativeLocationPath |
| 2299 | * | PrimaryExpr Predicate* '//' RelativeLocationPath |
| 2300 | * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 2301 | * [8] PrimaryExpr ::= VariableReference | '(' Expr ')' | Literal | Number | FunctionCall |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2302 | * |
| 2303 | * @param[in] ctx Context for logging. |
| 2304 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2305 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2306 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2307 | * @return LY_ERR |
| 2308 | */ |
| 2309 | static LY_ERR |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2310 | reparse_path_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2311 | { |
| 2312 | LY_ERR rc; |
| 2313 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2314 | if (lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 2315 | return LY_EVALID; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2316 | } |
| 2317 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2318 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2319 | case LYXP_TOKEN_PAR1: |
| 2320 | /* '(' Expr ')' Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2321 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2322 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2323 | rc = reparse_or_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2324 | LY_CHECK_RET(rc); |
| 2325 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2326 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2327 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2328 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2329 | goto predicate; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2330 | case LYXP_TOKEN_DOT: |
| 2331 | case LYXP_TOKEN_DDOT: |
| 2332 | case LYXP_TOKEN_AT: |
| 2333 | case LYXP_TOKEN_NAMETEST: |
| 2334 | case LYXP_TOKEN_NODETYPE: |
| 2335 | /* RelativeLocationPath */ |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2336 | rc = reparse_relative_location_path(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2337 | LY_CHECK_RET(rc); |
| 2338 | break; |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 2339 | case LYXP_TOKEN_VARREF: |
| 2340 | /* VariableReference */ |
| 2341 | ++(*tok_idx); |
| 2342 | goto predicate; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2343 | case LYXP_TOKEN_FUNCNAME: |
| 2344 | /* FunctionCall */ |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2345 | rc = reparse_function_call(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2346 | LY_CHECK_RET(rc); |
| 2347 | goto predicate; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2348 | case LYXP_TOKEN_OPER_PATH: |
| 2349 | case LYXP_TOKEN_OPER_RPATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2350 | /* AbsoluteLocationPath */ |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2351 | rc = reparse_absolute_location_path(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2352 | LY_CHECK_RET(rc); |
| 2353 | break; |
| 2354 | case LYXP_TOKEN_LITERAL: |
| 2355 | /* Literal */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2356 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2357 | goto predicate; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2358 | case LYXP_TOKEN_NUMBER: |
| 2359 | /* Number */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2360 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2361 | goto predicate; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2362 | default: |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2363 | LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2364 | return LY_EVALID; |
| 2365 | } |
| 2366 | |
| 2367 | return LY_SUCCESS; |
| 2368 | |
| 2369 | predicate: |
| 2370 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2371 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2372 | rc = reparse_predicate(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2373 | LY_CHECK_RET(rc); |
| 2374 | } |
| 2375 | |
| 2376 | /* ('/' or '//') RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2377 | 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] | 2378 | |
| 2379 | /* '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2380 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2381 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2382 | rc = reparse_relative_location_path(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2383 | LY_CHECK_RET(rc); |
| 2384 | } |
| 2385 | |
| 2386 | return LY_SUCCESS; |
| 2387 | } |
| 2388 | |
| 2389 | /** |
| 2390 | * @brief Reparse UnaryExpr. Logs directly on error. |
| 2391 | * |
| 2392 | * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr |
| 2393 | * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr |
| 2394 | * |
| 2395 | * @param[in] ctx Context for logging. |
| 2396 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2397 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2398 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2399 | * @return LY_ERR |
| 2400 | */ |
| 2401 | static LY_ERR |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2402 | reparse_unary_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2403 | { |
| 2404 | uint16_t prev_exp; |
| 2405 | LY_ERR rc; |
| 2406 | |
| 2407 | /* ('-')* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2408 | prev_exp = *tok_idx; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2409 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) && |
| 2410 | (exp->expr[exp->tok_pos[*tok_idx]] == '-')) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2411 | exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2412 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2413 | } |
| 2414 | |
| 2415 | /* PathExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2416 | prev_exp = *tok_idx; |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2417 | rc = reparse_path_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2418 | LY_CHECK_RET(rc); |
| 2419 | |
| 2420 | /* ('|' PathExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2421 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_UNI)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2422 | exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2423 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2424 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2425 | rc = reparse_path_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2426 | LY_CHECK_RET(rc); |
| 2427 | } |
| 2428 | |
| 2429 | return LY_SUCCESS; |
| 2430 | } |
| 2431 | |
| 2432 | /** |
| 2433 | * @brief Reparse AdditiveExpr. Logs directly on error. |
| 2434 | * |
| 2435 | * [15] AdditiveExpr ::= MultiplicativeExpr |
| 2436 | * | AdditiveExpr '+' MultiplicativeExpr |
| 2437 | * | AdditiveExpr '-' MultiplicativeExpr |
| 2438 | * [16] MultiplicativeExpr ::= UnaryExpr |
| 2439 | * | MultiplicativeExpr '*' UnaryExpr |
| 2440 | * | MultiplicativeExpr 'div' UnaryExpr |
| 2441 | * | MultiplicativeExpr 'mod' UnaryExpr |
| 2442 | * |
| 2443 | * @param[in] ctx Context for logging. |
| 2444 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2445 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2446 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2447 | * @return LY_ERR |
| 2448 | */ |
| 2449 | static LY_ERR |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2450 | reparse_additive_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2451 | { |
| 2452 | uint16_t prev_add_exp, prev_mul_exp; |
| 2453 | LY_ERR rc; |
| 2454 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2455 | prev_add_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2456 | goto reparse_multiplicative_expr; |
| 2457 | |
| 2458 | /* ('+' / '-' MultiplicativeExpr)* */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2459 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) && |
| 2460 | ((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] | 2461 | exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2462 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2463 | |
| 2464 | reparse_multiplicative_expr: |
| 2465 | /* UnaryExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2466 | prev_mul_exp = *tok_idx; |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2467 | rc = reparse_unary_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2468 | LY_CHECK_RET(rc); |
| 2469 | |
| 2470 | /* ('*' / 'div' / 'mod' UnaryExpr)* */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2471 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) && |
| 2472 | ((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] | 2473 | exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2474 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2475 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2476 | rc = reparse_unary_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2477 | LY_CHECK_RET(rc); |
| 2478 | } |
| 2479 | } |
| 2480 | |
| 2481 | return LY_SUCCESS; |
| 2482 | } |
| 2483 | |
| 2484 | /** |
| 2485 | * @brief Reparse EqualityExpr. Logs directly on error. |
| 2486 | * |
| 2487 | * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr |
| 2488 | * | EqualityExpr '!=' RelationalExpr |
| 2489 | * [14] RelationalExpr ::= AdditiveExpr |
| 2490 | * | RelationalExpr '<' AdditiveExpr |
| 2491 | * | RelationalExpr '>' AdditiveExpr |
| 2492 | * | RelationalExpr '<=' AdditiveExpr |
| 2493 | * | RelationalExpr '>=' AdditiveExpr |
| 2494 | * |
| 2495 | * @param[in] ctx Context for logging. |
| 2496 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2497 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2498 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2499 | * @return LY_ERR |
| 2500 | */ |
| 2501 | static LY_ERR |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2502 | reparse_equality_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2503 | { |
| 2504 | uint16_t prev_eq_exp, prev_rel_exp; |
| 2505 | LY_ERR rc; |
| 2506 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2507 | prev_eq_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2508 | goto reparse_additive_expr; |
| 2509 | |
| 2510 | /* ('=' / '!=' RelationalExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2511 | 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] | 2512 | exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2513 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2514 | |
| 2515 | reparse_additive_expr: |
| 2516 | /* AdditiveExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2517 | prev_rel_exp = *tok_idx; |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2518 | rc = reparse_additive_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2519 | LY_CHECK_RET(rc); |
| 2520 | |
| 2521 | /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2522 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_COMP)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2523 | exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL); |
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_additive_expr(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 | |
| 2531 | return LY_SUCCESS; |
| 2532 | } |
| 2533 | |
| 2534 | /** |
| 2535 | * @brief Reparse OrExpr. Logs directly on error. |
| 2536 | * |
| 2537 | * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr |
| 2538 | * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr |
| 2539 | * |
| 2540 | * @param[in] ctx Context for logging. |
| 2541 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2542 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2543 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2544 | * @return LY_ERR |
| 2545 | */ |
| 2546 | static LY_ERR |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2547 | reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2548 | { |
| 2549 | uint16_t prev_or_exp, prev_and_exp; |
| 2550 | LY_ERR rc; |
| 2551 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2552 | ++depth; |
| 2553 | LY_CHECK_ERR_RET(depth > LYXP_MAX_BLOCK_DEPTH, LOGVAL(ctx, LY_VCODE_XP_DEPTH), LY_EINVAL); |
| 2554 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2555 | prev_or_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2556 | goto reparse_equality_expr; |
| 2557 | |
| 2558 | /* ('or' AndExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2559 | 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] | 2560 | exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2561 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2562 | |
| 2563 | reparse_equality_expr: |
| 2564 | /* EqualityExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2565 | prev_and_exp = *tok_idx; |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2566 | rc = reparse_equality_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2567 | LY_CHECK_RET(rc); |
| 2568 | |
| 2569 | /* ('and' EqualityExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2570 | 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] | 2571 | exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2572 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2573 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2574 | rc = reparse_equality_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2575 | LY_CHECK_RET(rc); |
| 2576 | } |
| 2577 | } |
| 2578 | |
| 2579 | return LY_SUCCESS; |
| 2580 | } |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2581 | |
| 2582 | /** |
| 2583 | * @brief Parse NCName. |
| 2584 | * |
| 2585 | * @param[in] ncname Name to parse. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2586 | * @return Length of @p ncname valid bytes. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2587 | */ |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2588 | static long int |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2589 | parse_ncname(const char *ncname) |
| 2590 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2591 | uint32_t uc; |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2592 | size_t size; |
| 2593 | long int len = 0; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2594 | |
| 2595 | LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0); |
| 2596 | if (!is_xmlqnamestartchar(uc) || (uc == ':')) { |
| 2597 | return len; |
| 2598 | } |
| 2599 | |
| 2600 | do { |
| 2601 | len += size; |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 2602 | if (!*ncname) { |
| 2603 | break; |
| 2604 | } |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2605 | LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2606 | } while (is_xmlqnamechar(uc) && (uc != ':')); |
| 2607 | |
| 2608 | return len; |
| 2609 | } |
| 2610 | |
| 2611 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2612 | * @brief Add @p token into the expression @p exp. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2613 | * |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2614 | * @param[in] ctx Context for logging. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2615 | * @param[in] exp Expression to use. |
| 2616 | * @param[in] token Token to add. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2617 | * @param[in] tok_pos Token position in the XPath expression. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2618 | * @param[in] tok_len Token length in the XPath expression. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2619 | * @return LY_ERR |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2620 | */ |
| 2621 | static LY_ERR |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 2622 | exp_add_token(const struct ly_ctx *ctx, struct lyxp_expr *exp, enum lyxp_token token, uint16_t tok_pos, uint16_t tok_len) |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2623 | { |
| 2624 | uint32_t prev; |
| 2625 | |
| 2626 | if (exp->used == exp->size) { |
| 2627 | prev = exp->size; |
| 2628 | exp->size += LYXP_EXPR_SIZE_STEP; |
| 2629 | if (prev > exp->size) { |
| 2630 | LOGINT(ctx); |
| 2631 | return LY_EINT; |
| 2632 | } |
| 2633 | |
| 2634 | exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens); |
| 2635 | LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM); |
| 2636 | exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos); |
| 2637 | LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM); |
| 2638 | exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len); |
| 2639 | LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM); |
| 2640 | } |
| 2641 | |
| 2642 | exp->tokens[exp->used] = token; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2643 | exp->tok_pos[exp->used] = tok_pos; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2644 | exp->tok_len[exp->used] = tok_len; |
| 2645 | ++exp->used; |
| 2646 | return LY_SUCCESS; |
| 2647 | } |
| 2648 | |
| 2649 | void |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 2650 | lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr) |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2651 | { |
| 2652 | uint16_t i; |
| 2653 | |
| 2654 | if (!expr) { |
| 2655 | return; |
| 2656 | } |
| 2657 | |
| 2658 | lydict_remove(ctx, expr->expr); |
| 2659 | free(expr->tokens); |
| 2660 | free(expr->tok_pos); |
| 2661 | free(expr->tok_len); |
| 2662 | if (expr->repeat) { |
| 2663 | for (i = 0; i < expr->used; ++i) { |
| 2664 | free(expr->repeat[i]); |
| 2665 | } |
| 2666 | } |
| 2667 | free(expr->repeat); |
| 2668 | free(expr); |
| 2669 | } |
| 2670 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2671 | LY_ERR |
| 2672 | 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] | 2673 | { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2674 | LY_ERR ret = LY_SUCCESS; |
| 2675 | struct lyxp_expr *expr; |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2676 | size_t parsed = 0, tok_len; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2677 | enum lyxp_token tok_type; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 2678 | ly_bool prev_function_check = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2679 | uint16_t tok_idx = 0; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2680 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2681 | assert(expr_p); |
| 2682 | |
| 2683 | if (!expr_str[0]) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2684 | LOGVAL(ctx, LY_VCODE_XP_EOF); |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2685 | return LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2686 | } |
| 2687 | |
| 2688 | if (!expr_len) { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2689 | expr_len = strlen(expr_str); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2690 | } |
| 2691 | if (expr_len > UINT16_MAX) { |
Michal Vasko | 623ac7b | 2021-04-09 12:44:23 +0200 | [diff] [blame] | 2692 | LOGVAL(ctx, LYVE_XPATH, "XPath expression cannot be longer than %u characters.", UINT16_MAX); |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2693 | return LY_EVALID; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2694 | } |
| 2695 | |
| 2696 | /* init lyxp_expr structure */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2697 | expr = calloc(1, sizeof *expr); |
| 2698 | LY_CHECK_ERR_GOTO(!expr, LOGMEM(ctx); ret = LY_EMEM, error); |
| 2699 | LY_CHECK_GOTO(ret = lydict_insert(ctx, expr_str, expr_len, &expr->expr), error); |
| 2700 | expr->used = 0; |
| 2701 | expr->size = LYXP_EXPR_SIZE_START; |
| 2702 | expr->tokens = malloc(expr->size * sizeof *expr->tokens); |
| 2703 | LY_CHECK_ERR_GOTO(!expr->tokens, LOGMEM(ctx); ret = LY_EMEM, error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2704 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2705 | expr->tok_pos = malloc(expr->size * sizeof *expr->tok_pos); |
| 2706 | 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] | 2707 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2708 | expr->tok_len = malloc(expr->size * sizeof *expr->tok_len); |
| 2709 | 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] | 2710 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2711 | /* make expr 0-terminated */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2712 | expr_str = expr->expr; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2713 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2714 | while (is_xmlws(expr_str[parsed])) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2715 | ++parsed; |
| 2716 | } |
| 2717 | |
| 2718 | do { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2719 | if (expr_str[parsed] == '(') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2720 | |
| 2721 | /* '(' */ |
| 2722 | tok_len = 1; |
| 2723 | tok_type = LYXP_TOKEN_PAR1; |
| 2724 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2725 | if (prev_function_check && expr->used && (expr->tokens[expr->used - 1] == LYXP_TOKEN_NAMETEST)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2726 | /* it is a NodeType/FunctionName after all */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2727 | if (((expr->tok_len[expr->used - 1] == 4) && |
| 2728 | (!strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "node", 4) || |
| 2729 | !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "text", 4))) || |
| 2730 | ((expr->tok_len[expr->used - 1] == 7) && |
| 2731 | !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "comment", 7))) { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2732 | expr->tokens[expr->used - 1] = LYXP_TOKEN_NODETYPE; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2733 | } else { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2734 | expr->tokens[expr->used - 1] = LYXP_TOKEN_FUNCNAME; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2735 | } |
| 2736 | prev_function_check = 0; |
| 2737 | } |
| 2738 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2739 | } else if (expr_str[parsed] == ')') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2740 | |
| 2741 | /* ')' */ |
| 2742 | tok_len = 1; |
| 2743 | tok_type = LYXP_TOKEN_PAR2; |
| 2744 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2745 | } else if (expr_str[parsed] == '[') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2746 | |
| 2747 | /* '[' */ |
| 2748 | tok_len = 1; |
| 2749 | tok_type = LYXP_TOKEN_BRACK1; |
| 2750 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2751 | } else if (expr_str[parsed] == ']') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2752 | |
| 2753 | /* ']' */ |
| 2754 | tok_len = 1; |
| 2755 | tok_type = LYXP_TOKEN_BRACK2; |
| 2756 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2757 | } else if (!strncmp(&expr_str[parsed], "..", 2)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2758 | |
| 2759 | /* '..' */ |
| 2760 | tok_len = 2; |
| 2761 | tok_type = LYXP_TOKEN_DDOT; |
| 2762 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2763 | } else if ((expr_str[parsed] == '.') && (!isdigit(expr_str[parsed + 1]))) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2764 | |
| 2765 | /* '.' */ |
| 2766 | tok_len = 1; |
| 2767 | tok_type = LYXP_TOKEN_DOT; |
| 2768 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2769 | } else if (expr_str[parsed] == '@') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2770 | |
| 2771 | /* '@' */ |
| 2772 | tok_len = 1; |
| 2773 | tok_type = LYXP_TOKEN_AT; |
| 2774 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2775 | } else if (expr_str[parsed] == ',') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2776 | |
| 2777 | /* ',' */ |
| 2778 | tok_len = 1; |
| 2779 | tok_type = LYXP_TOKEN_COMMA; |
| 2780 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2781 | } else if (expr_str[parsed] == '\'') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2782 | |
| 2783 | /* Literal with ' */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2784 | for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\''); ++tok_len) {} |
| 2785 | LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0', |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2786 | 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] | 2787 | error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2788 | ++tok_len; |
| 2789 | tok_type = LYXP_TOKEN_LITERAL; |
| 2790 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2791 | } else if (expr_str[parsed] == '\"') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2792 | |
| 2793 | /* Literal with " */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2794 | for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\"'); ++tok_len) {} |
| 2795 | LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0', |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2796 | 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] | 2797 | error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2798 | ++tok_len; |
| 2799 | tok_type = LYXP_TOKEN_LITERAL; |
| 2800 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2801 | } else if ((expr_str[parsed] == '.') || (isdigit(expr_str[parsed]))) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2802 | |
| 2803 | /* Number */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2804 | for (tok_len = 0; isdigit(expr_str[parsed + tok_len]); ++tok_len) {} |
| 2805 | if (expr_str[parsed + tok_len] == '.') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2806 | ++tok_len; |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2807 | for ( ; isdigit(expr_str[parsed + tok_len]); ++tok_len) {} |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2808 | } |
| 2809 | tok_type = LYXP_TOKEN_NUMBER; |
| 2810 | |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 2811 | } else if (expr_str[parsed] == '$') { |
| 2812 | |
| 2813 | /* VariableReference */ |
| 2814 | parsed++; |
| 2815 | long int ncname_len = parse_ncname(&expr_str[parsed]); |
| 2816 | LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len], |
| 2817 | parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error); |
| 2818 | tok_len = ncname_len; |
| 2819 | LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == ':', |
| 2820 | LOGVAL(ctx, LYVE_XPATH, "Variable with prefix is not supported."); ret = LY_EVALID, |
| 2821 | error); |
| 2822 | tok_type = LYXP_TOKEN_VARREF; |
| 2823 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2824 | } else if (expr_str[parsed] == '/') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2825 | |
| 2826 | /* Operator '/', '//' */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2827 | if (!strncmp(&expr_str[parsed], "//", 2)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2828 | tok_len = 2; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2829 | tok_type = LYXP_TOKEN_OPER_RPATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2830 | } else { |
| 2831 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2832 | tok_type = LYXP_TOKEN_OPER_PATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2833 | } |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2834 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2835 | } else if (!strncmp(&expr_str[parsed], "!=", 2)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2836 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2837 | /* Operator '!=' */ |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2838 | tok_len = 2; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2839 | tok_type = LYXP_TOKEN_OPER_NEQUAL; |
| 2840 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2841 | } else if (!strncmp(&expr_str[parsed], "<=", 2) || !strncmp(&expr_str[parsed], ">=", 2)) { |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2842 | |
| 2843 | /* Operator '<=', '>=' */ |
| 2844 | tok_len = 2; |
| 2845 | tok_type = LYXP_TOKEN_OPER_COMP; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2846 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2847 | } else if (expr_str[parsed] == '|') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2848 | |
| 2849 | /* Operator '|' */ |
| 2850 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2851 | tok_type = LYXP_TOKEN_OPER_UNI; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2852 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2853 | } else if ((expr_str[parsed] == '+') || (expr_str[parsed] == '-')) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2854 | |
| 2855 | /* Operator '+', '-' */ |
| 2856 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2857 | tok_type = LYXP_TOKEN_OPER_MATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2858 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2859 | } else if (expr_str[parsed] == '=') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2860 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2861 | /* Operator '=' */ |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2862 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2863 | tok_type = LYXP_TOKEN_OPER_EQUAL; |
| 2864 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2865 | } else if ((expr_str[parsed] == '<') || (expr_str[parsed] == '>')) { |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2866 | |
| 2867 | /* Operator '<', '>' */ |
| 2868 | tok_len = 1; |
| 2869 | tok_type = LYXP_TOKEN_OPER_COMP; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2870 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2871 | } else if (expr->used && (expr->tokens[expr->used - 1] != LYXP_TOKEN_AT) && |
| 2872 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_PAR1) && |
| 2873 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_BRACK1) && |
| 2874 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_COMMA) && |
| 2875 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_LOG) && |
| 2876 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_EQUAL) && |
| 2877 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_NEQUAL) && |
| 2878 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_COMP) && |
| 2879 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_MATH) && |
| 2880 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_UNI) && |
| 2881 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_PATH) && |
| 2882 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_RPATH)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2883 | |
| 2884 | /* Operator '*', 'or', 'and', 'mod', or 'div' */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2885 | if (expr_str[parsed] == '*') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2886 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2887 | tok_type = LYXP_TOKEN_OPER_MATH; |
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 | } else if (!strncmp(&expr_str[parsed], "or", 2)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2890 | tok_len = 2; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2891 | tok_type = LYXP_TOKEN_OPER_LOG; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2892 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2893 | } else if (!strncmp(&expr_str[parsed], "and", 3)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2894 | tok_len = 3; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2895 | tok_type = LYXP_TOKEN_OPER_LOG; |
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 | } 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] | 2898 | tok_len = 3; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2899 | tok_type = LYXP_TOKEN_OPER_MATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2900 | |
| 2901 | } else if (prev_function_check) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2902 | LOGVAL(ctx, LYVE_XPATH, "Invalid character 0x%x ('%c'), perhaps \"%.*s\" is supposed to be a function call.", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2903 | expr_str[parsed], expr_str[parsed], expr->tok_len[expr->used - 1], &expr->expr[expr->tok_pos[expr->used - 1]]); |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2904 | ret = LY_EVALID; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2905 | goto error; |
| 2906 | } else { |
Michal Vasko | 774ce40 | 2021-04-14 15:35:06 +0200 | [diff] [blame] | 2907 | LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed], parsed + 1, expr_str); |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2908 | ret = LY_EVALID; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2909 | goto error; |
| 2910 | } |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2911 | } else if (expr_str[parsed] == '*') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2912 | |
| 2913 | /* NameTest '*' */ |
| 2914 | tok_len = 1; |
| 2915 | tok_type = LYXP_TOKEN_NAMETEST; |
| 2916 | |
| 2917 | } else { |
| 2918 | |
| 2919 | /* NameTest (NCName ':' '*' | QName) or NodeType/FunctionName */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2920 | long int ncname_len = parse_ncname(&expr_str[parsed]); |
Michal Vasko | e2be546 | 2021-08-04 10:49:42 +0200 | [diff] [blame] | 2921 | LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len], |
Michal Vasko | 774ce40 | 2021-04-14 15:35:06 +0200 | [diff] [blame] | 2922 | parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2923 | tok_len = ncname_len; |
| 2924 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2925 | if (expr_str[parsed + tok_len] == ':') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2926 | ++tok_len; |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2927 | if (expr_str[parsed + tok_len] == '*') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2928 | ++tok_len; |
| 2929 | } else { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2930 | ncname_len = parse_ncname(&expr_str[parsed + tok_len]); |
Michal Vasko | e2be546 | 2021-08-04 10:49:42 +0200 | [diff] [blame] | 2931 | LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len], |
Michal Vasko | 774ce40 | 2021-04-14 15:35:06 +0200 | [diff] [blame] | 2932 | parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2933 | tok_len += ncname_len; |
| 2934 | } |
| 2935 | /* remove old flag to prevent ambiguities */ |
| 2936 | prev_function_check = 0; |
| 2937 | tok_type = LYXP_TOKEN_NAMETEST; |
| 2938 | } else { |
| 2939 | /* there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */ |
| 2940 | prev_function_check = 1; |
| 2941 | tok_type = LYXP_TOKEN_NAMETEST; |
| 2942 | } |
| 2943 | } |
| 2944 | |
| 2945 | /* store the token, move on to the next one */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2946 | 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] | 2947 | parsed += tok_len; |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2948 | while (is_xmlws(expr_str[parsed])) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2949 | ++parsed; |
| 2950 | } |
| 2951 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2952 | } while (expr_str[parsed]); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2953 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2954 | if (reparse) { |
| 2955 | /* prealloc repeat */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2956 | expr->repeat = calloc(expr->size, sizeof *expr->repeat); |
| 2957 | LY_CHECK_ERR_GOTO(!expr->repeat, LOGMEM(ctx); ret = LY_EMEM, error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2958 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2959 | /* fill repeat */ |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2960 | 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] | 2961 | if (expr->used > tok_idx) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2962 | 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] | 2963 | &expr->expr[expr->tok_pos[tok_idx]]); |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2964 | ret = LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2965 | goto error; |
| 2966 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2967 | } |
| 2968 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2969 | print_expr_struct_debug(expr); |
| 2970 | *expr_p = expr; |
| 2971 | return LY_SUCCESS; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2972 | |
| 2973 | error: |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2974 | lyxp_expr_free(ctx, expr); |
| 2975 | return ret; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2976 | } |
| 2977 | |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2978 | LY_ERR |
| 2979 | lyxp_expr_dup(const struct ly_ctx *ctx, const struct lyxp_expr *exp, struct lyxp_expr **dup_p) |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2980 | { |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2981 | LY_ERR ret = LY_SUCCESS; |
| 2982 | struct lyxp_expr *dup = NULL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2983 | uint32_t i, j; |
| 2984 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 2985 | if (!exp) { |
| 2986 | goto cleanup; |
| 2987 | } |
| 2988 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2989 | dup = calloc(1, sizeof *dup); |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2990 | LY_CHECK_ERR_GOTO(!dup, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2991 | |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 2992 | if (exp->used) { |
| 2993 | dup->tokens = malloc(exp->used * sizeof *dup->tokens); |
| 2994 | LY_CHECK_ERR_GOTO(!dup->tokens, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 2995 | memcpy(dup->tokens, exp->tokens, exp->used * sizeof *dup->tokens); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2996 | |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 2997 | dup->tok_pos = malloc(exp->used * sizeof *dup->tok_pos); |
| 2998 | LY_CHECK_ERR_GOTO(!dup->tok_pos, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 2999 | memcpy(dup->tok_pos, exp->tok_pos, exp->used * sizeof *dup->tok_pos); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3000 | |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 3001 | dup->tok_len = malloc(exp->used * sizeof *dup->tok_len); |
| 3002 | LY_CHECK_ERR_GOTO(!dup->tok_len, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 3003 | memcpy(dup->tok_len, exp->tok_len, exp->used * sizeof *dup->tok_len); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3004 | |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 3005 | dup->repeat = malloc(exp->used * sizeof *dup->repeat); |
| 3006 | LY_CHECK_ERR_GOTO(!dup->repeat, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 3007 | for (i = 0; i < exp->used; ++i) { |
| 3008 | if (!exp->repeat[i]) { |
| 3009 | dup->repeat[i] = NULL; |
| 3010 | } else { |
| 3011 | for (j = 0; exp->repeat[i][j]; ++j) {} |
| 3012 | /* the ending 0 as well */ |
| 3013 | ++j; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3014 | |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 3015 | dup->repeat[i] = malloc(j * sizeof **dup->repeat); |
| 3016 | LY_CHECK_ERR_GOTO(!dup->repeat[i], LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 3017 | memcpy(dup->repeat[i], exp->repeat[i], j * sizeof **dup->repeat); |
| 3018 | dup->repeat[i][j - 1] = 0; |
| 3019 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3020 | } |
| 3021 | } |
| 3022 | |
| 3023 | dup->used = exp->used; |
| 3024 | dup->size = exp->used; |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 3025 | LY_CHECK_GOTO(ret = lydict_insert(ctx, exp->expr, 0, &dup->expr), cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3026 | |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 3027 | cleanup: |
| 3028 | if (ret) { |
| 3029 | lyxp_expr_free(ctx, dup); |
| 3030 | } else { |
| 3031 | *dup_p = dup; |
| 3032 | } |
| 3033 | return ret; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3034 | } |
| 3035 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3036 | /** |
| 3037 | * @brief Get the last-added schema node that is currently in the context. |
| 3038 | * |
| 3039 | * @param[in] set Set to search in. |
| 3040 | * @return Last-added schema context node, NULL if no node is in context. |
| 3041 | */ |
| 3042 | static struct lysc_node * |
| 3043 | warn_get_scnode_in_ctx(struct lyxp_set *set) |
| 3044 | { |
| 3045 | uint32_t i; |
| 3046 | |
| 3047 | if (!set || (set->type != LYXP_SET_SCNODE_SET)) { |
| 3048 | return NULL; |
| 3049 | } |
| 3050 | |
| 3051 | i = set->used; |
| 3052 | do { |
| 3053 | --i; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 3054 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3055 | /* if there are more, simply return the first found (last added) */ |
| 3056 | return set->val.scnodes[i].scnode; |
| 3057 | } |
| 3058 | } while (i); |
| 3059 | |
| 3060 | return NULL; |
| 3061 | } |
| 3062 | |
| 3063 | /** |
| 3064 | * @brief Test whether a type is numeric - integer type or decimal64. |
| 3065 | * |
| 3066 | * @param[in] type Type to test. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3067 | * @return Boolean value whether @p type is numeric type or not. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3068 | */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3069 | static ly_bool |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3070 | warn_is_numeric_type(struct lysc_type *type) |
| 3071 | { |
| 3072 | struct lysc_type_union *uni; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3073 | ly_bool ret; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3074 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3075 | |
| 3076 | switch (type->basetype) { |
| 3077 | case LY_TYPE_DEC64: |
| 3078 | case LY_TYPE_INT8: |
| 3079 | case LY_TYPE_UINT8: |
| 3080 | case LY_TYPE_INT16: |
| 3081 | case LY_TYPE_UINT16: |
| 3082 | case LY_TYPE_INT32: |
| 3083 | case LY_TYPE_UINT32: |
| 3084 | case LY_TYPE_INT64: |
| 3085 | case LY_TYPE_UINT64: |
| 3086 | return 1; |
| 3087 | case LY_TYPE_UNION: |
| 3088 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3089 | LY_ARRAY_FOR(uni->types, u) { |
| 3090 | ret = warn_is_numeric_type(uni->types[u]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3091 | if (ret) { |
| 3092 | /* found a suitable type */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3093 | return ret; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3094 | } |
| 3095 | } |
| 3096 | /* did not find any suitable type */ |
| 3097 | return 0; |
| 3098 | case LY_TYPE_LEAFREF: |
| 3099 | return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype); |
| 3100 | default: |
| 3101 | return 0; |
| 3102 | } |
| 3103 | } |
| 3104 | |
| 3105 | /** |
| 3106 | * @brief Test whether a type is string-like - no integers, decimal64 or binary. |
| 3107 | * |
| 3108 | * @param[in] type Type to test. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3109 | * @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] | 3110 | */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3111 | static ly_bool |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3112 | warn_is_string_type(struct lysc_type *type) |
| 3113 | { |
| 3114 | struct lysc_type_union *uni; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3115 | ly_bool ret; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3116 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3117 | |
| 3118 | switch (type->basetype) { |
| 3119 | case LY_TYPE_BITS: |
| 3120 | case LY_TYPE_ENUM: |
| 3121 | case LY_TYPE_IDENT: |
| 3122 | case LY_TYPE_INST: |
| 3123 | case LY_TYPE_STRING: |
| 3124 | return 1; |
| 3125 | case LY_TYPE_UNION: |
| 3126 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3127 | LY_ARRAY_FOR(uni->types, u) { |
| 3128 | ret = warn_is_string_type(uni->types[u]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3129 | if (ret) { |
| 3130 | /* found a suitable type */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3131 | return ret; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3132 | } |
| 3133 | } |
| 3134 | /* did not find any suitable type */ |
| 3135 | return 0; |
| 3136 | case LY_TYPE_LEAFREF: |
| 3137 | return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype); |
| 3138 | default: |
| 3139 | return 0; |
| 3140 | } |
| 3141 | } |
| 3142 | |
| 3143 | /** |
| 3144 | * @brief Test whether a type is one specific type. |
| 3145 | * |
| 3146 | * @param[in] type Type to test. |
| 3147 | * @param[in] base Expected type. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3148 | * @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] | 3149 | */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3150 | static ly_bool |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3151 | warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base) |
| 3152 | { |
| 3153 | struct lysc_type_union *uni; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3154 | ly_bool ret; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3155 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3156 | |
| 3157 | if (type->basetype == base) { |
| 3158 | return 1; |
| 3159 | } else if (type->basetype == LY_TYPE_UNION) { |
| 3160 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3161 | LY_ARRAY_FOR(uni->types, u) { |
| 3162 | ret = warn_is_specific_type(uni->types[u], base); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3163 | if (ret) { |
| 3164 | /* found a suitable type */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3165 | return ret; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3166 | } |
| 3167 | } |
| 3168 | /* did not find any suitable type */ |
| 3169 | return 0; |
| 3170 | } else if (type->basetype == LY_TYPE_LEAFREF) { |
| 3171 | return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base); |
| 3172 | } |
| 3173 | |
| 3174 | return 0; |
| 3175 | } |
| 3176 | |
| 3177 | /** |
| 3178 | * @brief Get next type of a (union) type. |
| 3179 | * |
| 3180 | * @param[in] type Base type. |
| 3181 | * @param[in] prev_type Previously returned type. |
| 3182 | * @return Next type or NULL. |
| 3183 | */ |
| 3184 | static struct lysc_type * |
| 3185 | warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type) |
| 3186 | { |
| 3187 | struct lysc_type_union *uni; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3188 | ly_bool found = 0; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3189 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3190 | |
| 3191 | switch (type->basetype) { |
| 3192 | case LY_TYPE_UNION: |
| 3193 | uni = (struct lysc_type_union *)type; |
| 3194 | if (!prev_type) { |
| 3195 | return uni->types[0]; |
| 3196 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3197 | LY_ARRAY_FOR(uni->types, u) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3198 | if (found) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3199 | return uni->types[u]; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3200 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3201 | if (prev_type == uni->types[u]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3202 | found = 1; |
| 3203 | } |
| 3204 | } |
| 3205 | return NULL; |
| 3206 | default: |
| 3207 | if (prev_type) { |
| 3208 | assert(type == prev_type); |
| 3209 | return NULL; |
| 3210 | } else { |
| 3211 | return type; |
| 3212 | } |
| 3213 | } |
| 3214 | } |
| 3215 | |
| 3216 | /** |
| 3217 | * @brief Test whether 2 types have a common type. |
| 3218 | * |
| 3219 | * @param[in] type1 First type. |
| 3220 | * @param[in] type2 Second type. |
| 3221 | * @return 1 if they do, 0 otherwise. |
| 3222 | */ |
| 3223 | static int |
| 3224 | warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2) |
| 3225 | { |
| 3226 | struct lysc_type *t1, *rt1, *t2, *rt2; |
| 3227 | |
| 3228 | t1 = NULL; |
| 3229 | while ((t1 = warn_is_equal_type_next_type(type1, t1))) { |
| 3230 | if (t1->basetype == LY_TYPE_LEAFREF) { |
| 3231 | rt1 = ((struct lysc_type_leafref *)t1)->realtype; |
| 3232 | } else { |
| 3233 | rt1 = t1; |
| 3234 | } |
| 3235 | |
| 3236 | t2 = NULL; |
| 3237 | while ((t2 = warn_is_equal_type_next_type(type2, t2))) { |
| 3238 | if (t2->basetype == LY_TYPE_LEAFREF) { |
| 3239 | rt2 = ((struct lysc_type_leafref *)t2)->realtype; |
| 3240 | } else { |
| 3241 | rt2 = t2; |
| 3242 | } |
| 3243 | |
| 3244 | if (rt2->basetype == rt1->basetype) { |
| 3245 | /* match found */ |
| 3246 | return 1; |
| 3247 | } |
| 3248 | } |
| 3249 | } |
| 3250 | |
| 3251 | return 0; |
| 3252 | } |
| 3253 | |
| 3254 | /** |
Michal Vasko | aa95652 | 2021-11-11 10:45:34 +0100 | [diff] [blame] | 3255 | * @brief Print warning with information about the XPath subexpression that caused previous warning. |
| 3256 | * |
| 3257 | * @param[in] ctx Context for logging. |
| 3258 | * @param[in] tok_pos Index of the subexpression in the whole expression. |
| 3259 | * @param[in] subexpr Subexpression start. |
| 3260 | * @param[in] subexpr_len Length of @p subexpr to print. |
| 3261 | * @param[in] cur_scnode Expression context node. |
| 3262 | */ |
| 3263 | static void |
| 3264 | warn_subexpr_log(const struct ly_ctx *ctx, uint16_t tok_pos, const char *subexpr, int subexpr_len, |
| 3265 | const struct lysc_node *cur_scnode) |
| 3266 | { |
| 3267 | char *path; |
| 3268 | |
| 3269 | path = lysc_path(cur_scnode, LYSC_PATH_LOG, NULL, 0); |
| 3270 | LOGWRN(ctx, "Previous warning generated by XPath subexpression[%" PRIu16 "] \"%.*s\" with context node \"%s\".", |
| 3271 | tok_pos, subexpr_len, subexpr, path); |
| 3272 | free(path); |
| 3273 | } |
| 3274 | |
| 3275 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3276 | * @brief Check both operands of comparison operators. |
| 3277 | * |
| 3278 | * @param[in] ctx Context for errors. |
| 3279 | * @param[in] set1 First operand set. |
| 3280 | * @param[in] set2 Second operand set. |
| 3281 | * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!='). |
| 3282 | * @param[in] expr Start of the expression to print with the warning. |
| 3283 | * @param[in] tok_pos Token position. |
| 3284 | */ |
| 3285 | static void |
Michal Vasko | aa95652 | 2021-11-11 10:45:34 +0100 | [diff] [blame] | 3286 | warn_operands(struct ly_ctx *ctx, struct lyxp_set *set1, struct lyxp_set *set2, ly_bool numbers_only, const char *expr, |
| 3287 | uint16_t tok_pos) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3288 | { |
| 3289 | struct lysc_node_leaf *node1, *node2; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3290 | ly_bool leaves = 1, warning = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3291 | |
| 3292 | node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1); |
| 3293 | node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2); |
| 3294 | |
| 3295 | if (!node1 && !node2) { |
| 3296 | /* no node-sets involved, nothing to do */ |
| 3297 | return; |
| 3298 | } |
| 3299 | |
| 3300 | if (node1) { |
| 3301 | if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3302 | LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name); |
| 3303 | warning = 1; |
| 3304 | leaves = 0; |
| 3305 | } else if (numbers_only && !warn_is_numeric_type(node1->type)) { |
| 3306 | LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name); |
| 3307 | warning = 1; |
| 3308 | } |
| 3309 | } |
| 3310 | |
| 3311 | if (node2) { |
| 3312 | if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3313 | LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name); |
| 3314 | warning = 1; |
| 3315 | leaves = 0; |
| 3316 | } else if (numbers_only && !warn_is_numeric_type(node2->type)) { |
| 3317 | LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name); |
| 3318 | warning = 1; |
| 3319 | } |
| 3320 | } |
| 3321 | |
| 3322 | if (node1 && node2 && leaves && !numbers_only) { |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3323 | if ((warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type)) || |
| 3324 | (!warn_is_numeric_type(node1->type) && warn_is_numeric_type(node2->type)) || |
| 3325 | (!warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type) && |
| 3326 | !warn_is_equal_type(node1->type, node2->type))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3327 | LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name); |
| 3328 | warning = 1; |
| 3329 | } |
| 3330 | } |
| 3331 | |
| 3332 | if (warning) { |
Michal Vasko | aa95652 | 2021-11-11 10:45:34 +0100 | [diff] [blame] | 3333 | 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] | 3334 | } |
| 3335 | } |
| 3336 | |
| 3337 | /** |
| 3338 | * @brief Check that a value is valid for a leaf. If not applicable, does nothing. |
| 3339 | * |
| 3340 | * @param[in] exp Parsed XPath expression. |
| 3341 | * @param[in] set Set with the leaf/leaf-list. |
| 3342 | * @param[in] val_exp Index of the value (literal/number) in @p exp. |
| 3343 | * @param[in] equal_exp Index of the start of the equality expression in @p exp. |
| 3344 | * @param[in] last_equal_exp Index of the end of the equality expression in @p exp. |
| 3345 | */ |
| 3346 | static void |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 3347 | warn_equality_value(const struct lyxp_expr *exp, struct lyxp_set *set, uint16_t val_exp, uint16_t equal_exp, |
| 3348 | uint16_t last_equal_exp) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3349 | { |
| 3350 | struct lysc_node *scnode; |
| 3351 | struct lysc_type *type; |
| 3352 | char *value; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3353 | struct lyd_value storage; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3354 | LY_ERR rc; |
| 3355 | struct ly_err_item *err = NULL; |
| 3356 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3357 | if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && |
| 3358 | ((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] | 3359 | /* check that the node can have the specified value */ |
| 3360 | if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) { |
| 3361 | value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2); |
| 3362 | } else { |
| 3363 | value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]); |
| 3364 | } |
| 3365 | if (!value) { |
| 3366 | LOGMEM(set->ctx); |
| 3367 | return; |
| 3368 | } |
| 3369 | |
| 3370 | if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) { |
| 3371 | 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] | 3372 | " 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] | 3373 | 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] | 3374 | (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] | 3375 | set->cur_scnode); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3376 | } |
| 3377 | |
| 3378 | type = ((struct lysc_node_leaf *)scnode)->type; |
| 3379 | if (type->basetype != LY_TYPE_IDENT) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 3380 | 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] | 3381 | LYD_HINT_DATA, scnode, &storage, NULL, &err); |
Michal Vasko | bf42e83 | 2020-11-23 16:59:42 +0100 | [diff] [blame] | 3382 | if (rc == LY_EINCOMPLETE) { |
| 3383 | rc = LY_SUCCESS; |
| 3384 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3385 | |
| 3386 | if (err) { |
| 3387 | LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg); |
| 3388 | ly_err_free(err); |
| 3389 | } else if (rc != LY_SUCCESS) { |
| 3390 | LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value); |
| 3391 | } |
| 3392 | if (rc != LY_SUCCESS) { |
Michal Vasko | aa95652 | 2021-11-11 10:45:34 +0100 | [diff] [blame] | 3393 | 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] | 3394 | (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] | 3395 | set->cur_scnode); |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3396 | } else { |
| 3397 | type->plugin->free(set->ctx, &storage); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3398 | } |
| 3399 | } |
| 3400 | free(value); |
| 3401 | } |
| 3402 | } |
| 3403 | |
| 3404 | /* |
| 3405 | * XPath functions |
| 3406 | */ |
| 3407 | |
| 3408 | /** |
| 3409 | * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN |
| 3410 | * depending on whether the first node bit value from the second argument is set. |
| 3411 | * |
| 3412 | * @param[in] args Array of arguments. |
| 3413 | * @param[in] arg_count Count of elements in @p args. |
| 3414 | * @param[in,out] set Context and result set at the same time. |
| 3415 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3416 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3417 | */ |
| 3418 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3419 | xpath_bit_is_set(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3420 | { |
| 3421 | struct lyd_node_term *leaf; |
| 3422 | struct lysc_node_leaf *sleaf; |
Michal Vasko | 2588b95 | 2021-07-29 07:43:26 +0200 | [diff] [blame] | 3423 | struct lyd_value_bits *bits; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3424 | LY_ERR rc = LY_SUCCESS; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3425 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3426 | |
| 3427 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3428 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3429 | 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] | 3430 | } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3431 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3432 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 3433 | sleaf->name); |
| 3434 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) { |
| 3435 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"bits\".", __func__, sleaf->name); |
| 3436 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3437 | } |
| 3438 | |
| 3439 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 3440 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3441 | LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 3442 | sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3443 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3444 | 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] | 3445 | } |
| 3446 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3447 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3448 | return rc; |
| 3449 | } |
| 3450 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3451 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 3452 | 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] | 3453 | return LY_EVALID; |
| 3454 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3455 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3456 | LY_CHECK_RET(rc); |
| 3457 | |
| 3458 | set_fill_boolean(set, 0); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3459 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3460 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
Michal Vasko | 2588b95 | 2021-07-29 07:43:26 +0200 | [diff] [blame] | 3461 | if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (leaf->value.realtype->basetype == LY_TYPE_BITS)) { |
| 3462 | LYD_VALUE_GET(&leaf->value, bits); |
| 3463 | LY_ARRAY_FOR(bits->items, u) { |
| 3464 | if (!strcmp(bits->items[u]->name, args[1]->val.str)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3465 | set_fill_boolean(set, 1); |
| 3466 | break; |
| 3467 | } |
| 3468 | } |
| 3469 | } |
| 3470 | } |
| 3471 | |
| 3472 | return LY_SUCCESS; |
| 3473 | } |
| 3474 | |
| 3475 | /** |
| 3476 | * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN |
| 3477 | * with the argument converted to boolean. |
| 3478 | * |
| 3479 | * @param[in] args Array of arguments. |
| 3480 | * @param[in] arg_count Count of elements in @p args. |
| 3481 | * @param[in,out] set Context and result set at the same time. |
| 3482 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3483 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3484 | */ |
| 3485 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3486 | xpath_boolean(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3487 | { |
| 3488 | LY_ERR rc; |
| 3489 | |
| 3490 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3491 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3492 | return LY_SUCCESS; |
| 3493 | } |
| 3494 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3495 | rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3496 | LY_CHECK_RET(rc); |
| 3497 | set_fill_set(set, args[0]); |
| 3498 | |
| 3499 | return LY_SUCCESS; |
| 3500 | } |
| 3501 | |
| 3502 | /** |
| 3503 | * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER |
| 3504 | * with the first argument rounded up to the nearest integer. |
| 3505 | * |
| 3506 | * @param[in] args Array of arguments. |
| 3507 | * @param[in] arg_count Count of elements in @p args. |
| 3508 | * @param[in,out] set Context and result set at the same time. |
| 3509 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3510 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3511 | */ |
| 3512 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3513 | xpath_ceiling(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3514 | { |
| 3515 | struct lysc_node_leaf *sleaf; |
| 3516 | LY_ERR rc = LY_SUCCESS; |
| 3517 | |
| 3518 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3519 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3520 | 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] | 3521 | } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3522 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3523 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 3524 | sleaf->name); |
| 3525 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) { |
| 3526 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name); |
| 3527 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3528 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3529 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3530 | return rc; |
| 3531 | } |
| 3532 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3533 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3534 | LY_CHECK_RET(rc); |
| 3535 | if ((long long)args[0]->val.num != args[0]->val.num) { |
| 3536 | set_fill_number(set, ((long long)args[0]->val.num) + 1); |
| 3537 | } else { |
| 3538 | set_fill_number(set, args[0]->val.num); |
| 3539 | } |
| 3540 | |
| 3541 | return LY_SUCCESS; |
| 3542 | } |
| 3543 | |
| 3544 | /** |
| 3545 | * @brief Execute the XPath concat(string, string, string*) function. |
| 3546 | * Returns LYXP_SET_STRING with the concatenation of all the arguments. |
| 3547 | * |
| 3548 | * @param[in] args Array of arguments. |
| 3549 | * @param[in] arg_count Count of elements in @p args. |
| 3550 | * @param[in,out] set Context and result set at the same time. |
| 3551 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3552 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3553 | */ |
| 3554 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3555 | xpath_concat(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3556 | { |
| 3557 | uint16_t i; |
| 3558 | char *str = NULL; |
| 3559 | size_t used = 1; |
| 3560 | LY_ERR rc = LY_SUCCESS; |
| 3561 | struct lysc_node_leaf *sleaf; |
| 3562 | |
| 3563 | if (options & LYXP_SCNODE_ALL) { |
| 3564 | for (i = 0; i < arg_count; ++i) { |
| 3565 | if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) { |
| 3566 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3567 | LOGWRN(set->ctx, "Argument #%u of %s is a %s node \"%s\".", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3568 | i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3569 | } else if (!warn_is_string_type(sleaf->type)) { |
Radek Krejci | 70124c8 | 2020-08-14 22:17:03 +0200 | [diff] [blame] | 3570 | LOGWRN(set->ctx, "Argument #%u of %s is node \"%s\", not of string-type.", i + 1, __func__, sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3571 | } |
| 3572 | } |
| 3573 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3574 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3575 | return rc; |
| 3576 | } |
| 3577 | |
| 3578 | for (i = 0; i < arg_count; ++i) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3579 | rc = lyxp_set_cast(args[i], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3580 | if (rc != LY_SUCCESS) { |
| 3581 | free(str); |
| 3582 | return rc; |
| 3583 | } |
| 3584 | |
| 3585 | str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char)); |
| 3586 | LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM); |
| 3587 | strcpy(str + used - 1, args[i]->val.str); |
| 3588 | used += strlen(args[i]->val.str); |
| 3589 | } |
| 3590 | |
| 3591 | /* free, kind of */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3592 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3593 | set->type = LYXP_SET_STRING; |
| 3594 | set->val.str = str; |
| 3595 | |
| 3596 | return LY_SUCCESS; |
| 3597 | } |
| 3598 | |
| 3599 | /** |
| 3600 | * @brief Execute the XPath contains(string, string) function. |
| 3601 | * Returns LYXP_SET_BOOLEAN whether the second argument can |
| 3602 | * be found in the first or not. |
| 3603 | * |
| 3604 | * @param[in] args Array of arguments. |
| 3605 | * @param[in] arg_count Count of elements in @p args. |
| 3606 | * @param[in,out] set Context and result set at the same time. |
| 3607 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3608 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3609 | */ |
| 3610 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3611 | xpath_contains(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3612 | { |
| 3613 | struct lysc_node_leaf *sleaf; |
| 3614 | LY_ERR rc = LY_SUCCESS; |
| 3615 | |
| 3616 | if (options & LYXP_SCNODE_ALL) { |
| 3617 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3618 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3619 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 3620 | sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3621 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3622 | 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] | 3623 | } |
| 3624 | } |
| 3625 | |
| 3626 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 3627 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3628 | LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 3629 | sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3630 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3631 | 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] | 3632 | } |
| 3633 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3634 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3635 | return rc; |
| 3636 | } |
| 3637 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3638 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3639 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3640 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3641 | LY_CHECK_RET(rc); |
| 3642 | |
| 3643 | if (strstr(args[0]->val.str, args[1]->val.str)) { |
| 3644 | set_fill_boolean(set, 1); |
| 3645 | } else { |
| 3646 | set_fill_boolean(set, 0); |
| 3647 | } |
| 3648 | |
| 3649 | return LY_SUCCESS; |
| 3650 | } |
| 3651 | |
| 3652 | /** |
| 3653 | * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER |
| 3654 | * with the size of the node-set from the argument. |
| 3655 | * |
| 3656 | * @param[in] args Array of arguments. |
| 3657 | * @param[in] arg_count Count of elements in @p args. |
| 3658 | * @param[in,out] set Context and result set at the same time. |
| 3659 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3660 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3661 | */ |
| 3662 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3663 | xpath_count(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3664 | { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3665 | LY_ERR rc = LY_SUCCESS; |
| 3666 | |
| 3667 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3668 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3669 | 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] | 3670 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3671 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3672 | return rc; |
| 3673 | } |
| 3674 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3675 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 3676 | 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] | 3677 | return LY_EVALID; |
| 3678 | } |
| 3679 | |
| 3680 | set_fill_number(set, args[0]->used); |
| 3681 | return LY_SUCCESS; |
| 3682 | } |
| 3683 | |
| 3684 | /** |
| 3685 | * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET |
| 3686 | * with the context with the intial node. |
| 3687 | * |
| 3688 | * @param[in] args Array of arguments. |
| 3689 | * @param[in] arg_count Count of elements in @p args. |
| 3690 | * @param[in,out] set Context and result set at the same time. |
| 3691 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3692 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3693 | */ |
| 3694 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3695 | xpath_current(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3696 | { |
| 3697 | if (arg_count || args) { |
Radek Krejci | e87c7dc | 2021-06-02 21:25:42 +0200 | [diff] [blame] | 3698 | 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] | 3699 | return LY_EVALID; |
| 3700 | } |
| 3701 | |
| 3702 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3703 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3704 | |
Michal Vasko | 62b7f76 | 2021-12-13 16:57:42 +0100 | [diff] [blame] | 3705 | if (set->cur_scnode) { |
| 3706 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, set->cur_scnode, LYXP_NODE_ELEM, NULL)); |
| 3707 | } else { |
| 3708 | /* root node */ |
| 3709 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, NULL, set->root_type, NULL)); |
| 3710 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3711 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3712 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3713 | |
Michal Vasko | 62b7f76 | 2021-12-13 16:57:42 +0100 | [diff] [blame] | 3714 | if (set->cur_node) { |
| 3715 | /* position is filled later */ |
| 3716 | set_insert_node(set, set->cur_node, 0, LYXP_NODE_ELEM, 0); |
| 3717 | } else { |
| 3718 | /* root node */ |
| 3719 | set_insert_node(set, NULL, 0, set->root_type, 0); |
| 3720 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3721 | } |
| 3722 | |
| 3723 | return LY_SUCCESS; |
| 3724 | } |
| 3725 | |
| 3726 | /** |
| 3727 | * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either |
| 3728 | * leafref or instance-identifier target node(s). |
| 3729 | * |
| 3730 | * @param[in] args Array of arguments. |
| 3731 | * @param[in] arg_count Count of elements in @p args. |
| 3732 | * @param[in,out] set Context and result set at the same time. |
| 3733 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3734 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3735 | */ |
| 3736 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3737 | xpath_deref(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3738 | { |
| 3739 | struct lyd_node_term *leaf; |
Michal Vasko | 42e497c | 2020-01-06 08:38:25 +0100 | [diff] [blame] | 3740 | struct lysc_node_leaf *sleaf = NULL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3741 | struct lysc_type_leafref *lref; |
Michal Vasko | ae9e4cb | 2019-09-25 08:43:05 +0200 | [diff] [blame] | 3742 | const struct lysc_node *target; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3743 | struct ly_path *p; |
| 3744 | struct lyd_node *node; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3745 | char *errmsg = NULL; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 3746 | uint8_t oper; |
Michal Vasko | 741bb56 | 2021-06-24 11:59:50 +0200 | [diff] [blame] | 3747 | LY_ERR r; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3748 | |
| 3749 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3750 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3751 | 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] | 3752 | } 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] | 3753 | if (!(sleaf->nodetype & LYD_NODE_TERM)) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3754 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 3755 | sleaf->name); |
Michal Vasko | ed725d7 | 2021-06-23 12:03:45 +0200 | [diff] [blame] | 3756 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) && |
| 3757 | !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3758 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"leafref\" nor \"instance-identifier\".", |
| 3759 | __func__, sleaf->name); |
| 3760 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3761 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3762 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 423ba3f | 2021-07-19 13:08:50 +0200 | [diff] [blame] | 3763 | 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] | 3764 | lref = (struct lysc_type_leafref *)sleaf->type; |
Michal Vasko | d1e53b9 | 2021-01-28 13:11:06 +0100 | [diff] [blame] | 3765 | 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] | 3766 | |
| 3767 | /* it was already evaluated on schema, it must succeed */ |
Michal Vasko | 741bb56 | 2021-06-24 11:59:50 +0200 | [diff] [blame] | 3768 | 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] | 3769 | LY_VALUE_SCHEMA_RESOLVED, lref->prefixes, &p); |
Michal Vasko | 741bb56 | 2021-06-24 11:59:50 +0200 | [diff] [blame] | 3770 | if (!r) { |
| 3771 | /* get the target node */ |
| 3772 | target = p[LY_ARRAY_COUNT(p) - 1].node; |
| 3773 | ly_path_free(set->ctx, p); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3774 | |
Michal Vasko | 741bb56 | 2021-06-24 11:59:50 +0200 | [diff] [blame] | 3775 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, target, LYXP_NODE_ELEM, NULL)); |
| 3776 | } /* 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] | 3777 | } |
| 3778 | |
Michal Vasko | 741bb56 | 2021-06-24 11:59:50 +0200 | [diff] [blame] | 3779 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3780 | } |
| 3781 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3782 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 3783 | LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "deref(node-set)"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3784 | return LY_EVALID; |
| 3785 | } |
| 3786 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3787 | lyxp_set_free_content(set); |
| 3788 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3789 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
| 3790 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 3791 | if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 3792 | if (sleaf->type->basetype == LY_TYPE_LEAFREF) { |
| 3793 | /* find leafref target */ |
Radek Krejci | 0b01330 | 2021-03-29 15:22:32 +0200 | [diff] [blame] | 3794 | if (lyplg_type_resolve_leafref((struct lysc_type_leafref *)sleaf->type, &leaf->node, &leaf->value, set->tree, |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 3795 | &node, &errmsg)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3796 | LOGERR(set->ctx, LY_EVALID, errmsg); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3797 | free(errmsg); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3798 | return LY_EVALID; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3799 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3800 | } else { |
| 3801 | assert(sleaf->type->basetype == LY_TYPE_INST); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3802 | if (ly_path_eval(leaf->value.target, set->tree, &node)) { |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3803 | LOGERR(set->ctx, LY_EVALID, "Invalid instance-identifier \"%s\" value - required instance not found.", |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 3804 | lyd_get_value(&leaf->node)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3805 | return LY_EVALID; |
| 3806 | } |
| 3807 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3808 | |
| 3809 | /* insert it */ |
| 3810 | set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3811 | } |
| 3812 | } |
| 3813 | |
| 3814 | return LY_SUCCESS; |
| 3815 | } |
| 3816 | |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3817 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3818 | 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] | 3819 | { |
Michal Vasko | 1fdd8fa | 2021-01-08 09:21:45 +0100 | [diff] [blame] | 3820 | uint32_t i; |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3821 | LY_ARRAY_COUNT_TYPE u; |
| 3822 | struct lyd_node_term *leaf; |
| 3823 | struct lysc_node_leaf *sleaf; |
| 3824 | struct lyd_meta *meta; |
Michal Vasko | 9392369 | 2021-05-07 15:28:02 +0200 | [diff] [blame] | 3825 | struct lyd_value *val; |
| 3826 | const struct lys_module *mod; |
| 3827 | const char *id_name; |
| 3828 | uint16_t id_len; |
| 3829 | struct lysc_ident *id; |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3830 | LY_ERR rc = LY_SUCCESS; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3831 | ly_bool found; |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3832 | |
| 3833 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3834 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3835 | 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] | 3836 | } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3837 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3838 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype), |
| 3839 | sleaf->name); |
| 3840 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) { |
| 3841 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", func, sleaf->name); |
| 3842 | } |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3843 | } |
| 3844 | |
| 3845 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 3846 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3847 | 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] | 3848 | sleaf->name); |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3849 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3850 | LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", func, sleaf->name); |
| 3851 | } |
| 3852 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3853 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3854 | return rc; |
| 3855 | } |
| 3856 | |
| 3857 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 3858 | 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] | 3859 | return LY_EVALID; |
| 3860 | } |
| 3861 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
| 3862 | LY_CHECK_RET(rc); |
| 3863 | |
Michal Vasko | 9392369 | 2021-05-07 15:28:02 +0200 | [diff] [blame] | 3864 | /* parse the identity */ |
| 3865 | id_name = args[1]->val.str; |
| 3866 | id_len = strlen(id_name); |
| 3867 | rc = moveto_resolve_model(&id_name, &id_len, set, set->cur_node ? set->cur_node->schema : NULL, &mod); |
| 3868 | LY_CHECK_RET(rc); |
| 3869 | if (!mod) { |
| 3870 | LOGVAL(set->ctx, LYVE_XPATH, "Identity \"%.*s\" without a prefix.", (int)id_len, id_name); |
| 3871 | return LY_EVALID; |
| 3872 | } |
| 3873 | |
| 3874 | /* find the identity */ |
| 3875 | found = 0; |
| 3876 | LY_ARRAY_FOR(mod->identities, u) { |
| 3877 | if (!ly_strncmp(mod->identities[u].name, id_name, id_len)) { |
| 3878 | /* we have match */ |
| 3879 | found = 1; |
| 3880 | break; |
| 3881 | } |
| 3882 | } |
| 3883 | if (!found) { |
| 3884 | LOGVAL(set->ctx, LYVE_XPATH, "Identity \"%.*s\" not found in module \"%s\".", (int)id_len, id_name, mod->name); |
| 3885 | return LY_EVALID; |
| 3886 | } |
| 3887 | id = &mod->identities[u]; |
| 3888 | |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3889 | set_fill_boolean(set, 0); |
| 3890 | found = 0; |
| 3891 | for (i = 0; i < args[0]->used; ++i) { |
| 3892 | if ((args[0]->val.nodes[i].type != LYXP_NODE_ELEM) && (args[0]->val.nodes[i].type != LYXP_NODE_META)) { |
| 3893 | continue; |
| 3894 | } |
| 3895 | |
| 3896 | if (args[0]->val.nodes[i].type == LYXP_NODE_ELEM) { |
| 3897 | leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node; |
| 3898 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 3899 | val = &leaf->value; |
| 3900 | if (!(sleaf->nodetype & LYD_NODE_TERM) || (leaf->value.realtype->basetype != LY_TYPE_IDENT)) { |
| 3901 | /* uninteresting */ |
| 3902 | continue; |
| 3903 | } |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3904 | } else { |
| 3905 | meta = args[0]->val.meta[i].meta; |
| 3906 | val = &meta->value; |
| 3907 | if (val->realtype->basetype != LY_TYPE_IDENT) { |
| 3908 | /* uninteresting */ |
| 3909 | continue; |
| 3910 | } |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3911 | } |
| 3912 | |
Michal Vasko | 9392369 | 2021-05-07 15:28:02 +0200 | [diff] [blame] | 3913 | /* check the identity itself */ |
| 3914 | if (self_match && (id == val->ident)) { |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3915 | set_fill_boolean(set, 1); |
| 3916 | found = 1; |
| 3917 | } |
Michal Vasko | 9392369 | 2021-05-07 15:28:02 +0200 | [diff] [blame] | 3918 | if (!found && !lyplg_type_identity_isderived(id, val->ident)) { |
| 3919 | set_fill_boolean(set, 1); |
| 3920 | found = 1; |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3921 | } |
| 3922 | |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3923 | if (found) { |
| 3924 | break; |
| 3925 | } |
| 3926 | } |
| 3927 | |
| 3928 | return LY_SUCCESS; |
| 3929 | } |
| 3930 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3931 | /** |
| 3932 | * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending |
| 3933 | * on whether the first argument nodes contain a node of an identity derived from the second |
| 3934 | * argument identity. |
| 3935 | * |
| 3936 | * @param[in] args Array of arguments. |
| 3937 | * @param[in] arg_count Count of elements in @p args. |
| 3938 | * @param[in,out] set Context and result set at the same time. |
| 3939 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3940 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3941 | */ |
| 3942 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3943 | xpath_derived_from(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3944 | { |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3945 | return xpath_derived_(args, set, options, 0, __func__); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3946 | } |
| 3947 | |
| 3948 | /** |
| 3949 | * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending |
| 3950 | * on whether the first argument nodes contain a node of an identity that either is or is derived from |
| 3951 | * the second argument identity. |
| 3952 | * |
| 3953 | * @param[in] args Array of arguments. |
| 3954 | * @param[in] arg_count Count of elements in @p args. |
| 3955 | * @param[in,out] set Context and result set at the same time. |
| 3956 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3957 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3958 | */ |
| 3959 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3960 | xpath_derived_from_or_self(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3961 | { |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3962 | return xpath_derived_(args, set, options, 1, __func__); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3963 | } |
| 3964 | |
| 3965 | /** |
| 3966 | * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER |
| 3967 | * with the integer value of the first node's enum value, otherwise NaN. |
| 3968 | * |
| 3969 | * @param[in] args Array of arguments. |
| 3970 | * @param[in] arg_count Count of elements in @p args. |
| 3971 | * @param[in,out] set Context and result set at the same time. |
| 3972 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3973 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3974 | */ |
| 3975 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3976 | xpath_enum_value(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3977 | { |
| 3978 | struct lyd_node_term *leaf; |
| 3979 | struct lysc_node_leaf *sleaf; |
| 3980 | LY_ERR rc = LY_SUCCESS; |
| 3981 | |
| 3982 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3983 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3984 | 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] | 3985 | } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3986 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3987 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 3988 | sleaf->name); |
| 3989 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) { |
| 3990 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"enumeration\".", __func__, sleaf->name); |
| 3991 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3992 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3993 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3994 | return rc; |
| 3995 | } |
| 3996 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3997 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 3998 | 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] | 3999 | return LY_EVALID; |
| 4000 | } |
| 4001 | |
| 4002 | set_fill_number(set, NAN); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4003 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4004 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
| 4005 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 4006 | if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) { |
| 4007 | set_fill_number(set, leaf->value.enum_item->value); |
| 4008 | } |
| 4009 | } |
| 4010 | |
| 4011 | return LY_SUCCESS; |
| 4012 | } |
| 4013 | |
| 4014 | /** |
| 4015 | * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN |
| 4016 | * with false value. |
| 4017 | * |
| 4018 | * @param[in] args Array of arguments. |
| 4019 | * @param[in] arg_count Count of elements in @p args. |
| 4020 | * @param[in,out] set Context and result set at the same time. |
| 4021 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4022 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4023 | */ |
| 4024 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4025 | xpath_false(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4026 | { |
| 4027 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4028 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4029 | return LY_SUCCESS; |
| 4030 | } |
| 4031 | |
| 4032 | set_fill_boolean(set, 0); |
| 4033 | return LY_SUCCESS; |
| 4034 | } |
| 4035 | |
| 4036 | /** |
| 4037 | * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER |
| 4038 | * with the first argument floored (truncated). |
| 4039 | * |
| 4040 | * @param[in] args Array of arguments. |
| 4041 | * @param[in] arg_count Count of elements in @p args. |
| 4042 | * @param[in,out] set Context and result set at the same time. |
| 4043 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4044 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4045 | */ |
| 4046 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4047 | xpath_floor(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t UNUSED(options)) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4048 | { |
| 4049 | LY_ERR rc; |
| 4050 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4051 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4052 | LY_CHECK_RET(rc); |
| 4053 | if (isfinite(args[0]->val.num)) { |
| 4054 | set_fill_number(set, (long long)args[0]->val.num); |
| 4055 | } |
| 4056 | |
| 4057 | return LY_SUCCESS; |
| 4058 | } |
| 4059 | |
| 4060 | /** |
| 4061 | * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN |
| 4062 | * whether the language of the text matches the one from the argument. |
| 4063 | * |
| 4064 | * @param[in] args Array of arguments. |
| 4065 | * @param[in] arg_count Count of elements in @p args. |
| 4066 | * @param[in,out] set Context and result set at the same time. |
| 4067 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4068 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4069 | */ |
| 4070 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4071 | xpath_lang(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4072 | { |
| 4073 | const struct lyd_node *node; |
| 4074 | struct lysc_node_leaf *sleaf; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4075 | struct lyd_meta *meta = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4076 | const char *val; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4077 | LY_ERR rc = LY_SUCCESS; |
| 4078 | |
| 4079 | if (options & LYXP_SCNODE_ALL) { |
| 4080 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4081 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4082 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 4083 | sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4084 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4085 | 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] | 4086 | } |
| 4087 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4088 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4089 | return rc; |
| 4090 | } |
| 4091 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4092 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4093 | LY_CHECK_RET(rc); |
| 4094 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4095 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4096 | 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] | 4097 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4098 | } else if (!set->used) { |
| 4099 | set_fill_boolean(set, 0); |
| 4100 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4101 | } |
| 4102 | |
| 4103 | switch (set->val.nodes[0].type) { |
| 4104 | case LYXP_NODE_ELEM: |
| 4105 | case LYXP_NODE_TEXT: |
| 4106 | node = set->val.nodes[0].node; |
| 4107 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4108 | case LYXP_NODE_META: |
| 4109 | node = set->val.meta[0].meta->parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4110 | break; |
| 4111 | default: |
| 4112 | /* nothing to do with roots */ |
| 4113 | set_fill_boolean(set, 0); |
| 4114 | return LY_SUCCESS; |
| 4115 | } |
| 4116 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4117 | /* find lang metadata */ |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 4118 | for ( ; node; node = lyd_parent(node)) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4119 | for (meta = node->meta; meta; meta = meta->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4120 | /* annotations */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4121 | 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] | 4122 | break; |
| 4123 | } |
| 4124 | } |
| 4125 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4126 | if (meta) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4127 | break; |
| 4128 | } |
| 4129 | } |
| 4130 | |
| 4131 | /* compare languages */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4132 | if (!meta) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4133 | set_fill_boolean(set, 0); |
| 4134 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4135 | uint64_t i; |
| 4136 | |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 4137 | val = lyd_get_meta_value(meta); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4138 | for (i = 0; args[0]->val.str[i]; ++i) { |
| 4139 | if (tolower(args[0]->val.str[i]) != tolower(val[i])) { |
| 4140 | set_fill_boolean(set, 0); |
| 4141 | break; |
| 4142 | } |
| 4143 | } |
| 4144 | if (!args[0]->val.str[i]) { |
| 4145 | if (!val[i] || (val[i] == '-')) { |
| 4146 | set_fill_boolean(set, 1); |
| 4147 | } else { |
| 4148 | set_fill_boolean(set, 0); |
| 4149 | } |
| 4150 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4151 | } |
| 4152 | |
| 4153 | return LY_SUCCESS; |
| 4154 | } |
| 4155 | |
| 4156 | /** |
| 4157 | * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER |
| 4158 | * with the context size. |
| 4159 | * |
| 4160 | * @param[in] args Array of arguments. |
| 4161 | * @param[in] arg_count Count of elements in @p args. |
| 4162 | * @param[in,out] set Context and result set at the same time. |
| 4163 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4164 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4165 | */ |
| 4166 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4167 | xpath_last(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4168 | { |
| 4169 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4170 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4171 | return LY_SUCCESS; |
| 4172 | } |
| 4173 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4174 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4175 | LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "last()"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4176 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4177 | } else if (!set->used) { |
| 4178 | set_fill_number(set, 0); |
| 4179 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4180 | } |
| 4181 | |
| 4182 | set_fill_number(set, set->ctx_size); |
| 4183 | return LY_SUCCESS; |
| 4184 | } |
| 4185 | |
| 4186 | /** |
| 4187 | * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING |
| 4188 | * with the node name without namespace from the argument or the context. |
| 4189 | * |
| 4190 | * @param[in] args Array of arguments. |
| 4191 | * @param[in] arg_count Count of elements in @p args. |
| 4192 | * @param[in,out] set Context and result set at the same time. |
| 4193 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4194 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4195 | */ |
| 4196 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4197 | xpath_local_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4198 | { |
| 4199 | struct lyxp_set_node *item; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 4200 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4201 | /* suppress unused variable warning */ |
| 4202 | (void)options; |
| 4203 | |
| 4204 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4205 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4206 | return LY_SUCCESS; |
| 4207 | } |
| 4208 | |
| 4209 | if (arg_count) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4210 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4211 | 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] | 4212 | "local-name(node-set?)"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4213 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4214 | } else if (!args[0]->used) { |
| 4215 | set_fill_string(set, "", 0); |
| 4216 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4217 | } |
| 4218 | |
| 4219 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4220 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4221 | |
| 4222 | item = &args[0]->val.nodes[0]; |
| 4223 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4224 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4225 | 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] | 4226 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4227 | } else if (!set->used) { |
| 4228 | set_fill_string(set, "", 0); |
| 4229 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4230 | } |
| 4231 | |
| 4232 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4233 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4234 | |
| 4235 | item = &set->val.nodes[0]; |
| 4236 | } |
| 4237 | |
| 4238 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4239 | case LYXP_NODE_NONE: |
| 4240 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4241 | case LYXP_NODE_ROOT: |
| 4242 | case LYXP_NODE_ROOT_CONFIG: |
| 4243 | case LYXP_NODE_TEXT: |
| 4244 | set_fill_string(set, "", 0); |
| 4245 | break; |
| 4246 | case LYXP_NODE_ELEM: |
| 4247 | set_fill_string(set, item->node->schema->name, strlen(item->node->schema->name)); |
| 4248 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4249 | case LYXP_NODE_META: |
| 4250 | 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] | 4251 | break; |
| 4252 | } |
| 4253 | |
| 4254 | return LY_SUCCESS; |
| 4255 | } |
| 4256 | |
| 4257 | /** |
| 4258 | * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING |
| 4259 | * with the node name fully qualified (with namespace) from the argument or the context. |
| 4260 | * |
| 4261 | * @param[in] args Array of arguments. |
| 4262 | * @param[in] arg_count Count of elements in @p args. |
| 4263 | * @param[in,out] set Context and result set at the same time. |
| 4264 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4265 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4266 | */ |
| 4267 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4268 | xpath_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4269 | { |
| 4270 | struct lyxp_set_node *item; |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 4271 | struct lys_module *mod = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4272 | char *str; |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 4273 | const char *name = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4274 | |
| 4275 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4276 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4277 | return LY_SUCCESS; |
| 4278 | } |
| 4279 | |
| 4280 | if (arg_count) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4281 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4282 | 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] | 4283 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4284 | } else if (!args[0]->used) { |
| 4285 | set_fill_string(set, "", 0); |
| 4286 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4287 | } |
| 4288 | |
| 4289 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4290 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4291 | |
| 4292 | item = &args[0]->val.nodes[0]; |
| 4293 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4294 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4295 | LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "name(node-set?)"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4296 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4297 | } else if (!set->used) { |
| 4298 | set_fill_string(set, "", 0); |
| 4299 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4300 | } |
| 4301 | |
| 4302 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4303 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4304 | |
| 4305 | item = &set->val.nodes[0]; |
| 4306 | } |
| 4307 | |
| 4308 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4309 | case LYXP_NODE_NONE: |
| 4310 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4311 | case LYXP_NODE_ROOT: |
| 4312 | case LYXP_NODE_ROOT_CONFIG: |
| 4313 | case LYXP_NODE_TEXT: |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 4314 | /* keep NULL */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4315 | break; |
| 4316 | case LYXP_NODE_ELEM: |
| 4317 | mod = item->node->schema->module; |
| 4318 | name = item->node->schema->name; |
| 4319 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4320 | case LYXP_NODE_META: |
| 4321 | mod = ((struct lyd_meta *)item->node)->annotation->module; |
| 4322 | name = ((struct lyd_meta *)item->node)->name; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4323 | break; |
| 4324 | } |
| 4325 | |
| 4326 | if (mod && name) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 4327 | int rc = asprintf(&str, "%s:%s", ly_get_prefix(mod, set->format, set->prefix_data), name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4328 | LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM); |
| 4329 | set_fill_string(set, str, strlen(str)); |
| 4330 | free(str); |
| 4331 | } else { |
| 4332 | set_fill_string(set, "", 0); |
| 4333 | } |
| 4334 | |
| 4335 | return LY_SUCCESS; |
| 4336 | } |
| 4337 | |
| 4338 | /** |
| 4339 | * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING |
| 4340 | * with the namespace of the node from the argument or the context. |
| 4341 | * |
| 4342 | * @param[in] args Array of arguments. |
| 4343 | * @param[in] arg_count Count of elements in @p args. |
| 4344 | * @param[in,out] set Context and result set at the same time. |
| 4345 | * @param[in] options XPath options. |
| 4346 | * @return LY_ERR (LY_EINVAL for wrong arguments on schema) |
| 4347 | */ |
| 4348 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4349 | xpath_namespace_uri(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4350 | { |
| 4351 | struct lyxp_set_node *item; |
| 4352 | struct lys_module *mod; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 4353 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4354 | /* suppress unused variable warning */ |
| 4355 | (void)options; |
| 4356 | |
| 4357 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4358 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4359 | return LY_SUCCESS; |
| 4360 | } |
| 4361 | |
| 4362 | if (arg_count) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4363 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4364 | 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] | 4365 | "namespace-uri(node-set?)"); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4366 | return LY_EVALID; |
| 4367 | } else if (!args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4368 | set_fill_string(set, "", 0); |
| 4369 | return LY_SUCCESS; |
| 4370 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4371 | |
| 4372 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4373 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4374 | |
| 4375 | item = &args[0]->val.nodes[0]; |
| 4376 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4377 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4378 | 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] | 4379 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4380 | } else if (!set->used) { |
| 4381 | set_fill_string(set, "", 0); |
| 4382 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4383 | } |
| 4384 | |
| 4385 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4386 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4387 | |
| 4388 | item = &set->val.nodes[0]; |
| 4389 | } |
| 4390 | |
| 4391 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4392 | case LYXP_NODE_NONE: |
| 4393 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4394 | case LYXP_NODE_ROOT: |
| 4395 | case LYXP_NODE_ROOT_CONFIG: |
| 4396 | case LYXP_NODE_TEXT: |
| 4397 | set_fill_string(set, "", 0); |
| 4398 | break; |
| 4399 | case LYXP_NODE_ELEM: |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4400 | case LYXP_NODE_META: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4401 | if (item->type == LYXP_NODE_ELEM) { |
| 4402 | mod = item->node->schema->module; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4403 | } else { /* LYXP_NODE_META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4404 | /* annotations */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4405 | mod = ((struct lyd_meta *)item->node)->annotation->module; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4406 | } |
| 4407 | |
| 4408 | set_fill_string(set, mod->ns, strlen(mod->ns)); |
| 4409 | break; |
| 4410 | } |
| 4411 | |
| 4412 | return LY_SUCCESS; |
| 4413 | } |
| 4414 | |
| 4415 | /** |
| 4416 | * @brief Execute the XPath node() function (node type). Returns LYXP_SET_NODE_SET |
| 4417 | * with only nodes from the context. In practice it either leaves the context |
| 4418 | * as it is or returns an empty node set. |
| 4419 | * |
| 4420 | * @param[in] args Array of arguments. |
| 4421 | * @param[in] arg_count Count of elements in @p args. |
| 4422 | * @param[in,out] set Context and result set at the same time. |
| 4423 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4424 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4425 | */ |
| 4426 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4427 | xpath_node(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4428 | { |
| 4429 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4430 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4431 | return LY_SUCCESS; |
| 4432 | } |
| 4433 | |
| 4434 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4435 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4436 | } |
| 4437 | return LY_SUCCESS; |
| 4438 | } |
| 4439 | |
| 4440 | /** |
| 4441 | * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING |
| 4442 | * with normalized value (no leading, trailing, double white spaces) of the node |
| 4443 | * from the argument or the context. |
| 4444 | * |
| 4445 | * @param[in] args Array of arguments. |
| 4446 | * @param[in] arg_count Count of elements in @p args. |
| 4447 | * @param[in,out] set Context and result set at the same time. |
| 4448 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4449 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4450 | */ |
| 4451 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4452 | xpath_normalize_space(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4453 | { |
| 4454 | uint16_t i, new_used; |
| 4455 | char *new; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 4456 | ly_bool have_spaces = 0, space_before = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4457 | struct lysc_node_leaf *sleaf; |
| 4458 | LY_ERR rc = LY_SUCCESS; |
| 4459 | |
| 4460 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4461 | if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && |
| 4462 | (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4463 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4464 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 4465 | sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4466 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4467 | 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] | 4468 | } |
| 4469 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4470 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4471 | return rc; |
| 4472 | } |
| 4473 | |
| 4474 | if (arg_count) { |
| 4475 | set_fill_set(set, args[0]); |
| 4476 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4477 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4478 | LY_CHECK_RET(rc); |
| 4479 | |
| 4480 | /* is there any normalization necessary? */ |
| 4481 | for (i = 0; set->val.str[i]; ++i) { |
| 4482 | if (is_xmlws(set->val.str[i])) { |
| 4483 | if ((i == 0) || space_before || (!set->val.str[i + 1])) { |
| 4484 | have_spaces = 1; |
| 4485 | break; |
| 4486 | } |
| 4487 | space_before = 1; |
| 4488 | } else { |
| 4489 | space_before = 0; |
| 4490 | } |
| 4491 | } |
| 4492 | |
| 4493 | /* yep, there is */ |
| 4494 | if (have_spaces) { |
| 4495 | /* it's enough, at least one character will go, makes space for ending '\0' */ |
| 4496 | new = malloc(strlen(set->val.str) * sizeof(char)); |
| 4497 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 4498 | new_used = 0; |
| 4499 | |
| 4500 | space_before = 0; |
| 4501 | for (i = 0; set->val.str[i]; ++i) { |
| 4502 | if (is_xmlws(set->val.str[i])) { |
| 4503 | if ((i == 0) || space_before) { |
| 4504 | space_before = 1; |
| 4505 | continue; |
| 4506 | } else { |
| 4507 | space_before = 1; |
| 4508 | } |
| 4509 | } else { |
| 4510 | space_before = 0; |
| 4511 | } |
| 4512 | |
| 4513 | new[new_used] = (space_before ? ' ' : set->val.str[i]); |
| 4514 | ++new_used; |
| 4515 | } |
| 4516 | |
| 4517 | /* at worst there is one trailing space now */ |
| 4518 | if (new_used && is_xmlws(new[new_used - 1])) { |
| 4519 | --new_used; |
| 4520 | } |
| 4521 | |
| 4522 | new = ly_realloc(new, (new_used + 1) * sizeof(char)); |
| 4523 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 4524 | new[new_used] = '\0'; |
| 4525 | |
| 4526 | free(set->val.str); |
| 4527 | set->val.str = new; |
| 4528 | } |
| 4529 | |
| 4530 | return LY_SUCCESS; |
| 4531 | } |
| 4532 | |
| 4533 | /** |
| 4534 | * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN |
| 4535 | * with the argument converted to boolean and logically inverted. |
| 4536 | * |
| 4537 | * @param[in] args Array of arguments. |
| 4538 | * @param[in] arg_count Count of elements in @p args. |
| 4539 | * @param[in,out] set Context and result set at the same time. |
| 4540 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4541 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4542 | */ |
| 4543 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4544 | xpath_not(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4545 | { |
| 4546 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4547 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4548 | return LY_SUCCESS; |
| 4549 | } |
| 4550 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4551 | lyxp_set_cast(args[0], LYXP_SET_BOOLEAN); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4552 | if (args[0]->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4553 | set_fill_boolean(set, 0); |
| 4554 | } else { |
| 4555 | set_fill_boolean(set, 1); |
| 4556 | } |
| 4557 | |
| 4558 | return LY_SUCCESS; |
| 4559 | } |
| 4560 | |
| 4561 | /** |
| 4562 | * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER |
| 4563 | * with the number representation of either the argument or the context. |
| 4564 | * |
| 4565 | * @param[in] args Array of arguments. |
| 4566 | * @param[in] arg_count Count of elements in @p args. |
| 4567 | * @param[in,out] set Context and result set at the same time. |
| 4568 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4569 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4570 | */ |
| 4571 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4572 | xpath_number(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4573 | { |
| 4574 | LY_ERR rc; |
| 4575 | |
| 4576 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4577 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4578 | return LY_SUCCESS; |
| 4579 | } |
| 4580 | |
| 4581 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4582 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4583 | LY_CHECK_RET(rc); |
| 4584 | set_fill_set(set, args[0]); |
| 4585 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4586 | rc = lyxp_set_cast(set, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4587 | LY_CHECK_RET(rc); |
| 4588 | } |
| 4589 | |
| 4590 | return LY_SUCCESS; |
| 4591 | } |
| 4592 | |
| 4593 | /** |
| 4594 | * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER |
| 4595 | * with the context position. |
| 4596 | * |
| 4597 | * @param[in] args Array of arguments. |
| 4598 | * @param[in] arg_count Count of elements in @p args. |
| 4599 | * @param[in,out] set Context and result set at the same time. |
| 4600 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4601 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4602 | */ |
| 4603 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4604 | xpath_position(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4605 | { |
| 4606 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4607 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4608 | return LY_SUCCESS; |
| 4609 | } |
| 4610 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4611 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4612 | LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "position()"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4613 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4614 | } else if (!set->used) { |
| 4615 | set_fill_number(set, 0); |
| 4616 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4617 | } |
| 4618 | |
| 4619 | set_fill_number(set, set->ctx_pos); |
| 4620 | |
| 4621 | /* UNUSED in 'Release' build type */ |
| 4622 | (void)options; |
| 4623 | return LY_SUCCESS; |
| 4624 | } |
| 4625 | |
| 4626 | /** |
| 4627 | * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN |
| 4628 | * depending on whether the second argument regex matches the first argument string. For details refer to |
| 4629 | * YANG 1.1 RFC section 10.2.1. |
| 4630 | * |
| 4631 | * @param[in] args Array of arguments. |
| 4632 | * @param[in] arg_count Count of elements in @p args. |
| 4633 | * @param[in,out] set Context and result set at the same time. |
| 4634 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4635 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4636 | */ |
| 4637 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4638 | xpath_re_match(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4639 | { |
| 4640 | struct lysc_pattern **patterns = NULL, **pattern; |
| 4641 | struct lysc_node_leaf *sleaf; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4642 | LY_ERR rc = LY_SUCCESS; |
| 4643 | struct ly_err_item *err; |
| 4644 | |
| 4645 | if (options & LYXP_SCNODE_ALL) { |
| 4646 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4647 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4648 | 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] | 4649 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4650 | 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] | 4651 | } |
| 4652 | } |
| 4653 | |
| 4654 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4655 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4656 | 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] | 4657 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4658 | 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] | 4659 | } |
| 4660 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4661 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4662 | return rc; |
| 4663 | } |
| 4664 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4665 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4666 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4667 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4668 | LY_CHECK_RET(rc); |
| 4669 | |
| 4670 | LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM); |
Radek Iša | 45802b5 | 2021-02-09 09:21:58 +0100 | [diff] [blame] | 4671 | *pattern = calloc(1, sizeof **pattern); |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 4672 | LOG_LOCSET(NULL, set->cur_node, NULL, NULL); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4673 | rc = lys_compile_type_pattern_check(set->ctx, args[1]->val.str, &(*pattern)->code); |
Michal Vasko | 0245d50 | 2021-12-13 17:05:06 +0100 | [diff] [blame] | 4674 | if (set->cur_node) { |
| 4675 | LOG_LOCBACK(0, 1, 0, 0); |
| 4676 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4677 | if (rc != LY_SUCCESS) { |
| 4678 | LY_ARRAY_FREE(patterns); |
| 4679 | return rc; |
| 4680 | } |
| 4681 | |
Radek Krejci | 0b01330 | 2021-03-29 15:22:32 +0200 | [diff] [blame] | 4682 | 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] | 4683 | pcre2_code_free((*pattern)->code); |
| 4684 | free(*pattern); |
| 4685 | LY_ARRAY_FREE(patterns); |
| 4686 | if (rc && (rc != LY_EVALID)) { |
Michal Vasko | 177d0ed | 2020-11-23 16:43:03 +0100 | [diff] [blame] | 4687 | ly_err_print(set->ctx, err); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4688 | ly_err_free(err); |
| 4689 | return rc; |
| 4690 | } |
| 4691 | |
| 4692 | if (rc == LY_EVALID) { |
| 4693 | ly_err_free(err); |
| 4694 | set_fill_boolean(set, 0); |
| 4695 | } else { |
| 4696 | set_fill_boolean(set, 1); |
| 4697 | } |
| 4698 | |
| 4699 | return LY_SUCCESS; |
| 4700 | } |
| 4701 | |
| 4702 | /** |
| 4703 | * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER |
| 4704 | * with the rounded first argument. For details refer to |
| 4705 | * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round. |
| 4706 | * |
| 4707 | * @param[in] args Array of arguments. |
| 4708 | * @param[in] arg_count Count of elements in @p args. |
| 4709 | * @param[in,out] set Context and result set at the same time. |
| 4710 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4711 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4712 | */ |
| 4713 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4714 | xpath_round(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4715 | { |
| 4716 | struct lysc_node_leaf *sleaf; |
| 4717 | LY_ERR rc = LY_SUCCESS; |
| 4718 | |
| 4719 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 4720 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4721 | 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] | 4722 | } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4723 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4724 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 4725 | sleaf->name); |
| 4726 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) { |
| 4727 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name); |
| 4728 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4729 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4730 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4731 | return rc; |
| 4732 | } |
| 4733 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4734 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4735 | LY_CHECK_RET(rc); |
| 4736 | |
| 4737 | /* cover only the cases where floor can't be used */ |
| 4738 | if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) { |
| 4739 | set_fill_number(set, -0.0f); |
| 4740 | } else { |
| 4741 | args[0]->val.num += 0.5; |
| 4742 | rc = xpath_floor(args, 1, args[0], options); |
| 4743 | LY_CHECK_RET(rc); |
| 4744 | set_fill_number(set, args[0]->val.num); |
| 4745 | } |
| 4746 | |
| 4747 | return LY_SUCCESS; |
| 4748 | } |
| 4749 | |
| 4750 | /** |
| 4751 | * @brief Execute the XPath starts-with(string, string) function. |
| 4752 | * Returns LYXP_SET_BOOLEAN whether the second argument is |
| 4753 | * the prefix of the first or not. |
| 4754 | * |
| 4755 | * @param[in] args Array of arguments. |
| 4756 | * @param[in] arg_count Count of elements in @p args. |
| 4757 | * @param[in,out] set Context and result set at the same time. |
| 4758 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4759 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4760 | */ |
| 4761 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4762 | xpath_starts_with(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4763 | { |
| 4764 | struct lysc_node_leaf *sleaf; |
| 4765 | LY_ERR rc = LY_SUCCESS; |
| 4766 | |
| 4767 | if (options & LYXP_SCNODE_ALL) { |
| 4768 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4769 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4770 | 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] | 4771 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4772 | 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] | 4773 | } |
| 4774 | } |
| 4775 | |
| 4776 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4777 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4778 | 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] | 4779 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4780 | 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] | 4781 | } |
| 4782 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4783 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4784 | return rc; |
| 4785 | } |
| 4786 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4787 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4788 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4789 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4790 | LY_CHECK_RET(rc); |
| 4791 | |
| 4792 | if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) { |
| 4793 | set_fill_boolean(set, 0); |
| 4794 | } else { |
| 4795 | set_fill_boolean(set, 1); |
| 4796 | } |
| 4797 | |
| 4798 | return LY_SUCCESS; |
| 4799 | } |
| 4800 | |
| 4801 | /** |
| 4802 | * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING |
| 4803 | * with the string representation of either the argument or the context. |
| 4804 | * |
| 4805 | * @param[in] args Array of arguments. |
| 4806 | * @param[in] arg_count Count of elements in @p args. |
| 4807 | * @param[in,out] set Context and result set at the same time. |
| 4808 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4809 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4810 | */ |
| 4811 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4812 | xpath_string(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4813 | { |
| 4814 | LY_ERR rc; |
| 4815 | |
| 4816 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4817 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4818 | return LY_SUCCESS; |
| 4819 | } |
| 4820 | |
| 4821 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4822 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4823 | LY_CHECK_RET(rc); |
| 4824 | set_fill_set(set, args[0]); |
| 4825 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4826 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4827 | LY_CHECK_RET(rc); |
| 4828 | } |
| 4829 | |
| 4830 | return LY_SUCCESS; |
| 4831 | } |
| 4832 | |
| 4833 | /** |
| 4834 | * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER |
| 4835 | * with the length of the string in either the argument or the context. |
| 4836 | * |
| 4837 | * @param[in] args Array of arguments. |
| 4838 | * @param[in] arg_count Count of elements in @p args. |
| 4839 | * @param[in,out] set Context and result set at the same time. |
| 4840 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4841 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4842 | */ |
| 4843 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4844 | xpath_string_length(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4845 | { |
| 4846 | struct lysc_node_leaf *sleaf; |
| 4847 | LY_ERR rc = LY_SUCCESS; |
| 4848 | |
| 4849 | if (options & LYXP_SCNODE_ALL) { |
| 4850 | if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4851 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4852 | 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] | 4853 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4854 | 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] | 4855 | } |
| 4856 | } |
| 4857 | if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) { |
| 4858 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4859 | 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] | 4860 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4861 | 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] | 4862 | } |
| 4863 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4864 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4865 | return rc; |
| 4866 | } |
| 4867 | |
| 4868 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4869 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4870 | LY_CHECK_RET(rc); |
| 4871 | set_fill_number(set, strlen(args[0]->val.str)); |
| 4872 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4873 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4874 | LY_CHECK_RET(rc); |
| 4875 | set_fill_number(set, strlen(set->val.str)); |
| 4876 | } |
| 4877 | |
| 4878 | return LY_SUCCESS; |
| 4879 | } |
| 4880 | |
| 4881 | /** |
| 4882 | * @brief Execute the XPath substring(string, number, number?) function. |
| 4883 | * Returns LYXP_SET_STRING substring of the first argument starting |
| 4884 | * on the second argument index ending on the third argument index, |
| 4885 | * indexed from 1. For exact definition refer to |
| 4886 | * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring. |
| 4887 | * |
| 4888 | * @param[in] args Array of arguments. |
| 4889 | * @param[in] arg_count Count of elements in @p args. |
| 4890 | * @param[in,out] set Context and result set at the same time. |
| 4891 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4892 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4893 | */ |
| 4894 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4895 | xpath_substring(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4896 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4897 | int32_t start, len; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4898 | uint16_t str_start, str_len, pos; |
| 4899 | struct lysc_node_leaf *sleaf; |
| 4900 | LY_ERR rc = LY_SUCCESS; |
| 4901 | |
| 4902 | if (options & LYXP_SCNODE_ALL) { |
| 4903 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4904 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4905 | 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] | 4906 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4907 | 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] | 4908 | } |
| 4909 | } |
| 4910 | |
| 4911 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4912 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4913 | 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] | 4914 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 4915 | 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] | 4916 | } |
| 4917 | } |
| 4918 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 4919 | if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET) && |
| 4920 | (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4921 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4922 | 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] | 4923 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 4924 | 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] | 4925 | } |
| 4926 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4927 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4928 | return rc; |
| 4929 | } |
| 4930 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4931 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4932 | LY_CHECK_RET(rc); |
| 4933 | |
| 4934 | /* start */ |
| 4935 | if (xpath_round(&args[1], 1, args[1], options)) { |
| 4936 | return -1; |
| 4937 | } |
| 4938 | if (isfinite(args[1]->val.num)) { |
| 4939 | start = args[1]->val.num - 1; |
| 4940 | } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4941 | start = INT32_MIN; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4942 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4943 | start = INT32_MAX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4944 | } |
| 4945 | |
| 4946 | /* len */ |
| 4947 | if (arg_count == 3) { |
| 4948 | rc = xpath_round(&args[2], 1, args[2], options); |
| 4949 | LY_CHECK_RET(rc); |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4950 | if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4951 | len = 0; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4952 | } else if (isfinite(args[2]->val.num)) { |
| 4953 | len = args[2]->val.num; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4954 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4955 | len = INT32_MAX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4956 | } |
| 4957 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4958 | len = INT32_MAX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4959 | } |
| 4960 | |
| 4961 | /* find matching character positions */ |
| 4962 | str_start = 0; |
| 4963 | str_len = 0; |
| 4964 | for (pos = 0; args[0]->val.str[pos]; ++pos) { |
| 4965 | if (pos < start) { |
| 4966 | ++str_start; |
| 4967 | } else if (pos < start + len) { |
| 4968 | ++str_len; |
| 4969 | } else { |
| 4970 | break; |
| 4971 | } |
| 4972 | } |
| 4973 | |
| 4974 | set_fill_string(set, args[0]->val.str + str_start, str_len); |
| 4975 | return LY_SUCCESS; |
| 4976 | } |
| 4977 | |
| 4978 | /** |
| 4979 | * @brief Execute the XPath substring-after(string, string) function. |
| 4980 | * Returns LYXP_SET_STRING with the string succeeding the occurance |
| 4981 | * of the second argument in the first or an empty string. |
| 4982 | * |
| 4983 | * @param[in] args Array of arguments. |
| 4984 | * @param[in] arg_count Count of elements in @p args. |
| 4985 | * @param[in,out] set Context and result set at the same time. |
| 4986 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4987 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4988 | */ |
| 4989 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4990 | xpath_substring_after(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4991 | { |
| 4992 | char *ptr; |
| 4993 | struct lysc_node_leaf *sleaf; |
| 4994 | LY_ERR rc = LY_SUCCESS; |
| 4995 | |
| 4996 | if (options & LYXP_SCNODE_ALL) { |
| 4997 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4998 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4999 | 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] | 5000 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5001 | 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] | 5002 | } |
| 5003 | } |
| 5004 | |
| 5005 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 5006 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5007 | 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] | 5008 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5009 | 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] | 5010 | } |
| 5011 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5012 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5013 | return rc; |
| 5014 | } |
| 5015 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5016 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5017 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5018 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5019 | LY_CHECK_RET(rc); |
| 5020 | |
| 5021 | ptr = strstr(args[0]->val.str, args[1]->val.str); |
| 5022 | if (ptr) { |
| 5023 | set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str))); |
| 5024 | } else { |
| 5025 | set_fill_string(set, "", 0); |
| 5026 | } |
| 5027 | |
| 5028 | return LY_SUCCESS; |
| 5029 | } |
| 5030 | |
| 5031 | /** |
| 5032 | * @brief Execute the XPath substring-before(string, string) function. |
| 5033 | * Returns LYXP_SET_STRING with the string preceding the occurance |
| 5034 | * of the second argument in the first or an empty string. |
| 5035 | * |
| 5036 | * @param[in] args Array of arguments. |
| 5037 | * @param[in] arg_count Count of elements in @p args. |
| 5038 | * @param[in,out] set Context and result set at the same time. |
| 5039 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5040 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5041 | */ |
| 5042 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5043 | xpath_substring_before(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5044 | { |
| 5045 | char *ptr; |
| 5046 | struct lysc_node_leaf *sleaf; |
| 5047 | LY_ERR rc = LY_SUCCESS; |
| 5048 | |
| 5049 | if (options & LYXP_SCNODE_ALL) { |
| 5050 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 5051 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5052 | 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] | 5053 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5054 | 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] | 5055 | } |
| 5056 | } |
| 5057 | |
| 5058 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 5059 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5060 | 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] | 5061 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5062 | 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] | 5063 | } |
| 5064 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5065 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5066 | return rc; |
| 5067 | } |
| 5068 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5069 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5070 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5071 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5072 | LY_CHECK_RET(rc); |
| 5073 | |
| 5074 | ptr = strstr(args[0]->val.str, args[1]->val.str); |
| 5075 | if (ptr) { |
| 5076 | set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str); |
| 5077 | } else { |
| 5078 | set_fill_string(set, "", 0); |
| 5079 | } |
| 5080 | |
| 5081 | return LY_SUCCESS; |
| 5082 | } |
| 5083 | |
| 5084 | /** |
| 5085 | * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER |
| 5086 | * with the sum of all the nodes in the context. |
| 5087 | * |
| 5088 | * @param[in] args Array of arguments. |
| 5089 | * @param[in] arg_count Count of elements in @p args. |
| 5090 | * @param[in,out] set Context and result set at the same time. |
| 5091 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5092 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5093 | */ |
| 5094 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5095 | xpath_sum(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5096 | { |
| 5097 | long double num; |
| 5098 | char *str; |
Michal Vasko | 1fdd8fa | 2021-01-08 09:21:45 +0100 | [diff] [blame] | 5099 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5100 | struct lyxp_set set_item; |
| 5101 | struct lysc_node_leaf *sleaf; |
| 5102 | LY_ERR rc = LY_SUCCESS; |
| 5103 | |
| 5104 | if (options & LYXP_SCNODE_ALL) { |
| 5105 | if (args[0]->type == LYXP_SET_SCNODE_SET) { |
| 5106 | for (i = 0; i < args[0]->used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 5107 | 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] | 5108 | sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode; |
| 5109 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5110 | 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] | 5111 | lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5112 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 5113 | 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] | 5114 | } |
| 5115 | } |
| 5116 | } |
| 5117 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5118 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5119 | return rc; |
| 5120 | } |
| 5121 | |
| 5122 | set_fill_number(set, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5123 | |
| 5124 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 5125 | 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] | 5126 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5127 | } else if (!args[0]->used) { |
| 5128 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5129 | } |
| 5130 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5131 | set_init(&set_item, set); |
| 5132 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5133 | set_item.type = LYXP_SET_NODE_SET; |
Michal Vasko | 41decbf | 2021-11-02 11:50:21 +0100 | [diff] [blame] | 5134 | set_item.val.nodes = calloc(1, sizeof *set_item.val.nodes); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5135 | LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM); |
| 5136 | |
| 5137 | set_item.used = 1; |
| 5138 | set_item.size = 1; |
| 5139 | |
| 5140 | for (i = 0; i < args[0]->used; ++i) { |
| 5141 | set_item.val.nodes[0] = args[0]->val.nodes[i]; |
| 5142 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5143 | rc = cast_node_set_to_string(&set_item, &str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5144 | LY_CHECK_RET(rc); |
| 5145 | num = cast_string_to_number(str); |
| 5146 | free(str); |
| 5147 | set->val.num += num; |
| 5148 | } |
| 5149 | |
| 5150 | free(set_item.val.nodes); |
| 5151 | |
| 5152 | return LY_SUCCESS; |
| 5153 | } |
| 5154 | |
| 5155 | /** |
| 5156 | * @brief Execute the XPath text() function (node type). Returns LYXP_SET_NODE_SET |
| 5157 | * with the text content of the nodes in the context. |
| 5158 | * |
| 5159 | * @param[in] args Array of arguments. |
| 5160 | * @param[in] arg_count Count of elements in @p args. |
| 5161 | * @param[in,out] set Context and result set at the same time. |
| 5162 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5163 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5164 | */ |
| 5165 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5166 | xpath_text(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5167 | { |
| 5168 | uint32_t i; |
| 5169 | |
| 5170 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5171 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5172 | return LY_SUCCESS; |
| 5173 | } |
| 5174 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5175 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 5176 | LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "text()"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5177 | return LY_EVALID; |
| 5178 | } |
| 5179 | |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 5180 | for (i = 0; i < set->used; ) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5181 | switch (set->val.nodes[i].type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 5182 | case LYXP_NODE_NONE: |
| 5183 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5184 | case LYXP_NODE_ELEM: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5185 | if (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 5186 | set->val.nodes[i].type = LYXP_NODE_TEXT; |
| 5187 | ++i; |
| 5188 | break; |
| 5189 | } |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 5190 | /* fall through */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5191 | case LYXP_NODE_ROOT: |
| 5192 | case LYXP_NODE_ROOT_CONFIG: |
| 5193 | case LYXP_NODE_TEXT: |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5194 | case LYXP_NODE_META: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5195 | set_remove_node(set, i); |
| 5196 | break; |
| 5197 | } |
| 5198 | } |
| 5199 | |
| 5200 | return LY_SUCCESS; |
| 5201 | } |
| 5202 | |
| 5203 | /** |
| 5204 | * @brief Execute the XPath translate(string, string, string) function. |
| 5205 | * Returns LYXP_SET_STRING with the first argument with the characters |
| 5206 | * from the second argument replaced by those on the corresponding |
| 5207 | * positions in the third argument. |
| 5208 | * |
| 5209 | * @param[in] args Array of arguments. |
| 5210 | * @param[in] arg_count Count of elements in @p args. |
| 5211 | * @param[in,out] set Context and result set at the same time. |
| 5212 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5213 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5214 | */ |
| 5215 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5216 | xpath_translate(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5217 | { |
| 5218 | uint16_t i, j, new_used; |
| 5219 | char *new; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 5220 | ly_bool have_removed; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5221 | struct lysc_node_leaf *sleaf; |
| 5222 | LY_ERR rc = LY_SUCCESS; |
| 5223 | |
| 5224 | if (options & LYXP_SCNODE_ALL) { |
| 5225 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 5226 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5227 | 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] | 5228 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5229 | 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] | 5230 | } |
| 5231 | } |
| 5232 | |
| 5233 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 5234 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5235 | 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] | 5236 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5237 | 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] | 5238 | } |
| 5239 | } |
| 5240 | |
| 5241 | if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) { |
| 5242 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5243 | 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] | 5244 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5245 | 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] | 5246 | } |
| 5247 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5248 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5249 | return rc; |
| 5250 | } |
| 5251 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5252 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5253 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5254 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5255 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5256 | rc = lyxp_set_cast(args[2], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5257 | LY_CHECK_RET(rc); |
| 5258 | |
| 5259 | new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char)); |
| 5260 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 5261 | new_used = 0; |
| 5262 | |
| 5263 | have_removed = 0; |
| 5264 | for (i = 0; args[0]->val.str[i]; ++i) { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 5265 | ly_bool found = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5266 | |
| 5267 | for (j = 0; args[1]->val.str[j]; ++j) { |
| 5268 | if (args[0]->val.str[i] == args[1]->val.str[j]) { |
| 5269 | /* removing this char */ |
| 5270 | if (j >= strlen(args[2]->val.str)) { |
| 5271 | have_removed = 1; |
| 5272 | found = 1; |
| 5273 | break; |
| 5274 | } |
| 5275 | /* replacing this char */ |
| 5276 | new[new_used] = args[2]->val.str[j]; |
| 5277 | ++new_used; |
| 5278 | found = 1; |
| 5279 | break; |
| 5280 | } |
| 5281 | } |
| 5282 | |
| 5283 | /* copying this char */ |
| 5284 | if (!found) { |
| 5285 | new[new_used] = args[0]->val.str[i]; |
| 5286 | ++new_used; |
| 5287 | } |
| 5288 | } |
| 5289 | |
| 5290 | if (have_removed) { |
| 5291 | new = ly_realloc(new, (new_used + 1) * sizeof(char)); |
| 5292 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 5293 | } |
| 5294 | new[new_used] = '\0'; |
| 5295 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5296 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5297 | set->type = LYXP_SET_STRING; |
| 5298 | set->val.str = new; |
| 5299 | |
| 5300 | return LY_SUCCESS; |
| 5301 | } |
| 5302 | |
| 5303 | /** |
| 5304 | * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN |
| 5305 | * with true value. |
| 5306 | * |
| 5307 | * @param[in] args Array of arguments. |
| 5308 | * @param[in] arg_count Count of elements in @p args. |
| 5309 | * @param[in,out] set Context and result set at the same time. |
| 5310 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5311 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5312 | */ |
| 5313 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5314 | xpath_true(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5315 | { |
| 5316 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5317 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5318 | return LY_SUCCESS; |
| 5319 | } |
| 5320 | |
| 5321 | set_fill_boolean(set, 1); |
| 5322 | return LY_SUCCESS; |
| 5323 | } |
| 5324 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5325 | /** |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5326 | * @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] | 5327 | * |
Michal Vasko | 2104e9f | 2020-03-06 08:23:25 +0100 | [diff] [blame] | 5328 | * XPath @p set is expected to be a (sc)node set! |
| 5329 | * |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5330 | * @param[in,out] qname Qualified node name. If includes prefix, it is skipped. |
| 5331 | * @param[in,out] qname_len Length of @p qname, is updated accordingly. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5332 | * @param[in] set Set with general XPath context. |
| 5333 | * @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] | 5334 | * @param[out] moveto_mod Expected module of a matching node. |
| 5335 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5336 | */ |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5337 | static LY_ERR |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5338 | moveto_resolve_model(const char **qname, uint16_t *qname_len, const struct lyxp_set *set, |
| 5339 | const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5340 | { |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 5341 | const struct lys_module *mod = NULL; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5342 | const char *ptr; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5343 | size_t pref_len; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5344 | |
Michal Vasko | 2104e9f | 2020-03-06 08:23:25 +0100 | [diff] [blame] | 5345 | assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET)); |
| 5346 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5347 | if ((ptr = ly_strnchr(*qname, ':', *qname_len))) { |
| 5348 | /* specific module */ |
| 5349 | pref_len = ptr - *qname; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5350 | 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] | 5351 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5352 | /* check for errors and non-implemented modules, as they are not valid */ |
Juraj Vijtiuk | d75faa6 | 2019-11-26 14:10:10 +0100 | [diff] [blame] | 5353 | if (!mod || !mod->implemented) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 5354 | LOGVAL(set->ctx, LY_VCODE_XP_INMOD, pref_len, *qname); |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5355 | return LY_EVALID; |
| 5356 | } |
Juraj Vijtiuk | d75faa6 | 2019-11-26 14:10:10 +0100 | [diff] [blame] | 5357 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5358 | *qname += pref_len + 1; |
| 5359 | *qname_len -= pref_len + 1; |
| 5360 | } else if (((*qname)[0] == '*') && (*qname_len == 1)) { |
| 5361 | /* all modules - special case */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5362 | mod = NULL; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5363 | } else { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5364 | switch (set->format) { |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 5365 | case LY_VALUE_SCHEMA: |
| 5366 | case LY_VALUE_SCHEMA_RESOLVED: |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5367 | /* current module */ |
| 5368 | mod = set->cur_mod; |
| 5369 | break; |
Radek Krejci | 224d4b4 | 2021-04-23 13:54:59 +0200 | [diff] [blame] | 5370 | case LY_VALUE_CANON: |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 5371 | case LY_VALUE_JSON: |
Radek Krejci | f994364 | 2021-04-26 10:18:21 +0200 | [diff] [blame] | 5372 | case LY_VALUE_LYB: |
Michal Vasko | aac267d | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 5373 | case LY_VALUE_STR_NS: |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5374 | /* inherit parent (context node) module */ |
| 5375 | if (ctx_scnode) { |
| 5376 | mod = ctx_scnode->module; |
| 5377 | } else { |
| 5378 | mod = NULL; |
| 5379 | } |
| 5380 | break; |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 5381 | case LY_VALUE_XML: |
Michal Vasko | 52143d1 | 2021-04-14 15:36:39 +0200 | [diff] [blame] | 5382 | /* all nodes need to be prefixed */ |
| 5383 | LOGVAL(set->ctx, LYVE_DATA, "Non-prefixed node \"%.*s\" in XML xpath found.", *qname_len, *qname); |
| 5384 | return LY_EVALID; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5385 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5386 | } |
| 5387 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5388 | *moveto_mod = mod; |
| 5389 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5390 | } |
| 5391 | |
| 5392 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5393 | * @brief Move context @p set to the root. Handles absolute path. |
| 5394 | * Result is LYXP_SET_NODE_SET. |
| 5395 | * |
| 5396 | * @param[in,out] set Set to use. |
| 5397 | * @param[in] options Xpath options. |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 5398 | * @return LY_ERR value. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5399 | */ |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 5400 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5401 | moveto_root(struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5402 | { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 5403 | assert(!(options & LYXP_SKIP_EXPR)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5404 | |
| 5405 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5406 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 5407 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, NULL, set->root_type, NULL)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5408 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5409 | set->type = LYXP_SET_NODE_SET; |
| 5410 | set->used = 0; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5411 | set_insert_node(set, NULL, 0, set->root_type, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5412 | } |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 5413 | |
| 5414 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5415 | } |
| 5416 | |
| 5417 | /** |
| 5418 | * @brief Check @p node as a part of NameTest processing. |
| 5419 | * |
| 5420 | * @param[in] node Node to check. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5421 | * @param[in] set Set to read general context from. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5422 | * @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] | 5423 | * @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] | 5424 | * @param[in] options XPath options. |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5425 | * @return LY_ERR (LY_ENOT if node does not match, LY_EINCOMPLETE on unresolved when, |
| 5426 | * LY_EINVAL if netither node nor any children match) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5427 | */ |
| 5428 | static LY_ERR |
Michal Vasko | db08ce5 | 2021-10-06 08:57:49 +0200 | [diff] [blame] | 5429 | moveto_node_check(const struct lyd_node *node, const struct lyxp_set *set, const char *node_name, |
| 5430 | const struct lys_module *moveto_mod, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5431 | { |
Michal Vasko | dca9f12 | 2021-07-16 13:56:22 +0200 | [diff] [blame] | 5432 | if (!node->schema) { |
| 5433 | /* opaque node never matches */ |
| 5434 | return LY_ENOT; |
| 5435 | } |
| 5436 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5437 | /* module check */ |
| 5438 | if (moveto_mod && (node->schema->module != moveto_mod)) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5439 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5440 | } |
| 5441 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5442 | /* context check */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5443 | if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5444 | return LY_EINVAL; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5445 | } else if (set->context_op && (node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && |
| 5446 | (node->schema != set->context_op)) { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5447 | return LY_EINVAL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5448 | } |
| 5449 | |
| 5450 | /* name check */ |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5451 | if (node_name && strcmp(node_name, "*") && (node->schema->name != node_name)) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5452 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5453 | } |
| 5454 | |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 5455 | /* when check */ |
Michal Vasko | d5cfa6e | 2020-11-23 16:56:08 +0100 | [diff] [blame] | 5456 | if (!(options & LYXP_IGNORE_WHEN) && lysc_has_when(node->schema) && !(node->flags & LYD_WHEN_TRUE)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5457 | return LY_EINCOMPLETE; |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 5458 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5459 | |
| 5460 | /* match */ |
| 5461 | return LY_SUCCESS; |
| 5462 | } |
| 5463 | |
| 5464 | /** |
| 5465 | * @brief Check @p node as a part of schema NameTest processing. |
| 5466 | * |
| 5467 | * @param[in] node Schema node to check. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5468 | * @param[in] ctx_scnode Context node. |
| 5469 | * @param[in] set Set to read general context from. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5470 | * @param[in] node_name Node name in the dictionary to move to, NULL for any nodes. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5471 | * @param[in] moveto_mod Expected module of the node, NULL for no prefix. |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5472 | * @return LY_ERR (LY_ENOT if node does not match, LY_EINVAL if neither node nor any children match) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5473 | */ |
| 5474 | static LY_ERR |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5475 | moveto_scnode_check(const struct lysc_node *node, const struct lysc_node *ctx_scnode, const struct lyxp_set *set, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 5476 | const char *node_name, const struct lys_module *moveto_mod) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5477 | { |
Michal Vasko | c8c94eb | 2022-02-02 11:16:56 +0100 | [diff] [blame] | 5478 | if (!moveto_mod && node_name && strcmp(node_name, "*")) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5479 | switch (set->format) { |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 5480 | case LY_VALUE_SCHEMA: |
| 5481 | case LY_VALUE_SCHEMA_RESOLVED: |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5482 | /* use current module */ |
| 5483 | moveto_mod = set->cur_mod; |
| 5484 | break; |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 5485 | case LY_VALUE_JSON: |
Radek Krejci | f994364 | 2021-04-26 10:18:21 +0200 | [diff] [blame] | 5486 | case LY_VALUE_LYB: |
Michal Vasko | aac267d | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 5487 | case LY_VALUE_STR_NS: |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5488 | /* inherit module of the context node, if any */ |
| 5489 | if (ctx_scnode) { |
| 5490 | moveto_mod = ctx_scnode->module; |
| 5491 | } |
| 5492 | break; |
Radek Krejci | 224d4b4 | 2021-04-23 13:54:59 +0200 | [diff] [blame] | 5493 | case LY_VALUE_CANON: |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 5494 | case LY_VALUE_XML: |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5495 | /* not defined */ |
| 5496 | LOGINT(set->ctx); |
| 5497 | return LY_EINVAL; |
| 5498 | } |
| 5499 | } |
| 5500 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5501 | /* module check */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5502 | if (moveto_mod && (node->module != moveto_mod)) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5503 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5504 | } |
| 5505 | |
| 5506 | /* context check */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5507 | if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5508 | return LY_EINVAL; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5509 | } else if (set->context_op && (node->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node != set->context_op)) { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5510 | return LY_EINVAL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5511 | } |
| 5512 | |
| 5513 | /* name check */ |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5514 | if (node_name && strcmp(node_name, "*") && (node->name != node_name)) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5515 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5516 | } |
| 5517 | |
| 5518 | /* match */ |
| 5519 | return LY_SUCCESS; |
| 5520 | } |
| 5521 | |
| 5522 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5523 | * @brief Move context @p set to a node. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY). Context position aware. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5524 | * |
| 5525 | * @param[in,out] set Set to use. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5526 | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5527 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 5528 | * @param[in] options XPath options. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5529 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 5530 | */ |
| 5531 | static LY_ERR |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 5532 | moveto_node(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] | 5533 | { |
Michal Vasko | 9d41411 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 5534 | LY_ERR r, rc = LY_SUCCESS; |
Michal Vasko | db08ce5 | 2021-10-06 08:57:49 +0200 | [diff] [blame] | 5535 | const struct lyd_node *siblings, *sub; |
Michal Vasko | 9d41411 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 5536 | struct lyxp_set result; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5537 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 5538 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5539 | return LY_SUCCESS; |
| 5540 | } |
| 5541 | |
| 5542 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 5543 | 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] | 5544 | return LY_EVALID; |
| 5545 | } |
| 5546 | |
Michal Vasko | 9d41411 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 5547 | /* init result set */ |
| 5548 | set_init(&result, set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5549 | |
Michal Vasko | 9d41411 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 5550 | for (uint32_t i = 0; i < set->used; ++i) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5551 | if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5552 | assert(!set->val.nodes[i].node); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5553 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5554 | /* search in all the trees */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5555 | siblings = set->tree; |
Michal Vasko | 5bfd4be | 2020-06-23 13:26:19 +0200 | [diff] [blame] | 5556 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5557 | /* search in children */ |
Michal Vasko | db08ce5 | 2021-10-06 08:57:49 +0200 | [diff] [blame] | 5558 | siblings = lyd_child(set->val.nodes[i].node); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5559 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5560 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5561 | for (sub = siblings; sub; sub = sub->next) { |
Michal Vasko | 9d41411 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 5562 | r = moveto_node_check(sub, set, ncname, moveto_mod, options); |
| 5563 | if (r == LY_SUCCESS) { |
| 5564 | /* matching node */ |
| 5565 | set_insert_node(&result, sub, 0, LYXP_NODE_ELEM, result.used); |
| 5566 | } else if (r == LY_EINCOMPLETE) { |
| 5567 | rc = r; |
| 5568 | goto cleanup; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5569 | } |
| 5570 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5571 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5572 | |
Michal Vasko | 9d41411 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 5573 | /* move result to the set */ |
| 5574 | lyxp_set_free_content(set); |
| 5575 | *set = result; |
| 5576 | result.type = LYXP_SET_NUMBER; |
| 5577 | assert(!set_sort(set)); |
| 5578 | |
| 5579 | cleanup: |
| 5580 | lyxp_set_free_content(&result); |
| 5581 | return rc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5582 | } |
| 5583 | |
| 5584 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5585 | * @brief Move context @p set to a node using hashes. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY). |
| 5586 | * Context position aware. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5587 | * |
| 5588 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5589 | * @param[in] scnode Matching node schema. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5590 | * @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] | 5591 | * @param[in] options XPath options. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5592 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 5593 | */ |
| 5594 | static LY_ERR |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 5595 | moveto_node_hash(struct lyxp_set *set, const struct lysc_node *scnode, const struct ly_path_predicate *predicates, |
| 5596 | uint32_t options) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5597 | { |
Michal Vasko | aac267d | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 5598 | LY_ERR ret = LY_SUCCESS, r; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5599 | uint32_t i; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5600 | const struct lyd_node *siblings; |
Michal Vasko | 9d41411 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 5601 | struct lyxp_set result; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5602 | struct lyd_node *sub, *inst = NULL; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5603 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5604 | assert(scnode && (!(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) || predicates)); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5605 | |
Michal Vasko | a5af776 | 2021-12-02 13:11:56 +0100 | [diff] [blame] | 5606 | /* init result set */ |
| 5607 | set_init(&result, set); |
| 5608 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 5609 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5610 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5611 | } |
| 5612 | |
| 5613 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 5614 | 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] | 5615 | ret = LY_EVALID; |
| 5616 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5617 | } |
| 5618 | |
| 5619 | /* context check for all the nodes since we have the schema node */ |
| 5620 | if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) { |
| 5621 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5622 | goto cleanup; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 5623 | } else if (set->context_op && (scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && |
| 5624 | (scnode != set->context_op)) { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5625 | lyxp_set_free_content(set); |
| 5626 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5627 | } |
| 5628 | |
| 5629 | /* create specific data instance if needed */ |
| 5630 | if (scnode->nodetype == LYS_LIST) { |
| 5631 | LY_CHECK_GOTO(ret = lyd_create_list(scnode, predicates, &inst), cleanup); |
| 5632 | } else if (scnode->nodetype == LYS_LEAFLIST) { |
| 5633 | 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] | 5634 | } |
| 5635 | |
Michal Vasko | 9d41411 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 5636 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5637 | siblings = NULL; |
| 5638 | |
| 5639 | if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) { |
| 5640 | assert(!set->val.nodes[i].node); |
| 5641 | |
| 5642 | /* search in all the trees */ |
| 5643 | siblings = set->tree; |
| 5644 | } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
| 5645 | /* search in children */ |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 5646 | siblings = lyd_child(set->val.nodes[i].node); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5647 | } |
| 5648 | |
| 5649 | /* find the node using hashes */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5650 | if (inst) { |
Michal Vasko | aac267d | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 5651 | r = lyd_find_sibling_first(siblings, inst, &sub); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5652 | } else { |
Michal Vasko | aac267d | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 5653 | r = lyd_find_sibling_val(siblings, scnode, NULL, 0, &sub); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5654 | } |
Michal Vasko | aac267d | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 5655 | LY_CHECK_ERR_GOTO(r && (r != LY_ENOTFOUND), ret = r, cleanup); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5656 | |
| 5657 | /* when check */ |
Michal Vasko | d5cfa6e | 2020-11-23 16:56:08 +0100 | [diff] [blame] | 5658 | 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] | 5659 | ret = LY_EINCOMPLETE; |
| 5660 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5661 | } |
| 5662 | |
| 5663 | if (sub) { |
| 5664 | /* pos filled later */ |
Michal Vasko | 9d41411 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 5665 | set_insert_node(&result, sub, 0, LYXP_NODE_ELEM, result.used); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5666 | } |
| 5667 | } |
| 5668 | |
Michal Vasko | 9d41411 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 5669 | /* move result to the set */ |
| 5670 | lyxp_set_free_content(set); |
| 5671 | *set = result; |
| 5672 | result.type = LYXP_SET_NUMBER; |
| 5673 | assert(!set_sort(set)); |
| 5674 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5675 | cleanup: |
Michal Vasko | 9d41411 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 5676 | lyxp_set_free_content(&result); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5677 | lyd_free_tree(inst); |
| 5678 | return ret; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5679 | } |
| 5680 | |
| 5681 | /** |
| 5682 | * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY). |
| 5683 | * |
| 5684 | * @param[in,out] set Set to use. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5685 | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5686 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5687 | * @param[in] options XPath options. |
| 5688 | * @return LY_ERR |
| 5689 | */ |
| 5690 | static LY_ERR |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5691 | moveto_scnode(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] | 5692 | { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 5693 | ly_bool temp_ctx = 0; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5694 | uint32_t getnext_opts; |
| 5695 | uint32_t orig_used, i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5696 | uint32_t mod_idx; |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 5697 | const struct lysc_node *iter, *start_parent; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5698 | const struct lys_module *mod; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5699 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 5700 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5701 | return LY_SUCCESS; |
| 5702 | } |
| 5703 | |
| 5704 | if (set->type != LYXP_SET_SCNODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 5705 | 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] | 5706 | return LY_EVALID; |
| 5707 | } |
| 5708 | |
Michal Vasko | cafad9d | 2019-11-07 15:20:03 +0100 | [diff] [blame] | 5709 | /* getnext opts */ |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 5710 | getnext_opts = 0; |
Michal Vasko | cafad9d | 2019-11-07 15:20:03 +0100 | [diff] [blame] | 5711 | if (options & LYXP_SCNODE_OUTPUT) { |
| 5712 | getnext_opts |= LYS_GETNEXT_OUTPUT; |
| 5713 | } |
| 5714 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5715 | orig_used = set->used; |
| 5716 | for (i = 0; i < orig_used; ++i) { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 5717 | uint32_t idx; |
| 5718 | |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 5719 | if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) { |
| 5720 | if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5721 | continue; |
| 5722 | } |
| 5723 | |
| 5724 | /* remember context node */ |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 5725 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED; |
Michal Vasko | ec4df48 | 2019-12-16 10:02:18 +0100 | [diff] [blame] | 5726 | } else { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5727 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5728 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5729 | |
| 5730 | start_parent = set->val.scnodes[i].scnode; |
| 5731 | |
| 5732 | if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5733 | /* it can actually be in any module, it's all <running>, and even if it's moveto_mod (if set), |
Michal Vasko | 9e9f26d | 2020-10-12 16:31:33 +0200 | [diff] [blame] | 5734 | * it can be in a top-level augment (the root node itself is useless in this case) */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5735 | mod_idx = 0; |
Michal Vasko | a51ef07 | 2021-07-02 10:40:30 +0200 | [diff] [blame] | 5736 | while ((mod = ly_ctx_get_module_iter(set->ctx, &mod_idx))) { |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 5737 | iter = NULL; |
Michal Vasko | 919954a | 2021-07-26 09:21:42 +0200 | [diff] [blame] | 5738 | /* module may not be implemented or not compiled yet */ |
| 5739 | while (mod->compiled && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5740 | if (!moveto_scnode_check(iter, NULL, set, ncname, moveto_mod)) { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 5741 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx)); |
| 5742 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5743 | /* we need to prevent these nodes from being considered in this moveto */ |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 5744 | if ((idx < orig_used) && (idx > i)) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 5745 | set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5746 | temp_ctx = 1; |
| 5747 | } |
| 5748 | } |
| 5749 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5750 | } |
| 5751 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 5752 | } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) { |
| 5753 | iter = NULL; |
| 5754 | while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5755 | if (!moveto_scnode_check(iter, start_parent, set, ncname, moveto_mod)) { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 5756 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx)); |
| 5757 | |
| 5758 | if ((idx < orig_used) && (idx > i)) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 5759 | set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5760 | temp_ctx = 1; |
| 5761 | } |
| 5762 | } |
| 5763 | } |
| 5764 | } |
| 5765 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5766 | |
| 5767 | /* correct temporary in_ctx values */ |
| 5768 | if (temp_ctx) { |
| 5769 | for (i = 0; i < orig_used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 5770 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NEW_CTX) { |
| 5771 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5772 | } |
| 5773 | } |
| 5774 | } |
| 5775 | |
| 5776 | return LY_SUCCESS; |
| 5777 | } |
| 5778 | |
| 5779 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5780 | * @brief Move context @p set to a node and all its descendants. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY). |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5781 | * Context position aware. |
| 5782 | * |
| 5783 | * @param[in] set Set to use. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5784 | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5785 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 5786 | * @param[in] options XPath options. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5787 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 5788 | */ |
| 5789 | static LY_ERR |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 5790 | moveto_node_alldesc(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] | 5791 | { |
| 5792 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5793 | const struct lyd_node *next, *elem, *start; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5794 | struct lyxp_set ret_set; |
| 5795 | LY_ERR rc; |
| 5796 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 5797 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5798 | return LY_SUCCESS; |
| 5799 | } |
| 5800 | |
| 5801 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 5802 | 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] | 5803 | return LY_EVALID; |
| 5804 | } |
| 5805 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5806 | /* replace the original nodes (and throws away all text and meta nodes, root is replaced by a child) */ |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 5807 | rc = moveto_node(set, NULL, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5808 | LY_CHECK_RET(rc); |
| 5809 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5810 | /* 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] | 5811 | set_init(&ret_set, set); |
| 5812 | for (i = 0; i < set->used; ++i) { |
| 5813 | |
| 5814 | /* TREE DFS */ |
| 5815 | start = set->val.nodes[i].node; |
| 5816 | for (elem = next = start; elem; elem = next) { |
Michal Vasko | db08ce5 | 2021-10-06 08:57:49 +0200 | [diff] [blame] | 5817 | rc = moveto_node_check(elem, set, ncname, moveto_mod, options); |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5818 | if (!rc) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5819 | /* add matching node into result set */ |
| 5820 | set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used); |
| 5821 | if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) { |
| 5822 | /* the node is a duplicate, we'll process it later in the set */ |
| 5823 | goto skip_children; |
| 5824 | } |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5825 | } else if (rc == LY_EINCOMPLETE) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5826 | return rc; |
| 5827 | } else if (rc == LY_EINVAL) { |
| 5828 | goto skip_children; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5829 | } |
| 5830 | |
| 5831 | /* TREE DFS NEXT ELEM */ |
| 5832 | /* select element for the next run - children first */ |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 5833 | next = lyd_child(elem); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5834 | if (!next) { |
| 5835 | skip_children: |
| 5836 | /* no children, so try siblings, but only if it's not the start, |
| 5837 | * that is considered to be the root and it's siblings are not traversed */ |
| 5838 | if (elem != start) { |
| 5839 | next = elem->next; |
| 5840 | } else { |
| 5841 | break; |
| 5842 | } |
| 5843 | } |
| 5844 | while (!next) { |
| 5845 | /* no siblings, go back through the parents */ |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 5846 | if (lyd_parent(elem) == start) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5847 | /* we are done, no next element to process */ |
| 5848 | break; |
| 5849 | } |
| 5850 | /* parent is already processed, go to its sibling */ |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 5851 | elem = lyd_parent(elem); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5852 | next = elem->next; |
| 5853 | } |
| 5854 | } |
| 5855 | } |
| 5856 | |
| 5857 | /* make the temporary set the current one */ |
| 5858 | ret_set.ctx_pos = set->ctx_pos; |
| 5859 | ret_set.ctx_size = set->ctx_size; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5860 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5861 | memcpy(set, &ret_set, sizeof *set); |
| 5862 | |
| 5863 | return LY_SUCCESS; |
| 5864 | } |
| 5865 | |
| 5866 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5867 | * @brief Move context @p set to a schema node and all its descendants. Result is LYXP_SET_NODE_SET. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5868 | * |
| 5869 | * @param[in] set Set to use. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5870 | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5871 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5872 | * @param[in] options XPath options. |
| 5873 | * @return LY_ERR |
| 5874 | */ |
| 5875 | static LY_ERR |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5876 | moveto_scnode_alldesc(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] | 5877 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5878 | uint32_t i, orig_used; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5879 | const struct lysc_node *next, *elem, *start; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5880 | LY_ERR rc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5881 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 5882 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5883 | return LY_SUCCESS; |
| 5884 | } |
| 5885 | |
| 5886 | if (set->type != LYXP_SET_SCNODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 5887 | 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] | 5888 | return LY_EVALID; |
| 5889 | } |
| 5890 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5891 | orig_used = set->used; |
| 5892 | for (i = 0; i < orig_used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 5893 | if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) { |
| 5894 | if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5895 | continue; |
| 5896 | } |
| 5897 | |
| 5898 | /* remember context node */ |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 5899 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED; |
Michal Vasko | ec4df48 | 2019-12-16 10:02:18 +0100 | [diff] [blame] | 5900 | } else { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5901 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5902 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5903 | |
| 5904 | /* TREE DFS */ |
| 5905 | start = set->val.scnodes[i].scnode; |
| 5906 | for (elem = next = start; elem; elem = next) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5907 | if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) { |
| 5908 | /* schema-only nodes, skip root */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5909 | goto next_iter; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5910 | } |
| 5911 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5912 | rc = moveto_scnode_check(elem, start, set, ncname, moveto_mod); |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5913 | if (!rc) { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 5914 | uint32_t idx; |
| 5915 | |
| 5916 | if (lyxp_set_scnode_contains(set, elem, LYXP_NODE_ELEM, i, &idx)) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 5917 | set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5918 | if ((uint32_t)idx > i) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5919 | /* we will process it later in the set */ |
| 5920 | goto skip_children; |
| 5921 | } |
| 5922 | } else { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 5923 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, elem, LYXP_NODE_ELEM, NULL)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5924 | } |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5925 | } else if (rc == LY_EINVAL) { |
| 5926 | goto skip_children; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5927 | } |
| 5928 | |
| 5929 | next_iter: |
| 5930 | /* TREE DFS NEXT ELEM */ |
| 5931 | /* select element for the next run - children first */ |
Michal Vasko | 544e58a | 2021-01-28 14:33:41 +0100 | [diff] [blame] | 5932 | next = lysc_node_child(elem); |
| 5933 | if (next && (next->nodetype == LYS_INPUT) && (options & LYXP_SCNODE_OUTPUT)) { |
| 5934 | next = next->next; |
| 5935 | } else if (next && (next->nodetype == LYS_OUTPUT) && !(options & LYXP_SCNODE_OUTPUT)) { |
| 5936 | next = next->next; |
| 5937 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5938 | if (!next) { |
| 5939 | skip_children: |
| 5940 | /* no children, so try siblings, but only if it's not the start, |
| 5941 | * that is considered to be the root and it's siblings are not traversed */ |
| 5942 | if (elem != start) { |
| 5943 | next = elem->next; |
| 5944 | } else { |
| 5945 | break; |
| 5946 | } |
| 5947 | } |
| 5948 | while (!next) { |
| 5949 | /* no siblings, go back through the parents */ |
| 5950 | if (elem->parent == start) { |
| 5951 | /* we are done, no next element to process */ |
| 5952 | break; |
| 5953 | } |
| 5954 | /* parent is already processed, go to its sibling */ |
| 5955 | elem = elem->parent; |
| 5956 | next = elem->next; |
| 5957 | } |
| 5958 | } |
| 5959 | } |
| 5960 | |
| 5961 | return LY_SUCCESS; |
| 5962 | } |
| 5963 | |
| 5964 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5965 | * @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] | 5966 | * Indirectly context position aware. |
| 5967 | * |
| 5968 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5969 | * @param[in] mod Matching metadata module, NULL for any. |
| 5970 | * @param[in] ncname Matching metadata name in the dictionary, NULL for any. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 5971 | * @param[in] options XPath options. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5972 | * @return LY_ERR |
| 5973 | */ |
| 5974 | static LY_ERR |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 5975 | 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] | 5976 | { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5977 | struct lyd_meta *sub; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5978 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 5979 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5980 | return LY_SUCCESS; |
| 5981 | } |
| 5982 | |
| 5983 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 5984 | 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] | 5985 | return LY_EVALID; |
| 5986 | } |
| 5987 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5988 | for (uint32_t i = 0; i < set->used; ) { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 5989 | ly_bool replaced = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5990 | |
| 5991 | /* only attributes of an elem (not dummy) can be in the result, skip all the rest; |
| 5992 | * our attributes are always qualified */ |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5993 | if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5994 | for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5995 | |
| 5996 | /* check "namespace" */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5997 | if (mod && (sub->annotation->module != mod)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5998 | continue; |
| 5999 | } |
| 6000 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6001 | if (!ncname || (sub->name == ncname)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6002 | /* match */ |
| 6003 | if (!replaced) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6004 | set->val.meta[i].meta = sub; |
| 6005 | set->val.meta[i].type = LYXP_NODE_META; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6006 | /* pos does not change */ |
| 6007 | replaced = 1; |
| 6008 | } else { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6009 | 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] | 6010 | } |
| 6011 | ++i; |
| 6012 | } |
| 6013 | } |
| 6014 | } |
| 6015 | |
| 6016 | if (!replaced) { |
| 6017 | /* no match */ |
| 6018 | set_remove_node(set, i); |
| 6019 | } |
| 6020 | } |
| 6021 | |
| 6022 | return LY_SUCCESS; |
| 6023 | } |
| 6024 | |
| 6025 | /** |
| 6026 | * @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] | 6027 | * Result is LYXP_SET_NODE_SET. Context position aware. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6028 | * |
| 6029 | * @param[in,out] set1 Set to use for the result. |
| 6030 | * @param[in] set2 Set that is copied to @p set1. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6031 | * @return LY_ERR |
| 6032 | */ |
| 6033 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6034 | moveto_union(struct lyxp_set *set1, struct lyxp_set *set2) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6035 | { |
| 6036 | LY_ERR rc; |
| 6037 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6038 | 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] | 6039 | 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] | 6040 | return LY_EVALID; |
| 6041 | } |
| 6042 | |
| 6043 | /* set2 is empty or both set1 and set2 */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6044 | if (!set2->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6045 | return LY_SUCCESS; |
| 6046 | } |
| 6047 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6048 | if (!set1->used) { |
aPiecek | adc1e4f | 2021-10-07 11:15:12 +0200 | [diff] [blame] | 6049 | /* release hidden allocated data (lyxp_set.size) */ |
| 6050 | lyxp_set_free_content(set1); |
| 6051 | /* direct copying of the entire structure */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6052 | memcpy(set1, set2, sizeof *set1); |
| 6053 | /* dynamic memory belongs to set1 now, do not free */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6054 | memset(set2, 0, sizeof *set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6055 | return LY_SUCCESS; |
| 6056 | } |
| 6057 | |
| 6058 | /* we assume sets are sorted */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6059 | assert(!set_sort(set1) && !set_sort(set2)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6060 | |
| 6061 | /* sort, remove duplicates */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6062 | rc = set_sorted_merge(set1, set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6063 | LY_CHECK_RET(rc); |
| 6064 | |
| 6065 | /* final set must be sorted */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6066 | assert(!set_sort(set1)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6067 | |
| 6068 | return LY_SUCCESS; |
| 6069 | } |
| 6070 | |
| 6071 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6072 | * @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] | 6073 | * Context position aware. |
| 6074 | * |
| 6075 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6076 | * @param[in] mod Matching metadata module, NULL for any. |
| 6077 | * @param[in] ncname Matching metadata name in the dictionary, NULL for any. |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 6078 | * @param[in] options XPath options. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6079 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6080 | */ |
| 6081 | static int |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 6082 | 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] | 6083 | { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6084 | struct lyd_meta *sub; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6085 | struct lyxp_set *set_all_desc = NULL; |
| 6086 | LY_ERR rc; |
| 6087 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6088 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6089 | return LY_SUCCESS; |
| 6090 | } |
| 6091 | |
| 6092 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 6093 | 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] | 6094 | return LY_EVALID; |
| 6095 | } |
| 6096 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6097 | /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory, |
| 6098 | * but it likely won't be used much, so it's a waste of time */ |
| 6099 | /* copy the context */ |
| 6100 | set_all_desc = set_copy(set); |
| 6101 | /* get all descendant nodes (the original context nodes are removed) */ |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 6102 | rc = moveto_node_alldesc(set_all_desc, NULL, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6103 | if (rc != LY_SUCCESS) { |
| 6104 | lyxp_set_free(set_all_desc); |
| 6105 | return rc; |
| 6106 | } |
| 6107 | /* prepend the original context nodes */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6108 | rc = moveto_union(set, set_all_desc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6109 | if (rc != LY_SUCCESS) { |
| 6110 | lyxp_set_free(set_all_desc); |
| 6111 | return rc; |
| 6112 | } |
| 6113 | lyxp_set_free(set_all_desc); |
| 6114 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6115 | for (uint32_t i = 0; i < set->used; ) { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6116 | ly_bool replaced = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6117 | |
| 6118 | /* only attributes of an elem can be in the result, skip all the rest, |
| 6119 | * we have all attributes qualified in lyd tree */ |
| 6120 | if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6121 | for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6122 | /* check "namespace" */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6123 | if (mod && (sub->annotation->module != mod)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6124 | continue; |
| 6125 | } |
| 6126 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6127 | if (!ncname || (sub->name == ncname)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6128 | /* match */ |
| 6129 | if (!replaced) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6130 | set->val.meta[i].meta = sub; |
| 6131 | set->val.meta[i].type = LYXP_NODE_META; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6132 | /* pos does not change */ |
| 6133 | replaced = 1; |
| 6134 | } else { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6135 | 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] | 6136 | } |
| 6137 | ++i; |
| 6138 | } |
| 6139 | } |
| 6140 | } |
| 6141 | |
| 6142 | if (!replaced) { |
| 6143 | /* no match */ |
| 6144 | set_remove_node(set, i); |
| 6145 | } |
| 6146 | } |
| 6147 | |
| 6148 | return LY_SUCCESS; |
| 6149 | } |
| 6150 | |
| 6151 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6152 | * @brief Move context @p set to self and al chilren, recursively. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET. |
| 6153 | * Context position aware. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6154 | * |
| 6155 | * @param[in] parent Current parent. |
| 6156 | * @param[in] parent_pos Position of @p parent. |
| 6157 | * @param[in] parent_type Node type of @p parent. |
| 6158 | * @param[in,out] to_set Set to use. |
| 6159 | * @param[in] dup_check_set Set for checking duplicities. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6160 | * @param[in] options XPath options. |
| 6161 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6162 | */ |
| 6163 | static LY_ERR |
| 6164 | moveto_self_add_children_r(const struct lyd_node *parent, uint32_t parent_pos, enum lyxp_node_type parent_type, |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6165 | struct lyxp_set *to_set, const struct lyxp_set *dup_check_set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6166 | { |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6167 | const struct lyd_node *iter, *first; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6168 | LY_ERR rc; |
| 6169 | |
| 6170 | switch (parent_type) { |
| 6171 | case LYXP_NODE_ROOT: |
| 6172 | case LYXP_NODE_ROOT_CONFIG: |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6173 | assert(!parent); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6174 | |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6175 | /* add all top-level nodes as elements */ |
| 6176 | first = to_set->tree; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6177 | break; |
| 6178 | case LYXP_NODE_ELEM: |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6179 | /* add just the text node of this term element node */ |
| 6180 | if (parent->schema->nodetype & (LYD_NODE_TERM | LYD_NODE_ANY)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6181 | if (!set_dup_node_check(dup_check_set, parent, LYXP_NODE_TEXT, -1)) { |
| 6182 | set_insert_node(to_set, parent, parent_pos, LYXP_NODE_TEXT, to_set->used); |
| 6183 | } |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6184 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6185 | } |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6186 | |
| 6187 | /* add all the children of this node */ |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 6188 | first = lyd_child(parent); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6189 | break; |
| 6190 | default: |
| 6191 | LOGINT_RET(parent->schema->module->ctx); |
| 6192 | } |
| 6193 | |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6194 | /* add all top-level nodes as elements */ |
| 6195 | LY_LIST_FOR(first, iter) { |
| 6196 | /* context check */ |
| 6197 | if ((parent_type == LYXP_NODE_ROOT_CONFIG) && (iter->schema->flags & LYS_CONFIG_R)) { |
| 6198 | continue; |
| 6199 | } |
| 6200 | |
| 6201 | /* when check */ |
Michal Vasko | d5cfa6e | 2020-11-23 16:56:08 +0100 | [diff] [blame] | 6202 | if (!(options & LYXP_IGNORE_WHEN) && lysc_has_when(iter->schema) && !(iter->flags & LYD_WHEN_TRUE)) { |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6203 | return LY_EINCOMPLETE; |
| 6204 | } |
| 6205 | |
| 6206 | if (!set_dup_node_check(dup_check_set, iter, LYXP_NODE_ELEM, -1)) { |
| 6207 | set_insert_node(to_set, iter, 0, LYXP_NODE_ELEM, to_set->used); |
| 6208 | |
| 6209 | /* also add all the children of this node, recursively */ |
| 6210 | rc = moveto_self_add_children_r(iter, 0, LYXP_NODE_ELEM, to_set, dup_check_set, options); |
| 6211 | LY_CHECK_RET(rc); |
| 6212 | } |
| 6213 | } |
| 6214 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6215 | return LY_SUCCESS; |
| 6216 | } |
| 6217 | |
| 6218 | /** |
| 6219 | * @brief Move context @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET |
| 6220 | * (or LYXP_SET_EMPTY). Context position aware. |
| 6221 | * |
| 6222 | * @param[in,out] set Set to use. |
| 6223 | * @param[in] all_desc Whether to go to all descendants ('//') or not ('/'). |
| 6224 | * @param[in] options XPath options. |
| 6225 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6226 | */ |
| 6227 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6228 | moveto_self(struct lyxp_set *set, ly_bool all_desc, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6229 | { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6230 | struct lyxp_set ret_set; |
| 6231 | LY_ERR rc; |
| 6232 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6233 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6234 | return LY_SUCCESS; |
| 6235 | } |
| 6236 | |
| 6237 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 6238 | 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] | 6239 | return LY_EVALID; |
| 6240 | } |
| 6241 | |
| 6242 | /* nothing to do */ |
| 6243 | if (!all_desc) { |
| 6244 | return LY_SUCCESS; |
| 6245 | } |
| 6246 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6247 | /* add all the children, they get added recursively */ |
| 6248 | set_init(&ret_set, set); |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6249 | for (uint32_t i = 0; i < set->used; ++i) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6250 | /* copy the current node to tmp */ |
| 6251 | set_insert_node(&ret_set, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, ret_set.used); |
| 6252 | |
| 6253 | /* do not touch attributes and text nodes */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6254 | if ((set->val.nodes[i].type == LYXP_NODE_TEXT) || (set->val.nodes[i].type == LYXP_NODE_META)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6255 | continue; |
| 6256 | } |
| 6257 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6258 | /* add all the children */ |
| 6259 | rc = moveto_self_add_children_r(set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, &ret_set, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 6260 | set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6261 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6262 | lyxp_set_free_content(&ret_set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6263 | return rc; |
| 6264 | } |
| 6265 | } |
| 6266 | |
| 6267 | /* use the temporary set as the current one */ |
| 6268 | ret_set.ctx_pos = set->ctx_pos; |
| 6269 | ret_set.ctx_size = set->ctx_size; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6270 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6271 | memcpy(set, &ret_set, sizeof *set); |
| 6272 | |
| 6273 | return LY_SUCCESS; |
| 6274 | } |
| 6275 | |
| 6276 | /** |
| 6277 | * @brief Move context schema @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_SCNODE_SET |
| 6278 | * (or LYXP_SET_EMPTY). |
| 6279 | * |
| 6280 | * @param[in,out] set Set to use. |
| 6281 | * @param[in] all_desc Whether to go to all descendants ('//') or not ('/'). |
| 6282 | * @param[in] options XPath options. |
| 6283 | * @return LY_ERR |
| 6284 | */ |
| 6285 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6286 | moveto_scnode_self(struct lyxp_set *set, ly_bool all_desc, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6287 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6288 | uint32_t getnext_opts; |
| 6289 | uint32_t mod_idx; |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6290 | const struct lysc_node *iter, *start_parent; |
| 6291 | const struct lys_module *mod; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6292 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6293 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6294 | return LY_SUCCESS; |
| 6295 | } |
| 6296 | |
| 6297 | if (set->type != LYXP_SET_SCNODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 6298 | 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] | 6299 | return LY_EVALID; |
| 6300 | } |
| 6301 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6302 | /* getnext opts */ |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 6303 | getnext_opts = 0; |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6304 | if (options & LYXP_SCNODE_OUTPUT) { |
| 6305 | getnext_opts |= LYS_GETNEXT_OUTPUT; |
| 6306 | } |
| 6307 | |
| 6308 | /* add all the children, recursively as they are being added into the same set */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6309 | for (uint32_t i = 0; i < set->used; ++i) { |
Michal Vasko | e657f96 | 2020-12-10 12:19:48 +0100 | [diff] [blame] | 6310 | if (!all_desc) { |
| 6311 | /* traverse the start node */ |
| 6312 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START) { |
| 6313 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
| 6314 | } |
| 6315 | continue; |
| 6316 | } |
| 6317 | |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 6318 | if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) { |
| 6319 | if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6320 | continue; |
| 6321 | } |
| 6322 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6323 | /* remember context node */ |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 6324 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED; |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6325 | } else { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 6326 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6327 | } |
| 6328 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6329 | start_parent = set->val.scnodes[i].scnode; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6330 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6331 | if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) { |
| 6332 | /* it can actually be in any module, it's all <running> */ |
| 6333 | mod_idx = 0; |
Michal Vasko | a51ef07 | 2021-07-02 10:40:30 +0200 | [diff] [blame] | 6334 | while ((mod = ly_ctx_get_module_iter(set->ctx, &mod_idx))) { |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6335 | iter = NULL; |
| 6336 | /* module may not be implemented */ |
| 6337 | while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) { |
| 6338 | /* context check */ |
| 6339 | if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) { |
| 6340 | continue; |
| 6341 | } |
| 6342 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 6343 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, NULL)); |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6344 | /* throw away the insert index, we want to consider that node again, recursively */ |
| 6345 | } |
| 6346 | } |
| 6347 | |
| 6348 | } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) { |
| 6349 | iter = NULL; |
| 6350 | while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6351 | /* context check */ |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6352 | if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6353 | continue; |
| 6354 | } |
| 6355 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 6356 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, NULL)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6357 | } |
| 6358 | } |
| 6359 | } |
| 6360 | |
| 6361 | return LY_SUCCESS; |
| 6362 | } |
| 6363 | |
| 6364 | /** |
| 6365 | * @brief Move context @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_NODE_SET |
| 6366 | * (or LYXP_SET_EMPTY). Context position aware. |
| 6367 | * |
| 6368 | * @param[in] set Set to use. |
| 6369 | * @param[in] all_desc Whether to go to all descendants ('//') or not ('/'). |
| 6370 | * @param[in] options XPath options. |
| 6371 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6372 | */ |
| 6373 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6374 | moveto_parent(struct lyxp_set *set, ly_bool all_desc, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6375 | { |
Michal Vasko | 9d41411 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6376 | LY_ERR rc = LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6377 | struct lyd_node *node, *new_node; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6378 | enum lyxp_node_type new_type; |
Michal Vasko | 9d41411 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6379 | struct lyxp_set result; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6380 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6381 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6382 | return LY_SUCCESS; |
| 6383 | } |
| 6384 | |
| 6385 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 6386 | 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] | 6387 | return LY_EVALID; |
| 6388 | } |
| 6389 | |
| 6390 | if (all_desc) { |
| 6391 | /* <path>//.. == <path>//./.. */ |
| 6392 | rc = moveto_self(set, 1, options); |
| 6393 | LY_CHECK_RET(rc); |
| 6394 | } |
| 6395 | |
Michal Vasko | 9d41411 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6396 | /* init result set */ |
| 6397 | set_init(&result, set); |
| 6398 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6399 | for (uint32_t i = 0; i < set->used; ++i) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6400 | node = set->val.nodes[i].node; |
| 6401 | |
| 6402 | if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 6403 | new_node = lyd_parent(node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6404 | } else if (set->val.nodes[i].type == LYXP_NODE_TEXT) { |
| 6405 | new_node = node; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6406 | } else if (set->val.nodes[i].type == LYXP_NODE_META) { |
| 6407 | new_node = set->val.meta[i].meta->parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6408 | if (!new_node) { |
Michal Vasko | 9d41411 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6409 | LOGINT(set->ctx); |
| 6410 | rc = LY_EINT; |
| 6411 | goto cleanup; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6412 | } |
| 6413 | } else { |
| 6414 | /* root does not have a parent */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6415 | continue; |
| 6416 | } |
| 6417 | |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 6418 | /* when check */ |
Michal Vasko | 9d41411 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6419 | if (!(options & LYXP_IGNORE_WHEN) && new_node && lysc_has_when(new_node->schema) && |
| 6420 | !(new_node->flags & LYD_WHEN_TRUE)) { |
| 6421 | rc = LY_EINCOMPLETE; |
| 6422 | goto cleanup; |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 6423 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6424 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6425 | if (!new_node) { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 6426 | /* node already there can also be the root */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6427 | new_type = set->root_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6428 | } else { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 6429 | /* node has a standard parent (it can equal the root, it's not the root yet since they are fake) */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6430 | new_type = LYXP_NODE_ELEM; |
| 6431 | } |
| 6432 | |
Michal Vasko | 9d41411 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6433 | /* check for duplicates, several nodes may have the same parent */ |
| 6434 | if (!set_dup_node_check(&result, new_node, new_type, -1)) { |
| 6435 | set_insert_node(&result, new_node, 0, new_type, result.used); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6436 | } |
| 6437 | } |
| 6438 | |
Michal Vasko | 9d41411 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6439 | /* move result to the set */ |
| 6440 | lyxp_set_free_content(set); |
| 6441 | *set = result; |
| 6442 | result.type = LYXP_SET_NUMBER; |
| 6443 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6444 | |
Michal Vasko | 9d41411 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6445 | cleanup: |
| 6446 | lyxp_set_free_content(&result); |
| 6447 | return rc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6448 | } |
| 6449 | |
| 6450 | /** |
| 6451 | * @brief Move context schema @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_SCNODE_SET |
| 6452 | * (or LYXP_SET_EMPTY). |
| 6453 | * |
| 6454 | * @param[in] set Set to use. |
| 6455 | * @param[in] all_desc Whether to go to all descendants ('//') or not ('/'). |
| 6456 | * @param[in] options XPath options. |
| 6457 | * @return LY_ERR |
| 6458 | */ |
| 6459 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6460 | moveto_scnode_parent(struct lyxp_set *set, ly_bool all_desc, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6461 | { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 6462 | uint32_t i, orig_used, idx; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6463 | ly_bool temp_ctx = 0; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6464 | const struct lysc_node *node, *new_node; |
| 6465 | enum lyxp_node_type new_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6466 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6467 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6468 | return LY_SUCCESS; |
| 6469 | } |
| 6470 | |
| 6471 | if (set->type != LYXP_SET_SCNODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 6472 | 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] | 6473 | return LY_EVALID; |
| 6474 | } |
| 6475 | |
| 6476 | if (all_desc) { |
| 6477 | /* <path>//.. == <path>//./.. */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6478 | LY_CHECK_RET(moveto_scnode_self(set, 1, options)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6479 | } |
| 6480 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6481 | orig_used = set->used; |
| 6482 | for (i = 0; i < orig_used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 6483 | if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) { |
| 6484 | if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6485 | continue; |
| 6486 | } |
| 6487 | |
| 6488 | /* remember context node */ |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 6489 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED; |
Michal Vasko | ec4df48 | 2019-12-16 10:02:18 +0100 | [diff] [blame] | 6490 | } else { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 6491 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6492 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6493 | |
| 6494 | node = set->val.scnodes[i].scnode; |
| 6495 | |
| 6496 | if (set->val.scnodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6497 | new_node = lysc_data_parent(node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6498 | } else { |
| 6499 | /* root does not have a parent */ |
| 6500 | continue; |
| 6501 | } |
| 6502 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6503 | if (!new_node) { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 6504 | /* node has no parent */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6505 | new_type = set->root_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6506 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6507 | } else { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 6508 | /* node has a standard parent (it can equal the root, it's not the root yet since they are fake) */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6509 | new_type = LYXP_NODE_ELEM; |
| 6510 | } |
| 6511 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 6512 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, new_node, new_type, &idx)); |
| 6513 | if ((idx < orig_used) && (idx > i)) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 6514 | set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6515 | temp_ctx = 1; |
| 6516 | } |
| 6517 | } |
| 6518 | |
| 6519 | if (temp_ctx) { |
| 6520 | for (i = 0; i < orig_used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 6521 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NEW_CTX) { |
| 6522 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6523 | } |
| 6524 | } |
| 6525 | } |
| 6526 | |
| 6527 | return LY_SUCCESS; |
| 6528 | } |
| 6529 | |
| 6530 | /** |
| 6531 | * @brief Move context @p set to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'. |
| 6532 | * Result is LYXP_SET_BOOLEAN. Indirectly context position aware. |
| 6533 | * |
| 6534 | * @param[in,out] set1 Set to use for the result. |
| 6535 | * @param[in] set2 Set acting as the second operand for @p op. |
| 6536 | * @param[in] op Comparison operator to process. |
| 6537 | * @param[in] options XPath options. |
| 6538 | * @return LY_ERR |
| 6539 | */ |
| 6540 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6541 | moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6542 | { |
| 6543 | /* |
| 6544 | * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING |
| 6545 | * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING) |
| 6546 | * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 6547 | * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 6548 | * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING |
| 6549 | * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6550 | * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 6551 | * |
| 6552 | * '=' or '!=' |
| 6553 | * BOOLEAN + BOOLEAN |
| 6554 | * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 6555 | * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 6556 | * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 6557 | * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 6558 | * NUMBER + NUMBER |
| 6559 | * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6560 | * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 6561 | * STRING + STRING |
| 6562 | * |
| 6563 | * '<=', '<', '>=', '>' |
| 6564 | * NUMBER + NUMBER |
| 6565 | * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 6566 | * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 6567 | * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 6568 | * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6569 | * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 6570 | * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 6571 | * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6572 | * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6573 | */ |
| 6574 | struct lyxp_set iter1, iter2; |
| 6575 | int result; |
| 6576 | int64_t i; |
| 6577 | LY_ERR rc; |
| 6578 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6579 | memset(&iter1, 0, sizeof iter1); |
| 6580 | memset(&iter2, 0, sizeof iter2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6581 | |
| 6582 | /* iterative evaluation with node-sets */ |
| 6583 | if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) { |
| 6584 | if (set1->type == LYXP_SET_NODE_SET) { |
| 6585 | for (i = 0; i < set1->used; ++i) { |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6586 | /* cast set1 */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6587 | switch (set2->type) { |
| 6588 | case LYXP_SET_NUMBER: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6589 | rc = set_comp_cast(&iter1, set1, LYXP_SET_NUMBER, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6590 | break; |
| 6591 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6592 | rc = set_comp_cast(&iter1, set1, LYXP_SET_BOOLEAN, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6593 | break; |
| 6594 | default: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6595 | rc = set_comp_cast(&iter1, set1, LYXP_SET_STRING, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6596 | break; |
| 6597 | } |
| 6598 | LY_CHECK_RET(rc); |
| 6599 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6600 | /* canonize set2 */ |
| 6601 | LY_CHECK_ERR_RET(rc = set_comp_canonize(&iter2, set2, &set1->val.nodes[i]), lyxp_set_free_content(&iter1), rc); |
| 6602 | |
| 6603 | /* compare recursively */ |
| 6604 | rc = moveto_op_comp(&iter1, &iter2, op, options); |
| 6605 | lyxp_set_free_content(&iter2); |
| 6606 | LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6607 | |
| 6608 | /* lazy evaluation until true */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6609 | if (iter1.val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6610 | set_fill_boolean(set1, 1); |
| 6611 | return LY_SUCCESS; |
| 6612 | } |
| 6613 | } |
| 6614 | } else { |
| 6615 | for (i = 0; i < set2->used; ++i) { |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6616 | /* set set2 */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6617 | switch (set1->type) { |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6618 | case LYXP_SET_NUMBER: |
| 6619 | rc = set_comp_cast(&iter2, set2, LYXP_SET_NUMBER, i); |
| 6620 | break; |
| 6621 | case LYXP_SET_BOOLEAN: |
| 6622 | rc = set_comp_cast(&iter2, set2, LYXP_SET_BOOLEAN, i); |
| 6623 | break; |
| 6624 | default: |
| 6625 | rc = set_comp_cast(&iter2, set2, LYXP_SET_STRING, i); |
| 6626 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6627 | } |
| 6628 | LY_CHECK_RET(rc); |
| 6629 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6630 | /* canonize set1 */ |
| 6631 | LY_CHECK_ERR_RET(rc = set_comp_canonize(&iter1, set1, &set2->val.nodes[i]), lyxp_set_free_content(&iter2), rc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6632 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6633 | /* compare recursively */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6634 | rc = moveto_op_comp(&iter1, &iter2, op, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6635 | lyxp_set_free_content(&iter2); |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6636 | LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6637 | |
| 6638 | /* lazy evaluation until true */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6639 | if (iter1.val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6640 | set_fill_boolean(set1, 1); |
| 6641 | return LY_SUCCESS; |
| 6642 | } |
| 6643 | } |
| 6644 | } |
| 6645 | |
| 6646 | /* false for all nodes */ |
| 6647 | set_fill_boolean(set1, 0); |
| 6648 | return LY_SUCCESS; |
| 6649 | } |
| 6650 | |
| 6651 | /* first convert properly */ |
| 6652 | if ((op[0] == '=') || (op[0] == '!')) { |
| 6653 | if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6654 | lyxp_set_cast(set1, LYXP_SET_BOOLEAN); |
| 6655 | lyxp_set_cast(set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6656 | } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6657 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6658 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6659 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6660 | LY_CHECK_RET(rc); |
| 6661 | } /* else we have 2 strings */ |
| 6662 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6663 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6664 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6665 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6666 | LY_CHECK_RET(rc); |
| 6667 | } |
| 6668 | |
| 6669 | assert(set1->type == set2->type); |
| 6670 | |
| 6671 | /* compute result */ |
| 6672 | if (op[0] == '=') { |
| 6673 | if (set1->type == LYXP_SET_BOOLEAN) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6674 | result = (set1->val.bln == set2->val.bln); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6675 | } else if (set1->type == LYXP_SET_NUMBER) { |
| 6676 | result = (set1->val.num == set2->val.num); |
| 6677 | } else { |
| 6678 | assert(set1->type == LYXP_SET_STRING); |
Michal Vasko | ac6c72f | 2019-11-14 16:09:34 +0100 | [diff] [blame] | 6679 | result = !strcmp(set1->val.str, set2->val.str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6680 | } |
| 6681 | } else if (op[0] == '!') { |
| 6682 | if (set1->type == LYXP_SET_BOOLEAN) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6683 | result = (set1->val.bln != set2->val.bln); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6684 | } else if (set1->type == LYXP_SET_NUMBER) { |
| 6685 | result = (set1->val.num != set2->val.num); |
| 6686 | } else { |
| 6687 | assert(set1->type == LYXP_SET_STRING); |
Michal Vasko | c205843 | 2020-11-06 17:26:21 +0100 | [diff] [blame] | 6688 | result = strcmp(set1->val.str, set2->val.str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6689 | } |
| 6690 | } else { |
| 6691 | assert(set1->type == LYXP_SET_NUMBER); |
| 6692 | if (op[0] == '<') { |
| 6693 | if (op[1] == '=') { |
| 6694 | result = (set1->val.num <= set2->val.num); |
| 6695 | } else { |
| 6696 | result = (set1->val.num < set2->val.num); |
| 6697 | } |
| 6698 | } else { |
| 6699 | if (op[1] == '=') { |
| 6700 | result = (set1->val.num >= set2->val.num); |
| 6701 | } else { |
| 6702 | result = (set1->val.num > set2->val.num); |
| 6703 | } |
| 6704 | } |
| 6705 | } |
| 6706 | |
| 6707 | /* assign result */ |
| 6708 | if (result) { |
| 6709 | set_fill_boolean(set1, 1); |
| 6710 | } else { |
| 6711 | set_fill_boolean(set1, 0); |
| 6712 | } |
| 6713 | |
| 6714 | return LY_SUCCESS; |
| 6715 | } |
| 6716 | |
| 6717 | /** |
| 6718 | * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div', |
| 6719 | * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware. |
| 6720 | * |
| 6721 | * @param[in,out] set1 Set to use for the result. |
| 6722 | * @param[in] set2 Set acting as the second operand for @p op. |
| 6723 | * @param[in] op Operator to process. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6724 | * @return LY_ERR |
| 6725 | */ |
| 6726 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6727 | 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] | 6728 | { |
| 6729 | LY_ERR rc; |
| 6730 | |
| 6731 | /* unary '-' */ |
| 6732 | if (!set2 && (op[0] == '-')) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6733 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6734 | LY_CHECK_RET(rc); |
| 6735 | set1->val.num *= -1; |
| 6736 | lyxp_set_free(set2); |
| 6737 | return LY_SUCCESS; |
| 6738 | } |
| 6739 | |
| 6740 | assert(set1 && set2); |
| 6741 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6742 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6743 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6744 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6745 | LY_CHECK_RET(rc); |
| 6746 | |
| 6747 | switch (op[0]) { |
| 6748 | /* '+' */ |
| 6749 | case '+': |
| 6750 | set1->val.num += set2->val.num; |
| 6751 | break; |
| 6752 | |
| 6753 | /* '-' */ |
| 6754 | case '-': |
| 6755 | set1->val.num -= set2->val.num; |
| 6756 | break; |
| 6757 | |
| 6758 | /* '*' */ |
| 6759 | case '*': |
| 6760 | set1->val.num *= set2->val.num; |
| 6761 | break; |
| 6762 | |
| 6763 | /* 'div' */ |
| 6764 | case 'd': |
| 6765 | set1->val.num /= set2->val.num; |
| 6766 | break; |
| 6767 | |
| 6768 | /* 'mod' */ |
| 6769 | case 'm': |
| 6770 | set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num); |
| 6771 | break; |
| 6772 | |
| 6773 | default: |
| 6774 | LOGINT_RET(set1->ctx); |
| 6775 | } |
| 6776 | |
| 6777 | return LY_SUCCESS; |
| 6778 | } |
| 6779 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6780 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6781 | * @brief Evaluate Predicate. Logs directly on error. |
| 6782 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6783 | * [9] Predicate ::= '[' Expr ']' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6784 | * |
| 6785 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6786 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6787 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6788 | * @param[in] options XPath options. |
| 6789 | * @param[in] parent_pos_pred Whether parent predicate was a positional one. |
| 6790 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6791 | */ |
| 6792 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 6793 | eval_predicate(const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, uint32_t options, ly_bool parent_pos_pred) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6794 | { |
| 6795 | LY_ERR rc; |
Michal Vasko | 1fdd8fa | 2021-01-08 09:21:45 +0100 | [diff] [blame] | 6796 | uint16_t orig_exp; |
| 6797 | uint32_t i, orig_pos, orig_size; |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6798 | int32_t pred_in_ctx; |
aPiecek | fff4dca | 2021-10-07 10:59:53 +0200 | [diff] [blame] | 6799 | struct lyxp_set set2 = {0}; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6800 | struct lyd_node *orig_parent; |
| 6801 | |
| 6802 | /* '[' */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6803 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 6804 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6805 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6806 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6807 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6808 | only_parse: |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6809 | 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] | 6810 | LY_CHECK_RET(rc); |
| 6811 | } else if (set->type == LYXP_SET_NODE_SET) { |
| 6812 | /* 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] | 6813 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6814 | |
| 6815 | /* empty set, nothing to evaluate */ |
| 6816 | if (!set->used) { |
| 6817 | goto only_parse; |
| 6818 | } |
| 6819 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6820 | orig_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6821 | orig_pos = 0; |
| 6822 | orig_size = set->used; |
| 6823 | orig_parent = NULL; |
Michal Vasko | 39dbf35 | 2020-05-21 10:08:59 +0200 | [diff] [blame] | 6824 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6825 | set_init(&set2, set); |
| 6826 | set_insert_node(&set2, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, 0); |
| 6827 | /* remember the node context position for position() and context size for last(), |
| 6828 | * predicates should always be evaluated with respect to the child axis (since we do |
| 6829 | * not support explicit axes) so we assign positions based on their parents */ |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 6830 | if (parent_pos_pred && (lyd_parent(set->val.nodes[i].node) != orig_parent)) { |
| 6831 | orig_parent = lyd_parent(set->val.nodes[i].node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6832 | orig_pos = 1; |
| 6833 | } else { |
| 6834 | ++orig_pos; |
| 6835 | } |
| 6836 | |
| 6837 | set2.ctx_pos = orig_pos; |
| 6838 | set2.ctx_size = orig_size; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6839 | *tok_idx = orig_exp; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6840 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6841 | rc = eval_expr_select(exp, tok_idx, 0, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6842 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6843 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6844 | return rc; |
| 6845 | } |
| 6846 | |
| 6847 | /* number is a position */ |
| 6848 | if (set2.type == LYXP_SET_NUMBER) { |
| 6849 | if ((long long)set2.val.num == orig_pos) { |
| 6850 | set2.val.num = 1; |
| 6851 | } else { |
| 6852 | set2.val.num = 0; |
| 6853 | } |
| 6854 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6855 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6856 | |
| 6857 | /* predicate satisfied or not? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6858 | if (!set2.val.bln) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6859 | set_remove_node_none(set, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6860 | } |
| 6861 | } |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6862 | set_remove_nodes_none(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6863 | |
| 6864 | } else if (set->type == LYXP_SET_SCNODE_SET) { |
| 6865 | for (i = 0; i < set->used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 6866 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6867 | /* there is a currently-valid node */ |
| 6868 | break; |
| 6869 | } |
| 6870 | } |
| 6871 | /* empty set, nothing to evaluate */ |
| 6872 | if (i == set->used) { |
| 6873 | goto only_parse; |
| 6874 | } |
| 6875 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6876 | orig_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6877 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6878 | /* set special in_ctx to all the valid snodes */ |
| 6879 | pred_in_ctx = set_scnode_new_in_ctx(set); |
| 6880 | |
| 6881 | /* use the valid snodes one-by-one */ |
| 6882 | for (i = 0; i < set->used; ++i) { |
| 6883 | if (set->val.scnodes[i].in_ctx != pred_in_ctx) { |
| 6884 | continue; |
| 6885 | } |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 6886 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6887 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6888 | *tok_idx = orig_exp; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6889 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6890 | rc = eval_expr_select(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6891 | LY_CHECK_RET(rc); |
| 6892 | |
| 6893 | set->val.scnodes[i].in_ctx = pred_in_ctx; |
| 6894 | } |
| 6895 | |
| 6896 | /* restore the state as it was before the predicate */ |
| 6897 | for (i = 0; i < set->used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 6898 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 6899 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6900 | } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 6901 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6902 | } |
| 6903 | } |
| 6904 | |
| 6905 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6906 | set2.type = LYXP_SET_NODE_SET; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6907 | set_fill_set(&set2, set); |
| 6908 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6909 | rc = eval_expr_select(exp, tok_idx, 0, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6910 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6911 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6912 | return rc; |
| 6913 | } |
| 6914 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6915 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6916 | if (!set2.val.bln) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6917 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6918 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6919 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6920 | } |
| 6921 | |
| 6922 | /* ']' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6923 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6924 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 6925 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6926 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6927 | |
| 6928 | return LY_SUCCESS; |
| 6929 | } |
| 6930 | |
| 6931 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6932 | * @brief Evaluate Literal. Logs directly on error. |
| 6933 | * |
| 6934 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6935 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6936 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 6937 | */ |
| 6938 | static void |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 6939 | eval_literal(const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6940 | { |
| 6941 | if (set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6942 | if (exp->tok_len[*tok_idx] == 2) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6943 | set_fill_string(set, "", 0); |
| 6944 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6945 | 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] | 6946 | } |
| 6947 | } |
| 6948 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 6949 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6950 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6951 | } |
| 6952 | |
| 6953 | /** |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6954 | * @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] | 6955 | * |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6956 | * @param[in] exp Full parsed XPath expression. |
| 6957 | * @param[in,out] tok_idx Index in @p exp at the beginning of the predicate, is updated on success. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6958 | * @param[in] ctx_node Found schema node as the context for the predicate. |
| 6959 | * @param[in] cur_mod Current module for the expression. |
| 6960 | * @param[in] cur_node Current (original context) node. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6961 | * @param[in] format Format of any prefixes in key names/values. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6962 | * @param[in] prefix_data Format-specific prefix data (see ::ly_resolve_prefix). |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6963 | * @param[out] predicates Parsed predicates. |
| 6964 | * @param[out] pred_type Type of @p predicates. |
| 6965 | * @return LY_SUCCESS on success, |
| 6966 | * @return LY_ERR on any error. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6967 | */ |
| 6968 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 6969 | eval_name_test_try_compile_predicates(const struct lyxp_expr *exp, uint16_t *tok_idx, const struct lysc_node *ctx_node, |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 6970 | const struct lys_module *cur_mod, const struct lysc_node *cur_node, LY_VALUE_FORMAT format, void *prefix_data, |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6971 | struct ly_path_predicate **predicates, enum ly_path_pred_type *pred_type) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6972 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6973 | LY_ERR ret = LY_SUCCESS; |
| 6974 | uint16_t key_count, e_idx, pred_idx = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6975 | const struct lysc_node *key; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6976 | size_t pred_len; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6977 | uint32_t prev_lo; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6978 | struct lyxp_expr *exp2 = NULL; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6979 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6980 | assert(ctx_node->nodetype & (LYS_LIST | LYS_LEAFLIST)); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6981 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6982 | if (ctx_node->nodetype == LYS_LIST) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6983 | /* get key count */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6984 | if (ctx_node->flags & LYS_KEYLESS) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6985 | return LY_EINVAL; |
| 6986 | } |
Michal Vasko | 544e58a | 2021-01-28 14:33:41 +0100 | [diff] [blame] | 6987 | for (key_count = 0, key = lysc_node_child(ctx_node); key && (key->flags & LYS_KEY); key = key->next, ++key_count) {} |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6988 | assert(key_count); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6989 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6990 | /* learn where the predicates end */ |
| 6991 | e_idx = *tok_idx; |
| 6992 | while (key_count) { |
| 6993 | /* '[' */ |
| 6994 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) { |
| 6995 | return LY_EINVAL; |
| 6996 | } |
| 6997 | ++e_idx; |
| 6998 | |
Michal Vasko | 3354d27 | 2021-04-06 09:40:06 +0200 | [diff] [blame] | 6999 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_NAMETEST)) { |
| 7000 | /* definitely not a key */ |
| 7001 | return LY_EINVAL; |
| 7002 | } |
| 7003 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7004 | /* ']' */ |
| 7005 | while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) { |
| 7006 | ++e_idx; |
| 7007 | } |
| 7008 | ++e_idx; |
| 7009 | |
| 7010 | /* another presumably key predicate parsed */ |
| 7011 | --key_count; |
| 7012 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7013 | } else { |
| 7014 | /* learn just where this single predicate ends */ |
| 7015 | e_idx = *tok_idx; |
| 7016 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7017 | /* '[' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7018 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) { |
| 7019 | return LY_EINVAL; |
| 7020 | } |
| 7021 | ++e_idx; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7022 | |
Michal Vasko | 3354d27 | 2021-04-06 09:40:06 +0200 | [diff] [blame] | 7023 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_DOT)) { |
| 7024 | /* definitely not the value */ |
| 7025 | return LY_EINVAL; |
| 7026 | } |
| 7027 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7028 | /* ']' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7029 | while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) { |
| 7030 | ++e_idx; |
| 7031 | } |
| 7032 | ++e_idx; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7033 | } |
| 7034 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7035 | /* get the joined length of all the keys predicates/of the single leaf-list predicate */ |
| 7036 | pred_len = (exp->tok_pos[e_idx - 1] + exp->tok_len[e_idx - 1]) - exp->tok_pos[*tok_idx]; |
| 7037 | |
| 7038 | /* turn logging off */ |
| 7039 | prev_lo = ly_log_options(0); |
| 7040 | |
| 7041 | /* parse the predicate(s) */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7042 | LY_CHECK_GOTO(ret = ly_path_parse_predicate(ctx_node->module->ctx, ctx_node, exp->expr + exp->tok_pos[*tok_idx], |
| 7043 | pred_len, LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp2), cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7044 | |
| 7045 | /* compile */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7046 | ret = ly_path_compile_predicate(ctx_node->module->ctx, cur_node, cur_mod, ctx_node, exp2, &pred_idx, format, |
| 7047 | prefix_data, predicates, pred_type); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7048 | LY_CHECK_GOTO(ret, cleanup); |
| 7049 | |
| 7050 | /* success, the predicate must include all the needed information for hash-based search */ |
| 7051 | *tok_idx = e_idx; |
| 7052 | |
| 7053 | cleanup: |
| 7054 | ly_log_options(prev_lo); |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7055 | lyxp_expr_free(ctx_node->module->ctx, exp2); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7056 | return ret; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7057 | } |
| 7058 | |
| 7059 | /** |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7060 | * @brief Search for/check the next schema node that could be the only matching schema node meaning the |
| 7061 | * data node(s) could be found using a single hash-based search. |
| 7062 | * |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7063 | * @param[in] ctx libyang context. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7064 | * @param[in] node Next context node to check. |
| 7065 | * @param[in] name Expected node name. |
| 7066 | * @param[in] name_len Length of @p name. |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7067 | * @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] | 7068 | * @param[in] root_type XPath root type. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7069 | * @param[in] format Prefix format. |
| 7070 | * @param[in,out] found Previously found node, is updated. |
| 7071 | * @return LY_SUCCESS on success, |
| 7072 | * @return LY_ENOT if the whole check failed and hashes cannot be used. |
| 7073 | */ |
| 7074 | static LY_ERR |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7075 | eval_name_test_with_predicate_get_scnode(const struct ly_ctx *ctx, const struct lyd_node *node, const char *name, |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 7076 | uint16_t name_len, const struct lys_module *moveto_mod, enum lyxp_node_type root_type, LY_VALUE_FORMAT format, |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7077 | const struct lysc_node **found) |
| 7078 | { |
| 7079 | const struct lysc_node *scnode; |
| 7080 | const struct lys_module *mod; |
| 7081 | uint32_t idx = 0; |
| 7082 | |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 7083 | assert((format == LY_VALUE_JSON) || moveto_mod); |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7084 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7085 | continue_search: |
Michal Vasko | 7d1d091 | 2020-10-16 08:38:30 +0200 | [diff] [blame] | 7086 | scnode = NULL; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7087 | if (!node) { |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 7088 | if ((format == LY_VALUE_JSON) && !moveto_mod) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7089 | /* search all modules for a single match */ |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7090 | while ((mod = ly_ctx_get_module_iter(ctx, &idx))) { |
Michal Vasko | 35a3b1d | 2021-07-14 09:34:16 +0200 | [diff] [blame] | 7091 | if (!mod->implemented) { |
| 7092 | continue; |
| 7093 | } |
| 7094 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7095 | scnode = lys_find_child(NULL, mod, name, name_len, 0, 0); |
| 7096 | if (scnode) { |
| 7097 | /* we have found a match */ |
| 7098 | break; |
| 7099 | } |
| 7100 | } |
| 7101 | |
Michal Vasko | 7d1d091 | 2020-10-16 08:38:30 +0200 | [diff] [blame] | 7102 | if (!scnode) { |
| 7103 | /* all modules searched */ |
| 7104 | idx = 0; |
| 7105 | } |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7106 | } else { |
| 7107 | /* search in top-level */ |
| 7108 | scnode = lys_find_child(NULL, moveto_mod, name, name_len, 0, 0); |
| 7109 | } |
| 7110 | } else if (!*found || (lysc_data_parent(*found) != node->schema)) { |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 7111 | if ((format == LY_VALUE_JSON) && !moveto_mod) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7112 | /* we must adjust the module to inherit the one from the context node */ |
| 7113 | moveto_mod = node->schema->module; |
| 7114 | } |
| 7115 | |
| 7116 | /* search in children, do not repeat the same search */ |
| 7117 | scnode = lys_find_child(node->schema, moveto_mod, name, name_len, 0, 0); |
Michal Vasko | 7d1d091 | 2020-10-16 08:38:30 +0200 | [diff] [blame] | 7118 | } /* else skip redundant search */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7119 | |
| 7120 | /* additional context check */ |
| 7121 | if (scnode && (root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) { |
| 7122 | scnode = NULL; |
| 7123 | } |
| 7124 | |
| 7125 | if (scnode) { |
| 7126 | if (*found) { |
| 7127 | /* we found a schema node with the same name but at different level, give up, too complicated |
| 7128 | * (more hash-based searches would be required, not supported) */ |
| 7129 | return LY_ENOT; |
| 7130 | } else { |
| 7131 | /* remember the found schema node and continue to make sure it can be used */ |
| 7132 | *found = scnode; |
| 7133 | } |
| 7134 | } |
| 7135 | |
| 7136 | if (idx) { |
| 7137 | /* continue searching all the following models */ |
| 7138 | goto continue_search; |
| 7139 | } |
| 7140 | |
| 7141 | return LY_SUCCESS; |
| 7142 | } |
| 7143 | |
| 7144 | /** |
Michal Vasko | 4ad69e7 | 2021-10-26 16:25:55 +0200 | [diff] [blame] | 7145 | * @brief Generate message when no matching schema nodes were found for a path segment. |
| 7146 | * |
| 7147 | * @param[in] set XPath set. |
| 7148 | * @param[in] scparent Previous schema parent in the context, if only one. |
| 7149 | * @param[in] ncname XPath NCName being evaluated. |
| 7150 | * @param[in] ncname_len Length of @p ncname. |
| 7151 | * @param[in] expr Whole XPath expression. |
| 7152 | * @param[in] options XPath options. |
| 7153 | */ |
| 7154 | static void |
| 7155 | eval_name_test_scnode_no_match_msg(struct lyxp_set *set, const struct lyxp_set_scnode *scparent, const char *ncname, |
| 7156 | uint16_t ncname_len, const char *expr, uint32_t options) |
| 7157 | { |
| 7158 | const char *format; |
| 7159 | char *path = NULL, *ppath = NULL; |
| 7160 | |
| 7161 | path = lysc_path(set->cur_scnode, LYSC_PATH_LOG, NULL, 0); |
| 7162 | if (scparent) { |
| 7163 | /* generate path for the parent */ |
| 7164 | if (scparent->type == LYXP_NODE_ELEM) { |
| 7165 | ppath = lysc_path(scparent->scnode, LYSC_PATH_LOG, NULL, 0); |
| 7166 | } else if (scparent->type == LYXP_NODE_ROOT) { |
| 7167 | ppath = strdup("<root>"); |
| 7168 | } else if (scparent->type == LYXP_NODE_ROOT_CONFIG) { |
| 7169 | ppath = strdup("<config-root>"); |
| 7170 | } |
| 7171 | } |
| 7172 | if (ppath) { |
| 7173 | format = "Schema node \"%.*s\" for parent \"%s\" not found; in expr \"%.*s\" with context node \"%s\"."; |
| 7174 | if (options & LYXP_SCNODE_ERROR) { |
| 7175 | LOGERR(set->ctx, LY_EVALID, format, ncname_len, ncname, ppath, (ncname - expr) + ncname_len, expr, path); |
| 7176 | } else { |
| 7177 | LOGWRN(set->ctx, format, ncname_len, ncname, ppath, (ncname - expr) + ncname_len, expr, path); |
| 7178 | } |
| 7179 | } else { |
| 7180 | format = "Schema node \"%.*s\" not found; in expr \"%.*s\" with context node \"%s\"."; |
| 7181 | if (options & LYXP_SCNODE_ERROR) { |
| 7182 | LOGERR(set->ctx, LY_EVALID, format, ncname_len, ncname, (ncname - expr) + ncname_len, expr, path); |
| 7183 | } else { |
| 7184 | LOGWRN(set->ctx, format, ncname_len, ncname, (ncname - expr) + ncname_len, expr, path); |
| 7185 | } |
| 7186 | } |
| 7187 | free(path); |
| 7188 | free(ppath); |
| 7189 | } |
| 7190 | |
| 7191 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7192 | * @brief Evaluate NameTest and any following Predicates. Logs directly on error. |
| 7193 | * |
| 7194 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 7195 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 7196 | * [7] NameTest ::= '*' | NCName ':' '*' | QName |
| 7197 | * |
| 7198 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7199 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7200 | * @param[in] attr_axis Whether to search attributes or standard nodes. |
| 7201 | * @param[in] all_desc Whether to search all the descendants or children only. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7202 | * @param[in,out] set Context and result set. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7203 | * @param[in] options XPath options. |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 7204 | * @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] | 7205 | */ |
| 7206 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 7207 | eval_name_test_with_predicate(const struct lyxp_expr *exp, uint16_t *tok_idx, ly_bool attr_axis, ly_bool all_desc, |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7208 | struct lyxp_set *set, uint32_t options) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7209 | { |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 7210 | LY_ERR rc = LY_SUCCESS, r; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7211 | const char *ncname, *ncname_dict = NULL; |
| 7212 | uint16_t ncname_len; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7213 | const struct lys_module *moveto_mod = NULL; |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7214 | const struct lysc_node *scnode = NULL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7215 | struct ly_path_predicate *predicates = NULL; |
| 7216 | enum ly_path_pred_type pred_type = 0; |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 7217 | int scnode_skip_pred = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7218 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7219 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7220 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7221 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7222 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7223 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7224 | goto moveto; |
| 7225 | } |
| 7226 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7227 | ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]]; |
| 7228 | ncname_len = exp->tok_len[*tok_idx - 1]; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7229 | |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7230 | if ((ncname[0] == '*') && (ncname_len == 1)) { |
| 7231 | /* all nodes will match */ |
| 7232 | goto moveto; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7233 | } |
| 7234 | |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7235 | /* parse (and skip) module name */ |
| 7236 | rc = moveto_resolve_model(&ncname, &ncname_len, set, NULL, &moveto_mod); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7237 | LY_CHECK_GOTO(rc, cleanup); |
| 7238 | |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 7239 | if (((set->format == LY_VALUE_JSON) || moveto_mod) && !attr_axis && !all_desc && (set->type == LYXP_SET_NODE_SET)) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7240 | /* find the matching schema node in some parent in the context */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7241 | for (uint32_t i = 0; i < set->used; ++i) { |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7242 | if (eval_name_test_with_predicate_get_scnode(set->ctx, set->val.nodes[i].node, ncname, ncname_len, |
| 7243 | moveto_mod, set->root_type, set->format, &scnode)) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7244 | /* check failed */ |
| 7245 | scnode = NULL; |
| 7246 | break; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7247 | } |
| 7248 | } |
| 7249 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7250 | if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) { |
| 7251 | /* try to create the predicates */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7252 | if (eval_name_test_try_compile_predicates(exp, tok_idx, scnode, set->cur_mod, set->cur_node ? |
| 7253 | set->cur_node->schema : NULL, set->format, set->prefix_data, &predicates, &pred_type)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7254 | /* hashes cannot be used */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7255 | scnode = NULL; |
| 7256 | } |
| 7257 | } |
| 7258 | } |
| 7259 | |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7260 | if (!scnode) { |
| 7261 | /* 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] | 7262 | * use dictionary for efficient comparison */ |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 7263 | 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] | 7264 | } |
| 7265 | |
| 7266 | moveto: |
| 7267 | /* move to the attribute(s), data node(s), or schema node(s) */ |
| 7268 | if (attr_axis) { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7269 | if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 7270 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7271 | } else { |
| 7272 | if (all_desc) { |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 7273 | rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7274 | } else { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7275 | rc = moveto_attr(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7276 | } |
| 7277 | LY_CHECK_GOTO(rc, cleanup); |
| 7278 | } |
| 7279 | } else { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7280 | if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7281 | int64_t i; |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 7282 | const struct lyxp_set_scnode *scparent = NULL; |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 7283 | |
| 7284 | /* remember parent if there is only one, to print in the warning */ |
| 7285 | for (i = 0; i < set->used; ++i) { |
| 7286 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
| 7287 | if (!scparent) { |
| 7288 | /* remember the context node */ |
| 7289 | scparent = &set->val.scnodes[i]; |
| 7290 | } else { |
| 7291 | /* several context nodes, no reasonable error possible */ |
| 7292 | scparent = NULL; |
| 7293 | break; |
| 7294 | } |
| 7295 | } |
| 7296 | } |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7297 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7298 | if (all_desc) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7299 | rc = moveto_scnode_alldesc(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7300 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7301 | rc = moveto_scnode(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7302 | } |
| 7303 | LY_CHECK_GOTO(rc, cleanup); |
| 7304 | |
| 7305 | for (i = set->used - 1; i > -1; --i) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 7306 | if (set->val.scnodes[i].in_ctx > LYXP_SET_SCNODE_ATOM_NODE) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7307 | break; |
| 7308 | } |
| 7309 | } |
| 7310 | if (i == -1) { |
Michal Vasko | 4ad69e7 | 2021-10-26 16:25:55 +0200 | [diff] [blame] | 7311 | /* generate message */ |
| 7312 | eval_name_test_scnode_no_match_msg(set, scparent, ncname, ncname_len, exp->expr, options); |
| 7313 | |
| 7314 | if (options & LYXP_SCNODE_ERROR) { |
| 7315 | /* error */ |
| 7316 | rc = LY_EVALID; |
| 7317 | goto cleanup; |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 7318 | } |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 7319 | |
| 7320 | /* skip the predicates and the rest of this path to not generate invalid warnings */ |
| 7321 | rc = LY_ENOT; |
| 7322 | scnode_skip_pred = 1; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7323 | } |
| 7324 | } else { |
| 7325 | if (all_desc) { |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 7326 | rc = moveto_node_alldesc(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7327 | } else { |
| 7328 | if (scnode) { |
| 7329 | /* we can find the nodes using hashes */ |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 7330 | rc = moveto_node_hash(set, scnode, predicates, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7331 | } else { |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 7332 | rc = moveto_node(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7333 | } |
| 7334 | } |
| 7335 | LY_CHECK_GOTO(rc, cleanup); |
| 7336 | } |
| 7337 | } |
| 7338 | |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 7339 | if (scnode_skip_pred) { |
| 7340 | /* skip predicates */ |
| 7341 | options |= LYXP_SKIP_EXPR; |
| 7342 | } |
| 7343 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7344 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7345 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 7346 | r = eval_predicate(exp, tok_idx, set, options, 1); |
| 7347 | LY_CHECK_ERR_GOTO(r, rc = r, cleanup); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7348 | } |
| 7349 | |
| 7350 | cleanup: |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 7351 | if (scnode_skip_pred) { |
| 7352 | /* restore options */ |
| 7353 | options &= ~LYXP_SKIP_EXPR; |
| 7354 | } |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7355 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7356 | lydict_remove(set->ctx, ncname_dict); |
Michal Vasko | f7e16e2 | 2020-10-21 09:24:39 +0200 | [diff] [blame] | 7357 | ly_path_predicates_free(set->ctx, pred_type, predicates); |
Michal Vasko | db51a8d | 2020-05-27 15:22:29 +0200 | [diff] [blame] | 7358 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7359 | return rc; |
| 7360 | } |
| 7361 | |
| 7362 | /** |
| 7363 | * @brief Evaluate NodeType and any following Predicates. Logs directly on error. |
| 7364 | * |
| 7365 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 7366 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 7367 | * [8] NodeType ::= 'text' | 'node' |
| 7368 | * |
| 7369 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7370 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7371 | * @param[in] attr_axis Whether to search attributes or standard nodes. |
| 7372 | * @param[in] all_desc Whether to search all the descendants or children only. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7373 | * @param[in,out] set Context and result set. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7374 | * @param[in] options XPath options. |
| 7375 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7376 | */ |
| 7377 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 7378 | eval_node_type_with_predicate(const struct lyxp_expr *exp, uint16_t *tok_idx, ly_bool attr_axis, ly_bool all_desc, |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7379 | struct lyxp_set *set, uint32_t options) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7380 | { |
| 7381 | LY_ERR rc; |
| 7382 | |
| 7383 | /* TODO */ |
| 7384 | (void)attr_axis; |
| 7385 | (void)all_desc; |
| 7386 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7387 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7388 | assert(exp->tok_len[*tok_idx] == 4); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7389 | if (set->type == LYXP_SET_SCNODE_SET) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 7390 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7391 | options |= LYXP_SKIP_EXPR; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7392 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7393 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7394 | rc = xpath_node(NULL, 0, set, options); |
| 7395 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7396 | assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4)); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7397 | rc = xpath_text(NULL, 0, set, options); |
| 7398 | } |
| 7399 | LY_CHECK_RET(rc); |
| 7400 | } |
| 7401 | } |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7402 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7403 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7404 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7405 | |
| 7406 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7407 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7408 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7409 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7410 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7411 | |
| 7412 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7413 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7414 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7415 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7416 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7417 | |
| 7418 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7419 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 7420 | rc = eval_predicate(exp, tok_idx, set, options, 1); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7421 | LY_CHECK_RET(rc); |
| 7422 | } |
| 7423 | |
| 7424 | return LY_SUCCESS; |
| 7425 | } |
| 7426 | |
| 7427 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7428 | * @brief Evaluate RelativeLocationPath. Logs directly on error. |
| 7429 | * |
| 7430 | * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step |
| 7431 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7432 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7433 | * |
| 7434 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7435 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7436 | * @param[in] all_desc Whether to search all the descendants or children only. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7437 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7438 | * @param[in] options XPath options. |
| 7439 | * @return LY_ERR (YL_EINCOMPLETE on unresolved when) |
| 7440 | */ |
| 7441 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 7442 | eval_relative_location_path(const struct lyxp_expr *exp, uint16_t *tok_idx, ly_bool all_desc, struct lyxp_set *set, |
| 7443 | uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7444 | { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 7445 | ly_bool attr_axis; |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 7446 | LY_ERR rc = LY_SUCCESS; |
| 7447 | int scnode_skip_path = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7448 | |
| 7449 | goto step; |
| 7450 | do { |
| 7451 | /* evaluate '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7452 | if (exp->tok_len[*tok_idx] == 1) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7453 | all_desc = 0; |
| 7454 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7455 | assert(exp->tok_len[*tok_idx] == 2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7456 | all_desc = 1; |
| 7457 | } |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7458 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7459 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7460 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7461 | |
| 7462 | step: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7463 | /* evaluate abbreviated axis '@'? if any */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7464 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7465 | attr_axis = 1; |
| 7466 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7467 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7468 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7469 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7470 | } else { |
| 7471 | attr_axis = 0; |
| 7472 | } |
| 7473 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7474 | /* Step */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7475 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7476 | case LYXP_TOKEN_DOT: |
| 7477 | /* evaluate '.' */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7478 | if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7479 | rc = moveto_scnode_self(set, all_desc, options); |
| 7480 | } else { |
| 7481 | rc = moveto_self(set, all_desc, options); |
| 7482 | } |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 7483 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7484 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7485 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7486 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7487 | break; |
| 7488 | |
| 7489 | case LYXP_TOKEN_DDOT: |
| 7490 | /* evaluate '..' */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7491 | if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7492 | rc = moveto_scnode_parent(set, all_desc, options); |
| 7493 | } else { |
| 7494 | rc = moveto_parent(set, all_desc, options); |
| 7495 | } |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 7496 | LY_CHECK_GOTO(rc, cleanup); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7497 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7498 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7499 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7500 | break; |
| 7501 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7502 | case LYXP_TOKEN_NAMETEST: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7503 | /* evaluate NameTest Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7504 | rc = eval_name_test_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options); |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 7505 | if (rc == LY_ENOT) { |
| 7506 | assert(options & LYXP_SCNODE_ALL); |
| 7507 | /* skip the rest of this path */ |
| 7508 | rc = LY_SUCCESS; |
| 7509 | scnode_skip_path = 1; |
| 7510 | options |= LYXP_SKIP_EXPR; |
| 7511 | } |
| 7512 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7513 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7514 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7515 | case LYXP_TOKEN_NODETYPE: |
| 7516 | /* evaluate NodeType Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7517 | rc = eval_node_type_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options); |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 7518 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7519 | break; |
| 7520 | |
| 7521 | default: |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 7522 | LOGINT(set->ctx); |
| 7523 | rc = LY_EINT; |
| 7524 | goto cleanup; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7525 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7526 | } 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] | 7527 | |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 7528 | cleanup: |
| 7529 | if (scnode_skip_path) { |
| 7530 | options &= ~LYXP_SKIP_EXPR; |
| 7531 | } |
| 7532 | return rc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7533 | } |
| 7534 | |
| 7535 | /** |
| 7536 | * @brief Evaluate AbsoluteLocationPath. Logs directly on error. |
| 7537 | * |
| 7538 | * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath |
| 7539 | * |
| 7540 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7541 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7542 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7543 | * @param[in] options XPath options. |
| 7544 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7545 | */ |
| 7546 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 7547 | eval_absolute_location_path(const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7548 | { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 7549 | ly_bool all_desc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7550 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7551 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7552 | /* no matter what tokens follow, we need to be at the root */ |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 7553 | LY_CHECK_RET(moveto_root(set, options)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7554 | } |
| 7555 | |
| 7556 | /* '/' RelativeLocationPath? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7557 | if (exp->tok_len[*tok_idx] == 1) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7558 | /* evaluate '/' - deferred */ |
| 7559 | all_desc = 0; |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7560 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7561 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7562 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7563 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7564 | if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7565 | return LY_SUCCESS; |
| 7566 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7567 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7568 | case LYXP_TOKEN_DOT: |
| 7569 | case LYXP_TOKEN_DDOT: |
| 7570 | case LYXP_TOKEN_AT: |
| 7571 | case LYXP_TOKEN_NAMETEST: |
| 7572 | case LYXP_TOKEN_NODETYPE: |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 7573 | 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] | 7574 | break; |
| 7575 | default: |
| 7576 | break; |
| 7577 | } |
| 7578 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7579 | } else { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 7580 | /* '//' RelativeLocationPath */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7581 | /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */ |
| 7582 | all_desc = 1; |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7583 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7584 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7585 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7586 | |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 7587 | 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] | 7588 | } |
| 7589 | |
| 7590 | return LY_SUCCESS; |
| 7591 | } |
| 7592 | |
| 7593 | /** |
| 7594 | * @brief Evaluate FunctionCall. Logs directly on error. |
| 7595 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7596 | * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7597 | * |
| 7598 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7599 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7600 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7601 | * @param[in] options XPath options. |
| 7602 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7603 | */ |
| 7604 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 7605 | eval_function_call(const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7606 | { |
| 7607 | LY_ERR rc; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7608 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7609 | LY_ERR (*xpath_func)(struct lyxp_set **, uint16_t, struct lyxp_set *, uint32_t) = NULL; |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 7610 | uint16_t arg_count = 0, i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7611 | struct lyxp_set **args = NULL, **args_aux; |
| 7612 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7613 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7614 | /* FunctionName */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7615 | switch (exp->tok_len[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7616 | case 3: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7617 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7618 | xpath_func = &xpath_not; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7619 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7620 | xpath_func = &xpath_sum; |
| 7621 | } |
| 7622 | break; |
| 7623 | case 4: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7624 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7625 | xpath_func = &xpath_lang; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7626 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7627 | xpath_func = &xpath_last; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7628 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7629 | xpath_func = &xpath_name; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7630 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7631 | xpath_func = &xpath_true; |
| 7632 | } |
| 7633 | break; |
| 7634 | case 5: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7635 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7636 | xpath_func = &xpath_count; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7637 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7638 | xpath_func = &xpath_false; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7639 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7640 | xpath_func = &xpath_floor; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7641 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7642 | xpath_func = &xpath_round; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7643 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7644 | xpath_func = &xpath_deref; |
| 7645 | } |
| 7646 | break; |
| 7647 | case 6: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7648 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7649 | xpath_func = &xpath_concat; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7650 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7651 | xpath_func = &xpath_number; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7652 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7653 | xpath_func = &xpath_string; |
| 7654 | } |
| 7655 | break; |
| 7656 | case 7: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7657 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7658 | xpath_func = &xpath_boolean; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7659 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7660 | xpath_func = &xpath_ceiling; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7661 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7662 | xpath_func = &xpath_current; |
| 7663 | } |
| 7664 | break; |
| 7665 | case 8: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7666 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7667 | xpath_func = &xpath_contains; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7668 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7669 | xpath_func = &xpath_position; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7670 | } 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] | 7671 | xpath_func = &xpath_re_match; |
| 7672 | } |
| 7673 | break; |
| 7674 | case 9: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7675 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7676 | xpath_func = &xpath_substring; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7677 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7678 | xpath_func = &xpath_translate; |
| 7679 | } |
| 7680 | break; |
| 7681 | case 10: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7682 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7683 | xpath_func = &xpath_local_name; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7684 | } 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] | 7685 | xpath_func = &xpath_enum_value; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7686 | } 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] | 7687 | xpath_func = &xpath_bit_is_set; |
| 7688 | } |
| 7689 | break; |
| 7690 | case 11: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7691 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7692 | xpath_func = &xpath_starts_with; |
| 7693 | } |
| 7694 | break; |
| 7695 | case 12: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7696 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7697 | xpath_func = &xpath_derived_from; |
| 7698 | } |
| 7699 | break; |
| 7700 | case 13: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7701 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7702 | xpath_func = &xpath_namespace_uri; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7703 | } 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] | 7704 | xpath_func = &xpath_string_length; |
| 7705 | } |
| 7706 | break; |
| 7707 | case 15: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7708 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7709 | xpath_func = &xpath_normalize_space; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7710 | } 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] | 7711 | xpath_func = &xpath_substring_after; |
| 7712 | } |
| 7713 | break; |
| 7714 | case 16: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7715 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7716 | xpath_func = &xpath_substring_before; |
| 7717 | } |
| 7718 | break; |
| 7719 | case 20: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7720 | 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] | 7721 | xpath_func = &xpath_derived_from_or_self; |
| 7722 | } |
| 7723 | break; |
| 7724 | } |
| 7725 | |
| 7726 | if (!xpath_func) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 7727 | LOGVAL(set->ctx, LY_VCODE_XP_INFUNC, exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7728 | return LY_EVALID; |
| 7729 | } |
| 7730 | } |
| 7731 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7732 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7733 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7734 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7735 | |
| 7736 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7737 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7738 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7739 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7740 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7741 | |
| 7742 | /* ( Expr ( ',' Expr )* )? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7743 | if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7744 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7745 | args = malloc(sizeof *args); |
| 7746 | LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup); |
| 7747 | arg_count = 1; |
| 7748 | args[0] = set_copy(set); |
| 7749 | if (!args[0]) { |
| 7750 | rc = LY_EMEM; |
| 7751 | goto cleanup; |
| 7752 | } |
| 7753 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7754 | rc = eval_expr_select(exp, tok_idx, 0, args[0], options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7755 | LY_CHECK_GOTO(rc, cleanup); |
| 7756 | } else { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7757 | 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] | 7758 | LY_CHECK_GOTO(rc, cleanup); |
| 7759 | } |
| 7760 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7761 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7762 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7763 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7764 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7765 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7766 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7767 | ++arg_count; |
| 7768 | args_aux = realloc(args, arg_count * sizeof *args); |
| 7769 | LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup); |
| 7770 | args = args_aux; |
| 7771 | args[arg_count - 1] = set_copy(set); |
| 7772 | if (!args[arg_count - 1]) { |
| 7773 | rc = LY_EMEM; |
| 7774 | goto cleanup; |
| 7775 | } |
| 7776 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7777 | 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] | 7778 | LY_CHECK_GOTO(rc, cleanup); |
| 7779 | } else { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7780 | 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] | 7781 | LY_CHECK_GOTO(rc, cleanup); |
| 7782 | } |
| 7783 | } |
| 7784 | |
| 7785 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7786 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7787 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7788 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7789 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7790 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7791 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7792 | /* evaluate function */ |
| 7793 | rc = xpath_func(args, arg_count, set, options); |
| 7794 | |
| 7795 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7796 | /* merge all nodes from arg evaluations */ |
| 7797 | for (i = 0; i < arg_count; ++i) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 7798 | set_scnode_clear_ctx(args[i], LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7799 | lyxp_set_scnode_merge(set, args[i]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7800 | } |
| 7801 | } |
| 7802 | } else { |
| 7803 | rc = LY_SUCCESS; |
| 7804 | } |
| 7805 | |
| 7806 | cleanup: |
| 7807 | for (i = 0; i < arg_count; ++i) { |
| 7808 | lyxp_set_free(args[i]); |
| 7809 | } |
| 7810 | free(args); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7811 | return rc; |
| 7812 | } |
| 7813 | |
| 7814 | /** |
| 7815 | * @brief Evaluate Number. Logs directly on error. |
| 7816 | * |
| 7817 | * @param[in] ctx Context for errors. |
| 7818 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7819 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7820 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7821 | * @return LY_ERR |
| 7822 | */ |
| 7823 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 7824 | eval_number(struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7825 | { |
| 7826 | long double num; |
| 7827 | char *endptr; |
| 7828 | |
| 7829 | if (set) { |
| 7830 | errno = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7831 | num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7832 | if (errno) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 7833 | LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]); |
| 7834 | LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double (%s).", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7835 | 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] | 7836 | return LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7837 | } else if (endptr - &exp->expr[exp->tok_pos[*tok_idx]] != exp->tok_len[*tok_idx]) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 7838 | LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]); |
| 7839 | LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double.", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7840 | exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7841 | return LY_EVALID; |
| 7842 | } |
| 7843 | |
| 7844 | set_fill_number(set, num); |
| 7845 | } |
| 7846 | |
| 7847 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7848 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7849 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7850 | return LY_SUCCESS; |
| 7851 | } |
| 7852 | |
aPiecek | df23eee | 2021-10-07 12:21:50 +0200 | [diff] [blame] | 7853 | LY_ERR |
| 7854 | lyxp_vars_find(struct lyxp_var *vars, const char *name, size_t name_len, struct lyxp_var **var) |
| 7855 | { |
| 7856 | LY_ERR ret = LY_ENOTFOUND; |
| 7857 | LY_ARRAY_COUNT_TYPE u; |
| 7858 | |
| 7859 | assert(vars && name); |
| 7860 | |
| 7861 | name_len = name_len ? name_len : strlen(name); |
| 7862 | |
| 7863 | LY_ARRAY_FOR(vars, u) { |
| 7864 | if (!strncmp(vars[u].name, name, name_len)) { |
| 7865 | ret = LY_SUCCESS; |
| 7866 | break; |
| 7867 | } |
| 7868 | } |
| 7869 | |
| 7870 | if (var && !ret) { |
| 7871 | *var = &vars[u]; |
| 7872 | } |
| 7873 | |
| 7874 | return ret; |
| 7875 | } |
| 7876 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7877 | /** |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 7878 | * @brief Evaluate VariableReference. |
| 7879 | * |
| 7880 | * @param[in] exp Parsed XPath expression. |
| 7881 | * @param[in] tok_idx Position in the expression @p exp. |
| 7882 | * @param[in] vars [Sized array](@ref sizedarrays) of XPath variables. |
| 7883 | * @param[in,out] set Context and result set. |
| 7884 | * @param[in] options XPath options. |
| 7885 | * @return LY_ERR value. |
| 7886 | */ |
| 7887 | static LY_ERR |
| 7888 | eval_variable_reference(const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, uint32_t options) |
| 7889 | { |
| 7890 | LY_ERR ret; |
| 7891 | const char *name; |
| 7892 | struct lyxp_var *var; |
| 7893 | const struct lyxp_var *vars; |
| 7894 | struct lyxp_expr *tokens = NULL; |
| 7895 | uint16_t token_index; |
| 7896 | |
| 7897 | vars = set->vars; |
| 7898 | |
| 7899 | /* Find out the name and value of the variable. */ |
| 7900 | name = &exp->expr[exp->tok_pos[*tok_idx]]; |
| 7901 | ret = lyxp_vars_find((struct lyxp_var *)vars, name, exp->tok_len[*tok_idx], &var); |
| 7902 | LY_CHECK_ERR_RET(ret, LOGERR(set->ctx, ret, |
| 7903 | "XPath variable \"%s\" not defined.", name), ret); |
| 7904 | |
| 7905 | /* Parse value. */ |
| 7906 | ret = lyxp_expr_parse(set->ctx, var->value, 0, 1, &tokens); |
| 7907 | LY_CHECK_GOTO(ret, cleanup); |
| 7908 | |
| 7909 | /* Evaluate value. */ |
| 7910 | token_index = 0; |
| 7911 | ret = eval_expr_select(tokens, &token_index, 0, set, options); |
| 7912 | LY_CHECK_GOTO(ret, cleanup); |
| 7913 | |
| 7914 | cleanup: |
| 7915 | lyxp_expr_free(set->ctx, tokens); |
| 7916 | |
| 7917 | return ret; |
| 7918 | } |
| 7919 | |
| 7920 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7921 | * @brief Evaluate PathExpr. Logs directly on error. |
| 7922 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7923 | * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate* |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7924 | * | PrimaryExpr Predicate* '/' RelativeLocationPath |
| 7925 | * | PrimaryExpr Predicate* '//' RelativeLocationPath |
| 7926 | * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 7927 | * [10] PrimaryExpr ::= VariableReference | '(' Expr ')' | Literal | Number | FunctionCall |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7928 | * |
| 7929 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7930 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7931 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7932 | * @param[in] options XPath options. |
| 7933 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7934 | */ |
| 7935 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 7936 | eval_path_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7937 | { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 7938 | ly_bool all_desc, parent_pos_pred; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7939 | LY_ERR rc; |
| 7940 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7941 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7942 | case LYXP_TOKEN_PAR1: |
| 7943 | /* '(' Expr ')' */ |
| 7944 | |
| 7945 | /* '(' */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7946 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7947 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7948 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7949 | |
| 7950 | /* Expr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7951 | rc = eval_expr_select(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7952 | LY_CHECK_RET(rc); |
| 7953 | |
| 7954 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7955 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7956 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7957 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7958 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7959 | |
| 7960 | parent_pos_pred = 0; |
| 7961 | goto predicate; |
| 7962 | |
| 7963 | case LYXP_TOKEN_DOT: |
| 7964 | case LYXP_TOKEN_DDOT: |
| 7965 | case LYXP_TOKEN_AT: |
| 7966 | case LYXP_TOKEN_NAMETEST: |
| 7967 | case LYXP_TOKEN_NODETYPE: |
| 7968 | /* RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7969 | rc = eval_relative_location_path(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7970 | LY_CHECK_RET(rc); |
| 7971 | break; |
| 7972 | |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 7973 | case LYXP_TOKEN_VARREF: |
| 7974 | /* VariableReference */ |
| 7975 | rc = eval_variable_reference(exp, tok_idx, set, options); |
| 7976 | LY_CHECK_RET(rc); |
| 7977 | ++(*tok_idx); |
| 7978 | |
| 7979 | parent_pos_pred = 1; |
| 7980 | goto predicate; |
| 7981 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7982 | case LYXP_TOKEN_FUNCNAME: |
| 7983 | /* FunctionCall */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7984 | rc = eval_function_call(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7985 | LY_CHECK_RET(rc); |
| 7986 | |
| 7987 | parent_pos_pred = 1; |
| 7988 | goto predicate; |
| 7989 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 7990 | case LYXP_TOKEN_OPER_PATH: |
| 7991 | case LYXP_TOKEN_OPER_RPATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7992 | /* AbsoluteLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7993 | rc = eval_absolute_location_path(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7994 | LY_CHECK_RET(rc); |
| 7995 | break; |
| 7996 | |
| 7997 | case LYXP_TOKEN_LITERAL: |
| 7998 | /* Literal */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7999 | if ((options & LYXP_SKIP_EXPR) || (options & LYXP_SCNODE_ALL)) { |
| 8000 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 8001 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8002 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8003 | eval_literal(exp, tok_idx, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8004 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8005 | eval_literal(exp, tok_idx, set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8006 | } |
| 8007 | |
| 8008 | parent_pos_pred = 1; |
| 8009 | goto predicate; |
| 8010 | |
| 8011 | case LYXP_TOKEN_NUMBER: |
| 8012 | /* Number */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8013 | if ((options & LYXP_SKIP_EXPR) || (options & LYXP_SCNODE_ALL)) { |
| 8014 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 8015 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8016 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8017 | rc = eval_number(NULL, exp, tok_idx, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8018 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8019 | rc = eval_number(set->ctx, exp, tok_idx, set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8020 | } |
| 8021 | LY_CHECK_RET(rc); |
| 8022 | |
| 8023 | parent_pos_pred = 1; |
| 8024 | goto predicate; |
| 8025 | |
| 8026 | default: |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 8027 | LOGVAL(set->ctx, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8028 | return LY_EVALID; |
| 8029 | } |
| 8030 | |
| 8031 | return LY_SUCCESS; |
| 8032 | |
| 8033 | predicate: |
| 8034 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8035 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 8036 | rc = eval_predicate(exp, tok_idx, set, options, parent_pos_pred); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8037 | LY_CHECK_RET(rc); |
| 8038 | } |
| 8039 | |
| 8040 | /* ('/' or '//') RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8041 | 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] | 8042 | |
| 8043 | /* evaluate '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8044 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8045 | all_desc = 0; |
| 8046 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8047 | all_desc = 1; |
| 8048 | } |
| 8049 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8050 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 8051 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8052 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8053 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8054 | rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8055 | LY_CHECK_RET(rc); |
| 8056 | } |
| 8057 | |
| 8058 | return LY_SUCCESS; |
| 8059 | } |
| 8060 | |
| 8061 | /** |
| 8062 | * @brief Evaluate UnionExpr. Logs directly on error. |
| 8063 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8064 | * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8065 | * |
| 8066 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8067 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8068 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8069 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8070 | * @param[in] options XPath options. |
| 8071 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8072 | */ |
| 8073 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 8074 | eval_union_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8075 | { |
| 8076 | LY_ERR rc = LY_SUCCESS; |
| 8077 | struct lyxp_set orig_set, set2; |
| 8078 | uint16_t i; |
| 8079 | |
| 8080 | assert(repeat); |
| 8081 | |
| 8082 | set_init(&orig_set, set); |
| 8083 | set_init(&set2, set); |
| 8084 | |
| 8085 | set_fill_set(&orig_set, set); |
| 8086 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8087 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8088 | LY_CHECK_GOTO(rc, cleanup); |
| 8089 | |
| 8090 | /* ('|' PathExpr)* */ |
| 8091 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8092 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8093 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 8094 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8095 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8096 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8097 | if (options & LYXP_SKIP_EXPR) { |
| 8098 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8099 | LY_CHECK_GOTO(rc, cleanup); |
| 8100 | continue; |
| 8101 | } |
| 8102 | |
| 8103 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8104 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8105 | LY_CHECK_GOTO(rc, cleanup); |
| 8106 | |
| 8107 | /* eval */ |
| 8108 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 8109 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8110 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8111 | rc = moveto_union(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8112 | LY_CHECK_GOTO(rc, cleanup); |
| 8113 | } |
| 8114 | } |
| 8115 | |
| 8116 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8117 | lyxp_set_free_content(&orig_set); |
| 8118 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8119 | return rc; |
| 8120 | } |
| 8121 | |
| 8122 | /** |
| 8123 | * @brief Evaluate UnaryExpr. Logs directly on error. |
| 8124 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8125 | * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8126 | * |
| 8127 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8128 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8129 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8130 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8131 | * @param[in] options XPath options. |
| 8132 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8133 | */ |
| 8134 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 8135 | eval_unary_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8136 | { |
| 8137 | LY_ERR rc; |
| 8138 | uint16_t this_op, i; |
| 8139 | |
| 8140 | assert(repeat); |
| 8141 | |
| 8142 | /* ('-')+ */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8143 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8144 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8145 | 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] | 8146 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8147 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 8148 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8149 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8150 | } |
| 8151 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8152 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8153 | LY_CHECK_RET(rc); |
| 8154 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8155 | if (!(options & LYXP_SKIP_EXPR) && (repeat % 2)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8156 | if (options & LYXP_SCNODE_ALL) { |
| 8157 | warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]); |
| 8158 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8159 | 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] | 8160 | LY_CHECK_RET(rc); |
| 8161 | } |
| 8162 | } |
| 8163 | |
| 8164 | return LY_SUCCESS; |
| 8165 | } |
| 8166 | |
| 8167 | /** |
| 8168 | * @brief Evaluate MultiplicativeExpr. Logs directly on error. |
| 8169 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8170 | * [18] MultiplicativeExpr ::= UnaryExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8171 | * | MultiplicativeExpr '*' UnaryExpr |
| 8172 | * | MultiplicativeExpr 'div' UnaryExpr |
| 8173 | * | MultiplicativeExpr 'mod' UnaryExpr |
| 8174 | * |
| 8175 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8176 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8177 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8178 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8179 | * @param[in] options XPath options. |
| 8180 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8181 | */ |
| 8182 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 8183 | eval_multiplicative_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, |
| 8184 | uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8185 | { |
| 8186 | LY_ERR rc; |
| 8187 | uint16_t this_op; |
| 8188 | struct lyxp_set orig_set, set2; |
| 8189 | uint16_t i; |
| 8190 | |
| 8191 | assert(repeat); |
| 8192 | |
| 8193 | set_init(&orig_set, set); |
| 8194 | set_init(&set2, set); |
| 8195 | |
| 8196 | set_fill_set(&orig_set, set); |
| 8197 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8198 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8199 | LY_CHECK_GOTO(rc, cleanup); |
| 8200 | |
| 8201 | /* ('*' / 'div' / 'mod' UnaryExpr)* */ |
| 8202 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8203 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8204 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8205 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8206 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 8207 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8208 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8209 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8210 | if (options & LYXP_SKIP_EXPR) { |
| 8211 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8212 | LY_CHECK_GOTO(rc, cleanup); |
| 8213 | continue; |
| 8214 | } |
| 8215 | |
| 8216 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8217 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8218 | LY_CHECK_GOTO(rc, cleanup); |
| 8219 | |
| 8220 | /* eval */ |
| 8221 | if (options & LYXP_SCNODE_ALL) { |
| 8222 | 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] | 8223 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 8224 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8225 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8226 | 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] | 8227 | LY_CHECK_GOTO(rc, cleanup); |
| 8228 | } |
| 8229 | } |
| 8230 | |
| 8231 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8232 | lyxp_set_free_content(&orig_set); |
| 8233 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8234 | return rc; |
| 8235 | } |
| 8236 | |
| 8237 | /** |
| 8238 | * @brief Evaluate AdditiveExpr. Logs directly on error. |
| 8239 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8240 | * [17] AdditiveExpr ::= MultiplicativeExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8241 | * | AdditiveExpr '+' MultiplicativeExpr |
| 8242 | * | AdditiveExpr '-' MultiplicativeExpr |
| 8243 | * |
| 8244 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8245 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8246 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8247 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8248 | * @param[in] options XPath options. |
| 8249 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8250 | */ |
| 8251 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 8252 | eval_additive_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8253 | { |
| 8254 | LY_ERR rc; |
| 8255 | uint16_t this_op; |
| 8256 | struct lyxp_set orig_set, set2; |
| 8257 | uint16_t i; |
| 8258 | |
| 8259 | assert(repeat); |
| 8260 | |
| 8261 | set_init(&orig_set, set); |
| 8262 | set_init(&set2, set); |
| 8263 | |
| 8264 | set_fill_set(&orig_set, set); |
| 8265 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8266 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8267 | LY_CHECK_GOTO(rc, cleanup); |
| 8268 | |
| 8269 | /* ('+' / '-' MultiplicativeExpr)* */ |
| 8270 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8271 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8272 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8273 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8274 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 8275 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8276 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8277 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8278 | if (options & LYXP_SKIP_EXPR) { |
| 8279 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8280 | LY_CHECK_GOTO(rc, cleanup); |
| 8281 | continue; |
| 8282 | } |
| 8283 | |
| 8284 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8285 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8286 | LY_CHECK_GOTO(rc, cleanup); |
| 8287 | |
| 8288 | /* eval */ |
| 8289 | if (options & LYXP_SCNODE_ALL) { |
| 8290 | 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] | 8291 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 8292 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8293 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8294 | 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] | 8295 | LY_CHECK_GOTO(rc, cleanup); |
| 8296 | } |
| 8297 | } |
| 8298 | |
| 8299 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8300 | lyxp_set_free_content(&orig_set); |
| 8301 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8302 | return rc; |
| 8303 | } |
| 8304 | |
| 8305 | /** |
| 8306 | * @brief Evaluate RelationalExpr. Logs directly on error. |
| 8307 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8308 | * [16] RelationalExpr ::= AdditiveExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8309 | * | RelationalExpr '<' AdditiveExpr |
| 8310 | * | RelationalExpr '>' AdditiveExpr |
| 8311 | * | RelationalExpr '<=' AdditiveExpr |
| 8312 | * | RelationalExpr '>=' AdditiveExpr |
| 8313 | * |
| 8314 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8315 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8316 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8317 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8318 | * @param[in] options XPath options. |
| 8319 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8320 | */ |
| 8321 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 8322 | eval_relational_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8323 | { |
| 8324 | LY_ERR rc; |
| 8325 | uint16_t this_op; |
| 8326 | struct lyxp_set orig_set, set2; |
| 8327 | uint16_t i; |
| 8328 | |
| 8329 | assert(repeat); |
| 8330 | |
| 8331 | set_init(&orig_set, set); |
| 8332 | set_init(&set2, set); |
| 8333 | |
| 8334 | set_fill_set(&orig_set, set); |
| 8335 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8336 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8337 | LY_CHECK_GOTO(rc, cleanup); |
| 8338 | |
| 8339 | /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */ |
| 8340 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8341 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8342 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8343 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8344 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 8345 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8346 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8347 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8348 | if (options & LYXP_SKIP_EXPR) { |
| 8349 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8350 | LY_CHECK_GOTO(rc, cleanup); |
| 8351 | continue; |
| 8352 | } |
| 8353 | |
| 8354 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8355 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8356 | LY_CHECK_GOTO(rc, cleanup); |
| 8357 | |
| 8358 | /* eval */ |
| 8359 | if (options & LYXP_SCNODE_ALL) { |
| 8360 | 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] | 8361 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 8362 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8363 | } else { |
| 8364 | rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options); |
| 8365 | LY_CHECK_GOTO(rc, cleanup); |
| 8366 | } |
| 8367 | } |
| 8368 | |
| 8369 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8370 | lyxp_set_free_content(&orig_set); |
| 8371 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8372 | return rc; |
| 8373 | } |
| 8374 | |
| 8375 | /** |
| 8376 | * @brief Evaluate EqualityExpr. Logs directly on error. |
| 8377 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8378 | * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8379 | * | EqualityExpr '!=' RelationalExpr |
| 8380 | * |
| 8381 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8382 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8383 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8384 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8385 | * @param[in] options XPath options. |
| 8386 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8387 | */ |
| 8388 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 8389 | eval_equality_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8390 | { |
| 8391 | LY_ERR rc; |
| 8392 | uint16_t this_op; |
| 8393 | struct lyxp_set orig_set, set2; |
| 8394 | uint16_t i; |
| 8395 | |
| 8396 | assert(repeat); |
| 8397 | |
| 8398 | set_init(&orig_set, set); |
| 8399 | set_init(&set2, set); |
| 8400 | |
| 8401 | set_fill_set(&orig_set, set); |
| 8402 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8403 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8404 | LY_CHECK_GOTO(rc, cleanup); |
| 8405 | |
| 8406 | /* ('=' / '!=' RelationalExpr)* */ |
| 8407 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8408 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8409 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8410 | 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] | 8411 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 8412 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8413 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8414 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8415 | if (options & LYXP_SKIP_EXPR) { |
| 8416 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8417 | LY_CHECK_GOTO(rc, cleanup); |
| 8418 | continue; |
| 8419 | } |
| 8420 | |
| 8421 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8422 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8423 | LY_CHECK_GOTO(rc, cleanup); |
| 8424 | |
| 8425 | /* eval */ |
| 8426 | if (options & LYXP_SCNODE_ALL) { |
| 8427 | 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] | 8428 | warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1); |
| 8429 | 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] | 8430 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 8431 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8432 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8433 | rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options); |
| 8434 | LY_CHECK_GOTO(rc, cleanup); |
| 8435 | } |
| 8436 | } |
| 8437 | |
| 8438 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8439 | lyxp_set_free_content(&orig_set); |
| 8440 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8441 | return rc; |
| 8442 | } |
| 8443 | |
| 8444 | /** |
| 8445 | * @brief Evaluate AndExpr. Logs directly on error. |
| 8446 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8447 | * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8448 | * |
| 8449 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8450 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8451 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8452 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8453 | * @param[in] options XPath options. |
| 8454 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8455 | */ |
| 8456 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 8457 | eval_and_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8458 | { |
| 8459 | LY_ERR rc; |
| 8460 | struct lyxp_set orig_set, set2; |
| 8461 | uint16_t i; |
| 8462 | |
| 8463 | assert(repeat); |
| 8464 | |
| 8465 | set_init(&orig_set, set); |
| 8466 | set_init(&set2, set); |
| 8467 | |
| 8468 | set_fill_set(&orig_set, set); |
| 8469 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8470 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8471 | LY_CHECK_GOTO(rc, cleanup); |
| 8472 | |
| 8473 | /* cast to boolean, we know that will be the final result */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8474 | if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 8475 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8476 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8477 | lyxp_set_cast(set, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8478 | } |
| 8479 | |
| 8480 | /* ('and' EqualityExpr)* */ |
| 8481 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8482 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8483 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, ((options & LYXP_SKIP_EXPR) || !set->val.bln ? "skipped" : "parsed"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 8484 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8485 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8486 | |
| 8487 | /* lazy evaluation */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8488 | if ((options & LYXP_SKIP_EXPR) || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) { |
| 8489 | 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] | 8490 | LY_CHECK_GOTO(rc, cleanup); |
| 8491 | continue; |
| 8492 | } |
| 8493 | |
| 8494 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8495 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8496 | LY_CHECK_GOTO(rc, cleanup); |
| 8497 | |
| 8498 | /* eval - just get boolean value actually */ |
| 8499 | if (set->type == LYXP_SET_SCNODE_SET) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 8500 | set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 8501 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8502 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8503 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8504 | set_fill_set(set, &set2); |
| 8505 | } |
| 8506 | } |
| 8507 | |
| 8508 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8509 | lyxp_set_free_content(&orig_set); |
| 8510 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8511 | return rc; |
| 8512 | } |
| 8513 | |
| 8514 | /** |
| 8515 | * @brief Evaluate OrExpr. Logs directly on error. |
| 8516 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8517 | * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8518 | * |
| 8519 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8520 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8521 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8522 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8523 | * @param[in] options XPath options. |
| 8524 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8525 | */ |
| 8526 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 8527 | eval_or_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8528 | { |
| 8529 | LY_ERR rc; |
| 8530 | struct lyxp_set orig_set, set2; |
| 8531 | uint16_t i; |
| 8532 | |
| 8533 | assert(repeat); |
| 8534 | |
| 8535 | set_init(&orig_set, set); |
| 8536 | set_init(&set2, set); |
| 8537 | |
| 8538 | set_fill_set(&orig_set, set); |
| 8539 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8540 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8541 | LY_CHECK_GOTO(rc, cleanup); |
| 8542 | |
| 8543 | /* cast to boolean, we know that will be the final result */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8544 | if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 8545 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8546 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8547 | lyxp_set_cast(set, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8548 | } |
| 8549 | |
| 8550 | /* ('or' AndExpr)* */ |
| 8551 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8552 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8553 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, ((options & LYXP_SKIP_EXPR) || set->val.bln ? "skipped" : "parsed"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 8554 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8555 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8556 | |
| 8557 | /* lazy evaluation */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8558 | if ((options & LYXP_SKIP_EXPR) || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) { |
| 8559 | 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] | 8560 | LY_CHECK_GOTO(rc, cleanup); |
| 8561 | continue; |
| 8562 | } |
| 8563 | |
| 8564 | set_fill_set(&set2, &orig_set); |
| 8565 | /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one), |
| 8566 | * but it does not matter */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8567 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8568 | LY_CHECK_GOTO(rc, cleanup); |
| 8569 | |
| 8570 | /* eval - just get boolean value actually */ |
| 8571 | if (set->type == LYXP_SET_SCNODE_SET) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 8572 | set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 8573 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8574 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8575 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8576 | set_fill_set(set, &set2); |
| 8577 | } |
| 8578 | } |
| 8579 | |
| 8580 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8581 | lyxp_set_free_content(&orig_set); |
| 8582 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8583 | return rc; |
| 8584 | } |
| 8585 | |
| 8586 | /** |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8587 | * @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] | 8588 | * |
| 8589 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8590 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8591 | * @param[in] etype Expression type to evaluate. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8592 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8593 | * @param[in] options XPath options. |
| 8594 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8595 | */ |
| 8596 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 8597 | eval_expr_select(const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype, struct lyxp_set *set, |
| 8598 | uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8599 | { |
| 8600 | uint16_t i, count; |
| 8601 | enum lyxp_expr_type next_etype; |
| 8602 | LY_ERR rc; |
| 8603 | |
| 8604 | /* process operator repeats */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8605 | if (!exp->repeat[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8606 | next_etype = LYXP_EXPR_NONE; |
| 8607 | } else { |
| 8608 | /* find etype repeat */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 8609 | for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i) {} |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8610 | |
| 8611 | /* select one-priority lower because etype expression called us */ |
| 8612 | if (i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8613 | next_etype = exp->repeat[*tok_idx][i - 1]; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8614 | /* count repeats for that expression */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 8615 | 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] | 8616 | } else { |
| 8617 | next_etype = LYXP_EXPR_NONE; |
| 8618 | } |
| 8619 | } |
| 8620 | |
| 8621 | /* decide what expression are we parsing based on the repeat */ |
| 8622 | switch (next_etype) { |
| 8623 | case LYXP_EXPR_OR: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8624 | rc = eval_or_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8625 | break; |
| 8626 | case LYXP_EXPR_AND: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8627 | rc = eval_and_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8628 | break; |
| 8629 | case LYXP_EXPR_EQUALITY: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8630 | rc = eval_equality_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8631 | break; |
| 8632 | case LYXP_EXPR_RELATIONAL: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8633 | rc = eval_relational_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8634 | break; |
| 8635 | case LYXP_EXPR_ADDITIVE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8636 | rc = eval_additive_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8637 | break; |
| 8638 | case LYXP_EXPR_MULTIPLICATIVE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8639 | rc = eval_multiplicative_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8640 | break; |
| 8641 | case LYXP_EXPR_UNARY: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8642 | rc = eval_unary_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8643 | break; |
| 8644 | case LYXP_EXPR_UNION: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8645 | rc = eval_union_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8646 | break; |
| 8647 | case LYXP_EXPR_NONE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8648 | rc = eval_path_expr(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8649 | break; |
| 8650 | default: |
| 8651 | LOGINT_RET(set->ctx); |
| 8652 | } |
| 8653 | |
| 8654 | return rc; |
| 8655 | } |
| 8656 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8657 | /** |
| 8658 | * @brief Get root type. |
| 8659 | * |
| 8660 | * @param[in] ctx_node Context node. |
| 8661 | * @param[in] ctx_scnode Schema context node. |
| 8662 | * @param[in] options XPath options. |
| 8663 | * @return Root type. |
| 8664 | */ |
| 8665 | static enum lyxp_node_type |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 8666 | 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] | 8667 | { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 8668 | const struct lysc_node *op; |
| 8669 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8670 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8671 | /* schema */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 8672 | 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] | 8673 | |
| 8674 | if (op || (options & LYXP_SCNODE)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8675 | /* general root that can access everything */ |
| 8676 | return LYXP_NODE_ROOT; |
| 8677 | } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) { |
| 8678 | /* root context node can access only config data (because we said so, it is unspecified) */ |
| 8679 | return LYXP_NODE_ROOT_CONFIG; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8680 | } |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 8681 | return LYXP_NODE_ROOT; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8682 | } |
| 8683 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8684 | /* data */ |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 8685 | op = ctx_node ? ctx_node->schema : NULL; |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 8686 | 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] | 8687 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8688 | if (op || !(options & LYXP_SCHEMA)) { |
| 8689 | /* general root that can access everything */ |
| 8690 | return LYXP_NODE_ROOT; |
Christian Hopps | b6ecaea | 2021-02-06 09:45:38 -0500 | [diff] [blame] | 8691 | } 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] | 8692 | /* root context node can access only config data (because we said so, it is unspecified) */ |
| 8693 | return LYXP_NODE_ROOT_CONFIG; |
| 8694 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8695 | return LYXP_NODE_ROOT; |
| 8696 | } |
| 8697 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8698 | LY_ERR |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 8699 | lyxp_eval(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod, |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 8700 | LY_VALUE_FORMAT format, void *prefix_data, const struct lyd_node *ctx_node, const struct lyd_node *tree, |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8701 | const struct lyxp_var *vars, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8702 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8703 | uint16_t tok_idx = 0; |
Michal Vasko | aac267d | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 8704 | const struct lysc_node *snode; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8705 | LY_ERR rc; |
| 8706 | |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 8707 | LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL); |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 8708 | if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) { |
Michal Vasko | aac267d | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 8709 | 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] | 8710 | return LY_EINVAL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8711 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8712 | |
Michal Vasko | d3bb12f | 2020-12-04 14:33:09 +0100 | [diff] [blame] | 8713 | if (tree) { |
| 8714 | /* adjust the pointer to be the first top-level sibling */ |
| 8715 | while (tree->parent) { |
| 8716 | tree = lyd_parent(tree); |
| 8717 | } |
| 8718 | tree = lyd_first_sibling(tree); |
Michal Vasko | aac267d | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 8719 | |
| 8720 | for (snode = tree->schema->parent; snode && (snode->nodetype & (LYS_CASE | LYS_CHOICE)); snode = snode->parent) {} |
| 8721 | if (snode) { |
| 8722 | /* unable to evaluate absolute paths */ |
| 8723 | LOGERR(ctx, LY_EINVAL, "Data node \"%s\" has no parent but is not instance of a top-level schema node.", |
| 8724 | LYD_NAME(tree)); |
| 8725 | return LY_EINVAL; |
| 8726 | } |
Michal Vasko | d3bb12f | 2020-12-04 14:33:09 +0100 | [diff] [blame] | 8727 | } |
| 8728 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8729 | /* prepare set for evaluation */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8730 | memset(set, 0, sizeof *set); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8731 | set->type = LYXP_SET_NODE_SET; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8732 | set->root_type = lyxp_get_root_type(ctx_node, NULL, options); |
| 8733 | set_insert_node(set, (struct lyd_node *)ctx_node, 0, ctx_node ? LYXP_NODE_ELEM : set->root_type, 0); |
| 8734 | |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 8735 | set->ctx = (struct ly_ctx *)ctx; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8736 | set->cur_node = ctx_node; |
| 8737 | for (set->context_op = ctx_node ? ctx_node->schema : NULL; |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 8738 | set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); |
| 8739 | set->context_op = set->context_op->parent) {} |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 8740 | set->tree = tree; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8741 | set->cur_mod = cur_mod; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8742 | set->format = format; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8743 | set->prefix_data = prefix_data; |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8744 | set->vars = vars; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8745 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 8746 | LOG_LOCSET(NULL, set->cur_node, NULL, NULL); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 8747 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8748 | /* evaluate */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8749 | rc = eval_expr_select(exp, &tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8750 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8751 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8752 | } |
| 8753 | |
Michal Vasko | 0245d50 | 2021-12-13 17:05:06 +0100 | [diff] [blame] | 8754 | if (set->cur_node) { |
| 8755 | LOG_LOCBACK(0, 1, 0, 0); |
| 8756 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8757 | return rc; |
| 8758 | } |
| 8759 | |
| 8760 | #if 0 |
| 8761 | |
| 8762 | /* full xml printing of set elements, not used currently */ |
| 8763 | |
| 8764 | void |
| 8765 | lyxp_set_print_xml(FILE *f, struct lyxp_set *set) |
| 8766 | { |
| 8767 | uint32_t i; |
| 8768 | char *str_num; |
| 8769 | struct lyout out; |
| 8770 | |
| 8771 | memset(&out, 0, sizeof out); |
| 8772 | |
| 8773 | out.type = LYOUT_STREAM; |
| 8774 | out.method.f = f; |
| 8775 | |
| 8776 | switch (set->type) { |
| 8777 | case LYXP_SET_EMPTY: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8778 | ly_print_(&out, "Empty XPath set\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8779 | break; |
| 8780 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8781 | ly_print_(&out, "Boolean XPath set:\n"); |
| 8782 | ly_print_(&out, "%s\n\n", set->value.bool ? "true" : "false"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8783 | break; |
| 8784 | case LYXP_SET_STRING: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8785 | ly_print_(&out, "String XPath set:\n"); |
| 8786 | ly_print_(&out, "\"%s\"\n\n", set->value.str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8787 | break; |
| 8788 | case LYXP_SET_NUMBER: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8789 | ly_print_(&out, "Number XPath set:\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8790 | |
| 8791 | if (isnan(set->value.num)) { |
| 8792 | str_num = strdup("NaN"); |
| 8793 | } else if ((set->value.num == 0) || (set->value.num == -0.0f)) { |
| 8794 | str_num = strdup("0"); |
| 8795 | } else if (isinf(set->value.num) && !signbit(set->value.num)) { |
| 8796 | str_num = strdup("Infinity"); |
| 8797 | } else if (isinf(set->value.num) && signbit(set->value.num)) { |
| 8798 | str_num = strdup("-Infinity"); |
| 8799 | } else if ((long long)set->value.num == set->value.num) { |
| 8800 | if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) { |
| 8801 | str_num = NULL; |
| 8802 | } |
| 8803 | } else { |
| 8804 | if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) { |
| 8805 | str_num = NULL; |
| 8806 | } |
| 8807 | } |
| 8808 | if (!str_num) { |
| 8809 | LOGMEM; |
| 8810 | return; |
| 8811 | } |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8812 | ly_print_(&out, "%s\n\n", str_num); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8813 | free(str_num); |
| 8814 | break; |
| 8815 | case LYXP_SET_NODE_SET: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8816 | ly_print_(&out, "Node XPath set:\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8817 | |
| 8818 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8819 | ly_print_(&out, "%d. ", i + 1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8820 | switch (set->node_type[i]) { |
| 8821 | case LYXP_NODE_ROOT_ALL: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8822 | ly_print_(&out, "ROOT all\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8823 | break; |
| 8824 | case LYXP_NODE_ROOT_CONFIG: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8825 | ly_print_(&out, "ROOT config\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8826 | break; |
| 8827 | case LYXP_NODE_ROOT_STATE: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8828 | ly_print_(&out, "ROOT state\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8829 | break; |
| 8830 | case LYXP_NODE_ROOT_NOTIF: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8831 | 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] | 8832 | break; |
| 8833 | case LYXP_NODE_ROOT_RPC: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8834 | 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] | 8835 | break; |
| 8836 | case LYXP_NODE_ROOT_OUTPUT: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8837 | 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] | 8838 | break; |
| 8839 | case LYXP_NODE_ELEM: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8840 | ly_print_(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8841 | xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT); |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8842 | ly_print_(&out, "\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8843 | break; |
| 8844 | case LYXP_NODE_TEXT: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8845 | 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] | 8846 | break; |
| 8847 | case LYXP_NODE_ATTR: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8848 | 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] | 8849 | break; |
| 8850 | } |
| 8851 | } |
| 8852 | break; |
| 8853 | } |
| 8854 | } |
| 8855 | |
| 8856 | #endif |
| 8857 | |
| 8858 | LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8859 | lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8860 | { |
| 8861 | long double num; |
| 8862 | char *str; |
| 8863 | LY_ERR rc; |
| 8864 | |
| 8865 | if (!set || (set->type == target)) { |
| 8866 | return LY_SUCCESS; |
| 8867 | } |
| 8868 | |
| 8869 | /* it's not possible to convert anything into a node set */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8870 | assert(target != LYXP_SET_NODE_SET); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8871 | |
| 8872 | if (set->type == LYXP_SET_SCNODE_SET) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8873 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8874 | return LY_EINVAL; |
| 8875 | } |
| 8876 | |
| 8877 | /* to STRING */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8878 | 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] | 8879 | switch (set->type) { |
| 8880 | case LYXP_SET_NUMBER: |
| 8881 | if (isnan(set->val.num)) { |
| 8882 | set->val.str = strdup("NaN"); |
| 8883 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 8884 | } else if ((set->val.num == 0) || (set->val.num == -0.0f)) { |
| 8885 | set->val.str = strdup("0"); |
| 8886 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 8887 | } else if (isinf(set->val.num) && !signbit(set->val.num)) { |
| 8888 | set->val.str = strdup("Infinity"); |
| 8889 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 8890 | } else if (isinf(set->val.num) && signbit(set->val.num)) { |
| 8891 | set->val.str = strdup("-Infinity"); |
| 8892 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 8893 | } else if ((long long)set->val.num == set->val.num) { |
| 8894 | if (asprintf(&str, "%lld", (long long)set->val.num) == -1) { |
| 8895 | LOGMEM_RET(set->ctx); |
| 8896 | } |
| 8897 | set->val.str = str; |
| 8898 | } else { |
| 8899 | if (asprintf(&str, "%03.1Lf", set->val.num) == -1) { |
| 8900 | LOGMEM_RET(set->ctx); |
| 8901 | } |
| 8902 | set->val.str = str; |
| 8903 | } |
| 8904 | break; |
| 8905 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8906 | if (set->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8907 | set->val.str = strdup("true"); |
| 8908 | } else { |
| 8909 | set->val.str = strdup("false"); |
| 8910 | } |
| 8911 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM); |
| 8912 | break; |
| 8913 | case LYXP_SET_NODE_SET: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8914 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8915 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8916 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8917 | rc = cast_node_set_to_string(set, &str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8918 | LY_CHECK_RET(rc); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8919 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8920 | set->val.str = str; |
| 8921 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8922 | default: |
| 8923 | LOGINT_RET(set->ctx); |
| 8924 | } |
| 8925 | set->type = LYXP_SET_STRING; |
| 8926 | } |
| 8927 | |
| 8928 | /* to NUMBER */ |
| 8929 | if (target == LYXP_SET_NUMBER) { |
| 8930 | switch (set->type) { |
| 8931 | case LYXP_SET_STRING: |
| 8932 | num = cast_string_to_number(set->val.str); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8933 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8934 | set->val.num = num; |
| 8935 | break; |
| 8936 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8937 | if (set->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8938 | set->val.num = 1; |
| 8939 | } else { |
| 8940 | set->val.num = 0; |
| 8941 | } |
| 8942 | break; |
| 8943 | default: |
| 8944 | LOGINT_RET(set->ctx); |
| 8945 | } |
| 8946 | set->type = LYXP_SET_NUMBER; |
| 8947 | } |
| 8948 | |
| 8949 | /* to BOOLEAN */ |
| 8950 | if (target == LYXP_SET_BOOLEAN) { |
| 8951 | switch (set->type) { |
| 8952 | case LYXP_SET_NUMBER: |
| 8953 | 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] | 8954 | set->val.bln = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8955 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8956 | set->val.bln = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8957 | } |
| 8958 | break; |
| 8959 | case LYXP_SET_STRING: |
| 8960 | if (set->val.str[0]) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8961 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8962 | set->val.bln = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8963 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8964 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8965 | set->val.bln = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8966 | } |
| 8967 | break; |
| 8968 | case LYXP_SET_NODE_SET: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8969 | if (set->used) { |
| 8970 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8971 | set->val.bln = 1; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8972 | } else { |
| 8973 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8974 | set->val.bln = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8975 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8976 | break; |
| 8977 | default: |
| 8978 | LOGINT_RET(set->ctx); |
| 8979 | } |
| 8980 | set->type = LYXP_SET_BOOLEAN; |
| 8981 | } |
| 8982 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8983 | return LY_SUCCESS; |
| 8984 | } |
| 8985 | |
| 8986 | LY_ERR |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 8987 | lyxp_atomize(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod, |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 8988 | LY_VALUE_FORMAT format, void *prefix_data, const struct lysc_node *ctx_scnode, struct lyxp_set *set, |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 8989 | uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8990 | { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 8991 | LY_ERR ret; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8992 | uint16_t tok_idx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8993 | |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 8994 | LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL); |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 8995 | if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8996 | LOGARG(NULL, "Current module must be set if schema format is used."); |
| 8997 | return LY_EINVAL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8998 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8999 | |
| 9000 | /* prepare set for evaluation */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9001 | memset(set, 0, sizeof *set); |
| 9002 | set->type = LYXP_SET_SCNODE_SET; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9003 | set->root_type = lyxp_get_root_type(NULL, ctx_scnode, options); |
| 9004 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, ctx_scnode, ctx_scnode ? LYXP_NODE_ELEM : set->root_type, NULL)); |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 9005 | set->val.scnodes[0].in_ctx = LYXP_SET_SCNODE_START; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9006 | |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 9007 | set->ctx = (struct ly_ctx *)ctx; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9008 | set->cur_scnode = ctx_scnode; |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 9009 | for (set->context_op = ctx_scnode; |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 9010 | set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); |
| 9011 | set->context_op = set->context_op->parent) {} |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9012 | set->cur_mod = cur_mod; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9013 | set->format = format; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9014 | set->prefix_data = prefix_data; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9015 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 9016 | LOG_LOCSET(set->cur_scnode, NULL, NULL, NULL); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 9017 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9018 | /* evaluate */ |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 9019 | ret = eval_expr_select(exp, &tok_idx, 0, set, options); |
| 9020 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 9021 | LOG_LOCBACK(1, 0, 0, 0); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 9022 | return ret; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9023 | } |
Michal Vasko | d43d71a | 2020-08-07 14:54:58 +0200 | [diff] [blame] | 9024 | |
Jan Kundrát | c6e39de | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 9025 | LIBYANG_API_DEF const char * |
Michal Vasko | d43d71a | 2020-08-07 14:54:58 +0200 | [diff] [blame] | 9026 | lyxp_get_expr(const struct lyxp_expr *path) |
| 9027 | { |
| 9028 | if (!path) { |
| 9029 | return NULL; |
| 9030 | } |
| 9031 | |
| 9032 | return path->expr; |
| 9033 | } |