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 |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 346 | cast_string_recursive(const struct lyd_node *node, ly_bool fake_cont, enum lyxp_node_type root_type, uint16_t indent, char **str, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 347 | 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 | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 418 | if (lyd_parse_data_mem((struct ly_ctx *)LYD_CTX(node), any->value.mem, LYD_LYB, LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, &tree) == LY_SUCCESS) { |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 419 | /* successfully parsed */ |
| 420 | free(any->value.mem); |
| 421 | any->value.tree = tree; |
| 422 | any->value_type = LYD_ANYDATA_DATATREE; |
| 423 | } |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 424 | /* 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] | 425 | } |
| 426 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 427 | switch (any->value_type) { |
| 428 | case LYD_ANYDATA_STRING: |
| 429 | case LYD_ANYDATA_XML: |
| 430 | case LYD_ANYDATA_JSON: |
| 431 | buf = strdup(any->value.json); |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 432 | LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_CTX(node)), LY_EMEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 433 | break; |
| 434 | case LYD_ANYDATA_DATATREE: |
Radek Krejci | 84ce7b1 | 2020-06-11 17:28:25 +0200 | [diff] [blame] | 435 | LY_CHECK_RET(ly_out_new_memory(&buf, 0, &out)); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 436 | rc = lyd_print_all(out, any->value.tree, LYD_XML, 0); |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 437 | ly_out_free(out, NULL, 0); |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 438 | LY_CHECK_RET(rc < 0, -rc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 439 | break; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 440 | case LYD_ANYDATA_LYB: |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 441 | LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot convert LYB anydata into string."); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 442 | return LY_EINVAL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 443 | } |
| 444 | } |
| 445 | |
| 446 | line = strtok_r(buf, "\n", &ptr); |
| 447 | do { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 448 | 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] | 449 | if (rc != LY_SUCCESS) { |
| 450 | free(buf); |
| 451 | return rc; |
| 452 | } |
| 453 | memset(*str + (*used - 1), ' ', indent * 2); |
| 454 | *used += indent * 2; |
| 455 | |
| 456 | strcpy(*str + (*used - 1), line); |
| 457 | *used += strlen(line); |
| 458 | |
| 459 | strcpy(*str + (*used - 1), "\n"); |
| 460 | *used += 1; |
| 461 | } while ((line = strtok_r(NULL, "\n", &ptr))); |
| 462 | |
| 463 | free(buf); |
| 464 | break; |
| 465 | |
| 466 | default: |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 467 | LOGINT_RET(LYD_CTX(node)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | if (fake_cont) { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 471 | rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 472 | LY_CHECK_RET(rc); |
| 473 | strcpy(*str + (*used - 1), "\n"); |
| 474 | ++(*used); |
| 475 | |
| 476 | --indent; |
| 477 | } |
| 478 | |
| 479 | return LY_SUCCESS; |
| 480 | } |
| 481 | |
| 482 | /** |
| 483 | * @brief Cast an element into a string. |
| 484 | * |
| 485 | * @param[in] node Node to cast. |
| 486 | * @param[in] fake_cont Whether to put the data into a "fake" container. |
| 487 | * @param[in] root_type Type of the XPath root. |
| 488 | * @param[out] str Element cast to dynamically-allocated string. |
| 489 | * @return LY_ERR |
| 490 | */ |
| 491 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 492 | 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] | 493 | { |
| 494 | uint16_t used, size; |
| 495 | LY_ERR rc; |
| 496 | |
| 497 | *str = malloc(LYXP_STRING_CAST_SIZE_START * sizeof(char)); |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 498 | LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_CTX(node)), LY_EMEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 499 | (*str)[0] = '\0'; |
| 500 | used = 1; |
| 501 | size = LYXP_STRING_CAST_SIZE_START; |
| 502 | |
| 503 | rc = cast_string_recursive(node, fake_cont, root_type, 0, str, &used, &size); |
| 504 | if (rc != LY_SUCCESS) { |
| 505 | free(*str); |
| 506 | return rc; |
| 507 | } |
| 508 | |
| 509 | if (size > used) { |
| 510 | *str = ly_realloc(*str, used * sizeof(char)); |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 511 | LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_CTX(node)), LY_EMEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 512 | } |
| 513 | return LY_SUCCESS; |
| 514 | } |
| 515 | |
| 516 | /** |
| 517 | * @brief Cast a LYXP_SET_NODE_SET set into a string. |
| 518 | * Context position aware. |
| 519 | * |
| 520 | * @param[in] set Set to cast. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 521 | * @param[out] str Cast dynamically-allocated string. |
| 522 | * @return LY_ERR |
| 523 | */ |
| 524 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 525 | cast_node_set_to_string(struct lyxp_set *set, char **str) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 526 | { |
Michal Vasko | aa67706 | 2021-03-09 13:52:53 +0100 | [diff] [blame] | 527 | if (!set->used) { |
| 528 | *str = strdup(""); |
| 529 | if (!*str) { |
| 530 | LOGMEM_RET(set->ctx); |
| 531 | } |
| 532 | return LY_SUCCESS; |
| 533 | } |
| 534 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 535 | switch (set->val.nodes[0].type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 536 | case LYXP_NODE_NONE: |
| 537 | /* invalid */ |
| 538 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 539 | case LYXP_NODE_ROOT: |
| 540 | case LYXP_NODE_ROOT_CONFIG: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 541 | 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] | 542 | case LYXP_NODE_ELEM: |
| 543 | case LYXP_NODE_TEXT: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 544 | 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] | 545 | case LYXP_NODE_META: |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 546 | *str = strdup(lyd_get_meta_value(set->val.meta[0].meta)); |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 547 | if (!*str) { |
| 548 | LOGMEM_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 549 | } |
| 550 | return LY_SUCCESS; |
| 551 | } |
| 552 | |
| 553 | LOGINT_RET(set->ctx); |
| 554 | } |
| 555 | |
| 556 | /** |
| 557 | * @brief Cast a string into an XPath number. |
| 558 | * |
| 559 | * @param[in] str String to use. |
| 560 | * @return Cast number. |
| 561 | */ |
| 562 | static long double |
| 563 | cast_string_to_number(const char *str) |
| 564 | { |
| 565 | long double num; |
| 566 | char *ptr; |
| 567 | |
| 568 | errno = 0; |
| 569 | num = strtold(str, &ptr); |
| 570 | if (errno || *ptr) { |
| 571 | num = NAN; |
| 572 | } |
| 573 | return num; |
| 574 | } |
| 575 | |
| 576 | /** |
| 577 | * @brief Callback for checking value equality. |
| 578 | * |
Michal Vasko | 62524a9 | 2021-02-26 10:08:50 +0100 | [diff] [blame] | 579 | * Implementation of ::lyht_value_equal_cb. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 580 | * |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 581 | * @param[in] val1_p First value. |
| 582 | * @param[in] val2_p Second value. |
| 583 | * @param[in] mod Whether hash table is being modified. |
| 584 | * @param[in] cb_data Callback data. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 585 | * @return Boolean value whether values are equal or not. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 586 | */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 587 | static ly_bool |
| 588 | 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] | 589 | { |
| 590 | struct lyxp_set_hash_node *val1, *val2; |
| 591 | |
| 592 | val1 = (struct lyxp_set_hash_node *)val1_p; |
| 593 | val2 = (struct lyxp_set_hash_node *)val2_p; |
| 594 | |
| 595 | if ((val1->node == val2->node) && (val1->type == val2->type)) { |
| 596 | return 1; |
| 597 | } |
| 598 | |
| 599 | return 0; |
| 600 | } |
| 601 | |
| 602 | /** |
| 603 | * @brief Insert node and its hash into set. |
| 604 | * |
| 605 | * @param[in] set et to insert to. |
| 606 | * @param[in] node Node with hash. |
| 607 | * @param[in] type Node type. |
| 608 | */ |
| 609 | static void |
| 610 | set_insert_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type) |
| 611 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 612 | LY_ERR r; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 613 | uint32_t i, hash; |
| 614 | struct lyxp_set_hash_node hnode; |
| 615 | |
| 616 | if (!set->ht && (set->used >= LYD_HT_MIN_ITEMS)) { |
| 617 | /* create hash table and add all the nodes */ |
| 618 | set->ht = lyht_new(1, sizeof(struct lyxp_set_hash_node), set_values_equal_cb, NULL, 1); |
| 619 | for (i = 0; i < set->used; ++i) { |
| 620 | hnode.node = set->val.nodes[i].node; |
| 621 | hnode.type = set->val.nodes[i].type; |
| 622 | |
| 623 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 624 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 625 | hash = dict_hash_multi(hash, NULL, 0); |
| 626 | |
| 627 | r = lyht_insert(set->ht, &hnode, hash, NULL); |
| 628 | assert(!r); |
| 629 | (void)r; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 630 | |
Michal Vasko | 9d6befd | 2019-12-11 14:56:56 +0100 | [diff] [blame] | 631 | if (hnode.node == node) { |
| 632 | /* it was just added, do not add it twice */ |
| 633 | node = NULL; |
| 634 | } |
| 635 | } |
| 636 | } |
| 637 | |
| 638 | if (set->ht && node) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 639 | /* add the new node into hash table */ |
| 640 | hnode.node = node; |
| 641 | hnode.type = type; |
| 642 | |
| 643 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 644 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 645 | hash = dict_hash_multi(hash, NULL, 0); |
| 646 | |
| 647 | r = lyht_insert(set->ht, &hnode, hash, NULL); |
| 648 | assert(!r); |
| 649 | (void)r; |
| 650 | } |
| 651 | } |
| 652 | |
| 653 | /** |
| 654 | * @brief Remove node and its hash from set. |
| 655 | * |
| 656 | * @param[in] set Set to remove from. |
| 657 | * @param[in] node Node to remove. |
| 658 | * @param[in] type Node type. |
| 659 | */ |
| 660 | static void |
| 661 | set_remove_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type) |
| 662 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 663 | LY_ERR r; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 664 | struct lyxp_set_hash_node hnode; |
| 665 | uint32_t hash; |
| 666 | |
| 667 | if (set->ht) { |
| 668 | hnode.node = node; |
| 669 | hnode.type = type; |
| 670 | |
| 671 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 672 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 673 | hash = dict_hash_multi(hash, NULL, 0); |
| 674 | |
| 675 | r = lyht_remove(set->ht, &hnode, hash); |
| 676 | assert(!r); |
| 677 | (void)r; |
| 678 | |
| 679 | if (!set->ht->used) { |
| 680 | lyht_free(set->ht); |
| 681 | set->ht = NULL; |
| 682 | } |
| 683 | } |
| 684 | } |
| 685 | |
| 686 | /** |
| 687 | * @brief Check whether node is in set based on its hash. |
| 688 | * |
| 689 | * @param[in] set Set to search in. |
| 690 | * @param[in] node Node to search for. |
| 691 | * @param[in] type Node type. |
| 692 | * @param[in] skip_idx Index in @p set to skip. |
| 693 | * @return LY_ERR |
| 694 | */ |
| 695 | static LY_ERR |
| 696 | set_dup_node_hash_check(const struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type, int skip_idx) |
| 697 | { |
| 698 | struct lyxp_set_hash_node hnode, *match_p; |
| 699 | uint32_t hash; |
| 700 | |
| 701 | hnode.node = node; |
| 702 | hnode.type = type; |
| 703 | |
| 704 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 705 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 706 | hash = dict_hash_multi(hash, NULL, 0); |
| 707 | |
| 708 | if (!lyht_find(set->ht, &hnode, hash, (void **)&match_p)) { |
| 709 | if ((skip_idx > -1) && (set->val.nodes[skip_idx].node == match_p->node) && (set->val.nodes[skip_idx].type == match_p->type)) { |
| 710 | /* we found it on the index that should be skipped, find another */ |
| 711 | hnode = *match_p; |
| 712 | if (lyht_find_next(set->ht, &hnode, hash, (void **)&match_p)) { |
| 713 | /* none other found */ |
| 714 | return LY_SUCCESS; |
| 715 | } |
| 716 | } |
| 717 | |
| 718 | return LY_EEXIST; |
| 719 | } |
| 720 | |
| 721 | /* not found */ |
| 722 | return LY_SUCCESS; |
| 723 | } |
| 724 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 725 | void |
| 726 | lyxp_set_free_content(struct lyxp_set *set) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 727 | { |
| 728 | if (!set) { |
| 729 | return; |
| 730 | } |
| 731 | |
| 732 | if (set->type == LYXP_SET_NODE_SET) { |
| 733 | free(set->val.nodes); |
| 734 | lyht_free(set->ht); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 735 | } else if (set->type == LYXP_SET_SCNODE_SET) { |
| 736 | free(set->val.scnodes); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 737 | lyht_free(set->ht); |
| 738 | } else { |
| 739 | if (set->type == LYXP_SET_STRING) { |
| 740 | free(set->val.str); |
| 741 | } |
| 742 | set->type = LYXP_SET_NODE_SET; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 743 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 744 | |
| 745 | set->val.nodes = NULL; |
| 746 | set->used = 0; |
| 747 | set->size = 0; |
| 748 | set->ht = NULL; |
| 749 | set->ctx_pos = 0; |
aPiecek | 748da73 | 2021-06-01 09:56:43 +0200 | [diff] [blame] | 750 | set->ctx_size = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 751 | } |
| 752 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 753 | /** |
| 754 | * @brief Free dynamically-allocated set. |
| 755 | * |
| 756 | * @param[in] set Set to free. |
| 757 | */ |
| 758 | static void |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 759 | lyxp_set_free(struct lyxp_set *set) |
| 760 | { |
| 761 | if (!set) { |
| 762 | return; |
| 763 | } |
| 764 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 765 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 766 | free(set); |
| 767 | } |
| 768 | |
| 769 | /** |
| 770 | * @brief Initialize set context. |
| 771 | * |
| 772 | * @param[in] new Set to initialize. |
| 773 | * @param[in] set Arbitrary initialized set. |
| 774 | */ |
| 775 | static void |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 776 | set_init(struct lyxp_set *new, const struct lyxp_set *set) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 777 | { |
| 778 | memset(new, 0, sizeof *new); |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 779 | if (set) { |
| 780 | new->ctx = set->ctx; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 781 | new->cur_node = set->cur_node; |
Michal Vasko | 588112f | 2019-12-10 14:51:53 +0100 | [diff] [blame] | 782 | new->root_type = set->root_type; |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 783 | new->context_op = set->context_op; |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 784 | new->tree = set->tree; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 785 | new->cur_mod = set->cur_mod; |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 786 | new->format = set->format; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 787 | new->prefix_data = set->prefix_data; |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 788 | new->vars = set->vars; |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 789 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 790 | } |
| 791 | |
| 792 | /** |
| 793 | * @brief Create a deep copy of a set. |
| 794 | * |
| 795 | * @param[in] set Set to copy. |
| 796 | * @return Copy of @p set. |
| 797 | */ |
| 798 | static struct lyxp_set * |
| 799 | set_copy(struct lyxp_set *set) |
| 800 | { |
| 801 | struct lyxp_set *ret; |
Michal Vasko | 1fdd8fa | 2021-01-08 09:21:45 +0100 | [diff] [blame] | 802 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 803 | |
| 804 | if (!set) { |
| 805 | return NULL; |
| 806 | } |
| 807 | |
| 808 | ret = malloc(sizeof *ret); |
| 809 | LY_CHECK_ERR_RET(!ret, LOGMEM(set->ctx), NULL); |
| 810 | set_init(ret, set); |
| 811 | |
| 812 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 813 | ret->type = set->type; |
| 814 | |
| 815 | for (i = 0; i < set->used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 816 | if ((set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) || |
| 817 | (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START)) { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 818 | uint32_t idx; |
| 819 | LY_CHECK_ERR_RET(lyxp_set_scnode_insert_node(ret, set->val.scnodes[i].scnode, set->val.scnodes[i].type, &idx), |
| 820 | lyxp_set_free(ret), NULL); |
Michal Vasko | 3f27c52 | 2020-01-06 08:37:49 +0100 | [diff] [blame] | 821 | /* coverity seems to think scnodes can be NULL */ |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 822 | if (!ret->val.scnodes) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 823 | lyxp_set_free(ret); |
| 824 | return NULL; |
| 825 | } |
Michal Vasko | ba71654 | 2019-12-16 10:01:58 +0100 | [diff] [blame] | 826 | ret->val.scnodes[idx].in_ctx = set->val.scnodes[i].in_ctx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 827 | } |
| 828 | } |
| 829 | } else if (set->type == LYXP_SET_NODE_SET) { |
| 830 | ret->type = set->type; |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 831 | if (set->used) { |
| 832 | ret->val.nodes = malloc(set->used * sizeof *ret->val.nodes); |
| 833 | LY_CHECK_ERR_RET(!ret->val.nodes, LOGMEM(set->ctx); free(ret), NULL); |
| 834 | memcpy(ret->val.nodes, set->val.nodes, set->used * sizeof *ret->val.nodes); |
| 835 | } else { |
| 836 | ret->val.nodes = NULL; |
| 837 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 838 | |
| 839 | ret->used = ret->size = set->used; |
| 840 | ret->ctx_pos = set->ctx_pos; |
| 841 | ret->ctx_size = set->ctx_size; |
Michal Vasko | 4a04e54 | 2021-02-01 08:58:30 +0100 | [diff] [blame] | 842 | if (set->ht) { |
| 843 | ret->ht = lyht_dup(set->ht); |
| 844 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 845 | } else { |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 846 | memcpy(ret, set, sizeof *ret); |
| 847 | if (set->type == LYXP_SET_STRING) { |
| 848 | ret->val.str = strdup(set->val.str); |
| 849 | LY_CHECK_ERR_RET(!ret->val.str, LOGMEM(set->ctx); free(ret), NULL); |
| 850 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 851 | } |
| 852 | |
| 853 | return ret; |
| 854 | } |
| 855 | |
| 856 | /** |
| 857 | * @brief Fill XPath set with a string. Any current data are disposed of. |
| 858 | * |
| 859 | * @param[in] set Set to fill. |
| 860 | * @param[in] string String to fill into \p set. |
| 861 | * @param[in] str_len Length of \p string. 0 is a valid value! |
| 862 | */ |
| 863 | static void |
| 864 | set_fill_string(struct lyxp_set *set, const char *string, uint16_t str_len) |
| 865 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 866 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 867 | |
| 868 | set->type = LYXP_SET_STRING; |
| 869 | if ((str_len == 0) && (string[0] != '\0')) { |
| 870 | string = ""; |
| 871 | } |
| 872 | set->val.str = strndup(string, str_len); |
| 873 | } |
| 874 | |
| 875 | /** |
| 876 | * @brief Fill XPath set with a number. Any current data are disposed of. |
| 877 | * |
| 878 | * @param[in] set Set to fill. |
| 879 | * @param[in] number Number to fill into \p set. |
| 880 | */ |
| 881 | static void |
| 882 | set_fill_number(struct lyxp_set *set, long double number) |
| 883 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 884 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 885 | |
| 886 | set->type = LYXP_SET_NUMBER; |
| 887 | set->val.num = number; |
| 888 | } |
| 889 | |
| 890 | /** |
| 891 | * @brief Fill XPath set with a boolean. Any current data are disposed of. |
| 892 | * |
| 893 | * @param[in] set Set to fill. |
| 894 | * @param[in] boolean Boolean to fill into \p set. |
| 895 | */ |
| 896 | static void |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 897 | set_fill_boolean(struct lyxp_set *set, ly_bool boolean) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 898 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 899 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 900 | |
| 901 | set->type = LYXP_SET_BOOLEAN; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 902 | set->val.bln = boolean; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 903 | } |
| 904 | |
| 905 | /** |
| 906 | * @brief Fill XPath set with the value from another set (deep assign). |
| 907 | * Any current data are disposed of. |
| 908 | * |
| 909 | * @param[in] trg Set to fill. |
| 910 | * @param[in] src Source set to copy into \p trg. |
| 911 | */ |
| 912 | static void |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 913 | set_fill_set(struct lyxp_set *trg, const struct lyxp_set *src) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 914 | { |
| 915 | if (!trg || !src) { |
| 916 | return; |
| 917 | } |
| 918 | |
| 919 | if (trg->type == LYXP_SET_NODE_SET) { |
| 920 | free(trg->val.nodes); |
| 921 | } else if (trg->type == LYXP_SET_STRING) { |
| 922 | free(trg->val.str); |
| 923 | } |
| 924 | set_init(trg, src); |
| 925 | |
| 926 | if (src->type == LYXP_SET_SCNODE_SET) { |
| 927 | trg->type = LYXP_SET_SCNODE_SET; |
| 928 | trg->used = src->used; |
| 929 | trg->size = src->used; |
| 930 | |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 931 | if (trg->size) { |
| 932 | trg->val.scnodes = ly_realloc(trg->val.scnodes, trg->size * sizeof *trg->val.scnodes); |
| 933 | LY_CHECK_ERR_RET(!trg->val.scnodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), ); |
| 934 | memcpy(trg->val.scnodes, src->val.scnodes, src->used * sizeof *src->val.scnodes); |
| 935 | } else { |
| 936 | trg->val.scnodes = NULL; |
| 937 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 938 | } else if (src->type == LYXP_SET_BOOLEAN) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 939 | set_fill_boolean(trg, src->val.bln); |
Michal Vasko | 44f3d2c | 2020-08-24 09:49:38 +0200 | [diff] [blame] | 940 | } else if (src->type == LYXP_SET_NUMBER) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 941 | set_fill_number(trg, src->val.num); |
| 942 | } else if (src->type == LYXP_SET_STRING) { |
| 943 | set_fill_string(trg, src->val.str, strlen(src->val.str)); |
| 944 | } else { |
| 945 | if (trg->type == LYXP_SET_NODE_SET) { |
| 946 | free(trg->val.nodes); |
| 947 | } else if (trg->type == LYXP_SET_STRING) { |
| 948 | free(trg->val.str); |
| 949 | } |
| 950 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 951 | assert(src->type == LYXP_SET_NODE_SET); |
| 952 | |
| 953 | trg->type = LYXP_SET_NODE_SET; |
| 954 | trg->used = src->used; |
| 955 | trg->size = src->used; |
| 956 | trg->ctx_pos = src->ctx_pos; |
| 957 | trg->ctx_size = src->ctx_size; |
| 958 | |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 959 | if (trg->size) { |
| 960 | trg->val.nodes = malloc(trg->size * sizeof *trg->val.nodes); |
| 961 | LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), ); |
| 962 | memcpy(trg->val.nodes, src->val.nodes, src->used * sizeof *src->val.nodes); |
| 963 | } else { |
| 964 | trg->val.nodes = NULL; |
| 965 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 966 | if (src->ht) { |
| 967 | trg->ht = lyht_dup(src->ht); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 968 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 969 | trg->ht = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 970 | } |
| 971 | } |
| 972 | } |
| 973 | |
| 974 | /** |
| 975 | * @brief Clear context of all schema nodes. |
| 976 | * |
| 977 | * @param[in] set Set to clear. |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 978 | * @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] | 979 | */ |
| 980 | static void |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 981 | set_scnode_clear_ctx(struct lyxp_set *set, int32_t new_ctx) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 982 | { |
| 983 | uint32_t i; |
| 984 | |
| 985 | for (i = 0; i < set->used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 986 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 987 | set->val.scnodes[i].in_ctx = new_ctx; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 988 | } else if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START) { |
| 989 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 990 | } |
| 991 | } |
| 992 | } |
| 993 | |
| 994 | /** |
| 995 | * @brief Remove a node from a set. Removing last node changes |
| 996 | * set into LYXP_SET_EMPTY. Context position aware. |
| 997 | * |
| 998 | * @param[in] set Set to use. |
| 999 | * @param[in] idx Index from @p set of the node to be removed. |
| 1000 | */ |
| 1001 | static void |
| 1002 | set_remove_node(struct lyxp_set *set, uint32_t idx) |
| 1003 | { |
| 1004 | assert(set && (set->type == LYXP_SET_NODE_SET)); |
| 1005 | assert(idx < set->used); |
| 1006 | |
| 1007 | set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
| 1008 | |
| 1009 | --set->used; |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 1010 | if (idx < set->used) { |
| 1011 | memmove(&set->val.nodes[idx], &set->val.nodes[idx + 1], (set->used - idx) * sizeof *set->val.nodes); |
| 1012 | } else if (!set->used) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1013 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1014 | } |
| 1015 | } |
| 1016 | |
| 1017 | /** |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1018 | * @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] | 1019 | * |
| 1020 | * @param[in] set Set to use. |
| 1021 | * @param[in] idx Index from @p set of the node to be removed. |
| 1022 | */ |
| 1023 | static void |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1024 | set_remove_node_none(struct lyxp_set *set, uint32_t idx) |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1025 | { |
| 1026 | assert(set && (set->type == LYXP_SET_NODE_SET)); |
| 1027 | assert(idx < set->used); |
| 1028 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1029 | if (set->val.nodes[idx].type == LYXP_NODE_ELEM) { |
| 1030 | set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
| 1031 | } |
| 1032 | set->val.nodes[idx].type = LYXP_NODE_NONE; |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1033 | } |
| 1034 | |
| 1035 | /** |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1036 | * @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] | 1037 | * set into LYXP_SET_EMPTY. Context position aware. |
| 1038 | * |
| 1039 | * @param[in] set Set to consolidate. |
| 1040 | */ |
| 1041 | static void |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1042 | set_remove_nodes_none(struct lyxp_set *set) |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1043 | { |
Michal Vasko | 1fdd8fa | 2021-01-08 09:21:45 +0100 | [diff] [blame] | 1044 | uint32_t i, orig_used, end = 0; |
| 1045 | int64_t start; |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1046 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1047 | assert(set); |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1048 | |
| 1049 | orig_used = set->used; |
| 1050 | set->used = 0; |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 1051 | for (i = 0; i < orig_used; ) { |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1052 | start = -1; |
| 1053 | do { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1054 | if ((set->val.nodes[i].type != LYXP_NODE_NONE) && (start == -1)) { |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1055 | start = i; |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1056 | } else if ((start > -1) && (set->val.nodes[i].type == LYXP_NODE_NONE)) { |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1057 | end = i; |
| 1058 | ++i; |
| 1059 | break; |
| 1060 | } |
| 1061 | |
| 1062 | ++i; |
| 1063 | if (i == orig_used) { |
| 1064 | end = i; |
| 1065 | } |
| 1066 | } while (i < orig_used); |
| 1067 | |
| 1068 | if (start > -1) { |
| 1069 | /* move the whole chunk of valid nodes together */ |
| 1070 | if (set->used != (unsigned)start) { |
| 1071 | memmove(&set->val.nodes[set->used], &set->val.nodes[start], (end - start) * sizeof *set->val.nodes); |
| 1072 | } |
| 1073 | set->used += end - start; |
| 1074 | } |
| 1075 | } |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1076 | } |
| 1077 | |
| 1078 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1079 | * @brief Check for duplicates in a node set. |
| 1080 | * |
| 1081 | * @param[in] set Set to check. |
| 1082 | * @param[in] node Node to look for in @p set. |
| 1083 | * @param[in] node_type Type of @p node. |
| 1084 | * @param[in] skip_idx Index from @p set to skip. |
| 1085 | * @return LY_ERR |
| 1086 | */ |
| 1087 | static LY_ERR |
| 1088 | set_dup_node_check(const struct lyxp_set *set, const struct lyd_node *node, enum lyxp_node_type node_type, int skip_idx) |
| 1089 | { |
| 1090 | uint32_t i; |
| 1091 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1092 | if (set->ht && node) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1093 | return set_dup_node_hash_check(set, (struct lyd_node *)node, node_type, skip_idx); |
| 1094 | } |
| 1095 | |
| 1096 | for (i = 0; i < set->used; ++i) { |
| 1097 | if ((skip_idx > -1) && (i == (unsigned)skip_idx)) { |
| 1098 | continue; |
| 1099 | } |
| 1100 | |
| 1101 | if ((set->val.nodes[i].node == node) && (set->val.nodes[i].type == node_type)) { |
| 1102 | return LY_EEXIST; |
| 1103 | } |
| 1104 | } |
| 1105 | |
| 1106 | return LY_SUCCESS; |
| 1107 | } |
| 1108 | |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 1109 | ly_bool |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1110 | lyxp_set_scnode_contains(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, int skip_idx, |
| 1111 | uint32_t *index_p) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1112 | { |
| 1113 | uint32_t i; |
| 1114 | |
| 1115 | for (i = 0; i < set->used; ++i) { |
| 1116 | if ((skip_idx > -1) && (i == (unsigned)skip_idx)) { |
| 1117 | continue; |
| 1118 | } |
| 1119 | |
| 1120 | 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] | 1121 | if (index_p) { |
| 1122 | *index_p = i; |
| 1123 | } |
| 1124 | return 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1125 | } |
| 1126 | } |
| 1127 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1128 | return 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1129 | } |
| 1130 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 1131 | void |
| 1132 | lyxp_set_scnode_merge(struct lyxp_set *set1, struct lyxp_set *set2) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1133 | { |
| 1134 | uint32_t orig_used, i, j; |
| 1135 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1136 | 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] | 1137 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1138 | if (!set2->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1139 | return; |
| 1140 | } |
| 1141 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1142 | if (!set1->used) { |
aPiecek | adc1e4f | 2021-10-07 11:15:12 +0200 | [diff] [blame] | 1143 | /* release hidden allocated data (lyxp_set.size) */ |
| 1144 | lyxp_set_free_content(set1); |
| 1145 | /* direct copying of the entire structure */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1146 | memcpy(set1, set2, sizeof *set1); |
| 1147 | return; |
| 1148 | } |
| 1149 | |
| 1150 | if (set1->used + set2->used > set1->size) { |
| 1151 | set1->size = set1->used + set2->used; |
| 1152 | set1->val.scnodes = ly_realloc(set1->val.scnodes, set1->size * sizeof *set1->val.scnodes); |
| 1153 | LY_CHECK_ERR_RET(!set1->val.scnodes, LOGMEM(set1->ctx), ); |
| 1154 | } |
| 1155 | |
| 1156 | orig_used = set1->used; |
| 1157 | |
| 1158 | for (i = 0; i < set2->used; ++i) { |
| 1159 | for (j = 0; j < orig_used; ++j) { |
| 1160 | /* detect duplicities */ |
| 1161 | if (set1->val.scnodes[j].scnode == set2->val.scnodes[i].scnode) { |
| 1162 | break; |
| 1163 | } |
| 1164 | } |
| 1165 | |
Michal Vasko | 0587bce | 2020-12-10 12:19:21 +0100 | [diff] [blame] | 1166 | if (j < orig_used) { |
| 1167 | /* node is there, but update its status if needed */ |
| 1168 | if (set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_START_USED) { |
| 1169 | set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx; |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 1170 | } else if ((set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_ATOM_NODE) && |
| 1171 | (set2->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_VAL)) { |
| 1172 | set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx; |
Michal Vasko | 0587bce | 2020-12-10 12:19:21 +0100 | [diff] [blame] | 1173 | } |
| 1174 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1175 | memcpy(&set1->val.scnodes[set1->used], &set2->val.scnodes[i], sizeof *set2->val.scnodes); |
| 1176 | ++set1->used; |
| 1177 | } |
| 1178 | } |
| 1179 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1180 | lyxp_set_free_content(set2); |
| 1181 | set2->type = LYXP_SET_SCNODE_SET; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1182 | } |
| 1183 | |
| 1184 | /** |
| 1185 | * @brief Insert a node into a set. Context position aware. |
| 1186 | * |
| 1187 | * @param[in] set Set to use. |
| 1188 | * @param[in] node Node to insert to @p set. |
| 1189 | * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting. |
| 1190 | * @param[in] node_type Node type of @p node. |
| 1191 | * @param[in] idx Index in @p set to insert into. |
| 1192 | */ |
| 1193 | static void |
| 1194 | set_insert_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx) |
| 1195 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1196 | assert(set && (set->type == LYXP_SET_NODE_SET)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1197 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1198 | if (!set->size) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1199 | /* first item */ |
| 1200 | if (idx) { |
| 1201 | /* no real harm done, but it is a bug */ |
| 1202 | LOGINT(set->ctx); |
| 1203 | idx = 0; |
| 1204 | } |
| 1205 | set->val.nodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.nodes); |
| 1206 | LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), ); |
| 1207 | set->type = LYXP_SET_NODE_SET; |
| 1208 | set->used = 0; |
| 1209 | set->size = LYXP_SET_SIZE_START; |
| 1210 | set->ctx_pos = 1; |
| 1211 | set->ctx_size = 1; |
| 1212 | set->ht = NULL; |
| 1213 | } else { |
| 1214 | /* not an empty set */ |
| 1215 | if (set->used == set->size) { |
| 1216 | |
| 1217 | /* set is full */ |
| 1218 | set->val.nodes = ly_realloc(set->val.nodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.nodes); |
| 1219 | LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), ); |
| 1220 | set->size += LYXP_SET_SIZE_STEP; |
| 1221 | } |
| 1222 | |
| 1223 | if (idx > set->used) { |
| 1224 | LOGINT(set->ctx); |
| 1225 | idx = set->used; |
| 1226 | } |
| 1227 | |
| 1228 | /* make space for the new node */ |
| 1229 | if (idx < set->used) { |
| 1230 | memmove(&set->val.nodes[idx + 1], &set->val.nodes[idx], (set->used - idx) * sizeof *set->val.nodes); |
| 1231 | } |
| 1232 | } |
| 1233 | |
| 1234 | /* finally assign the value */ |
| 1235 | set->val.nodes[idx].node = (struct lyd_node *)node; |
| 1236 | set->val.nodes[idx].type = node_type; |
| 1237 | set->val.nodes[idx].pos = pos; |
| 1238 | ++set->used; |
| 1239 | |
Michal Vasko | 2416fdd | 2021-10-01 11:07:10 +0200 | [diff] [blame] | 1240 | /* add into hash table */ |
| 1241 | set_insert_node_hash(set, (struct lyd_node *)node, node_type); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1242 | } |
| 1243 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1244 | LY_ERR |
| 1245 | lyxp_set_scnode_insert_node(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, uint32_t *index_p) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1246 | { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1247 | uint32_t index; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1248 | |
| 1249 | assert(set->type == LYXP_SET_SCNODE_SET); |
| 1250 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1251 | if (lyxp_set_scnode_contains(set, node, node_type, -1, &index)) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1252 | set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1253 | } else { |
| 1254 | if (set->used == set->size) { |
| 1255 | 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] | 1256 | LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), LY_EMEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1257 | set->size += LYXP_SET_SIZE_STEP; |
| 1258 | } |
| 1259 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1260 | index = set->used; |
| 1261 | set->val.scnodes[index].scnode = (struct lysc_node *)node; |
| 1262 | set->val.scnodes[index].type = node_type; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1263 | set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1264 | ++set->used; |
| 1265 | } |
| 1266 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1267 | if (index_p) { |
| 1268 | *index_p = index; |
| 1269 | } |
| 1270 | |
| 1271 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1272 | } |
| 1273 | |
| 1274 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1275 | * @brief Set all nodes with ctx 1 to a new unique context value. |
| 1276 | * |
| 1277 | * @param[in] set Set to modify. |
| 1278 | * @return New context value. |
| 1279 | */ |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 1280 | static int32_t |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1281 | set_scnode_new_in_ctx(struct lyxp_set *set) |
| 1282 | { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 1283 | uint32_t i; |
| 1284 | int32_t ret_ctx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1285 | |
| 1286 | assert(set->type == LYXP_SET_SCNODE_SET); |
| 1287 | |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1288 | ret_ctx = LYXP_SET_SCNODE_ATOM_PRED_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1289 | retry: |
| 1290 | for (i = 0; i < set->used; ++i) { |
| 1291 | if (set->val.scnodes[i].in_ctx >= ret_ctx) { |
| 1292 | ret_ctx = set->val.scnodes[i].in_ctx + 1; |
| 1293 | goto retry; |
| 1294 | } |
| 1295 | } |
| 1296 | for (i = 0; i < set->used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1297 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1298 | set->val.scnodes[i].in_ctx = ret_ctx; |
| 1299 | } |
| 1300 | } |
| 1301 | |
| 1302 | return ret_ctx; |
| 1303 | } |
| 1304 | |
| 1305 | /** |
| 1306 | * @brief Get unique @p node position in the data. |
| 1307 | * |
| 1308 | * @param[in] node Node to find. |
| 1309 | * @param[in] node_type Node type of @p node. |
| 1310 | * @param[in] root Root node. |
| 1311 | * @param[in] root_type Type of the XPath @p root node. |
| 1312 | * @param[in] prev Node that we think is before @p node in DFS from @p root. Can optionally |
| 1313 | * be used to increase efficiency and start the DFS from this node. |
| 1314 | * @param[in] prev_pos Node @p prev position. Optional, but must be set if @p prev is set. |
| 1315 | * @return Node position. |
| 1316 | */ |
| 1317 | static uint32_t |
| 1318 | 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] | 1319 | 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] | 1320 | { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1321 | const struct lyd_node *elem = NULL, *top_sibling; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1322 | uint32_t pos = 1; |
Michal Vasko | fb6c9dd | 2020-11-18 18:18:47 +0100 | [diff] [blame] | 1323 | ly_bool found = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1324 | |
| 1325 | assert(prev && prev_pos && !root->prev->next); |
| 1326 | |
| 1327 | if ((node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ROOT_CONFIG)) { |
| 1328 | return 0; |
| 1329 | } |
| 1330 | |
| 1331 | if (*prev) { |
| 1332 | /* start from the previous element instead from the root */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1333 | pos = *prev_pos; |
Michal Vasko | fb6c9dd | 2020-11-18 18:18:47 +0100 | [diff] [blame] | 1334 | 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] | 1335 | goto dfs_search; |
| 1336 | } |
| 1337 | |
Michal Vasko | fb6c9dd | 2020-11-18 18:18:47 +0100 | [diff] [blame] | 1338 | LY_LIST_FOR(root, top_sibling) { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1339 | LYD_TREE_DFS_BEGIN(top_sibling, elem) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1340 | dfs_search: |
Michal Vasko | a9309bb | 2021-07-09 09:31:55 +0200 | [diff] [blame] | 1341 | LYD_TREE_DFS_continue = 0; |
| 1342 | |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1343 | if (*prev && !elem) { |
| 1344 | /* resume previous DFS */ |
| 1345 | elem = LYD_TREE_DFS_next = (struct lyd_node *)*prev; |
| 1346 | LYD_TREE_DFS_continue = 0; |
| 1347 | } |
| 1348 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1349 | 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] | 1350 | /* skip */ |
| 1351 | LYD_TREE_DFS_continue = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1352 | } else { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1353 | if (elem == node) { |
Michal Vasko | fb6c9dd | 2020-11-18 18:18:47 +0100 | [diff] [blame] | 1354 | found = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1355 | break; |
| 1356 | } |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1357 | ++pos; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1358 | } |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1359 | |
| 1360 | LYD_TREE_DFS_END(top_sibling, elem); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1361 | } |
| 1362 | |
| 1363 | /* node found */ |
Michal Vasko | fb6c9dd | 2020-11-18 18:18:47 +0100 | [diff] [blame] | 1364 | if (found) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1365 | break; |
| 1366 | } |
| 1367 | } |
| 1368 | |
Michal Vasko | fb6c9dd | 2020-11-18 18:18:47 +0100 | [diff] [blame] | 1369 | if (!found) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1370 | if (!(*prev)) { |
| 1371 | /* we went from root and failed to find it, cannot be */ |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1372 | LOGINT(LYD_CTX(node)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1373 | return 0; |
| 1374 | } else { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1375 | /* start the search again from the beginning */ |
| 1376 | *prev = root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1377 | |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1378 | top_sibling = root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1379 | pos = 1; |
| 1380 | goto dfs_search; |
| 1381 | } |
| 1382 | } |
| 1383 | |
| 1384 | /* remember the last found node for next time */ |
| 1385 | *prev = node; |
| 1386 | *prev_pos = pos; |
| 1387 | |
| 1388 | return pos; |
| 1389 | } |
| 1390 | |
| 1391 | /** |
| 1392 | * @brief Assign (fill) missing node positions. |
| 1393 | * |
| 1394 | * @param[in] set Set to fill positions in. |
| 1395 | * @param[in] root Context root node. |
| 1396 | * @param[in] root_type Context root type. |
| 1397 | * @return LY_ERR |
| 1398 | */ |
| 1399 | static LY_ERR |
| 1400 | set_assign_pos(struct lyxp_set *set, const struct lyd_node *root, enum lyxp_node_type root_type) |
| 1401 | { |
| 1402 | const struct lyd_node *prev = NULL, *tmp_node; |
| 1403 | uint32_t i, tmp_pos = 0; |
| 1404 | |
| 1405 | for (i = 0; i < set->used; ++i) { |
| 1406 | if (!set->val.nodes[i].pos) { |
| 1407 | tmp_node = NULL; |
| 1408 | switch (set->val.nodes[i].type) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1409 | case LYXP_NODE_META: |
| 1410 | tmp_node = set->val.meta[i].meta->parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1411 | if (!tmp_node) { |
| 1412 | LOGINT_RET(root->schema->module->ctx); |
| 1413 | } |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1414 | /* fall through */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1415 | case LYXP_NODE_ELEM: |
| 1416 | case LYXP_NODE_TEXT: |
| 1417 | if (!tmp_node) { |
| 1418 | tmp_node = set->val.nodes[i].node; |
| 1419 | } |
| 1420 | set->val.nodes[i].pos = get_node_pos(tmp_node, set->val.nodes[i].type, root, root_type, &prev, &tmp_pos); |
| 1421 | break; |
| 1422 | default: |
| 1423 | /* all roots have position 0 */ |
| 1424 | break; |
| 1425 | } |
| 1426 | } |
| 1427 | } |
| 1428 | |
| 1429 | return LY_SUCCESS; |
| 1430 | } |
| 1431 | |
| 1432 | /** |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1433 | * @brief Get unique @p meta position in the parent metadata. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1434 | * |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1435 | * @param[in] meta Metadata to use. |
| 1436 | * @return Metadata position. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1437 | */ |
| 1438 | static uint16_t |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1439 | get_meta_pos(struct lyd_meta *meta) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1440 | { |
| 1441 | uint16_t pos = 0; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1442 | struct lyd_meta *meta2; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1443 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1444 | for (meta2 = meta->parent->meta; meta2 && (meta2 != meta); meta2 = meta2->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1445 | ++pos; |
| 1446 | } |
| 1447 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1448 | assert(meta2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1449 | return pos; |
| 1450 | } |
| 1451 | |
| 1452 | /** |
| 1453 | * @brief Compare 2 nodes in respect to XPath document order. |
| 1454 | * |
| 1455 | * @param[in] item1 1st node. |
| 1456 | * @param[in] item2 2nd node. |
| 1457 | * @return If 1st > 2nd returns 1, 1st == 2nd returns 0, and 1st < 2nd returns -1. |
| 1458 | */ |
| 1459 | static int |
| 1460 | set_sort_compare(struct lyxp_set_node *item1, struct lyxp_set_node *item2) |
| 1461 | { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1462 | uint32_t meta_pos1 = 0, meta_pos2 = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1463 | |
| 1464 | if (item1->pos < item2->pos) { |
| 1465 | return -1; |
| 1466 | } |
| 1467 | |
| 1468 | if (item1->pos > item2->pos) { |
| 1469 | return 1; |
| 1470 | } |
| 1471 | |
| 1472 | /* node positions are equal, the fun case */ |
| 1473 | |
| 1474 | /* 1st ELEM - == - 2nd TEXT, 1st TEXT - == - 2nd ELEM */ |
| 1475 | /* special case since text nodes are actually saved as their parents */ |
| 1476 | if ((item1->node == item2->node) && (item1->type != item2->type)) { |
| 1477 | if (item1->type == LYXP_NODE_ELEM) { |
| 1478 | assert(item2->type == LYXP_NODE_TEXT); |
| 1479 | return -1; |
| 1480 | } else { |
| 1481 | assert((item1->type == LYXP_NODE_TEXT) && (item2->type == LYXP_NODE_ELEM)); |
| 1482 | return 1; |
| 1483 | } |
| 1484 | } |
| 1485 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1486 | /* we need meta positions now */ |
| 1487 | if (item1->type == LYXP_NODE_META) { |
| 1488 | meta_pos1 = get_meta_pos((struct lyd_meta *)item1->node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1489 | } |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1490 | if (item2->type == LYXP_NODE_META) { |
| 1491 | meta_pos2 = get_meta_pos((struct lyd_meta *)item2->node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1492 | } |
| 1493 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1494 | /* 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] | 1495 | /* check for duplicates */ |
| 1496 | if (item1->node == item2->node) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1497 | 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] | 1498 | return 0; |
| 1499 | } |
| 1500 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1501 | /* 1st ELEM - 2nd TEXT, 1st ELEM - any pos - 2nd META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1502 | /* elem is always first, 2nd node is after it */ |
| 1503 | if (item1->type == LYXP_NODE_ELEM) { |
| 1504 | assert(item2->type != LYXP_NODE_ELEM); |
| 1505 | return -1; |
| 1506 | } |
| 1507 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1508 | /* 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] | 1509 | /* 2nd is before 1st */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1510 | if (((item1->type == LYXP_NODE_TEXT) && |
| 1511 | ((item2->type == LYXP_NODE_ELEM) || (item2->type == LYXP_NODE_META))) || |
| 1512 | ((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_ELEM)) || |
| 1513 | (((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_META)) && |
| 1514 | (meta_pos1 > meta_pos2))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1515 | return 1; |
| 1516 | } |
| 1517 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1518 | /* 1st META - any pos - 2nd TEXT, 1st META <pos< - 2nd META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1519 | /* 2nd is after 1st */ |
| 1520 | return -1; |
| 1521 | } |
| 1522 | |
| 1523 | /** |
| 1524 | * @brief Set cast for comparisons. |
| 1525 | * |
| 1526 | * @param[in] trg Target set to cast source into. |
| 1527 | * @param[in] src Source set. |
| 1528 | * @param[in] type Target set type. |
| 1529 | * @param[in] src_idx Source set node index. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1530 | * @return LY_ERR |
| 1531 | */ |
| 1532 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1533 | 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] | 1534 | { |
| 1535 | assert(src->type == LYXP_SET_NODE_SET); |
| 1536 | |
| 1537 | set_init(trg, src); |
| 1538 | |
| 1539 | /* insert node into target set */ |
| 1540 | set_insert_node(trg, src->val.nodes[src_idx].node, src->val.nodes[src_idx].pos, src->val.nodes[src_idx].type, 0); |
| 1541 | |
| 1542 | /* cast target set appropriately */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1543 | return lyxp_set_cast(trg, type); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1544 | } |
| 1545 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1546 | /** |
| 1547 | * @brief Set content canonization for comparisons. |
| 1548 | * |
| 1549 | * @param[in] trg Target set to put the canononized source into. |
| 1550 | * @param[in] src Source set. |
| 1551 | * @param[in] xp_node Source XPath node/meta to use for canonization. |
| 1552 | * @return LY_ERR |
| 1553 | */ |
| 1554 | static LY_ERR |
| 1555 | set_comp_canonize(struct lyxp_set *trg, const struct lyxp_set *src, const struct lyxp_set_node *xp_node) |
| 1556 | { |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 1557 | const struct lysc_type *type = NULL; |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1558 | struct lyd_value val; |
| 1559 | struct ly_err_item *err = NULL; |
| 1560 | char *str, *ptr; |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1561 | LY_ERR rc; |
| 1562 | |
| 1563 | /* is there anything to canonize even? */ |
| 1564 | if ((src->type == LYXP_SET_NUMBER) || (src->type == LYXP_SET_STRING)) { |
| 1565 | /* do we have a type to use for canonization? */ |
| 1566 | if ((xp_node->type == LYXP_NODE_ELEM) && (xp_node->node->schema->nodetype & LYD_NODE_TERM)) { |
| 1567 | type = ((struct lyd_node_term *)xp_node->node)->value.realtype; |
| 1568 | } else if (xp_node->type == LYXP_NODE_META) { |
| 1569 | type = ((struct lyd_meta *)xp_node->node)->value.realtype; |
| 1570 | } |
| 1571 | } |
| 1572 | if (!type) { |
| 1573 | goto fill; |
| 1574 | } |
| 1575 | |
| 1576 | if (src->type == LYXP_SET_NUMBER) { |
| 1577 | /* canonize number */ |
| 1578 | if (asprintf(&str, "%Lf", src->val.num) == -1) { |
| 1579 | LOGMEM(src->ctx); |
| 1580 | return LY_EMEM; |
| 1581 | } |
| 1582 | } else { |
| 1583 | /* canonize string */ |
| 1584 | str = strdup(src->val.str); |
| 1585 | } |
| 1586 | |
| 1587 | /* ignore errors, the value may not satisfy schema constraints */ |
Radek Krejci | 0b01330 | 2021-03-29 15:22:32 +0200 | [diff] [blame] | 1588 | 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] | 1589 | LYD_HINT_DATA, xp_node->node->schema, &val, NULL, &err); |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1590 | ly_err_free(err); |
| 1591 | if (rc) { |
| 1592 | /* invalid value */ |
Radek IÅ¡a | 4846022 | 2021-03-02 15:18:32 +0100 | [diff] [blame] | 1593 | /* function store automaticaly dealloc value when fail */ |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1594 | goto fill; |
| 1595 | } |
| 1596 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1597 | /* use the canonized value */ |
| 1598 | set_init(trg, src); |
| 1599 | trg->type = src->type; |
| 1600 | if (src->type == LYXP_SET_NUMBER) { |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 1601 | 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] | 1602 | LY_CHECK_ERR_RET(ptr[0], LOGINT(src->ctx), LY_EINT); |
| 1603 | } else { |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 1604 | 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] | 1605 | } |
| 1606 | type->plugin->free(src->ctx, &val); |
| 1607 | return LY_SUCCESS; |
| 1608 | |
| 1609 | fill: |
| 1610 | /* no canonization needed/possible */ |
| 1611 | set_fill_set(trg, src); |
| 1612 | return LY_SUCCESS; |
| 1613 | } |
| 1614 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1615 | #ifndef NDEBUG |
| 1616 | |
| 1617 | /** |
| 1618 | * @brief Bubble sort @p set into XPath document order. |
| 1619 | * Context position aware. Unused in the 'Release' build target. |
| 1620 | * |
| 1621 | * @param[in] set Set to sort. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1622 | * @return How many times the whole set was traversed - 1 (if set was sorted, returns 0). |
| 1623 | */ |
| 1624 | static int |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1625 | set_sort(struct lyxp_set *set) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1626 | { |
| 1627 | uint32_t i, j; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 1628 | int ret = 0, cmp; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 1629 | ly_bool inverted, change; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1630 | const struct lyd_node *root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1631 | struct lyxp_set_node item; |
| 1632 | struct lyxp_set_hash_node hnode; |
| 1633 | uint64_t hash; |
| 1634 | |
Michal Vasko | 3cf8fbf | 2020-05-27 15:21:21 +0200 | [diff] [blame] | 1635 | if ((set->type != LYXP_SET_NODE_SET) || (set->used < 2)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1636 | return 0; |
| 1637 | } |
| 1638 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1639 | /* find first top-level node to be used as anchor for positions */ |
Michal Vasko | 0245d50 | 2021-12-13 17:05:06 +0100 | [diff] [blame] | 1640 | for (root = set->tree; root->parent; root = lyd_parent(root)) {} |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 1641 | for ( ; root->prev->next; root = root->prev) {} |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1642 | |
| 1643 | /* fill positions */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1644 | if (set_assign_pos(set, root, set->root_type)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1645 | return -1; |
| 1646 | } |
| 1647 | |
| 1648 | LOGDBG(LY_LDGXPATH, "SORT BEGIN"); |
| 1649 | print_set_debug(set); |
| 1650 | |
| 1651 | for (i = 0; i < set->used; ++i) { |
| 1652 | inverted = 0; |
| 1653 | change = 0; |
| 1654 | |
| 1655 | for (j = 1; j < set->used - i; ++j) { |
| 1656 | /* compare node positions */ |
| 1657 | if (inverted) { |
| 1658 | cmp = set_sort_compare(&set->val.nodes[j], &set->val.nodes[j - 1]); |
| 1659 | } else { |
| 1660 | cmp = set_sort_compare(&set->val.nodes[j - 1], &set->val.nodes[j]); |
| 1661 | } |
| 1662 | |
| 1663 | /* swap if needed */ |
| 1664 | if ((inverted && (cmp < 0)) || (!inverted && (cmp > 0))) { |
| 1665 | change = 1; |
| 1666 | |
| 1667 | item = set->val.nodes[j - 1]; |
| 1668 | set->val.nodes[j - 1] = set->val.nodes[j]; |
| 1669 | set->val.nodes[j] = item; |
| 1670 | } else { |
| 1671 | /* whether node_pos1 should be smaller than node_pos2 or the other way around */ |
| 1672 | inverted = !inverted; |
| 1673 | } |
| 1674 | } |
| 1675 | |
| 1676 | ++ret; |
| 1677 | |
| 1678 | if (!change) { |
| 1679 | break; |
| 1680 | } |
| 1681 | } |
| 1682 | |
| 1683 | LOGDBG(LY_LDGXPATH, "SORT END %d", ret); |
| 1684 | print_set_debug(set); |
| 1685 | |
| 1686 | /* check node hashes */ |
| 1687 | if (set->used >= LYD_HT_MIN_ITEMS) { |
| 1688 | assert(set->ht); |
| 1689 | for (i = 0; i < set->used; ++i) { |
| 1690 | hnode.node = set->val.nodes[i].node; |
| 1691 | hnode.type = set->val.nodes[i].type; |
| 1692 | |
| 1693 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 1694 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 1695 | hash = dict_hash_multi(hash, NULL, 0); |
| 1696 | |
| 1697 | assert(!lyht_find(set->ht, &hnode, hash, NULL)); |
| 1698 | } |
| 1699 | } |
| 1700 | |
| 1701 | return ret - 1; |
| 1702 | } |
| 1703 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1704 | #endif |
| 1705 | |
| 1706 | /** |
| 1707 | * @brief Merge 2 sorted sets into one. |
| 1708 | * |
| 1709 | * @param[in,out] trg Set to merge into. Duplicates are removed. |
| 1710 | * @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] | 1711 | * @return LY_ERR |
| 1712 | */ |
| 1713 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1714 | set_sorted_merge(struct lyxp_set *trg, struct lyxp_set *src) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1715 | { |
| 1716 | uint32_t i, j, k, count, dup_count; |
| 1717 | int cmp; |
| 1718 | const struct lyd_node *root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1719 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1720 | 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] | 1721 | return LY_EINVAL; |
| 1722 | } |
| 1723 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1724 | if (!src->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1725 | return LY_SUCCESS; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1726 | } else if (!trg->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1727 | set_fill_set(trg, src); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1728 | lyxp_set_free_content(src); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1729 | return LY_SUCCESS; |
| 1730 | } |
| 1731 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1732 | /* find first top-level node to be used as anchor for positions */ |
Michal Vasko | 80a5b0c | 2021-12-13 17:13:09 +0100 | [diff] [blame^] | 1733 | for (root = trg->tree; root->parent; root = lyd_parent(root)) {} |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 1734 | for ( ; root->prev->next; root = root->prev) {} |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1735 | |
| 1736 | /* fill positions */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1737 | 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] | 1738 | return LY_EINT; |
| 1739 | } |
| 1740 | |
| 1741 | #ifndef NDEBUG |
| 1742 | LOGDBG(LY_LDGXPATH, "MERGE target"); |
| 1743 | print_set_debug(trg); |
| 1744 | LOGDBG(LY_LDGXPATH, "MERGE source"); |
| 1745 | print_set_debug(src); |
| 1746 | #endif |
| 1747 | |
| 1748 | /* make memory for the merge (duplicates are not detected yet, so space |
| 1749 | * will likely be wasted on them, too bad) */ |
| 1750 | if (trg->size - trg->used < src->used) { |
| 1751 | trg->size = trg->used + src->used; |
| 1752 | |
| 1753 | trg->val.nodes = ly_realloc(trg->val.nodes, trg->size * sizeof *trg->val.nodes); |
| 1754 | LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx), LY_EMEM); |
| 1755 | } |
| 1756 | |
| 1757 | i = 0; |
| 1758 | j = 0; |
| 1759 | count = 0; |
| 1760 | dup_count = 0; |
| 1761 | do { |
| 1762 | cmp = set_sort_compare(&src->val.nodes[i], &trg->val.nodes[j]); |
| 1763 | if (!cmp) { |
| 1764 | if (!count) { |
| 1765 | /* duplicate, just skip it */ |
| 1766 | ++i; |
| 1767 | ++j; |
| 1768 | } else { |
| 1769 | /* we are copying something already, so let's copy the duplicate too, |
| 1770 | * we are hoping that afterwards there are some more nodes to |
| 1771 | * copy and this way we can copy them all together */ |
| 1772 | ++count; |
| 1773 | ++dup_count; |
| 1774 | ++i; |
| 1775 | ++j; |
| 1776 | } |
| 1777 | } else if (cmp < 0) { |
| 1778 | /* inserting src node into trg, just remember it for now */ |
| 1779 | ++count; |
| 1780 | ++i; |
| 1781 | |
| 1782 | /* insert the hash now */ |
| 1783 | set_insert_node_hash(trg, src->val.nodes[i - 1].node, src->val.nodes[i - 1].type); |
| 1784 | } else if (count) { |
| 1785 | copy_nodes: |
| 1786 | /* time to actually copy the nodes, we have found the largest block of nodes */ |
| 1787 | memmove(&trg->val.nodes[j + (count - dup_count)], |
| 1788 | &trg->val.nodes[j], |
| 1789 | (trg->used - j) * sizeof *trg->val.nodes); |
| 1790 | memcpy(&trg->val.nodes[j - dup_count], &src->val.nodes[i - count], count * sizeof *src->val.nodes); |
| 1791 | |
| 1792 | trg->used += count - dup_count; |
| 1793 | /* do not change i, except the copying above, we are basically doing exactly what is in the else branch below */ |
| 1794 | j += count - dup_count; |
| 1795 | |
| 1796 | count = 0; |
| 1797 | dup_count = 0; |
| 1798 | } else { |
| 1799 | ++j; |
| 1800 | } |
| 1801 | } while ((i < src->used) && (j < trg->used)); |
| 1802 | |
| 1803 | if ((i < src->used) || count) { |
| 1804 | /* insert all the hashes first */ |
| 1805 | for (k = i; k < src->used; ++k) { |
| 1806 | set_insert_node_hash(trg, src->val.nodes[k].node, src->val.nodes[k].type); |
| 1807 | } |
| 1808 | |
| 1809 | /* loop ended, but we need to copy something at trg end */ |
| 1810 | count += src->used - i; |
| 1811 | i = src->used; |
| 1812 | goto copy_nodes; |
| 1813 | } |
| 1814 | |
| 1815 | /* we are inserting hashes before the actual node insert, which causes |
| 1816 | * situations when there were initially not enough items for a hash table, |
| 1817 | * but even after some were inserted, hash table was not created (during |
| 1818 | * insertion the number of items is not updated yet) */ |
| 1819 | if (!trg->ht && (trg->used >= LYD_HT_MIN_ITEMS)) { |
| 1820 | set_insert_node_hash(trg, NULL, 0); |
| 1821 | } |
| 1822 | |
| 1823 | #ifndef NDEBUG |
| 1824 | LOGDBG(LY_LDGXPATH, "MERGE result"); |
| 1825 | print_set_debug(trg); |
| 1826 | #endif |
| 1827 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1828 | lyxp_set_free_content(src); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1829 | return LY_SUCCESS; |
| 1830 | } |
| 1831 | |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1832 | LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1833 | 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] | 1834 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1835 | if (exp->used == tok_idx) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1836 | if (ctx) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1837 | LOGVAL(ctx, LY_VCODE_XP_EOF); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1838 | } |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1839 | return LY_EINCOMPLETE; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1840 | } |
| 1841 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1842 | if (want_tok && (exp->tokens[tok_idx] != want_tok)) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1843 | if (ctx) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1844 | 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] | 1845 | &exp->expr[exp->tok_pos[tok_idx]], lyxp_print_token(want_tok)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1846 | } |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1847 | return LY_ENOT; |
| 1848 | } |
| 1849 | |
| 1850 | return LY_SUCCESS; |
| 1851 | } |
| 1852 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1853 | LY_ERR |
| 1854 | lyxp_next_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok) |
| 1855 | { |
| 1856 | LY_CHECK_RET(lyxp_check_token(ctx, exp, *tok_idx, want_tok)); |
| 1857 | |
| 1858 | /* skip the token */ |
| 1859 | ++(*tok_idx); |
| 1860 | |
| 1861 | return LY_SUCCESS; |
| 1862 | } |
| 1863 | |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1864 | /* just like lyxp_check_token() but tests for 2 tokens */ |
| 1865 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 1866 | 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] | 1867 | enum lyxp_token want_tok2) |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1868 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1869 | if (exp->used == tok_idx) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1870 | if (ctx) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1871 | LOGVAL(ctx, LY_VCODE_XP_EOF); |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1872 | } |
| 1873 | return LY_EINCOMPLETE; |
| 1874 | } |
| 1875 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1876 | 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] | 1877 | if (ctx) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1878 | 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] | 1879 | &exp->expr[exp->tok_pos[tok_idx]]); |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1880 | } |
| 1881 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1882 | } |
| 1883 | |
| 1884 | return LY_SUCCESS; |
| 1885 | } |
| 1886 | |
Michal Vasko | 4911eeb | 2021-06-28 11:23:05 +0200 | [diff] [blame] | 1887 | LY_ERR |
| 1888 | lyxp_next_token2(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok1, |
| 1889 | enum lyxp_token want_tok2) |
| 1890 | { |
| 1891 | LY_CHECK_RET(exp_check_token2(ctx, exp, *tok_idx, want_tok1, want_tok2)); |
| 1892 | |
| 1893 | /* skip the token */ |
| 1894 | ++(*tok_idx); |
| 1895 | |
| 1896 | return LY_SUCCESS; |
| 1897 | } |
| 1898 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1899 | /** |
| 1900 | * @brief Stack operation push on the repeat array. |
| 1901 | * |
| 1902 | * @param[in] exp Expression to use. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1903 | * @param[in] tok_idx Position in the expresion \p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1904 | * @param[in] repeat_op_idx Index from \p exp of the operator token. This value is pushed. |
| 1905 | */ |
| 1906 | static void |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1907 | 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] | 1908 | { |
| 1909 | uint16_t i; |
| 1910 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1911 | if (exp->repeat[tok_idx]) { |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 1912 | for (i = 0; exp->repeat[tok_idx][i]; ++i) {} |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1913 | exp->repeat[tok_idx] = realloc(exp->repeat[tok_idx], (i + 2) * sizeof *exp->repeat[tok_idx]); |
| 1914 | LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), ); |
| 1915 | exp->repeat[tok_idx][i] = repeat_op_idx; |
| 1916 | exp->repeat[tok_idx][i + 1] = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1917 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1918 | exp->repeat[tok_idx] = calloc(2, sizeof *exp->repeat[tok_idx]); |
| 1919 | LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), ); |
| 1920 | exp->repeat[tok_idx][0] = repeat_op_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1921 | } |
| 1922 | } |
| 1923 | |
| 1924 | /** |
| 1925 | * @brief Reparse Predicate. Logs directly on error. |
| 1926 | * |
| 1927 | * [7] Predicate ::= '[' Expr ']' |
| 1928 | * |
| 1929 | * @param[in] ctx Context for logging. |
| 1930 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1931 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 1932 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1933 | * @return LY_ERR |
| 1934 | */ |
| 1935 | static LY_ERR |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 1936 | 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] | 1937 | { |
| 1938 | LY_ERR rc; |
| 1939 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1940 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1941 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1942 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1943 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 1944 | rc = reparse_or_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1945 | LY_CHECK_RET(rc); |
| 1946 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1947 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1948 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1949 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1950 | |
| 1951 | return LY_SUCCESS; |
| 1952 | } |
| 1953 | |
| 1954 | /** |
| 1955 | * @brief Reparse RelativeLocationPath. Logs directly on error. |
| 1956 | * |
| 1957 | * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step |
| 1958 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 1959 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 1960 | * |
| 1961 | * @param[in] ctx Context for logging. |
| 1962 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1963 | * @param[in] tok_idx Position in the expression \p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 1964 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1965 | * @return LY_ERR (LY_EINCOMPLETE on forward reference) |
| 1966 | */ |
| 1967 | static LY_ERR |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 1968 | 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] | 1969 | { |
| 1970 | LY_ERR rc; |
| 1971 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1972 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1973 | LY_CHECK_RET(rc); |
| 1974 | |
| 1975 | goto step; |
| 1976 | do { |
| 1977 | /* '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1978 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1979 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1980 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1981 | LY_CHECK_RET(rc); |
| 1982 | step: |
| 1983 | /* Step */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1984 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1985 | case LYXP_TOKEN_DOT: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1986 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1987 | break; |
| 1988 | |
| 1989 | case LYXP_TOKEN_DDOT: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1990 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1991 | break; |
| 1992 | |
| 1993 | case LYXP_TOKEN_AT: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1994 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1995 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1996 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1997 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1998 | 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] | 1999 | 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] | 2000 | return LY_EVALID; |
| 2001 | } |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 2002 | /* fall through */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2003 | case LYXP_TOKEN_NAMETEST: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2004 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2005 | goto reparse_predicate; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2006 | |
| 2007 | case LYXP_TOKEN_NODETYPE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2008 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2009 | |
| 2010 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2011 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2012 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2013 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2014 | |
| 2015 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2016 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2017 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2018 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2019 | |
| 2020 | reparse_predicate: |
| 2021 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2022 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2023 | rc = reparse_predicate(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2024 | LY_CHECK_RET(rc); |
| 2025 | } |
| 2026 | break; |
| 2027 | default: |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2028 | 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] | 2029 | return LY_EVALID; |
| 2030 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2031 | } 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] | 2032 | |
| 2033 | return LY_SUCCESS; |
| 2034 | } |
| 2035 | |
| 2036 | /** |
| 2037 | * @brief Reparse AbsoluteLocationPath. Logs directly on error. |
| 2038 | * |
| 2039 | * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath |
| 2040 | * |
| 2041 | * @param[in] ctx Context for logging. |
| 2042 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2043 | * @param[in] tok_idx Position in the expression \p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2044 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2045 | * @return LY_ERR |
| 2046 | */ |
| 2047 | static LY_ERR |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2048 | 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] | 2049 | { |
| 2050 | LY_ERR rc; |
| 2051 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2052 | 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] | 2053 | |
| 2054 | /* '/' RelativeLocationPath? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2055 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2056 | /* '/' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2057 | ++(*tok_idx); |
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 | if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2060 | return LY_SUCCESS; |
| 2061 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2062 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2063 | case LYXP_TOKEN_DOT: |
| 2064 | case LYXP_TOKEN_DDOT: |
| 2065 | case LYXP_TOKEN_AT: |
| 2066 | case LYXP_TOKEN_NAMETEST: |
| 2067 | case LYXP_TOKEN_NODETYPE: |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2068 | rc = reparse_relative_location_path(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2069 | LY_CHECK_RET(rc); |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 2070 | /* fall through */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2071 | default: |
| 2072 | break; |
| 2073 | } |
| 2074 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2075 | } else { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 2076 | /* '//' RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2077 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2078 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2079 | rc = reparse_relative_location_path(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2080 | LY_CHECK_RET(rc); |
| 2081 | } |
| 2082 | |
| 2083 | return LY_SUCCESS; |
| 2084 | } |
| 2085 | |
| 2086 | /** |
| 2087 | * @brief Reparse FunctionCall. Logs directly on error. |
| 2088 | * |
| 2089 | * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')' |
| 2090 | * |
| 2091 | * @param[in] ctx Context for logging. |
| 2092 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2093 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2094 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2095 | * @return LY_ERR |
| 2096 | */ |
| 2097 | static LY_ERR |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2098 | 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] | 2099 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2100 | int8_t min_arg_count = -1; |
| 2101 | uint32_t arg_count, max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2102 | uint16_t func_tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2103 | LY_ERR rc; |
| 2104 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2105 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2106 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2107 | func_tok_idx = *tok_idx; |
| 2108 | switch (exp->tok_len[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2109 | case 3: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2110 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2111 | min_arg_count = 1; |
| 2112 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2113 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2114 | min_arg_count = 1; |
| 2115 | max_arg_count = 1; |
| 2116 | } |
| 2117 | break; |
| 2118 | case 4: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2119 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2120 | min_arg_count = 1; |
| 2121 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2122 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2123 | min_arg_count = 0; |
| 2124 | max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2125 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2126 | min_arg_count = 0; |
| 2127 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2128 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2129 | min_arg_count = 0; |
| 2130 | max_arg_count = 0; |
| 2131 | } |
| 2132 | break; |
| 2133 | case 5: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2134 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2135 | min_arg_count = 1; |
| 2136 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2137 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2138 | min_arg_count = 0; |
| 2139 | max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2140 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2141 | min_arg_count = 1; |
| 2142 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2143 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2144 | min_arg_count = 1; |
| 2145 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2146 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2147 | min_arg_count = 1; |
| 2148 | max_arg_count = 1; |
| 2149 | } |
| 2150 | break; |
| 2151 | case 6: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2152 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2153 | min_arg_count = 2; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2154 | max_arg_count = UINT32_MAX; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2155 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2156 | min_arg_count = 0; |
| 2157 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2158 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2159 | min_arg_count = 0; |
| 2160 | max_arg_count = 1; |
| 2161 | } |
| 2162 | break; |
| 2163 | case 7: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2164 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2165 | min_arg_count = 1; |
| 2166 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2167 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2168 | min_arg_count = 1; |
| 2169 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2170 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2171 | min_arg_count = 0; |
| 2172 | max_arg_count = 0; |
| 2173 | } |
| 2174 | break; |
| 2175 | case 8: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2176 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2177 | min_arg_count = 2; |
| 2178 | max_arg_count = 2; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2179 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2180 | min_arg_count = 0; |
| 2181 | max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2182 | } 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] | 2183 | min_arg_count = 2; |
| 2184 | max_arg_count = 2; |
| 2185 | } |
| 2186 | break; |
| 2187 | case 9: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2188 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2189 | min_arg_count = 2; |
| 2190 | max_arg_count = 3; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2191 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2192 | min_arg_count = 3; |
| 2193 | max_arg_count = 3; |
| 2194 | } |
| 2195 | break; |
| 2196 | case 10: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2197 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2198 | min_arg_count = 0; |
| 2199 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2200 | } 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] | 2201 | min_arg_count = 1; |
| 2202 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2203 | } 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] | 2204 | min_arg_count = 2; |
| 2205 | max_arg_count = 2; |
| 2206 | } |
| 2207 | break; |
| 2208 | case 11: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2209 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2210 | min_arg_count = 2; |
| 2211 | max_arg_count = 2; |
| 2212 | } |
| 2213 | break; |
| 2214 | case 12: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2215 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2216 | min_arg_count = 2; |
| 2217 | max_arg_count = 2; |
| 2218 | } |
| 2219 | break; |
| 2220 | case 13: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2221 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2222 | min_arg_count = 0; |
| 2223 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2224 | } 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] | 2225 | min_arg_count = 0; |
| 2226 | max_arg_count = 1; |
| 2227 | } |
| 2228 | break; |
| 2229 | case 15: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2230 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2231 | min_arg_count = 0; |
| 2232 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2233 | } 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] | 2234 | min_arg_count = 2; |
| 2235 | max_arg_count = 2; |
| 2236 | } |
| 2237 | break; |
| 2238 | case 16: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2239 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2240 | min_arg_count = 2; |
| 2241 | max_arg_count = 2; |
| 2242 | } |
| 2243 | break; |
| 2244 | case 20: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2245 | 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] | 2246 | min_arg_count = 2; |
| 2247 | max_arg_count = 2; |
| 2248 | } |
| 2249 | break; |
| 2250 | } |
| 2251 | if (min_arg_count == -1) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2252 | 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] | 2253 | return LY_EINVAL; |
| 2254 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2255 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2256 | |
| 2257 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2258 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2259 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2260 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2261 | |
| 2262 | /* ( Expr ( ',' Expr )* )? */ |
| 2263 | arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2264 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2265 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2266 | if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2267 | ++arg_count; |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2268 | rc = reparse_or_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2269 | LY_CHECK_RET(rc); |
| 2270 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2271 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) { |
| 2272 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2273 | |
| 2274 | ++arg_count; |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2275 | rc = reparse_or_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2276 | LY_CHECK_RET(rc); |
| 2277 | } |
| 2278 | |
| 2279 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2280 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2281 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2282 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2283 | |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 2284 | 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] | 2285 | 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] | 2286 | return LY_EVALID; |
| 2287 | } |
| 2288 | |
| 2289 | return LY_SUCCESS; |
| 2290 | } |
| 2291 | |
| 2292 | /** |
| 2293 | * @brief Reparse PathExpr. Logs directly on error. |
| 2294 | * |
| 2295 | * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate* |
| 2296 | * | PrimaryExpr Predicate* '/' RelativeLocationPath |
| 2297 | * | PrimaryExpr Predicate* '//' RelativeLocationPath |
| 2298 | * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 2299 | * [8] PrimaryExpr ::= VariableReference | '(' Expr ')' | Literal | Number | FunctionCall |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2300 | * |
| 2301 | * @param[in] ctx Context for logging. |
| 2302 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2303 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2304 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2305 | * @return LY_ERR |
| 2306 | */ |
| 2307 | static LY_ERR |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2308 | 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] | 2309 | { |
| 2310 | LY_ERR rc; |
| 2311 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2312 | if (lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 2313 | return LY_EVALID; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2314 | } |
| 2315 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2316 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2317 | case LYXP_TOKEN_PAR1: |
| 2318 | /* '(' Expr ')' Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2319 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2320 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2321 | rc = reparse_or_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2322 | LY_CHECK_RET(rc); |
| 2323 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2324 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2325 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2326 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2327 | goto predicate; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2328 | case LYXP_TOKEN_DOT: |
| 2329 | case LYXP_TOKEN_DDOT: |
| 2330 | case LYXP_TOKEN_AT: |
| 2331 | case LYXP_TOKEN_NAMETEST: |
| 2332 | case LYXP_TOKEN_NODETYPE: |
| 2333 | /* RelativeLocationPath */ |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2334 | rc = reparse_relative_location_path(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2335 | LY_CHECK_RET(rc); |
| 2336 | break; |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 2337 | case LYXP_TOKEN_VARREF: |
| 2338 | /* VariableReference */ |
| 2339 | ++(*tok_idx); |
| 2340 | goto predicate; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2341 | case LYXP_TOKEN_FUNCNAME: |
| 2342 | /* FunctionCall */ |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2343 | rc = reparse_function_call(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2344 | LY_CHECK_RET(rc); |
| 2345 | goto predicate; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2346 | case LYXP_TOKEN_OPER_PATH: |
| 2347 | case LYXP_TOKEN_OPER_RPATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2348 | /* AbsoluteLocationPath */ |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2349 | rc = reparse_absolute_location_path(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2350 | LY_CHECK_RET(rc); |
| 2351 | break; |
| 2352 | case LYXP_TOKEN_LITERAL: |
| 2353 | /* Literal */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2354 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2355 | goto predicate; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2356 | case LYXP_TOKEN_NUMBER: |
| 2357 | /* Number */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2358 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2359 | goto predicate; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2360 | default: |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2361 | 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] | 2362 | return LY_EVALID; |
| 2363 | } |
| 2364 | |
| 2365 | return LY_SUCCESS; |
| 2366 | |
| 2367 | predicate: |
| 2368 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2369 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2370 | rc = reparse_predicate(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2371 | LY_CHECK_RET(rc); |
| 2372 | } |
| 2373 | |
| 2374 | /* ('/' or '//') RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2375 | 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] | 2376 | |
| 2377 | /* '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2378 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2379 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2380 | rc = reparse_relative_location_path(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2381 | LY_CHECK_RET(rc); |
| 2382 | } |
| 2383 | |
| 2384 | return LY_SUCCESS; |
| 2385 | } |
| 2386 | |
| 2387 | /** |
| 2388 | * @brief Reparse UnaryExpr. Logs directly on error. |
| 2389 | * |
| 2390 | * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr |
| 2391 | * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr |
| 2392 | * |
| 2393 | * @param[in] ctx Context for logging. |
| 2394 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2395 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2396 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2397 | * @return LY_ERR |
| 2398 | */ |
| 2399 | static LY_ERR |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2400 | 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] | 2401 | { |
| 2402 | uint16_t prev_exp; |
| 2403 | LY_ERR rc; |
| 2404 | |
| 2405 | /* ('-')* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2406 | prev_exp = *tok_idx; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2407 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) && |
| 2408 | (exp->expr[exp->tok_pos[*tok_idx]] == '-')) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2409 | exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2410 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2411 | } |
| 2412 | |
| 2413 | /* PathExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2414 | prev_exp = *tok_idx; |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2415 | rc = reparse_path_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2416 | LY_CHECK_RET(rc); |
| 2417 | |
| 2418 | /* ('|' PathExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2419 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_UNI)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2420 | exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2421 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2422 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2423 | rc = reparse_path_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2424 | LY_CHECK_RET(rc); |
| 2425 | } |
| 2426 | |
| 2427 | return LY_SUCCESS; |
| 2428 | } |
| 2429 | |
| 2430 | /** |
| 2431 | * @brief Reparse AdditiveExpr. Logs directly on error. |
| 2432 | * |
| 2433 | * [15] AdditiveExpr ::= MultiplicativeExpr |
| 2434 | * | AdditiveExpr '+' MultiplicativeExpr |
| 2435 | * | AdditiveExpr '-' MultiplicativeExpr |
| 2436 | * [16] MultiplicativeExpr ::= UnaryExpr |
| 2437 | * | MultiplicativeExpr '*' UnaryExpr |
| 2438 | * | MultiplicativeExpr 'div' UnaryExpr |
| 2439 | * | MultiplicativeExpr 'mod' UnaryExpr |
| 2440 | * |
| 2441 | * @param[in] ctx Context for logging. |
| 2442 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2443 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2444 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2445 | * @return LY_ERR |
| 2446 | */ |
| 2447 | static LY_ERR |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2448 | 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] | 2449 | { |
| 2450 | uint16_t prev_add_exp, prev_mul_exp; |
| 2451 | LY_ERR rc; |
| 2452 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2453 | prev_add_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2454 | goto reparse_multiplicative_expr; |
| 2455 | |
| 2456 | /* ('+' / '-' MultiplicativeExpr)* */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2457 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) && |
| 2458 | ((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] | 2459 | exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2460 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2461 | |
| 2462 | reparse_multiplicative_expr: |
| 2463 | /* UnaryExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2464 | prev_mul_exp = *tok_idx; |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2465 | rc = reparse_unary_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2466 | LY_CHECK_RET(rc); |
| 2467 | |
| 2468 | /* ('*' / 'div' / 'mod' UnaryExpr)* */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2469 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) && |
| 2470 | ((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] | 2471 | exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2472 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2473 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2474 | rc = reparse_unary_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2475 | LY_CHECK_RET(rc); |
| 2476 | } |
| 2477 | } |
| 2478 | |
| 2479 | return LY_SUCCESS; |
| 2480 | } |
| 2481 | |
| 2482 | /** |
| 2483 | * @brief Reparse EqualityExpr. Logs directly on error. |
| 2484 | * |
| 2485 | * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr |
| 2486 | * | EqualityExpr '!=' RelationalExpr |
| 2487 | * [14] RelationalExpr ::= AdditiveExpr |
| 2488 | * | RelationalExpr '<' AdditiveExpr |
| 2489 | * | RelationalExpr '>' AdditiveExpr |
| 2490 | * | RelationalExpr '<=' AdditiveExpr |
| 2491 | * | RelationalExpr '>=' AdditiveExpr |
| 2492 | * |
| 2493 | * @param[in] ctx Context for logging. |
| 2494 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2495 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2496 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2497 | * @return LY_ERR |
| 2498 | */ |
| 2499 | static LY_ERR |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2500 | 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] | 2501 | { |
| 2502 | uint16_t prev_eq_exp, prev_rel_exp; |
| 2503 | LY_ERR rc; |
| 2504 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2505 | prev_eq_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2506 | goto reparse_additive_expr; |
| 2507 | |
| 2508 | /* ('=' / '!=' RelationalExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2509 | 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] | 2510 | exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2511 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2512 | |
| 2513 | reparse_additive_expr: |
| 2514 | /* AdditiveExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2515 | prev_rel_exp = *tok_idx; |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2516 | rc = reparse_additive_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2517 | LY_CHECK_RET(rc); |
| 2518 | |
| 2519 | /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2520 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_COMP)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2521 | exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2522 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2523 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2524 | rc = reparse_additive_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2525 | LY_CHECK_RET(rc); |
| 2526 | } |
| 2527 | } |
| 2528 | |
| 2529 | return LY_SUCCESS; |
| 2530 | } |
| 2531 | |
| 2532 | /** |
| 2533 | * @brief Reparse OrExpr. Logs directly on error. |
| 2534 | * |
| 2535 | * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr |
| 2536 | * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr |
| 2537 | * |
| 2538 | * @param[in] ctx Context for logging. |
| 2539 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2540 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2541 | * @param[in] depth Current number of nested expressions. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2542 | * @return LY_ERR |
| 2543 | */ |
| 2544 | static LY_ERR |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2545 | 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] | 2546 | { |
| 2547 | uint16_t prev_or_exp, prev_and_exp; |
| 2548 | LY_ERR rc; |
| 2549 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2550 | ++depth; |
| 2551 | LY_CHECK_ERR_RET(depth > LYXP_MAX_BLOCK_DEPTH, LOGVAL(ctx, LY_VCODE_XP_DEPTH), LY_EINVAL); |
| 2552 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2553 | prev_or_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2554 | goto reparse_equality_expr; |
| 2555 | |
| 2556 | /* ('or' AndExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2557 | 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] | 2558 | exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2559 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2560 | |
| 2561 | reparse_equality_expr: |
| 2562 | /* EqualityExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2563 | prev_and_exp = *tok_idx; |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2564 | rc = reparse_equality_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2565 | LY_CHECK_RET(rc); |
| 2566 | |
| 2567 | /* ('and' EqualityExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2568 | 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] | 2569 | exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2570 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2571 | |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2572 | rc = reparse_equality_expr(ctx, exp, tok_idx, depth); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2573 | LY_CHECK_RET(rc); |
| 2574 | } |
| 2575 | } |
| 2576 | |
| 2577 | return LY_SUCCESS; |
| 2578 | } |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2579 | |
| 2580 | /** |
| 2581 | * @brief Parse NCName. |
| 2582 | * |
| 2583 | * @param[in] ncname Name to parse. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2584 | * @return Length of @p ncname valid bytes. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2585 | */ |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2586 | static long int |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2587 | parse_ncname(const char *ncname) |
| 2588 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2589 | uint32_t uc; |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2590 | size_t size; |
| 2591 | long int len = 0; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2592 | |
| 2593 | LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0); |
| 2594 | if (!is_xmlqnamestartchar(uc) || (uc == ':')) { |
| 2595 | return len; |
| 2596 | } |
| 2597 | |
| 2598 | do { |
| 2599 | len += size; |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 2600 | if (!*ncname) { |
| 2601 | break; |
| 2602 | } |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2603 | LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2604 | } while (is_xmlqnamechar(uc) && (uc != ':')); |
| 2605 | |
| 2606 | return len; |
| 2607 | } |
| 2608 | |
| 2609 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2610 | * @brief Add @p token into the expression @p exp. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2611 | * |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2612 | * @param[in] ctx Context for logging. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2613 | * @param[in] exp Expression to use. |
| 2614 | * @param[in] token Token to add. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2615 | * @param[in] tok_pos Token position in the XPath expression. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2616 | * @param[in] tok_len Token length in the XPath expression. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2617 | * @return LY_ERR |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2618 | */ |
| 2619 | static LY_ERR |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 2620 | 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] | 2621 | { |
| 2622 | uint32_t prev; |
| 2623 | |
| 2624 | if (exp->used == exp->size) { |
| 2625 | prev = exp->size; |
| 2626 | exp->size += LYXP_EXPR_SIZE_STEP; |
| 2627 | if (prev > exp->size) { |
| 2628 | LOGINT(ctx); |
| 2629 | return LY_EINT; |
| 2630 | } |
| 2631 | |
| 2632 | exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens); |
| 2633 | LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM); |
| 2634 | exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos); |
| 2635 | LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM); |
| 2636 | exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len); |
| 2637 | LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM); |
| 2638 | } |
| 2639 | |
| 2640 | exp->tokens[exp->used] = token; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2641 | exp->tok_pos[exp->used] = tok_pos; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2642 | exp->tok_len[exp->used] = tok_len; |
| 2643 | ++exp->used; |
| 2644 | return LY_SUCCESS; |
| 2645 | } |
| 2646 | |
| 2647 | void |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 2648 | lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr) |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2649 | { |
| 2650 | uint16_t i; |
| 2651 | |
| 2652 | if (!expr) { |
| 2653 | return; |
| 2654 | } |
| 2655 | |
| 2656 | lydict_remove(ctx, expr->expr); |
| 2657 | free(expr->tokens); |
| 2658 | free(expr->tok_pos); |
| 2659 | free(expr->tok_len); |
| 2660 | if (expr->repeat) { |
| 2661 | for (i = 0; i < expr->used; ++i) { |
| 2662 | free(expr->repeat[i]); |
| 2663 | } |
| 2664 | } |
| 2665 | free(expr->repeat); |
| 2666 | free(expr); |
| 2667 | } |
| 2668 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2669 | LY_ERR |
| 2670 | 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] | 2671 | { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2672 | LY_ERR ret = LY_SUCCESS; |
| 2673 | struct lyxp_expr *expr; |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2674 | size_t parsed = 0, tok_len; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2675 | enum lyxp_token tok_type; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 2676 | ly_bool prev_function_check = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2677 | uint16_t tok_idx = 0; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2678 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2679 | assert(expr_p); |
| 2680 | |
| 2681 | if (!expr_str[0]) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2682 | LOGVAL(ctx, LY_VCODE_XP_EOF); |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2683 | return LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2684 | } |
| 2685 | |
| 2686 | if (!expr_len) { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2687 | expr_len = strlen(expr_str); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2688 | } |
| 2689 | if (expr_len > UINT16_MAX) { |
Michal Vasko | 623ac7b | 2021-04-09 12:44:23 +0200 | [diff] [blame] | 2690 | 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] | 2691 | return LY_EVALID; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2692 | } |
| 2693 | |
| 2694 | /* init lyxp_expr structure */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2695 | expr = calloc(1, sizeof *expr); |
| 2696 | LY_CHECK_ERR_GOTO(!expr, LOGMEM(ctx); ret = LY_EMEM, error); |
| 2697 | LY_CHECK_GOTO(ret = lydict_insert(ctx, expr_str, expr_len, &expr->expr), error); |
| 2698 | expr->used = 0; |
| 2699 | expr->size = LYXP_EXPR_SIZE_START; |
| 2700 | expr->tokens = malloc(expr->size * sizeof *expr->tokens); |
| 2701 | LY_CHECK_ERR_GOTO(!expr->tokens, LOGMEM(ctx); ret = LY_EMEM, error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2702 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2703 | expr->tok_pos = malloc(expr->size * sizeof *expr->tok_pos); |
| 2704 | 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] | 2705 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2706 | expr->tok_len = malloc(expr->size * sizeof *expr->tok_len); |
| 2707 | 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] | 2708 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2709 | /* make expr 0-terminated */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2710 | expr_str = expr->expr; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2711 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2712 | while (is_xmlws(expr_str[parsed])) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2713 | ++parsed; |
| 2714 | } |
| 2715 | |
| 2716 | do { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2717 | if (expr_str[parsed] == '(') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2718 | |
| 2719 | /* '(' */ |
| 2720 | tok_len = 1; |
| 2721 | tok_type = LYXP_TOKEN_PAR1; |
| 2722 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2723 | 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] | 2724 | /* it is a NodeType/FunctionName after all */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2725 | if (((expr->tok_len[expr->used - 1] == 4) && |
| 2726 | (!strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "node", 4) || |
| 2727 | !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "text", 4))) || |
| 2728 | ((expr->tok_len[expr->used - 1] == 7) && |
| 2729 | !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "comment", 7))) { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2730 | expr->tokens[expr->used - 1] = LYXP_TOKEN_NODETYPE; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2731 | } else { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2732 | expr->tokens[expr->used - 1] = LYXP_TOKEN_FUNCNAME; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2733 | } |
| 2734 | prev_function_check = 0; |
| 2735 | } |
| 2736 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2737 | } else if (expr_str[parsed] == ')') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2738 | |
| 2739 | /* ')' */ |
| 2740 | tok_len = 1; |
| 2741 | tok_type = LYXP_TOKEN_PAR2; |
| 2742 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2743 | } else if (expr_str[parsed] == '[') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2744 | |
| 2745 | /* '[' */ |
| 2746 | tok_len = 1; |
| 2747 | tok_type = LYXP_TOKEN_BRACK1; |
| 2748 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2749 | } else if (expr_str[parsed] == ']') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2750 | |
| 2751 | /* ']' */ |
| 2752 | tok_len = 1; |
| 2753 | tok_type = LYXP_TOKEN_BRACK2; |
| 2754 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2755 | } else if (!strncmp(&expr_str[parsed], "..", 2)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2756 | |
| 2757 | /* '..' */ |
| 2758 | tok_len = 2; |
| 2759 | tok_type = LYXP_TOKEN_DDOT; |
| 2760 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2761 | } else if ((expr_str[parsed] == '.') && (!isdigit(expr_str[parsed + 1]))) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2762 | |
| 2763 | /* '.' */ |
| 2764 | tok_len = 1; |
| 2765 | tok_type = LYXP_TOKEN_DOT; |
| 2766 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2767 | } else if (expr_str[parsed] == '@') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2768 | |
| 2769 | /* '@' */ |
| 2770 | tok_len = 1; |
| 2771 | tok_type = LYXP_TOKEN_AT; |
| 2772 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2773 | } else if (expr_str[parsed] == ',') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2774 | |
| 2775 | /* ',' */ |
| 2776 | tok_len = 1; |
| 2777 | tok_type = LYXP_TOKEN_COMMA; |
| 2778 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2779 | } else if (expr_str[parsed] == '\'') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2780 | |
| 2781 | /* Literal with ' */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2782 | for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\''); ++tok_len) {} |
| 2783 | LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0', |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2784 | 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] | 2785 | error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2786 | ++tok_len; |
| 2787 | tok_type = LYXP_TOKEN_LITERAL; |
| 2788 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2789 | } else if (expr_str[parsed] == '\"') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2790 | |
| 2791 | /* Literal with " */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2792 | for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\"'); ++tok_len) {} |
| 2793 | LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0', |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2794 | 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] | 2795 | error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2796 | ++tok_len; |
| 2797 | tok_type = LYXP_TOKEN_LITERAL; |
| 2798 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2799 | } else if ((expr_str[parsed] == '.') || (isdigit(expr_str[parsed]))) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2800 | |
| 2801 | /* Number */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2802 | for (tok_len = 0; isdigit(expr_str[parsed + tok_len]); ++tok_len) {} |
| 2803 | if (expr_str[parsed + tok_len] == '.') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2804 | ++tok_len; |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2805 | for ( ; isdigit(expr_str[parsed + tok_len]); ++tok_len) {} |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2806 | } |
| 2807 | tok_type = LYXP_TOKEN_NUMBER; |
| 2808 | |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 2809 | } else if (expr_str[parsed] == '$') { |
| 2810 | |
| 2811 | /* VariableReference */ |
| 2812 | parsed++; |
| 2813 | long int ncname_len = parse_ncname(&expr_str[parsed]); |
| 2814 | LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len], |
| 2815 | parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error); |
| 2816 | tok_len = ncname_len; |
| 2817 | LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == ':', |
| 2818 | LOGVAL(ctx, LYVE_XPATH, "Variable with prefix is not supported."); ret = LY_EVALID, |
| 2819 | error); |
| 2820 | tok_type = LYXP_TOKEN_VARREF; |
| 2821 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2822 | } else if (expr_str[parsed] == '/') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2823 | |
| 2824 | /* Operator '/', '//' */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2825 | if (!strncmp(&expr_str[parsed], "//", 2)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2826 | tok_len = 2; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2827 | tok_type = LYXP_TOKEN_OPER_RPATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2828 | } else { |
| 2829 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2830 | tok_type = LYXP_TOKEN_OPER_PATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2831 | } |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2832 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2833 | } else if (!strncmp(&expr_str[parsed], "!=", 2)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2834 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2835 | /* Operator '!=' */ |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2836 | tok_len = 2; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2837 | tok_type = LYXP_TOKEN_OPER_NEQUAL; |
| 2838 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2839 | } else if (!strncmp(&expr_str[parsed], "<=", 2) || !strncmp(&expr_str[parsed], ">=", 2)) { |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2840 | |
| 2841 | /* Operator '<=', '>=' */ |
| 2842 | tok_len = 2; |
| 2843 | tok_type = LYXP_TOKEN_OPER_COMP; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2844 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2845 | } else if (expr_str[parsed] == '|') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2846 | |
| 2847 | /* Operator '|' */ |
| 2848 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2849 | tok_type = LYXP_TOKEN_OPER_UNI; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2850 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2851 | } else if ((expr_str[parsed] == '+') || (expr_str[parsed] == '-')) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2852 | |
| 2853 | /* Operator '+', '-' */ |
| 2854 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2855 | tok_type = LYXP_TOKEN_OPER_MATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2856 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2857 | } else if (expr_str[parsed] == '=') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2858 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2859 | /* Operator '=' */ |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2860 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2861 | tok_type = LYXP_TOKEN_OPER_EQUAL; |
| 2862 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2863 | } else if ((expr_str[parsed] == '<') || (expr_str[parsed] == '>')) { |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2864 | |
| 2865 | /* Operator '<', '>' */ |
| 2866 | tok_len = 1; |
| 2867 | tok_type = LYXP_TOKEN_OPER_COMP; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2868 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2869 | } else if (expr->used && (expr->tokens[expr->used - 1] != LYXP_TOKEN_AT) && |
| 2870 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_PAR1) && |
| 2871 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_BRACK1) && |
| 2872 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_COMMA) && |
| 2873 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_LOG) && |
| 2874 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_EQUAL) && |
| 2875 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_NEQUAL) && |
| 2876 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_COMP) && |
| 2877 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_MATH) && |
| 2878 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_UNI) && |
| 2879 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_PATH) && |
| 2880 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_RPATH)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2881 | |
| 2882 | /* Operator '*', 'or', 'and', 'mod', or 'div' */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2883 | if (expr_str[parsed] == '*') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2884 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2885 | tok_type = LYXP_TOKEN_OPER_MATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2886 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2887 | } else if (!strncmp(&expr_str[parsed], "or", 2)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2888 | tok_len = 2; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2889 | tok_type = LYXP_TOKEN_OPER_LOG; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2890 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2891 | } else if (!strncmp(&expr_str[parsed], "and", 3)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2892 | tok_len = 3; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2893 | tok_type = LYXP_TOKEN_OPER_LOG; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2894 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2895 | } 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] | 2896 | tok_len = 3; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2897 | tok_type = LYXP_TOKEN_OPER_MATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2898 | |
| 2899 | } else if (prev_function_check) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2900 | 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] | 2901 | 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] | 2902 | ret = LY_EVALID; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2903 | goto error; |
| 2904 | } else { |
Michal Vasko | 774ce40 | 2021-04-14 15:35:06 +0200 | [diff] [blame] | 2905 | 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] | 2906 | ret = LY_EVALID; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2907 | goto error; |
| 2908 | } |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2909 | } else if (expr_str[parsed] == '*') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2910 | |
| 2911 | /* NameTest '*' */ |
| 2912 | tok_len = 1; |
| 2913 | tok_type = LYXP_TOKEN_NAMETEST; |
| 2914 | |
| 2915 | } else { |
| 2916 | |
| 2917 | /* NameTest (NCName ':' '*' | QName) or NodeType/FunctionName */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2918 | long int ncname_len = parse_ncname(&expr_str[parsed]); |
Michal Vasko | e2be546 | 2021-08-04 10:49:42 +0200 | [diff] [blame] | 2919 | 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] | 2920 | parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2921 | tok_len = ncname_len; |
| 2922 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2923 | if (expr_str[parsed + tok_len] == ':') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2924 | ++tok_len; |
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; |
| 2927 | } else { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2928 | ncname_len = parse_ncname(&expr_str[parsed + tok_len]); |
Michal Vasko | e2be546 | 2021-08-04 10:49:42 +0200 | [diff] [blame] | 2929 | 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] | 2930 | parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2931 | tok_len += ncname_len; |
| 2932 | } |
| 2933 | /* remove old flag to prevent ambiguities */ |
| 2934 | prev_function_check = 0; |
| 2935 | tok_type = LYXP_TOKEN_NAMETEST; |
| 2936 | } else { |
| 2937 | /* there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */ |
| 2938 | prev_function_check = 1; |
| 2939 | tok_type = LYXP_TOKEN_NAMETEST; |
| 2940 | } |
| 2941 | } |
| 2942 | |
| 2943 | /* store the token, move on to the next one */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2944 | 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] | 2945 | parsed += tok_len; |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2946 | while (is_xmlws(expr_str[parsed])) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2947 | ++parsed; |
| 2948 | } |
| 2949 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2950 | } while (expr_str[parsed]); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2951 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2952 | if (reparse) { |
| 2953 | /* prealloc repeat */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2954 | expr->repeat = calloc(expr->size, sizeof *expr->repeat); |
| 2955 | LY_CHECK_ERR_GOTO(!expr->repeat, LOGMEM(ctx); ret = LY_EMEM, error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2956 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2957 | /* fill repeat */ |
aPiecek | bf968d9 | 2021-05-27 14:35:05 +0200 | [diff] [blame] | 2958 | 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] | 2959 | if (expr->used > tok_idx) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2960 | 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] | 2961 | &expr->expr[expr->tok_pos[tok_idx]]); |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2962 | ret = LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2963 | goto error; |
| 2964 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2965 | } |
| 2966 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2967 | print_expr_struct_debug(expr); |
| 2968 | *expr_p = expr; |
| 2969 | return LY_SUCCESS; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2970 | |
| 2971 | error: |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2972 | lyxp_expr_free(ctx, expr); |
| 2973 | return ret; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2974 | } |
| 2975 | |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2976 | LY_ERR |
| 2977 | 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] | 2978 | { |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2979 | LY_ERR ret = LY_SUCCESS; |
| 2980 | struct lyxp_expr *dup = NULL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2981 | uint32_t i, j; |
| 2982 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 2983 | if (!exp) { |
| 2984 | goto cleanup; |
| 2985 | } |
| 2986 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2987 | dup = calloc(1, sizeof *dup); |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2988 | LY_CHECK_ERR_GOTO(!dup, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2989 | |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 2990 | if (exp->used) { |
| 2991 | dup->tokens = malloc(exp->used * sizeof *dup->tokens); |
| 2992 | LY_CHECK_ERR_GOTO(!dup->tokens, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 2993 | memcpy(dup->tokens, exp->tokens, exp->used * sizeof *dup->tokens); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2994 | |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 2995 | dup->tok_pos = malloc(exp->used * sizeof *dup->tok_pos); |
| 2996 | LY_CHECK_ERR_GOTO(!dup->tok_pos, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 2997 | 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] | 2998 | |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 2999 | dup->tok_len = malloc(exp->used * sizeof *dup->tok_len); |
| 3000 | LY_CHECK_ERR_GOTO(!dup->tok_len, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 3001 | 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] | 3002 | |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 3003 | dup->repeat = malloc(exp->used * sizeof *dup->repeat); |
| 3004 | LY_CHECK_ERR_GOTO(!dup->repeat, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 3005 | for (i = 0; i < exp->used; ++i) { |
| 3006 | if (!exp->repeat[i]) { |
| 3007 | dup->repeat[i] = NULL; |
| 3008 | } else { |
| 3009 | for (j = 0; exp->repeat[i][j]; ++j) {} |
| 3010 | /* the ending 0 as well */ |
| 3011 | ++j; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3012 | |
Michal Vasko | 08e9b11 | 2021-06-11 15:41:17 +0200 | [diff] [blame] | 3013 | dup->repeat[i] = malloc(j * sizeof **dup->repeat); |
| 3014 | LY_CHECK_ERR_GOTO(!dup->repeat[i], LOGMEM(ctx); ret = LY_EMEM, cleanup); |
| 3015 | memcpy(dup->repeat[i], exp->repeat[i], j * sizeof **dup->repeat); |
| 3016 | dup->repeat[i][j - 1] = 0; |
| 3017 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3018 | } |
| 3019 | } |
| 3020 | |
| 3021 | dup->used = exp->used; |
| 3022 | dup->size = exp->used; |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 3023 | 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] | 3024 | |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 3025 | cleanup: |
| 3026 | if (ret) { |
| 3027 | lyxp_expr_free(ctx, dup); |
| 3028 | } else { |
| 3029 | *dup_p = dup; |
| 3030 | } |
| 3031 | return ret; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3032 | } |
| 3033 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3034 | /** |
| 3035 | * @brief Get the last-added schema node that is currently in the context. |
| 3036 | * |
| 3037 | * @param[in] set Set to search in. |
| 3038 | * @return Last-added schema context node, NULL if no node is in context. |
| 3039 | */ |
| 3040 | static struct lysc_node * |
| 3041 | warn_get_scnode_in_ctx(struct lyxp_set *set) |
| 3042 | { |
| 3043 | uint32_t i; |
| 3044 | |
| 3045 | if (!set || (set->type != LYXP_SET_SCNODE_SET)) { |
| 3046 | return NULL; |
| 3047 | } |
| 3048 | |
| 3049 | i = set->used; |
| 3050 | do { |
| 3051 | --i; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 3052 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3053 | /* if there are more, simply return the first found (last added) */ |
| 3054 | return set->val.scnodes[i].scnode; |
| 3055 | } |
| 3056 | } while (i); |
| 3057 | |
| 3058 | return NULL; |
| 3059 | } |
| 3060 | |
| 3061 | /** |
| 3062 | * @brief Test whether a type is numeric - integer type or decimal64. |
| 3063 | * |
| 3064 | * @param[in] type Type to test. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3065 | * @return Boolean value whether @p type is numeric type or not. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3066 | */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3067 | static ly_bool |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3068 | warn_is_numeric_type(struct lysc_type *type) |
| 3069 | { |
| 3070 | struct lysc_type_union *uni; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3071 | ly_bool ret; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3072 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3073 | |
| 3074 | switch (type->basetype) { |
| 3075 | case LY_TYPE_DEC64: |
| 3076 | case LY_TYPE_INT8: |
| 3077 | case LY_TYPE_UINT8: |
| 3078 | case LY_TYPE_INT16: |
| 3079 | case LY_TYPE_UINT16: |
| 3080 | case LY_TYPE_INT32: |
| 3081 | case LY_TYPE_UINT32: |
| 3082 | case LY_TYPE_INT64: |
| 3083 | case LY_TYPE_UINT64: |
| 3084 | return 1; |
| 3085 | case LY_TYPE_UNION: |
| 3086 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3087 | LY_ARRAY_FOR(uni->types, u) { |
| 3088 | ret = warn_is_numeric_type(uni->types[u]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3089 | if (ret) { |
| 3090 | /* found a suitable type */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3091 | return ret; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3092 | } |
| 3093 | } |
| 3094 | /* did not find any suitable type */ |
| 3095 | return 0; |
| 3096 | case LY_TYPE_LEAFREF: |
| 3097 | return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype); |
| 3098 | default: |
| 3099 | return 0; |
| 3100 | } |
| 3101 | } |
| 3102 | |
| 3103 | /** |
| 3104 | * @brief Test whether a type is string-like - no integers, decimal64 or binary. |
| 3105 | * |
| 3106 | * @param[in] type Type to test. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3107 | * @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] | 3108 | */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3109 | static ly_bool |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3110 | warn_is_string_type(struct lysc_type *type) |
| 3111 | { |
| 3112 | struct lysc_type_union *uni; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3113 | ly_bool ret; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3114 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3115 | |
| 3116 | switch (type->basetype) { |
| 3117 | case LY_TYPE_BITS: |
| 3118 | case LY_TYPE_ENUM: |
| 3119 | case LY_TYPE_IDENT: |
| 3120 | case LY_TYPE_INST: |
| 3121 | case LY_TYPE_STRING: |
| 3122 | return 1; |
| 3123 | case LY_TYPE_UNION: |
| 3124 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3125 | LY_ARRAY_FOR(uni->types, u) { |
| 3126 | ret = warn_is_string_type(uni->types[u]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3127 | if (ret) { |
| 3128 | /* found a suitable type */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3129 | return ret; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3130 | } |
| 3131 | } |
| 3132 | /* did not find any suitable type */ |
| 3133 | return 0; |
| 3134 | case LY_TYPE_LEAFREF: |
| 3135 | return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype); |
| 3136 | default: |
| 3137 | return 0; |
| 3138 | } |
| 3139 | } |
| 3140 | |
| 3141 | /** |
| 3142 | * @brief Test whether a type is one specific type. |
| 3143 | * |
| 3144 | * @param[in] type Type to test. |
| 3145 | * @param[in] base Expected type. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3146 | * @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] | 3147 | */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3148 | static ly_bool |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3149 | warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base) |
| 3150 | { |
| 3151 | struct lysc_type_union *uni; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3152 | ly_bool ret; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3153 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3154 | |
| 3155 | if (type->basetype == base) { |
| 3156 | return 1; |
| 3157 | } else if (type->basetype == LY_TYPE_UNION) { |
| 3158 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3159 | LY_ARRAY_FOR(uni->types, u) { |
| 3160 | ret = warn_is_specific_type(uni->types[u], base); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3161 | if (ret) { |
| 3162 | /* found a suitable type */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3163 | return ret; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3164 | } |
| 3165 | } |
| 3166 | /* did not find any suitable type */ |
| 3167 | return 0; |
| 3168 | } else if (type->basetype == LY_TYPE_LEAFREF) { |
| 3169 | return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base); |
| 3170 | } |
| 3171 | |
| 3172 | return 0; |
| 3173 | } |
| 3174 | |
| 3175 | /** |
| 3176 | * @brief Get next type of a (union) type. |
| 3177 | * |
| 3178 | * @param[in] type Base type. |
| 3179 | * @param[in] prev_type Previously returned type. |
| 3180 | * @return Next type or NULL. |
| 3181 | */ |
| 3182 | static struct lysc_type * |
| 3183 | warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type) |
| 3184 | { |
| 3185 | struct lysc_type_union *uni; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3186 | ly_bool found = 0; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3187 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3188 | |
| 3189 | switch (type->basetype) { |
| 3190 | case LY_TYPE_UNION: |
| 3191 | uni = (struct lysc_type_union *)type; |
| 3192 | if (!prev_type) { |
| 3193 | return uni->types[0]; |
| 3194 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3195 | LY_ARRAY_FOR(uni->types, u) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3196 | if (found) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3197 | return uni->types[u]; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3198 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3199 | if (prev_type == uni->types[u]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3200 | found = 1; |
| 3201 | } |
| 3202 | } |
| 3203 | return NULL; |
| 3204 | default: |
| 3205 | if (prev_type) { |
| 3206 | assert(type == prev_type); |
| 3207 | return NULL; |
| 3208 | } else { |
| 3209 | return type; |
| 3210 | } |
| 3211 | } |
| 3212 | } |
| 3213 | |
| 3214 | /** |
| 3215 | * @brief Test whether 2 types have a common type. |
| 3216 | * |
| 3217 | * @param[in] type1 First type. |
| 3218 | * @param[in] type2 Second type. |
| 3219 | * @return 1 if they do, 0 otherwise. |
| 3220 | */ |
| 3221 | static int |
| 3222 | warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2) |
| 3223 | { |
| 3224 | struct lysc_type *t1, *rt1, *t2, *rt2; |
| 3225 | |
| 3226 | t1 = NULL; |
| 3227 | while ((t1 = warn_is_equal_type_next_type(type1, t1))) { |
| 3228 | if (t1->basetype == LY_TYPE_LEAFREF) { |
| 3229 | rt1 = ((struct lysc_type_leafref *)t1)->realtype; |
| 3230 | } else { |
| 3231 | rt1 = t1; |
| 3232 | } |
| 3233 | |
| 3234 | t2 = NULL; |
| 3235 | while ((t2 = warn_is_equal_type_next_type(type2, t2))) { |
| 3236 | if (t2->basetype == LY_TYPE_LEAFREF) { |
| 3237 | rt2 = ((struct lysc_type_leafref *)t2)->realtype; |
| 3238 | } else { |
| 3239 | rt2 = t2; |
| 3240 | } |
| 3241 | |
| 3242 | if (rt2->basetype == rt1->basetype) { |
| 3243 | /* match found */ |
| 3244 | return 1; |
| 3245 | } |
| 3246 | } |
| 3247 | } |
| 3248 | |
| 3249 | return 0; |
| 3250 | } |
| 3251 | |
| 3252 | /** |
Michal Vasko | aa95652 | 2021-11-11 10:45:34 +0100 | [diff] [blame] | 3253 | * @brief Print warning with information about the XPath subexpression that caused previous warning. |
| 3254 | * |
| 3255 | * @param[in] ctx Context for logging. |
| 3256 | * @param[in] tok_pos Index of the subexpression in the whole expression. |
| 3257 | * @param[in] subexpr Subexpression start. |
| 3258 | * @param[in] subexpr_len Length of @p subexpr to print. |
| 3259 | * @param[in] cur_scnode Expression context node. |
| 3260 | */ |
| 3261 | static void |
| 3262 | warn_subexpr_log(const struct ly_ctx *ctx, uint16_t tok_pos, const char *subexpr, int subexpr_len, |
| 3263 | const struct lysc_node *cur_scnode) |
| 3264 | { |
| 3265 | char *path; |
| 3266 | |
| 3267 | path = lysc_path(cur_scnode, LYSC_PATH_LOG, NULL, 0); |
| 3268 | LOGWRN(ctx, "Previous warning generated by XPath subexpression[%" PRIu16 "] \"%.*s\" with context node \"%s\".", |
| 3269 | tok_pos, subexpr_len, subexpr, path); |
| 3270 | free(path); |
| 3271 | } |
| 3272 | |
| 3273 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3274 | * @brief Check both operands of comparison operators. |
| 3275 | * |
| 3276 | * @param[in] ctx Context for errors. |
| 3277 | * @param[in] set1 First operand set. |
| 3278 | * @param[in] set2 Second operand set. |
| 3279 | * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!='). |
| 3280 | * @param[in] expr Start of the expression to print with the warning. |
| 3281 | * @param[in] tok_pos Token position. |
| 3282 | */ |
| 3283 | static void |
Michal Vasko | aa95652 | 2021-11-11 10:45:34 +0100 | [diff] [blame] | 3284 | warn_operands(struct ly_ctx *ctx, struct lyxp_set *set1, struct lyxp_set *set2, ly_bool numbers_only, const char *expr, |
| 3285 | uint16_t tok_pos) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3286 | { |
| 3287 | struct lysc_node_leaf *node1, *node2; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3288 | ly_bool leaves = 1, warning = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3289 | |
| 3290 | node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1); |
| 3291 | node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2); |
| 3292 | |
| 3293 | if (!node1 && !node2) { |
| 3294 | /* no node-sets involved, nothing to do */ |
| 3295 | return; |
| 3296 | } |
| 3297 | |
| 3298 | if (node1) { |
| 3299 | if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3300 | LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name); |
| 3301 | warning = 1; |
| 3302 | leaves = 0; |
| 3303 | } else if (numbers_only && !warn_is_numeric_type(node1->type)) { |
| 3304 | LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name); |
| 3305 | warning = 1; |
| 3306 | } |
| 3307 | } |
| 3308 | |
| 3309 | if (node2) { |
| 3310 | if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3311 | LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name); |
| 3312 | warning = 1; |
| 3313 | leaves = 0; |
| 3314 | } else if (numbers_only && !warn_is_numeric_type(node2->type)) { |
| 3315 | LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name); |
| 3316 | warning = 1; |
| 3317 | } |
| 3318 | } |
| 3319 | |
| 3320 | if (node1 && node2 && leaves && !numbers_only) { |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3321 | if ((warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type)) || |
| 3322 | (!warn_is_numeric_type(node1->type) && warn_is_numeric_type(node2->type)) || |
| 3323 | (!warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type) && |
| 3324 | !warn_is_equal_type(node1->type, node2->type))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3325 | LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name); |
| 3326 | warning = 1; |
| 3327 | } |
| 3328 | } |
| 3329 | |
| 3330 | if (warning) { |
Michal Vasko | aa95652 | 2021-11-11 10:45:34 +0100 | [diff] [blame] | 3331 | 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] | 3332 | } |
| 3333 | } |
| 3334 | |
| 3335 | /** |
| 3336 | * @brief Check that a value is valid for a leaf. If not applicable, does nothing. |
| 3337 | * |
| 3338 | * @param[in] exp Parsed XPath expression. |
| 3339 | * @param[in] set Set with the leaf/leaf-list. |
| 3340 | * @param[in] val_exp Index of the value (literal/number) in @p exp. |
| 3341 | * @param[in] equal_exp Index of the start of the equality expression in @p exp. |
| 3342 | * @param[in] last_equal_exp Index of the end of the equality expression in @p exp. |
| 3343 | */ |
| 3344 | static void |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 3345 | warn_equality_value(const struct lyxp_expr *exp, struct lyxp_set *set, uint16_t val_exp, uint16_t equal_exp, |
| 3346 | uint16_t last_equal_exp) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3347 | { |
| 3348 | struct lysc_node *scnode; |
| 3349 | struct lysc_type *type; |
| 3350 | char *value; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3351 | struct lyd_value storage; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3352 | LY_ERR rc; |
| 3353 | struct ly_err_item *err = NULL; |
| 3354 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3355 | if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && |
| 3356 | ((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] | 3357 | /* check that the node can have the specified value */ |
| 3358 | if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) { |
| 3359 | value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2); |
| 3360 | } else { |
| 3361 | value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]); |
| 3362 | } |
| 3363 | if (!value) { |
| 3364 | LOGMEM(set->ctx); |
| 3365 | return; |
| 3366 | } |
| 3367 | |
| 3368 | if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) { |
| 3369 | 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] | 3370 | " 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] | 3371 | 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] | 3372 | (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] | 3373 | set->cur_scnode); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3374 | } |
| 3375 | |
| 3376 | type = ((struct lysc_node_leaf *)scnode)->type; |
| 3377 | if (type->basetype != LY_TYPE_IDENT) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 3378 | 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] | 3379 | LYD_HINT_DATA, scnode, &storage, NULL, &err); |
Michal Vasko | bf42e83 | 2020-11-23 16:59:42 +0100 | [diff] [blame] | 3380 | if (rc == LY_EINCOMPLETE) { |
| 3381 | rc = LY_SUCCESS; |
| 3382 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3383 | |
| 3384 | if (err) { |
| 3385 | LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg); |
| 3386 | ly_err_free(err); |
| 3387 | } else if (rc != LY_SUCCESS) { |
| 3388 | LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value); |
| 3389 | } |
| 3390 | if (rc != LY_SUCCESS) { |
Michal Vasko | aa95652 | 2021-11-11 10:45:34 +0100 | [diff] [blame] | 3391 | 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] | 3392 | (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] | 3393 | set->cur_scnode); |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3394 | } else { |
| 3395 | type->plugin->free(set->ctx, &storage); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3396 | } |
| 3397 | } |
| 3398 | free(value); |
| 3399 | } |
| 3400 | } |
| 3401 | |
| 3402 | /* |
| 3403 | * XPath functions |
| 3404 | */ |
| 3405 | |
| 3406 | /** |
| 3407 | * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN |
| 3408 | * depending on whether the first node bit value from the second argument is set. |
| 3409 | * |
| 3410 | * @param[in] args Array of arguments. |
| 3411 | * @param[in] arg_count Count of elements in @p args. |
| 3412 | * @param[in,out] set Context and result set at the same time. |
| 3413 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3414 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3415 | */ |
| 3416 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3417 | 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] | 3418 | { |
| 3419 | struct lyd_node_term *leaf; |
| 3420 | struct lysc_node_leaf *sleaf; |
Michal Vasko | 2588b95 | 2021-07-29 07:43:26 +0200 | [diff] [blame] | 3421 | struct lyd_value_bits *bits; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3422 | LY_ERR rc = LY_SUCCESS; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3423 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3424 | |
| 3425 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3426 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3427 | 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] | 3428 | } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3429 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3430 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 3431 | sleaf->name); |
| 3432 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) { |
| 3433 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"bits\".", __func__, sleaf->name); |
| 3434 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3435 | } |
| 3436 | |
| 3437 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 3438 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3439 | LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 3440 | sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3441 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3442 | 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] | 3443 | } |
| 3444 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3445 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3446 | return rc; |
| 3447 | } |
| 3448 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3449 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 3450 | 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] | 3451 | return LY_EVALID; |
| 3452 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3453 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3454 | LY_CHECK_RET(rc); |
| 3455 | |
| 3456 | set_fill_boolean(set, 0); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3457 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3458 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
Michal Vasko | 2588b95 | 2021-07-29 07:43:26 +0200 | [diff] [blame] | 3459 | if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (leaf->value.realtype->basetype == LY_TYPE_BITS)) { |
| 3460 | LYD_VALUE_GET(&leaf->value, bits); |
| 3461 | LY_ARRAY_FOR(bits->items, u) { |
| 3462 | if (!strcmp(bits->items[u]->name, args[1]->val.str)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3463 | set_fill_boolean(set, 1); |
| 3464 | break; |
| 3465 | } |
| 3466 | } |
| 3467 | } |
| 3468 | } |
| 3469 | |
| 3470 | return LY_SUCCESS; |
| 3471 | } |
| 3472 | |
| 3473 | /** |
| 3474 | * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN |
| 3475 | * with the argument converted to boolean. |
| 3476 | * |
| 3477 | * @param[in] args Array of arguments. |
| 3478 | * @param[in] arg_count Count of elements in @p args. |
| 3479 | * @param[in,out] set Context and result set at the same time. |
| 3480 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3481 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3482 | */ |
| 3483 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3484 | 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] | 3485 | { |
| 3486 | LY_ERR rc; |
| 3487 | |
| 3488 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3489 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3490 | return LY_SUCCESS; |
| 3491 | } |
| 3492 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3493 | rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3494 | LY_CHECK_RET(rc); |
| 3495 | set_fill_set(set, args[0]); |
| 3496 | |
| 3497 | return LY_SUCCESS; |
| 3498 | } |
| 3499 | |
| 3500 | /** |
| 3501 | * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER |
| 3502 | * with the first argument rounded up to the nearest integer. |
| 3503 | * |
| 3504 | * @param[in] args Array of arguments. |
| 3505 | * @param[in] arg_count Count of elements in @p args. |
| 3506 | * @param[in,out] set Context and result set at the same time. |
| 3507 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3508 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3509 | */ |
| 3510 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3511 | 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] | 3512 | { |
| 3513 | struct lysc_node_leaf *sleaf; |
| 3514 | LY_ERR rc = LY_SUCCESS; |
| 3515 | |
| 3516 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3517 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3518 | 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] | 3519 | } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3520 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3521 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 3522 | sleaf->name); |
| 3523 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) { |
| 3524 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name); |
| 3525 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3526 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3527 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3528 | return rc; |
| 3529 | } |
| 3530 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3531 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3532 | LY_CHECK_RET(rc); |
| 3533 | if ((long long)args[0]->val.num != args[0]->val.num) { |
| 3534 | set_fill_number(set, ((long long)args[0]->val.num) + 1); |
| 3535 | } else { |
| 3536 | set_fill_number(set, args[0]->val.num); |
| 3537 | } |
| 3538 | |
| 3539 | return LY_SUCCESS; |
| 3540 | } |
| 3541 | |
| 3542 | /** |
| 3543 | * @brief Execute the XPath concat(string, string, string*) function. |
| 3544 | * Returns LYXP_SET_STRING with the concatenation of all the arguments. |
| 3545 | * |
| 3546 | * @param[in] args Array of arguments. |
| 3547 | * @param[in] arg_count Count of elements in @p args. |
| 3548 | * @param[in,out] set Context and result set at the same time. |
| 3549 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3550 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3551 | */ |
| 3552 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3553 | 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] | 3554 | { |
| 3555 | uint16_t i; |
| 3556 | char *str = NULL; |
| 3557 | size_t used = 1; |
| 3558 | LY_ERR rc = LY_SUCCESS; |
| 3559 | struct lysc_node_leaf *sleaf; |
| 3560 | |
| 3561 | if (options & LYXP_SCNODE_ALL) { |
| 3562 | for (i = 0; i < arg_count; ++i) { |
| 3563 | if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) { |
| 3564 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3565 | LOGWRN(set->ctx, "Argument #%u of %s is a %s node \"%s\".", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3566 | i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3567 | } else if (!warn_is_string_type(sleaf->type)) { |
Radek Krejci | 70124c8 | 2020-08-14 22:17:03 +0200 | [diff] [blame] | 3568 | 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] | 3569 | } |
| 3570 | } |
| 3571 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3572 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3573 | return rc; |
| 3574 | } |
| 3575 | |
| 3576 | for (i = 0; i < arg_count; ++i) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3577 | rc = lyxp_set_cast(args[i], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3578 | if (rc != LY_SUCCESS) { |
| 3579 | free(str); |
| 3580 | return rc; |
| 3581 | } |
| 3582 | |
| 3583 | str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char)); |
| 3584 | LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM); |
| 3585 | strcpy(str + used - 1, args[i]->val.str); |
| 3586 | used += strlen(args[i]->val.str); |
| 3587 | } |
| 3588 | |
| 3589 | /* free, kind of */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3590 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3591 | set->type = LYXP_SET_STRING; |
| 3592 | set->val.str = str; |
| 3593 | |
| 3594 | return LY_SUCCESS; |
| 3595 | } |
| 3596 | |
| 3597 | /** |
| 3598 | * @brief Execute the XPath contains(string, string) function. |
| 3599 | * Returns LYXP_SET_BOOLEAN whether the second argument can |
| 3600 | * be found in the first or not. |
| 3601 | * |
| 3602 | * @param[in] args Array of arguments. |
| 3603 | * @param[in] arg_count Count of elements in @p args. |
| 3604 | * @param[in,out] set Context and result set at the same time. |
| 3605 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3606 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3607 | */ |
| 3608 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3609 | 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] | 3610 | { |
| 3611 | struct lysc_node_leaf *sleaf; |
| 3612 | LY_ERR rc = LY_SUCCESS; |
| 3613 | |
| 3614 | if (options & LYXP_SCNODE_ALL) { |
| 3615 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3616 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3617 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 3618 | sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3619 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3620 | 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] | 3621 | } |
| 3622 | } |
| 3623 | |
| 3624 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 3625 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3626 | LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 3627 | sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3628 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3629 | 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] | 3630 | } |
| 3631 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3632 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3633 | return rc; |
| 3634 | } |
| 3635 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3636 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3637 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3638 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3639 | LY_CHECK_RET(rc); |
| 3640 | |
| 3641 | if (strstr(args[0]->val.str, args[1]->val.str)) { |
| 3642 | set_fill_boolean(set, 1); |
| 3643 | } else { |
| 3644 | set_fill_boolean(set, 0); |
| 3645 | } |
| 3646 | |
| 3647 | return LY_SUCCESS; |
| 3648 | } |
| 3649 | |
| 3650 | /** |
| 3651 | * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER |
| 3652 | * with the size of the node-set from the argument. |
| 3653 | * |
| 3654 | * @param[in] args Array of arguments. |
| 3655 | * @param[in] arg_count Count of elements in @p args. |
| 3656 | * @param[in,out] set Context and result set at the same time. |
| 3657 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3658 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3659 | */ |
| 3660 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3661 | 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] | 3662 | { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3663 | LY_ERR rc = LY_SUCCESS; |
| 3664 | |
| 3665 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3666 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3667 | 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] | 3668 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3669 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3670 | return rc; |
| 3671 | } |
| 3672 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3673 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 3674 | 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] | 3675 | return LY_EVALID; |
| 3676 | } |
| 3677 | |
| 3678 | set_fill_number(set, args[0]->used); |
| 3679 | return LY_SUCCESS; |
| 3680 | } |
| 3681 | |
| 3682 | /** |
| 3683 | * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET |
| 3684 | * with the context with the intial node. |
| 3685 | * |
| 3686 | * @param[in] args Array of arguments. |
| 3687 | * @param[in] arg_count Count of elements in @p args. |
| 3688 | * @param[in,out] set Context and result set at the same time. |
| 3689 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3690 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3691 | */ |
| 3692 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3693 | 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] | 3694 | { |
| 3695 | if (arg_count || args) { |
Radek Krejci | e87c7dc | 2021-06-02 21:25:42 +0200 | [diff] [blame] | 3696 | 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] | 3697 | return LY_EVALID; |
| 3698 | } |
| 3699 | |
| 3700 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3701 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3702 | |
Michal Vasko | 62b7f76 | 2021-12-13 16:57:42 +0100 | [diff] [blame] | 3703 | if (set->cur_scnode) { |
| 3704 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, set->cur_scnode, LYXP_NODE_ELEM, NULL)); |
| 3705 | } else { |
| 3706 | /* root node */ |
| 3707 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, NULL, set->root_type, NULL)); |
| 3708 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3709 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3710 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3711 | |
Michal Vasko | 62b7f76 | 2021-12-13 16:57:42 +0100 | [diff] [blame] | 3712 | if (set->cur_node) { |
| 3713 | /* position is filled later */ |
| 3714 | set_insert_node(set, set->cur_node, 0, LYXP_NODE_ELEM, 0); |
| 3715 | } else { |
| 3716 | /* root node */ |
| 3717 | set_insert_node(set, NULL, 0, set->root_type, 0); |
| 3718 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3719 | } |
| 3720 | |
| 3721 | return LY_SUCCESS; |
| 3722 | } |
| 3723 | |
| 3724 | /** |
| 3725 | * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either |
| 3726 | * leafref or instance-identifier target node(s). |
| 3727 | * |
| 3728 | * @param[in] args Array of arguments. |
| 3729 | * @param[in] arg_count Count of elements in @p args. |
| 3730 | * @param[in,out] set Context and result set at the same time. |
| 3731 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3732 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3733 | */ |
| 3734 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3735 | 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] | 3736 | { |
| 3737 | struct lyd_node_term *leaf; |
Michal Vasko | 42e497c | 2020-01-06 08:38:25 +0100 | [diff] [blame] | 3738 | struct lysc_node_leaf *sleaf = NULL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3739 | struct lysc_type_leafref *lref; |
Michal Vasko | ae9e4cb | 2019-09-25 08:43:05 +0200 | [diff] [blame] | 3740 | const struct lysc_node *target; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3741 | struct ly_path *p; |
| 3742 | struct lyd_node *node; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3743 | char *errmsg = NULL; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 3744 | uint8_t oper; |
Michal Vasko | 741bb56 | 2021-06-24 11:59:50 +0200 | [diff] [blame] | 3745 | LY_ERR r; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3746 | |
| 3747 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3748 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3749 | 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] | 3750 | } 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] | 3751 | if (!(sleaf->nodetype & LYD_NODE_TERM)) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3752 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 3753 | sleaf->name); |
Michal Vasko | ed725d7 | 2021-06-23 12:03:45 +0200 | [diff] [blame] | 3754 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) && |
| 3755 | !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3756 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"leafref\" nor \"instance-identifier\".", |
| 3757 | __func__, sleaf->name); |
| 3758 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3759 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3760 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 423ba3f | 2021-07-19 13:08:50 +0200 | [diff] [blame] | 3761 | 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] | 3762 | lref = (struct lysc_type_leafref *)sleaf->type; |
Michal Vasko | d1e53b9 | 2021-01-28 13:11:06 +0100 | [diff] [blame] | 3763 | 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] | 3764 | |
| 3765 | /* it was already evaluated on schema, it must succeed */ |
Michal Vasko | 741bb56 | 2021-06-24 11:59:50 +0200 | [diff] [blame] | 3766 | 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] | 3767 | LY_VALUE_SCHEMA_RESOLVED, lref->prefixes, &p); |
Michal Vasko | 741bb56 | 2021-06-24 11:59:50 +0200 | [diff] [blame] | 3768 | if (!r) { |
| 3769 | /* get the target node */ |
| 3770 | target = p[LY_ARRAY_COUNT(p) - 1].node; |
| 3771 | ly_path_free(set->ctx, p); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3772 | |
Michal Vasko | 741bb56 | 2021-06-24 11:59:50 +0200 | [diff] [blame] | 3773 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, target, LYXP_NODE_ELEM, NULL)); |
| 3774 | } /* 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] | 3775 | } |
| 3776 | |
Michal Vasko | 741bb56 | 2021-06-24 11:59:50 +0200 | [diff] [blame] | 3777 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3778 | } |
| 3779 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3780 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 3781 | 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] | 3782 | return LY_EVALID; |
| 3783 | } |
| 3784 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3785 | lyxp_set_free_content(set); |
| 3786 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3787 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
| 3788 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 3789 | if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 3790 | if (sleaf->type->basetype == LY_TYPE_LEAFREF) { |
| 3791 | /* find leafref target */ |
Radek Krejci | 0b01330 | 2021-03-29 15:22:32 +0200 | [diff] [blame] | 3792 | 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] | 3793 | &node, &errmsg)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3794 | LOGERR(set->ctx, LY_EVALID, errmsg); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3795 | free(errmsg); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3796 | return LY_EVALID; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3797 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3798 | } else { |
| 3799 | assert(sleaf->type->basetype == LY_TYPE_INST); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3800 | if (ly_path_eval(leaf->value.target, set->tree, &node)) { |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3801 | 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] | 3802 | lyd_get_value(&leaf->node)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3803 | return LY_EVALID; |
| 3804 | } |
| 3805 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3806 | |
| 3807 | /* insert it */ |
| 3808 | set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3809 | } |
| 3810 | } |
| 3811 | |
| 3812 | return LY_SUCCESS; |
| 3813 | } |
| 3814 | |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3815 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3816 | 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] | 3817 | { |
Michal Vasko | 1fdd8fa | 2021-01-08 09:21:45 +0100 | [diff] [blame] | 3818 | uint32_t i; |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3819 | LY_ARRAY_COUNT_TYPE u; |
| 3820 | struct lyd_node_term *leaf; |
| 3821 | struct lysc_node_leaf *sleaf; |
| 3822 | struct lyd_meta *meta; |
Michal Vasko | 9392369 | 2021-05-07 15:28:02 +0200 | [diff] [blame] | 3823 | struct lyd_value *val; |
| 3824 | const struct lys_module *mod; |
| 3825 | const char *id_name; |
| 3826 | uint16_t id_len; |
| 3827 | struct lysc_ident *id; |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3828 | LY_ERR rc = LY_SUCCESS; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3829 | ly_bool found; |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3830 | |
| 3831 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3832 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3833 | 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] | 3834 | } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3835 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3836 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype), |
| 3837 | sleaf->name); |
| 3838 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) { |
| 3839 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", func, sleaf->name); |
| 3840 | } |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3841 | } |
| 3842 | |
| 3843 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 3844 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3845 | 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] | 3846 | sleaf->name); |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3847 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3848 | LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", func, sleaf->name); |
| 3849 | } |
| 3850 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3851 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3852 | return rc; |
| 3853 | } |
| 3854 | |
| 3855 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 3856 | 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] | 3857 | return LY_EVALID; |
| 3858 | } |
| 3859 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
| 3860 | LY_CHECK_RET(rc); |
| 3861 | |
Michal Vasko | 9392369 | 2021-05-07 15:28:02 +0200 | [diff] [blame] | 3862 | /* parse the identity */ |
| 3863 | id_name = args[1]->val.str; |
| 3864 | id_len = strlen(id_name); |
| 3865 | rc = moveto_resolve_model(&id_name, &id_len, set, set->cur_node ? set->cur_node->schema : NULL, &mod); |
| 3866 | LY_CHECK_RET(rc); |
| 3867 | if (!mod) { |
| 3868 | LOGVAL(set->ctx, LYVE_XPATH, "Identity \"%.*s\" without a prefix.", (int)id_len, id_name); |
| 3869 | return LY_EVALID; |
| 3870 | } |
| 3871 | |
| 3872 | /* find the identity */ |
| 3873 | found = 0; |
| 3874 | LY_ARRAY_FOR(mod->identities, u) { |
| 3875 | if (!ly_strncmp(mod->identities[u].name, id_name, id_len)) { |
| 3876 | /* we have match */ |
| 3877 | found = 1; |
| 3878 | break; |
| 3879 | } |
| 3880 | } |
| 3881 | if (!found) { |
| 3882 | LOGVAL(set->ctx, LYVE_XPATH, "Identity \"%.*s\" not found in module \"%s\".", (int)id_len, id_name, mod->name); |
| 3883 | return LY_EVALID; |
| 3884 | } |
| 3885 | id = &mod->identities[u]; |
| 3886 | |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3887 | set_fill_boolean(set, 0); |
| 3888 | found = 0; |
| 3889 | for (i = 0; i < args[0]->used; ++i) { |
| 3890 | if ((args[0]->val.nodes[i].type != LYXP_NODE_ELEM) && (args[0]->val.nodes[i].type != LYXP_NODE_META)) { |
| 3891 | continue; |
| 3892 | } |
| 3893 | |
| 3894 | if (args[0]->val.nodes[i].type == LYXP_NODE_ELEM) { |
| 3895 | leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node; |
| 3896 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 3897 | val = &leaf->value; |
| 3898 | if (!(sleaf->nodetype & LYD_NODE_TERM) || (leaf->value.realtype->basetype != LY_TYPE_IDENT)) { |
| 3899 | /* uninteresting */ |
| 3900 | continue; |
| 3901 | } |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3902 | } else { |
| 3903 | meta = args[0]->val.meta[i].meta; |
| 3904 | val = &meta->value; |
| 3905 | if (val->realtype->basetype != LY_TYPE_IDENT) { |
| 3906 | /* uninteresting */ |
| 3907 | continue; |
| 3908 | } |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3909 | } |
| 3910 | |
Michal Vasko | 9392369 | 2021-05-07 15:28:02 +0200 | [diff] [blame] | 3911 | /* check the identity itself */ |
| 3912 | if (self_match && (id == val->ident)) { |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3913 | set_fill_boolean(set, 1); |
| 3914 | found = 1; |
| 3915 | } |
Michal Vasko | 9392369 | 2021-05-07 15:28:02 +0200 | [diff] [blame] | 3916 | if (!found && !lyplg_type_identity_isderived(id, val->ident)) { |
| 3917 | set_fill_boolean(set, 1); |
| 3918 | found = 1; |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3919 | } |
| 3920 | |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3921 | if (found) { |
| 3922 | break; |
| 3923 | } |
| 3924 | } |
| 3925 | |
| 3926 | return LY_SUCCESS; |
| 3927 | } |
| 3928 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3929 | /** |
| 3930 | * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending |
| 3931 | * on whether the first argument nodes contain a node of an identity derived from the second |
| 3932 | * argument identity. |
| 3933 | * |
| 3934 | * @param[in] args Array of arguments. |
| 3935 | * @param[in] arg_count Count of elements in @p args. |
| 3936 | * @param[in,out] set Context and result set at the same time. |
| 3937 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3938 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3939 | */ |
| 3940 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3941 | 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] | 3942 | { |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3943 | return xpath_derived_(args, set, options, 0, __func__); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3944 | } |
| 3945 | |
| 3946 | /** |
| 3947 | * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending |
| 3948 | * on whether the first argument nodes contain a node of an identity that either is or is derived from |
| 3949 | * the second argument identity. |
| 3950 | * |
| 3951 | * @param[in] args Array of arguments. |
| 3952 | * @param[in] arg_count Count of elements in @p args. |
| 3953 | * @param[in,out] set Context and result set at the same time. |
| 3954 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3955 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3956 | */ |
| 3957 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3958 | 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] | 3959 | { |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3960 | return xpath_derived_(args, set, options, 1, __func__); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3961 | } |
| 3962 | |
| 3963 | /** |
| 3964 | * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER |
| 3965 | * with the integer value of the first node's enum value, otherwise NaN. |
| 3966 | * |
| 3967 | * @param[in] args Array of arguments. |
| 3968 | * @param[in] arg_count Count of elements in @p args. |
| 3969 | * @param[in,out] set Context and result set at the same time. |
| 3970 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3971 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3972 | */ |
| 3973 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3974 | 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] | 3975 | { |
| 3976 | struct lyd_node_term *leaf; |
| 3977 | struct lysc_node_leaf *sleaf; |
| 3978 | LY_ERR rc = LY_SUCCESS; |
| 3979 | |
| 3980 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3981 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3982 | 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] | 3983 | } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3984 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3985 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 3986 | sleaf->name); |
| 3987 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) { |
| 3988 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"enumeration\".", __func__, sleaf->name); |
| 3989 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3990 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 3991 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3992 | return rc; |
| 3993 | } |
| 3994 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3995 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 3996 | 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] | 3997 | return LY_EVALID; |
| 3998 | } |
| 3999 | |
| 4000 | set_fill_number(set, NAN); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4001 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4002 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
| 4003 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 4004 | if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) { |
| 4005 | set_fill_number(set, leaf->value.enum_item->value); |
| 4006 | } |
| 4007 | } |
| 4008 | |
| 4009 | return LY_SUCCESS; |
| 4010 | } |
| 4011 | |
| 4012 | /** |
| 4013 | * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN |
| 4014 | * with false value. |
| 4015 | * |
| 4016 | * @param[in] args Array of arguments. |
| 4017 | * @param[in] arg_count Count of elements in @p args. |
| 4018 | * @param[in,out] set Context and result set at the same time. |
| 4019 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4020 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4021 | */ |
| 4022 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4023 | 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] | 4024 | { |
| 4025 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4026 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4027 | return LY_SUCCESS; |
| 4028 | } |
| 4029 | |
| 4030 | set_fill_boolean(set, 0); |
| 4031 | return LY_SUCCESS; |
| 4032 | } |
| 4033 | |
| 4034 | /** |
| 4035 | * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER |
| 4036 | * with the first argument floored (truncated). |
| 4037 | * |
| 4038 | * @param[in] args Array of arguments. |
| 4039 | * @param[in] arg_count Count of elements in @p args. |
| 4040 | * @param[in,out] set Context and result set at the same time. |
| 4041 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4042 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4043 | */ |
| 4044 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4045 | 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] | 4046 | { |
| 4047 | LY_ERR rc; |
| 4048 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4049 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4050 | LY_CHECK_RET(rc); |
| 4051 | if (isfinite(args[0]->val.num)) { |
| 4052 | set_fill_number(set, (long long)args[0]->val.num); |
| 4053 | } |
| 4054 | |
| 4055 | return LY_SUCCESS; |
| 4056 | } |
| 4057 | |
| 4058 | /** |
| 4059 | * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN |
| 4060 | * whether the language of the text matches the one from the argument. |
| 4061 | * |
| 4062 | * @param[in] args Array of arguments. |
| 4063 | * @param[in] arg_count Count of elements in @p args. |
| 4064 | * @param[in,out] set Context and result set at the same time. |
| 4065 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4066 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4067 | */ |
| 4068 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4069 | 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] | 4070 | { |
| 4071 | const struct lyd_node *node; |
| 4072 | struct lysc_node_leaf *sleaf; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4073 | struct lyd_meta *meta = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4074 | const char *val; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4075 | LY_ERR rc = LY_SUCCESS; |
| 4076 | |
| 4077 | if (options & LYXP_SCNODE_ALL) { |
| 4078 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4079 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4080 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 4081 | sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4082 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4083 | 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] | 4084 | } |
| 4085 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4086 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4087 | return rc; |
| 4088 | } |
| 4089 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4090 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4091 | LY_CHECK_RET(rc); |
| 4092 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4093 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4094 | 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] | 4095 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4096 | } else if (!set->used) { |
| 4097 | set_fill_boolean(set, 0); |
| 4098 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4099 | } |
| 4100 | |
| 4101 | switch (set->val.nodes[0].type) { |
| 4102 | case LYXP_NODE_ELEM: |
| 4103 | case LYXP_NODE_TEXT: |
| 4104 | node = set->val.nodes[0].node; |
| 4105 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4106 | case LYXP_NODE_META: |
| 4107 | node = set->val.meta[0].meta->parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4108 | break; |
| 4109 | default: |
| 4110 | /* nothing to do with roots */ |
| 4111 | set_fill_boolean(set, 0); |
| 4112 | return LY_SUCCESS; |
| 4113 | } |
| 4114 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4115 | /* find lang metadata */ |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 4116 | for ( ; node; node = lyd_parent(node)) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4117 | for (meta = node->meta; meta; meta = meta->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4118 | /* annotations */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4119 | 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] | 4120 | break; |
| 4121 | } |
| 4122 | } |
| 4123 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4124 | if (meta) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4125 | break; |
| 4126 | } |
| 4127 | } |
| 4128 | |
| 4129 | /* compare languages */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4130 | if (!meta) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4131 | set_fill_boolean(set, 0); |
| 4132 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4133 | uint64_t i; |
| 4134 | |
Radek Krejci | 6d5ba0c | 2021-04-26 07:49:59 +0200 | [diff] [blame] | 4135 | val = lyd_get_meta_value(meta); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4136 | for (i = 0; args[0]->val.str[i]; ++i) { |
| 4137 | if (tolower(args[0]->val.str[i]) != tolower(val[i])) { |
| 4138 | set_fill_boolean(set, 0); |
| 4139 | break; |
| 4140 | } |
| 4141 | } |
| 4142 | if (!args[0]->val.str[i]) { |
| 4143 | if (!val[i] || (val[i] == '-')) { |
| 4144 | set_fill_boolean(set, 1); |
| 4145 | } else { |
| 4146 | set_fill_boolean(set, 0); |
| 4147 | } |
| 4148 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4149 | } |
| 4150 | |
| 4151 | return LY_SUCCESS; |
| 4152 | } |
| 4153 | |
| 4154 | /** |
| 4155 | * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER |
| 4156 | * with the context size. |
| 4157 | * |
| 4158 | * @param[in] args Array of arguments. |
| 4159 | * @param[in] arg_count Count of elements in @p args. |
| 4160 | * @param[in,out] set Context and result set at the same time. |
| 4161 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4162 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4163 | */ |
| 4164 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4165 | 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] | 4166 | { |
| 4167 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4168 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4169 | return LY_SUCCESS; |
| 4170 | } |
| 4171 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4172 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4173 | LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "last()"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4174 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4175 | } else if (!set->used) { |
| 4176 | set_fill_number(set, 0); |
| 4177 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4178 | } |
| 4179 | |
| 4180 | set_fill_number(set, set->ctx_size); |
| 4181 | return LY_SUCCESS; |
| 4182 | } |
| 4183 | |
| 4184 | /** |
| 4185 | * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING |
| 4186 | * with the node name without namespace from the argument or the context. |
| 4187 | * |
| 4188 | * @param[in] args Array of arguments. |
| 4189 | * @param[in] arg_count Count of elements in @p args. |
| 4190 | * @param[in,out] set Context and result set at the same time. |
| 4191 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4192 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4193 | */ |
| 4194 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4195 | 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] | 4196 | { |
| 4197 | struct lyxp_set_node *item; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 4198 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4199 | /* suppress unused variable warning */ |
| 4200 | (void)options; |
| 4201 | |
| 4202 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4203 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4204 | return LY_SUCCESS; |
| 4205 | } |
| 4206 | |
| 4207 | if (arg_count) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4208 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4209 | 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] | 4210 | "local-name(node-set?)"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4211 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4212 | } else if (!args[0]->used) { |
| 4213 | set_fill_string(set, "", 0); |
| 4214 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4215 | } |
| 4216 | |
| 4217 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4218 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4219 | |
| 4220 | item = &args[0]->val.nodes[0]; |
| 4221 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4222 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4223 | 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] | 4224 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4225 | } else if (!set->used) { |
| 4226 | set_fill_string(set, "", 0); |
| 4227 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4228 | } |
| 4229 | |
| 4230 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4231 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4232 | |
| 4233 | item = &set->val.nodes[0]; |
| 4234 | } |
| 4235 | |
| 4236 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4237 | case LYXP_NODE_NONE: |
| 4238 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4239 | case LYXP_NODE_ROOT: |
| 4240 | case LYXP_NODE_ROOT_CONFIG: |
| 4241 | case LYXP_NODE_TEXT: |
| 4242 | set_fill_string(set, "", 0); |
| 4243 | break; |
| 4244 | case LYXP_NODE_ELEM: |
| 4245 | set_fill_string(set, item->node->schema->name, strlen(item->node->schema->name)); |
| 4246 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4247 | case LYXP_NODE_META: |
| 4248 | 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] | 4249 | break; |
| 4250 | } |
| 4251 | |
| 4252 | return LY_SUCCESS; |
| 4253 | } |
| 4254 | |
| 4255 | /** |
| 4256 | * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING |
| 4257 | * with the node name fully qualified (with namespace) from the argument or the context. |
| 4258 | * |
| 4259 | * @param[in] args Array of arguments. |
| 4260 | * @param[in] arg_count Count of elements in @p args. |
| 4261 | * @param[in,out] set Context and result set at the same time. |
| 4262 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4263 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4264 | */ |
| 4265 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4266 | 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] | 4267 | { |
| 4268 | struct lyxp_set_node *item; |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 4269 | struct lys_module *mod = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4270 | char *str; |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 4271 | const char *name = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4272 | |
| 4273 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4274 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4275 | return LY_SUCCESS; |
| 4276 | } |
| 4277 | |
| 4278 | if (arg_count) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4279 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4280 | 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] | 4281 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4282 | } else if (!args[0]->used) { |
| 4283 | set_fill_string(set, "", 0); |
| 4284 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4285 | } |
| 4286 | |
| 4287 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4288 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4289 | |
| 4290 | item = &args[0]->val.nodes[0]; |
| 4291 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4292 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4293 | 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] | 4294 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4295 | } else if (!set->used) { |
| 4296 | set_fill_string(set, "", 0); |
| 4297 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4298 | } |
| 4299 | |
| 4300 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4301 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4302 | |
| 4303 | item = &set->val.nodes[0]; |
| 4304 | } |
| 4305 | |
| 4306 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4307 | case LYXP_NODE_NONE: |
| 4308 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4309 | case LYXP_NODE_ROOT: |
| 4310 | case LYXP_NODE_ROOT_CONFIG: |
| 4311 | case LYXP_NODE_TEXT: |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 4312 | /* keep NULL */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4313 | break; |
| 4314 | case LYXP_NODE_ELEM: |
| 4315 | mod = item->node->schema->module; |
| 4316 | name = item->node->schema->name; |
| 4317 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4318 | case LYXP_NODE_META: |
| 4319 | mod = ((struct lyd_meta *)item->node)->annotation->module; |
| 4320 | name = ((struct lyd_meta *)item->node)->name; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4321 | break; |
| 4322 | } |
| 4323 | |
| 4324 | if (mod && name) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 4325 | 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] | 4326 | LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM); |
| 4327 | set_fill_string(set, str, strlen(str)); |
| 4328 | free(str); |
| 4329 | } else { |
| 4330 | set_fill_string(set, "", 0); |
| 4331 | } |
| 4332 | |
| 4333 | return LY_SUCCESS; |
| 4334 | } |
| 4335 | |
| 4336 | /** |
| 4337 | * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING |
| 4338 | * with the namespace of the node from the argument or the context. |
| 4339 | * |
| 4340 | * @param[in] args Array of arguments. |
| 4341 | * @param[in] arg_count Count of elements in @p args. |
| 4342 | * @param[in,out] set Context and result set at the same time. |
| 4343 | * @param[in] options XPath options. |
| 4344 | * @return LY_ERR (LY_EINVAL for wrong arguments on schema) |
| 4345 | */ |
| 4346 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4347 | 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] | 4348 | { |
| 4349 | struct lyxp_set_node *item; |
| 4350 | struct lys_module *mod; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 4351 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4352 | /* suppress unused variable warning */ |
| 4353 | (void)options; |
| 4354 | |
| 4355 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4356 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4357 | return LY_SUCCESS; |
| 4358 | } |
| 4359 | |
| 4360 | if (arg_count) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4361 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4362 | 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] | 4363 | "namespace-uri(node-set?)"); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4364 | return LY_EVALID; |
| 4365 | } else if (!args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4366 | set_fill_string(set, "", 0); |
| 4367 | return LY_SUCCESS; |
| 4368 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4369 | |
| 4370 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4371 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4372 | |
| 4373 | item = &args[0]->val.nodes[0]; |
| 4374 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4375 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4376 | 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] | 4377 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4378 | } else if (!set->used) { |
| 4379 | set_fill_string(set, "", 0); |
| 4380 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4381 | } |
| 4382 | |
| 4383 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4384 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4385 | |
| 4386 | item = &set->val.nodes[0]; |
| 4387 | } |
| 4388 | |
| 4389 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4390 | case LYXP_NODE_NONE: |
| 4391 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4392 | case LYXP_NODE_ROOT: |
| 4393 | case LYXP_NODE_ROOT_CONFIG: |
| 4394 | case LYXP_NODE_TEXT: |
| 4395 | set_fill_string(set, "", 0); |
| 4396 | break; |
| 4397 | case LYXP_NODE_ELEM: |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4398 | case LYXP_NODE_META: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4399 | if (item->type == LYXP_NODE_ELEM) { |
| 4400 | mod = item->node->schema->module; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4401 | } else { /* LYXP_NODE_META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4402 | /* annotations */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4403 | mod = ((struct lyd_meta *)item->node)->annotation->module; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4404 | } |
| 4405 | |
| 4406 | set_fill_string(set, mod->ns, strlen(mod->ns)); |
| 4407 | break; |
| 4408 | } |
| 4409 | |
| 4410 | return LY_SUCCESS; |
| 4411 | } |
| 4412 | |
| 4413 | /** |
| 4414 | * @brief Execute the XPath node() function (node type). Returns LYXP_SET_NODE_SET |
| 4415 | * with only nodes from the context. In practice it either leaves the context |
| 4416 | * as it is or returns an empty node set. |
| 4417 | * |
| 4418 | * @param[in] args Array of arguments. |
| 4419 | * @param[in] arg_count Count of elements in @p args. |
| 4420 | * @param[in,out] set Context and result set at the same time. |
| 4421 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4422 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4423 | */ |
| 4424 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4425 | 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] | 4426 | { |
| 4427 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4428 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4429 | return LY_SUCCESS; |
| 4430 | } |
| 4431 | |
| 4432 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4433 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4434 | } |
| 4435 | return LY_SUCCESS; |
| 4436 | } |
| 4437 | |
| 4438 | /** |
| 4439 | * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING |
| 4440 | * with normalized value (no leading, trailing, double white spaces) of the node |
| 4441 | * from the argument or the context. |
| 4442 | * |
| 4443 | * @param[in] args Array of arguments. |
| 4444 | * @param[in] arg_count Count of elements in @p args. |
| 4445 | * @param[in,out] set Context and result set at the same time. |
| 4446 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4447 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4448 | */ |
| 4449 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4450 | 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] | 4451 | { |
| 4452 | uint16_t i, new_used; |
| 4453 | char *new; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 4454 | ly_bool have_spaces = 0, space_before = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4455 | struct lysc_node_leaf *sleaf; |
| 4456 | LY_ERR rc = LY_SUCCESS; |
| 4457 | |
| 4458 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4459 | if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && |
| 4460 | (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4461 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4462 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 4463 | sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4464 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4465 | 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] | 4466 | } |
| 4467 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4468 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4469 | return rc; |
| 4470 | } |
| 4471 | |
| 4472 | if (arg_count) { |
| 4473 | set_fill_set(set, args[0]); |
| 4474 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4475 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4476 | LY_CHECK_RET(rc); |
| 4477 | |
| 4478 | /* is there any normalization necessary? */ |
| 4479 | for (i = 0; set->val.str[i]; ++i) { |
| 4480 | if (is_xmlws(set->val.str[i])) { |
| 4481 | if ((i == 0) || space_before || (!set->val.str[i + 1])) { |
| 4482 | have_spaces = 1; |
| 4483 | break; |
| 4484 | } |
| 4485 | space_before = 1; |
| 4486 | } else { |
| 4487 | space_before = 0; |
| 4488 | } |
| 4489 | } |
| 4490 | |
| 4491 | /* yep, there is */ |
| 4492 | if (have_spaces) { |
| 4493 | /* it's enough, at least one character will go, makes space for ending '\0' */ |
| 4494 | new = malloc(strlen(set->val.str) * sizeof(char)); |
| 4495 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 4496 | new_used = 0; |
| 4497 | |
| 4498 | space_before = 0; |
| 4499 | for (i = 0; set->val.str[i]; ++i) { |
| 4500 | if (is_xmlws(set->val.str[i])) { |
| 4501 | if ((i == 0) || space_before) { |
| 4502 | space_before = 1; |
| 4503 | continue; |
| 4504 | } else { |
| 4505 | space_before = 1; |
| 4506 | } |
| 4507 | } else { |
| 4508 | space_before = 0; |
| 4509 | } |
| 4510 | |
| 4511 | new[new_used] = (space_before ? ' ' : set->val.str[i]); |
| 4512 | ++new_used; |
| 4513 | } |
| 4514 | |
| 4515 | /* at worst there is one trailing space now */ |
| 4516 | if (new_used && is_xmlws(new[new_used - 1])) { |
| 4517 | --new_used; |
| 4518 | } |
| 4519 | |
| 4520 | new = ly_realloc(new, (new_used + 1) * sizeof(char)); |
| 4521 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 4522 | new[new_used] = '\0'; |
| 4523 | |
| 4524 | free(set->val.str); |
| 4525 | set->val.str = new; |
| 4526 | } |
| 4527 | |
| 4528 | return LY_SUCCESS; |
| 4529 | } |
| 4530 | |
| 4531 | /** |
| 4532 | * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN |
| 4533 | * with the argument converted to boolean and logically inverted. |
| 4534 | * |
| 4535 | * @param[in] args Array of arguments. |
| 4536 | * @param[in] arg_count Count of elements in @p args. |
| 4537 | * @param[in,out] set Context and result set at the same time. |
| 4538 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4539 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4540 | */ |
| 4541 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4542 | 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] | 4543 | { |
| 4544 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4545 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4546 | return LY_SUCCESS; |
| 4547 | } |
| 4548 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4549 | lyxp_set_cast(args[0], LYXP_SET_BOOLEAN); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4550 | if (args[0]->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4551 | set_fill_boolean(set, 0); |
| 4552 | } else { |
| 4553 | set_fill_boolean(set, 1); |
| 4554 | } |
| 4555 | |
| 4556 | return LY_SUCCESS; |
| 4557 | } |
| 4558 | |
| 4559 | /** |
| 4560 | * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER |
| 4561 | * with the number representation of either the argument or the context. |
| 4562 | * |
| 4563 | * @param[in] args Array of arguments. |
| 4564 | * @param[in] arg_count Count of elements in @p args. |
| 4565 | * @param[in,out] set Context and result set at the same time. |
| 4566 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4567 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4568 | */ |
| 4569 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4570 | 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] | 4571 | { |
| 4572 | LY_ERR rc; |
| 4573 | |
| 4574 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4575 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4576 | return LY_SUCCESS; |
| 4577 | } |
| 4578 | |
| 4579 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4580 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4581 | LY_CHECK_RET(rc); |
| 4582 | set_fill_set(set, args[0]); |
| 4583 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4584 | rc = lyxp_set_cast(set, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4585 | LY_CHECK_RET(rc); |
| 4586 | } |
| 4587 | |
| 4588 | return LY_SUCCESS; |
| 4589 | } |
| 4590 | |
| 4591 | /** |
| 4592 | * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER |
| 4593 | * with the context position. |
| 4594 | * |
| 4595 | * @param[in] args Array of arguments. |
| 4596 | * @param[in] arg_count Count of elements in @p args. |
| 4597 | * @param[in,out] set Context and result set at the same time. |
| 4598 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4599 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4600 | */ |
| 4601 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4602 | 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] | 4603 | { |
| 4604 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4605 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4606 | return LY_SUCCESS; |
| 4607 | } |
| 4608 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4609 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4610 | LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "position()"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4611 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4612 | } else if (!set->used) { |
| 4613 | set_fill_number(set, 0); |
| 4614 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4615 | } |
| 4616 | |
| 4617 | set_fill_number(set, set->ctx_pos); |
| 4618 | |
| 4619 | /* UNUSED in 'Release' build type */ |
| 4620 | (void)options; |
| 4621 | return LY_SUCCESS; |
| 4622 | } |
| 4623 | |
| 4624 | /** |
| 4625 | * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN |
| 4626 | * depending on whether the second argument regex matches the first argument string. For details refer to |
| 4627 | * YANG 1.1 RFC section 10.2.1. |
| 4628 | * |
| 4629 | * @param[in] args Array of arguments. |
| 4630 | * @param[in] arg_count Count of elements in @p args. |
| 4631 | * @param[in,out] set Context and result set at the same time. |
| 4632 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4633 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4634 | */ |
| 4635 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4636 | 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] | 4637 | { |
| 4638 | struct lysc_pattern **patterns = NULL, **pattern; |
| 4639 | struct lysc_node_leaf *sleaf; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4640 | LY_ERR rc = LY_SUCCESS; |
| 4641 | struct ly_err_item *err; |
| 4642 | |
| 4643 | if (options & LYXP_SCNODE_ALL) { |
| 4644 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4645 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4646 | 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] | 4647 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4648 | 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] | 4649 | } |
| 4650 | } |
| 4651 | |
| 4652 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4653 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4654 | 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] | 4655 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4656 | 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] | 4657 | } |
| 4658 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4659 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4660 | return rc; |
| 4661 | } |
| 4662 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4663 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4664 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4665 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4666 | LY_CHECK_RET(rc); |
| 4667 | |
| 4668 | LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM); |
Radek IÅ¡a | 45802b5 | 2021-02-09 09:21:58 +0100 | [diff] [blame] | 4669 | *pattern = calloc(1, sizeof **pattern); |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 4670 | LOG_LOCSET(NULL, set->cur_node, NULL, NULL); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4671 | 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] | 4672 | if (set->cur_node) { |
| 4673 | LOG_LOCBACK(0, 1, 0, 0); |
| 4674 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4675 | if (rc != LY_SUCCESS) { |
| 4676 | LY_ARRAY_FREE(patterns); |
| 4677 | return rc; |
| 4678 | } |
| 4679 | |
Radek Krejci | 0b01330 | 2021-03-29 15:22:32 +0200 | [diff] [blame] | 4680 | 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] | 4681 | pcre2_code_free((*pattern)->code); |
| 4682 | free(*pattern); |
| 4683 | LY_ARRAY_FREE(patterns); |
| 4684 | if (rc && (rc != LY_EVALID)) { |
Michal Vasko | 177d0ed | 2020-11-23 16:43:03 +0100 | [diff] [blame] | 4685 | ly_err_print(set->ctx, err); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4686 | ly_err_free(err); |
| 4687 | return rc; |
| 4688 | } |
| 4689 | |
| 4690 | if (rc == LY_EVALID) { |
| 4691 | ly_err_free(err); |
| 4692 | set_fill_boolean(set, 0); |
| 4693 | } else { |
| 4694 | set_fill_boolean(set, 1); |
| 4695 | } |
| 4696 | |
| 4697 | return LY_SUCCESS; |
| 4698 | } |
| 4699 | |
| 4700 | /** |
| 4701 | * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER |
| 4702 | * with the rounded first argument. For details refer to |
| 4703 | * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round. |
| 4704 | * |
| 4705 | * @param[in] args Array of arguments. |
| 4706 | * @param[in] arg_count Count of elements in @p args. |
| 4707 | * @param[in,out] set Context and result set at the same time. |
| 4708 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4709 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4710 | */ |
| 4711 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4712 | 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] | 4713 | { |
| 4714 | struct lysc_node_leaf *sleaf; |
| 4715 | LY_ERR rc = LY_SUCCESS; |
| 4716 | |
| 4717 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 4718 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4719 | 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] | 4720 | } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4721 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4722 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 4723 | sleaf->name); |
| 4724 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) { |
| 4725 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name); |
| 4726 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4727 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4728 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4729 | return rc; |
| 4730 | } |
| 4731 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4732 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4733 | LY_CHECK_RET(rc); |
| 4734 | |
| 4735 | /* cover only the cases where floor can't be used */ |
| 4736 | if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) { |
| 4737 | set_fill_number(set, -0.0f); |
| 4738 | } else { |
| 4739 | args[0]->val.num += 0.5; |
| 4740 | rc = xpath_floor(args, 1, args[0], options); |
| 4741 | LY_CHECK_RET(rc); |
| 4742 | set_fill_number(set, args[0]->val.num); |
| 4743 | } |
| 4744 | |
| 4745 | return LY_SUCCESS; |
| 4746 | } |
| 4747 | |
| 4748 | /** |
| 4749 | * @brief Execute the XPath starts-with(string, string) function. |
| 4750 | * Returns LYXP_SET_BOOLEAN whether the second argument is |
| 4751 | * the prefix of the first or not. |
| 4752 | * |
| 4753 | * @param[in] args Array of arguments. |
| 4754 | * @param[in] arg_count Count of elements in @p args. |
| 4755 | * @param[in,out] set Context and result set at the same time. |
| 4756 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4757 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4758 | */ |
| 4759 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4760 | 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] | 4761 | { |
| 4762 | struct lysc_node_leaf *sleaf; |
| 4763 | LY_ERR rc = LY_SUCCESS; |
| 4764 | |
| 4765 | if (options & LYXP_SCNODE_ALL) { |
| 4766 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4767 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4768 | 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] | 4769 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4770 | 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] | 4771 | } |
| 4772 | } |
| 4773 | |
| 4774 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4775 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4776 | 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] | 4777 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4778 | 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] | 4779 | } |
| 4780 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4781 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4782 | return rc; |
| 4783 | } |
| 4784 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4785 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4786 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4787 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4788 | LY_CHECK_RET(rc); |
| 4789 | |
| 4790 | if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) { |
| 4791 | set_fill_boolean(set, 0); |
| 4792 | } else { |
| 4793 | set_fill_boolean(set, 1); |
| 4794 | } |
| 4795 | |
| 4796 | return LY_SUCCESS; |
| 4797 | } |
| 4798 | |
| 4799 | /** |
| 4800 | * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING |
| 4801 | * with the string representation of either the argument or the context. |
| 4802 | * |
| 4803 | * @param[in] args Array of arguments. |
| 4804 | * @param[in] arg_count Count of elements in @p args. |
| 4805 | * @param[in,out] set Context and result set at the same time. |
| 4806 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4807 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4808 | */ |
| 4809 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4810 | 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] | 4811 | { |
| 4812 | LY_ERR rc; |
| 4813 | |
| 4814 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4815 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4816 | return LY_SUCCESS; |
| 4817 | } |
| 4818 | |
| 4819 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4820 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4821 | LY_CHECK_RET(rc); |
| 4822 | set_fill_set(set, args[0]); |
| 4823 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4824 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4825 | LY_CHECK_RET(rc); |
| 4826 | } |
| 4827 | |
| 4828 | return LY_SUCCESS; |
| 4829 | } |
| 4830 | |
| 4831 | /** |
| 4832 | * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER |
| 4833 | * with the length of the string in either the argument or the context. |
| 4834 | * |
| 4835 | * @param[in] args Array of arguments. |
| 4836 | * @param[in] arg_count Count of elements in @p args. |
| 4837 | * @param[in,out] set Context and result set at the same time. |
| 4838 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4839 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4840 | */ |
| 4841 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4842 | 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] | 4843 | { |
| 4844 | struct lysc_node_leaf *sleaf; |
| 4845 | LY_ERR rc = LY_SUCCESS; |
| 4846 | |
| 4847 | if (options & LYXP_SCNODE_ALL) { |
| 4848 | if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4849 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4850 | 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] | 4851 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4852 | 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] | 4853 | } |
| 4854 | } |
| 4855 | if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) { |
| 4856 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4857 | 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] | 4858 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4859 | 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] | 4860 | } |
| 4861 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4862 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4863 | return rc; |
| 4864 | } |
| 4865 | |
| 4866 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4867 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4868 | LY_CHECK_RET(rc); |
| 4869 | set_fill_number(set, strlen(args[0]->val.str)); |
| 4870 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4871 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4872 | LY_CHECK_RET(rc); |
| 4873 | set_fill_number(set, strlen(set->val.str)); |
| 4874 | } |
| 4875 | |
| 4876 | return LY_SUCCESS; |
| 4877 | } |
| 4878 | |
| 4879 | /** |
| 4880 | * @brief Execute the XPath substring(string, number, number?) function. |
| 4881 | * Returns LYXP_SET_STRING substring of the first argument starting |
| 4882 | * on the second argument index ending on the third argument index, |
| 4883 | * indexed from 1. For exact definition refer to |
| 4884 | * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring. |
| 4885 | * |
| 4886 | * @param[in] args Array of arguments. |
| 4887 | * @param[in] arg_count Count of elements in @p args. |
| 4888 | * @param[in,out] set Context and result set at the same time. |
| 4889 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4890 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4891 | */ |
| 4892 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4893 | 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] | 4894 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4895 | int32_t start, len; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4896 | uint16_t str_start, str_len, pos; |
| 4897 | struct lysc_node_leaf *sleaf; |
| 4898 | LY_ERR rc = LY_SUCCESS; |
| 4899 | |
| 4900 | if (options & LYXP_SCNODE_ALL) { |
| 4901 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4902 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4903 | 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] | 4904 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4905 | 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] | 4906 | } |
| 4907 | } |
| 4908 | |
| 4909 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4910 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4911 | 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] | 4912 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 4913 | 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] | 4914 | } |
| 4915 | } |
| 4916 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 4917 | if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET) && |
| 4918 | (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4919 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4920 | 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] | 4921 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 4922 | 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] | 4923 | } |
| 4924 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 4925 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4926 | return rc; |
| 4927 | } |
| 4928 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4929 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4930 | LY_CHECK_RET(rc); |
| 4931 | |
| 4932 | /* start */ |
| 4933 | if (xpath_round(&args[1], 1, args[1], options)) { |
| 4934 | return -1; |
| 4935 | } |
| 4936 | if (isfinite(args[1]->val.num)) { |
| 4937 | start = args[1]->val.num - 1; |
| 4938 | } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4939 | start = INT32_MIN; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4940 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4941 | start = INT32_MAX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4942 | } |
| 4943 | |
| 4944 | /* len */ |
| 4945 | if (arg_count == 3) { |
| 4946 | rc = xpath_round(&args[2], 1, args[2], options); |
| 4947 | LY_CHECK_RET(rc); |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4948 | if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4949 | len = 0; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4950 | } else if (isfinite(args[2]->val.num)) { |
| 4951 | len = args[2]->val.num; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4952 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4953 | len = INT32_MAX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4954 | } |
| 4955 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4956 | len = INT32_MAX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4957 | } |
| 4958 | |
| 4959 | /* find matching character positions */ |
| 4960 | str_start = 0; |
| 4961 | str_len = 0; |
| 4962 | for (pos = 0; args[0]->val.str[pos]; ++pos) { |
| 4963 | if (pos < start) { |
| 4964 | ++str_start; |
| 4965 | } else if (pos < start + len) { |
| 4966 | ++str_len; |
| 4967 | } else { |
| 4968 | break; |
| 4969 | } |
| 4970 | } |
| 4971 | |
| 4972 | set_fill_string(set, args[0]->val.str + str_start, str_len); |
| 4973 | return LY_SUCCESS; |
| 4974 | } |
| 4975 | |
| 4976 | /** |
| 4977 | * @brief Execute the XPath substring-after(string, string) function. |
| 4978 | * Returns LYXP_SET_STRING with the string succeeding the occurance |
| 4979 | * of the second argument in the first or an empty string. |
| 4980 | * |
| 4981 | * @param[in] args Array of arguments. |
| 4982 | * @param[in] arg_count Count of elements in @p args. |
| 4983 | * @param[in,out] set Context and result set at the same time. |
| 4984 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4985 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4986 | */ |
| 4987 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4988 | 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] | 4989 | { |
| 4990 | char *ptr; |
| 4991 | struct lysc_node_leaf *sleaf; |
| 4992 | LY_ERR rc = LY_SUCCESS; |
| 4993 | |
| 4994 | if (options & LYXP_SCNODE_ALL) { |
| 4995 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4996 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4997 | 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] | 4998 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4999 | 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] | 5000 | } |
| 5001 | } |
| 5002 | |
| 5003 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 5004 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5005 | 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] | 5006 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5007 | 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] | 5008 | } |
| 5009 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5010 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5011 | return rc; |
| 5012 | } |
| 5013 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5014 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5015 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5016 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5017 | LY_CHECK_RET(rc); |
| 5018 | |
| 5019 | ptr = strstr(args[0]->val.str, args[1]->val.str); |
| 5020 | if (ptr) { |
| 5021 | set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str))); |
| 5022 | } else { |
| 5023 | set_fill_string(set, "", 0); |
| 5024 | } |
| 5025 | |
| 5026 | return LY_SUCCESS; |
| 5027 | } |
| 5028 | |
| 5029 | /** |
| 5030 | * @brief Execute the XPath substring-before(string, string) function. |
| 5031 | * Returns LYXP_SET_STRING with the string preceding the occurance |
| 5032 | * of the second argument in the first or an empty string. |
| 5033 | * |
| 5034 | * @param[in] args Array of arguments. |
| 5035 | * @param[in] arg_count Count of elements in @p args. |
| 5036 | * @param[in,out] set Context and result set at the same time. |
| 5037 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5038 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5039 | */ |
| 5040 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5041 | 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] | 5042 | { |
| 5043 | char *ptr; |
| 5044 | struct lysc_node_leaf *sleaf; |
| 5045 | LY_ERR rc = LY_SUCCESS; |
| 5046 | |
| 5047 | if (options & LYXP_SCNODE_ALL) { |
| 5048 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 5049 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5050 | 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] | 5051 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5052 | 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] | 5053 | } |
| 5054 | } |
| 5055 | |
| 5056 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 5057 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5058 | 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] | 5059 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5060 | 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] | 5061 | } |
| 5062 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5063 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5064 | return rc; |
| 5065 | } |
| 5066 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5067 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5068 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5069 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5070 | LY_CHECK_RET(rc); |
| 5071 | |
| 5072 | ptr = strstr(args[0]->val.str, args[1]->val.str); |
| 5073 | if (ptr) { |
| 5074 | set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str); |
| 5075 | } else { |
| 5076 | set_fill_string(set, "", 0); |
| 5077 | } |
| 5078 | |
| 5079 | return LY_SUCCESS; |
| 5080 | } |
| 5081 | |
| 5082 | /** |
| 5083 | * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER |
| 5084 | * with the sum of all the nodes in the context. |
| 5085 | * |
| 5086 | * @param[in] args Array of arguments. |
| 5087 | * @param[in] arg_count Count of elements in @p args. |
| 5088 | * @param[in,out] set Context and result set at the same time. |
| 5089 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5090 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5091 | */ |
| 5092 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5093 | 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] | 5094 | { |
| 5095 | long double num; |
| 5096 | char *str; |
Michal Vasko | 1fdd8fa | 2021-01-08 09:21:45 +0100 | [diff] [blame] | 5097 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5098 | struct lyxp_set set_item; |
| 5099 | struct lysc_node_leaf *sleaf; |
| 5100 | LY_ERR rc = LY_SUCCESS; |
| 5101 | |
| 5102 | if (options & LYXP_SCNODE_ALL) { |
| 5103 | if (args[0]->type == LYXP_SET_SCNODE_SET) { |
| 5104 | for (i = 0; i < args[0]->used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 5105 | 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] | 5106 | sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode; |
| 5107 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5108 | 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] | 5109 | lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5110 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 5111 | 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] | 5112 | } |
| 5113 | } |
| 5114 | } |
| 5115 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5116 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5117 | return rc; |
| 5118 | } |
| 5119 | |
| 5120 | set_fill_number(set, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5121 | |
| 5122 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 5123 | 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] | 5124 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5125 | } else if (!args[0]->used) { |
| 5126 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5127 | } |
| 5128 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5129 | set_init(&set_item, set); |
| 5130 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5131 | set_item.type = LYXP_SET_NODE_SET; |
Michal Vasko | 41decbf | 2021-11-02 11:50:21 +0100 | [diff] [blame] | 5132 | set_item.val.nodes = calloc(1, sizeof *set_item.val.nodes); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5133 | LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM); |
| 5134 | |
| 5135 | set_item.used = 1; |
| 5136 | set_item.size = 1; |
| 5137 | |
| 5138 | for (i = 0; i < args[0]->used; ++i) { |
| 5139 | set_item.val.nodes[0] = args[0]->val.nodes[i]; |
| 5140 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5141 | rc = cast_node_set_to_string(&set_item, &str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5142 | LY_CHECK_RET(rc); |
| 5143 | num = cast_string_to_number(str); |
| 5144 | free(str); |
| 5145 | set->val.num += num; |
| 5146 | } |
| 5147 | |
| 5148 | free(set_item.val.nodes); |
| 5149 | |
| 5150 | return LY_SUCCESS; |
| 5151 | } |
| 5152 | |
| 5153 | /** |
| 5154 | * @brief Execute the XPath text() function (node type). Returns LYXP_SET_NODE_SET |
| 5155 | * with the text content of the nodes in the context. |
| 5156 | * |
| 5157 | * @param[in] args Array of arguments. |
| 5158 | * @param[in] arg_count Count of elements in @p args. |
| 5159 | * @param[in,out] set Context and result set at the same time. |
| 5160 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5161 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5162 | */ |
| 5163 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5164 | 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] | 5165 | { |
| 5166 | uint32_t i; |
| 5167 | |
| 5168 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5169 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5170 | return LY_SUCCESS; |
| 5171 | } |
| 5172 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5173 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 5174 | LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "text()"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5175 | return LY_EVALID; |
| 5176 | } |
| 5177 | |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 5178 | for (i = 0; i < set->used; ) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5179 | switch (set->val.nodes[i].type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 5180 | case LYXP_NODE_NONE: |
| 5181 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5182 | case LYXP_NODE_ELEM: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5183 | if (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 5184 | set->val.nodes[i].type = LYXP_NODE_TEXT; |
| 5185 | ++i; |
| 5186 | break; |
| 5187 | } |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 5188 | /* fall through */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5189 | case LYXP_NODE_ROOT: |
| 5190 | case LYXP_NODE_ROOT_CONFIG: |
| 5191 | case LYXP_NODE_TEXT: |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5192 | case LYXP_NODE_META: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5193 | set_remove_node(set, i); |
| 5194 | break; |
| 5195 | } |
| 5196 | } |
| 5197 | |
| 5198 | return LY_SUCCESS; |
| 5199 | } |
| 5200 | |
| 5201 | /** |
| 5202 | * @brief Execute the XPath translate(string, string, string) function. |
| 5203 | * Returns LYXP_SET_STRING with the first argument with the characters |
| 5204 | * from the second argument replaced by those on the corresponding |
| 5205 | * positions in the third argument. |
| 5206 | * |
| 5207 | * @param[in] args Array of arguments. |
| 5208 | * @param[in] arg_count Count of elements in @p args. |
| 5209 | * @param[in,out] set Context and result set at the same time. |
| 5210 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5211 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5212 | */ |
| 5213 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5214 | xpath_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] | 5215 | { |
| 5216 | uint16_t i, j, new_used; |
| 5217 | char *new; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 5218 | ly_bool have_removed; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5219 | struct lysc_node_leaf *sleaf; |
| 5220 | LY_ERR rc = LY_SUCCESS; |
| 5221 | |
| 5222 | if (options & LYXP_SCNODE_ALL) { |
| 5223 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 5224 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5225 | 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] | 5226 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5227 | 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] | 5228 | } |
| 5229 | } |
| 5230 | |
| 5231 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 5232 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5233 | 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] | 5234 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5235 | 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] | 5236 | } |
| 5237 | } |
| 5238 | |
| 5239 | if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) { |
| 5240 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5241 | 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] | 5242 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5243 | 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] | 5244 | } |
| 5245 | } |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5246 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5247 | return rc; |
| 5248 | } |
| 5249 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5250 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5251 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5252 | rc = lyxp_set_cast(args[1], 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[2], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5255 | LY_CHECK_RET(rc); |
| 5256 | |
| 5257 | new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char)); |
| 5258 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 5259 | new_used = 0; |
| 5260 | |
| 5261 | have_removed = 0; |
| 5262 | for (i = 0; args[0]->val.str[i]; ++i) { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 5263 | ly_bool found = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5264 | |
| 5265 | for (j = 0; args[1]->val.str[j]; ++j) { |
| 5266 | if (args[0]->val.str[i] == args[1]->val.str[j]) { |
| 5267 | /* removing this char */ |
| 5268 | if (j >= strlen(args[2]->val.str)) { |
| 5269 | have_removed = 1; |
| 5270 | found = 1; |
| 5271 | break; |
| 5272 | } |
| 5273 | /* replacing this char */ |
| 5274 | new[new_used] = args[2]->val.str[j]; |
| 5275 | ++new_used; |
| 5276 | found = 1; |
| 5277 | break; |
| 5278 | } |
| 5279 | } |
| 5280 | |
| 5281 | /* copying this char */ |
| 5282 | if (!found) { |
| 5283 | new[new_used] = args[0]->val.str[i]; |
| 5284 | ++new_used; |
| 5285 | } |
| 5286 | } |
| 5287 | |
| 5288 | if (have_removed) { |
| 5289 | new = ly_realloc(new, (new_used + 1) * sizeof(char)); |
| 5290 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 5291 | } |
| 5292 | new[new_used] = '\0'; |
| 5293 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5294 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5295 | set->type = LYXP_SET_STRING; |
| 5296 | set->val.str = new; |
| 5297 | |
| 5298 | return LY_SUCCESS; |
| 5299 | } |
| 5300 | |
| 5301 | /** |
| 5302 | * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN |
| 5303 | * with true value. |
| 5304 | * |
| 5305 | * @param[in] args Array of arguments. |
| 5306 | * @param[in] arg_count Count of elements in @p args. |
| 5307 | * @param[in,out] set Context and result set at the same time. |
| 5308 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5309 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5310 | */ |
| 5311 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5312 | 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] | 5313 | { |
| 5314 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5315 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5316 | return LY_SUCCESS; |
| 5317 | } |
| 5318 | |
| 5319 | set_fill_boolean(set, 1); |
| 5320 | return LY_SUCCESS; |
| 5321 | } |
| 5322 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5323 | /** |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5324 | * @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] | 5325 | * |
Michal Vasko | 2104e9f | 2020-03-06 08:23:25 +0100 | [diff] [blame] | 5326 | * XPath @p set is expected to be a (sc)node set! |
| 5327 | * |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5328 | * @param[in,out] qname Qualified node name. If includes prefix, it is skipped. |
| 5329 | * @param[in,out] qname_len Length of @p qname, is updated accordingly. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5330 | * @param[in] set Set with general XPath context. |
| 5331 | * @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] | 5332 | * @param[out] moveto_mod Expected module of a matching node. |
| 5333 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5334 | */ |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5335 | static LY_ERR |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5336 | moveto_resolve_model(const char **qname, uint16_t *qname_len, const struct lyxp_set *set, |
| 5337 | const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5338 | { |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 5339 | const struct lys_module *mod = NULL; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5340 | const char *ptr; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5341 | size_t pref_len; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5342 | |
Michal Vasko | 2104e9f | 2020-03-06 08:23:25 +0100 | [diff] [blame] | 5343 | assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET)); |
| 5344 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5345 | if ((ptr = ly_strnchr(*qname, ':', *qname_len))) { |
| 5346 | /* specific module */ |
| 5347 | pref_len = ptr - *qname; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5348 | 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] | 5349 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5350 | /* check for errors and non-implemented modules, as they are not valid */ |
Juraj Vijtiuk | d75faa6 | 2019-11-26 14:10:10 +0100 | [diff] [blame] | 5351 | if (!mod || !mod->implemented) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 5352 | LOGVAL(set->ctx, LY_VCODE_XP_INMOD, pref_len, *qname); |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5353 | return LY_EVALID; |
| 5354 | } |
Juraj Vijtiuk | d75faa6 | 2019-11-26 14:10:10 +0100 | [diff] [blame] | 5355 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5356 | *qname += pref_len + 1; |
| 5357 | *qname_len -= pref_len + 1; |
| 5358 | } else if (((*qname)[0] == '*') && (*qname_len == 1)) { |
| 5359 | /* all modules - special case */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5360 | mod = NULL; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5361 | } else { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5362 | switch (set->format) { |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 5363 | case LY_VALUE_SCHEMA: |
| 5364 | case LY_VALUE_SCHEMA_RESOLVED: |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5365 | /* current module */ |
| 5366 | mod = set->cur_mod; |
| 5367 | break; |
Radek Krejci | 224d4b4 | 2021-04-23 13:54:59 +0200 | [diff] [blame] | 5368 | case LY_VALUE_CANON: |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 5369 | case LY_VALUE_JSON: |
Radek Krejci | f994364 | 2021-04-26 10:18:21 +0200 | [diff] [blame] | 5370 | case LY_VALUE_LYB: |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5371 | /* inherit parent (context node) module */ |
| 5372 | if (ctx_scnode) { |
| 5373 | mod = ctx_scnode->module; |
| 5374 | } else { |
| 5375 | mod = NULL; |
| 5376 | } |
| 5377 | break; |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 5378 | case LY_VALUE_XML: |
Michal Vasko | 52143d1 | 2021-04-14 15:36:39 +0200 | [diff] [blame] | 5379 | /* all nodes need to be prefixed */ |
| 5380 | LOGVAL(set->ctx, LYVE_DATA, "Non-prefixed node \"%.*s\" in XML xpath found.", *qname_len, *qname); |
| 5381 | return LY_EVALID; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5382 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5383 | } |
| 5384 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5385 | *moveto_mod = mod; |
| 5386 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5387 | } |
| 5388 | |
| 5389 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5390 | * @brief Move context @p set to the root. Handles absolute path. |
| 5391 | * Result is LYXP_SET_NODE_SET. |
| 5392 | * |
| 5393 | * @param[in,out] set Set to use. |
| 5394 | * @param[in] options Xpath options. |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 5395 | * @return LY_ERR value. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5396 | */ |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 5397 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5398 | moveto_root(struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5399 | { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 5400 | assert(!(options & LYXP_SKIP_EXPR)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5401 | |
| 5402 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5403 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 5404 | 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] | 5405 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5406 | set->type = LYXP_SET_NODE_SET; |
| 5407 | set->used = 0; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5408 | set_insert_node(set, NULL, 0, set->root_type, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5409 | } |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 5410 | |
| 5411 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5412 | } |
| 5413 | |
| 5414 | /** |
| 5415 | * @brief Check @p node as a part of NameTest processing. |
| 5416 | * |
| 5417 | * @param[in] node Node to check. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5418 | * @param[in] set Set to read general context from. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5419 | * @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] | 5420 | * @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] | 5421 | * @param[in] options XPath options. |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5422 | * @return LY_ERR (LY_ENOT if node does not match, LY_EINCOMPLETE on unresolved when, |
| 5423 | * LY_EINVAL if netither node nor any children match) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5424 | */ |
| 5425 | static LY_ERR |
Michal Vasko | db08ce5 | 2021-10-06 08:57:49 +0200 | [diff] [blame] | 5426 | moveto_node_check(const struct lyd_node *node, const struct lyxp_set *set, const char *node_name, |
| 5427 | const struct lys_module *moveto_mod, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5428 | { |
Michal Vasko | dca9f12 | 2021-07-16 13:56:22 +0200 | [diff] [blame] | 5429 | if (!node->schema) { |
| 5430 | /* opaque node never matches */ |
| 5431 | return LY_ENOT; |
| 5432 | } |
| 5433 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5434 | /* module check */ |
| 5435 | if (moveto_mod && (node->schema->module != moveto_mod)) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5436 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5437 | } |
| 5438 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5439 | /* context check */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5440 | 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] | 5441 | return LY_EINVAL; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5442 | } else if (set->context_op && (node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && |
| 5443 | (node->schema != set->context_op)) { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5444 | return LY_EINVAL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5445 | } |
| 5446 | |
| 5447 | /* name check */ |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5448 | if (node_name && strcmp(node_name, "*") && (node->schema->name != node_name)) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5449 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5450 | } |
| 5451 | |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 5452 | /* when check */ |
Michal Vasko | d5cfa6e | 2020-11-23 16:56:08 +0100 | [diff] [blame] | 5453 | 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] | 5454 | return LY_EINCOMPLETE; |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 5455 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5456 | |
| 5457 | /* match */ |
| 5458 | return LY_SUCCESS; |
| 5459 | } |
| 5460 | |
| 5461 | /** |
| 5462 | * @brief Check @p node as a part of schema NameTest processing. |
| 5463 | * |
| 5464 | * @param[in] node Schema node to check. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5465 | * @param[in] ctx_scnode Context node. |
| 5466 | * @param[in] set Set to read general context from. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5467 | * @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] | 5468 | * @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] | 5469 | * @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] | 5470 | */ |
| 5471 | static LY_ERR |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5472 | 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] | 5473 | const char *node_name, const struct lys_module *moveto_mod) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5474 | { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5475 | if (!moveto_mod && (!node_name || strcmp(node_name, "*"))) { |
| 5476 | switch (set->format) { |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 5477 | case LY_VALUE_SCHEMA: |
| 5478 | case LY_VALUE_SCHEMA_RESOLVED: |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5479 | /* use current module */ |
| 5480 | moveto_mod = set->cur_mod; |
| 5481 | break; |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 5482 | case LY_VALUE_JSON: |
Radek Krejci | f994364 | 2021-04-26 10:18:21 +0200 | [diff] [blame] | 5483 | case LY_VALUE_LYB: |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5484 | /* inherit module of the context node, if any */ |
| 5485 | if (ctx_scnode) { |
| 5486 | moveto_mod = ctx_scnode->module; |
| 5487 | } |
| 5488 | break; |
Radek Krejci | 224d4b4 | 2021-04-23 13:54:59 +0200 | [diff] [blame] | 5489 | case LY_VALUE_CANON: |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 5490 | case LY_VALUE_XML: |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5491 | /* not defined */ |
| 5492 | LOGINT(set->ctx); |
| 5493 | return LY_EINVAL; |
| 5494 | } |
| 5495 | } |
| 5496 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5497 | /* module check */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5498 | if (moveto_mod && (node->module != moveto_mod)) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5499 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5500 | } |
| 5501 | |
| 5502 | /* context check */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5503 | 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] | 5504 | return LY_EINVAL; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5505 | } 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] | 5506 | return LY_EINVAL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5507 | } |
| 5508 | |
| 5509 | /* name check */ |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5510 | if (node_name && strcmp(node_name, "*") && (node->name != node_name)) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5511 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5512 | } |
| 5513 | |
| 5514 | /* match */ |
| 5515 | return LY_SUCCESS; |
| 5516 | } |
| 5517 | |
| 5518 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5519 | * @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] | 5520 | * |
| 5521 | * @param[in,out] set Set to use. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5522 | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5523 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 5524 | * @param[in] options XPath options. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5525 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 5526 | */ |
| 5527 | static LY_ERR |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 5528 | 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] | 5529 | { |
Michal Vasko | 9d41411 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 5530 | LY_ERR r, rc = LY_SUCCESS; |
Michal Vasko | db08ce5 | 2021-10-06 08:57:49 +0200 | [diff] [blame] | 5531 | const struct lyd_node *siblings, *sub; |
Michal Vasko | 9d41411 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 5532 | struct lyxp_set result; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5533 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 5534 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5535 | return LY_SUCCESS; |
| 5536 | } |
| 5537 | |
| 5538 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 5539 | 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] | 5540 | return LY_EVALID; |
| 5541 | } |
| 5542 | |
Michal Vasko | 9d41411 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 5543 | /* init result set */ |
| 5544 | set_init(&result, set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5545 | |
Michal Vasko | 9d41411 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 5546 | for (uint32_t i = 0; i < set->used; ++i) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5547 | 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] | 5548 | assert(!set->val.nodes[i].node); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5549 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5550 | /* search in all the trees */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5551 | siblings = set->tree; |
Michal Vasko | 5bfd4be | 2020-06-23 13:26:19 +0200 | [diff] [blame] | 5552 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5553 | /* search in children */ |
Michal Vasko | db08ce5 | 2021-10-06 08:57:49 +0200 | [diff] [blame] | 5554 | siblings = lyd_child(set->val.nodes[i].node); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5555 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5556 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5557 | for (sub = siblings; sub; sub = sub->next) { |
Michal Vasko | 9d41411 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 5558 | r = moveto_node_check(sub, set, ncname, moveto_mod, options); |
| 5559 | if (r == LY_SUCCESS) { |
| 5560 | /* matching node */ |
| 5561 | set_insert_node(&result, sub, 0, LYXP_NODE_ELEM, result.used); |
| 5562 | } else if (r == LY_EINCOMPLETE) { |
| 5563 | rc = r; |
| 5564 | goto cleanup; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5565 | } |
| 5566 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5567 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5568 | |
Michal Vasko | 9d41411 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 5569 | /* move result to the set */ |
| 5570 | lyxp_set_free_content(set); |
| 5571 | *set = result; |
| 5572 | result.type = LYXP_SET_NUMBER; |
| 5573 | assert(!set_sort(set)); |
| 5574 | |
| 5575 | cleanup: |
| 5576 | lyxp_set_free_content(&result); |
| 5577 | return rc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5578 | } |
| 5579 | |
| 5580 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5581 | * @brief Move context @p set to a node using hashes. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY). |
| 5582 | * Context position aware. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5583 | * |
| 5584 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5585 | * @param[in] scnode Matching node schema. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5586 | * @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] | 5587 | * @param[in] options XPath options. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5588 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 5589 | */ |
| 5590 | static LY_ERR |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 5591 | moveto_node_hash(struct lyxp_set *set, const struct lysc_node *scnode, const struct ly_path_predicate *predicates, |
| 5592 | uint32_t options) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5593 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5594 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5595 | uint32_t i; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5596 | const struct lyd_node *siblings; |
Michal Vasko | 9d41411 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 5597 | struct lyxp_set result; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5598 | struct lyd_node *sub, *inst = NULL; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5599 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5600 | assert(scnode && (!(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) || predicates)); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5601 | |
Michal Vasko | a5af776 | 2021-12-02 13:11:56 +0100 | [diff] [blame] | 5602 | /* init result set */ |
| 5603 | set_init(&result, set); |
| 5604 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 5605 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5606 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5607 | } |
| 5608 | |
| 5609 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 5610 | 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] | 5611 | ret = LY_EVALID; |
| 5612 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5613 | } |
| 5614 | |
| 5615 | /* context check for all the nodes since we have the schema node */ |
| 5616 | if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) { |
| 5617 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5618 | goto cleanup; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 5619 | } else if (set->context_op && (scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && |
| 5620 | (scnode != set->context_op)) { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5621 | lyxp_set_free_content(set); |
| 5622 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5623 | } |
| 5624 | |
| 5625 | /* create specific data instance if needed */ |
| 5626 | if (scnode->nodetype == LYS_LIST) { |
| 5627 | LY_CHECK_GOTO(ret = lyd_create_list(scnode, predicates, &inst), cleanup); |
| 5628 | } else if (scnode->nodetype == LYS_LEAFLIST) { |
| 5629 | 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] | 5630 | } |
| 5631 | |
Michal Vasko | 9d41411 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 5632 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5633 | siblings = NULL; |
| 5634 | |
| 5635 | if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) { |
| 5636 | assert(!set->val.nodes[i].node); |
| 5637 | |
| 5638 | /* search in all the trees */ |
| 5639 | siblings = set->tree; |
| 5640 | } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
| 5641 | /* search in children */ |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 5642 | siblings = lyd_child(set->val.nodes[i].node); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5643 | } |
| 5644 | |
| 5645 | /* find the node using hashes */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5646 | if (inst) { |
| 5647 | lyd_find_sibling_first(siblings, inst, &sub); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5648 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5649 | lyd_find_sibling_val(siblings, scnode, NULL, 0, &sub); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5650 | } |
| 5651 | |
| 5652 | /* when check */ |
Michal Vasko | d5cfa6e | 2020-11-23 16:56:08 +0100 | [diff] [blame] | 5653 | 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] | 5654 | ret = LY_EINCOMPLETE; |
| 5655 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5656 | } |
| 5657 | |
| 5658 | if (sub) { |
| 5659 | /* pos filled later */ |
Michal Vasko | 9d41411 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 5660 | set_insert_node(&result, sub, 0, LYXP_NODE_ELEM, result.used); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5661 | } |
| 5662 | } |
| 5663 | |
Michal Vasko | 9d41411 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 5664 | /* move result to the set */ |
| 5665 | lyxp_set_free_content(set); |
| 5666 | *set = result; |
| 5667 | result.type = LYXP_SET_NUMBER; |
| 5668 | assert(!set_sort(set)); |
| 5669 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5670 | cleanup: |
Michal Vasko | 9d41411 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 5671 | lyxp_set_free_content(&result); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5672 | lyd_free_tree(inst); |
| 5673 | return ret; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5674 | } |
| 5675 | |
| 5676 | /** |
| 5677 | * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY). |
| 5678 | * |
| 5679 | * @param[in,out] set Set to use. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5680 | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5681 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5682 | * @param[in] options XPath options. |
| 5683 | * @return LY_ERR |
| 5684 | */ |
| 5685 | static LY_ERR |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5686 | 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] | 5687 | { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 5688 | ly_bool temp_ctx = 0; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5689 | uint32_t getnext_opts; |
| 5690 | uint32_t orig_used, i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5691 | uint32_t mod_idx; |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 5692 | const struct lysc_node *iter, *start_parent; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5693 | const struct lys_module *mod; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5694 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 5695 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5696 | return LY_SUCCESS; |
| 5697 | } |
| 5698 | |
| 5699 | if (set->type != LYXP_SET_SCNODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 5700 | 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] | 5701 | return LY_EVALID; |
| 5702 | } |
| 5703 | |
Michal Vasko | cafad9d | 2019-11-07 15:20:03 +0100 | [diff] [blame] | 5704 | /* getnext opts */ |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 5705 | getnext_opts = 0; |
Michal Vasko | cafad9d | 2019-11-07 15:20:03 +0100 | [diff] [blame] | 5706 | if (options & LYXP_SCNODE_OUTPUT) { |
| 5707 | getnext_opts |= LYS_GETNEXT_OUTPUT; |
| 5708 | } |
| 5709 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5710 | orig_used = set->used; |
| 5711 | for (i = 0; i < orig_used; ++i) { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 5712 | uint32_t idx; |
| 5713 | |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 5714 | if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) { |
| 5715 | if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5716 | continue; |
| 5717 | } |
| 5718 | |
| 5719 | /* remember context node */ |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 5720 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED; |
Michal Vasko | ec4df48 | 2019-12-16 10:02:18 +0100 | [diff] [blame] | 5721 | } else { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5722 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5723 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5724 | |
| 5725 | start_parent = set->val.scnodes[i].scnode; |
| 5726 | |
| 5727 | 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] | 5728 | /* 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] | 5729 | * 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] | 5730 | mod_idx = 0; |
Michal Vasko | a51ef07 | 2021-07-02 10:40:30 +0200 | [diff] [blame] | 5731 | while ((mod = ly_ctx_get_module_iter(set->ctx, &mod_idx))) { |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 5732 | iter = NULL; |
Michal Vasko | 919954a | 2021-07-26 09:21:42 +0200 | [diff] [blame] | 5733 | /* module may not be implemented or not compiled yet */ |
| 5734 | while (mod->compiled && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5735 | if (!moveto_scnode_check(iter, NULL, set, ncname, moveto_mod)) { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 5736 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx)); |
| 5737 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5738 | /* we need to prevent these nodes from being considered in this moveto */ |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 5739 | if ((idx < orig_used) && (idx > i)) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 5740 | set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5741 | temp_ctx = 1; |
| 5742 | } |
| 5743 | } |
| 5744 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5745 | } |
| 5746 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 5747 | } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) { |
| 5748 | iter = NULL; |
| 5749 | while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5750 | if (!moveto_scnode_check(iter, start_parent, set, ncname, moveto_mod)) { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 5751 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx)); |
| 5752 | |
| 5753 | if ((idx < orig_used) && (idx > i)) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 5754 | set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5755 | temp_ctx = 1; |
| 5756 | } |
| 5757 | } |
| 5758 | } |
| 5759 | } |
| 5760 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5761 | |
| 5762 | /* correct temporary in_ctx values */ |
| 5763 | if (temp_ctx) { |
| 5764 | for (i = 0; i < orig_used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 5765 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NEW_CTX) { |
| 5766 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5767 | } |
| 5768 | } |
| 5769 | } |
| 5770 | |
| 5771 | return LY_SUCCESS; |
| 5772 | } |
| 5773 | |
| 5774 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5775 | * @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] | 5776 | * Context position aware. |
| 5777 | * |
| 5778 | * @param[in] set Set to use. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5779 | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5780 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 5781 | * @param[in] options XPath options. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5782 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 5783 | */ |
| 5784 | static LY_ERR |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 5785 | 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] | 5786 | { |
| 5787 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5788 | const struct lyd_node *next, *elem, *start; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5789 | struct lyxp_set ret_set; |
| 5790 | LY_ERR rc; |
| 5791 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 5792 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5793 | return LY_SUCCESS; |
| 5794 | } |
| 5795 | |
| 5796 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 5797 | 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] | 5798 | return LY_EVALID; |
| 5799 | } |
| 5800 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5801 | /* 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] | 5802 | rc = moveto_node(set, NULL, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5803 | LY_CHECK_RET(rc); |
| 5804 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5805 | /* 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] | 5806 | set_init(&ret_set, set); |
| 5807 | for (i = 0; i < set->used; ++i) { |
| 5808 | |
| 5809 | /* TREE DFS */ |
| 5810 | start = set->val.nodes[i].node; |
| 5811 | for (elem = next = start; elem; elem = next) { |
Michal Vasko | db08ce5 | 2021-10-06 08:57:49 +0200 | [diff] [blame] | 5812 | rc = moveto_node_check(elem, set, ncname, moveto_mod, options); |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5813 | if (!rc) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5814 | /* add matching node into result set */ |
| 5815 | set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used); |
| 5816 | if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) { |
| 5817 | /* the node is a duplicate, we'll process it later in the set */ |
| 5818 | goto skip_children; |
| 5819 | } |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5820 | } else if (rc == LY_EINCOMPLETE) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5821 | return rc; |
| 5822 | } else if (rc == LY_EINVAL) { |
| 5823 | goto skip_children; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5824 | } |
| 5825 | |
| 5826 | /* TREE DFS NEXT ELEM */ |
| 5827 | /* select element for the next run - children first */ |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 5828 | next = lyd_child(elem); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5829 | if (!next) { |
| 5830 | skip_children: |
| 5831 | /* no children, so try siblings, but only if it's not the start, |
| 5832 | * that is considered to be the root and it's siblings are not traversed */ |
| 5833 | if (elem != start) { |
| 5834 | next = elem->next; |
| 5835 | } else { |
| 5836 | break; |
| 5837 | } |
| 5838 | } |
| 5839 | while (!next) { |
| 5840 | /* no siblings, go back through the parents */ |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 5841 | if (lyd_parent(elem) == start) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5842 | /* we are done, no next element to process */ |
| 5843 | break; |
| 5844 | } |
| 5845 | /* parent is already processed, go to its sibling */ |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 5846 | elem = lyd_parent(elem); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5847 | next = elem->next; |
| 5848 | } |
| 5849 | } |
| 5850 | } |
| 5851 | |
| 5852 | /* make the temporary set the current one */ |
| 5853 | ret_set.ctx_pos = set->ctx_pos; |
| 5854 | ret_set.ctx_size = set->ctx_size; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5855 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5856 | memcpy(set, &ret_set, sizeof *set); |
| 5857 | |
| 5858 | return LY_SUCCESS; |
| 5859 | } |
| 5860 | |
| 5861 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5862 | * @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] | 5863 | * |
| 5864 | * @param[in] set Set to use. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5865 | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5866 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5867 | * @param[in] options XPath options. |
| 5868 | * @return LY_ERR |
| 5869 | */ |
| 5870 | static LY_ERR |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5871 | 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] | 5872 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5873 | uint32_t i, orig_used; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5874 | const struct lysc_node *next, *elem, *start; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5875 | LY_ERR rc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5876 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 5877 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5878 | return LY_SUCCESS; |
| 5879 | } |
| 5880 | |
| 5881 | if (set->type != LYXP_SET_SCNODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 5882 | 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] | 5883 | return LY_EVALID; |
| 5884 | } |
| 5885 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5886 | orig_used = set->used; |
| 5887 | for (i = 0; i < orig_used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 5888 | if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) { |
| 5889 | if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5890 | continue; |
| 5891 | } |
| 5892 | |
| 5893 | /* remember context node */ |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 5894 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED; |
Michal Vasko | ec4df48 | 2019-12-16 10:02:18 +0100 | [diff] [blame] | 5895 | } else { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 5896 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5897 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5898 | |
| 5899 | /* TREE DFS */ |
| 5900 | start = set->val.scnodes[i].scnode; |
| 5901 | for (elem = next = start; elem; elem = next) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5902 | if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) { |
| 5903 | /* schema-only nodes, skip root */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5904 | goto next_iter; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5905 | } |
| 5906 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5907 | rc = moveto_scnode_check(elem, start, set, ncname, moveto_mod); |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5908 | if (!rc) { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 5909 | uint32_t idx; |
| 5910 | |
| 5911 | if (lyxp_set_scnode_contains(set, elem, LYXP_NODE_ELEM, i, &idx)) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 5912 | set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5913 | if ((uint32_t)idx > i) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5914 | /* we will process it later in the set */ |
| 5915 | goto skip_children; |
| 5916 | } |
| 5917 | } else { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 5918 | 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] | 5919 | } |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5920 | } else if (rc == LY_EINVAL) { |
| 5921 | goto skip_children; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5922 | } |
| 5923 | |
| 5924 | next_iter: |
| 5925 | /* TREE DFS NEXT ELEM */ |
| 5926 | /* select element for the next run - children first */ |
Michal Vasko | 544e58a | 2021-01-28 14:33:41 +0100 | [diff] [blame] | 5927 | next = lysc_node_child(elem); |
| 5928 | if (next && (next->nodetype == LYS_INPUT) && (options & LYXP_SCNODE_OUTPUT)) { |
| 5929 | next = next->next; |
| 5930 | } else if (next && (next->nodetype == LYS_OUTPUT) && !(options & LYXP_SCNODE_OUTPUT)) { |
| 5931 | next = next->next; |
| 5932 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5933 | if (!next) { |
| 5934 | skip_children: |
| 5935 | /* no children, so try siblings, but only if it's not the start, |
| 5936 | * that is considered to be the root and it's siblings are not traversed */ |
| 5937 | if (elem != start) { |
| 5938 | next = elem->next; |
| 5939 | } else { |
| 5940 | break; |
| 5941 | } |
| 5942 | } |
| 5943 | while (!next) { |
| 5944 | /* no siblings, go back through the parents */ |
| 5945 | if (elem->parent == start) { |
| 5946 | /* we are done, no next element to process */ |
| 5947 | break; |
| 5948 | } |
| 5949 | /* parent is already processed, go to its sibling */ |
| 5950 | elem = elem->parent; |
| 5951 | next = elem->next; |
| 5952 | } |
| 5953 | } |
| 5954 | } |
| 5955 | |
| 5956 | return LY_SUCCESS; |
| 5957 | } |
| 5958 | |
| 5959 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5960 | * @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] | 5961 | * Indirectly context position aware. |
| 5962 | * |
| 5963 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5964 | * @param[in] mod Matching metadata module, NULL for any. |
| 5965 | * @param[in] ncname Matching metadata name in the dictionary, NULL for any. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 5966 | * @param[in] options XPath options. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5967 | * @return LY_ERR |
| 5968 | */ |
| 5969 | static LY_ERR |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 5970 | 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] | 5971 | { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5972 | struct lyd_meta *sub; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5973 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 5974 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5975 | return LY_SUCCESS; |
| 5976 | } |
| 5977 | |
| 5978 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 5979 | 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] | 5980 | return LY_EVALID; |
| 5981 | } |
| 5982 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5983 | for (uint32_t i = 0; i < set->used; ) { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 5984 | ly_bool replaced = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5985 | |
| 5986 | /* only attributes of an elem (not dummy) can be in the result, skip all the rest; |
| 5987 | * our attributes are always qualified */ |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5988 | if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5989 | for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5990 | |
| 5991 | /* check "namespace" */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5992 | if (mod && (sub->annotation->module != mod)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5993 | continue; |
| 5994 | } |
| 5995 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5996 | if (!ncname || (sub->name == ncname)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5997 | /* match */ |
| 5998 | if (!replaced) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5999 | set->val.meta[i].meta = sub; |
| 6000 | set->val.meta[i].type = LYXP_NODE_META; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6001 | /* pos does not change */ |
| 6002 | replaced = 1; |
| 6003 | } else { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6004 | 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] | 6005 | } |
| 6006 | ++i; |
| 6007 | } |
| 6008 | } |
| 6009 | } |
| 6010 | |
| 6011 | if (!replaced) { |
| 6012 | /* no match */ |
| 6013 | set_remove_node(set, i); |
| 6014 | } |
| 6015 | } |
| 6016 | |
| 6017 | return LY_SUCCESS; |
| 6018 | } |
| 6019 | |
| 6020 | /** |
| 6021 | * @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] | 6022 | * Result is LYXP_SET_NODE_SET. Context position aware. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6023 | * |
| 6024 | * @param[in,out] set1 Set to use for the result. |
| 6025 | * @param[in] set2 Set that is copied to @p set1. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6026 | * @return LY_ERR |
| 6027 | */ |
| 6028 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6029 | moveto_union(struct lyxp_set *set1, struct lyxp_set *set2) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6030 | { |
| 6031 | LY_ERR rc; |
| 6032 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6033 | 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] | 6034 | 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] | 6035 | return LY_EVALID; |
| 6036 | } |
| 6037 | |
| 6038 | /* set2 is empty or both set1 and set2 */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6039 | if (!set2->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6040 | return LY_SUCCESS; |
| 6041 | } |
| 6042 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6043 | if (!set1->used) { |
aPiecek | adc1e4f | 2021-10-07 11:15:12 +0200 | [diff] [blame] | 6044 | /* release hidden allocated data (lyxp_set.size) */ |
| 6045 | lyxp_set_free_content(set1); |
| 6046 | /* direct copying of the entire structure */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6047 | memcpy(set1, set2, sizeof *set1); |
| 6048 | /* dynamic memory belongs to set1 now, do not free */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6049 | memset(set2, 0, sizeof *set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6050 | return LY_SUCCESS; |
| 6051 | } |
| 6052 | |
| 6053 | /* we assume sets are sorted */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6054 | assert(!set_sort(set1) && !set_sort(set2)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6055 | |
| 6056 | /* sort, remove duplicates */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6057 | rc = set_sorted_merge(set1, set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6058 | LY_CHECK_RET(rc); |
| 6059 | |
| 6060 | /* final set must be sorted */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6061 | assert(!set_sort(set1)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6062 | |
| 6063 | return LY_SUCCESS; |
| 6064 | } |
| 6065 | |
| 6066 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6067 | * @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] | 6068 | * Context position aware. |
| 6069 | * |
| 6070 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6071 | * @param[in] mod Matching metadata module, NULL for any. |
| 6072 | * @param[in] ncname Matching metadata name in the dictionary, NULL for any. |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 6073 | * @param[in] options XPath options. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6074 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6075 | */ |
| 6076 | static int |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 6077 | 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] | 6078 | { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6079 | struct lyd_meta *sub; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6080 | struct lyxp_set *set_all_desc = NULL; |
| 6081 | LY_ERR rc; |
| 6082 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6083 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6084 | return LY_SUCCESS; |
| 6085 | } |
| 6086 | |
| 6087 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 6088 | 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] | 6089 | return LY_EVALID; |
| 6090 | } |
| 6091 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6092 | /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory, |
| 6093 | * but it likely won't be used much, so it's a waste of time */ |
| 6094 | /* copy the context */ |
| 6095 | set_all_desc = set_copy(set); |
| 6096 | /* get all descendant nodes (the original context nodes are removed) */ |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 6097 | rc = moveto_node_alldesc(set_all_desc, NULL, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6098 | if (rc != LY_SUCCESS) { |
| 6099 | lyxp_set_free(set_all_desc); |
| 6100 | return rc; |
| 6101 | } |
| 6102 | /* prepend the original context nodes */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6103 | rc = moveto_union(set, set_all_desc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6104 | if (rc != LY_SUCCESS) { |
| 6105 | lyxp_set_free(set_all_desc); |
| 6106 | return rc; |
| 6107 | } |
| 6108 | lyxp_set_free(set_all_desc); |
| 6109 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6110 | for (uint32_t i = 0; i < set->used; ) { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6111 | ly_bool replaced = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6112 | |
| 6113 | /* only attributes of an elem can be in the result, skip all the rest, |
| 6114 | * we have all attributes qualified in lyd tree */ |
| 6115 | if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6116 | for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6117 | /* check "namespace" */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6118 | if (mod && (sub->annotation->module != mod)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6119 | continue; |
| 6120 | } |
| 6121 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6122 | if (!ncname || (sub->name == ncname)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6123 | /* match */ |
| 6124 | if (!replaced) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6125 | set->val.meta[i].meta = sub; |
| 6126 | set->val.meta[i].type = LYXP_NODE_META; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6127 | /* pos does not change */ |
| 6128 | replaced = 1; |
| 6129 | } else { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6130 | 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] | 6131 | } |
| 6132 | ++i; |
| 6133 | } |
| 6134 | } |
| 6135 | } |
| 6136 | |
| 6137 | if (!replaced) { |
| 6138 | /* no match */ |
| 6139 | set_remove_node(set, i); |
| 6140 | } |
| 6141 | } |
| 6142 | |
| 6143 | return LY_SUCCESS; |
| 6144 | } |
| 6145 | |
| 6146 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6147 | * @brief Move context @p set to self and al chilren, recursively. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET. |
| 6148 | * Context position aware. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6149 | * |
| 6150 | * @param[in] parent Current parent. |
| 6151 | * @param[in] parent_pos Position of @p parent. |
| 6152 | * @param[in] parent_type Node type of @p parent. |
| 6153 | * @param[in,out] to_set Set to use. |
| 6154 | * @param[in] dup_check_set Set for checking duplicities. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6155 | * @param[in] options XPath options. |
| 6156 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6157 | */ |
| 6158 | static LY_ERR |
| 6159 | 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] | 6160 | 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] | 6161 | { |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6162 | const struct lyd_node *iter, *first; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6163 | LY_ERR rc; |
| 6164 | |
| 6165 | switch (parent_type) { |
| 6166 | case LYXP_NODE_ROOT: |
| 6167 | case LYXP_NODE_ROOT_CONFIG: |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6168 | assert(!parent); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6169 | |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6170 | /* add all top-level nodes as elements */ |
| 6171 | first = to_set->tree; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6172 | break; |
| 6173 | case LYXP_NODE_ELEM: |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6174 | /* add just the text node of this term element node */ |
| 6175 | if (parent->schema->nodetype & (LYD_NODE_TERM | LYD_NODE_ANY)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6176 | if (!set_dup_node_check(dup_check_set, parent, LYXP_NODE_TEXT, -1)) { |
| 6177 | set_insert_node(to_set, parent, parent_pos, LYXP_NODE_TEXT, to_set->used); |
| 6178 | } |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6179 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6180 | } |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6181 | |
| 6182 | /* add all the children of this node */ |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 6183 | first = lyd_child(parent); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6184 | break; |
| 6185 | default: |
| 6186 | LOGINT_RET(parent->schema->module->ctx); |
| 6187 | } |
| 6188 | |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6189 | /* add all top-level nodes as elements */ |
| 6190 | LY_LIST_FOR(first, iter) { |
| 6191 | /* context check */ |
| 6192 | if ((parent_type == LYXP_NODE_ROOT_CONFIG) && (iter->schema->flags & LYS_CONFIG_R)) { |
| 6193 | continue; |
| 6194 | } |
| 6195 | |
| 6196 | /* when check */ |
Michal Vasko | d5cfa6e | 2020-11-23 16:56:08 +0100 | [diff] [blame] | 6197 | 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] | 6198 | return LY_EINCOMPLETE; |
| 6199 | } |
| 6200 | |
| 6201 | if (!set_dup_node_check(dup_check_set, iter, LYXP_NODE_ELEM, -1)) { |
| 6202 | set_insert_node(to_set, iter, 0, LYXP_NODE_ELEM, to_set->used); |
| 6203 | |
| 6204 | /* also add all the children of this node, recursively */ |
| 6205 | rc = moveto_self_add_children_r(iter, 0, LYXP_NODE_ELEM, to_set, dup_check_set, options); |
| 6206 | LY_CHECK_RET(rc); |
| 6207 | } |
| 6208 | } |
| 6209 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6210 | return LY_SUCCESS; |
| 6211 | } |
| 6212 | |
| 6213 | /** |
| 6214 | * @brief Move context @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET |
| 6215 | * (or LYXP_SET_EMPTY). Context position aware. |
| 6216 | * |
| 6217 | * @param[in,out] set Set to use. |
| 6218 | * @param[in] all_desc Whether to go to all descendants ('//') or not ('/'). |
| 6219 | * @param[in] options XPath options. |
| 6220 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6221 | */ |
| 6222 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6223 | 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] | 6224 | { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6225 | struct lyxp_set ret_set; |
| 6226 | LY_ERR rc; |
| 6227 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6228 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6229 | return LY_SUCCESS; |
| 6230 | } |
| 6231 | |
| 6232 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 6233 | 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] | 6234 | return LY_EVALID; |
| 6235 | } |
| 6236 | |
| 6237 | /* nothing to do */ |
| 6238 | if (!all_desc) { |
| 6239 | return LY_SUCCESS; |
| 6240 | } |
| 6241 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6242 | /* add all the children, they get added recursively */ |
| 6243 | set_init(&ret_set, set); |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6244 | for (uint32_t i = 0; i < set->used; ++i) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6245 | /* copy the current node to tmp */ |
| 6246 | set_insert_node(&ret_set, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, ret_set.used); |
| 6247 | |
| 6248 | /* do not touch attributes and text nodes */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6249 | 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] | 6250 | continue; |
| 6251 | } |
| 6252 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6253 | /* add all the children */ |
| 6254 | 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] | 6255 | set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6256 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6257 | lyxp_set_free_content(&ret_set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6258 | return rc; |
| 6259 | } |
| 6260 | } |
| 6261 | |
| 6262 | /* use the temporary set as the current one */ |
| 6263 | ret_set.ctx_pos = set->ctx_pos; |
| 6264 | ret_set.ctx_size = set->ctx_size; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6265 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6266 | memcpy(set, &ret_set, sizeof *set); |
| 6267 | |
| 6268 | return LY_SUCCESS; |
| 6269 | } |
| 6270 | |
| 6271 | /** |
| 6272 | * @brief Move context schema @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_SCNODE_SET |
| 6273 | * (or LYXP_SET_EMPTY). |
| 6274 | * |
| 6275 | * @param[in,out] set Set to use. |
| 6276 | * @param[in] all_desc Whether to go to all descendants ('//') or not ('/'). |
| 6277 | * @param[in] options XPath options. |
| 6278 | * @return LY_ERR |
| 6279 | */ |
| 6280 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6281 | 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] | 6282 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6283 | uint32_t getnext_opts; |
| 6284 | uint32_t mod_idx; |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6285 | const struct lysc_node *iter, *start_parent; |
| 6286 | const struct lys_module *mod; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6287 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6288 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6289 | return LY_SUCCESS; |
| 6290 | } |
| 6291 | |
| 6292 | if (set->type != LYXP_SET_SCNODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 6293 | 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] | 6294 | return LY_EVALID; |
| 6295 | } |
| 6296 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6297 | /* getnext opts */ |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 6298 | getnext_opts = 0; |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6299 | if (options & LYXP_SCNODE_OUTPUT) { |
| 6300 | getnext_opts |= LYS_GETNEXT_OUTPUT; |
| 6301 | } |
| 6302 | |
| 6303 | /* 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] | 6304 | for (uint32_t i = 0; i < set->used; ++i) { |
Michal Vasko | e657f96 | 2020-12-10 12:19:48 +0100 | [diff] [blame] | 6305 | if (!all_desc) { |
| 6306 | /* traverse the start node */ |
| 6307 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START) { |
| 6308 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
| 6309 | } |
| 6310 | continue; |
| 6311 | } |
| 6312 | |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 6313 | if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) { |
| 6314 | if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6315 | continue; |
| 6316 | } |
| 6317 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6318 | /* remember context node */ |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 6319 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED; |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6320 | } else { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 6321 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6322 | } |
| 6323 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6324 | start_parent = set->val.scnodes[i].scnode; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6325 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6326 | if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) { |
| 6327 | /* it can actually be in any module, it's all <running> */ |
| 6328 | mod_idx = 0; |
Michal Vasko | a51ef07 | 2021-07-02 10:40:30 +0200 | [diff] [blame] | 6329 | while ((mod = ly_ctx_get_module_iter(set->ctx, &mod_idx))) { |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6330 | iter = NULL; |
| 6331 | /* module may not be implemented */ |
| 6332 | while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) { |
| 6333 | /* context check */ |
| 6334 | if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) { |
| 6335 | continue; |
| 6336 | } |
| 6337 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 6338 | 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] | 6339 | /* throw away the insert index, we want to consider that node again, recursively */ |
| 6340 | } |
| 6341 | } |
| 6342 | |
| 6343 | } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) { |
| 6344 | iter = NULL; |
| 6345 | while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6346 | /* context check */ |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6347 | 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] | 6348 | continue; |
| 6349 | } |
| 6350 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 6351 | 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] | 6352 | } |
| 6353 | } |
| 6354 | } |
| 6355 | |
| 6356 | return LY_SUCCESS; |
| 6357 | } |
| 6358 | |
| 6359 | /** |
| 6360 | * @brief Move context @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_NODE_SET |
| 6361 | * (or LYXP_SET_EMPTY). Context position aware. |
| 6362 | * |
| 6363 | * @param[in] set Set to use. |
| 6364 | * @param[in] all_desc Whether to go to all descendants ('//') or not ('/'). |
| 6365 | * @param[in] options XPath options. |
| 6366 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6367 | */ |
| 6368 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6369 | 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] | 6370 | { |
Michal Vasko | 9d41411 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6371 | LY_ERR rc = LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6372 | struct lyd_node *node, *new_node; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6373 | enum lyxp_node_type new_type; |
Michal Vasko | 9d41411 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6374 | struct lyxp_set result; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6375 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6376 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6377 | return LY_SUCCESS; |
| 6378 | } |
| 6379 | |
| 6380 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 6381 | 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] | 6382 | return LY_EVALID; |
| 6383 | } |
| 6384 | |
| 6385 | if (all_desc) { |
| 6386 | /* <path>//.. == <path>//./.. */ |
| 6387 | rc = moveto_self(set, 1, options); |
| 6388 | LY_CHECK_RET(rc); |
| 6389 | } |
| 6390 | |
Michal Vasko | 9d41411 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6391 | /* init result set */ |
| 6392 | set_init(&result, set); |
| 6393 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6394 | for (uint32_t i = 0; i < set->used; ++i) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6395 | node = set->val.nodes[i].node; |
| 6396 | |
| 6397 | if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 6398 | new_node = lyd_parent(node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6399 | } else if (set->val.nodes[i].type == LYXP_NODE_TEXT) { |
| 6400 | new_node = node; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6401 | } else if (set->val.nodes[i].type == LYXP_NODE_META) { |
| 6402 | new_node = set->val.meta[i].meta->parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6403 | if (!new_node) { |
Michal Vasko | 9d41411 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6404 | LOGINT(set->ctx); |
| 6405 | rc = LY_EINT; |
| 6406 | goto cleanup; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6407 | } |
| 6408 | } else { |
| 6409 | /* root does not have a parent */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6410 | continue; |
| 6411 | } |
| 6412 | |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 6413 | /* when check */ |
Michal Vasko | 9d41411 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6414 | if (!(options & LYXP_IGNORE_WHEN) && new_node && lysc_has_when(new_node->schema) && |
| 6415 | !(new_node->flags & LYD_WHEN_TRUE)) { |
| 6416 | rc = LY_EINCOMPLETE; |
| 6417 | goto cleanup; |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 6418 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6419 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6420 | if (!new_node) { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 6421 | /* node already there can also be the root */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6422 | new_type = set->root_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6423 | } else { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 6424 | /* 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] | 6425 | new_type = LYXP_NODE_ELEM; |
| 6426 | } |
| 6427 | |
Michal Vasko | 9d41411 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6428 | /* check for duplicates, several nodes may have the same parent */ |
| 6429 | if (!set_dup_node_check(&result, new_node, new_type, -1)) { |
| 6430 | set_insert_node(&result, new_node, 0, new_type, result.used); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6431 | } |
| 6432 | } |
| 6433 | |
Michal Vasko | 9d41411 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6434 | /* move result to the set */ |
| 6435 | lyxp_set_free_content(set); |
| 6436 | *set = result; |
| 6437 | result.type = LYXP_SET_NUMBER; |
| 6438 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6439 | |
Michal Vasko | 9d41411 | 2021-12-02 12:31:00 +0100 | [diff] [blame] | 6440 | cleanup: |
| 6441 | lyxp_set_free_content(&result); |
| 6442 | return rc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6443 | } |
| 6444 | |
| 6445 | /** |
| 6446 | * @brief Move context schema @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_SCNODE_SET |
| 6447 | * (or LYXP_SET_EMPTY). |
| 6448 | * |
| 6449 | * @param[in] set Set to use. |
| 6450 | * @param[in] all_desc Whether to go to all descendants ('//') or not ('/'). |
| 6451 | * @param[in] options XPath options. |
| 6452 | * @return LY_ERR |
| 6453 | */ |
| 6454 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6455 | 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] | 6456 | { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 6457 | uint32_t i, orig_used, idx; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6458 | ly_bool temp_ctx = 0; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6459 | const struct lysc_node *node, *new_node; |
| 6460 | enum lyxp_node_type new_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6461 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6462 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6463 | return LY_SUCCESS; |
| 6464 | } |
| 6465 | |
| 6466 | if (set->type != LYXP_SET_SCNODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 6467 | 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] | 6468 | return LY_EVALID; |
| 6469 | } |
| 6470 | |
| 6471 | if (all_desc) { |
| 6472 | /* <path>//.. == <path>//./.. */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6473 | LY_CHECK_RET(moveto_scnode_self(set, 1, options)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6474 | } |
| 6475 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6476 | orig_used = set->used; |
| 6477 | for (i = 0; i < orig_used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 6478 | if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) { |
| 6479 | if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6480 | continue; |
| 6481 | } |
| 6482 | |
| 6483 | /* remember context node */ |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 6484 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED; |
Michal Vasko | ec4df48 | 2019-12-16 10:02:18 +0100 | [diff] [blame] | 6485 | } else { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 6486 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6487 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6488 | |
| 6489 | node = set->val.scnodes[i].scnode; |
| 6490 | |
| 6491 | if (set->val.scnodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6492 | new_node = lysc_data_parent(node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6493 | } else { |
| 6494 | /* root does not have a parent */ |
| 6495 | continue; |
| 6496 | } |
| 6497 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6498 | if (!new_node) { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 6499 | /* node has no parent */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6500 | new_type = set->root_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6501 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6502 | } else { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 6503 | /* 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] | 6504 | new_type = LYXP_NODE_ELEM; |
| 6505 | } |
| 6506 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 6507 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, new_node, new_type, &idx)); |
| 6508 | if ((idx < orig_used) && (idx > i)) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 6509 | set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6510 | temp_ctx = 1; |
| 6511 | } |
| 6512 | } |
| 6513 | |
| 6514 | if (temp_ctx) { |
| 6515 | for (i = 0; i < orig_used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 6516 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NEW_CTX) { |
| 6517 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6518 | } |
| 6519 | } |
| 6520 | } |
| 6521 | |
| 6522 | return LY_SUCCESS; |
| 6523 | } |
| 6524 | |
| 6525 | /** |
| 6526 | * @brief Move context @p set to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'. |
| 6527 | * Result is LYXP_SET_BOOLEAN. Indirectly context position aware. |
| 6528 | * |
| 6529 | * @param[in,out] set1 Set to use for the result. |
| 6530 | * @param[in] set2 Set acting as the second operand for @p op. |
| 6531 | * @param[in] op Comparison operator to process. |
| 6532 | * @param[in] options XPath options. |
| 6533 | * @return LY_ERR |
| 6534 | */ |
| 6535 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6536 | 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] | 6537 | { |
| 6538 | /* |
| 6539 | * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING |
| 6540 | * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING) |
| 6541 | * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 6542 | * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 6543 | * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING |
| 6544 | * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6545 | * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 6546 | * |
| 6547 | * '=' or '!=' |
| 6548 | * BOOLEAN + BOOLEAN |
| 6549 | * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 6550 | * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 6551 | * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 6552 | * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 6553 | * NUMBER + NUMBER |
| 6554 | * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6555 | * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 6556 | * STRING + STRING |
| 6557 | * |
| 6558 | * '<=', '<', '>=', '>' |
| 6559 | * NUMBER + NUMBER |
| 6560 | * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 6561 | * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 6562 | * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 6563 | * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6564 | * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 6565 | * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 6566 | * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6567 | * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6568 | */ |
| 6569 | struct lyxp_set iter1, iter2; |
| 6570 | int result; |
| 6571 | int64_t i; |
| 6572 | LY_ERR rc; |
| 6573 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6574 | memset(&iter1, 0, sizeof iter1); |
| 6575 | memset(&iter2, 0, sizeof iter2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6576 | |
| 6577 | /* iterative evaluation with node-sets */ |
| 6578 | if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) { |
| 6579 | if (set1->type == LYXP_SET_NODE_SET) { |
| 6580 | for (i = 0; i < set1->used; ++i) { |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6581 | /* cast set1 */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6582 | switch (set2->type) { |
| 6583 | case LYXP_SET_NUMBER: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6584 | rc = set_comp_cast(&iter1, set1, LYXP_SET_NUMBER, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6585 | break; |
| 6586 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6587 | rc = set_comp_cast(&iter1, set1, LYXP_SET_BOOLEAN, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6588 | break; |
| 6589 | default: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6590 | rc = set_comp_cast(&iter1, set1, LYXP_SET_STRING, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6591 | break; |
| 6592 | } |
| 6593 | LY_CHECK_RET(rc); |
| 6594 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6595 | /* canonize set2 */ |
| 6596 | LY_CHECK_ERR_RET(rc = set_comp_canonize(&iter2, set2, &set1->val.nodes[i]), lyxp_set_free_content(&iter1), rc); |
| 6597 | |
| 6598 | /* compare recursively */ |
| 6599 | rc = moveto_op_comp(&iter1, &iter2, op, options); |
| 6600 | lyxp_set_free_content(&iter2); |
| 6601 | LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6602 | |
| 6603 | /* lazy evaluation until true */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6604 | if (iter1.val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6605 | set_fill_boolean(set1, 1); |
| 6606 | return LY_SUCCESS; |
| 6607 | } |
| 6608 | } |
| 6609 | } else { |
| 6610 | for (i = 0; i < set2->used; ++i) { |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6611 | /* set set2 */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6612 | switch (set1->type) { |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6613 | case LYXP_SET_NUMBER: |
| 6614 | rc = set_comp_cast(&iter2, set2, LYXP_SET_NUMBER, i); |
| 6615 | break; |
| 6616 | case LYXP_SET_BOOLEAN: |
| 6617 | rc = set_comp_cast(&iter2, set2, LYXP_SET_BOOLEAN, i); |
| 6618 | break; |
| 6619 | default: |
| 6620 | rc = set_comp_cast(&iter2, set2, LYXP_SET_STRING, i); |
| 6621 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6622 | } |
| 6623 | LY_CHECK_RET(rc); |
| 6624 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6625 | /* canonize set1 */ |
| 6626 | 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] | 6627 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6628 | /* compare recursively */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6629 | rc = moveto_op_comp(&iter1, &iter2, op, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6630 | lyxp_set_free_content(&iter2); |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6631 | LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6632 | |
| 6633 | /* lazy evaluation until true */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6634 | if (iter1.val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6635 | set_fill_boolean(set1, 1); |
| 6636 | return LY_SUCCESS; |
| 6637 | } |
| 6638 | } |
| 6639 | } |
| 6640 | |
| 6641 | /* false for all nodes */ |
| 6642 | set_fill_boolean(set1, 0); |
| 6643 | return LY_SUCCESS; |
| 6644 | } |
| 6645 | |
| 6646 | /* first convert properly */ |
| 6647 | if ((op[0] == '=') || (op[0] == '!')) { |
| 6648 | if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6649 | lyxp_set_cast(set1, LYXP_SET_BOOLEAN); |
| 6650 | lyxp_set_cast(set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6651 | } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6652 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6653 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6654 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6655 | LY_CHECK_RET(rc); |
| 6656 | } /* else we have 2 strings */ |
| 6657 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6658 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6659 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6660 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6661 | LY_CHECK_RET(rc); |
| 6662 | } |
| 6663 | |
| 6664 | assert(set1->type == set2->type); |
| 6665 | |
| 6666 | /* compute result */ |
| 6667 | if (op[0] == '=') { |
| 6668 | if (set1->type == LYXP_SET_BOOLEAN) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6669 | result = (set1->val.bln == set2->val.bln); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6670 | } else if (set1->type == LYXP_SET_NUMBER) { |
| 6671 | result = (set1->val.num == set2->val.num); |
| 6672 | } else { |
| 6673 | assert(set1->type == LYXP_SET_STRING); |
Michal Vasko | ac6c72f | 2019-11-14 16:09:34 +0100 | [diff] [blame] | 6674 | result = !strcmp(set1->val.str, set2->val.str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6675 | } |
| 6676 | } else if (op[0] == '!') { |
| 6677 | if (set1->type == LYXP_SET_BOOLEAN) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6678 | result = (set1->val.bln != set2->val.bln); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6679 | } else if (set1->type == LYXP_SET_NUMBER) { |
| 6680 | result = (set1->val.num != set2->val.num); |
| 6681 | } else { |
| 6682 | assert(set1->type == LYXP_SET_STRING); |
Michal Vasko | c205843 | 2020-11-06 17:26:21 +0100 | [diff] [blame] | 6683 | result = strcmp(set1->val.str, set2->val.str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6684 | } |
| 6685 | } else { |
| 6686 | assert(set1->type == LYXP_SET_NUMBER); |
| 6687 | if (op[0] == '<') { |
| 6688 | if (op[1] == '=') { |
| 6689 | result = (set1->val.num <= set2->val.num); |
| 6690 | } else { |
| 6691 | result = (set1->val.num < set2->val.num); |
| 6692 | } |
| 6693 | } else { |
| 6694 | if (op[1] == '=') { |
| 6695 | result = (set1->val.num >= set2->val.num); |
| 6696 | } else { |
| 6697 | result = (set1->val.num > set2->val.num); |
| 6698 | } |
| 6699 | } |
| 6700 | } |
| 6701 | |
| 6702 | /* assign result */ |
| 6703 | if (result) { |
| 6704 | set_fill_boolean(set1, 1); |
| 6705 | } else { |
| 6706 | set_fill_boolean(set1, 0); |
| 6707 | } |
| 6708 | |
| 6709 | return LY_SUCCESS; |
| 6710 | } |
| 6711 | |
| 6712 | /** |
| 6713 | * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div', |
| 6714 | * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware. |
| 6715 | * |
| 6716 | * @param[in,out] set1 Set to use for the result. |
| 6717 | * @param[in] set2 Set acting as the second operand for @p op. |
| 6718 | * @param[in] op Operator to process. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6719 | * @return LY_ERR |
| 6720 | */ |
| 6721 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6722 | 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] | 6723 | { |
| 6724 | LY_ERR rc; |
| 6725 | |
| 6726 | /* unary '-' */ |
| 6727 | if (!set2 && (op[0] == '-')) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6728 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6729 | LY_CHECK_RET(rc); |
| 6730 | set1->val.num *= -1; |
| 6731 | lyxp_set_free(set2); |
| 6732 | return LY_SUCCESS; |
| 6733 | } |
| 6734 | |
| 6735 | assert(set1 && set2); |
| 6736 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6737 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6738 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6739 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6740 | LY_CHECK_RET(rc); |
| 6741 | |
| 6742 | switch (op[0]) { |
| 6743 | /* '+' */ |
| 6744 | case '+': |
| 6745 | set1->val.num += set2->val.num; |
| 6746 | break; |
| 6747 | |
| 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 | /* 'div' */ |
| 6759 | case 'd': |
| 6760 | set1->val.num /= set2->val.num; |
| 6761 | break; |
| 6762 | |
| 6763 | /* 'mod' */ |
| 6764 | case 'm': |
| 6765 | set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num); |
| 6766 | break; |
| 6767 | |
| 6768 | default: |
| 6769 | LOGINT_RET(set1->ctx); |
| 6770 | } |
| 6771 | |
| 6772 | return LY_SUCCESS; |
| 6773 | } |
| 6774 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6775 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6776 | * @brief Evaluate Predicate. Logs directly on error. |
| 6777 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6778 | * [9] Predicate ::= '[' Expr ']' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6779 | * |
| 6780 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6781 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6782 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6783 | * @param[in] options XPath options. |
| 6784 | * @param[in] parent_pos_pred Whether parent predicate was a positional one. |
| 6785 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6786 | */ |
| 6787 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 6788 | 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] | 6789 | { |
| 6790 | LY_ERR rc; |
Michal Vasko | 1fdd8fa | 2021-01-08 09:21:45 +0100 | [diff] [blame] | 6791 | uint16_t orig_exp; |
| 6792 | uint32_t i, orig_pos, orig_size; |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6793 | int32_t pred_in_ctx; |
aPiecek | fff4dca | 2021-10-07 10:59:53 +0200 | [diff] [blame] | 6794 | struct lyxp_set set2 = {0}; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6795 | struct lyd_node *orig_parent; |
| 6796 | |
| 6797 | /* '[' */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6798 | 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] | 6799 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6800 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6801 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6802 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6803 | only_parse: |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6804 | 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] | 6805 | LY_CHECK_RET(rc); |
| 6806 | } else if (set->type == LYXP_SET_NODE_SET) { |
| 6807 | /* 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] | 6808 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6809 | |
| 6810 | /* empty set, nothing to evaluate */ |
| 6811 | if (!set->used) { |
| 6812 | goto only_parse; |
| 6813 | } |
| 6814 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6815 | orig_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6816 | orig_pos = 0; |
| 6817 | orig_size = set->used; |
| 6818 | orig_parent = NULL; |
Michal Vasko | 39dbf35 | 2020-05-21 10:08:59 +0200 | [diff] [blame] | 6819 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6820 | set_init(&set2, set); |
| 6821 | set_insert_node(&set2, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, 0); |
| 6822 | /* remember the node context position for position() and context size for last(), |
| 6823 | * predicates should always be evaluated with respect to the child axis (since we do |
| 6824 | * not support explicit axes) so we assign positions based on their parents */ |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 6825 | if (parent_pos_pred && (lyd_parent(set->val.nodes[i].node) != orig_parent)) { |
| 6826 | orig_parent = lyd_parent(set->val.nodes[i].node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6827 | orig_pos = 1; |
| 6828 | } else { |
| 6829 | ++orig_pos; |
| 6830 | } |
| 6831 | |
| 6832 | set2.ctx_pos = orig_pos; |
| 6833 | set2.ctx_size = orig_size; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6834 | *tok_idx = orig_exp; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6835 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6836 | rc = eval_expr_select(exp, tok_idx, 0, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6837 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6838 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6839 | return rc; |
| 6840 | } |
| 6841 | |
| 6842 | /* number is a position */ |
| 6843 | if (set2.type == LYXP_SET_NUMBER) { |
| 6844 | if ((long long)set2.val.num == orig_pos) { |
| 6845 | set2.val.num = 1; |
| 6846 | } else { |
| 6847 | set2.val.num = 0; |
| 6848 | } |
| 6849 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6850 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6851 | |
| 6852 | /* predicate satisfied or not? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6853 | if (!set2.val.bln) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6854 | set_remove_node_none(set, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6855 | } |
| 6856 | } |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6857 | set_remove_nodes_none(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6858 | |
| 6859 | } else if (set->type == LYXP_SET_SCNODE_SET) { |
| 6860 | for (i = 0; i < set->used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 6861 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6862 | /* there is a currently-valid node */ |
| 6863 | break; |
| 6864 | } |
| 6865 | } |
| 6866 | /* empty set, nothing to evaluate */ |
| 6867 | if (i == set->used) { |
| 6868 | goto only_parse; |
| 6869 | } |
| 6870 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6871 | orig_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6872 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6873 | /* set special in_ctx to all the valid snodes */ |
| 6874 | pred_in_ctx = set_scnode_new_in_ctx(set); |
| 6875 | |
| 6876 | /* use the valid snodes one-by-one */ |
| 6877 | for (i = 0; i < set->used; ++i) { |
| 6878 | if (set->val.scnodes[i].in_ctx != pred_in_ctx) { |
| 6879 | continue; |
| 6880 | } |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 6881 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6882 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6883 | *tok_idx = orig_exp; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6884 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6885 | rc = eval_expr_select(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6886 | LY_CHECK_RET(rc); |
| 6887 | |
| 6888 | set->val.scnodes[i].in_ctx = pred_in_ctx; |
| 6889 | } |
| 6890 | |
| 6891 | /* restore the state as it was before the predicate */ |
| 6892 | for (i = 0; i < set->used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 6893 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 6894 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6895 | } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 6896 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6897 | } |
| 6898 | } |
| 6899 | |
| 6900 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6901 | set2.type = LYXP_SET_NODE_SET; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6902 | set_fill_set(&set2, set); |
| 6903 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6904 | rc = eval_expr_select(exp, tok_idx, 0, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6905 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6906 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6907 | return rc; |
| 6908 | } |
| 6909 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6910 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6911 | if (!set2.val.bln) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6912 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6913 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6914 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6915 | } |
| 6916 | |
| 6917 | /* ']' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6918 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 6919 | 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] | 6920 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6921 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6922 | |
| 6923 | return LY_SUCCESS; |
| 6924 | } |
| 6925 | |
| 6926 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6927 | * @brief Evaluate Literal. Logs directly on error. |
| 6928 | * |
| 6929 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6930 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6931 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 6932 | */ |
| 6933 | static void |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 6934 | 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] | 6935 | { |
| 6936 | if (set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6937 | if (exp->tok_len[*tok_idx] == 2) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6938 | set_fill_string(set, "", 0); |
| 6939 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6940 | 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] | 6941 | } |
| 6942 | } |
| 6943 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 6944 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6945 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6946 | } |
| 6947 | |
| 6948 | /** |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6949 | * @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] | 6950 | * |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6951 | * @param[in] exp Full parsed XPath expression. |
| 6952 | * @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] | 6953 | * @param[in] ctx_node Found schema node as the context for the predicate. |
| 6954 | * @param[in] cur_mod Current module for the expression. |
| 6955 | * @param[in] cur_node Current (original context) node. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6956 | * @param[in] format Format of any prefixes in key names/values. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6957 | * @param[in] prefix_data Format-specific prefix data (see ::ly_resolve_prefix). |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6958 | * @param[out] predicates Parsed predicates. |
| 6959 | * @param[out] pred_type Type of @p predicates. |
| 6960 | * @return LY_SUCCESS on success, |
| 6961 | * @return LY_ERR on any error. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6962 | */ |
| 6963 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 6964 | 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] | 6965 | 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] | 6966 | struct ly_path_predicate **predicates, enum ly_path_pred_type *pred_type) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6967 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6968 | LY_ERR ret = LY_SUCCESS; |
| 6969 | uint16_t key_count, e_idx, pred_idx = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6970 | const struct lysc_node *key; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6971 | size_t pred_len; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6972 | uint32_t prev_lo; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6973 | struct lyxp_expr *exp2 = NULL; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6974 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6975 | assert(ctx_node->nodetype & (LYS_LIST | LYS_LEAFLIST)); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6976 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6977 | if (ctx_node->nodetype == LYS_LIST) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6978 | /* get key count */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6979 | if (ctx_node->flags & LYS_KEYLESS) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6980 | return LY_EINVAL; |
| 6981 | } |
Michal Vasko | 544e58a | 2021-01-28 14:33:41 +0100 | [diff] [blame] | 6982 | 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] | 6983 | assert(key_count); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6984 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6985 | /* learn where the predicates end */ |
| 6986 | e_idx = *tok_idx; |
| 6987 | while (key_count) { |
| 6988 | /* '[' */ |
| 6989 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) { |
| 6990 | return LY_EINVAL; |
| 6991 | } |
| 6992 | ++e_idx; |
| 6993 | |
Michal Vasko | 3354d27 | 2021-04-06 09:40:06 +0200 | [diff] [blame] | 6994 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_NAMETEST)) { |
| 6995 | /* definitely not a key */ |
| 6996 | return LY_EINVAL; |
| 6997 | } |
| 6998 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6999 | /* ']' */ |
| 7000 | while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) { |
| 7001 | ++e_idx; |
| 7002 | } |
| 7003 | ++e_idx; |
| 7004 | |
| 7005 | /* another presumably key predicate parsed */ |
| 7006 | --key_count; |
| 7007 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7008 | } else { |
| 7009 | /* learn just where this single predicate ends */ |
| 7010 | e_idx = *tok_idx; |
| 7011 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7012 | /* '[' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7013 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) { |
| 7014 | return LY_EINVAL; |
| 7015 | } |
| 7016 | ++e_idx; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7017 | |
Michal Vasko | 3354d27 | 2021-04-06 09:40:06 +0200 | [diff] [blame] | 7018 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_DOT)) { |
| 7019 | /* definitely not the value */ |
| 7020 | return LY_EINVAL; |
| 7021 | } |
| 7022 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7023 | /* ']' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7024 | while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) { |
| 7025 | ++e_idx; |
| 7026 | } |
| 7027 | ++e_idx; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7028 | } |
| 7029 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7030 | /* get the joined length of all the keys predicates/of the single leaf-list predicate */ |
| 7031 | pred_len = (exp->tok_pos[e_idx - 1] + exp->tok_len[e_idx - 1]) - exp->tok_pos[*tok_idx]; |
| 7032 | |
| 7033 | /* turn logging off */ |
| 7034 | prev_lo = ly_log_options(0); |
| 7035 | |
| 7036 | /* parse the predicate(s) */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7037 | LY_CHECK_GOTO(ret = ly_path_parse_predicate(ctx_node->module->ctx, ctx_node, exp->expr + exp->tok_pos[*tok_idx], |
| 7038 | pred_len, LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp2), cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7039 | |
| 7040 | /* compile */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7041 | ret = ly_path_compile_predicate(ctx_node->module->ctx, cur_node, cur_mod, ctx_node, exp2, &pred_idx, format, |
| 7042 | prefix_data, predicates, pred_type); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7043 | LY_CHECK_GOTO(ret, cleanup); |
| 7044 | |
| 7045 | /* success, the predicate must include all the needed information for hash-based search */ |
| 7046 | *tok_idx = e_idx; |
| 7047 | |
| 7048 | cleanup: |
| 7049 | ly_log_options(prev_lo); |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7050 | lyxp_expr_free(ctx_node->module->ctx, exp2); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7051 | return ret; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7052 | } |
| 7053 | |
| 7054 | /** |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7055 | * @brief Search for/check the next schema node that could be the only matching schema node meaning the |
| 7056 | * data node(s) could be found using a single hash-based search. |
| 7057 | * |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7058 | * @param[in] ctx libyang context. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7059 | * @param[in] node Next context node to check. |
| 7060 | * @param[in] name Expected node name. |
| 7061 | * @param[in] name_len Length of @p name. |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7062 | * @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] | 7063 | * @param[in] root_type XPath root type. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7064 | * @param[in] format Prefix format. |
| 7065 | * @param[in,out] found Previously found node, is updated. |
| 7066 | * @return LY_SUCCESS on success, |
| 7067 | * @return LY_ENOT if the whole check failed and hashes cannot be used. |
| 7068 | */ |
| 7069 | static LY_ERR |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7070 | 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] | 7071 | 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] | 7072 | const struct lysc_node **found) |
| 7073 | { |
| 7074 | const struct lysc_node *scnode; |
| 7075 | const struct lys_module *mod; |
| 7076 | uint32_t idx = 0; |
| 7077 | |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 7078 | assert((format == LY_VALUE_JSON) || moveto_mod); |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7079 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7080 | continue_search: |
Michal Vasko | 7d1d091 | 2020-10-16 08:38:30 +0200 | [diff] [blame] | 7081 | scnode = NULL; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7082 | if (!node) { |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 7083 | if ((format == LY_VALUE_JSON) && !moveto_mod) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7084 | /* search all modules for a single match */ |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7085 | while ((mod = ly_ctx_get_module_iter(ctx, &idx))) { |
Michal Vasko | 35a3b1d | 2021-07-14 09:34:16 +0200 | [diff] [blame] | 7086 | if (!mod->implemented) { |
| 7087 | continue; |
| 7088 | } |
| 7089 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7090 | scnode = lys_find_child(NULL, mod, name, name_len, 0, 0); |
| 7091 | if (scnode) { |
| 7092 | /* we have found a match */ |
| 7093 | break; |
| 7094 | } |
| 7095 | } |
| 7096 | |
Michal Vasko | 7d1d091 | 2020-10-16 08:38:30 +0200 | [diff] [blame] | 7097 | if (!scnode) { |
| 7098 | /* all modules searched */ |
| 7099 | idx = 0; |
| 7100 | } |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7101 | } else { |
| 7102 | /* search in top-level */ |
| 7103 | scnode = lys_find_child(NULL, moveto_mod, name, name_len, 0, 0); |
| 7104 | } |
| 7105 | } else if (!*found || (lysc_data_parent(*found) != node->schema)) { |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 7106 | if ((format == LY_VALUE_JSON) && !moveto_mod) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7107 | /* we must adjust the module to inherit the one from the context node */ |
| 7108 | moveto_mod = node->schema->module; |
| 7109 | } |
| 7110 | |
| 7111 | /* search in children, do not repeat the same search */ |
| 7112 | 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] | 7113 | } /* else skip redundant search */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7114 | |
| 7115 | /* additional context check */ |
| 7116 | if (scnode && (root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) { |
| 7117 | scnode = NULL; |
| 7118 | } |
| 7119 | |
| 7120 | if (scnode) { |
| 7121 | if (*found) { |
| 7122 | /* we found a schema node with the same name but at different level, give up, too complicated |
| 7123 | * (more hash-based searches would be required, not supported) */ |
| 7124 | return LY_ENOT; |
| 7125 | } else { |
| 7126 | /* remember the found schema node and continue to make sure it can be used */ |
| 7127 | *found = scnode; |
| 7128 | } |
| 7129 | } |
| 7130 | |
| 7131 | if (idx) { |
| 7132 | /* continue searching all the following models */ |
| 7133 | goto continue_search; |
| 7134 | } |
| 7135 | |
| 7136 | return LY_SUCCESS; |
| 7137 | } |
| 7138 | |
| 7139 | /** |
Michal Vasko | 4ad69e7 | 2021-10-26 16:25:55 +0200 | [diff] [blame] | 7140 | * @brief Generate message when no matching schema nodes were found for a path segment. |
| 7141 | * |
| 7142 | * @param[in] set XPath set. |
| 7143 | * @param[in] scparent Previous schema parent in the context, if only one. |
| 7144 | * @param[in] ncname XPath NCName being evaluated. |
| 7145 | * @param[in] ncname_len Length of @p ncname. |
| 7146 | * @param[in] expr Whole XPath expression. |
| 7147 | * @param[in] options XPath options. |
| 7148 | */ |
| 7149 | static void |
| 7150 | eval_name_test_scnode_no_match_msg(struct lyxp_set *set, const struct lyxp_set_scnode *scparent, const char *ncname, |
| 7151 | uint16_t ncname_len, const char *expr, uint32_t options) |
| 7152 | { |
| 7153 | const char *format; |
| 7154 | char *path = NULL, *ppath = NULL; |
| 7155 | |
| 7156 | path = lysc_path(set->cur_scnode, LYSC_PATH_LOG, NULL, 0); |
| 7157 | if (scparent) { |
| 7158 | /* generate path for the parent */ |
| 7159 | if (scparent->type == LYXP_NODE_ELEM) { |
| 7160 | ppath = lysc_path(scparent->scnode, LYSC_PATH_LOG, NULL, 0); |
| 7161 | } else if (scparent->type == LYXP_NODE_ROOT) { |
| 7162 | ppath = strdup("<root>"); |
| 7163 | } else if (scparent->type == LYXP_NODE_ROOT_CONFIG) { |
| 7164 | ppath = strdup("<config-root>"); |
| 7165 | } |
| 7166 | } |
| 7167 | if (ppath) { |
| 7168 | format = "Schema node \"%.*s\" for parent \"%s\" not found; in expr \"%.*s\" with context node \"%s\"."; |
| 7169 | if (options & LYXP_SCNODE_ERROR) { |
| 7170 | LOGERR(set->ctx, LY_EVALID, format, ncname_len, ncname, ppath, (ncname - expr) + ncname_len, expr, path); |
| 7171 | } else { |
| 7172 | LOGWRN(set->ctx, format, ncname_len, ncname, ppath, (ncname - expr) + ncname_len, expr, path); |
| 7173 | } |
| 7174 | } else { |
| 7175 | format = "Schema node \"%.*s\" not found; in expr \"%.*s\" with context node \"%s\"."; |
| 7176 | if (options & LYXP_SCNODE_ERROR) { |
| 7177 | LOGERR(set->ctx, LY_EVALID, format, ncname_len, ncname, (ncname - expr) + ncname_len, expr, path); |
| 7178 | } else { |
| 7179 | LOGWRN(set->ctx, format, ncname_len, ncname, (ncname - expr) + ncname_len, expr, path); |
| 7180 | } |
| 7181 | } |
| 7182 | free(path); |
| 7183 | free(ppath); |
| 7184 | } |
| 7185 | |
| 7186 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7187 | * @brief Evaluate NameTest and any following Predicates. Logs directly on error. |
| 7188 | * |
| 7189 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 7190 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 7191 | * [7] NameTest ::= '*' | NCName ':' '*' | QName |
| 7192 | * |
| 7193 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7194 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7195 | * @param[in] attr_axis Whether to search attributes or standard nodes. |
| 7196 | * @param[in] all_desc Whether to search all the descendants or children only. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7197 | * @param[in,out] set Context and result set. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7198 | * @param[in] options XPath options. |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 7199 | * @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] | 7200 | */ |
| 7201 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 7202 | 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] | 7203 | struct lyxp_set *set, uint32_t options) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7204 | { |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 7205 | LY_ERR rc = LY_SUCCESS, r; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7206 | const char *ncname, *ncname_dict = NULL; |
| 7207 | uint16_t ncname_len; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7208 | const struct lys_module *moveto_mod = NULL; |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7209 | const struct lysc_node *scnode = NULL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7210 | struct ly_path_predicate *predicates = NULL; |
| 7211 | enum ly_path_pred_type pred_type = 0; |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 7212 | int scnode_skip_pred = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7213 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7214 | 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] | 7215 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7216 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7217 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7218 | if (options & LYXP_SKIP_EXPR) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7219 | goto moveto; |
| 7220 | } |
| 7221 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7222 | ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]]; |
| 7223 | ncname_len = exp->tok_len[*tok_idx - 1]; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7224 | |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7225 | if ((ncname[0] == '*') && (ncname_len == 1)) { |
| 7226 | /* all nodes will match */ |
| 7227 | goto moveto; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7228 | } |
| 7229 | |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7230 | /* parse (and skip) module name */ |
| 7231 | rc = moveto_resolve_model(&ncname, &ncname_len, set, NULL, &moveto_mod); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7232 | LY_CHECK_GOTO(rc, cleanup); |
| 7233 | |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 7234 | 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] | 7235 | /* find the matching schema node in some parent in the context */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7236 | for (uint32_t i = 0; i < set->used; ++i) { |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7237 | if (eval_name_test_with_predicate_get_scnode(set->ctx, set->val.nodes[i].node, ncname, ncname_len, |
| 7238 | moveto_mod, set->root_type, set->format, &scnode)) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7239 | /* check failed */ |
| 7240 | scnode = NULL; |
| 7241 | break; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7242 | } |
| 7243 | } |
| 7244 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7245 | if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) { |
| 7246 | /* try to create the predicates */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7247 | if (eval_name_test_try_compile_predicates(exp, tok_idx, scnode, set->cur_mod, set->cur_node ? |
| 7248 | 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] | 7249 | /* hashes cannot be used */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7250 | scnode = NULL; |
| 7251 | } |
| 7252 | } |
| 7253 | } |
| 7254 | |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7255 | if (!scnode) { |
| 7256 | /* 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] | 7257 | * use dictionary for efficient comparison */ |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 7258 | 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] | 7259 | } |
| 7260 | |
| 7261 | moveto: |
| 7262 | /* move to the attribute(s), data node(s), or schema node(s) */ |
| 7263 | if (attr_axis) { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7264 | if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 7265 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7266 | } else { |
| 7267 | if (all_desc) { |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 7268 | rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7269 | } else { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7270 | rc = moveto_attr(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7271 | } |
| 7272 | LY_CHECK_GOTO(rc, cleanup); |
| 7273 | } |
| 7274 | } else { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7275 | if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7276 | int64_t i; |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 7277 | const struct lyxp_set_scnode *scparent = NULL; |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 7278 | |
| 7279 | /* remember parent if there is only one, to print in the warning */ |
| 7280 | for (i = 0; i < set->used; ++i) { |
| 7281 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
| 7282 | if (!scparent) { |
| 7283 | /* remember the context node */ |
| 7284 | scparent = &set->val.scnodes[i]; |
| 7285 | } else { |
| 7286 | /* several context nodes, no reasonable error possible */ |
| 7287 | scparent = NULL; |
| 7288 | break; |
| 7289 | } |
| 7290 | } |
| 7291 | } |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7292 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7293 | if (all_desc) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7294 | rc = moveto_scnode_alldesc(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7295 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7296 | rc = moveto_scnode(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7297 | } |
| 7298 | LY_CHECK_GOTO(rc, cleanup); |
| 7299 | |
| 7300 | for (i = set->used - 1; i > -1; --i) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 7301 | if (set->val.scnodes[i].in_ctx > LYXP_SET_SCNODE_ATOM_NODE) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7302 | break; |
| 7303 | } |
| 7304 | } |
| 7305 | if (i == -1) { |
Michal Vasko | 4ad69e7 | 2021-10-26 16:25:55 +0200 | [diff] [blame] | 7306 | /* generate message */ |
| 7307 | eval_name_test_scnode_no_match_msg(set, scparent, ncname, ncname_len, exp->expr, options); |
| 7308 | |
| 7309 | if (options & LYXP_SCNODE_ERROR) { |
| 7310 | /* error */ |
| 7311 | rc = LY_EVALID; |
| 7312 | goto cleanup; |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 7313 | } |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 7314 | |
| 7315 | /* skip the predicates and the rest of this path to not generate invalid warnings */ |
| 7316 | rc = LY_ENOT; |
| 7317 | scnode_skip_pred = 1; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7318 | } |
| 7319 | } else { |
| 7320 | if (all_desc) { |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 7321 | rc = moveto_node_alldesc(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7322 | } else { |
| 7323 | if (scnode) { |
| 7324 | /* we can find the nodes using hashes */ |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 7325 | rc = moveto_node_hash(set, scnode, predicates, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7326 | } else { |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 7327 | rc = moveto_node(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7328 | } |
| 7329 | } |
| 7330 | LY_CHECK_GOTO(rc, cleanup); |
| 7331 | } |
| 7332 | } |
| 7333 | |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 7334 | if (scnode_skip_pred) { |
| 7335 | /* skip predicates */ |
| 7336 | options |= LYXP_SKIP_EXPR; |
| 7337 | } |
| 7338 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7339 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7340 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 7341 | r = eval_predicate(exp, tok_idx, set, options, 1); |
| 7342 | LY_CHECK_ERR_GOTO(r, rc = r, cleanup); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7343 | } |
| 7344 | |
| 7345 | cleanup: |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 7346 | if (scnode_skip_pred) { |
| 7347 | /* restore options */ |
| 7348 | options &= ~LYXP_SKIP_EXPR; |
| 7349 | } |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7350 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7351 | lydict_remove(set->ctx, ncname_dict); |
Michal Vasko | f7e16e2 | 2020-10-21 09:24:39 +0200 | [diff] [blame] | 7352 | ly_path_predicates_free(set->ctx, pred_type, predicates); |
Michal Vasko | db51a8d | 2020-05-27 15:22:29 +0200 | [diff] [blame] | 7353 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7354 | return rc; |
| 7355 | } |
| 7356 | |
| 7357 | /** |
| 7358 | * @brief Evaluate NodeType and any following Predicates. Logs directly on error. |
| 7359 | * |
| 7360 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 7361 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 7362 | * [8] NodeType ::= 'text' | 'node' |
| 7363 | * |
| 7364 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7365 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7366 | * @param[in] attr_axis Whether to search attributes or standard nodes. |
| 7367 | * @param[in] all_desc Whether to search all the descendants or children only. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7368 | * @param[in,out] set Context and result set. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7369 | * @param[in] options XPath options. |
| 7370 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7371 | */ |
| 7372 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 7373 | 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] | 7374 | struct lyxp_set *set, uint32_t options) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7375 | { |
| 7376 | LY_ERR rc; |
| 7377 | |
| 7378 | /* TODO */ |
| 7379 | (void)attr_axis; |
| 7380 | (void)all_desc; |
| 7381 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7382 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7383 | assert(exp->tok_len[*tok_idx] == 4); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7384 | if (set->type == LYXP_SET_SCNODE_SET) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 7385 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7386 | options |= LYXP_SKIP_EXPR; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7387 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7388 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7389 | rc = xpath_node(NULL, 0, set, options); |
| 7390 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7391 | assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4)); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7392 | rc = xpath_text(NULL, 0, set, options); |
| 7393 | } |
| 7394 | LY_CHECK_RET(rc); |
| 7395 | } |
| 7396 | } |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7397 | 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] | 7398 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7399 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7400 | |
| 7401 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7402 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7403 | 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] | 7404 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7405 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7406 | |
| 7407 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7408 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7409 | 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] | 7410 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7411 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7412 | |
| 7413 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7414 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 7415 | rc = eval_predicate(exp, tok_idx, set, options, 1); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7416 | LY_CHECK_RET(rc); |
| 7417 | } |
| 7418 | |
| 7419 | return LY_SUCCESS; |
| 7420 | } |
| 7421 | |
| 7422 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7423 | * @brief Evaluate RelativeLocationPath. Logs directly on error. |
| 7424 | * |
| 7425 | * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step |
| 7426 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7427 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7428 | * |
| 7429 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7430 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7431 | * @param[in] all_desc Whether to search all the descendants or children only. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7432 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7433 | * @param[in] options XPath options. |
| 7434 | * @return LY_ERR (YL_EINCOMPLETE on unresolved when) |
| 7435 | */ |
| 7436 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 7437 | eval_relative_location_path(const struct lyxp_expr *exp, uint16_t *tok_idx, ly_bool all_desc, struct lyxp_set *set, |
| 7438 | uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7439 | { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 7440 | ly_bool attr_axis; |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 7441 | LY_ERR rc = LY_SUCCESS; |
| 7442 | int scnode_skip_path = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7443 | |
| 7444 | goto step; |
| 7445 | do { |
| 7446 | /* evaluate '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7447 | if (exp->tok_len[*tok_idx] == 1) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7448 | all_desc = 0; |
| 7449 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7450 | assert(exp->tok_len[*tok_idx] == 2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7451 | all_desc = 1; |
| 7452 | } |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7453 | 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] | 7454 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7455 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7456 | |
| 7457 | step: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7458 | /* evaluate abbreviated axis '@'? if any */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7459 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7460 | attr_axis = 1; |
| 7461 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7462 | 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] | 7463 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7464 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7465 | } else { |
| 7466 | attr_axis = 0; |
| 7467 | } |
| 7468 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7469 | /* Step */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7470 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7471 | case LYXP_TOKEN_DOT: |
| 7472 | /* evaluate '.' */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7473 | if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7474 | rc = moveto_scnode_self(set, all_desc, options); |
| 7475 | } else { |
| 7476 | rc = moveto_self(set, all_desc, options); |
| 7477 | } |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 7478 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7479 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7480 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7481 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7482 | break; |
| 7483 | |
| 7484 | case LYXP_TOKEN_DDOT: |
| 7485 | /* evaluate '..' */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7486 | if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7487 | rc = moveto_scnode_parent(set, all_desc, options); |
| 7488 | } else { |
| 7489 | rc = moveto_parent(set, all_desc, options); |
| 7490 | } |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 7491 | LY_CHECK_GOTO(rc, cleanup); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7492 | 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] | 7493 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7494 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7495 | break; |
| 7496 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7497 | case LYXP_TOKEN_NAMETEST: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7498 | /* evaluate NameTest Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7499 | 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] | 7500 | if (rc == LY_ENOT) { |
| 7501 | assert(options & LYXP_SCNODE_ALL); |
| 7502 | /* skip the rest of this path */ |
| 7503 | rc = LY_SUCCESS; |
| 7504 | scnode_skip_path = 1; |
| 7505 | options |= LYXP_SKIP_EXPR; |
| 7506 | } |
| 7507 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7508 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7509 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7510 | case LYXP_TOKEN_NODETYPE: |
| 7511 | /* evaluate NodeType Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7512 | 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] | 7513 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7514 | break; |
| 7515 | |
| 7516 | default: |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 7517 | LOGINT(set->ctx); |
| 7518 | rc = LY_EINT; |
| 7519 | goto cleanup; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7520 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7521 | } 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] | 7522 | |
Michal Vasko | a036a6b | 2021-10-12 16:05:23 +0200 | [diff] [blame] | 7523 | cleanup: |
| 7524 | if (scnode_skip_path) { |
| 7525 | options &= ~LYXP_SKIP_EXPR; |
| 7526 | } |
| 7527 | return rc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7528 | } |
| 7529 | |
| 7530 | /** |
| 7531 | * @brief Evaluate AbsoluteLocationPath. Logs directly on error. |
| 7532 | * |
| 7533 | * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath |
| 7534 | * |
| 7535 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7536 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7537 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7538 | * @param[in] options XPath options. |
| 7539 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7540 | */ |
| 7541 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 7542 | 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] | 7543 | { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 7544 | ly_bool all_desc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7545 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7546 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7547 | /* no matter what tokens follow, we need to be at the root */ |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 7548 | LY_CHECK_RET(moveto_root(set, options)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7549 | } |
| 7550 | |
| 7551 | /* '/' RelativeLocationPath? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7552 | if (exp->tok_len[*tok_idx] == 1) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7553 | /* evaluate '/' - deferred */ |
| 7554 | all_desc = 0; |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7555 | 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] | 7556 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7557 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7558 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7559 | if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7560 | return LY_SUCCESS; |
| 7561 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7562 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7563 | case LYXP_TOKEN_DOT: |
| 7564 | case LYXP_TOKEN_DDOT: |
| 7565 | case LYXP_TOKEN_AT: |
| 7566 | case LYXP_TOKEN_NAMETEST: |
| 7567 | case LYXP_TOKEN_NODETYPE: |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 7568 | 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] | 7569 | break; |
| 7570 | default: |
| 7571 | break; |
| 7572 | } |
| 7573 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7574 | } else { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 7575 | /* '//' RelativeLocationPath */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7576 | /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */ |
| 7577 | all_desc = 1; |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7578 | 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] | 7579 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7580 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7581 | |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 7582 | 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] | 7583 | } |
| 7584 | |
| 7585 | return LY_SUCCESS; |
| 7586 | } |
| 7587 | |
| 7588 | /** |
| 7589 | * @brief Evaluate FunctionCall. Logs directly on error. |
| 7590 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7591 | * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7592 | * |
| 7593 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7594 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7595 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7596 | * @param[in] options XPath options. |
| 7597 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7598 | */ |
| 7599 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 7600 | 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] | 7601 | { |
| 7602 | LY_ERR rc; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7603 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7604 | 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] | 7605 | uint16_t arg_count = 0, i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7606 | struct lyxp_set **args = NULL, **args_aux; |
| 7607 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7608 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7609 | /* FunctionName */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7610 | switch (exp->tok_len[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7611 | case 3: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7612 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7613 | xpath_func = &xpath_not; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7614 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7615 | xpath_func = &xpath_sum; |
| 7616 | } |
| 7617 | break; |
| 7618 | case 4: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7619 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7620 | xpath_func = &xpath_lang; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7621 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7622 | xpath_func = &xpath_last; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7623 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7624 | xpath_func = &xpath_name; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7625 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7626 | xpath_func = &xpath_true; |
| 7627 | } |
| 7628 | break; |
| 7629 | case 5: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7630 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7631 | xpath_func = &xpath_count; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7632 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7633 | xpath_func = &xpath_false; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7634 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7635 | xpath_func = &xpath_floor; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7636 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7637 | xpath_func = &xpath_round; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7638 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7639 | xpath_func = &xpath_deref; |
| 7640 | } |
| 7641 | break; |
| 7642 | case 6: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7643 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7644 | xpath_func = &xpath_concat; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7645 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7646 | xpath_func = &xpath_number; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7647 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7648 | xpath_func = &xpath_string; |
| 7649 | } |
| 7650 | break; |
| 7651 | case 7: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7652 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7653 | xpath_func = &xpath_boolean; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7654 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7655 | xpath_func = &xpath_ceiling; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7656 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7657 | xpath_func = &xpath_current; |
| 7658 | } |
| 7659 | break; |
| 7660 | case 8: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7661 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7662 | xpath_func = &xpath_contains; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7663 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7664 | xpath_func = &xpath_position; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7665 | } 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] | 7666 | xpath_func = &xpath_re_match; |
| 7667 | } |
| 7668 | break; |
| 7669 | case 9: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7670 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7671 | xpath_func = &xpath_substring; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7672 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7673 | xpath_func = &xpath_translate; |
| 7674 | } |
| 7675 | break; |
| 7676 | case 10: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7677 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7678 | xpath_func = &xpath_local_name; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7679 | } 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] | 7680 | xpath_func = &xpath_enum_value; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7681 | } 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] | 7682 | xpath_func = &xpath_bit_is_set; |
| 7683 | } |
| 7684 | break; |
| 7685 | case 11: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7686 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7687 | xpath_func = &xpath_starts_with; |
| 7688 | } |
| 7689 | break; |
| 7690 | case 12: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7691 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7692 | xpath_func = &xpath_derived_from; |
| 7693 | } |
| 7694 | break; |
| 7695 | case 13: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7696 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7697 | xpath_func = &xpath_namespace_uri; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7698 | } 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] | 7699 | xpath_func = &xpath_string_length; |
| 7700 | } |
| 7701 | break; |
| 7702 | case 15: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7703 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7704 | xpath_func = &xpath_normalize_space; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7705 | } 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] | 7706 | xpath_func = &xpath_substring_after; |
| 7707 | } |
| 7708 | break; |
| 7709 | case 16: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7710 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7711 | xpath_func = &xpath_substring_before; |
| 7712 | } |
| 7713 | break; |
| 7714 | case 20: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7715 | 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] | 7716 | xpath_func = &xpath_derived_from_or_self; |
| 7717 | } |
| 7718 | break; |
| 7719 | } |
| 7720 | |
| 7721 | if (!xpath_func) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 7722 | 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] | 7723 | return LY_EVALID; |
| 7724 | } |
| 7725 | } |
| 7726 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7727 | 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] | 7728 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7729 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7730 | |
| 7731 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7732 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7733 | 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] | 7734 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7735 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7736 | |
| 7737 | /* ( Expr ( ',' Expr )* )? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7738 | if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7739 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7740 | args = malloc(sizeof *args); |
| 7741 | LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup); |
| 7742 | arg_count = 1; |
| 7743 | args[0] = set_copy(set); |
| 7744 | if (!args[0]) { |
| 7745 | rc = LY_EMEM; |
| 7746 | goto cleanup; |
| 7747 | } |
| 7748 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7749 | rc = eval_expr_select(exp, tok_idx, 0, args[0], options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7750 | LY_CHECK_GOTO(rc, cleanup); |
| 7751 | } else { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7752 | 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] | 7753 | LY_CHECK_GOTO(rc, cleanup); |
| 7754 | } |
| 7755 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7756 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7757 | 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] | 7758 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7759 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7760 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7761 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7762 | ++arg_count; |
| 7763 | args_aux = realloc(args, arg_count * sizeof *args); |
| 7764 | LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup); |
| 7765 | args = args_aux; |
| 7766 | args[arg_count - 1] = set_copy(set); |
| 7767 | if (!args[arg_count - 1]) { |
| 7768 | rc = LY_EMEM; |
| 7769 | goto cleanup; |
| 7770 | } |
| 7771 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7772 | 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] | 7773 | LY_CHECK_GOTO(rc, cleanup); |
| 7774 | } else { |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7775 | 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] | 7776 | LY_CHECK_GOTO(rc, cleanup); |
| 7777 | } |
| 7778 | } |
| 7779 | |
| 7780 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7781 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7782 | 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] | 7783 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7784 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7785 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7786 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7787 | /* evaluate function */ |
| 7788 | rc = xpath_func(args, arg_count, set, options); |
| 7789 | |
| 7790 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7791 | /* merge all nodes from arg evaluations */ |
| 7792 | for (i = 0; i < arg_count; ++i) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 7793 | set_scnode_clear_ctx(args[i], LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7794 | lyxp_set_scnode_merge(set, args[i]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7795 | } |
| 7796 | } |
| 7797 | } else { |
| 7798 | rc = LY_SUCCESS; |
| 7799 | } |
| 7800 | |
| 7801 | cleanup: |
| 7802 | for (i = 0; i < arg_count; ++i) { |
| 7803 | lyxp_set_free(args[i]); |
| 7804 | } |
| 7805 | free(args); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7806 | return rc; |
| 7807 | } |
| 7808 | |
| 7809 | /** |
| 7810 | * @brief Evaluate Number. Logs directly on error. |
| 7811 | * |
| 7812 | * @param[in] ctx Context for errors. |
| 7813 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7814 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7815 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7816 | * @return LY_ERR |
| 7817 | */ |
| 7818 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 7819 | 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] | 7820 | { |
| 7821 | long double num; |
| 7822 | char *endptr; |
| 7823 | |
| 7824 | if (set) { |
| 7825 | errno = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7826 | num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7827 | if (errno) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 7828 | LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]); |
| 7829 | 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] | 7830 | 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] | 7831 | return LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7832 | } 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] | 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.", |
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]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7836 | return LY_EVALID; |
| 7837 | } |
| 7838 | |
| 7839 | set_fill_number(set, num); |
| 7840 | } |
| 7841 | |
| 7842 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7843 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7844 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7845 | return LY_SUCCESS; |
| 7846 | } |
| 7847 | |
aPiecek | df23eee | 2021-10-07 12:21:50 +0200 | [diff] [blame] | 7848 | LY_ERR |
| 7849 | lyxp_vars_find(struct lyxp_var *vars, const char *name, size_t name_len, struct lyxp_var **var) |
| 7850 | { |
| 7851 | LY_ERR ret = LY_ENOTFOUND; |
| 7852 | LY_ARRAY_COUNT_TYPE u; |
| 7853 | |
| 7854 | assert(vars && name); |
| 7855 | |
| 7856 | name_len = name_len ? name_len : strlen(name); |
| 7857 | |
| 7858 | LY_ARRAY_FOR(vars, u) { |
| 7859 | if (!strncmp(vars[u].name, name, name_len)) { |
| 7860 | ret = LY_SUCCESS; |
| 7861 | break; |
| 7862 | } |
| 7863 | } |
| 7864 | |
| 7865 | if (var && !ret) { |
| 7866 | *var = &vars[u]; |
| 7867 | } |
| 7868 | |
| 7869 | return ret; |
| 7870 | } |
| 7871 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7872 | /** |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 7873 | * @brief Evaluate VariableReference. |
| 7874 | * |
| 7875 | * @param[in] exp Parsed XPath expression. |
| 7876 | * @param[in] tok_idx Position in the expression @p exp. |
| 7877 | * @param[in] vars [Sized array](@ref sizedarrays) of XPath variables. |
| 7878 | * @param[in,out] set Context and result set. |
| 7879 | * @param[in] options XPath options. |
| 7880 | * @return LY_ERR value. |
| 7881 | */ |
| 7882 | static LY_ERR |
| 7883 | eval_variable_reference(const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, uint32_t options) |
| 7884 | { |
| 7885 | LY_ERR ret; |
| 7886 | const char *name; |
| 7887 | struct lyxp_var *var; |
| 7888 | const struct lyxp_var *vars; |
| 7889 | struct lyxp_expr *tokens = NULL; |
| 7890 | uint16_t token_index; |
| 7891 | |
| 7892 | vars = set->vars; |
| 7893 | |
| 7894 | /* Find out the name and value of the variable. */ |
| 7895 | name = &exp->expr[exp->tok_pos[*tok_idx]]; |
| 7896 | ret = lyxp_vars_find((struct lyxp_var *)vars, name, exp->tok_len[*tok_idx], &var); |
| 7897 | LY_CHECK_ERR_RET(ret, LOGERR(set->ctx, ret, |
| 7898 | "XPath variable \"%s\" not defined.", name), ret); |
| 7899 | |
| 7900 | /* Parse value. */ |
| 7901 | ret = lyxp_expr_parse(set->ctx, var->value, 0, 1, &tokens); |
| 7902 | LY_CHECK_GOTO(ret, cleanup); |
| 7903 | |
| 7904 | /* Evaluate value. */ |
| 7905 | token_index = 0; |
| 7906 | ret = eval_expr_select(tokens, &token_index, 0, set, options); |
| 7907 | LY_CHECK_GOTO(ret, cleanup); |
| 7908 | |
| 7909 | cleanup: |
| 7910 | lyxp_expr_free(set->ctx, tokens); |
| 7911 | |
| 7912 | return ret; |
| 7913 | } |
| 7914 | |
| 7915 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7916 | * @brief Evaluate PathExpr. Logs directly on error. |
| 7917 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7918 | * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate* |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7919 | * | PrimaryExpr Predicate* '/' RelativeLocationPath |
| 7920 | * | PrimaryExpr Predicate* '//' RelativeLocationPath |
| 7921 | * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 7922 | * [10] PrimaryExpr ::= VariableReference | '(' Expr ')' | Literal | Number | FunctionCall |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7923 | * |
| 7924 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7925 | * @param[in] tok_idx Position in the expression @p exp. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7926 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7927 | * @param[in] options XPath options. |
| 7928 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7929 | */ |
| 7930 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 7931 | 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] | 7932 | { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 7933 | ly_bool all_desc, parent_pos_pred; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7934 | LY_ERR rc; |
| 7935 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7936 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7937 | case LYXP_TOKEN_PAR1: |
| 7938 | /* '(' Expr ')' */ |
| 7939 | |
| 7940 | /* '(' */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7941 | 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] | 7942 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7943 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7944 | |
| 7945 | /* Expr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7946 | rc = eval_expr_select(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7947 | LY_CHECK_RET(rc); |
| 7948 | |
| 7949 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7950 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7951 | 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] | 7952 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7953 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7954 | |
| 7955 | parent_pos_pred = 0; |
| 7956 | goto predicate; |
| 7957 | |
| 7958 | case LYXP_TOKEN_DOT: |
| 7959 | case LYXP_TOKEN_DDOT: |
| 7960 | case LYXP_TOKEN_AT: |
| 7961 | case LYXP_TOKEN_NAMETEST: |
| 7962 | case LYXP_TOKEN_NODETYPE: |
| 7963 | /* RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7964 | rc = eval_relative_location_path(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7965 | LY_CHECK_RET(rc); |
| 7966 | break; |
| 7967 | |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 7968 | case LYXP_TOKEN_VARREF: |
| 7969 | /* VariableReference */ |
| 7970 | rc = eval_variable_reference(exp, tok_idx, set, options); |
| 7971 | LY_CHECK_RET(rc); |
| 7972 | ++(*tok_idx); |
| 7973 | |
| 7974 | parent_pos_pred = 1; |
| 7975 | goto predicate; |
| 7976 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7977 | case LYXP_TOKEN_FUNCNAME: |
| 7978 | /* FunctionCall */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7979 | rc = eval_function_call(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7980 | LY_CHECK_RET(rc); |
| 7981 | |
| 7982 | parent_pos_pred = 1; |
| 7983 | goto predicate; |
| 7984 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 7985 | case LYXP_TOKEN_OPER_PATH: |
| 7986 | case LYXP_TOKEN_OPER_RPATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7987 | /* AbsoluteLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7988 | rc = eval_absolute_location_path(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7989 | LY_CHECK_RET(rc); |
| 7990 | break; |
| 7991 | |
| 7992 | case LYXP_TOKEN_LITERAL: |
| 7993 | /* Literal */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 7994 | if ((options & LYXP_SKIP_EXPR) || (options & LYXP_SCNODE_ALL)) { |
| 7995 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 7996 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7997 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7998 | eval_literal(exp, tok_idx, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7999 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8000 | eval_literal(exp, tok_idx, set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8001 | } |
| 8002 | |
| 8003 | parent_pos_pred = 1; |
| 8004 | goto predicate; |
| 8005 | |
| 8006 | case LYXP_TOKEN_NUMBER: |
| 8007 | /* Number */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8008 | if ((options & LYXP_SKIP_EXPR) || (options & LYXP_SCNODE_ALL)) { |
| 8009 | if (!(options & LYXP_SKIP_EXPR)) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 8010 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8011 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8012 | rc = eval_number(NULL, exp, tok_idx, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8013 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8014 | rc = eval_number(set->ctx, exp, tok_idx, set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8015 | } |
| 8016 | LY_CHECK_RET(rc); |
| 8017 | |
| 8018 | parent_pos_pred = 1; |
| 8019 | goto predicate; |
| 8020 | |
| 8021 | default: |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 8022 | 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] | 8023 | return LY_EVALID; |
| 8024 | } |
| 8025 | |
| 8026 | return LY_SUCCESS; |
| 8027 | |
| 8028 | predicate: |
| 8029 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8030 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 8031 | rc = eval_predicate(exp, tok_idx, set, options, parent_pos_pred); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8032 | LY_CHECK_RET(rc); |
| 8033 | } |
| 8034 | |
| 8035 | /* ('/' or '//') RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8036 | 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] | 8037 | |
| 8038 | /* evaluate '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8039 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8040 | all_desc = 0; |
| 8041 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8042 | all_desc = 1; |
| 8043 | } |
| 8044 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8045 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 8046 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8047 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8048 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8049 | rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8050 | LY_CHECK_RET(rc); |
| 8051 | } |
| 8052 | |
| 8053 | return LY_SUCCESS; |
| 8054 | } |
| 8055 | |
| 8056 | /** |
| 8057 | * @brief Evaluate UnionExpr. Logs directly on error. |
| 8058 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8059 | * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8060 | * |
| 8061 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8062 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8063 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8064 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8065 | * @param[in] options XPath options. |
| 8066 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8067 | */ |
| 8068 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 8069 | 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] | 8070 | { |
| 8071 | LY_ERR rc = LY_SUCCESS; |
| 8072 | struct lyxp_set orig_set, set2; |
| 8073 | uint16_t i; |
| 8074 | |
| 8075 | assert(repeat); |
| 8076 | |
| 8077 | set_init(&orig_set, set); |
| 8078 | set_init(&set2, set); |
| 8079 | |
| 8080 | set_fill_set(&orig_set, set); |
| 8081 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8082 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8083 | LY_CHECK_GOTO(rc, cleanup); |
| 8084 | |
| 8085 | /* ('|' PathExpr)* */ |
| 8086 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8087 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8088 | 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] | 8089 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8090 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8091 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8092 | if (options & LYXP_SKIP_EXPR) { |
| 8093 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8094 | LY_CHECK_GOTO(rc, cleanup); |
| 8095 | continue; |
| 8096 | } |
| 8097 | |
| 8098 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8099 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8100 | LY_CHECK_GOTO(rc, cleanup); |
| 8101 | |
| 8102 | /* eval */ |
| 8103 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 8104 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8105 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8106 | rc = moveto_union(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8107 | LY_CHECK_GOTO(rc, cleanup); |
| 8108 | } |
| 8109 | } |
| 8110 | |
| 8111 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8112 | lyxp_set_free_content(&orig_set); |
| 8113 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8114 | return rc; |
| 8115 | } |
| 8116 | |
| 8117 | /** |
| 8118 | * @brief Evaluate UnaryExpr. Logs directly on error. |
| 8119 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8120 | * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8121 | * |
| 8122 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8123 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8124 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8125 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8126 | * @param[in] options XPath options. |
| 8127 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8128 | */ |
| 8129 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 8130 | 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] | 8131 | { |
| 8132 | LY_ERR rc; |
| 8133 | uint16_t this_op, i; |
| 8134 | |
| 8135 | assert(repeat); |
| 8136 | |
| 8137 | /* ('-')+ */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8138 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8139 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8140 | 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] | 8141 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8142 | 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] | 8143 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8144 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8145 | } |
| 8146 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8147 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8148 | LY_CHECK_RET(rc); |
| 8149 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8150 | if (!(options & LYXP_SKIP_EXPR) && (repeat % 2)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8151 | if (options & LYXP_SCNODE_ALL) { |
| 8152 | warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]); |
| 8153 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8154 | 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] | 8155 | LY_CHECK_RET(rc); |
| 8156 | } |
| 8157 | } |
| 8158 | |
| 8159 | return LY_SUCCESS; |
| 8160 | } |
| 8161 | |
| 8162 | /** |
| 8163 | * @brief Evaluate MultiplicativeExpr. Logs directly on error. |
| 8164 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8165 | * [18] MultiplicativeExpr ::= UnaryExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8166 | * | MultiplicativeExpr '*' UnaryExpr |
| 8167 | * | MultiplicativeExpr 'div' UnaryExpr |
| 8168 | * | MultiplicativeExpr 'mod' UnaryExpr |
| 8169 | * |
| 8170 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8171 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8172 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8173 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8174 | * @param[in] options XPath options. |
| 8175 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8176 | */ |
| 8177 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 8178 | eval_multiplicative_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, |
| 8179 | uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8180 | { |
| 8181 | LY_ERR rc; |
| 8182 | uint16_t this_op; |
| 8183 | struct lyxp_set orig_set, set2; |
| 8184 | uint16_t i; |
| 8185 | |
| 8186 | assert(repeat); |
| 8187 | |
| 8188 | set_init(&orig_set, set); |
| 8189 | set_init(&set2, set); |
| 8190 | |
| 8191 | set_fill_set(&orig_set, set); |
| 8192 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8193 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8194 | LY_CHECK_GOTO(rc, cleanup); |
| 8195 | |
| 8196 | /* ('*' / 'div' / 'mod' UnaryExpr)* */ |
| 8197 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8198 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8199 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8200 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8201 | 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] | 8202 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8203 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8204 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8205 | if (options & LYXP_SKIP_EXPR) { |
| 8206 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8207 | LY_CHECK_GOTO(rc, cleanup); |
| 8208 | continue; |
| 8209 | } |
| 8210 | |
| 8211 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8212 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8213 | LY_CHECK_GOTO(rc, cleanup); |
| 8214 | |
| 8215 | /* eval */ |
| 8216 | if (options & LYXP_SCNODE_ALL) { |
| 8217 | 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] | 8218 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 8219 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8220 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8221 | 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] | 8222 | LY_CHECK_GOTO(rc, cleanup); |
| 8223 | } |
| 8224 | } |
| 8225 | |
| 8226 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8227 | lyxp_set_free_content(&orig_set); |
| 8228 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8229 | return rc; |
| 8230 | } |
| 8231 | |
| 8232 | /** |
| 8233 | * @brief Evaluate AdditiveExpr. Logs directly on error. |
| 8234 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8235 | * [17] AdditiveExpr ::= MultiplicativeExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8236 | * | AdditiveExpr '+' MultiplicativeExpr |
| 8237 | * | AdditiveExpr '-' MultiplicativeExpr |
| 8238 | * |
| 8239 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8240 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8241 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8242 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8243 | * @param[in] options XPath options. |
| 8244 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8245 | */ |
| 8246 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 8247 | 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] | 8248 | { |
| 8249 | LY_ERR rc; |
| 8250 | uint16_t this_op; |
| 8251 | struct lyxp_set orig_set, set2; |
| 8252 | uint16_t i; |
| 8253 | |
| 8254 | assert(repeat); |
| 8255 | |
| 8256 | set_init(&orig_set, set); |
| 8257 | set_init(&set2, set); |
| 8258 | |
| 8259 | set_fill_set(&orig_set, set); |
| 8260 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8261 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8262 | LY_CHECK_GOTO(rc, cleanup); |
| 8263 | |
| 8264 | /* ('+' / '-' MultiplicativeExpr)* */ |
| 8265 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8266 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8267 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8268 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8269 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 8270 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8271 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8272 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8273 | if (options & LYXP_SKIP_EXPR) { |
| 8274 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8275 | LY_CHECK_GOTO(rc, cleanup); |
| 8276 | continue; |
| 8277 | } |
| 8278 | |
| 8279 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8280 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8281 | LY_CHECK_GOTO(rc, cleanup); |
| 8282 | |
| 8283 | /* eval */ |
| 8284 | if (options & LYXP_SCNODE_ALL) { |
| 8285 | 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] | 8286 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 8287 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8288 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8289 | 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] | 8290 | LY_CHECK_GOTO(rc, cleanup); |
| 8291 | } |
| 8292 | } |
| 8293 | |
| 8294 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8295 | lyxp_set_free_content(&orig_set); |
| 8296 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8297 | return rc; |
| 8298 | } |
| 8299 | |
| 8300 | /** |
| 8301 | * @brief Evaluate RelationalExpr. Logs directly on error. |
| 8302 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8303 | * [16] RelationalExpr ::= AdditiveExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8304 | * | RelationalExpr '<' AdditiveExpr |
| 8305 | * | RelationalExpr '>' AdditiveExpr |
| 8306 | * | RelationalExpr '<=' AdditiveExpr |
| 8307 | * | RelationalExpr '>=' AdditiveExpr |
| 8308 | * |
| 8309 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8310 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8311 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8312 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8313 | * @param[in] options XPath options. |
| 8314 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8315 | */ |
| 8316 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 8317 | 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] | 8318 | { |
| 8319 | LY_ERR rc; |
| 8320 | uint16_t this_op; |
| 8321 | struct lyxp_set orig_set, set2; |
| 8322 | uint16_t i; |
| 8323 | |
| 8324 | assert(repeat); |
| 8325 | |
| 8326 | set_init(&orig_set, set); |
| 8327 | set_init(&set2, set); |
| 8328 | |
| 8329 | set_fill_set(&orig_set, set); |
| 8330 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8331 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8332 | LY_CHECK_GOTO(rc, cleanup); |
| 8333 | |
| 8334 | /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */ |
| 8335 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8336 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8337 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8338 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8339 | 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] | 8340 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8341 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8342 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8343 | if (options & LYXP_SKIP_EXPR) { |
| 8344 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8345 | LY_CHECK_GOTO(rc, cleanup); |
| 8346 | continue; |
| 8347 | } |
| 8348 | |
| 8349 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8350 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8351 | LY_CHECK_GOTO(rc, cleanup); |
| 8352 | |
| 8353 | /* eval */ |
| 8354 | if (options & LYXP_SCNODE_ALL) { |
| 8355 | 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] | 8356 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 8357 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8358 | } else { |
| 8359 | rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options); |
| 8360 | LY_CHECK_GOTO(rc, cleanup); |
| 8361 | } |
| 8362 | } |
| 8363 | |
| 8364 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8365 | lyxp_set_free_content(&orig_set); |
| 8366 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8367 | return rc; |
| 8368 | } |
| 8369 | |
| 8370 | /** |
| 8371 | * @brief Evaluate EqualityExpr. Logs directly on error. |
| 8372 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8373 | * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8374 | * | EqualityExpr '!=' RelationalExpr |
| 8375 | * |
| 8376 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8377 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8378 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8379 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8380 | * @param[in] options XPath options. |
| 8381 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8382 | */ |
| 8383 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 8384 | 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] | 8385 | { |
| 8386 | LY_ERR rc; |
| 8387 | uint16_t this_op; |
| 8388 | struct lyxp_set orig_set, set2; |
| 8389 | uint16_t i; |
| 8390 | |
| 8391 | assert(repeat); |
| 8392 | |
| 8393 | set_init(&orig_set, set); |
| 8394 | set_init(&set2, set); |
| 8395 | |
| 8396 | set_fill_set(&orig_set, set); |
| 8397 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8398 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8399 | LY_CHECK_GOTO(rc, cleanup); |
| 8400 | |
| 8401 | /* ('=' / '!=' RelationalExpr)* */ |
| 8402 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8403 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8404 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8405 | 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] | 8406 | 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] | 8407 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8408 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8409 | |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8410 | if (options & LYXP_SKIP_EXPR) { |
| 8411 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8412 | LY_CHECK_GOTO(rc, cleanup); |
| 8413 | continue; |
| 8414 | } |
| 8415 | |
| 8416 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8417 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8418 | LY_CHECK_GOTO(rc, cleanup); |
| 8419 | |
| 8420 | /* eval */ |
| 8421 | if (options & LYXP_SCNODE_ALL) { |
| 8422 | 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] | 8423 | warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1); |
| 8424 | 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] | 8425 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 8426 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8427 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8428 | rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options); |
| 8429 | LY_CHECK_GOTO(rc, cleanup); |
| 8430 | } |
| 8431 | } |
| 8432 | |
| 8433 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8434 | lyxp_set_free_content(&orig_set); |
| 8435 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8436 | return rc; |
| 8437 | } |
| 8438 | |
| 8439 | /** |
| 8440 | * @brief Evaluate AndExpr. Logs directly on error. |
| 8441 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8442 | * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8443 | * |
| 8444 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8445 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8446 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8447 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8448 | * @param[in] options XPath options. |
| 8449 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8450 | */ |
| 8451 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 8452 | 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] | 8453 | { |
| 8454 | LY_ERR rc; |
| 8455 | struct lyxp_set orig_set, set2; |
| 8456 | uint16_t i; |
| 8457 | |
| 8458 | assert(repeat); |
| 8459 | |
| 8460 | set_init(&orig_set, set); |
| 8461 | set_init(&set2, set); |
| 8462 | |
| 8463 | set_fill_set(&orig_set, set); |
| 8464 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8465 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8466 | LY_CHECK_GOTO(rc, cleanup); |
| 8467 | |
| 8468 | /* cast to boolean, we know that will be the final result */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8469 | if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 8470 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8471 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8472 | lyxp_set_cast(set, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8473 | } |
| 8474 | |
| 8475 | /* ('and' EqualityExpr)* */ |
| 8476 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8477 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8478 | 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] | 8479 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8480 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8481 | |
| 8482 | /* lazy evaluation */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8483 | if ((options & LYXP_SKIP_EXPR) || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) { |
| 8484 | 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] | 8485 | LY_CHECK_GOTO(rc, cleanup); |
| 8486 | continue; |
| 8487 | } |
| 8488 | |
| 8489 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8490 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8491 | LY_CHECK_GOTO(rc, cleanup); |
| 8492 | |
| 8493 | /* eval - just get boolean value actually */ |
| 8494 | if (set->type == LYXP_SET_SCNODE_SET) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 8495 | set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 8496 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8497 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8498 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8499 | set_fill_set(set, &set2); |
| 8500 | } |
| 8501 | } |
| 8502 | |
| 8503 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8504 | lyxp_set_free_content(&orig_set); |
| 8505 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8506 | return rc; |
| 8507 | } |
| 8508 | |
| 8509 | /** |
| 8510 | * @brief Evaluate OrExpr. Logs directly on error. |
| 8511 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8512 | * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8513 | * |
| 8514 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8515 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8516 | * @param[in] repeat How many times this expression is repeated. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8517 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8518 | * @param[in] options XPath options. |
| 8519 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8520 | */ |
| 8521 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 8522 | 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] | 8523 | { |
| 8524 | LY_ERR rc; |
| 8525 | struct lyxp_set orig_set, set2; |
| 8526 | uint16_t i; |
| 8527 | |
| 8528 | assert(repeat); |
| 8529 | |
| 8530 | set_init(&orig_set, set); |
| 8531 | set_init(&set2, set); |
| 8532 | |
| 8533 | set_fill_set(&orig_set, set); |
| 8534 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8535 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8536 | LY_CHECK_GOTO(rc, cleanup); |
| 8537 | |
| 8538 | /* cast to boolean, we know that will be the final result */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8539 | if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 8540 | set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8541 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8542 | lyxp_set_cast(set, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8543 | } |
| 8544 | |
| 8545 | /* ('or' AndExpr)* */ |
| 8546 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8547 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG); |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8548 | 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] | 8549 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8550 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8551 | |
| 8552 | /* lazy evaluation */ |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8553 | if ((options & LYXP_SKIP_EXPR) || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) { |
| 8554 | 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] | 8555 | LY_CHECK_GOTO(rc, cleanup); |
| 8556 | continue; |
| 8557 | } |
| 8558 | |
| 8559 | set_fill_set(&set2, &orig_set); |
| 8560 | /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one), |
| 8561 | * but it does not matter */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8562 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8563 | LY_CHECK_GOTO(rc, cleanup); |
| 8564 | |
| 8565 | /* eval - just get boolean value actually */ |
| 8566 | if (set->type == LYXP_SET_SCNODE_SET) { |
Michal Vasko | 1a09b21 | 2021-05-06 13:00:10 +0200 | [diff] [blame] | 8567 | set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 8568 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8569 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8570 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8571 | set_fill_set(set, &set2); |
| 8572 | } |
| 8573 | } |
| 8574 | |
| 8575 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8576 | lyxp_set_free_content(&orig_set); |
| 8577 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8578 | return rc; |
| 8579 | } |
| 8580 | |
| 8581 | /** |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8582 | * @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] | 8583 | * |
| 8584 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8585 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8586 | * @param[in] etype Expression type to evaluate. |
aPiecek | 8b0cc15 | 2021-05-31 16:40:31 +0200 | [diff] [blame] | 8587 | * @param[in,out] set Context and result set. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8588 | * @param[in] options XPath options. |
| 8589 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8590 | */ |
| 8591 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 8592 | eval_expr_select(const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype, struct lyxp_set *set, |
| 8593 | uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8594 | { |
| 8595 | uint16_t i, count; |
| 8596 | enum lyxp_expr_type next_etype; |
| 8597 | LY_ERR rc; |
| 8598 | |
| 8599 | /* process operator repeats */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8600 | if (!exp->repeat[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8601 | next_etype = LYXP_EXPR_NONE; |
| 8602 | } else { |
| 8603 | /* find etype repeat */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 8604 | for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i) {} |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8605 | |
| 8606 | /* select one-priority lower because etype expression called us */ |
| 8607 | if (i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8608 | next_etype = exp->repeat[*tok_idx][i - 1]; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8609 | /* count repeats for that expression */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 8610 | 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] | 8611 | } else { |
| 8612 | next_etype = LYXP_EXPR_NONE; |
| 8613 | } |
| 8614 | } |
| 8615 | |
| 8616 | /* decide what expression are we parsing based on the repeat */ |
| 8617 | switch (next_etype) { |
| 8618 | case LYXP_EXPR_OR: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8619 | rc = eval_or_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8620 | break; |
| 8621 | case LYXP_EXPR_AND: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8622 | rc = eval_and_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8623 | break; |
| 8624 | case LYXP_EXPR_EQUALITY: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8625 | rc = eval_equality_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8626 | break; |
| 8627 | case LYXP_EXPR_RELATIONAL: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8628 | rc = eval_relational_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8629 | break; |
| 8630 | case LYXP_EXPR_ADDITIVE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8631 | rc = eval_additive_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8632 | break; |
| 8633 | case LYXP_EXPR_MULTIPLICATIVE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8634 | rc = eval_multiplicative_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8635 | break; |
| 8636 | case LYXP_EXPR_UNARY: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8637 | rc = eval_unary_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8638 | break; |
| 8639 | case LYXP_EXPR_UNION: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8640 | rc = eval_union_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8641 | break; |
| 8642 | case LYXP_EXPR_NONE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8643 | rc = eval_path_expr(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8644 | break; |
| 8645 | default: |
| 8646 | LOGINT_RET(set->ctx); |
| 8647 | } |
| 8648 | |
| 8649 | return rc; |
| 8650 | } |
| 8651 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8652 | /** |
| 8653 | * @brief Get root type. |
| 8654 | * |
| 8655 | * @param[in] ctx_node Context node. |
| 8656 | * @param[in] ctx_scnode Schema context node. |
| 8657 | * @param[in] options XPath options. |
| 8658 | * @return Root type. |
| 8659 | */ |
| 8660 | static enum lyxp_node_type |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 8661 | 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] | 8662 | { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 8663 | const struct lysc_node *op; |
| 8664 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8665 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8666 | /* schema */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 8667 | 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] | 8668 | |
| 8669 | if (op || (options & LYXP_SCNODE)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8670 | /* general root that can access everything */ |
| 8671 | return LYXP_NODE_ROOT; |
| 8672 | } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) { |
| 8673 | /* root context node can access only config data (because we said so, it is unspecified) */ |
| 8674 | return LYXP_NODE_ROOT_CONFIG; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8675 | } |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 8676 | return LYXP_NODE_ROOT; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8677 | } |
| 8678 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8679 | /* data */ |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 8680 | op = ctx_node ? ctx_node->schema : NULL; |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 8681 | 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] | 8682 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8683 | if (op || !(options & LYXP_SCHEMA)) { |
| 8684 | /* general root that can access everything */ |
| 8685 | return LYXP_NODE_ROOT; |
Christian Hopps | b6ecaea | 2021-02-06 09:45:38 -0500 | [diff] [blame] | 8686 | } 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] | 8687 | /* root context node can access only config data (because we said so, it is unspecified) */ |
| 8688 | return LYXP_NODE_ROOT_CONFIG; |
| 8689 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8690 | return LYXP_NODE_ROOT; |
| 8691 | } |
| 8692 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8693 | LY_ERR |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 8694 | 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] | 8695 | 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] | 8696 | const struct lyxp_var *vars, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8697 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8698 | uint16_t tok_idx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8699 | LY_ERR rc; |
| 8700 | |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 8701 | LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL); |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 8702 | if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8703 | LOGARG(NULL, "Current module must be set if schema format is used."); |
| 8704 | return LY_EINVAL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8705 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8706 | |
Michal Vasko | d3bb12f | 2020-12-04 14:33:09 +0100 | [diff] [blame] | 8707 | if (tree) { |
| 8708 | /* adjust the pointer to be the first top-level sibling */ |
| 8709 | while (tree->parent) { |
| 8710 | tree = lyd_parent(tree); |
| 8711 | } |
| 8712 | tree = lyd_first_sibling(tree); |
| 8713 | } |
| 8714 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8715 | /* prepare set for evaluation */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8716 | memset(set, 0, sizeof *set); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8717 | set->type = LYXP_SET_NODE_SET; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8718 | set->root_type = lyxp_get_root_type(ctx_node, NULL, options); |
| 8719 | set_insert_node(set, (struct lyd_node *)ctx_node, 0, ctx_node ? LYXP_NODE_ELEM : set->root_type, 0); |
| 8720 | |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 8721 | set->ctx = (struct ly_ctx *)ctx; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8722 | set->cur_node = ctx_node; |
| 8723 | for (set->context_op = ctx_node ? ctx_node->schema : NULL; |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 8724 | set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); |
| 8725 | set->context_op = set->context_op->parent) {} |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 8726 | set->tree = tree; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8727 | set->cur_mod = cur_mod; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8728 | set->format = format; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8729 | set->prefix_data = prefix_data; |
aPiecek | fba7536 | 2021-10-07 12:39:48 +0200 | [diff] [blame] | 8730 | set->vars = vars; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8731 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 8732 | LOG_LOCSET(NULL, set->cur_node, NULL, NULL); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 8733 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8734 | /* evaluate */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8735 | rc = eval_expr_select(exp, &tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8736 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8737 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8738 | } |
| 8739 | |
Michal Vasko | 0245d50 | 2021-12-13 17:05:06 +0100 | [diff] [blame] | 8740 | if (set->cur_node) { |
| 8741 | LOG_LOCBACK(0, 1, 0, 0); |
| 8742 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8743 | return rc; |
| 8744 | } |
| 8745 | |
| 8746 | #if 0 |
| 8747 | |
| 8748 | /* full xml printing of set elements, not used currently */ |
| 8749 | |
| 8750 | void |
| 8751 | lyxp_set_print_xml(FILE *f, struct lyxp_set *set) |
| 8752 | { |
| 8753 | uint32_t i; |
| 8754 | char *str_num; |
| 8755 | struct lyout out; |
| 8756 | |
| 8757 | memset(&out, 0, sizeof out); |
| 8758 | |
| 8759 | out.type = LYOUT_STREAM; |
| 8760 | out.method.f = f; |
| 8761 | |
| 8762 | switch (set->type) { |
| 8763 | case LYXP_SET_EMPTY: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8764 | ly_print_(&out, "Empty XPath set\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8765 | break; |
| 8766 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8767 | ly_print_(&out, "Boolean XPath set:\n"); |
| 8768 | ly_print_(&out, "%s\n\n", set->value.bool ? "true" : "false"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8769 | break; |
| 8770 | case LYXP_SET_STRING: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8771 | ly_print_(&out, "String XPath set:\n"); |
| 8772 | ly_print_(&out, "\"%s\"\n\n", set->value.str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8773 | break; |
| 8774 | case LYXP_SET_NUMBER: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8775 | ly_print_(&out, "Number XPath set:\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8776 | |
| 8777 | if (isnan(set->value.num)) { |
| 8778 | str_num = strdup("NaN"); |
| 8779 | } else if ((set->value.num == 0) || (set->value.num == -0.0f)) { |
| 8780 | str_num = strdup("0"); |
| 8781 | } else if (isinf(set->value.num) && !signbit(set->value.num)) { |
| 8782 | str_num = strdup("Infinity"); |
| 8783 | } else if (isinf(set->value.num) && signbit(set->value.num)) { |
| 8784 | str_num = strdup("-Infinity"); |
| 8785 | } else if ((long long)set->value.num == set->value.num) { |
| 8786 | if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) { |
| 8787 | str_num = NULL; |
| 8788 | } |
| 8789 | } else { |
| 8790 | if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) { |
| 8791 | str_num = NULL; |
| 8792 | } |
| 8793 | } |
| 8794 | if (!str_num) { |
| 8795 | LOGMEM; |
| 8796 | return; |
| 8797 | } |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8798 | ly_print_(&out, "%s\n\n", str_num); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8799 | free(str_num); |
| 8800 | break; |
| 8801 | case LYXP_SET_NODE_SET: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8802 | ly_print_(&out, "Node XPath set:\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8803 | |
| 8804 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8805 | ly_print_(&out, "%d. ", i + 1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8806 | switch (set->node_type[i]) { |
| 8807 | case LYXP_NODE_ROOT_ALL: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8808 | ly_print_(&out, "ROOT all\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8809 | break; |
| 8810 | case LYXP_NODE_ROOT_CONFIG: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8811 | ly_print_(&out, "ROOT config\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8812 | break; |
| 8813 | case LYXP_NODE_ROOT_STATE: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8814 | ly_print_(&out, "ROOT state\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8815 | break; |
| 8816 | case LYXP_NODE_ROOT_NOTIF: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8817 | 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] | 8818 | break; |
| 8819 | case LYXP_NODE_ROOT_RPC: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8820 | 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] | 8821 | break; |
| 8822 | case LYXP_NODE_ROOT_OUTPUT: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8823 | 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] | 8824 | break; |
| 8825 | case LYXP_NODE_ELEM: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8826 | ly_print_(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8827 | xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT); |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8828 | ly_print_(&out, "\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8829 | break; |
| 8830 | case LYXP_NODE_TEXT: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8831 | 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] | 8832 | break; |
| 8833 | case LYXP_NODE_ATTR: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8834 | 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] | 8835 | break; |
| 8836 | } |
| 8837 | } |
| 8838 | break; |
| 8839 | } |
| 8840 | } |
| 8841 | |
| 8842 | #endif |
| 8843 | |
| 8844 | LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8845 | lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8846 | { |
| 8847 | long double num; |
| 8848 | char *str; |
| 8849 | LY_ERR rc; |
| 8850 | |
| 8851 | if (!set || (set->type == target)) { |
| 8852 | return LY_SUCCESS; |
| 8853 | } |
| 8854 | |
| 8855 | /* it's not possible to convert anything into a node set */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8856 | assert(target != LYXP_SET_NODE_SET); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8857 | |
| 8858 | if (set->type == LYXP_SET_SCNODE_SET) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8859 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8860 | return LY_EINVAL; |
| 8861 | } |
| 8862 | |
| 8863 | /* to STRING */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8864 | 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] | 8865 | switch (set->type) { |
| 8866 | case LYXP_SET_NUMBER: |
| 8867 | if (isnan(set->val.num)) { |
| 8868 | set->val.str = strdup("NaN"); |
| 8869 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 8870 | } else if ((set->val.num == 0) || (set->val.num == -0.0f)) { |
| 8871 | set->val.str = strdup("0"); |
| 8872 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 8873 | } else if (isinf(set->val.num) && !signbit(set->val.num)) { |
| 8874 | set->val.str = strdup("Infinity"); |
| 8875 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 8876 | } else if (isinf(set->val.num) && signbit(set->val.num)) { |
| 8877 | set->val.str = strdup("-Infinity"); |
| 8878 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 8879 | } else if ((long long)set->val.num == set->val.num) { |
| 8880 | if (asprintf(&str, "%lld", (long long)set->val.num) == -1) { |
| 8881 | LOGMEM_RET(set->ctx); |
| 8882 | } |
| 8883 | set->val.str = str; |
| 8884 | } else { |
| 8885 | if (asprintf(&str, "%03.1Lf", set->val.num) == -1) { |
| 8886 | LOGMEM_RET(set->ctx); |
| 8887 | } |
| 8888 | set->val.str = str; |
| 8889 | } |
| 8890 | break; |
| 8891 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8892 | if (set->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8893 | set->val.str = strdup("true"); |
| 8894 | } else { |
| 8895 | set->val.str = strdup("false"); |
| 8896 | } |
| 8897 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM); |
| 8898 | break; |
| 8899 | case LYXP_SET_NODE_SET: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8900 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8901 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8902 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8903 | rc = cast_node_set_to_string(set, &str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8904 | LY_CHECK_RET(rc); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8905 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8906 | set->val.str = str; |
| 8907 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8908 | default: |
| 8909 | LOGINT_RET(set->ctx); |
| 8910 | } |
| 8911 | set->type = LYXP_SET_STRING; |
| 8912 | } |
| 8913 | |
| 8914 | /* to NUMBER */ |
| 8915 | if (target == LYXP_SET_NUMBER) { |
| 8916 | switch (set->type) { |
| 8917 | case LYXP_SET_STRING: |
| 8918 | num = cast_string_to_number(set->val.str); |
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.num = num; |
| 8921 | break; |
| 8922 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8923 | if (set->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8924 | set->val.num = 1; |
| 8925 | } else { |
| 8926 | set->val.num = 0; |
| 8927 | } |
| 8928 | break; |
| 8929 | default: |
| 8930 | LOGINT_RET(set->ctx); |
| 8931 | } |
| 8932 | set->type = LYXP_SET_NUMBER; |
| 8933 | } |
| 8934 | |
| 8935 | /* to BOOLEAN */ |
| 8936 | if (target == LYXP_SET_BOOLEAN) { |
| 8937 | switch (set->type) { |
| 8938 | case LYXP_SET_NUMBER: |
| 8939 | 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] | 8940 | set->val.bln = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8941 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8942 | set->val.bln = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8943 | } |
| 8944 | break; |
| 8945 | case LYXP_SET_STRING: |
| 8946 | if (set->val.str[0]) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8947 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8948 | set->val.bln = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8949 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8950 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8951 | set->val.bln = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8952 | } |
| 8953 | break; |
| 8954 | case LYXP_SET_NODE_SET: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8955 | if (set->used) { |
| 8956 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8957 | set->val.bln = 1; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8958 | } else { |
| 8959 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8960 | set->val.bln = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8961 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8962 | break; |
| 8963 | default: |
| 8964 | LOGINT_RET(set->ctx); |
| 8965 | } |
| 8966 | set->type = LYXP_SET_BOOLEAN; |
| 8967 | } |
| 8968 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8969 | return LY_SUCCESS; |
| 8970 | } |
| 8971 | |
| 8972 | LY_ERR |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 8973 | 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] | 8974 | 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] | 8975 | uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8976 | { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 8977 | LY_ERR ret; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8978 | uint16_t tok_idx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8979 | |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 8980 | LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL); |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 8981 | if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8982 | LOGARG(NULL, "Current module must be set if schema format is used."); |
| 8983 | return LY_EINVAL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8984 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8985 | |
| 8986 | /* prepare set for evaluation */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8987 | memset(set, 0, sizeof *set); |
| 8988 | set->type = LYXP_SET_SCNODE_SET; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8989 | set->root_type = lyxp_get_root_type(NULL, ctx_scnode, options); |
| 8990 | 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] | 8991 | set->val.scnodes[0].in_ctx = LYXP_SET_SCNODE_START; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8992 | |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 8993 | set->ctx = (struct ly_ctx *)ctx; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8994 | set->cur_scnode = ctx_scnode; |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 8995 | for (set->context_op = ctx_scnode; |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 8996 | set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); |
| 8997 | set->context_op = set->context_op->parent) {} |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8998 | set->cur_mod = cur_mod; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8999 | set->format = format; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 9000 | set->prefix_data = prefix_data; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9001 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 9002 | LOG_LOCSET(set->cur_scnode, NULL, NULL, NULL); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 9003 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9004 | /* evaluate */ |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 9005 | ret = eval_expr_select(exp, &tok_idx, 0, set, options); |
| 9006 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 9007 | LOG_LOCBACK(1, 0, 0, 0); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 9008 | return ret; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 9009 | } |
Michal Vasko | d43d71a | 2020-08-07 14:54:58 +0200 | [diff] [blame] | 9010 | |
| 9011 | API const char * |
| 9012 | lyxp_get_expr(const struct lyxp_expr *path) |
| 9013 | { |
| 9014 | if (!path) { |
| 9015 | return NULL; |
| 9016 | } |
| 9017 | |
| 9018 | return path->expr; |
| 9019 | } |