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 | */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 14 | #define _GNU_SOURCE |
Radek Krejci | f8dc59a | 2020-11-25 13:47:44 +0100 | [diff] [blame] | 15 | #define _POSIX_C_SOURCE 200809L /* strdup, strndup */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 16 | |
| 17 | /* needed by libmath functions isfinite(), isinf(), isnan(), signbit(), ... */ |
| 18 | #define _ISOC99_SOURCE |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 19 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 20 | #include "xpath.h" |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 21 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 22 | #include <assert.h> |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 23 | #include <ctype.h> |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 24 | #include <errno.h> |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 25 | #include <math.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 26 | #include <stdint.h> |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 27 | #include <stdio.h> |
| 28 | #include <stdlib.h> |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 29 | #include <string.h> |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 30 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 31 | #include "common.h" |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 32 | #include "compat.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 33 | #include "context.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 34 | #include "dict.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 35 | #include "hash_table.h" |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 36 | #include "out.h" |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 37 | #include "parser_data.h" |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 38 | #include "path.h" |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 39 | #include "plugins_types.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 40 | #include "printer_data.h" |
Michal Vasko | 1a7a7bd | 2020-10-16 14:39:15 +0200 | [diff] [blame] | 41 | #include "schema_compile_node.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 42 | #include "tree.h" |
| 43 | #include "tree_data_internal.h" |
| 44 | #include "tree_schema_internal.h" |
| 45 | #include "xml.h" |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 46 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 47 | static LY_ERR reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx); |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 48 | static LY_ERR eval_expr_select(const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype, |
| 49 | struct lyxp_set *set, uint32_t options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 50 | |
| 51 | /** |
| 52 | * @brief Print the type of an XPath \p set. |
| 53 | * |
| 54 | * @param[in] set Set to use. |
| 55 | * @return Set type string. |
| 56 | */ |
| 57 | static const char * |
| 58 | print_set_type(struct lyxp_set *set) |
| 59 | { |
| 60 | switch (set->type) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 61 | case LYXP_SET_NODE_SET: |
| 62 | return "node set"; |
| 63 | case LYXP_SET_SCNODE_SET: |
| 64 | return "schema node set"; |
| 65 | case LYXP_SET_BOOLEAN: |
| 66 | return "boolean"; |
| 67 | case LYXP_SET_NUMBER: |
| 68 | return "number"; |
| 69 | case LYXP_SET_STRING: |
| 70 | return "string"; |
| 71 | } |
| 72 | |
| 73 | return NULL; |
| 74 | } |
| 75 | |
Michal Vasko | 24cddf8 | 2020-06-01 08:17:01 +0200 | [diff] [blame] | 76 | const char * |
| 77 | lyxp_print_token(enum lyxp_token tok) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 78 | { |
| 79 | switch (tok) { |
| 80 | case LYXP_TOKEN_PAR1: |
| 81 | return "("; |
| 82 | case LYXP_TOKEN_PAR2: |
| 83 | return ")"; |
| 84 | case LYXP_TOKEN_BRACK1: |
| 85 | return "["; |
| 86 | case LYXP_TOKEN_BRACK2: |
| 87 | return "]"; |
| 88 | case LYXP_TOKEN_DOT: |
| 89 | return "."; |
| 90 | case LYXP_TOKEN_DDOT: |
| 91 | return ".."; |
| 92 | case LYXP_TOKEN_AT: |
| 93 | return "@"; |
| 94 | case LYXP_TOKEN_COMMA: |
| 95 | return ","; |
| 96 | case LYXP_TOKEN_NAMETEST: |
| 97 | return "NameTest"; |
| 98 | case LYXP_TOKEN_NODETYPE: |
| 99 | return "NodeType"; |
| 100 | case LYXP_TOKEN_FUNCNAME: |
| 101 | return "FunctionName"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 102 | case LYXP_TOKEN_OPER_LOG: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 103 | return "Operator(Logic)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 104 | case LYXP_TOKEN_OPER_EQUAL: |
| 105 | return "Operator(Equal)"; |
| 106 | case LYXP_TOKEN_OPER_NEQUAL: |
| 107 | return "Operator(Non-equal)"; |
| 108 | case LYXP_TOKEN_OPER_COMP: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 109 | return "Operator(Comparison)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 110 | case LYXP_TOKEN_OPER_MATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 111 | return "Operator(Math)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 112 | case LYXP_TOKEN_OPER_UNI: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 113 | return "Operator(Union)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 114 | case LYXP_TOKEN_OPER_PATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 115 | return "Operator(Path)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 116 | case LYXP_TOKEN_OPER_RPATH: |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 117 | return "Operator(Recursive Path)"; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 118 | case LYXP_TOKEN_LITERAL: |
| 119 | return "Literal"; |
| 120 | case LYXP_TOKEN_NUMBER: |
| 121 | return "Number"; |
| 122 | default: |
| 123 | LOGINT(NULL); |
| 124 | return ""; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * @brief Print the whole expression \p exp to debug output. |
| 130 | * |
| 131 | * @param[in] exp Expression to use. |
| 132 | */ |
| 133 | static void |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 134 | print_expr_struct_debug(const struct lyxp_expr *exp) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 135 | { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 136 | #define MSG_BUFFER_SIZE 128 |
| 137 | char tmp[MSG_BUFFER_SIZE]; |
Michal Vasko | 1fdd8fa | 2021-01-08 09:21:45 +0100 | [diff] [blame] | 138 | uint32_t i, j; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 139 | |
Radek Krejci | 52b6d51 | 2020-10-12 12:33:17 +0200 | [diff] [blame] | 140 | if (!exp || (ly_ll < LY_LLDBG)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 141 | return; |
| 142 | } |
| 143 | |
| 144 | LOGDBG(LY_LDGXPATH, "expression \"%s\":", exp->expr); |
| 145 | for (i = 0; i < exp->used; ++i) { |
Michal Vasko | 24cddf8 | 2020-06-01 08:17:01 +0200 | [diff] [blame] | 146 | 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] | 147 | &exp->expr[exp->tok_pos[i]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 148 | if (exp->repeat[i]) { |
| 149 | sprintf(tmp + strlen(tmp), " (repeat %d", exp->repeat[i][0]); |
| 150 | for (j = 1; exp->repeat[i][j]; ++j) { |
| 151 | sprintf(tmp + strlen(tmp), ", %d", exp->repeat[i][j]); |
| 152 | } |
| 153 | strcat(tmp, ")"); |
| 154 | } |
| 155 | LOGDBG(LY_LDGXPATH, tmp); |
| 156 | } |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 157 | #undef MSG_BUFFER_SIZE |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | #ifndef NDEBUG |
| 161 | |
| 162 | /** |
| 163 | * @brief Print XPath set content to debug output. |
| 164 | * |
| 165 | * @param[in] set Set to print. |
| 166 | */ |
| 167 | static void |
| 168 | print_set_debug(struct lyxp_set *set) |
| 169 | { |
| 170 | uint32_t i; |
| 171 | char *str; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 172 | struct lyxp_set_node *item; |
| 173 | struct lyxp_set_scnode *sitem; |
| 174 | |
Radek Krejci | 52b6d51 | 2020-10-12 12:33:17 +0200 | [diff] [blame] | 175 | if (ly_ll < LY_LLDBG) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 176 | return; |
| 177 | } |
| 178 | |
| 179 | switch (set->type) { |
| 180 | case LYXP_SET_NODE_SET: |
| 181 | LOGDBG(LY_LDGXPATH, "set NODE SET:"); |
| 182 | for (i = 0; i < set->used; ++i) { |
| 183 | item = &set->val.nodes[i]; |
| 184 | |
| 185 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 186 | case LYXP_NODE_NONE: |
| 187 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): NONE", i + 1, item->pos); |
| 188 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 189 | case LYXP_NODE_ROOT: |
| 190 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT", i + 1, item->pos); |
| 191 | break; |
| 192 | case LYXP_NODE_ROOT_CONFIG: |
| 193 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT CONFIG", i + 1, item->pos); |
| 194 | break; |
| 195 | case LYXP_NODE_ELEM: |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 196 | 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] | 197 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (1st child val: %s)", i + 1, item->pos, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 198 | item->node->schema->name, LYD_CANON_VALUE(lyd_child(item->node))); |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 199 | } else if (item->node->schema->nodetype == LYS_LEAFLIST) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 200 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (val: %s)", i + 1, item->pos, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 201 | item->node->schema->name, LYD_CANON_VALUE(item->node)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 202 | } else { |
| 203 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s", i + 1, item->pos, item->node->schema->name); |
| 204 | } |
| 205 | break; |
| 206 | case LYXP_NODE_TEXT: |
| 207 | if (item->node->schema->nodetype & LYS_ANYDATA) { |
| 208 | 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] | 209 | item->node->schema->nodetype == LYS_ANYXML ? "anyxml" : "anydata"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 210 | } else { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 211 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT %s", i + 1, item->pos, LYD_CANON_VALUE(item->node)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 212 | } |
| 213 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 214 | case LYXP_NODE_META: |
| 215 | 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] | 216 | set->val.meta[i].meta->value); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 217 | break; |
| 218 | } |
| 219 | } |
| 220 | break; |
| 221 | |
| 222 | case LYXP_SET_SCNODE_SET: |
| 223 | LOGDBG(LY_LDGXPATH, "set SCNODE SET:"); |
| 224 | for (i = 0; i < set->used; ++i) { |
| 225 | sitem = &set->val.scnodes[i]; |
| 226 | |
| 227 | switch (sitem->type) { |
| 228 | case LYXP_NODE_ROOT: |
| 229 | LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT", i + 1, sitem->in_ctx); |
| 230 | break; |
| 231 | case LYXP_NODE_ROOT_CONFIG: |
| 232 | LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT CONFIG", i + 1, sitem->in_ctx); |
| 233 | break; |
| 234 | case LYXP_NODE_ELEM: |
| 235 | LOGDBG(LY_LDGXPATH, "\t%d (%u): ELEM %s", i + 1, sitem->in_ctx, sitem->scnode->name); |
| 236 | break; |
| 237 | default: |
| 238 | LOGINT(NULL); |
| 239 | break; |
| 240 | } |
| 241 | } |
| 242 | break; |
| 243 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 244 | case LYXP_SET_BOOLEAN: |
| 245 | LOGDBG(LY_LDGXPATH, "set BOOLEAN"); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 246 | LOGDBG(LY_LDGXPATH, "\t%s", (set->val.bln ? "true" : "false")); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 247 | break; |
| 248 | |
| 249 | case LYXP_SET_STRING: |
| 250 | LOGDBG(LY_LDGXPATH, "set STRING"); |
| 251 | LOGDBG(LY_LDGXPATH, "\t%s", set->val.str); |
| 252 | break; |
| 253 | |
| 254 | case LYXP_SET_NUMBER: |
| 255 | LOGDBG(LY_LDGXPATH, "set NUMBER"); |
| 256 | |
| 257 | if (isnan(set->val.num)) { |
| 258 | str = strdup("NaN"); |
| 259 | } else if ((set->val.num == 0) || (set->val.num == -0.0f)) { |
| 260 | str = strdup("0"); |
| 261 | } else if (isinf(set->val.num) && !signbit(set->val.num)) { |
| 262 | str = strdup("Infinity"); |
| 263 | } else if (isinf(set->val.num) && signbit(set->val.num)) { |
| 264 | str = strdup("-Infinity"); |
| 265 | } else if ((long long)set->val.num == set->val.num) { |
| 266 | if (asprintf(&str, "%lld", (long long)set->val.num) == -1) { |
| 267 | str = NULL; |
| 268 | } |
| 269 | } else { |
| 270 | if (asprintf(&str, "%03.1Lf", set->val.num) == -1) { |
| 271 | str = NULL; |
| 272 | } |
| 273 | } |
| 274 | LY_CHECK_ERR_RET(!str, LOGMEM(NULL), ); |
| 275 | |
| 276 | LOGDBG(LY_LDGXPATH, "\t%s", str); |
| 277 | free(str); |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | #endif |
| 282 | |
| 283 | /** |
| 284 | * @brief Realloc the string \p str. |
| 285 | * |
| 286 | * @param[in] ctx libyang context for logging. |
| 287 | * @param[in] needed How much free space is required. |
| 288 | * @param[in,out] str Pointer to the string to use. |
| 289 | * @param[in,out] used Used bytes in \p str. |
| 290 | * @param[in,out] size Allocated bytes in \p str. |
| 291 | * @return LY_ERR |
| 292 | */ |
| 293 | static LY_ERR |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 294 | 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] | 295 | { |
| 296 | if (*size - *used < needed) { |
| 297 | do { |
| 298 | if ((UINT16_MAX - *size) < LYXP_STRING_CAST_SIZE_STEP) { |
| 299 | LOGERR(ctx, LY_EINVAL, "XPath string length limit (%u) reached.", UINT16_MAX); |
| 300 | return LY_EINVAL; |
| 301 | } |
| 302 | *size += LYXP_STRING_CAST_SIZE_STEP; |
| 303 | } while (*size - *used < needed); |
| 304 | *str = ly_realloc(*str, *size * sizeof(char)); |
| 305 | LY_CHECK_ERR_RET(!(*str), LOGMEM(ctx), LY_EMEM); |
| 306 | } |
| 307 | |
| 308 | return LY_SUCCESS; |
| 309 | } |
| 310 | |
| 311 | /** |
| 312 | * @brief Cast nodes recursively to one string @p str. |
| 313 | * |
| 314 | * @param[in] node Node to cast. |
| 315 | * @param[in] fake_cont Whether to put the data into a "fake" container. |
| 316 | * @param[in] root_type Type of the XPath root. |
| 317 | * @param[in] indent Current indent. |
| 318 | * @param[in,out] str Resulting string. |
| 319 | * @param[in,out] used Used bytes in @p str. |
| 320 | * @param[in,out] size Allocated bytes in @p str. |
| 321 | * @return LY_ERR |
| 322 | */ |
| 323 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 324 | 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] | 325 | uint16_t *used, uint16_t *size) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 326 | { |
Radek Krejci | 7f769d7 | 2020-07-11 23:13:56 +0200 | [diff] [blame] | 327 | char *buf, *line, *ptr = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 328 | const char *value_str; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 329 | const struct lyd_node *child; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 330 | struct lyd_node *tree; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 331 | struct lyd_node_any *any; |
| 332 | LY_ERR rc; |
| 333 | |
| 334 | if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) { |
| 335 | return LY_SUCCESS; |
| 336 | } |
| 337 | |
| 338 | if (fake_cont) { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 339 | rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 340 | LY_CHECK_RET(rc); |
| 341 | strcpy(*str + (*used - 1), "\n"); |
| 342 | ++(*used); |
| 343 | |
| 344 | ++indent; |
| 345 | } |
| 346 | |
| 347 | switch (node->schema->nodetype) { |
| 348 | case LYS_CONTAINER: |
| 349 | case LYS_LIST: |
| 350 | case LYS_RPC: |
| 351 | case LYS_NOTIF: |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 352 | rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 353 | LY_CHECK_RET(rc); |
| 354 | strcpy(*str + (*used - 1), "\n"); |
| 355 | ++(*used); |
| 356 | |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 357 | for (child = lyd_child(node); child; child = child->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 358 | rc = cast_string_recursive(child, 0, root_type, indent + 1, str, used, size); |
| 359 | LY_CHECK_RET(rc); |
| 360 | } |
| 361 | |
| 362 | break; |
| 363 | |
| 364 | case LYS_LEAF: |
| 365 | case LYS_LEAFLIST: |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 366 | value_str = LYD_CANON_VALUE(node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 367 | |
| 368 | /* print indent */ |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 369 | 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] | 370 | memset(*str + (*used - 1), ' ', indent * 2); |
| 371 | *used += indent * 2; |
| 372 | |
| 373 | /* print value */ |
| 374 | if (*used == 1) { |
| 375 | sprintf(*str + (*used - 1), "%s", value_str); |
| 376 | *used += strlen(value_str); |
| 377 | } else { |
| 378 | sprintf(*str + (*used - 1), "%s\n", value_str); |
| 379 | *used += strlen(value_str) + 1; |
| 380 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 381 | |
| 382 | break; |
| 383 | |
| 384 | case LYS_ANYXML: |
| 385 | case LYS_ANYDATA: |
| 386 | any = (struct lyd_node_any *)node; |
| 387 | if (!(void *)any->value.tree) { |
| 388 | /* no content */ |
| 389 | buf = strdup(""); |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 390 | LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_CTX(node)), LY_EMEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 391 | } else { |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 392 | struct ly_out *out; |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 393 | |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 394 | if (any->value_type == LYD_ANYDATA_LYB) { |
| 395 | /* try to parse it into a data tree */ |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 396 | 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] | 397 | /* successfully parsed */ |
| 398 | free(any->value.mem); |
| 399 | any->value.tree = tree; |
| 400 | any->value_type = LYD_ANYDATA_DATATREE; |
| 401 | } |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 402 | /* 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] | 403 | } |
| 404 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 405 | switch (any->value_type) { |
| 406 | case LYD_ANYDATA_STRING: |
| 407 | case LYD_ANYDATA_XML: |
| 408 | case LYD_ANYDATA_JSON: |
| 409 | buf = strdup(any->value.json); |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 410 | LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_CTX(node)), LY_EMEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 411 | break; |
| 412 | case LYD_ANYDATA_DATATREE: |
Radek Krejci | 84ce7b1 | 2020-06-11 17:28:25 +0200 | [diff] [blame] | 413 | LY_CHECK_RET(ly_out_new_memory(&buf, 0, &out)); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 414 | rc = lyd_print_all(out, any->value.tree, LYD_XML, 0); |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 415 | ly_out_free(out, NULL, 0); |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 416 | LY_CHECK_RET(rc < 0, -rc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 417 | break; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 418 | case LYD_ANYDATA_LYB: |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 419 | LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot convert LYB anydata into string."); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 420 | return LY_EINVAL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 421 | } |
| 422 | } |
| 423 | |
| 424 | line = strtok_r(buf, "\n", &ptr); |
| 425 | do { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 426 | 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] | 427 | if (rc != LY_SUCCESS) { |
| 428 | free(buf); |
| 429 | return rc; |
| 430 | } |
| 431 | memset(*str + (*used - 1), ' ', indent * 2); |
| 432 | *used += indent * 2; |
| 433 | |
| 434 | strcpy(*str + (*used - 1), line); |
| 435 | *used += strlen(line); |
| 436 | |
| 437 | strcpy(*str + (*used - 1), "\n"); |
| 438 | *used += 1; |
| 439 | } while ((line = strtok_r(NULL, "\n", &ptr))); |
| 440 | |
| 441 | free(buf); |
| 442 | break; |
| 443 | |
| 444 | default: |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 445 | LOGINT_RET(LYD_CTX(node)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | if (fake_cont) { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 449 | rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 450 | LY_CHECK_RET(rc); |
| 451 | strcpy(*str + (*used - 1), "\n"); |
| 452 | ++(*used); |
| 453 | |
| 454 | --indent; |
| 455 | } |
| 456 | |
| 457 | return LY_SUCCESS; |
| 458 | } |
| 459 | |
| 460 | /** |
| 461 | * @brief Cast an element into a string. |
| 462 | * |
| 463 | * @param[in] node Node to cast. |
| 464 | * @param[in] fake_cont Whether to put the data into a "fake" container. |
| 465 | * @param[in] root_type Type of the XPath root. |
| 466 | * @param[out] str Element cast to dynamically-allocated string. |
| 467 | * @return LY_ERR |
| 468 | */ |
| 469 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 470 | 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] | 471 | { |
| 472 | uint16_t used, size; |
| 473 | LY_ERR rc; |
| 474 | |
| 475 | *str = malloc(LYXP_STRING_CAST_SIZE_START * sizeof(char)); |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 476 | LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_CTX(node)), LY_EMEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 477 | (*str)[0] = '\0'; |
| 478 | used = 1; |
| 479 | size = LYXP_STRING_CAST_SIZE_START; |
| 480 | |
| 481 | rc = cast_string_recursive(node, fake_cont, root_type, 0, str, &used, &size); |
| 482 | if (rc != LY_SUCCESS) { |
| 483 | free(*str); |
| 484 | return rc; |
| 485 | } |
| 486 | |
| 487 | if (size > used) { |
| 488 | *str = ly_realloc(*str, used * sizeof(char)); |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 489 | LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_CTX(node)), LY_EMEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 490 | } |
| 491 | return LY_SUCCESS; |
| 492 | } |
| 493 | |
| 494 | /** |
| 495 | * @brief Cast a LYXP_SET_NODE_SET set into a string. |
| 496 | * Context position aware. |
| 497 | * |
| 498 | * @param[in] set Set to cast. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 499 | * @param[out] str Cast dynamically-allocated string. |
| 500 | * @return LY_ERR |
| 501 | */ |
| 502 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 503 | cast_node_set_to_string(struct lyxp_set *set, char **str) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 504 | { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 505 | switch (set->val.nodes[0].type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 506 | case LYXP_NODE_NONE: |
| 507 | /* invalid */ |
| 508 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 509 | case LYXP_NODE_ROOT: |
| 510 | case LYXP_NODE_ROOT_CONFIG: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 511 | 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] | 512 | case LYXP_NODE_ELEM: |
| 513 | case LYXP_NODE_TEXT: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 514 | 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] | 515 | case LYXP_NODE_META: |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 516 | *str = strdup(set->val.meta[0].meta->value.canonical); |
| 517 | if (!*str) { |
| 518 | LOGMEM_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 519 | } |
| 520 | return LY_SUCCESS; |
| 521 | } |
| 522 | |
| 523 | LOGINT_RET(set->ctx); |
| 524 | } |
| 525 | |
| 526 | /** |
| 527 | * @brief Cast a string into an XPath number. |
| 528 | * |
| 529 | * @param[in] str String to use. |
| 530 | * @return Cast number. |
| 531 | */ |
| 532 | static long double |
| 533 | cast_string_to_number(const char *str) |
| 534 | { |
| 535 | long double num; |
| 536 | char *ptr; |
| 537 | |
| 538 | errno = 0; |
| 539 | num = strtold(str, &ptr); |
| 540 | if (errno || *ptr) { |
| 541 | num = NAN; |
| 542 | } |
| 543 | return num; |
| 544 | } |
| 545 | |
| 546 | /** |
| 547 | * @brief Callback for checking value equality. |
| 548 | * |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 549 | * Implementation of ::values_equal_cb. |
| 550 | * |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 551 | * @param[in] val1_p First value. |
| 552 | * @param[in] val2_p Second value. |
| 553 | * @param[in] mod Whether hash table is being modified. |
| 554 | * @param[in] cb_data Callback data. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 555 | * @return Boolean value whether values are equal or not. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 556 | */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 557 | static ly_bool |
| 558 | 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] | 559 | { |
| 560 | struct lyxp_set_hash_node *val1, *val2; |
| 561 | |
| 562 | val1 = (struct lyxp_set_hash_node *)val1_p; |
| 563 | val2 = (struct lyxp_set_hash_node *)val2_p; |
| 564 | |
| 565 | if ((val1->node == val2->node) && (val1->type == val2->type)) { |
| 566 | return 1; |
| 567 | } |
| 568 | |
| 569 | return 0; |
| 570 | } |
| 571 | |
| 572 | /** |
| 573 | * @brief Insert node and its hash into set. |
| 574 | * |
| 575 | * @param[in] set et to insert to. |
| 576 | * @param[in] node Node with hash. |
| 577 | * @param[in] type Node type. |
| 578 | */ |
| 579 | static void |
| 580 | set_insert_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type) |
| 581 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 582 | LY_ERR r; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 583 | uint32_t i, hash; |
| 584 | struct lyxp_set_hash_node hnode; |
| 585 | |
| 586 | if (!set->ht && (set->used >= LYD_HT_MIN_ITEMS)) { |
| 587 | /* create hash table and add all the nodes */ |
| 588 | set->ht = lyht_new(1, sizeof(struct lyxp_set_hash_node), set_values_equal_cb, NULL, 1); |
| 589 | for (i = 0; i < set->used; ++i) { |
| 590 | hnode.node = set->val.nodes[i].node; |
| 591 | hnode.type = set->val.nodes[i].type; |
| 592 | |
| 593 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 594 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 595 | hash = dict_hash_multi(hash, NULL, 0); |
| 596 | |
| 597 | r = lyht_insert(set->ht, &hnode, hash, NULL); |
| 598 | assert(!r); |
| 599 | (void)r; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 600 | |
Michal Vasko | 9d6befd | 2019-12-11 14:56:56 +0100 | [diff] [blame] | 601 | if (hnode.node == node) { |
| 602 | /* it was just added, do not add it twice */ |
| 603 | node = NULL; |
| 604 | } |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | if (set->ht && node) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 609 | /* add the new node into hash table */ |
| 610 | hnode.node = node; |
| 611 | hnode.type = type; |
| 612 | |
| 613 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 614 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 615 | hash = dict_hash_multi(hash, NULL, 0); |
| 616 | |
| 617 | r = lyht_insert(set->ht, &hnode, hash, NULL); |
| 618 | assert(!r); |
| 619 | (void)r; |
| 620 | } |
| 621 | } |
| 622 | |
| 623 | /** |
| 624 | * @brief Remove node and its hash from set. |
| 625 | * |
| 626 | * @param[in] set Set to remove from. |
| 627 | * @param[in] node Node to remove. |
| 628 | * @param[in] type Node type. |
| 629 | */ |
| 630 | static void |
| 631 | set_remove_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type) |
| 632 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 633 | LY_ERR r; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 634 | struct lyxp_set_hash_node hnode; |
| 635 | uint32_t hash; |
| 636 | |
| 637 | if (set->ht) { |
| 638 | hnode.node = node; |
| 639 | hnode.type = type; |
| 640 | |
| 641 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 642 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 643 | hash = dict_hash_multi(hash, NULL, 0); |
| 644 | |
| 645 | r = lyht_remove(set->ht, &hnode, hash); |
| 646 | assert(!r); |
| 647 | (void)r; |
| 648 | |
| 649 | if (!set->ht->used) { |
| 650 | lyht_free(set->ht); |
| 651 | set->ht = NULL; |
| 652 | } |
| 653 | } |
| 654 | } |
| 655 | |
| 656 | /** |
| 657 | * @brief Check whether node is in set based on its hash. |
| 658 | * |
| 659 | * @param[in] set Set to search in. |
| 660 | * @param[in] node Node to search for. |
| 661 | * @param[in] type Node type. |
| 662 | * @param[in] skip_idx Index in @p set to skip. |
| 663 | * @return LY_ERR |
| 664 | */ |
| 665 | static LY_ERR |
| 666 | set_dup_node_hash_check(const struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type, int skip_idx) |
| 667 | { |
| 668 | struct lyxp_set_hash_node hnode, *match_p; |
| 669 | uint32_t hash; |
| 670 | |
| 671 | hnode.node = node; |
| 672 | hnode.type = type; |
| 673 | |
| 674 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 675 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 676 | hash = dict_hash_multi(hash, NULL, 0); |
| 677 | |
| 678 | if (!lyht_find(set->ht, &hnode, hash, (void **)&match_p)) { |
| 679 | if ((skip_idx > -1) && (set->val.nodes[skip_idx].node == match_p->node) && (set->val.nodes[skip_idx].type == match_p->type)) { |
| 680 | /* we found it on the index that should be skipped, find another */ |
| 681 | hnode = *match_p; |
| 682 | if (lyht_find_next(set->ht, &hnode, hash, (void **)&match_p)) { |
| 683 | /* none other found */ |
| 684 | return LY_SUCCESS; |
| 685 | } |
| 686 | } |
| 687 | |
| 688 | return LY_EEXIST; |
| 689 | } |
| 690 | |
| 691 | /* not found */ |
| 692 | return LY_SUCCESS; |
| 693 | } |
| 694 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 695 | void |
| 696 | lyxp_set_free_content(struct lyxp_set *set) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 697 | { |
| 698 | if (!set) { |
| 699 | return; |
| 700 | } |
| 701 | |
| 702 | if (set->type == LYXP_SET_NODE_SET) { |
| 703 | free(set->val.nodes); |
| 704 | lyht_free(set->ht); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 705 | } else if (set->type == LYXP_SET_SCNODE_SET) { |
| 706 | free(set->val.scnodes); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 707 | lyht_free(set->ht); |
| 708 | } else { |
| 709 | if (set->type == LYXP_SET_STRING) { |
| 710 | free(set->val.str); |
| 711 | } |
| 712 | set->type = LYXP_SET_NODE_SET; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 713 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 714 | |
| 715 | set->val.nodes = NULL; |
| 716 | set->used = 0; |
| 717 | set->size = 0; |
| 718 | set->ht = NULL; |
| 719 | set->ctx_pos = 0; |
| 720 | set->ctx_pos = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 721 | } |
| 722 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 723 | /** |
| 724 | * @brief Free dynamically-allocated set. |
| 725 | * |
| 726 | * @param[in] set Set to free. |
| 727 | */ |
| 728 | static void |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 729 | lyxp_set_free(struct lyxp_set *set) |
| 730 | { |
| 731 | if (!set) { |
| 732 | return; |
| 733 | } |
| 734 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 735 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 736 | free(set); |
| 737 | } |
| 738 | |
| 739 | /** |
| 740 | * @brief Initialize set context. |
| 741 | * |
| 742 | * @param[in] new Set to initialize. |
| 743 | * @param[in] set Arbitrary initialized set. |
| 744 | */ |
| 745 | static void |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 746 | set_init(struct lyxp_set *new, const struct lyxp_set *set) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 747 | { |
| 748 | memset(new, 0, sizeof *new); |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 749 | if (set) { |
| 750 | new->ctx = set->ctx; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 751 | new->cur_node = set->cur_node; |
Michal Vasko | 588112f | 2019-12-10 14:51:53 +0100 | [diff] [blame] | 752 | new->root_type = set->root_type; |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 753 | new->context_op = set->context_op; |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 754 | new->tree = set->tree; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 755 | new->cur_mod = set->cur_mod; |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 756 | new->format = set->format; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 757 | new->prefix_data = set->prefix_data; |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 758 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 759 | } |
| 760 | |
| 761 | /** |
| 762 | * @brief Create a deep copy of a set. |
| 763 | * |
| 764 | * @param[in] set Set to copy. |
| 765 | * @return Copy of @p set. |
| 766 | */ |
| 767 | static struct lyxp_set * |
| 768 | set_copy(struct lyxp_set *set) |
| 769 | { |
| 770 | struct lyxp_set *ret; |
Michal Vasko | 1fdd8fa | 2021-01-08 09:21:45 +0100 | [diff] [blame] | 771 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 772 | |
| 773 | if (!set) { |
| 774 | return NULL; |
| 775 | } |
| 776 | |
| 777 | ret = malloc(sizeof *ret); |
| 778 | LY_CHECK_ERR_RET(!ret, LOGMEM(set->ctx), NULL); |
| 779 | set_init(ret, set); |
| 780 | |
| 781 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 782 | ret->type = set->type; |
| 783 | |
| 784 | for (i = 0; i < set->used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 785 | if ((set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) || |
| 786 | (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START)) { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 787 | uint32_t idx; |
| 788 | LY_CHECK_ERR_RET(lyxp_set_scnode_insert_node(ret, set->val.scnodes[i].scnode, set->val.scnodes[i].type, &idx), |
| 789 | lyxp_set_free(ret), NULL); |
Michal Vasko | 3f27c52 | 2020-01-06 08:37:49 +0100 | [diff] [blame] | 790 | /* coverity seems to think scnodes can be NULL */ |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 791 | if (!ret->val.scnodes) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 792 | lyxp_set_free(ret); |
| 793 | return NULL; |
| 794 | } |
Michal Vasko | ba71654 | 2019-12-16 10:01:58 +0100 | [diff] [blame] | 795 | ret->val.scnodes[idx].in_ctx = set->val.scnodes[i].in_ctx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 796 | } |
| 797 | } |
| 798 | } else if (set->type == LYXP_SET_NODE_SET) { |
| 799 | ret->type = set->type; |
| 800 | ret->val.nodes = malloc(set->used * sizeof *ret->val.nodes); |
| 801 | LY_CHECK_ERR_RET(!ret->val.nodes, LOGMEM(set->ctx); free(ret), NULL); |
| 802 | memcpy(ret->val.nodes, set->val.nodes, set->used * sizeof *ret->val.nodes); |
| 803 | |
| 804 | ret->used = ret->size = set->used; |
| 805 | ret->ctx_pos = set->ctx_pos; |
| 806 | ret->ctx_size = set->ctx_size; |
| 807 | ret->ht = lyht_dup(set->ht); |
| 808 | } else { |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 809 | memcpy(ret, set, sizeof *ret); |
| 810 | if (set->type == LYXP_SET_STRING) { |
| 811 | ret->val.str = strdup(set->val.str); |
| 812 | LY_CHECK_ERR_RET(!ret->val.str, LOGMEM(set->ctx); free(ret), NULL); |
| 813 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 814 | } |
| 815 | |
| 816 | return ret; |
| 817 | } |
| 818 | |
| 819 | /** |
| 820 | * @brief Fill XPath set with a string. Any current data are disposed of. |
| 821 | * |
| 822 | * @param[in] set Set to fill. |
| 823 | * @param[in] string String to fill into \p set. |
| 824 | * @param[in] str_len Length of \p string. 0 is a valid value! |
| 825 | */ |
| 826 | static void |
| 827 | set_fill_string(struct lyxp_set *set, const char *string, uint16_t str_len) |
| 828 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 829 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 830 | |
| 831 | set->type = LYXP_SET_STRING; |
| 832 | if ((str_len == 0) && (string[0] != '\0')) { |
| 833 | string = ""; |
| 834 | } |
| 835 | set->val.str = strndup(string, str_len); |
| 836 | } |
| 837 | |
| 838 | /** |
| 839 | * @brief Fill XPath set with a number. Any current data are disposed of. |
| 840 | * |
| 841 | * @param[in] set Set to fill. |
| 842 | * @param[in] number Number to fill into \p set. |
| 843 | */ |
| 844 | static void |
| 845 | set_fill_number(struct lyxp_set *set, long double number) |
| 846 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 847 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 848 | |
| 849 | set->type = LYXP_SET_NUMBER; |
| 850 | set->val.num = number; |
| 851 | } |
| 852 | |
| 853 | /** |
| 854 | * @brief Fill XPath set with a boolean. Any current data are disposed of. |
| 855 | * |
| 856 | * @param[in] set Set to fill. |
| 857 | * @param[in] boolean Boolean to fill into \p set. |
| 858 | */ |
| 859 | static void |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 860 | set_fill_boolean(struct lyxp_set *set, ly_bool boolean) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 861 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 862 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 863 | |
| 864 | set->type = LYXP_SET_BOOLEAN; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 865 | set->val.bln = boolean; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 866 | } |
| 867 | |
| 868 | /** |
| 869 | * @brief Fill XPath set with the value from another set (deep assign). |
| 870 | * Any current data are disposed of. |
| 871 | * |
| 872 | * @param[in] trg Set to fill. |
| 873 | * @param[in] src Source set to copy into \p trg. |
| 874 | */ |
| 875 | static void |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 876 | set_fill_set(struct lyxp_set *trg, const struct lyxp_set *src) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 877 | { |
| 878 | if (!trg || !src) { |
| 879 | return; |
| 880 | } |
| 881 | |
| 882 | if (trg->type == LYXP_SET_NODE_SET) { |
| 883 | free(trg->val.nodes); |
| 884 | } else if (trg->type == LYXP_SET_STRING) { |
| 885 | free(trg->val.str); |
| 886 | } |
| 887 | set_init(trg, src); |
| 888 | |
| 889 | if (src->type == LYXP_SET_SCNODE_SET) { |
| 890 | trg->type = LYXP_SET_SCNODE_SET; |
| 891 | trg->used = src->used; |
| 892 | trg->size = src->used; |
| 893 | |
| 894 | trg->val.scnodes = ly_realloc(trg->val.scnodes, trg->size * sizeof *trg->val.scnodes); |
| 895 | LY_CHECK_ERR_RET(!trg->val.scnodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), ); |
| 896 | memcpy(trg->val.scnodes, src->val.scnodes, src->used * sizeof *src->val.scnodes); |
| 897 | } else if (src->type == LYXP_SET_BOOLEAN) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 898 | set_fill_boolean(trg, src->val.bln); |
Michal Vasko | 44f3d2c | 2020-08-24 09:49:38 +0200 | [diff] [blame] | 899 | } else if (src->type == LYXP_SET_NUMBER) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 900 | set_fill_number(trg, src->val.num); |
| 901 | } else if (src->type == LYXP_SET_STRING) { |
| 902 | set_fill_string(trg, src->val.str, strlen(src->val.str)); |
| 903 | } else { |
| 904 | if (trg->type == LYXP_SET_NODE_SET) { |
| 905 | free(trg->val.nodes); |
| 906 | } else if (trg->type == LYXP_SET_STRING) { |
| 907 | free(trg->val.str); |
| 908 | } |
| 909 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 910 | assert(src->type == LYXP_SET_NODE_SET); |
| 911 | |
| 912 | trg->type = LYXP_SET_NODE_SET; |
| 913 | trg->used = src->used; |
| 914 | trg->size = src->used; |
| 915 | trg->ctx_pos = src->ctx_pos; |
| 916 | trg->ctx_size = src->ctx_size; |
| 917 | |
| 918 | trg->val.nodes = malloc(trg->used * sizeof *trg->val.nodes); |
| 919 | LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), ); |
| 920 | memcpy(trg->val.nodes, src->val.nodes, src->used * sizeof *src->val.nodes); |
| 921 | if (src->ht) { |
| 922 | trg->ht = lyht_dup(src->ht); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 923 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 924 | trg->ht = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 925 | } |
| 926 | } |
| 927 | } |
| 928 | |
| 929 | /** |
| 930 | * @brief Clear context of all schema nodes. |
| 931 | * |
| 932 | * @param[in] set Set to clear. |
| 933 | */ |
| 934 | static void |
| 935 | set_scnode_clear_ctx(struct lyxp_set *set) |
| 936 | { |
| 937 | uint32_t i; |
| 938 | |
| 939 | for (i = 0; i < set->used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 940 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
| 941 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM; |
| 942 | } else if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START) { |
| 943 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 944 | } |
| 945 | } |
| 946 | } |
| 947 | |
| 948 | /** |
| 949 | * @brief Remove a node from a set. Removing last node changes |
| 950 | * set into LYXP_SET_EMPTY. Context position aware. |
| 951 | * |
| 952 | * @param[in] set Set to use. |
| 953 | * @param[in] idx Index from @p set of the node to be removed. |
| 954 | */ |
| 955 | static void |
| 956 | set_remove_node(struct lyxp_set *set, uint32_t idx) |
| 957 | { |
| 958 | assert(set && (set->type == LYXP_SET_NODE_SET)); |
| 959 | assert(idx < set->used); |
| 960 | |
| 961 | set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
| 962 | |
| 963 | --set->used; |
| 964 | if (set->used) { |
| 965 | memmove(&set->val.nodes[idx], &set->val.nodes[idx + 1], |
| 966 | (set->used - idx) * sizeof *set->val.nodes); |
| 967 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 968 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 969 | } |
| 970 | } |
| 971 | |
| 972 | /** |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 973 | * @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] | 974 | * |
| 975 | * @param[in] set Set to use. |
| 976 | * @param[in] idx Index from @p set of the node to be removed. |
| 977 | */ |
| 978 | static void |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 979 | set_remove_node_none(struct lyxp_set *set, uint32_t idx) |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 980 | { |
| 981 | assert(set && (set->type == LYXP_SET_NODE_SET)); |
| 982 | assert(idx < set->used); |
| 983 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 984 | if (set->val.nodes[idx].type == LYXP_NODE_ELEM) { |
| 985 | set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
| 986 | } |
| 987 | set->val.nodes[idx].type = LYXP_NODE_NONE; |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 988 | } |
| 989 | |
| 990 | /** |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 991 | * @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] | 992 | * set into LYXP_SET_EMPTY. Context position aware. |
| 993 | * |
| 994 | * @param[in] set Set to consolidate. |
| 995 | */ |
| 996 | static void |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 997 | set_remove_nodes_none(struct lyxp_set *set) |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 998 | { |
Michal Vasko | 1fdd8fa | 2021-01-08 09:21:45 +0100 | [diff] [blame] | 999 | uint32_t i, orig_used, end = 0; |
| 1000 | int64_t start; |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1001 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1002 | assert(set); |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1003 | |
| 1004 | orig_used = set->used; |
| 1005 | set->used = 0; |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 1006 | for (i = 0; i < orig_used; ) { |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1007 | start = -1; |
| 1008 | do { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1009 | if ((set->val.nodes[i].type != LYXP_NODE_NONE) && (start == -1)) { |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1010 | start = i; |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1011 | } else if ((start > -1) && (set->val.nodes[i].type == LYXP_NODE_NONE)) { |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1012 | end = i; |
| 1013 | ++i; |
| 1014 | break; |
| 1015 | } |
| 1016 | |
| 1017 | ++i; |
| 1018 | if (i == orig_used) { |
| 1019 | end = i; |
| 1020 | } |
| 1021 | } while (i < orig_used); |
| 1022 | |
| 1023 | if (start > -1) { |
| 1024 | /* move the whole chunk of valid nodes together */ |
| 1025 | if (set->used != (unsigned)start) { |
| 1026 | memmove(&set->val.nodes[set->used], &set->val.nodes[start], (end - start) * sizeof *set->val.nodes); |
| 1027 | } |
| 1028 | set->used += end - start; |
| 1029 | } |
| 1030 | } |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1031 | } |
| 1032 | |
| 1033 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1034 | * @brief Check for duplicates in a node set. |
| 1035 | * |
| 1036 | * @param[in] set Set to check. |
| 1037 | * @param[in] node Node to look for in @p set. |
| 1038 | * @param[in] node_type Type of @p node. |
| 1039 | * @param[in] skip_idx Index from @p set to skip. |
| 1040 | * @return LY_ERR |
| 1041 | */ |
| 1042 | static LY_ERR |
| 1043 | set_dup_node_check(const struct lyxp_set *set, const struct lyd_node *node, enum lyxp_node_type node_type, int skip_idx) |
| 1044 | { |
| 1045 | uint32_t i; |
| 1046 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1047 | if (set->ht && node) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1048 | return set_dup_node_hash_check(set, (struct lyd_node *)node, node_type, skip_idx); |
| 1049 | } |
| 1050 | |
| 1051 | for (i = 0; i < set->used; ++i) { |
| 1052 | if ((skip_idx > -1) && (i == (unsigned)skip_idx)) { |
| 1053 | continue; |
| 1054 | } |
| 1055 | |
| 1056 | if ((set->val.nodes[i].node == node) && (set->val.nodes[i].type == node_type)) { |
| 1057 | return LY_EEXIST; |
| 1058 | } |
| 1059 | } |
| 1060 | |
| 1061 | return LY_SUCCESS; |
| 1062 | } |
| 1063 | |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 1064 | ly_bool |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1065 | lyxp_set_scnode_contains(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, int skip_idx, |
| 1066 | uint32_t *index_p) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1067 | { |
| 1068 | uint32_t i; |
| 1069 | |
| 1070 | for (i = 0; i < set->used; ++i) { |
| 1071 | if ((skip_idx > -1) && (i == (unsigned)skip_idx)) { |
| 1072 | continue; |
| 1073 | } |
| 1074 | |
| 1075 | 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] | 1076 | if (index_p) { |
| 1077 | *index_p = i; |
| 1078 | } |
| 1079 | return 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1080 | } |
| 1081 | } |
| 1082 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1083 | return 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1084 | } |
| 1085 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 1086 | void |
| 1087 | lyxp_set_scnode_merge(struct lyxp_set *set1, struct lyxp_set *set2) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1088 | { |
| 1089 | uint32_t orig_used, i, j; |
| 1090 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1091 | 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] | 1092 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1093 | if (!set2->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1094 | return; |
| 1095 | } |
| 1096 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1097 | if (!set1->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1098 | memcpy(set1, set2, sizeof *set1); |
| 1099 | return; |
| 1100 | } |
| 1101 | |
| 1102 | if (set1->used + set2->used > set1->size) { |
| 1103 | set1->size = set1->used + set2->used; |
| 1104 | set1->val.scnodes = ly_realloc(set1->val.scnodes, set1->size * sizeof *set1->val.scnodes); |
| 1105 | LY_CHECK_ERR_RET(!set1->val.scnodes, LOGMEM(set1->ctx), ); |
| 1106 | } |
| 1107 | |
| 1108 | orig_used = set1->used; |
| 1109 | |
| 1110 | for (i = 0; i < set2->used; ++i) { |
| 1111 | for (j = 0; j < orig_used; ++j) { |
| 1112 | /* detect duplicities */ |
| 1113 | if (set1->val.scnodes[j].scnode == set2->val.scnodes[i].scnode) { |
| 1114 | break; |
| 1115 | } |
| 1116 | } |
| 1117 | |
Michal Vasko | 0587bce | 2020-12-10 12:19:21 +0100 | [diff] [blame] | 1118 | if (j < orig_used) { |
| 1119 | /* node is there, but update its status if needed */ |
| 1120 | if (set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_START_USED) { |
| 1121 | set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx; |
| 1122 | } |
| 1123 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1124 | memcpy(&set1->val.scnodes[set1->used], &set2->val.scnodes[i], sizeof *set2->val.scnodes); |
| 1125 | ++set1->used; |
| 1126 | } |
| 1127 | } |
| 1128 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1129 | lyxp_set_free_content(set2); |
| 1130 | set2->type = LYXP_SET_SCNODE_SET; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1131 | } |
| 1132 | |
| 1133 | /** |
| 1134 | * @brief Insert a node into a set. Context position aware. |
| 1135 | * |
| 1136 | * @param[in] set Set to use. |
| 1137 | * @param[in] node Node to insert to @p set. |
| 1138 | * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting. |
| 1139 | * @param[in] node_type Node type of @p node. |
| 1140 | * @param[in] idx Index in @p set to insert into. |
| 1141 | */ |
| 1142 | static void |
| 1143 | set_insert_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx) |
| 1144 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1145 | assert(set && (set->type == LYXP_SET_NODE_SET)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1146 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1147 | if (!set->size) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1148 | /* first item */ |
| 1149 | if (idx) { |
| 1150 | /* no real harm done, but it is a bug */ |
| 1151 | LOGINT(set->ctx); |
| 1152 | idx = 0; |
| 1153 | } |
| 1154 | set->val.nodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.nodes); |
| 1155 | LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), ); |
| 1156 | set->type = LYXP_SET_NODE_SET; |
| 1157 | set->used = 0; |
| 1158 | set->size = LYXP_SET_SIZE_START; |
| 1159 | set->ctx_pos = 1; |
| 1160 | set->ctx_size = 1; |
| 1161 | set->ht = NULL; |
| 1162 | } else { |
| 1163 | /* not an empty set */ |
| 1164 | if (set->used == set->size) { |
| 1165 | |
| 1166 | /* set is full */ |
| 1167 | set->val.nodes = ly_realloc(set->val.nodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.nodes); |
| 1168 | LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), ); |
| 1169 | set->size += LYXP_SET_SIZE_STEP; |
| 1170 | } |
| 1171 | |
| 1172 | if (idx > set->used) { |
| 1173 | LOGINT(set->ctx); |
| 1174 | idx = set->used; |
| 1175 | } |
| 1176 | |
| 1177 | /* make space for the new node */ |
| 1178 | if (idx < set->used) { |
| 1179 | memmove(&set->val.nodes[idx + 1], &set->val.nodes[idx], (set->used - idx) * sizeof *set->val.nodes); |
| 1180 | } |
| 1181 | } |
| 1182 | |
| 1183 | /* finally assign the value */ |
| 1184 | set->val.nodes[idx].node = (struct lyd_node *)node; |
| 1185 | set->val.nodes[idx].type = node_type; |
| 1186 | set->val.nodes[idx].pos = pos; |
| 1187 | ++set->used; |
| 1188 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1189 | if (set->val.nodes[idx].type == LYXP_NODE_ELEM) { |
| 1190 | set_insert_node_hash(set, (struct lyd_node *)node, node_type); |
| 1191 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1192 | } |
| 1193 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1194 | LY_ERR |
| 1195 | 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] | 1196 | { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1197 | uint32_t index; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1198 | |
| 1199 | assert(set->type == LYXP_SET_SCNODE_SET); |
| 1200 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1201 | if (lyxp_set_scnode_contains(set, node, node_type, -1, &index)) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1202 | set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1203 | } else { |
| 1204 | if (set->used == set->size) { |
| 1205 | 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] | 1206 | LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), LY_EMEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1207 | set->size += LYXP_SET_SIZE_STEP; |
| 1208 | } |
| 1209 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1210 | index = set->used; |
| 1211 | set->val.scnodes[index].scnode = (struct lysc_node *)node; |
| 1212 | set->val.scnodes[index].type = node_type; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1213 | set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1214 | ++set->used; |
| 1215 | } |
| 1216 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1217 | if (index_p) { |
| 1218 | *index_p = index; |
| 1219 | } |
| 1220 | |
| 1221 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1222 | } |
| 1223 | |
| 1224 | /** |
| 1225 | * @brief Replace a node in a set with another. Context position aware. |
| 1226 | * |
| 1227 | * @param[in] set Set to use. |
| 1228 | * @param[in] node Node to insert to @p set. |
| 1229 | * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting. |
| 1230 | * @param[in] node_type Node type of @p node. |
| 1231 | * @param[in] idx Index in @p set of the node to replace. |
| 1232 | */ |
| 1233 | static void |
| 1234 | set_replace_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx) |
| 1235 | { |
| 1236 | assert(set && (idx < set->used)); |
| 1237 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1238 | if (set->val.nodes[idx].type == LYXP_NODE_ELEM) { |
| 1239 | set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
| 1240 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1241 | set->val.nodes[idx].node = (struct lyd_node *)node; |
| 1242 | set->val.nodes[idx].type = node_type; |
| 1243 | set->val.nodes[idx].pos = pos; |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1244 | if (set->val.nodes[idx].type == LYXP_NODE_ELEM) { |
| 1245 | set_insert_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
| 1246 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1247 | } |
| 1248 | |
| 1249 | /** |
| 1250 | * @brief Set all nodes with ctx 1 to a new unique context value. |
| 1251 | * |
| 1252 | * @param[in] set Set to modify. |
| 1253 | * @return New context value. |
| 1254 | */ |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 1255 | static int32_t |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1256 | set_scnode_new_in_ctx(struct lyxp_set *set) |
| 1257 | { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 1258 | uint32_t i; |
| 1259 | int32_t ret_ctx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1260 | |
| 1261 | assert(set->type == LYXP_SET_SCNODE_SET); |
| 1262 | |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1263 | ret_ctx = LYXP_SET_SCNODE_ATOM_PRED_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1264 | retry: |
| 1265 | for (i = 0; i < set->used; ++i) { |
| 1266 | if (set->val.scnodes[i].in_ctx >= ret_ctx) { |
| 1267 | ret_ctx = set->val.scnodes[i].in_ctx + 1; |
| 1268 | goto retry; |
| 1269 | } |
| 1270 | } |
| 1271 | for (i = 0; i < set->used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1272 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1273 | set->val.scnodes[i].in_ctx = ret_ctx; |
| 1274 | } |
| 1275 | } |
| 1276 | |
| 1277 | return ret_ctx; |
| 1278 | } |
| 1279 | |
| 1280 | /** |
| 1281 | * @brief Get unique @p node position in the data. |
| 1282 | * |
| 1283 | * @param[in] node Node to find. |
| 1284 | * @param[in] node_type Node type of @p node. |
| 1285 | * @param[in] root Root node. |
| 1286 | * @param[in] root_type Type of the XPath @p root node. |
| 1287 | * @param[in] prev Node that we think is before @p node in DFS from @p root. Can optionally |
| 1288 | * be used to increase efficiency and start the DFS from this node. |
| 1289 | * @param[in] prev_pos Node @p prev position. Optional, but must be set if @p prev is set. |
| 1290 | * @return Node position. |
| 1291 | */ |
| 1292 | static uint32_t |
| 1293 | 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] | 1294 | 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] | 1295 | { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1296 | const struct lyd_node *elem = NULL, *top_sibling; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1297 | uint32_t pos = 1; |
Michal Vasko | fb6c9dd | 2020-11-18 18:18:47 +0100 | [diff] [blame] | 1298 | ly_bool found = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1299 | |
| 1300 | assert(prev && prev_pos && !root->prev->next); |
| 1301 | |
| 1302 | if ((node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ROOT_CONFIG)) { |
| 1303 | return 0; |
| 1304 | } |
| 1305 | |
| 1306 | if (*prev) { |
| 1307 | /* start from the previous element instead from the root */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1308 | pos = *prev_pos; |
Michal Vasko | fb6c9dd | 2020-11-18 18:18:47 +0100 | [diff] [blame] | 1309 | 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] | 1310 | goto dfs_search; |
| 1311 | } |
| 1312 | |
Michal Vasko | fb6c9dd | 2020-11-18 18:18:47 +0100 | [diff] [blame] | 1313 | LY_LIST_FOR(root, top_sibling) { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1314 | LYD_TREE_DFS_BEGIN(top_sibling, elem) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1315 | dfs_search: |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1316 | if (*prev && !elem) { |
| 1317 | /* resume previous DFS */ |
| 1318 | elem = LYD_TREE_DFS_next = (struct lyd_node *)*prev; |
| 1319 | LYD_TREE_DFS_continue = 0; |
| 1320 | } |
| 1321 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1322 | 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] | 1323 | /* skip */ |
| 1324 | LYD_TREE_DFS_continue = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1325 | } else { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1326 | if (elem == node) { |
Michal Vasko | fb6c9dd | 2020-11-18 18:18:47 +0100 | [diff] [blame] | 1327 | found = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1328 | break; |
| 1329 | } |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1330 | ++pos; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1331 | } |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1332 | |
| 1333 | LYD_TREE_DFS_END(top_sibling, elem); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1334 | } |
| 1335 | |
| 1336 | /* node found */ |
Michal Vasko | fb6c9dd | 2020-11-18 18:18:47 +0100 | [diff] [blame] | 1337 | if (found) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1338 | break; |
| 1339 | } |
| 1340 | } |
| 1341 | |
Michal Vasko | fb6c9dd | 2020-11-18 18:18:47 +0100 | [diff] [blame] | 1342 | if (!found) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1343 | if (!(*prev)) { |
| 1344 | /* we went from root and failed to find it, cannot be */ |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1345 | LOGINT(LYD_CTX(node)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1346 | return 0; |
| 1347 | } else { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1348 | /* start the search again from the beginning */ |
| 1349 | *prev = root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1350 | |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1351 | top_sibling = root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1352 | pos = 1; |
| 1353 | goto dfs_search; |
| 1354 | } |
| 1355 | } |
| 1356 | |
| 1357 | /* remember the last found node for next time */ |
| 1358 | *prev = node; |
| 1359 | *prev_pos = pos; |
| 1360 | |
| 1361 | return pos; |
| 1362 | } |
| 1363 | |
| 1364 | /** |
| 1365 | * @brief Assign (fill) missing node positions. |
| 1366 | * |
| 1367 | * @param[in] set Set to fill positions in. |
| 1368 | * @param[in] root Context root node. |
| 1369 | * @param[in] root_type Context root type. |
| 1370 | * @return LY_ERR |
| 1371 | */ |
| 1372 | static LY_ERR |
| 1373 | set_assign_pos(struct lyxp_set *set, const struct lyd_node *root, enum lyxp_node_type root_type) |
| 1374 | { |
| 1375 | const struct lyd_node *prev = NULL, *tmp_node; |
| 1376 | uint32_t i, tmp_pos = 0; |
| 1377 | |
| 1378 | for (i = 0; i < set->used; ++i) { |
| 1379 | if (!set->val.nodes[i].pos) { |
| 1380 | tmp_node = NULL; |
| 1381 | switch (set->val.nodes[i].type) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1382 | case LYXP_NODE_META: |
| 1383 | tmp_node = set->val.meta[i].meta->parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1384 | if (!tmp_node) { |
| 1385 | LOGINT_RET(root->schema->module->ctx); |
| 1386 | } |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1387 | /* fall through */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1388 | case LYXP_NODE_ELEM: |
| 1389 | case LYXP_NODE_TEXT: |
| 1390 | if (!tmp_node) { |
| 1391 | tmp_node = set->val.nodes[i].node; |
| 1392 | } |
| 1393 | set->val.nodes[i].pos = get_node_pos(tmp_node, set->val.nodes[i].type, root, root_type, &prev, &tmp_pos); |
| 1394 | break; |
| 1395 | default: |
| 1396 | /* all roots have position 0 */ |
| 1397 | break; |
| 1398 | } |
| 1399 | } |
| 1400 | } |
| 1401 | |
| 1402 | return LY_SUCCESS; |
| 1403 | } |
| 1404 | |
| 1405 | /** |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1406 | * @brief Get unique @p meta position in the parent metadata. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1407 | * |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1408 | * @param[in] meta Metadata to use. |
| 1409 | * @return Metadata position. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1410 | */ |
| 1411 | static uint16_t |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1412 | get_meta_pos(struct lyd_meta *meta) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1413 | { |
| 1414 | uint16_t pos = 0; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1415 | struct lyd_meta *meta2; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1416 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1417 | for (meta2 = meta->parent->meta; meta2 && (meta2 != meta); meta2 = meta2->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1418 | ++pos; |
| 1419 | } |
| 1420 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1421 | assert(meta2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1422 | return pos; |
| 1423 | } |
| 1424 | |
| 1425 | /** |
| 1426 | * @brief Compare 2 nodes in respect to XPath document order. |
| 1427 | * |
| 1428 | * @param[in] item1 1st node. |
| 1429 | * @param[in] item2 2nd node. |
| 1430 | * @return If 1st > 2nd returns 1, 1st == 2nd returns 0, and 1st < 2nd returns -1. |
| 1431 | */ |
| 1432 | static int |
| 1433 | set_sort_compare(struct lyxp_set_node *item1, struct lyxp_set_node *item2) |
| 1434 | { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1435 | uint32_t meta_pos1 = 0, meta_pos2 = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1436 | |
| 1437 | if (item1->pos < item2->pos) { |
| 1438 | return -1; |
| 1439 | } |
| 1440 | |
| 1441 | if (item1->pos > item2->pos) { |
| 1442 | return 1; |
| 1443 | } |
| 1444 | |
| 1445 | /* node positions are equal, the fun case */ |
| 1446 | |
| 1447 | /* 1st ELEM - == - 2nd TEXT, 1st TEXT - == - 2nd ELEM */ |
| 1448 | /* special case since text nodes are actually saved as their parents */ |
| 1449 | if ((item1->node == item2->node) && (item1->type != item2->type)) { |
| 1450 | if (item1->type == LYXP_NODE_ELEM) { |
| 1451 | assert(item2->type == LYXP_NODE_TEXT); |
| 1452 | return -1; |
| 1453 | } else { |
| 1454 | assert((item1->type == LYXP_NODE_TEXT) && (item2->type == LYXP_NODE_ELEM)); |
| 1455 | return 1; |
| 1456 | } |
| 1457 | } |
| 1458 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1459 | /* we need meta positions now */ |
| 1460 | if (item1->type == LYXP_NODE_META) { |
| 1461 | meta_pos1 = get_meta_pos((struct lyd_meta *)item1->node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1462 | } |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1463 | if (item2->type == LYXP_NODE_META) { |
| 1464 | meta_pos2 = get_meta_pos((struct lyd_meta *)item2->node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1465 | } |
| 1466 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1467 | /* 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] | 1468 | /* check for duplicates */ |
| 1469 | if (item1->node == item2->node) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1470 | 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] | 1471 | return 0; |
| 1472 | } |
| 1473 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1474 | /* 1st ELEM - 2nd TEXT, 1st ELEM - any pos - 2nd META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1475 | /* elem is always first, 2nd node is after it */ |
| 1476 | if (item1->type == LYXP_NODE_ELEM) { |
| 1477 | assert(item2->type != LYXP_NODE_ELEM); |
| 1478 | return -1; |
| 1479 | } |
| 1480 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1481 | /* 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] | 1482 | /* 2nd is before 1st */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1483 | if (((item1->type == LYXP_NODE_TEXT) && |
| 1484 | ((item2->type == LYXP_NODE_ELEM) || (item2->type == LYXP_NODE_META))) || |
| 1485 | ((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_ELEM)) || |
| 1486 | (((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_META)) && |
| 1487 | (meta_pos1 > meta_pos2))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1488 | return 1; |
| 1489 | } |
| 1490 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1491 | /* 1st META - any pos - 2nd TEXT, 1st META <pos< - 2nd META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1492 | /* 2nd is after 1st */ |
| 1493 | return -1; |
| 1494 | } |
| 1495 | |
| 1496 | /** |
| 1497 | * @brief Set cast for comparisons. |
| 1498 | * |
| 1499 | * @param[in] trg Target set to cast source into. |
| 1500 | * @param[in] src Source set. |
| 1501 | * @param[in] type Target set type. |
| 1502 | * @param[in] src_idx Source set node index. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1503 | * @return LY_ERR |
| 1504 | */ |
| 1505 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1506 | 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] | 1507 | { |
| 1508 | assert(src->type == LYXP_SET_NODE_SET); |
| 1509 | |
| 1510 | set_init(trg, src); |
| 1511 | |
| 1512 | /* insert node into target set */ |
| 1513 | set_insert_node(trg, src->val.nodes[src_idx].node, src->val.nodes[src_idx].pos, src->val.nodes[src_idx].type, 0); |
| 1514 | |
| 1515 | /* cast target set appropriately */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1516 | return lyxp_set_cast(trg, type); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1517 | } |
| 1518 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1519 | /** |
| 1520 | * @brief Set content canonization for comparisons. |
| 1521 | * |
| 1522 | * @param[in] trg Target set to put the canononized source into. |
| 1523 | * @param[in] src Source set. |
| 1524 | * @param[in] xp_node Source XPath node/meta to use for canonization. |
| 1525 | * @return LY_ERR |
| 1526 | */ |
| 1527 | static LY_ERR |
| 1528 | set_comp_canonize(struct lyxp_set *trg, const struct lyxp_set *src, const struct lyxp_set_node *xp_node) |
| 1529 | { |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 1530 | const struct lysc_type *type = NULL; |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1531 | struct lyd_value val; |
| 1532 | struct ly_err_item *err = NULL; |
| 1533 | char *str, *ptr; |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1534 | LY_ERR rc; |
| 1535 | |
| 1536 | /* is there anything to canonize even? */ |
| 1537 | if ((src->type == LYXP_SET_NUMBER) || (src->type == LYXP_SET_STRING)) { |
| 1538 | /* do we have a type to use for canonization? */ |
| 1539 | if ((xp_node->type == LYXP_NODE_ELEM) && (xp_node->node->schema->nodetype & LYD_NODE_TERM)) { |
| 1540 | type = ((struct lyd_node_term *)xp_node->node)->value.realtype; |
| 1541 | } else if (xp_node->type == LYXP_NODE_META) { |
| 1542 | type = ((struct lyd_meta *)xp_node->node)->value.realtype; |
| 1543 | } |
| 1544 | } |
| 1545 | if (!type) { |
| 1546 | goto fill; |
| 1547 | } |
| 1548 | |
| 1549 | if (src->type == LYXP_SET_NUMBER) { |
| 1550 | /* canonize number */ |
| 1551 | if (asprintf(&str, "%Lf", src->val.num) == -1) { |
| 1552 | LOGMEM(src->ctx); |
| 1553 | return LY_EMEM; |
| 1554 | } |
| 1555 | } else { |
| 1556 | /* canonize string */ |
| 1557 | str = strdup(src->val.str); |
| 1558 | } |
| 1559 | |
| 1560 | /* ignore errors, the value may not satisfy schema constraints */ |
Michal Vasko | 0fdb0d9 | 2020-11-06 17:25:50 +0100 | [diff] [blame] | 1561 | rc = type->plugin->store(src->ctx, type, str, strlen(str), LY_TYPE_STORE_DYNAMIC, src->format, src->prefix_data, |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 1562 | LYD_HINT_DATA, xp_node->node->schema, &val, NULL, &err); |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1563 | ly_err_free(err); |
| 1564 | if (rc) { |
| 1565 | /* invalid value */ |
| 1566 | free(str); |
| 1567 | goto fill; |
| 1568 | } |
| 1569 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1570 | /* use the canonized value */ |
| 1571 | set_init(trg, src); |
| 1572 | trg->type = src->type; |
| 1573 | if (src->type == LYXP_SET_NUMBER) { |
Michal Vasko | 0fdb0d9 | 2020-11-06 17:25:50 +0100 | [diff] [blame] | 1574 | trg->val.num = strtold(val.canonical, &ptr); |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1575 | LY_CHECK_ERR_RET(ptr[0], LOGINT(src->ctx), LY_EINT); |
| 1576 | } else { |
Michal Vasko | 0fdb0d9 | 2020-11-06 17:25:50 +0100 | [diff] [blame] | 1577 | trg->val.str = strdup(val.canonical); |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1578 | } |
| 1579 | type->plugin->free(src->ctx, &val); |
| 1580 | return LY_SUCCESS; |
| 1581 | |
| 1582 | fill: |
| 1583 | /* no canonization needed/possible */ |
| 1584 | set_fill_set(trg, src); |
| 1585 | return LY_SUCCESS; |
| 1586 | } |
| 1587 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1588 | #ifndef NDEBUG |
| 1589 | |
| 1590 | /** |
| 1591 | * @brief Bubble sort @p set into XPath document order. |
| 1592 | * Context position aware. Unused in the 'Release' build target. |
| 1593 | * |
| 1594 | * @param[in] set Set to sort. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1595 | * @return How many times the whole set was traversed - 1 (if set was sorted, returns 0). |
| 1596 | */ |
| 1597 | static int |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1598 | set_sort(struct lyxp_set *set) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1599 | { |
| 1600 | uint32_t i, j; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 1601 | int ret = 0, cmp; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 1602 | ly_bool inverted, change; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1603 | const struct lyd_node *root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1604 | struct lyxp_set_node item; |
| 1605 | struct lyxp_set_hash_node hnode; |
| 1606 | uint64_t hash; |
| 1607 | |
Michal Vasko | 3cf8fbf | 2020-05-27 15:21:21 +0200 | [diff] [blame] | 1608 | if ((set->type != LYXP_SET_NODE_SET) || (set->used < 2)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1609 | return 0; |
| 1610 | } |
| 1611 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1612 | /* find first top-level node to be used as anchor for positions */ |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 1613 | for (root = set->cur_node; root->parent; root = lyd_parent(root)) {} |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 1614 | for ( ; root->prev->next; root = root->prev) {} |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1615 | |
| 1616 | /* fill positions */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1617 | if (set_assign_pos(set, root, set->root_type)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1618 | return -1; |
| 1619 | } |
| 1620 | |
| 1621 | LOGDBG(LY_LDGXPATH, "SORT BEGIN"); |
| 1622 | print_set_debug(set); |
| 1623 | |
| 1624 | for (i = 0; i < set->used; ++i) { |
| 1625 | inverted = 0; |
| 1626 | change = 0; |
| 1627 | |
| 1628 | for (j = 1; j < set->used - i; ++j) { |
| 1629 | /* compare node positions */ |
| 1630 | if (inverted) { |
| 1631 | cmp = set_sort_compare(&set->val.nodes[j], &set->val.nodes[j - 1]); |
| 1632 | } else { |
| 1633 | cmp = set_sort_compare(&set->val.nodes[j - 1], &set->val.nodes[j]); |
| 1634 | } |
| 1635 | |
| 1636 | /* swap if needed */ |
| 1637 | if ((inverted && (cmp < 0)) || (!inverted && (cmp > 0))) { |
| 1638 | change = 1; |
| 1639 | |
| 1640 | item = set->val.nodes[j - 1]; |
| 1641 | set->val.nodes[j - 1] = set->val.nodes[j]; |
| 1642 | set->val.nodes[j] = item; |
| 1643 | } else { |
| 1644 | /* whether node_pos1 should be smaller than node_pos2 or the other way around */ |
| 1645 | inverted = !inverted; |
| 1646 | } |
| 1647 | } |
| 1648 | |
| 1649 | ++ret; |
| 1650 | |
| 1651 | if (!change) { |
| 1652 | break; |
| 1653 | } |
| 1654 | } |
| 1655 | |
| 1656 | LOGDBG(LY_LDGXPATH, "SORT END %d", ret); |
| 1657 | print_set_debug(set); |
| 1658 | |
| 1659 | /* check node hashes */ |
| 1660 | if (set->used >= LYD_HT_MIN_ITEMS) { |
| 1661 | assert(set->ht); |
| 1662 | for (i = 0; i < set->used; ++i) { |
| 1663 | hnode.node = set->val.nodes[i].node; |
| 1664 | hnode.type = set->val.nodes[i].type; |
| 1665 | |
| 1666 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 1667 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 1668 | hash = dict_hash_multi(hash, NULL, 0); |
| 1669 | |
| 1670 | assert(!lyht_find(set->ht, &hnode, hash, NULL)); |
| 1671 | } |
| 1672 | } |
| 1673 | |
| 1674 | return ret - 1; |
| 1675 | } |
| 1676 | |
| 1677 | /** |
| 1678 | * @brief Remove duplicate entries in a sorted node set. |
| 1679 | * |
| 1680 | * @param[in] set Sorted set to check. |
| 1681 | * @return LY_ERR (LY_EEXIST if some duplicates are found) |
| 1682 | */ |
| 1683 | static LY_ERR |
| 1684 | set_sorted_dup_node_clean(struct lyxp_set *set) |
| 1685 | { |
| 1686 | uint32_t i = 0; |
| 1687 | LY_ERR ret = LY_SUCCESS; |
| 1688 | |
| 1689 | if (set->used > 1) { |
| 1690 | while (i < set->used - 1) { |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1691 | if ((set->val.nodes[i].node == set->val.nodes[i + 1].node) && |
| 1692 | (set->val.nodes[i].type == set->val.nodes[i + 1].type)) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1693 | set_remove_node_none(set, i + 1); |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1694 | ret = LY_EEXIST; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1695 | } |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1696 | ++i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1697 | } |
| 1698 | } |
| 1699 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1700 | set_remove_nodes_none(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1701 | return ret; |
| 1702 | } |
| 1703 | |
| 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 | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 1733 | for (root = trg->cur_node; 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 | |
| 1832 | /* |
| 1833 | * (re)parse functions |
| 1834 | * |
| 1835 | * Parse functions parse the expression into |
| 1836 | * tokens (syntactic analysis). |
| 1837 | * |
| 1838 | * Reparse functions perform semantic analysis |
| 1839 | * (do not save the result, just a check) of |
| 1840 | * the expression and fill repeat indices. |
| 1841 | */ |
| 1842 | |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1843 | LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1844 | 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] | 1845 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1846 | if (exp->used == tok_idx) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1847 | if (ctx) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1848 | LOGVAL(ctx, LY_VCODE_XP_EOF); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1849 | } |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1850 | return LY_EINCOMPLETE; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1851 | } |
| 1852 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1853 | if (want_tok && (exp->tokens[tok_idx] != want_tok)) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1854 | if (ctx) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1855 | 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] | 1856 | &exp->expr[exp->tok_pos[tok_idx]], lyxp_print_token(want_tok)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1857 | } |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1858 | return LY_ENOT; |
| 1859 | } |
| 1860 | |
| 1861 | return LY_SUCCESS; |
| 1862 | } |
| 1863 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1864 | LY_ERR |
| 1865 | lyxp_next_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok) |
| 1866 | { |
| 1867 | LY_CHECK_RET(lyxp_check_token(ctx, exp, *tok_idx, want_tok)); |
| 1868 | |
| 1869 | /* skip the token */ |
| 1870 | ++(*tok_idx); |
| 1871 | |
| 1872 | return LY_SUCCESS; |
| 1873 | } |
| 1874 | |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1875 | /* just like lyxp_check_token() but tests for 2 tokens */ |
| 1876 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 1877 | 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] | 1878 | enum lyxp_token want_tok2) |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1879 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1880 | if (exp->used == tok_idx) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1881 | if (ctx) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1882 | LOGVAL(ctx, LY_VCODE_XP_EOF); |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1883 | } |
| 1884 | return LY_EINCOMPLETE; |
| 1885 | } |
| 1886 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1887 | 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] | 1888 | if (ctx) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1889 | 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] | 1890 | &exp->expr[exp->tok_pos[tok_idx]]); |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1891 | } |
| 1892 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1893 | } |
| 1894 | |
| 1895 | return LY_SUCCESS; |
| 1896 | } |
| 1897 | |
| 1898 | /** |
| 1899 | * @brief Stack operation push on the repeat array. |
| 1900 | * |
| 1901 | * @param[in] exp Expression to use. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1902 | * @param[in] tok_idx Position in the expresion \p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1903 | * @param[in] repeat_op_idx Index from \p exp of the operator token. This value is pushed. |
| 1904 | */ |
| 1905 | static void |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1906 | 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] | 1907 | { |
| 1908 | uint16_t i; |
| 1909 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1910 | if (exp->repeat[tok_idx]) { |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 1911 | for (i = 0; exp->repeat[tok_idx][i]; ++i) {} |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1912 | exp->repeat[tok_idx] = realloc(exp->repeat[tok_idx], (i + 2) * sizeof *exp->repeat[tok_idx]); |
| 1913 | LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), ); |
| 1914 | exp->repeat[tok_idx][i] = repeat_op_idx; |
| 1915 | exp->repeat[tok_idx][i + 1] = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1916 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1917 | exp->repeat[tok_idx] = calloc(2, sizeof *exp->repeat[tok_idx]); |
| 1918 | LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), ); |
| 1919 | exp->repeat[tok_idx][0] = repeat_op_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1920 | } |
| 1921 | } |
| 1922 | |
| 1923 | /** |
| 1924 | * @brief Reparse Predicate. Logs directly on error. |
| 1925 | * |
| 1926 | * [7] Predicate ::= '[' Expr ']' |
| 1927 | * |
| 1928 | * @param[in] ctx Context for logging. |
| 1929 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1930 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1931 | * @return LY_ERR |
| 1932 | */ |
| 1933 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1934 | reparse_predicate(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1935 | { |
| 1936 | LY_ERR rc; |
| 1937 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1938 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1939 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1940 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1941 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1942 | rc = reparse_or_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1943 | LY_CHECK_RET(rc); |
| 1944 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1945 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1946 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1947 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1948 | |
| 1949 | return LY_SUCCESS; |
| 1950 | } |
| 1951 | |
| 1952 | /** |
| 1953 | * @brief Reparse RelativeLocationPath. Logs directly on error. |
| 1954 | * |
| 1955 | * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step |
| 1956 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 1957 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 1958 | * |
| 1959 | * @param[in] ctx Context for logging. |
| 1960 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1961 | * @param[in] tok_idx Position in the expression \p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1962 | * @return LY_ERR (LY_EINCOMPLETE on forward reference) |
| 1963 | */ |
| 1964 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1965 | reparse_relative_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1966 | { |
| 1967 | LY_ERR rc; |
| 1968 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1969 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1970 | LY_CHECK_RET(rc); |
| 1971 | |
| 1972 | goto step; |
| 1973 | do { |
| 1974 | /* '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1975 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1976 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1977 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1978 | LY_CHECK_RET(rc); |
| 1979 | step: |
| 1980 | /* Step */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1981 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1982 | case LYXP_TOKEN_DOT: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1983 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1984 | break; |
| 1985 | |
| 1986 | case LYXP_TOKEN_DDOT: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1987 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1988 | break; |
| 1989 | |
| 1990 | case LYXP_TOKEN_AT: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1991 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1992 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1993 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1994 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1995 | 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] | 1996 | 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] | 1997 | return LY_EVALID; |
| 1998 | } |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1999 | /* fall through */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2000 | case LYXP_TOKEN_NAMETEST: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2001 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2002 | goto reparse_predicate; |
| 2003 | break; |
| 2004 | |
| 2005 | case LYXP_TOKEN_NODETYPE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2006 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2007 | |
| 2008 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2009 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2010 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2011 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2012 | |
| 2013 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2014 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2015 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2016 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2017 | |
| 2018 | reparse_predicate: |
| 2019 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2020 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 2021 | rc = reparse_predicate(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2022 | LY_CHECK_RET(rc); |
| 2023 | } |
| 2024 | break; |
| 2025 | default: |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2026 | 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] | 2027 | return LY_EVALID; |
| 2028 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2029 | } 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] | 2030 | |
| 2031 | return LY_SUCCESS; |
| 2032 | } |
| 2033 | |
| 2034 | /** |
| 2035 | * @brief Reparse AbsoluteLocationPath. Logs directly on error. |
| 2036 | * |
| 2037 | * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath |
| 2038 | * |
| 2039 | * @param[in] ctx Context for logging. |
| 2040 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2041 | * @param[in] tok_idx Position in the expression \p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2042 | * @return LY_ERR |
| 2043 | */ |
| 2044 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2045 | reparse_absolute_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2046 | { |
| 2047 | LY_ERR rc; |
| 2048 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2049 | 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] | 2050 | |
| 2051 | /* '/' RelativeLocationPath? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2052 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2053 | /* '/' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2054 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2055 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2056 | if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2057 | return LY_SUCCESS; |
| 2058 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2059 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2060 | case LYXP_TOKEN_DOT: |
| 2061 | case LYXP_TOKEN_DDOT: |
| 2062 | case LYXP_TOKEN_AT: |
| 2063 | case LYXP_TOKEN_NAMETEST: |
| 2064 | case LYXP_TOKEN_NODETYPE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2065 | rc = reparse_relative_location_path(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2066 | LY_CHECK_RET(rc); |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 2067 | /* fall through */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2068 | default: |
| 2069 | break; |
| 2070 | } |
| 2071 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2072 | } else { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 2073 | /* '//' RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2074 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2075 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2076 | rc = reparse_relative_location_path(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2077 | LY_CHECK_RET(rc); |
| 2078 | } |
| 2079 | |
| 2080 | return LY_SUCCESS; |
| 2081 | } |
| 2082 | |
| 2083 | /** |
| 2084 | * @brief Reparse FunctionCall. Logs directly on error. |
| 2085 | * |
| 2086 | * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')' |
| 2087 | * |
| 2088 | * @param[in] ctx Context for logging. |
| 2089 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2090 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2091 | * @return LY_ERR |
| 2092 | */ |
| 2093 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2094 | reparse_function_call(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2095 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2096 | int8_t min_arg_count = -1; |
| 2097 | uint32_t arg_count, max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2098 | uint16_t func_tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2099 | LY_ERR rc; |
| 2100 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2101 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2102 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2103 | func_tok_idx = *tok_idx; |
| 2104 | switch (exp->tok_len[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2105 | case 3: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2106 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2107 | min_arg_count = 1; |
| 2108 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2109 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2110 | min_arg_count = 1; |
| 2111 | max_arg_count = 1; |
| 2112 | } |
| 2113 | break; |
| 2114 | case 4: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2115 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2116 | min_arg_count = 1; |
| 2117 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2118 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2119 | min_arg_count = 0; |
| 2120 | max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2121 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2122 | min_arg_count = 0; |
| 2123 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2124 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2125 | min_arg_count = 0; |
| 2126 | max_arg_count = 0; |
| 2127 | } |
| 2128 | break; |
| 2129 | case 5: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2130 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2131 | min_arg_count = 1; |
| 2132 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2133 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2134 | min_arg_count = 0; |
| 2135 | max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2136 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2137 | min_arg_count = 1; |
| 2138 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2139 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2140 | min_arg_count = 1; |
| 2141 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2142 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2143 | min_arg_count = 1; |
| 2144 | max_arg_count = 1; |
| 2145 | } |
| 2146 | break; |
| 2147 | case 6: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2148 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2149 | min_arg_count = 2; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2150 | max_arg_count = UINT32_MAX; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2151 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2152 | min_arg_count = 0; |
| 2153 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2154 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2155 | min_arg_count = 0; |
| 2156 | max_arg_count = 1; |
| 2157 | } |
| 2158 | break; |
| 2159 | case 7: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2160 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2161 | min_arg_count = 1; |
| 2162 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2163 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2164 | min_arg_count = 1; |
| 2165 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2166 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2167 | min_arg_count = 0; |
| 2168 | max_arg_count = 0; |
| 2169 | } |
| 2170 | break; |
| 2171 | case 8: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2172 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2173 | min_arg_count = 2; |
| 2174 | max_arg_count = 2; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2175 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2176 | min_arg_count = 0; |
| 2177 | max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2178 | } 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] | 2179 | min_arg_count = 2; |
| 2180 | max_arg_count = 2; |
| 2181 | } |
| 2182 | break; |
| 2183 | case 9: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2184 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2185 | min_arg_count = 2; |
| 2186 | max_arg_count = 3; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2187 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2188 | min_arg_count = 3; |
| 2189 | max_arg_count = 3; |
| 2190 | } |
| 2191 | break; |
| 2192 | case 10: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2193 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2194 | min_arg_count = 0; |
| 2195 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2196 | } 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] | 2197 | min_arg_count = 1; |
| 2198 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2199 | } 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] | 2200 | min_arg_count = 2; |
| 2201 | max_arg_count = 2; |
| 2202 | } |
| 2203 | break; |
| 2204 | case 11: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2205 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2206 | min_arg_count = 2; |
| 2207 | max_arg_count = 2; |
| 2208 | } |
| 2209 | break; |
| 2210 | case 12: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2211 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2212 | min_arg_count = 2; |
| 2213 | max_arg_count = 2; |
| 2214 | } |
| 2215 | break; |
| 2216 | case 13: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2217 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2218 | min_arg_count = 0; |
| 2219 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2220 | } 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] | 2221 | min_arg_count = 0; |
| 2222 | max_arg_count = 1; |
| 2223 | } |
| 2224 | break; |
| 2225 | case 15: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2226 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2227 | min_arg_count = 0; |
| 2228 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2229 | } 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] | 2230 | min_arg_count = 2; |
| 2231 | max_arg_count = 2; |
| 2232 | } |
| 2233 | break; |
| 2234 | case 16: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2235 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2236 | min_arg_count = 2; |
| 2237 | max_arg_count = 2; |
| 2238 | } |
| 2239 | break; |
| 2240 | case 20: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2241 | 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] | 2242 | min_arg_count = 2; |
| 2243 | max_arg_count = 2; |
| 2244 | } |
| 2245 | break; |
| 2246 | } |
| 2247 | if (min_arg_count == -1) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2248 | 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] | 2249 | return LY_EINVAL; |
| 2250 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2251 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2252 | |
| 2253 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2254 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2255 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2256 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2257 | |
| 2258 | /* ( Expr ( ',' Expr )* )? */ |
| 2259 | arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2260 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2261 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2262 | if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2263 | ++arg_count; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2264 | rc = reparse_or_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2265 | LY_CHECK_RET(rc); |
| 2266 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2267 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) { |
| 2268 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2269 | |
| 2270 | ++arg_count; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2271 | rc = reparse_or_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2272 | LY_CHECK_RET(rc); |
| 2273 | } |
| 2274 | |
| 2275 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2276 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2277 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2278 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2279 | |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 2280 | 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] | 2281 | 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] | 2282 | return LY_EVALID; |
| 2283 | } |
| 2284 | |
| 2285 | return LY_SUCCESS; |
| 2286 | } |
| 2287 | |
| 2288 | /** |
| 2289 | * @brief Reparse PathExpr. Logs directly on error. |
| 2290 | * |
| 2291 | * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate* |
| 2292 | * | PrimaryExpr Predicate* '/' RelativeLocationPath |
| 2293 | * | PrimaryExpr Predicate* '//' RelativeLocationPath |
| 2294 | * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath |
| 2295 | * [8] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall |
| 2296 | * |
| 2297 | * @param[in] ctx Context for logging. |
| 2298 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2299 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2300 | * @return LY_ERR |
| 2301 | */ |
| 2302 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2303 | reparse_path_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2304 | { |
| 2305 | LY_ERR rc; |
| 2306 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2307 | if (lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 2308 | return LY_EVALID; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2309 | } |
| 2310 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2311 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2312 | case LYXP_TOKEN_PAR1: |
| 2313 | /* '(' Expr ')' Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2314 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2315 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2316 | rc = reparse_or_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2317 | LY_CHECK_RET(rc); |
| 2318 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2319 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2320 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2321 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2322 | goto predicate; |
| 2323 | break; |
| 2324 | case LYXP_TOKEN_DOT: |
| 2325 | case LYXP_TOKEN_DDOT: |
| 2326 | case LYXP_TOKEN_AT: |
| 2327 | case LYXP_TOKEN_NAMETEST: |
| 2328 | case LYXP_TOKEN_NODETYPE: |
| 2329 | /* RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2330 | rc = reparse_relative_location_path(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2331 | LY_CHECK_RET(rc); |
| 2332 | break; |
| 2333 | case LYXP_TOKEN_FUNCNAME: |
| 2334 | /* FunctionCall */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2335 | rc = reparse_function_call(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2336 | LY_CHECK_RET(rc); |
| 2337 | goto predicate; |
| 2338 | break; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2339 | case LYXP_TOKEN_OPER_PATH: |
| 2340 | case LYXP_TOKEN_OPER_RPATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2341 | /* AbsoluteLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2342 | rc = reparse_absolute_location_path(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2343 | LY_CHECK_RET(rc); |
| 2344 | break; |
| 2345 | case LYXP_TOKEN_LITERAL: |
| 2346 | /* Literal */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2347 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2348 | goto predicate; |
| 2349 | break; |
| 2350 | case LYXP_TOKEN_NUMBER: |
| 2351 | /* Number */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2352 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2353 | goto predicate; |
| 2354 | break; |
| 2355 | default: |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2356 | 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] | 2357 | return LY_EVALID; |
| 2358 | } |
| 2359 | |
| 2360 | return LY_SUCCESS; |
| 2361 | |
| 2362 | predicate: |
| 2363 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2364 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 2365 | rc = reparse_predicate(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2366 | LY_CHECK_RET(rc); |
| 2367 | } |
| 2368 | |
| 2369 | /* ('/' or '//') RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2370 | 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] | 2371 | |
| 2372 | /* '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2373 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2374 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2375 | rc = reparse_relative_location_path(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2376 | LY_CHECK_RET(rc); |
| 2377 | } |
| 2378 | |
| 2379 | return LY_SUCCESS; |
| 2380 | } |
| 2381 | |
| 2382 | /** |
| 2383 | * @brief Reparse UnaryExpr. Logs directly on error. |
| 2384 | * |
| 2385 | * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr |
| 2386 | * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr |
| 2387 | * |
| 2388 | * @param[in] ctx Context for logging. |
| 2389 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2390 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2391 | * @return LY_ERR |
| 2392 | */ |
| 2393 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2394 | reparse_unary_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2395 | { |
| 2396 | uint16_t prev_exp; |
| 2397 | LY_ERR rc; |
| 2398 | |
| 2399 | /* ('-')* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2400 | prev_exp = *tok_idx; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2401 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) && |
| 2402 | (exp->expr[exp->tok_pos[*tok_idx]] == '-')) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2403 | exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2404 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2405 | } |
| 2406 | |
| 2407 | /* PathExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2408 | prev_exp = *tok_idx; |
| 2409 | rc = reparse_path_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2410 | LY_CHECK_RET(rc); |
| 2411 | |
| 2412 | /* ('|' PathExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2413 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_UNI)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2414 | exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2415 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2416 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2417 | rc = reparse_path_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2418 | LY_CHECK_RET(rc); |
| 2419 | } |
| 2420 | |
| 2421 | return LY_SUCCESS; |
| 2422 | } |
| 2423 | |
| 2424 | /** |
| 2425 | * @brief Reparse AdditiveExpr. Logs directly on error. |
| 2426 | * |
| 2427 | * [15] AdditiveExpr ::= MultiplicativeExpr |
| 2428 | * | AdditiveExpr '+' MultiplicativeExpr |
| 2429 | * | AdditiveExpr '-' MultiplicativeExpr |
| 2430 | * [16] MultiplicativeExpr ::= UnaryExpr |
| 2431 | * | MultiplicativeExpr '*' UnaryExpr |
| 2432 | * | MultiplicativeExpr 'div' UnaryExpr |
| 2433 | * | MultiplicativeExpr 'mod' UnaryExpr |
| 2434 | * |
| 2435 | * @param[in] ctx Context for logging. |
| 2436 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2437 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2438 | * @return LY_ERR |
| 2439 | */ |
| 2440 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2441 | reparse_additive_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2442 | { |
| 2443 | uint16_t prev_add_exp, prev_mul_exp; |
| 2444 | LY_ERR rc; |
| 2445 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2446 | prev_add_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2447 | goto reparse_multiplicative_expr; |
| 2448 | |
| 2449 | /* ('+' / '-' MultiplicativeExpr)* */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2450 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) && |
| 2451 | ((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] | 2452 | exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2453 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2454 | |
| 2455 | reparse_multiplicative_expr: |
| 2456 | /* UnaryExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2457 | prev_mul_exp = *tok_idx; |
| 2458 | rc = reparse_unary_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2459 | LY_CHECK_RET(rc); |
| 2460 | |
| 2461 | /* ('*' / 'div' / 'mod' UnaryExpr)* */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2462 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) && |
| 2463 | ((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] | 2464 | exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2465 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2466 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2467 | rc = reparse_unary_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2468 | LY_CHECK_RET(rc); |
| 2469 | } |
| 2470 | } |
| 2471 | |
| 2472 | return LY_SUCCESS; |
| 2473 | } |
| 2474 | |
| 2475 | /** |
| 2476 | * @brief Reparse EqualityExpr. Logs directly on error. |
| 2477 | * |
| 2478 | * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr |
| 2479 | * | EqualityExpr '!=' RelationalExpr |
| 2480 | * [14] RelationalExpr ::= AdditiveExpr |
| 2481 | * | RelationalExpr '<' AdditiveExpr |
| 2482 | * | RelationalExpr '>' AdditiveExpr |
| 2483 | * | RelationalExpr '<=' AdditiveExpr |
| 2484 | * | RelationalExpr '>=' AdditiveExpr |
| 2485 | * |
| 2486 | * @param[in] ctx Context for logging. |
| 2487 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2488 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2489 | * @return LY_ERR |
| 2490 | */ |
| 2491 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2492 | reparse_equality_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2493 | { |
| 2494 | uint16_t prev_eq_exp, prev_rel_exp; |
| 2495 | LY_ERR rc; |
| 2496 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2497 | prev_eq_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2498 | goto reparse_additive_expr; |
| 2499 | |
| 2500 | /* ('=' / '!=' RelationalExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2501 | 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] | 2502 | exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2503 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2504 | |
| 2505 | reparse_additive_expr: |
| 2506 | /* AdditiveExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2507 | prev_rel_exp = *tok_idx; |
| 2508 | rc = reparse_additive_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2509 | LY_CHECK_RET(rc); |
| 2510 | |
| 2511 | /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2512 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_COMP)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2513 | exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2514 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2515 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2516 | rc = reparse_additive_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2517 | LY_CHECK_RET(rc); |
| 2518 | } |
| 2519 | } |
| 2520 | |
| 2521 | return LY_SUCCESS; |
| 2522 | } |
| 2523 | |
| 2524 | /** |
| 2525 | * @brief Reparse OrExpr. Logs directly on error. |
| 2526 | * |
| 2527 | * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr |
| 2528 | * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr |
| 2529 | * |
| 2530 | * @param[in] ctx Context for logging. |
| 2531 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2532 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2533 | * @return LY_ERR |
| 2534 | */ |
| 2535 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2536 | reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2537 | { |
| 2538 | uint16_t prev_or_exp, prev_and_exp; |
| 2539 | LY_ERR rc; |
| 2540 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2541 | prev_or_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2542 | goto reparse_equality_expr; |
| 2543 | |
| 2544 | /* ('or' AndExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2545 | 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] | 2546 | exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2547 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2548 | |
| 2549 | reparse_equality_expr: |
| 2550 | /* EqualityExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2551 | prev_and_exp = *tok_idx; |
| 2552 | rc = reparse_equality_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2553 | LY_CHECK_RET(rc); |
| 2554 | |
| 2555 | /* ('and' EqualityExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2556 | 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] | 2557 | exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2558 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2559 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2560 | rc = reparse_equality_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2561 | LY_CHECK_RET(rc); |
| 2562 | } |
| 2563 | } |
| 2564 | |
| 2565 | return LY_SUCCESS; |
| 2566 | } |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2567 | |
| 2568 | /** |
| 2569 | * @brief Parse NCName. |
| 2570 | * |
| 2571 | * @param[in] ncname Name to parse. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2572 | * @return Length of @p ncname valid bytes. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2573 | */ |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2574 | static long int |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2575 | parse_ncname(const char *ncname) |
| 2576 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2577 | uint32_t uc; |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2578 | size_t size; |
| 2579 | long int len = 0; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2580 | |
| 2581 | LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0); |
| 2582 | if (!is_xmlqnamestartchar(uc) || (uc == ':')) { |
| 2583 | return len; |
| 2584 | } |
| 2585 | |
| 2586 | do { |
| 2587 | len += size; |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 2588 | if (!*ncname) { |
| 2589 | break; |
| 2590 | } |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2591 | LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2592 | } while (is_xmlqnamechar(uc) && (uc != ':')); |
| 2593 | |
| 2594 | return len; |
| 2595 | } |
| 2596 | |
| 2597 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2598 | * @brief Add @p token into the expression @p exp. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2599 | * |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2600 | * @param[in] ctx Context for logging. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2601 | * @param[in] exp Expression to use. |
| 2602 | * @param[in] token Token to add. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2603 | * @param[in] tok_pos Token position in the XPath expression. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2604 | * @param[in] tok_len Token length in the XPath expression. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2605 | * @return LY_ERR |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2606 | */ |
| 2607 | static LY_ERR |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 2608 | 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] | 2609 | { |
| 2610 | uint32_t prev; |
| 2611 | |
| 2612 | if (exp->used == exp->size) { |
| 2613 | prev = exp->size; |
| 2614 | exp->size += LYXP_EXPR_SIZE_STEP; |
| 2615 | if (prev > exp->size) { |
| 2616 | LOGINT(ctx); |
| 2617 | return LY_EINT; |
| 2618 | } |
| 2619 | |
| 2620 | exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens); |
| 2621 | LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM); |
| 2622 | exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos); |
| 2623 | LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM); |
| 2624 | exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len); |
| 2625 | LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM); |
| 2626 | } |
| 2627 | |
| 2628 | exp->tokens[exp->used] = token; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2629 | exp->tok_pos[exp->used] = tok_pos; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2630 | exp->tok_len[exp->used] = tok_len; |
| 2631 | ++exp->used; |
| 2632 | return LY_SUCCESS; |
| 2633 | } |
| 2634 | |
| 2635 | void |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 2636 | lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr) |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2637 | { |
| 2638 | uint16_t i; |
| 2639 | |
| 2640 | if (!expr) { |
| 2641 | return; |
| 2642 | } |
| 2643 | |
| 2644 | lydict_remove(ctx, expr->expr); |
| 2645 | free(expr->tokens); |
| 2646 | free(expr->tok_pos); |
| 2647 | free(expr->tok_len); |
| 2648 | if (expr->repeat) { |
| 2649 | for (i = 0; i < expr->used; ++i) { |
| 2650 | free(expr->repeat[i]); |
| 2651 | } |
| 2652 | } |
| 2653 | free(expr->repeat); |
| 2654 | free(expr); |
| 2655 | } |
| 2656 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2657 | LY_ERR |
| 2658 | 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] | 2659 | { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2660 | LY_ERR ret = LY_SUCCESS; |
| 2661 | struct lyxp_expr *expr; |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2662 | size_t parsed = 0, tok_len; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2663 | enum lyxp_token tok_type; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 2664 | ly_bool prev_function_check = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2665 | uint16_t tok_idx = 0; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2666 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2667 | assert(expr_p); |
| 2668 | |
| 2669 | if (!expr_str[0]) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2670 | LOGVAL(ctx, LY_VCODE_XP_EOF); |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2671 | return LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2672 | } |
| 2673 | |
| 2674 | if (!expr_len) { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2675 | expr_len = strlen(expr_str); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2676 | } |
| 2677 | if (expr_len > UINT16_MAX) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2678 | LOGVAL(ctx, LYVE_XPATH, "XPath expression cannot be longer than %ud characters.", UINT16_MAX); |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2679 | return LY_EVALID; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2680 | } |
| 2681 | |
| 2682 | /* init lyxp_expr structure */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2683 | expr = calloc(1, sizeof *expr); |
| 2684 | LY_CHECK_ERR_GOTO(!expr, LOGMEM(ctx); ret = LY_EMEM, error); |
| 2685 | LY_CHECK_GOTO(ret = lydict_insert(ctx, expr_str, expr_len, &expr->expr), error); |
| 2686 | expr->used = 0; |
| 2687 | expr->size = LYXP_EXPR_SIZE_START; |
| 2688 | expr->tokens = malloc(expr->size * sizeof *expr->tokens); |
| 2689 | LY_CHECK_ERR_GOTO(!expr->tokens, LOGMEM(ctx); ret = LY_EMEM, error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2690 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2691 | expr->tok_pos = malloc(expr->size * sizeof *expr->tok_pos); |
| 2692 | 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] | 2693 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2694 | expr->tok_len = malloc(expr->size * sizeof *expr->tok_len); |
| 2695 | 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] | 2696 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2697 | /* make expr 0-terminated */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2698 | expr_str = expr->expr; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2699 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2700 | while (is_xmlws(expr_str[parsed])) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2701 | ++parsed; |
| 2702 | } |
| 2703 | |
| 2704 | do { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2705 | if (expr_str[parsed] == '(') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2706 | |
| 2707 | /* '(' */ |
| 2708 | tok_len = 1; |
| 2709 | tok_type = LYXP_TOKEN_PAR1; |
| 2710 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2711 | 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] | 2712 | /* it is a NodeType/FunctionName after all */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2713 | if (((expr->tok_len[expr->used - 1] == 4) && |
| 2714 | (!strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "node", 4) || |
| 2715 | !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "text", 4))) || |
| 2716 | ((expr->tok_len[expr->used - 1] == 7) && |
| 2717 | !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "comment", 7))) { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2718 | expr->tokens[expr->used - 1] = LYXP_TOKEN_NODETYPE; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2719 | } else { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2720 | expr->tokens[expr->used - 1] = LYXP_TOKEN_FUNCNAME; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2721 | } |
| 2722 | prev_function_check = 0; |
| 2723 | } |
| 2724 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2725 | } else if (expr_str[parsed] == ')') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2726 | |
| 2727 | /* ')' */ |
| 2728 | tok_len = 1; |
| 2729 | tok_type = LYXP_TOKEN_PAR2; |
| 2730 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2731 | } else if (expr_str[parsed] == '[') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2732 | |
| 2733 | /* '[' */ |
| 2734 | tok_len = 1; |
| 2735 | tok_type = LYXP_TOKEN_BRACK1; |
| 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_BRACK2; |
| 2742 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2743 | } else if (!strncmp(&expr_str[parsed], "..", 2)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2744 | |
| 2745 | /* '..' */ |
| 2746 | tok_len = 2; |
| 2747 | tok_type = LYXP_TOKEN_DDOT; |
| 2748 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2749 | } else if ((expr_str[parsed] == '.') && (!isdigit(expr_str[parsed + 1]))) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2750 | |
| 2751 | /* '.' */ |
| 2752 | tok_len = 1; |
| 2753 | tok_type = LYXP_TOKEN_DOT; |
| 2754 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2755 | } else if (expr_str[parsed] == '@') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2756 | |
| 2757 | /* '@' */ |
| 2758 | tok_len = 1; |
| 2759 | tok_type = LYXP_TOKEN_AT; |
| 2760 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2761 | } else if (expr_str[parsed] == ',') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2762 | |
| 2763 | /* ',' */ |
| 2764 | tok_len = 1; |
| 2765 | tok_type = LYXP_TOKEN_COMMA; |
| 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 | /* Literal with ' */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2770 | for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\''); ++tok_len) {} |
| 2771 | LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0', |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2772 | 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] | 2773 | error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2774 | ++tok_len; |
| 2775 | tok_type = LYXP_TOKEN_LITERAL; |
| 2776 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2777 | } else if (expr_str[parsed] == '\"') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2778 | |
| 2779 | /* Literal with " */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2780 | for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\"'); ++tok_len) {} |
| 2781 | LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0', |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2782 | 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] | 2783 | error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2784 | ++tok_len; |
| 2785 | tok_type = LYXP_TOKEN_LITERAL; |
| 2786 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2787 | } else if ((expr_str[parsed] == '.') || (isdigit(expr_str[parsed]))) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2788 | |
| 2789 | /* Number */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2790 | for (tok_len = 0; isdigit(expr_str[parsed + tok_len]); ++tok_len) {} |
| 2791 | if (expr_str[parsed + tok_len] == '.') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2792 | ++tok_len; |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2793 | for ( ; isdigit(expr_str[parsed + tok_len]); ++tok_len) {} |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2794 | } |
| 2795 | tok_type = LYXP_TOKEN_NUMBER; |
| 2796 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2797 | } else if (expr_str[parsed] == '/') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2798 | |
| 2799 | /* Operator '/', '//' */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2800 | if (!strncmp(&expr_str[parsed], "//", 2)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2801 | tok_len = 2; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2802 | tok_type = LYXP_TOKEN_OPER_RPATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2803 | } else { |
| 2804 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2805 | tok_type = LYXP_TOKEN_OPER_PATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2806 | } |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2807 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2808 | } else if (!strncmp(&expr_str[parsed], "!=", 2)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2809 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2810 | /* Operator '!=' */ |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2811 | tok_len = 2; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2812 | tok_type = LYXP_TOKEN_OPER_NEQUAL; |
| 2813 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2814 | } else if (!strncmp(&expr_str[parsed], "<=", 2) || !strncmp(&expr_str[parsed], ">=", 2)) { |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2815 | |
| 2816 | /* Operator '<=', '>=' */ |
| 2817 | tok_len = 2; |
| 2818 | tok_type = LYXP_TOKEN_OPER_COMP; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2819 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2820 | } else if (expr_str[parsed] == '|') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2821 | |
| 2822 | /* Operator '|' */ |
| 2823 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2824 | tok_type = LYXP_TOKEN_OPER_UNI; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2825 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2826 | } else if ((expr_str[parsed] == '+') || (expr_str[parsed] == '-')) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2827 | |
| 2828 | /* Operator '+', '-' */ |
| 2829 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2830 | tok_type = LYXP_TOKEN_OPER_MATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2831 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2832 | } else if (expr_str[parsed] == '=') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2833 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2834 | /* Operator '=' */ |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2835 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2836 | tok_type = LYXP_TOKEN_OPER_EQUAL; |
| 2837 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2838 | } else if ((expr_str[parsed] == '<') || (expr_str[parsed] == '>')) { |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2839 | |
| 2840 | /* Operator '<', '>' */ |
| 2841 | tok_len = 1; |
| 2842 | tok_type = LYXP_TOKEN_OPER_COMP; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2843 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2844 | } else if (expr->used && (expr->tokens[expr->used - 1] != LYXP_TOKEN_AT) && |
| 2845 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_PAR1) && |
| 2846 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_BRACK1) && |
| 2847 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_COMMA) && |
| 2848 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_LOG) && |
| 2849 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_EQUAL) && |
| 2850 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_NEQUAL) && |
| 2851 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_COMP) && |
| 2852 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_MATH) && |
| 2853 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_UNI) && |
| 2854 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_PATH) && |
| 2855 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_RPATH)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2856 | |
| 2857 | /* Operator '*', 'or', 'and', 'mod', or 'div' */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2858 | if (expr_str[parsed] == '*') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2859 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2860 | tok_type = LYXP_TOKEN_OPER_MATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2861 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2862 | } else if (!strncmp(&expr_str[parsed], "or", 2)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2863 | tok_len = 2; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2864 | tok_type = LYXP_TOKEN_OPER_LOG; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2865 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2866 | } else if (!strncmp(&expr_str[parsed], "and", 3)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2867 | tok_len = 3; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2868 | tok_type = LYXP_TOKEN_OPER_LOG; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2869 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2870 | } 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] | 2871 | tok_len = 3; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2872 | tok_type = LYXP_TOKEN_OPER_MATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2873 | |
| 2874 | } else if (prev_function_check) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2875 | 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] | 2876 | 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] | 2877 | ret = LY_EVALID; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2878 | goto error; |
| 2879 | } else { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2880 | LOGVAL(ctx, LY_VCODE_XP_INEXPR, parsed + 1, expr_str); |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2881 | ret = LY_EVALID; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2882 | goto error; |
| 2883 | } |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2884 | } else if (expr_str[parsed] == '*') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2885 | |
| 2886 | /* NameTest '*' */ |
| 2887 | tok_len = 1; |
| 2888 | tok_type = LYXP_TOKEN_NAMETEST; |
| 2889 | |
| 2890 | } else { |
| 2891 | |
| 2892 | /* NameTest (NCName ':' '*' | QName) or NodeType/FunctionName */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2893 | long int ncname_len = parse_ncname(&expr_str[parsed]); |
| 2894 | LY_CHECK_ERR_GOTO(ncname_len < 0, |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2895 | LOGVAL(ctx, LY_VCODE_XP_INEXPR, parsed - ncname_len + 1, expr_str); ret = LY_EVALID, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2896 | error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2897 | tok_len = ncname_len; |
| 2898 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2899 | if (expr_str[parsed + tok_len] == ':') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2900 | ++tok_len; |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2901 | if (expr_str[parsed + tok_len] == '*') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2902 | ++tok_len; |
| 2903 | } else { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2904 | ncname_len = parse_ncname(&expr_str[parsed + tok_len]); |
| 2905 | LY_CHECK_ERR_GOTO(ncname_len < 0, |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2906 | LOGVAL(ctx, LY_VCODE_XP_INEXPR, parsed - ncname_len + 1, expr_str); ret = LY_EVALID, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2907 | error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2908 | tok_len += ncname_len; |
| 2909 | } |
| 2910 | /* remove old flag to prevent ambiguities */ |
| 2911 | prev_function_check = 0; |
| 2912 | tok_type = LYXP_TOKEN_NAMETEST; |
| 2913 | } else { |
| 2914 | /* there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */ |
| 2915 | prev_function_check = 1; |
| 2916 | tok_type = LYXP_TOKEN_NAMETEST; |
| 2917 | } |
| 2918 | } |
| 2919 | |
| 2920 | /* store the token, move on to the next one */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2921 | 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] | 2922 | parsed += tok_len; |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2923 | while (is_xmlws(expr_str[parsed])) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2924 | ++parsed; |
| 2925 | } |
| 2926 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2927 | } while (expr_str[parsed]); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2928 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2929 | if (reparse) { |
| 2930 | /* prealloc repeat */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2931 | expr->repeat = calloc(expr->size, sizeof *expr->repeat); |
| 2932 | LY_CHECK_ERR_GOTO(!expr->repeat, LOGMEM(ctx); ret = LY_EMEM, error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2933 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2934 | /* fill repeat */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2935 | LY_CHECK_ERR_GOTO(reparse_or_expr(ctx, expr, &tok_idx), ret = LY_EVALID, error); |
| 2936 | if (expr->used > tok_idx) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2937 | 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] | 2938 | &expr->expr[expr->tok_pos[tok_idx]]); |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2939 | ret = LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2940 | goto error; |
| 2941 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2942 | } |
| 2943 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2944 | print_expr_struct_debug(expr); |
| 2945 | *expr_p = expr; |
| 2946 | return LY_SUCCESS; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2947 | |
| 2948 | error: |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2949 | lyxp_expr_free(ctx, expr); |
| 2950 | return ret; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2951 | } |
| 2952 | |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2953 | LY_ERR |
| 2954 | 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] | 2955 | { |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2956 | LY_ERR ret = LY_SUCCESS; |
| 2957 | struct lyxp_expr *dup = NULL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2958 | uint32_t i, j; |
| 2959 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 2960 | if (!exp) { |
| 2961 | goto cleanup; |
| 2962 | } |
| 2963 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2964 | dup = calloc(1, sizeof *dup); |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2965 | LY_CHECK_ERR_GOTO(!dup, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2966 | |
| 2967 | dup->tokens = malloc(exp->used * sizeof *dup->tokens); |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2968 | LY_CHECK_ERR_GOTO(!dup->tokens, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2969 | memcpy(dup->tokens, exp->tokens, exp->used * sizeof *dup->tokens); |
| 2970 | |
| 2971 | dup->tok_pos = malloc(exp->used * sizeof *dup->tok_pos); |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2972 | LY_CHECK_ERR_GOTO(!dup->tok_pos, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2973 | memcpy(dup->tok_pos, exp->tok_pos, exp->used * sizeof *dup->tok_pos); |
| 2974 | |
| 2975 | dup->tok_len = malloc(exp->used * sizeof *dup->tok_len); |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2976 | LY_CHECK_ERR_GOTO(!dup->tok_len, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2977 | memcpy(dup->tok_len, exp->tok_len, exp->used * sizeof *dup->tok_len); |
| 2978 | |
| 2979 | dup->repeat = malloc(exp->used * sizeof *dup->repeat); |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2980 | LY_CHECK_ERR_GOTO(!dup->repeat, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2981 | for (i = 0; i < exp->used; ++i) { |
| 2982 | if (!exp->repeat[i]) { |
| 2983 | dup->repeat[i] = NULL; |
| 2984 | } else { |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 2985 | for (j = 0; exp->repeat[i][j]; ++j) {} |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2986 | /* the ending 0 as well */ |
| 2987 | ++j; |
| 2988 | |
Michal Vasko | 99c7164 | 2020-07-03 13:33:36 +0200 | [diff] [blame] | 2989 | dup->repeat[i] = malloc(j * sizeof **dup->repeat); |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2990 | LY_CHECK_ERR_GOTO(!dup->repeat[i], LOGMEM(ctx); ret = LY_EMEM, cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2991 | memcpy(dup->repeat[i], exp->repeat[i], j * sizeof **dup->repeat); |
| 2992 | dup->repeat[i][j - 1] = 0; |
| 2993 | } |
| 2994 | } |
| 2995 | |
| 2996 | dup->used = exp->used; |
| 2997 | dup->size = exp->used; |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2998 | 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] | 2999 | |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 3000 | cleanup: |
| 3001 | if (ret) { |
| 3002 | lyxp_expr_free(ctx, dup); |
| 3003 | } else { |
| 3004 | *dup_p = dup; |
| 3005 | } |
| 3006 | return ret; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3007 | } |
| 3008 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3009 | /* |
| 3010 | * warn functions |
| 3011 | * |
| 3012 | * Warn functions check specific reasonable conditions for schema XPath |
| 3013 | * and print a warning if they are not satisfied. |
| 3014 | */ |
| 3015 | |
| 3016 | /** |
| 3017 | * @brief Get the last-added schema node that is currently in the context. |
| 3018 | * |
| 3019 | * @param[in] set Set to search in. |
| 3020 | * @return Last-added schema context node, NULL if no node is in context. |
| 3021 | */ |
| 3022 | static struct lysc_node * |
| 3023 | warn_get_scnode_in_ctx(struct lyxp_set *set) |
| 3024 | { |
| 3025 | uint32_t i; |
| 3026 | |
| 3027 | if (!set || (set->type != LYXP_SET_SCNODE_SET)) { |
| 3028 | return NULL; |
| 3029 | } |
| 3030 | |
| 3031 | i = set->used; |
| 3032 | do { |
| 3033 | --i; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 3034 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3035 | /* if there are more, simply return the first found (last added) */ |
| 3036 | return set->val.scnodes[i].scnode; |
| 3037 | } |
| 3038 | } while (i); |
| 3039 | |
| 3040 | return NULL; |
| 3041 | } |
| 3042 | |
| 3043 | /** |
| 3044 | * @brief Test whether a type is numeric - integer type or decimal64. |
| 3045 | * |
| 3046 | * @param[in] type Type to test. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3047 | * @return Boolean value whether @p type is numeric type or not. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3048 | */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3049 | static ly_bool |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3050 | warn_is_numeric_type(struct lysc_type *type) |
| 3051 | { |
| 3052 | struct lysc_type_union *uni; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3053 | ly_bool ret; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3054 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3055 | |
| 3056 | switch (type->basetype) { |
| 3057 | case LY_TYPE_DEC64: |
| 3058 | case LY_TYPE_INT8: |
| 3059 | case LY_TYPE_UINT8: |
| 3060 | case LY_TYPE_INT16: |
| 3061 | case LY_TYPE_UINT16: |
| 3062 | case LY_TYPE_INT32: |
| 3063 | case LY_TYPE_UINT32: |
| 3064 | case LY_TYPE_INT64: |
| 3065 | case LY_TYPE_UINT64: |
| 3066 | return 1; |
| 3067 | case LY_TYPE_UNION: |
| 3068 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3069 | LY_ARRAY_FOR(uni->types, u) { |
| 3070 | ret = warn_is_numeric_type(uni->types[u]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3071 | if (ret) { |
| 3072 | /* found a suitable type */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3073 | return ret; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3074 | } |
| 3075 | } |
| 3076 | /* did not find any suitable type */ |
| 3077 | return 0; |
| 3078 | case LY_TYPE_LEAFREF: |
| 3079 | return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype); |
| 3080 | default: |
| 3081 | return 0; |
| 3082 | } |
| 3083 | } |
| 3084 | |
| 3085 | /** |
| 3086 | * @brief Test whether a type is string-like - no integers, decimal64 or binary. |
| 3087 | * |
| 3088 | * @param[in] type Type to test. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3089 | * @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] | 3090 | */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3091 | static ly_bool |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3092 | warn_is_string_type(struct lysc_type *type) |
| 3093 | { |
| 3094 | struct lysc_type_union *uni; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3095 | ly_bool ret; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3096 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3097 | |
| 3098 | switch (type->basetype) { |
| 3099 | case LY_TYPE_BITS: |
| 3100 | case LY_TYPE_ENUM: |
| 3101 | case LY_TYPE_IDENT: |
| 3102 | case LY_TYPE_INST: |
| 3103 | case LY_TYPE_STRING: |
| 3104 | return 1; |
| 3105 | case LY_TYPE_UNION: |
| 3106 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3107 | LY_ARRAY_FOR(uni->types, u) { |
| 3108 | ret = warn_is_string_type(uni->types[u]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3109 | if (ret) { |
| 3110 | /* found a suitable type */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3111 | return ret; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3112 | } |
| 3113 | } |
| 3114 | /* did not find any suitable type */ |
| 3115 | return 0; |
| 3116 | case LY_TYPE_LEAFREF: |
| 3117 | return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype); |
| 3118 | default: |
| 3119 | return 0; |
| 3120 | } |
| 3121 | } |
| 3122 | |
| 3123 | /** |
| 3124 | * @brief Test whether a type is one specific type. |
| 3125 | * |
| 3126 | * @param[in] type Type to test. |
| 3127 | * @param[in] base Expected type. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3128 | * @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] | 3129 | */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3130 | static ly_bool |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3131 | warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base) |
| 3132 | { |
| 3133 | struct lysc_type_union *uni; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3134 | ly_bool ret; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3135 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3136 | |
| 3137 | if (type->basetype == base) { |
| 3138 | return 1; |
| 3139 | } else if (type->basetype == LY_TYPE_UNION) { |
| 3140 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3141 | LY_ARRAY_FOR(uni->types, u) { |
| 3142 | ret = warn_is_specific_type(uni->types[u], base); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3143 | if (ret) { |
| 3144 | /* found a suitable type */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3145 | return ret; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3146 | } |
| 3147 | } |
| 3148 | /* did not find any suitable type */ |
| 3149 | return 0; |
| 3150 | } else if (type->basetype == LY_TYPE_LEAFREF) { |
| 3151 | return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base); |
| 3152 | } |
| 3153 | |
| 3154 | return 0; |
| 3155 | } |
| 3156 | |
| 3157 | /** |
| 3158 | * @brief Get next type of a (union) type. |
| 3159 | * |
| 3160 | * @param[in] type Base type. |
| 3161 | * @param[in] prev_type Previously returned type. |
| 3162 | * @return Next type or NULL. |
| 3163 | */ |
| 3164 | static struct lysc_type * |
| 3165 | warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type) |
| 3166 | { |
| 3167 | struct lysc_type_union *uni; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3168 | ly_bool found = 0; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3169 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3170 | |
| 3171 | switch (type->basetype) { |
| 3172 | case LY_TYPE_UNION: |
| 3173 | uni = (struct lysc_type_union *)type; |
| 3174 | if (!prev_type) { |
| 3175 | return uni->types[0]; |
| 3176 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3177 | LY_ARRAY_FOR(uni->types, u) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3178 | if (found) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3179 | return uni->types[u]; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3180 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3181 | if (prev_type == uni->types[u]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3182 | found = 1; |
| 3183 | } |
| 3184 | } |
| 3185 | return NULL; |
| 3186 | default: |
| 3187 | if (prev_type) { |
| 3188 | assert(type == prev_type); |
| 3189 | return NULL; |
| 3190 | } else { |
| 3191 | return type; |
| 3192 | } |
| 3193 | } |
| 3194 | } |
| 3195 | |
| 3196 | /** |
| 3197 | * @brief Test whether 2 types have a common type. |
| 3198 | * |
| 3199 | * @param[in] type1 First type. |
| 3200 | * @param[in] type2 Second type. |
| 3201 | * @return 1 if they do, 0 otherwise. |
| 3202 | */ |
| 3203 | static int |
| 3204 | warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2) |
| 3205 | { |
| 3206 | struct lysc_type *t1, *rt1, *t2, *rt2; |
| 3207 | |
| 3208 | t1 = NULL; |
| 3209 | while ((t1 = warn_is_equal_type_next_type(type1, t1))) { |
| 3210 | if (t1->basetype == LY_TYPE_LEAFREF) { |
| 3211 | rt1 = ((struct lysc_type_leafref *)t1)->realtype; |
| 3212 | } else { |
| 3213 | rt1 = t1; |
| 3214 | } |
| 3215 | |
| 3216 | t2 = NULL; |
| 3217 | while ((t2 = warn_is_equal_type_next_type(type2, t2))) { |
| 3218 | if (t2->basetype == LY_TYPE_LEAFREF) { |
| 3219 | rt2 = ((struct lysc_type_leafref *)t2)->realtype; |
| 3220 | } else { |
| 3221 | rt2 = t2; |
| 3222 | } |
| 3223 | |
| 3224 | if (rt2->basetype == rt1->basetype) { |
| 3225 | /* match found */ |
| 3226 | return 1; |
| 3227 | } |
| 3228 | } |
| 3229 | } |
| 3230 | |
| 3231 | return 0; |
| 3232 | } |
| 3233 | |
| 3234 | /** |
| 3235 | * @brief Check both operands of comparison operators. |
| 3236 | * |
| 3237 | * @param[in] ctx Context for errors. |
| 3238 | * @param[in] set1 First operand set. |
| 3239 | * @param[in] set2 Second operand set. |
| 3240 | * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!='). |
| 3241 | * @param[in] expr Start of the expression to print with the warning. |
| 3242 | * @param[in] tok_pos Token position. |
| 3243 | */ |
| 3244 | static void |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3245 | warn_operands(struct ly_ctx *ctx, struct lyxp_set *set1, struct lyxp_set *set2, ly_bool numbers_only, const char *expr, uint16_t tok_pos) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3246 | { |
| 3247 | struct lysc_node_leaf *node1, *node2; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3248 | ly_bool leaves = 1, warning = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3249 | |
| 3250 | node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1); |
| 3251 | node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2); |
| 3252 | |
| 3253 | if (!node1 && !node2) { |
| 3254 | /* no node-sets involved, nothing to do */ |
| 3255 | return; |
| 3256 | } |
| 3257 | |
| 3258 | if (node1) { |
| 3259 | if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3260 | LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name); |
| 3261 | warning = 1; |
| 3262 | leaves = 0; |
| 3263 | } else if (numbers_only && !warn_is_numeric_type(node1->type)) { |
| 3264 | LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name); |
| 3265 | warning = 1; |
| 3266 | } |
| 3267 | } |
| 3268 | |
| 3269 | if (node2) { |
| 3270 | if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3271 | LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name); |
| 3272 | warning = 1; |
| 3273 | leaves = 0; |
| 3274 | } else if (numbers_only && !warn_is_numeric_type(node2->type)) { |
| 3275 | LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name); |
| 3276 | warning = 1; |
| 3277 | } |
| 3278 | } |
| 3279 | |
| 3280 | if (node1 && node2 && leaves && !numbers_only) { |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3281 | if ((warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type)) || |
| 3282 | (!warn_is_numeric_type(node1->type) && warn_is_numeric_type(node2->type)) || |
| 3283 | (!warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type) && |
| 3284 | !warn_is_equal_type(node1->type, node2->type))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3285 | LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name); |
| 3286 | warning = 1; |
| 3287 | } |
| 3288 | } |
| 3289 | |
| 3290 | if (warning) { |
| 3291 | LOGWRN(ctx, "Previous warning generated by XPath subexpression[%u] \"%.20s\".", tok_pos, expr + tok_pos); |
| 3292 | } |
| 3293 | } |
| 3294 | |
| 3295 | /** |
| 3296 | * @brief Check that a value is valid for a leaf. If not applicable, does nothing. |
| 3297 | * |
| 3298 | * @param[in] exp Parsed XPath expression. |
| 3299 | * @param[in] set Set with the leaf/leaf-list. |
| 3300 | * @param[in] val_exp Index of the value (literal/number) in @p exp. |
| 3301 | * @param[in] equal_exp Index of the start of the equality expression in @p exp. |
| 3302 | * @param[in] last_equal_exp Index of the end of the equality expression in @p exp. |
| 3303 | */ |
| 3304 | static void |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 3305 | warn_equality_value(const struct lyxp_expr *exp, struct lyxp_set *set, uint16_t val_exp, uint16_t equal_exp, |
| 3306 | uint16_t last_equal_exp) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3307 | { |
| 3308 | struct lysc_node *scnode; |
| 3309 | struct lysc_type *type; |
| 3310 | char *value; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3311 | struct lyd_value storage; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3312 | LY_ERR rc; |
| 3313 | struct ly_err_item *err = NULL; |
| 3314 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3315 | if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && |
| 3316 | ((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] | 3317 | /* check that the node can have the specified value */ |
| 3318 | if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) { |
| 3319 | value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2); |
| 3320 | } else { |
| 3321 | value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]); |
| 3322 | } |
| 3323 | if (!value) { |
| 3324 | LOGMEM(set->ctx); |
| 3325 | return; |
| 3326 | } |
| 3327 | |
| 3328 | if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) { |
| 3329 | 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] | 3330 | " a prefix or best using \"derived-from(-or-self)()\" functions.", scnode->name, value); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3331 | LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp], |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 3332 | (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp], |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3333 | exp->expr + exp->tok_pos[equal_exp]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3334 | } |
| 3335 | |
| 3336 | type = ((struct lysc_node_leaf *)scnode)->type; |
| 3337 | if (type->basetype != LY_TYPE_IDENT) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 3338 | 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] | 3339 | LYD_HINT_DATA, scnode, &storage, NULL, &err); |
Michal Vasko | bf42e83 | 2020-11-23 16:59:42 +0100 | [diff] [blame] | 3340 | if (rc == LY_EINCOMPLETE) { |
| 3341 | rc = LY_SUCCESS; |
| 3342 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3343 | |
| 3344 | if (err) { |
| 3345 | LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg); |
| 3346 | ly_err_free(err); |
| 3347 | } else if (rc != LY_SUCCESS) { |
| 3348 | LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value); |
| 3349 | } |
| 3350 | if (rc != LY_SUCCESS) { |
| 3351 | LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp], |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 3352 | (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp], |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3353 | exp->expr + exp->tok_pos[equal_exp]); |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3354 | } else { |
| 3355 | type->plugin->free(set->ctx, &storage); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3356 | } |
| 3357 | } |
| 3358 | free(value); |
| 3359 | } |
| 3360 | } |
| 3361 | |
| 3362 | /* |
| 3363 | * XPath functions |
| 3364 | */ |
| 3365 | |
| 3366 | /** |
| 3367 | * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN |
| 3368 | * depending on whether the first node bit value from the second argument is set. |
| 3369 | * |
| 3370 | * @param[in] args Array of arguments. |
| 3371 | * @param[in] arg_count Count of elements in @p args. |
| 3372 | * @param[in,out] set Context and result set at the same time. |
| 3373 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3374 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3375 | */ |
| 3376 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3377 | 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] | 3378 | { |
| 3379 | struct lyd_node_term *leaf; |
| 3380 | struct lysc_node_leaf *sleaf; |
| 3381 | struct lysc_type_bits *bits; |
| 3382 | LY_ERR rc = LY_SUCCESS; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3383 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3384 | |
| 3385 | if (options & LYXP_SCNODE_ALL) { |
| 3386 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3387 | 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] | 3388 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3389 | 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] | 3390 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) { |
| 3391 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"bits\".", __func__, sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3392 | } |
| 3393 | |
| 3394 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 3395 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3396 | 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] | 3397 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3398 | 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] | 3399 | } |
| 3400 | } |
| 3401 | set_scnode_clear_ctx(set); |
| 3402 | return rc; |
| 3403 | } |
| 3404 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3405 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 3406 | 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] | 3407 | return LY_EVALID; |
| 3408 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3409 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3410 | LY_CHECK_RET(rc); |
| 3411 | |
| 3412 | set_fill_boolean(set, 0); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3413 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3414 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3415 | if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && |
| 3416 | (((struct lysc_node_leaf *)leaf->schema)->type->basetype == LY_TYPE_BITS)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3417 | bits = (struct lysc_type_bits *)((struct lysc_node_leaf *)leaf->schema)->type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3418 | LY_ARRAY_FOR(bits->bits, u) { |
| 3419 | if (!strcmp(bits->bits[u].name, args[1]->val.str)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3420 | set_fill_boolean(set, 1); |
| 3421 | break; |
| 3422 | } |
| 3423 | } |
| 3424 | } |
| 3425 | } |
| 3426 | |
| 3427 | return LY_SUCCESS; |
| 3428 | } |
| 3429 | |
| 3430 | /** |
| 3431 | * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN |
| 3432 | * with the argument converted to boolean. |
| 3433 | * |
| 3434 | * @param[in] args Array of arguments. |
| 3435 | * @param[in] arg_count Count of elements in @p args. |
| 3436 | * @param[in,out] set Context and result set at the same time. |
| 3437 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3438 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3439 | */ |
| 3440 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3441 | 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] | 3442 | { |
| 3443 | LY_ERR rc; |
| 3444 | |
| 3445 | if (options & LYXP_SCNODE_ALL) { |
| 3446 | set_scnode_clear_ctx(set); |
| 3447 | return LY_SUCCESS; |
| 3448 | } |
| 3449 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3450 | rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3451 | LY_CHECK_RET(rc); |
| 3452 | set_fill_set(set, args[0]); |
| 3453 | |
| 3454 | return LY_SUCCESS; |
| 3455 | } |
| 3456 | |
| 3457 | /** |
| 3458 | * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER |
| 3459 | * with the first argument rounded up to the nearest integer. |
| 3460 | * |
| 3461 | * @param[in] args Array of arguments. |
| 3462 | * @param[in] arg_count Count of elements in @p args. |
| 3463 | * @param[in,out] set Context and result set at the same time. |
| 3464 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3465 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3466 | */ |
| 3467 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3468 | 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] | 3469 | { |
| 3470 | struct lysc_node_leaf *sleaf; |
| 3471 | LY_ERR rc = LY_SUCCESS; |
| 3472 | |
| 3473 | if (options & LYXP_SCNODE_ALL) { |
| 3474 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3475 | 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] | 3476 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3477 | 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] | 3478 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) { |
| 3479 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3480 | } |
| 3481 | set_scnode_clear_ctx(set); |
| 3482 | return rc; |
| 3483 | } |
| 3484 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3485 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3486 | LY_CHECK_RET(rc); |
| 3487 | if ((long long)args[0]->val.num != args[0]->val.num) { |
| 3488 | set_fill_number(set, ((long long)args[0]->val.num) + 1); |
| 3489 | } else { |
| 3490 | set_fill_number(set, args[0]->val.num); |
| 3491 | } |
| 3492 | |
| 3493 | return LY_SUCCESS; |
| 3494 | } |
| 3495 | |
| 3496 | /** |
| 3497 | * @brief Execute the XPath concat(string, string, string*) function. |
| 3498 | * Returns LYXP_SET_STRING with the concatenation of all the arguments. |
| 3499 | * |
| 3500 | * @param[in] args Array of arguments. |
| 3501 | * @param[in] arg_count Count of elements in @p args. |
| 3502 | * @param[in,out] set Context and result set at the same time. |
| 3503 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3504 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3505 | */ |
| 3506 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3507 | 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] | 3508 | { |
| 3509 | uint16_t i; |
| 3510 | char *str = NULL; |
| 3511 | size_t used = 1; |
| 3512 | LY_ERR rc = LY_SUCCESS; |
| 3513 | struct lysc_node_leaf *sleaf; |
| 3514 | |
| 3515 | if (options & LYXP_SCNODE_ALL) { |
| 3516 | for (i = 0; i < arg_count; ++i) { |
| 3517 | if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) { |
| 3518 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3519 | LOGWRN(set->ctx, "Argument #%u of %s is a %s node \"%s\".", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3520 | i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3521 | } else if (!warn_is_string_type(sleaf->type)) { |
Radek Krejci | 70124c8 | 2020-08-14 22:17:03 +0200 | [diff] [blame] | 3522 | 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] | 3523 | } |
| 3524 | } |
| 3525 | } |
| 3526 | set_scnode_clear_ctx(set); |
| 3527 | return rc; |
| 3528 | } |
| 3529 | |
| 3530 | for (i = 0; i < arg_count; ++i) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3531 | rc = lyxp_set_cast(args[i], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3532 | if (rc != LY_SUCCESS) { |
| 3533 | free(str); |
| 3534 | return rc; |
| 3535 | } |
| 3536 | |
| 3537 | str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char)); |
| 3538 | LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM); |
| 3539 | strcpy(str + used - 1, args[i]->val.str); |
| 3540 | used += strlen(args[i]->val.str); |
| 3541 | } |
| 3542 | |
| 3543 | /* free, kind of */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3544 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3545 | set->type = LYXP_SET_STRING; |
| 3546 | set->val.str = str; |
| 3547 | |
| 3548 | return LY_SUCCESS; |
| 3549 | } |
| 3550 | |
| 3551 | /** |
| 3552 | * @brief Execute the XPath contains(string, string) function. |
| 3553 | * Returns LYXP_SET_BOOLEAN whether the second argument can |
| 3554 | * be found in the first or not. |
| 3555 | * |
| 3556 | * @param[in] args Array of arguments. |
| 3557 | * @param[in] arg_count Count of elements in @p args. |
| 3558 | * @param[in,out] set Context and result set at the same time. |
| 3559 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3560 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3561 | */ |
| 3562 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3563 | 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] | 3564 | { |
| 3565 | struct lysc_node_leaf *sleaf; |
| 3566 | LY_ERR rc = LY_SUCCESS; |
| 3567 | |
| 3568 | if (options & LYXP_SCNODE_ALL) { |
| 3569 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3570 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3571 | 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] | 3572 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3573 | 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] | 3574 | } |
| 3575 | } |
| 3576 | |
| 3577 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 3578 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3579 | 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] | 3580 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3581 | 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] | 3582 | } |
| 3583 | } |
| 3584 | set_scnode_clear_ctx(set); |
| 3585 | return rc; |
| 3586 | } |
| 3587 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3588 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3589 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3590 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3591 | LY_CHECK_RET(rc); |
| 3592 | |
| 3593 | if (strstr(args[0]->val.str, args[1]->val.str)) { |
| 3594 | set_fill_boolean(set, 1); |
| 3595 | } else { |
| 3596 | set_fill_boolean(set, 0); |
| 3597 | } |
| 3598 | |
| 3599 | return LY_SUCCESS; |
| 3600 | } |
| 3601 | |
| 3602 | /** |
| 3603 | * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER |
| 3604 | * with the size of the node-set from the argument. |
| 3605 | * |
| 3606 | * @param[in] args Array of arguments. |
| 3607 | * @param[in] arg_count Count of elements in @p args. |
| 3608 | * @param[in,out] set Context and result set at the same time. |
| 3609 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3610 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3611 | */ |
| 3612 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3613 | 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] | 3614 | { |
| 3615 | struct lysc_node *scnode = NULL; |
| 3616 | LY_ERR rc = LY_SUCCESS; |
| 3617 | |
| 3618 | if (options & LYXP_SCNODE_ALL) { |
| 3619 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(scnode = warn_get_scnode_in_ctx(args[0]))) { |
| 3620 | 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] | 3621 | } |
| 3622 | set_scnode_clear_ctx(set); |
| 3623 | return rc; |
| 3624 | } |
| 3625 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3626 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 3627 | 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] | 3628 | return LY_EVALID; |
| 3629 | } |
| 3630 | |
| 3631 | set_fill_number(set, args[0]->used); |
| 3632 | return LY_SUCCESS; |
| 3633 | } |
| 3634 | |
| 3635 | /** |
| 3636 | * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET |
| 3637 | * with the context with the intial node. |
| 3638 | * |
| 3639 | * @param[in] args Array of arguments. |
| 3640 | * @param[in] arg_count Count of elements in @p args. |
| 3641 | * @param[in,out] set Context and result set at the same time. |
| 3642 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3643 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3644 | */ |
| 3645 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3646 | 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] | 3647 | { |
| 3648 | if (arg_count || args) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 3649 | LOGVAL(set->ctx, LY_VCODE_XP_INARGCOUNT, arg_count, "current()"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3650 | return LY_EVALID; |
| 3651 | } |
| 3652 | |
| 3653 | if (options & LYXP_SCNODE_ALL) { |
| 3654 | set_scnode_clear_ctx(set); |
| 3655 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 3656 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, set->cur_scnode, LYXP_NODE_ELEM, NULL)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3657 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3658 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3659 | |
| 3660 | /* position is filled later */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 3661 | set_insert_node(set, set->cur_node, 0, LYXP_NODE_ELEM, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3662 | } |
| 3663 | |
| 3664 | return LY_SUCCESS; |
| 3665 | } |
| 3666 | |
| 3667 | /** |
| 3668 | * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either |
| 3669 | * leafref or instance-identifier target node(s). |
| 3670 | * |
| 3671 | * @param[in] args Array of arguments. |
| 3672 | * @param[in] arg_count Count of elements in @p args. |
| 3673 | * @param[in,out] set Context and result set at the same time. |
| 3674 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3675 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3676 | */ |
| 3677 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3678 | 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] | 3679 | { |
| 3680 | struct lyd_node_term *leaf; |
Michal Vasko | 42e497c | 2020-01-06 08:38:25 +0100 | [diff] [blame] | 3681 | struct lysc_node_leaf *sleaf = NULL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3682 | struct lysc_type_leafref *lref; |
Michal Vasko | ae9e4cb | 2019-09-25 08:43:05 +0200 | [diff] [blame] | 3683 | const struct lysc_node *target; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3684 | struct ly_path *p; |
| 3685 | struct lyd_node *node; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3686 | char *errmsg = NULL; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 3687 | uint8_t oper; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3688 | LY_ERR rc = LY_SUCCESS; |
| 3689 | |
| 3690 | if (options & LYXP_SCNODE_ALL) { |
| 3691 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3692 | 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] | 3693 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3694 | 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] | 3695 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) && !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) { |
| 3696 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"leafref\" nor \"instance-identifier\".", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3697 | __func__, sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3698 | } |
| 3699 | set_scnode_clear_ctx(set); |
Michal Vasko | 42e497c | 2020-01-06 08:38:25 +0100 | [diff] [blame] | 3700 | if (sleaf && (sleaf->type->basetype == LY_TYPE_LEAFREF)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3701 | lref = (struct lysc_type_leafref *)sleaf->type; |
Michal Vasko | d1e53b9 | 2021-01-28 13:11:06 +0100 | [diff] [blame] | 3702 | 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] | 3703 | |
| 3704 | /* it was already evaluated on schema, it must succeed */ |
Michal Vasko | 14ed9cd | 2021-01-28 14:16:25 +0100 | [diff] [blame] | 3705 | rc = ly_path_compile(set->ctx, lref->cur_mod, &sleaf->node, lref->path, LY_PATH_LREF_TRUE, oper, |
| 3706 | LY_PATH_TARGET_MANY, LY_PREF_SCHEMA_RESOLVED, lref->prefixes, NULL, &p); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3707 | assert(!rc); |
| 3708 | |
| 3709 | /* get the target node */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3710 | target = p[LY_ARRAY_COUNT(p) - 1].node; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3711 | ly_path_free(set->ctx, p); |
| 3712 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 3713 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, target, LYXP_NODE_ELEM, NULL)); |
Michal Vasko | ae9e4cb | 2019-09-25 08:43:05 +0200 | [diff] [blame] | 3714 | } |
| 3715 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3716 | return rc; |
| 3717 | } |
| 3718 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3719 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 3720 | LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "deref(node-set)"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3721 | return LY_EVALID; |
| 3722 | } |
| 3723 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3724 | lyxp_set_free_content(set); |
| 3725 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3726 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
| 3727 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 3728 | if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 3729 | if (sleaf->type->basetype == LY_TYPE_LEAFREF) { |
| 3730 | /* find leafref target */ |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 3731 | if (ly_type_find_leafref((struct lysc_type_leafref *)sleaf->type, &leaf->node, &leaf->value, set->tree, |
| 3732 | &node, &errmsg)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3733 | LOGERR(set->ctx, LY_EVALID, errmsg); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3734 | free(errmsg); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3735 | return LY_EVALID; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3736 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3737 | } else { |
| 3738 | assert(sleaf->type->basetype == LY_TYPE_INST); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3739 | if (ly_path_eval(leaf->value.target, set->tree, &node)) { |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3740 | LOGERR(set->ctx, LY_EVALID, "Invalid instance-identifier \"%s\" value - required instance not found.", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3741 | LYD_CANON_VALUE(leaf)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3742 | return LY_EVALID; |
| 3743 | } |
| 3744 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3745 | |
| 3746 | /* insert it */ |
| 3747 | set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3748 | } |
| 3749 | } |
| 3750 | |
| 3751 | return LY_SUCCESS; |
| 3752 | } |
| 3753 | |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3754 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3755 | 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] | 3756 | { |
Michal Vasko | 1fdd8fa | 2021-01-08 09:21:45 +0100 | [diff] [blame] | 3757 | uint32_t i; |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3758 | LY_ARRAY_COUNT_TYPE u; |
| 3759 | struct lyd_node_term *leaf; |
| 3760 | struct lysc_node_leaf *sleaf; |
| 3761 | struct lyd_meta *meta; |
| 3762 | struct lyd_value data = {0}, *val; |
| 3763 | struct ly_err_item *err = NULL; |
| 3764 | LY_ERR rc = LY_SUCCESS; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3765 | ly_bool found; |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3766 | |
| 3767 | if (options & LYXP_SCNODE_ALL) { |
| 3768 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3769 | LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", func); |
| 3770 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3771 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3772 | sleaf->name); |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3773 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) { |
| 3774 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", func, sleaf->name); |
| 3775 | } |
| 3776 | |
| 3777 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 3778 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3779 | 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] | 3780 | sleaf->name); |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3781 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3782 | LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", func, sleaf->name); |
| 3783 | } |
| 3784 | } |
| 3785 | set_scnode_clear_ctx(set); |
| 3786 | return rc; |
| 3787 | } |
| 3788 | |
| 3789 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 3790 | 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] | 3791 | return LY_EVALID; |
| 3792 | } |
| 3793 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
| 3794 | LY_CHECK_RET(rc); |
| 3795 | |
| 3796 | set_fill_boolean(set, 0); |
| 3797 | found = 0; |
| 3798 | for (i = 0; i < args[0]->used; ++i) { |
| 3799 | if ((args[0]->val.nodes[i].type != LYXP_NODE_ELEM) && (args[0]->val.nodes[i].type != LYXP_NODE_META)) { |
| 3800 | continue; |
| 3801 | } |
| 3802 | |
| 3803 | if (args[0]->val.nodes[i].type == LYXP_NODE_ELEM) { |
| 3804 | leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node; |
| 3805 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 3806 | val = &leaf->value; |
| 3807 | if (!(sleaf->nodetype & LYD_NODE_TERM) || (leaf->value.realtype->basetype != LY_TYPE_IDENT)) { |
| 3808 | /* uninteresting */ |
| 3809 | continue; |
| 3810 | } |
| 3811 | |
| 3812 | /* store args[1] as ident */ |
| 3813 | rc = val->realtype->plugin->store(set->ctx, val->realtype, args[1]->val.str, strlen(args[1]->val.str), |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 3814 | 0, set->format, set->prefix_data, LYD_HINT_DATA, leaf->schema, &data, NULL, &err); |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3815 | } else { |
| 3816 | meta = args[0]->val.meta[i].meta; |
| 3817 | val = &meta->value; |
| 3818 | if (val->realtype->basetype != LY_TYPE_IDENT) { |
| 3819 | /* uninteresting */ |
| 3820 | continue; |
| 3821 | } |
| 3822 | |
| 3823 | /* store args[1] as ident */ |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 3824 | rc = val->realtype->plugin->store(set->ctx, val->realtype, args[1]->val.str, strlen(args[1]->val.str), 0, |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 3825 | set->format, set->prefix_data, LYD_HINT_DATA, meta->parent->schema, &data, NULL, &err); |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3826 | } |
| 3827 | |
| 3828 | if (err) { |
Michal Vasko | 177d0ed | 2020-11-23 16:43:03 +0100 | [diff] [blame] | 3829 | ly_err_print(set->ctx, err); |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3830 | ly_err_free(err); |
| 3831 | } |
| 3832 | LY_CHECK_RET(rc); |
| 3833 | |
| 3834 | /* finally check the identity itself */ |
| 3835 | if (self_match && (data.ident == val->ident)) { |
| 3836 | set_fill_boolean(set, 1); |
| 3837 | found = 1; |
| 3838 | } |
| 3839 | if (!found) { |
| 3840 | LY_ARRAY_FOR(data.ident->derived, u) { |
| 3841 | if (data.ident->derived[u] == val->ident) { |
| 3842 | set_fill_boolean(set, 1); |
| 3843 | found = 1; |
| 3844 | break; |
| 3845 | } |
| 3846 | } |
| 3847 | } |
| 3848 | |
| 3849 | /* free temporary value */ |
| 3850 | val->realtype->plugin->free(set->ctx, &data); |
| 3851 | if (found) { |
| 3852 | break; |
| 3853 | } |
| 3854 | } |
| 3855 | |
| 3856 | return LY_SUCCESS; |
| 3857 | } |
| 3858 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3859 | /** |
| 3860 | * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending |
| 3861 | * on whether the first argument nodes contain a node of an identity derived from the second |
| 3862 | * argument identity. |
| 3863 | * |
| 3864 | * @param[in] args Array of arguments. |
| 3865 | * @param[in] arg_count Count of elements in @p args. |
| 3866 | * @param[in,out] set Context and result set at the same time. |
| 3867 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3868 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3869 | */ |
| 3870 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3871 | 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] | 3872 | { |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3873 | return xpath_derived_(args, set, options, 0, __func__); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3874 | } |
| 3875 | |
| 3876 | /** |
| 3877 | * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending |
| 3878 | * on whether the first argument nodes contain a node of an identity that either is or is derived from |
| 3879 | * the second argument identity. |
| 3880 | * |
| 3881 | * @param[in] args Array of arguments. |
| 3882 | * @param[in] arg_count Count of elements in @p args. |
| 3883 | * @param[in,out] set Context and result set at the same time. |
| 3884 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3885 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3886 | */ |
| 3887 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3888 | 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] | 3889 | { |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3890 | return xpath_derived_(args, set, options, 1, __func__); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3891 | } |
| 3892 | |
| 3893 | /** |
| 3894 | * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER |
| 3895 | * with the integer value of the first node's enum value, otherwise NaN. |
| 3896 | * |
| 3897 | * @param[in] args Array of arguments. |
| 3898 | * @param[in] arg_count Count of elements in @p args. |
| 3899 | * @param[in,out] set Context and result set at the same time. |
| 3900 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3901 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3902 | */ |
| 3903 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3904 | 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] | 3905 | { |
| 3906 | struct lyd_node_term *leaf; |
| 3907 | struct lysc_node_leaf *sleaf; |
| 3908 | LY_ERR rc = LY_SUCCESS; |
| 3909 | |
| 3910 | if (options & LYXP_SCNODE_ALL) { |
| 3911 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3912 | 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] | 3913 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3914 | 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] | 3915 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) { |
| 3916 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"enumeration\".", __func__, sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3917 | } |
| 3918 | set_scnode_clear_ctx(set); |
| 3919 | return rc; |
| 3920 | } |
| 3921 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3922 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 3923 | 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] | 3924 | return LY_EVALID; |
| 3925 | } |
| 3926 | |
| 3927 | set_fill_number(set, NAN); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3928 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3929 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
| 3930 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 3931 | if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) { |
| 3932 | set_fill_number(set, leaf->value.enum_item->value); |
| 3933 | } |
| 3934 | } |
| 3935 | |
| 3936 | return LY_SUCCESS; |
| 3937 | } |
| 3938 | |
| 3939 | /** |
| 3940 | * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN |
| 3941 | * with false value. |
| 3942 | * |
| 3943 | * @param[in] args Array of arguments. |
| 3944 | * @param[in] arg_count Count of elements in @p args. |
| 3945 | * @param[in,out] set Context and result set at the same time. |
| 3946 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3947 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3948 | */ |
| 3949 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3950 | 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] | 3951 | { |
| 3952 | if (options & LYXP_SCNODE_ALL) { |
| 3953 | set_scnode_clear_ctx(set); |
| 3954 | return LY_SUCCESS; |
| 3955 | } |
| 3956 | |
| 3957 | set_fill_boolean(set, 0); |
| 3958 | return LY_SUCCESS; |
| 3959 | } |
| 3960 | |
| 3961 | /** |
| 3962 | * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER |
| 3963 | * with the first argument floored (truncated). |
| 3964 | * |
| 3965 | * @param[in] args Array of arguments. |
| 3966 | * @param[in] arg_count Count of elements in @p args. |
| 3967 | * @param[in,out] set Context and result set at the same time. |
| 3968 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3969 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3970 | */ |
| 3971 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3972 | 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] | 3973 | { |
| 3974 | LY_ERR rc; |
| 3975 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3976 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3977 | LY_CHECK_RET(rc); |
| 3978 | if (isfinite(args[0]->val.num)) { |
| 3979 | set_fill_number(set, (long long)args[0]->val.num); |
| 3980 | } |
| 3981 | |
| 3982 | return LY_SUCCESS; |
| 3983 | } |
| 3984 | |
| 3985 | /** |
| 3986 | * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN |
| 3987 | * whether the language of the text matches the one from the argument. |
| 3988 | * |
| 3989 | * @param[in] args Array of arguments. |
| 3990 | * @param[in] arg_count Count of elements in @p args. |
| 3991 | * @param[in,out] set Context and result set at the same time. |
| 3992 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3993 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3994 | */ |
| 3995 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3996 | 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] | 3997 | { |
| 3998 | const struct lyd_node *node; |
| 3999 | struct lysc_node_leaf *sleaf; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4000 | struct lyd_meta *meta = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4001 | const char *val; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4002 | LY_ERR rc = LY_SUCCESS; |
| 4003 | |
| 4004 | if (options & LYXP_SCNODE_ALL) { |
| 4005 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4006 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4007 | 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] | 4008 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4009 | 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] | 4010 | } |
| 4011 | } |
| 4012 | set_scnode_clear_ctx(set); |
| 4013 | return rc; |
| 4014 | } |
| 4015 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4016 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4017 | LY_CHECK_RET(rc); |
| 4018 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4019 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4020 | 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] | 4021 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4022 | } else if (!set->used) { |
| 4023 | set_fill_boolean(set, 0); |
| 4024 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4025 | } |
| 4026 | |
| 4027 | switch (set->val.nodes[0].type) { |
| 4028 | case LYXP_NODE_ELEM: |
| 4029 | case LYXP_NODE_TEXT: |
| 4030 | node = set->val.nodes[0].node; |
| 4031 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4032 | case LYXP_NODE_META: |
| 4033 | node = set->val.meta[0].meta->parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4034 | break; |
| 4035 | default: |
| 4036 | /* nothing to do with roots */ |
| 4037 | set_fill_boolean(set, 0); |
| 4038 | return LY_SUCCESS; |
| 4039 | } |
| 4040 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4041 | /* find lang metadata */ |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 4042 | for ( ; node; node = lyd_parent(node)) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4043 | for (meta = node->meta; meta; meta = meta->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4044 | /* annotations */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4045 | 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] | 4046 | break; |
| 4047 | } |
| 4048 | } |
| 4049 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4050 | if (meta) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4051 | break; |
| 4052 | } |
| 4053 | } |
| 4054 | |
| 4055 | /* compare languages */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4056 | if (!meta) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4057 | set_fill_boolean(set, 0); |
| 4058 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4059 | uint64_t i; |
| 4060 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 4061 | val = meta->value.canonical; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4062 | for (i = 0; args[0]->val.str[i]; ++i) { |
| 4063 | if (tolower(args[0]->val.str[i]) != tolower(val[i])) { |
| 4064 | set_fill_boolean(set, 0); |
| 4065 | break; |
| 4066 | } |
| 4067 | } |
| 4068 | if (!args[0]->val.str[i]) { |
| 4069 | if (!val[i] || (val[i] == '-')) { |
| 4070 | set_fill_boolean(set, 1); |
| 4071 | } else { |
| 4072 | set_fill_boolean(set, 0); |
| 4073 | } |
| 4074 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4075 | } |
| 4076 | |
| 4077 | return LY_SUCCESS; |
| 4078 | } |
| 4079 | |
| 4080 | /** |
| 4081 | * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER |
| 4082 | * with the context size. |
| 4083 | * |
| 4084 | * @param[in] args Array of arguments. |
| 4085 | * @param[in] arg_count Count of elements in @p args. |
| 4086 | * @param[in,out] set Context and result set at the same time. |
| 4087 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4088 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4089 | */ |
| 4090 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4091 | 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] | 4092 | { |
| 4093 | if (options & LYXP_SCNODE_ALL) { |
| 4094 | set_scnode_clear_ctx(set); |
| 4095 | return LY_SUCCESS; |
| 4096 | } |
| 4097 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4098 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4099 | LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "last()"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4100 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4101 | } else if (!set->used) { |
| 4102 | set_fill_number(set, 0); |
| 4103 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4104 | } |
| 4105 | |
| 4106 | set_fill_number(set, set->ctx_size); |
| 4107 | return LY_SUCCESS; |
| 4108 | } |
| 4109 | |
| 4110 | /** |
| 4111 | * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING |
| 4112 | * with the node name without namespace from the argument or the context. |
| 4113 | * |
| 4114 | * @param[in] args Array of arguments. |
| 4115 | * @param[in] arg_count Count of elements in @p args. |
| 4116 | * @param[in,out] set Context and result set at the same time. |
| 4117 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4118 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4119 | */ |
| 4120 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4121 | 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] | 4122 | { |
| 4123 | struct lyxp_set_node *item; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 4124 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4125 | /* suppress unused variable warning */ |
| 4126 | (void)options; |
| 4127 | |
| 4128 | if (options & LYXP_SCNODE_ALL) { |
| 4129 | set_scnode_clear_ctx(set); |
| 4130 | return LY_SUCCESS; |
| 4131 | } |
| 4132 | |
| 4133 | if (arg_count) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4134 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4135 | 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] | 4136 | "local-name(node-set?)"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4137 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4138 | } else if (!args[0]->used) { |
| 4139 | set_fill_string(set, "", 0); |
| 4140 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4141 | } |
| 4142 | |
| 4143 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4144 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4145 | |
| 4146 | item = &args[0]->val.nodes[0]; |
| 4147 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4148 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4149 | 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] | 4150 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4151 | } else if (!set->used) { |
| 4152 | set_fill_string(set, "", 0); |
| 4153 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4154 | } |
| 4155 | |
| 4156 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4157 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4158 | |
| 4159 | item = &set->val.nodes[0]; |
| 4160 | } |
| 4161 | |
| 4162 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4163 | case LYXP_NODE_NONE: |
| 4164 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4165 | case LYXP_NODE_ROOT: |
| 4166 | case LYXP_NODE_ROOT_CONFIG: |
| 4167 | case LYXP_NODE_TEXT: |
| 4168 | set_fill_string(set, "", 0); |
| 4169 | break; |
| 4170 | case LYXP_NODE_ELEM: |
| 4171 | set_fill_string(set, item->node->schema->name, strlen(item->node->schema->name)); |
| 4172 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4173 | case LYXP_NODE_META: |
| 4174 | 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] | 4175 | break; |
| 4176 | } |
| 4177 | |
| 4178 | return LY_SUCCESS; |
| 4179 | } |
| 4180 | |
| 4181 | /** |
| 4182 | * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING |
| 4183 | * with the node name fully qualified (with namespace) from the argument or the context. |
| 4184 | * |
| 4185 | * @param[in] args Array of arguments. |
| 4186 | * @param[in] arg_count Count of elements in @p args. |
| 4187 | * @param[in,out] set Context and result set at the same time. |
| 4188 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4189 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4190 | */ |
| 4191 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4192 | 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] | 4193 | { |
| 4194 | struct lyxp_set_node *item; |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 4195 | struct lys_module *mod = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4196 | char *str; |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 4197 | const char *name = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4198 | |
| 4199 | if (options & LYXP_SCNODE_ALL) { |
| 4200 | set_scnode_clear_ctx(set); |
| 4201 | return LY_SUCCESS; |
| 4202 | } |
| 4203 | |
| 4204 | if (arg_count) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4205 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4206 | 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] | 4207 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4208 | } else if (!args[0]->used) { |
| 4209 | set_fill_string(set, "", 0); |
| 4210 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4211 | } |
| 4212 | |
| 4213 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4214 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4215 | |
| 4216 | item = &args[0]->val.nodes[0]; |
| 4217 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4218 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4219 | 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] | 4220 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4221 | } else if (!set->used) { |
| 4222 | set_fill_string(set, "", 0); |
| 4223 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4224 | } |
| 4225 | |
| 4226 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4227 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4228 | |
| 4229 | item = &set->val.nodes[0]; |
| 4230 | } |
| 4231 | |
| 4232 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4233 | case LYXP_NODE_NONE: |
| 4234 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4235 | case LYXP_NODE_ROOT: |
| 4236 | case LYXP_NODE_ROOT_CONFIG: |
| 4237 | case LYXP_NODE_TEXT: |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 4238 | /* keep NULL */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4239 | break; |
| 4240 | case LYXP_NODE_ELEM: |
| 4241 | mod = item->node->schema->module; |
| 4242 | name = item->node->schema->name; |
| 4243 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4244 | case LYXP_NODE_META: |
| 4245 | mod = ((struct lyd_meta *)item->node)->annotation->module; |
| 4246 | name = ((struct lyd_meta *)item->node)->name; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4247 | break; |
| 4248 | } |
| 4249 | |
| 4250 | if (mod && name) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 4251 | 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] | 4252 | LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM); |
| 4253 | set_fill_string(set, str, strlen(str)); |
| 4254 | free(str); |
| 4255 | } else { |
| 4256 | set_fill_string(set, "", 0); |
| 4257 | } |
| 4258 | |
| 4259 | return LY_SUCCESS; |
| 4260 | } |
| 4261 | |
| 4262 | /** |
| 4263 | * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING |
| 4264 | * with the namespace of the node from the argument or the context. |
| 4265 | * |
| 4266 | * @param[in] args Array of arguments. |
| 4267 | * @param[in] arg_count Count of elements in @p args. |
| 4268 | * @param[in,out] set Context and result set at the same time. |
| 4269 | * @param[in] options XPath options. |
| 4270 | * @return LY_ERR (LY_EINVAL for wrong arguments on schema) |
| 4271 | */ |
| 4272 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4273 | 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] | 4274 | { |
| 4275 | struct lyxp_set_node *item; |
| 4276 | struct lys_module *mod; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 4277 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4278 | /* suppress unused variable warning */ |
| 4279 | (void)options; |
| 4280 | |
| 4281 | if (options & LYXP_SCNODE_ALL) { |
| 4282 | set_scnode_clear_ctx(set); |
| 4283 | return LY_SUCCESS; |
| 4284 | } |
| 4285 | |
| 4286 | if (arg_count) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4287 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4288 | 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] | 4289 | "namespace-uri(node-set?)"); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4290 | return LY_EVALID; |
| 4291 | } else if (!args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4292 | set_fill_string(set, "", 0); |
| 4293 | return LY_SUCCESS; |
| 4294 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4295 | |
| 4296 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4297 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4298 | |
| 4299 | item = &args[0]->val.nodes[0]; |
| 4300 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4301 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4302 | 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] | 4303 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4304 | } else if (!set->used) { |
| 4305 | set_fill_string(set, "", 0); |
| 4306 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4307 | } |
| 4308 | |
| 4309 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4310 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4311 | |
| 4312 | item = &set->val.nodes[0]; |
| 4313 | } |
| 4314 | |
| 4315 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4316 | case LYXP_NODE_NONE: |
| 4317 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4318 | case LYXP_NODE_ROOT: |
| 4319 | case LYXP_NODE_ROOT_CONFIG: |
| 4320 | case LYXP_NODE_TEXT: |
| 4321 | set_fill_string(set, "", 0); |
| 4322 | break; |
| 4323 | case LYXP_NODE_ELEM: |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4324 | case LYXP_NODE_META: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4325 | if (item->type == LYXP_NODE_ELEM) { |
| 4326 | mod = item->node->schema->module; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4327 | } else { /* LYXP_NODE_META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4328 | /* annotations */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4329 | mod = ((struct lyd_meta *)item->node)->annotation->module; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4330 | } |
| 4331 | |
| 4332 | set_fill_string(set, mod->ns, strlen(mod->ns)); |
| 4333 | break; |
| 4334 | } |
| 4335 | |
| 4336 | return LY_SUCCESS; |
| 4337 | } |
| 4338 | |
| 4339 | /** |
| 4340 | * @brief Execute the XPath node() function (node type). Returns LYXP_SET_NODE_SET |
| 4341 | * with only nodes from the context. In practice it either leaves the context |
| 4342 | * as it is or returns an empty node set. |
| 4343 | * |
| 4344 | * @param[in] args Array of arguments. |
| 4345 | * @param[in] arg_count Count of elements in @p args. |
| 4346 | * @param[in,out] set Context and result set at the same time. |
| 4347 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4348 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4349 | */ |
| 4350 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4351 | 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] | 4352 | { |
| 4353 | if (options & LYXP_SCNODE_ALL) { |
| 4354 | set_scnode_clear_ctx(set); |
| 4355 | return LY_SUCCESS; |
| 4356 | } |
| 4357 | |
| 4358 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4359 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4360 | } |
| 4361 | return LY_SUCCESS; |
| 4362 | } |
| 4363 | |
| 4364 | /** |
| 4365 | * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING |
| 4366 | * with normalized value (no leading, trailing, double white spaces) of the node |
| 4367 | * from the argument or the context. |
| 4368 | * |
| 4369 | * @param[in] args Array of arguments. |
| 4370 | * @param[in] arg_count Count of elements in @p args. |
| 4371 | * @param[in,out] set Context and result set at the same time. |
| 4372 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4373 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4374 | */ |
| 4375 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4376 | 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] | 4377 | { |
| 4378 | uint16_t i, new_used; |
| 4379 | char *new; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 4380 | ly_bool have_spaces = 0, space_before = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4381 | struct lysc_node_leaf *sleaf; |
| 4382 | LY_ERR rc = LY_SUCCESS; |
| 4383 | |
| 4384 | if (options & LYXP_SCNODE_ALL) { |
| 4385 | if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4386 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4387 | 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] | 4388 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4389 | 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] | 4390 | } |
| 4391 | } |
| 4392 | set_scnode_clear_ctx(set); |
| 4393 | return rc; |
| 4394 | } |
| 4395 | |
| 4396 | if (arg_count) { |
| 4397 | set_fill_set(set, args[0]); |
| 4398 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4399 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4400 | LY_CHECK_RET(rc); |
| 4401 | |
| 4402 | /* is there any normalization necessary? */ |
| 4403 | for (i = 0; set->val.str[i]; ++i) { |
| 4404 | if (is_xmlws(set->val.str[i])) { |
| 4405 | if ((i == 0) || space_before || (!set->val.str[i + 1])) { |
| 4406 | have_spaces = 1; |
| 4407 | break; |
| 4408 | } |
| 4409 | space_before = 1; |
| 4410 | } else { |
| 4411 | space_before = 0; |
| 4412 | } |
| 4413 | } |
| 4414 | |
| 4415 | /* yep, there is */ |
| 4416 | if (have_spaces) { |
| 4417 | /* it's enough, at least one character will go, makes space for ending '\0' */ |
| 4418 | new = malloc(strlen(set->val.str) * sizeof(char)); |
| 4419 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 4420 | new_used = 0; |
| 4421 | |
| 4422 | space_before = 0; |
| 4423 | for (i = 0; set->val.str[i]; ++i) { |
| 4424 | if (is_xmlws(set->val.str[i])) { |
| 4425 | if ((i == 0) || space_before) { |
| 4426 | space_before = 1; |
| 4427 | continue; |
| 4428 | } else { |
| 4429 | space_before = 1; |
| 4430 | } |
| 4431 | } else { |
| 4432 | space_before = 0; |
| 4433 | } |
| 4434 | |
| 4435 | new[new_used] = (space_before ? ' ' : set->val.str[i]); |
| 4436 | ++new_used; |
| 4437 | } |
| 4438 | |
| 4439 | /* at worst there is one trailing space now */ |
| 4440 | if (new_used && is_xmlws(new[new_used - 1])) { |
| 4441 | --new_used; |
| 4442 | } |
| 4443 | |
| 4444 | new = ly_realloc(new, (new_used + 1) * sizeof(char)); |
| 4445 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 4446 | new[new_used] = '\0'; |
| 4447 | |
| 4448 | free(set->val.str); |
| 4449 | set->val.str = new; |
| 4450 | } |
| 4451 | |
| 4452 | return LY_SUCCESS; |
| 4453 | } |
| 4454 | |
| 4455 | /** |
| 4456 | * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN |
| 4457 | * with the argument converted to boolean and logically inverted. |
| 4458 | * |
| 4459 | * @param[in] args Array of arguments. |
| 4460 | * @param[in] arg_count Count of elements in @p args. |
| 4461 | * @param[in,out] set Context and result set at the same time. |
| 4462 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4463 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4464 | */ |
| 4465 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4466 | 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] | 4467 | { |
| 4468 | if (options & LYXP_SCNODE_ALL) { |
| 4469 | set_scnode_clear_ctx(set); |
| 4470 | return LY_SUCCESS; |
| 4471 | } |
| 4472 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4473 | lyxp_set_cast(args[0], LYXP_SET_BOOLEAN); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4474 | if (args[0]->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4475 | set_fill_boolean(set, 0); |
| 4476 | } else { |
| 4477 | set_fill_boolean(set, 1); |
| 4478 | } |
| 4479 | |
| 4480 | return LY_SUCCESS; |
| 4481 | } |
| 4482 | |
| 4483 | /** |
| 4484 | * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER |
| 4485 | * with the number representation of either the argument or the context. |
| 4486 | * |
| 4487 | * @param[in] args Array of arguments. |
| 4488 | * @param[in] arg_count Count of elements in @p args. |
| 4489 | * @param[in,out] set Context and result set at the same time. |
| 4490 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4491 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4492 | */ |
| 4493 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4494 | 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] | 4495 | { |
| 4496 | LY_ERR rc; |
| 4497 | |
| 4498 | if (options & LYXP_SCNODE_ALL) { |
| 4499 | set_scnode_clear_ctx(set); |
| 4500 | return LY_SUCCESS; |
| 4501 | } |
| 4502 | |
| 4503 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4504 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4505 | LY_CHECK_RET(rc); |
| 4506 | set_fill_set(set, args[0]); |
| 4507 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4508 | rc = lyxp_set_cast(set, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4509 | LY_CHECK_RET(rc); |
| 4510 | } |
| 4511 | |
| 4512 | return LY_SUCCESS; |
| 4513 | } |
| 4514 | |
| 4515 | /** |
| 4516 | * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER |
| 4517 | * with the context position. |
| 4518 | * |
| 4519 | * @param[in] args Array of arguments. |
| 4520 | * @param[in] arg_count Count of elements in @p args. |
| 4521 | * @param[in,out] set Context and result set at the same time. |
| 4522 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4523 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4524 | */ |
| 4525 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4526 | 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] | 4527 | { |
| 4528 | if (options & LYXP_SCNODE_ALL) { |
| 4529 | set_scnode_clear_ctx(set); |
| 4530 | return LY_SUCCESS; |
| 4531 | } |
| 4532 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4533 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4534 | LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "position()"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4535 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4536 | } else if (!set->used) { |
| 4537 | set_fill_number(set, 0); |
| 4538 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4539 | } |
| 4540 | |
| 4541 | set_fill_number(set, set->ctx_pos); |
| 4542 | |
| 4543 | /* UNUSED in 'Release' build type */ |
| 4544 | (void)options; |
| 4545 | return LY_SUCCESS; |
| 4546 | } |
| 4547 | |
| 4548 | /** |
| 4549 | * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN |
| 4550 | * depending on whether the second argument regex matches the first argument string. For details refer to |
| 4551 | * YANG 1.1 RFC section 10.2.1. |
| 4552 | * |
| 4553 | * @param[in] args Array of arguments. |
| 4554 | * @param[in] arg_count Count of elements in @p args. |
| 4555 | * @param[in,out] set Context and result set at the same time. |
| 4556 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4557 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4558 | */ |
| 4559 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4560 | 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] | 4561 | { |
| 4562 | struct lysc_pattern **patterns = NULL, **pattern; |
| 4563 | struct lysc_node_leaf *sleaf; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4564 | LY_ERR rc = LY_SUCCESS; |
| 4565 | struct ly_err_item *err; |
| 4566 | |
| 4567 | if (options & LYXP_SCNODE_ALL) { |
| 4568 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4569 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4570 | 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] | 4571 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4572 | 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] | 4573 | } |
| 4574 | } |
| 4575 | |
| 4576 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4577 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4578 | 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] | 4579 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4580 | 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] | 4581 | } |
| 4582 | } |
| 4583 | set_scnode_clear_ctx(set); |
| 4584 | return rc; |
| 4585 | } |
| 4586 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4587 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4588 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4589 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4590 | LY_CHECK_RET(rc); |
| 4591 | |
| 4592 | LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM); |
| 4593 | *pattern = malloc(sizeof **pattern); |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 4594 | LOG_LOCSET(NULL, set->cur_node, NULL, NULL); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4595 | rc = lys_compile_type_pattern_check(set->ctx, args[1]->val.str, &(*pattern)->code); |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 4596 | LOG_LOCBACK(0, 1, 0, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4597 | if (rc != LY_SUCCESS) { |
| 4598 | LY_ARRAY_FREE(patterns); |
| 4599 | return rc; |
| 4600 | } |
| 4601 | |
| 4602 | rc = ly_type_validate_patterns(patterns, args[0]->val.str, strlen(args[0]->val.str), &err); |
| 4603 | pcre2_code_free((*pattern)->code); |
| 4604 | free(*pattern); |
| 4605 | LY_ARRAY_FREE(patterns); |
| 4606 | if (rc && (rc != LY_EVALID)) { |
Michal Vasko | 177d0ed | 2020-11-23 16:43:03 +0100 | [diff] [blame] | 4607 | ly_err_print(set->ctx, err); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4608 | ly_err_free(err); |
| 4609 | return rc; |
| 4610 | } |
| 4611 | |
| 4612 | if (rc == LY_EVALID) { |
| 4613 | ly_err_free(err); |
| 4614 | set_fill_boolean(set, 0); |
| 4615 | } else { |
| 4616 | set_fill_boolean(set, 1); |
| 4617 | } |
| 4618 | |
| 4619 | return LY_SUCCESS; |
| 4620 | } |
| 4621 | |
| 4622 | /** |
| 4623 | * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER |
| 4624 | * with the rounded first argument. For details refer to |
| 4625 | * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round. |
| 4626 | * |
| 4627 | * @param[in] args Array of arguments. |
| 4628 | * @param[in] arg_count Count of elements in @p args. |
| 4629 | * @param[in,out] set Context and result set at the same time. |
| 4630 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4631 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4632 | */ |
| 4633 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4634 | 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] | 4635 | { |
| 4636 | struct lysc_node_leaf *sleaf; |
| 4637 | LY_ERR rc = LY_SUCCESS; |
| 4638 | |
| 4639 | if (options & LYXP_SCNODE_ALL) { |
| 4640 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4641 | 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] | 4642 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4643 | 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] | 4644 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) { |
| 4645 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4646 | } |
| 4647 | set_scnode_clear_ctx(set); |
| 4648 | return rc; |
| 4649 | } |
| 4650 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4651 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4652 | LY_CHECK_RET(rc); |
| 4653 | |
| 4654 | /* cover only the cases where floor can't be used */ |
| 4655 | if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) { |
| 4656 | set_fill_number(set, -0.0f); |
| 4657 | } else { |
| 4658 | args[0]->val.num += 0.5; |
| 4659 | rc = xpath_floor(args, 1, args[0], options); |
| 4660 | LY_CHECK_RET(rc); |
| 4661 | set_fill_number(set, args[0]->val.num); |
| 4662 | } |
| 4663 | |
| 4664 | return LY_SUCCESS; |
| 4665 | } |
| 4666 | |
| 4667 | /** |
| 4668 | * @brief Execute the XPath starts-with(string, string) function. |
| 4669 | * Returns LYXP_SET_BOOLEAN whether the second argument is |
| 4670 | * the prefix of the first or not. |
| 4671 | * |
| 4672 | * @param[in] args Array of arguments. |
| 4673 | * @param[in] arg_count Count of elements in @p args. |
| 4674 | * @param[in,out] set Context and result set at the same time. |
| 4675 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4676 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4677 | */ |
| 4678 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4679 | 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] | 4680 | { |
| 4681 | struct lysc_node_leaf *sleaf; |
| 4682 | LY_ERR rc = LY_SUCCESS; |
| 4683 | |
| 4684 | if (options & LYXP_SCNODE_ALL) { |
| 4685 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4686 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4687 | 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] | 4688 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4689 | 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] | 4690 | } |
| 4691 | } |
| 4692 | |
| 4693 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4694 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4695 | 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] | 4696 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4697 | 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] | 4698 | } |
| 4699 | } |
| 4700 | set_scnode_clear_ctx(set); |
| 4701 | return rc; |
| 4702 | } |
| 4703 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4704 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4705 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4706 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4707 | LY_CHECK_RET(rc); |
| 4708 | |
| 4709 | if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) { |
| 4710 | set_fill_boolean(set, 0); |
| 4711 | } else { |
| 4712 | set_fill_boolean(set, 1); |
| 4713 | } |
| 4714 | |
| 4715 | return LY_SUCCESS; |
| 4716 | } |
| 4717 | |
| 4718 | /** |
| 4719 | * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING |
| 4720 | * with the string representation of either the argument or the context. |
| 4721 | * |
| 4722 | * @param[in] args Array of arguments. |
| 4723 | * @param[in] arg_count Count of elements in @p args. |
| 4724 | * @param[in,out] set Context and result set at the same time. |
| 4725 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4726 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4727 | */ |
| 4728 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4729 | 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] | 4730 | { |
| 4731 | LY_ERR rc; |
| 4732 | |
| 4733 | if (options & LYXP_SCNODE_ALL) { |
| 4734 | set_scnode_clear_ctx(set); |
| 4735 | return LY_SUCCESS; |
| 4736 | } |
| 4737 | |
| 4738 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4739 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4740 | LY_CHECK_RET(rc); |
| 4741 | set_fill_set(set, args[0]); |
| 4742 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4743 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4744 | LY_CHECK_RET(rc); |
| 4745 | } |
| 4746 | |
| 4747 | return LY_SUCCESS; |
| 4748 | } |
| 4749 | |
| 4750 | /** |
| 4751 | * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER |
| 4752 | * with the length of the string in either the argument or the context. |
| 4753 | * |
| 4754 | * @param[in] args Array of arguments. |
| 4755 | * @param[in] arg_count Count of elements in @p args. |
| 4756 | * @param[in,out] set Context and result set at the same time. |
| 4757 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4758 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4759 | */ |
| 4760 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4761 | 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] | 4762 | { |
| 4763 | struct lysc_node_leaf *sleaf; |
| 4764 | LY_ERR rc = LY_SUCCESS; |
| 4765 | |
| 4766 | if (options & LYXP_SCNODE_ALL) { |
| 4767 | if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4768 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4769 | 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] | 4770 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4771 | 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] | 4772 | } |
| 4773 | } |
| 4774 | if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) { |
| 4775 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4776 | 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] | 4777 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4778 | 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] | 4779 | } |
| 4780 | } |
| 4781 | set_scnode_clear_ctx(set); |
| 4782 | return rc; |
| 4783 | } |
| 4784 | |
| 4785 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4786 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4787 | LY_CHECK_RET(rc); |
| 4788 | set_fill_number(set, strlen(args[0]->val.str)); |
| 4789 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4790 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4791 | LY_CHECK_RET(rc); |
| 4792 | set_fill_number(set, strlen(set->val.str)); |
| 4793 | } |
| 4794 | |
| 4795 | return LY_SUCCESS; |
| 4796 | } |
| 4797 | |
| 4798 | /** |
| 4799 | * @brief Execute the XPath substring(string, number, number?) function. |
| 4800 | * Returns LYXP_SET_STRING substring of the first argument starting |
| 4801 | * on the second argument index ending on the third argument index, |
| 4802 | * indexed from 1. For exact definition refer to |
| 4803 | * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring. |
| 4804 | * |
| 4805 | * @param[in] args Array of arguments. |
| 4806 | * @param[in] arg_count Count of elements in @p args. |
| 4807 | * @param[in,out] set Context and result set at the same time. |
| 4808 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4809 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4810 | */ |
| 4811 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4812 | xpath_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] | 4813 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4814 | int32_t start, len; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4815 | uint16_t str_start, str_len, pos; |
| 4816 | struct lysc_node_leaf *sleaf; |
| 4817 | LY_ERR rc = LY_SUCCESS; |
| 4818 | |
| 4819 | if (options & LYXP_SCNODE_ALL) { |
| 4820 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4821 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4822 | 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] | 4823 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4824 | 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] | 4825 | } |
| 4826 | } |
| 4827 | |
| 4828 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4829 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4830 | 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] | 4831 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 4832 | 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] | 4833 | } |
| 4834 | } |
| 4835 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 4836 | if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET) && |
| 4837 | (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4838 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4839 | 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] | 4840 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 4841 | 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] | 4842 | } |
| 4843 | } |
| 4844 | set_scnode_clear_ctx(set); |
| 4845 | return rc; |
| 4846 | } |
| 4847 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4848 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4849 | LY_CHECK_RET(rc); |
| 4850 | |
| 4851 | /* start */ |
| 4852 | if (xpath_round(&args[1], 1, args[1], options)) { |
| 4853 | return -1; |
| 4854 | } |
| 4855 | if (isfinite(args[1]->val.num)) { |
| 4856 | start = args[1]->val.num - 1; |
| 4857 | } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4858 | start = INT32_MIN; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4859 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4860 | start = INT32_MAX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4861 | } |
| 4862 | |
| 4863 | /* len */ |
| 4864 | if (arg_count == 3) { |
| 4865 | rc = xpath_round(&args[2], 1, args[2], options); |
| 4866 | LY_CHECK_RET(rc); |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4867 | if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4868 | len = 0; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4869 | } else if (isfinite(args[2]->val.num)) { |
| 4870 | len = args[2]->val.num; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4871 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4872 | len = INT32_MAX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4873 | } |
| 4874 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4875 | len = INT32_MAX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4876 | } |
| 4877 | |
| 4878 | /* find matching character positions */ |
| 4879 | str_start = 0; |
| 4880 | str_len = 0; |
| 4881 | for (pos = 0; args[0]->val.str[pos]; ++pos) { |
| 4882 | if (pos < start) { |
| 4883 | ++str_start; |
| 4884 | } else if (pos < start + len) { |
| 4885 | ++str_len; |
| 4886 | } else { |
| 4887 | break; |
| 4888 | } |
| 4889 | } |
| 4890 | |
| 4891 | set_fill_string(set, args[0]->val.str + str_start, str_len); |
| 4892 | return LY_SUCCESS; |
| 4893 | } |
| 4894 | |
| 4895 | /** |
| 4896 | * @brief Execute the XPath substring-after(string, string) function. |
| 4897 | * Returns LYXP_SET_STRING with the string succeeding the occurance |
| 4898 | * of the second argument in the first or an empty string. |
| 4899 | * |
| 4900 | * @param[in] args Array of arguments. |
| 4901 | * @param[in] arg_count Count of elements in @p args. |
| 4902 | * @param[in,out] set Context and result set at the same time. |
| 4903 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4904 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4905 | */ |
| 4906 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4907 | 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] | 4908 | { |
| 4909 | char *ptr; |
| 4910 | struct lysc_node_leaf *sleaf; |
| 4911 | LY_ERR rc = LY_SUCCESS; |
| 4912 | |
| 4913 | if (options & LYXP_SCNODE_ALL) { |
| 4914 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4915 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4916 | 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] | 4917 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4918 | 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] | 4919 | } |
| 4920 | } |
| 4921 | |
| 4922 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4923 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4924 | 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] | 4925 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4926 | 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] | 4927 | } |
| 4928 | } |
| 4929 | set_scnode_clear_ctx(set); |
| 4930 | return rc; |
| 4931 | } |
| 4932 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4933 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4934 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4935 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4936 | LY_CHECK_RET(rc); |
| 4937 | |
| 4938 | ptr = strstr(args[0]->val.str, args[1]->val.str); |
| 4939 | if (ptr) { |
| 4940 | set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str))); |
| 4941 | } else { |
| 4942 | set_fill_string(set, "", 0); |
| 4943 | } |
| 4944 | |
| 4945 | return LY_SUCCESS; |
| 4946 | } |
| 4947 | |
| 4948 | /** |
| 4949 | * @brief Execute the XPath substring-before(string, string) function. |
| 4950 | * Returns LYXP_SET_STRING with the string preceding the occurance |
| 4951 | * of the second argument in the first or an empty string. |
| 4952 | * |
| 4953 | * @param[in] args Array of arguments. |
| 4954 | * @param[in] arg_count Count of elements in @p args. |
| 4955 | * @param[in,out] set Context and result set at the same time. |
| 4956 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4957 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4958 | */ |
| 4959 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4960 | 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] | 4961 | { |
| 4962 | char *ptr; |
| 4963 | struct lysc_node_leaf *sleaf; |
| 4964 | LY_ERR rc = LY_SUCCESS; |
| 4965 | |
| 4966 | if (options & LYXP_SCNODE_ALL) { |
| 4967 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4968 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4969 | 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] | 4970 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4971 | 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] | 4972 | } |
| 4973 | } |
| 4974 | |
| 4975 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4976 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4977 | 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] | 4978 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4979 | 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] | 4980 | } |
| 4981 | } |
| 4982 | set_scnode_clear_ctx(set); |
| 4983 | return rc; |
| 4984 | } |
| 4985 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4986 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4987 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4988 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4989 | LY_CHECK_RET(rc); |
| 4990 | |
| 4991 | ptr = strstr(args[0]->val.str, args[1]->val.str); |
| 4992 | if (ptr) { |
| 4993 | set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str); |
| 4994 | } else { |
| 4995 | set_fill_string(set, "", 0); |
| 4996 | } |
| 4997 | |
| 4998 | return LY_SUCCESS; |
| 4999 | } |
| 5000 | |
| 5001 | /** |
| 5002 | * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER |
| 5003 | * with the sum of all the nodes in the context. |
| 5004 | * |
| 5005 | * @param[in] args Array of arguments. |
| 5006 | * @param[in] arg_count Count of elements in @p args. |
| 5007 | * @param[in,out] set Context and result set at the same time. |
| 5008 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5009 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5010 | */ |
| 5011 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5012 | 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] | 5013 | { |
| 5014 | long double num; |
| 5015 | char *str; |
Michal Vasko | 1fdd8fa | 2021-01-08 09:21:45 +0100 | [diff] [blame] | 5016 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5017 | struct lyxp_set set_item; |
| 5018 | struct lysc_node_leaf *sleaf; |
| 5019 | LY_ERR rc = LY_SUCCESS; |
| 5020 | |
| 5021 | if (options & LYXP_SCNODE_ALL) { |
| 5022 | if (args[0]->type == LYXP_SET_SCNODE_SET) { |
| 5023 | for (i = 0; i < args[0]->used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 5024 | 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] | 5025 | sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode; |
| 5026 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5027 | 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] | 5028 | lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5029 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 5030 | 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] | 5031 | } |
| 5032 | } |
| 5033 | } |
| 5034 | } |
| 5035 | set_scnode_clear_ctx(set); |
| 5036 | return rc; |
| 5037 | } |
| 5038 | |
| 5039 | set_fill_number(set, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5040 | |
| 5041 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 5042 | 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] | 5043 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5044 | } else if (!args[0]->used) { |
| 5045 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5046 | } |
| 5047 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5048 | set_init(&set_item, set); |
| 5049 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5050 | set_item.type = LYXP_SET_NODE_SET; |
| 5051 | set_item.val.nodes = malloc(sizeof *set_item.val.nodes); |
| 5052 | LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM); |
| 5053 | |
| 5054 | set_item.used = 1; |
| 5055 | set_item.size = 1; |
| 5056 | |
| 5057 | for (i = 0; i < args[0]->used; ++i) { |
| 5058 | set_item.val.nodes[0] = args[0]->val.nodes[i]; |
| 5059 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5060 | rc = cast_node_set_to_string(&set_item, &str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5061 | LY_CHECK_RET(rc); |
| 5062 | num = cast_string_to_number(str); |
| 5063 | free(str); |
| 5064 | set->val.num += num; |
| 5065 | } |
| 5066 | |
| 5067 | free(set_item.val.nodes); |
| 5068 | |
| 5069 | return LY_SUCCESS; |
| 5070 | } |
| 5071 | |
| 5072 | /** |
| 5073 | * @brief Execute the XPath text() function (node type). Returns LYXP_SET_NODE_SET |
| 5074 | * with the text content of the nodes in the context. |
| 5075 | * |
| 5076 | * @param[in] args Array of arguments. |
| 5077 | * @param[in] arg_count Count of elements in @p args. |
| 5078 | * @param[in,out] set Context and result set at the same time. |
| 5079 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5080 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5081 | */ |
| 5082 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5083 | 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] | 5084 | { |
| 5085 | uint32_t i; |
| 5086 | |
| 5087 | if (options & LYXP_SCNODE_ALL) { |
| 5088 | set_scnode_clear_ctx(set); |
| 5089 | return LY_SUCCESS; |
| 5090 | } |
| 5091 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5092 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 5093 | LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "text()"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5094 | return LY_EVALID; |
| 5095 | } |
| 5096 | |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 5097 | for (i = 0; i < set->used; ) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5098 | switch (set->val.nodes[i].type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 5099 | case LYXP_NODE_NONE: |
| 5100 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5101 | case LYXP_NODE_ELEM: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5102 | if (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 5103 | set->val.nodes[i].type = LYXP_NODE_TEXT; |
| 5104 | ++i; |
| 5105 | break; |
| 5106 | } |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 5107 | /* fall through */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5108 | case LYXP_NODE_ROOT: |
| 5109 | case LYXP_NODE_ROOT_CONFIG: |
| 5110 | case LYXP_NODE_TEXT: |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5111 | case LYXP_NODE_META: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5112 | set_remove_node(set, i); |
| 5113 | break; |
| 5114 | } |
| 5115 | } |
| 5116 | |
| 5117 | return LY_SUCCESS; |
| 5118 | } |
| 5119 | |
| 5120 | /** |
| 5121 | * @brief Execute the XPath translate(string, string, string) function. |
| 5122 | * Returns LYXP_SET_STRING with the first argument with the characters |
| 5123 | * from the second argument replaced by those on the corresponding |
| 5124 | * positions in the third argument. |
| 5125 | * |
| 5126 | * @param[in] args Array of arguments. |
| 5127 | * @param[in] arg_count Count of elements in @p args. |
| 5128 | * @param[in,out] set Context and result set at the same time. |
| 5129 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5130 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5131 | */ |
| 5132 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5133 | 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] | 5134 | { |
| 5135 | uint16_t i, j, new_used; |
| 5136 | char *new; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 5137 | ly_bool have_removed; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5138 | struct lysc_node_leaf *sleaf; |
| 5139 | LY_ERR rc = LY_SUCCESS; |
| 5140 | |
| 5141 | if (options & LYXP_SCNODE_ALL) { |
| 5142 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 5143 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5144 | 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] | 5145 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5146 | 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] | 5147 | } |
| 5148 | } |
| 5149 | |
| 5150 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 5151 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5152 | 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] | 5153 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5154 | 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] | 5155 | } |
| 5156 | } |
| 5157 | |
| 5158 | if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) { |
| 5159 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5160 | 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] | 5161 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5162 | 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] | 5163 | } |
| 5164 | } |
| 5165 | set_scnode_clear_ctx(set); |
| 5166 | return rc; |
| 5167 | } |
| 5168 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5169 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5170 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5171 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5172 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5173 | rc = lyxp_set_cast(args[2], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5174 | LY_CHECK_RET(rc); |
| 5175 | |
| 5176 | new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char)); |
| 5177 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 5178 | new_used = 0; |
| 5179 | |
| 5180 | have_removed = 0; |
| 5181 | for (i = 0; args[0]->val.str[i]; ++i) { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 5182 | ly_bool found = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5183 | |
| 5184 | for (j = 0; args[1]->val.str[j]; ++j) { |
| 5185 | if (args[0]->val.str[i] == args[1]->val.str[j]) { |
| 5186 | /* removing this char */ |
| 5187 | if (j >= strlen(args[2]->val.str)) { |
| 5188 | have_removed = 1; |
| 5189 | found = 1; |
| 5190 | break; |
| 5191 | } |
| 5192 | /* replacing this char */ |
| 5193 | new[new_used] = args[2]->val.str[j]; |
| 5194 | ++new_used; |
| 5195 | found = 1; |
| 5196 | break; |
| 5197 | } |
| 5198 | } |
| 5199 | |
| 5200 | /* copying this char */ |
| 5201 | if (!found) { |
| 5202 | new[new_used] = args[0]->val.str[i]; |
| 5203 | ++new_used; |
| 5204 | } |
| 5205 | } |
| 5206 | |
| 5207 | if (have_removed) { |
| 5208 | new = ly_realloc(new, (new_used + 1) * sizeof(char)); |
| 5209 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 5210 | } |
| 5211 | new[new_used] = '\0'; |
| 5212 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5213 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5214 | set->type = LYXP_SET_STRING; |
| 5215 | set->val.str = new; |
| 5216 | |
| 5217 | return LY_SUCCESS; |
| 5218 | } |
| 5219 | |
| 5220 | /** |
| 5221 | * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN |
| 5222 | * with true value. |
| 5223 | * |
| 5224 | * @param[in] args Array of arguments. |
| 5225 | * @param[in] arg_count Count of elements in @p args. |
| 5226 | * @param[in,out] set Context and result set at the same time. |
| 5227 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5228 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5229 | */ |
| 5230 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5231 | 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] | 5232 | { |
| 5233 | if (options & LYXP_SCNODE_ALL) { |
| 5234 | set_scnode_clear_ctx(set); |
| 5235 | return LY_SUCCESS; |
| 5236 | } |
| 5237 | |
| 5238 | set_fill_boolean(set, 1); |
| 5239 | return LY_SUCCESS; |
| 5240 | } |
| 5241 | |
| 5242 | /* |
| 5243 | * moveto functions |
| 5244 | * |
| 5245 | * They and only they actually change the context (set). |
| 5246 | */ |
| 5247 | |
| 5248 | /** |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5249 | * @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] | 5250 | * |
Michal Vasko | 2104e9f | 2020-03-06 08:23:25 +0100 | [diff] [blame] | 5251 | * XPath @p set is expected to be a (sc)node set! |
| 5252 | * |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5253 | * @param[in,out] qname Qualified node name. If includes prefix, it is skipped. |
| 5254 | * @param[in,out] qname_len Length of @p qname, is updated accordingly. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5255 | * @param[in] set Set with general XPath context. |
| 5256 | * @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] | 5257 | * @param[out] moveto_mod Expected module of a matching node. |
| 5258 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5259 | */ |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5260 | static LY_ERR |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5261 | moveto_resolve_model(const char **qname, uint16_t *qname_len, const struct lyxp_set *set, |
| 5262 | const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5263 | { |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 5264 | const struct lys_module *mod = NULL; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5265 | const char *ptr; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5266 | size_t pref_len; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5267 | |
Michal Vasko | 2104e9f | 2020-03-06 08:23:25 +0100 | [diff] [blame] | 5268 | assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET)); |
| 5269 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5270 | if ((ptr = ly_strnchr(*qname, ':', *qname_len))) { |
| 5271 | /* specific module */ |
| 5272 | pref_len = ptr - *qname; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5273 | 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] | 5274 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5275 | /* check for errors and non-implemented modules, as they are not valid */ |
Juraj Vijtiuk | d75faa6 | 2019-11-26 14:10:10 +0100 | [diff] [blame] | 5276 | if (!mod || !mod->implemented) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 5277 | LOGVAL(set->ctx, LY_VCODE_XP_INMOD, pref_len, *qname); |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5278 | return LY_EVALID; |
| 5279 | } |
Juraj Vijtiuk | d75faa6 | 2019-11-26 14:10:10 +0100 | [diff] [blame] | 5280 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5281 | *qname += pref_len + 1; |
| 5282 | *qname_len -= pref_len + 1; |
| 5283 | } else if (((*qname)[0] == '*') && (*qname_len == 1)) { |
| 5284 | /* all modules - special case */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5285 | mod = NULL; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5286 | } else { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5287 | switch (set->format) { |
| 5288 | case LY_PREF_SCHEMA: |
| 5289 | case LY_PREF_SCHEMA_RESOLVED: |
| 5290 | /* current module */ |
| 5291 | mod = set->cur_mod; |
| 5292 | break; |
| 5293 | case LY_PREF_JSON: |
| 5294 | /* inherit parent (context node) module */ |
| 5295 | if (ctx_scnode) { |
| 5296 | mod = ctx_scnode->module; |
| 5297 | } else { |
| 5298 | mod = NULL; |
| 5299 | } |
| 5300 | break; |
| 5301 | case LY_PREF_XML: |
| 5302 | /* not defined */ |
| 5303 | LOGINT_RET(set->ctx); |
| 5304 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5305 | } |
| 5306 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5307 | *moveto_mod = mod; |
| 5308 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5309 | } |
| 5310 | |
| 5311 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5312 | * @brief Move context @p set to the root. Handles absolute path. |
| 5313 | * Result is LYXP_SET_NODE_SET. |
| 5314 | * |
| 5315 | * @param[in,out] set Set to use. |
| 5316 | * @param[in] options Xpath options. |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 5317 | * @return LY_ERR value. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5318 | */ |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 5319 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5320 | moveto_root(struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5321 | { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5322 | if (!set) { |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 5323 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5324 | } |
| 5325 | |
| 5326 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5327 | set_scnode_clear_ctx(set); |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 5328 | 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] | 5329 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5330 | set->type = LYXP_SET_NODE_SET; |
| 5331 | set->used = 0; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5332 | set_insert_node(set, NULL, 0, set->root_type, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5333 | } |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 5334 | |
| 5335 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5336 | } |
| 5337 | |
| 5338 | /** |
| 5339 | * @brief Check @p node as a part of NameTest processing. |
| 5340 | * |
| 5341 | * @param[in] node Node to check. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5342 | * @param[in] ctx_node Context node. |
| 5343 | * @param[in] set Set to read general context from. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5344 | * @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] | 5345 | * @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] | 5346 | * @param[in] options XPath options. |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5347 | * @return LY_ERR (LY_ENOT if node does not match, LY_EINCOMPLETE on unresolved when, |
| 5348 | * LY_EINVAL if netither node nor any children match) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5349 | */ |
| 5350 | static LY_ERR |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5351 | moveto_node_check(const struct lyd_node *node, const struct lyd_node *ctx_node, const struct lyxp_set *set, |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 5352 | const char *node_name, const struct lys_module *moveto_mod, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5353 | { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5354 | if (!moveto_mod && (!node_name || strcmp(node_name, "*"))) { |
| 5355 | switch (set->format) { |
| 5356 | case LY_PREF_SCHEMA: |
| 5357 | case LY_PREF_SCHEMA_RESOLVED: |
| 5358 | /* use current module */ |
| 5359 | moveto_mod = set->cur_mod; |
| 5360 | break; |
| 5361 | case LY_PREF_JSON: |
| 5362 | /* inherit module of the context node, if any */ |
| 5363 | if (ctx_node) { |
| 5364 | moveto_mod = ctx_node->schema->module; |
| 5365 | } |
| 5366 | break; |
| 5367 | case LY_PREF_XML: |
| 5368 | /* not defined */ |
| 5369 | LOGINT(set->ctx); |
| 5370 | return LY_EINVAL; |
| 5371 | } |
| 5372 | } |
| 5373 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5374 | /* module check */ |
| 5375 | if (moveto_mod && (node->schema->module != moveto_mod)) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5376 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5377 | } |
| 5378 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5379 | /* context check */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5380 | 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] | 5381 | return LY_EINVAL; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5382 | } else if (set->context_op && (node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && |
| 5383 | (node->schema != set->context_op)) { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5384 | return LY_EINVAL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5385 | } |
| 5386 | |
| 5387 | /* name check */ |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5388 | if (node_name && strcmp(node_name, "*") && (node->schema->name != node_name)) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5389 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5390 | } |
| 5391 | |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 5392 | /* when check */ |
Michal Vasko | d5cfa6e | 2020-11-23 16:56:08 +0100 | [diff] [blame] | 5393 | 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] | 5394 | return LY_EINCOMPLETE; |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 5395 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5396 | |
| 5397 | /* match */ |
| 5398 | return LY_SUCCESS; |
| 5399 | } |
| 5400 | |
| 5401 | /** |
| 5402 | * @brief Check @p node as a part of schema NameTest processing. |
| 5403 | * |
| 5404 | * @param[in] node Schema node to check. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5405 | * @param[in] ctx_scnode Context node. |
| 5406 | * @param[in] set Set to read general context from. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5407 | * @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] | 5408 | * @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] | 5409 | * @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] | 5410 | */ |
| 5411 | static LY_ERR |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5412 | 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] | 5413 | const char *node_name, const struct lys_module *moveto_mod) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5414 | { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5415 | if (!moveto_mod && (!node_name || strcmp(node_name, "*"))) { |
| 5416 | switch (set->format) { |
| 5417 | case LY_PREF_SCHEMA: |
| 5418 | case LY_PREF_SCHEMA_RESOLVED: |
| 5419 | /* use current module */ |
| 5420 | moveto_mod = set->cur_mod; |
| 5421 | break; |
| 5422 | case LY_PREF_JSON: |
| 5423 | /* inherit module of the context node, if any */ |
| 5424 | if (ctx_scnode) { |
| 5425 | moveto_mod = ctx_scnode->module; |
| 5426 | } |
| 5427 | break; |
| 5428 | case LY_PREF_XML: |
| 5429 | /* not defined */ |
| 5430 | LOGINT(set->ctx); |
| 5431 | return LY_EINVAL; |
| 5432 | } |
| 5433 | } |
| 5434 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5435 | /* module check */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5436 | if (moveto_mod && (node->module != moveto_mod)) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5437 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5438 | } |
| 5439 | |
| 5440 | /* context check */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5441 | 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] | 5442 | return LY_EINVAL; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5443 | } 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] | 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->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 | |
| 5452 | /* match */ |
| 5453 | return LY_SUCCESS; |
| 5454 | } |
| 5455 | |
| 5456 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5457 | * @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] | 5458 | * |
| 5459 | * @param[in,out] set Set to use. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5460 | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5461 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 5462 | * @param[in] options XPath options. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5463 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 5464 | */ |
| 5465 | static LY_ERR |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 5466 | 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] | 5467 | { |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 5468 | uint32_t i; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5469 | const struct lyd_node *siblings, *sub, *ctx_node; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5470 | LY_ERR rc; |
| 5471 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5472 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5473 | return LY_SUCCESS; |
| 5474 | } |
| 5475 | |
| 5476 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 5477 | 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] | 5478 | return LY_EVALID; |
| 5479 | } |
| 5480 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5481 | for (i = 0; i < set->used; ) { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 5482 | ly_bool replaced = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5483 | |
| 5484 | 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] | 5485 | assert(!set->val.nodes[i].node); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5486 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5487 | /* search in all the trees */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5488 | ctx_node = NULL; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5489 | siblings = set->tree; |
Michal Vasko | 5bfd4be | 2020-06-23 13:26:19 +0200 | [diff] [blame] | 5490 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5491 | /* search in children */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5492 | ctx_node = set->val.nodes[i].node; |
| 5493 | siblings = lyd_child(ctx_node); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5494 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5495 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5496 | for (sub = siblings; sub; sub = sub->next) { |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 5497 | rc = moveto_node_check(sub, ctx_node, set, ncname, moveto_mod, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5498 | if (rc == LY_SUCCESS) { |
| 5499 | if (!replaced) { |
| 5500 | set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i); |
| 5501 | replaced = 1; |
| 5502 | } else { |
| 5503 | set_insert_node(set, sub, 0, LYXP_NODE_ELEM, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5504 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5505 | ++i; |
| 5506 | } else if (rc == LY_EINCOMPLETE) { |
| 5507 | return rc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5508 | } |
| 5509 | } |
| 5510 | |
| 5511 | if (!replaced) { |
| 5512 | /* no match */ |
| 5513 | set_remove_node(set, i); |
| 5514 | } |
| 5515 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5516 | |
| 5517 | return LY_SUCCESS; |
| 5518 | } |
| 5519 | |
| 5520 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5521 | * @brief Move context @p set to a node using hashes. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY). |
| 5522 | * Context position aware. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5523 | * |
| 5524 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5525 | * @param[in] scnode Matching node schema. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5526 | * @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] | 5527 | * @param[in] options XPath options. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5528 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 5529 | */ |
| 5530 | static LY_ERR |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 5531 | moveto_node_hash(struct lyxp_set *set, const struct lysc_node *scnode, const struct ly_path_predicate *predicates, |
| 5532 | uint32_t options) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5533 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5534 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5535 | uint32_t i; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5536 | const struct lyd_node *siblings; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5537 | struct lyd_node *sub, *inst = NULL; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5538 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5539 | assert(scnode && (!(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) || predicates)); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5540 | |
| 5541 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5542 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5543 | } |
| 5544 | |
| 5545 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 5546 | 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] | 5547 | ret = LY_EVALID; |
| 5548 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5549 | } |
| 5550 | |
| 5551 | /* context check for all the nodes since we have the schema node */ |
| 5552 | if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) { |
| 5553 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5554 | goto cleanup; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 5555 | } else if (set->context_op && (scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && |
| 5556 | (scnode != set->context_op)) { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5557 | lyxp_set_free_content(set); |
| 5558 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5559 | } |
| 5560 | |
| 5561 | /* create specific data instance if needed */ |
| 5562 | if (scnode->nodetype == LYS_LIST) { |
| 5563 | LY_CHECK_GOTO(ret = lyd_create_list(scnode, predicates, &inst), cleanup); |
| 5564 | } else if (scnode->nodetype == LYS_LEAFLIST) { |
| 5565 | 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] | 5566 | } |
| 5567 | |
| 5568 | for (i = 0; i < set->used; ) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5569 | siblings = NULL; |
| 5570 | |
| 5571 | if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) { |
| 5572 | assert(!set->val.nodes[i].node); |
| 5573 | |
| 5574 | /* search in all the trees */ |
| 5575 | siblings = set->tree; |
| 5576 | } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
| 5577 | /* search in children */ |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 5578 | siblings = lyd_child(set->val.nodes[i].node); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5579 | } |
| 5580 | |
| 5581 | /* find the node using hashes */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5582 | if (inst) { |
| 5583 | lyd_find_sibling_first(siblings, inst, &sub); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5584 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5585 | lyd_find_sibling_val(siblings, scnode, NULL, 0, &sub); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5586 | } |
| 5587 | |
| 5588 | /* when check */ |
Michal Vasko | d5cfa6e | 2020-11-23 16:56:08 +0100 | [diff] [blame] | 5589 | 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] | 5590 | ret = LY_EINCOMPLETE; |
| 5591 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5592 | } |
| 5593 | |
| 5594 | if (sub) { |
| 5595 | /* pos filled later */ |
Michal Vasko | cb1b7c0 | 2020-07-03 13:38:12 +0200 | [diff] [blame] | 5596 | set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5597 | ++i; |
Michal Vasko | cb1b7c0 | 2020-07-03 13:38:12 +0200 | [diff] [blame] | 5598 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5599 | /* no match */ |
| 5600 | set_remove_node(set, i); |
| 5601 | } |
| 5602 | } |
| 5603 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5604 | cleanup: |
| 5605 | lyd_free_tree(inst); |
| 5606 | return ret; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5607 | } |
| 5608 | |
| 5609 | /** |
| 5610 | * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY). |
| 5611 | * |
| 5612 | * @param[in,out] set Set to use. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5613 | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5614 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5615 | * @param[in] options XPath options. |
| 5616 | * @return LY_ERR |
| 5617 | */ |
| 5618 | static LY_ERR |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5619 | 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] | 5620 | { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 5621 | ly_bool temp_ctx = 0; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5622 | uint32_t getnext_opts; |
| 5623 | uint32_t orig_used, i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5624 | uint32_t mod_idx; |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 5625 | const struct lysc_node *iter, *start_parent; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5626 | const struct lys_module *mod; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5627 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5628 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5629 | return LY_SUCCESS; |
| 5630 | } |
| 5631 | |
| 5632 | if (set->type != LYXP_SET_SCNODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 5633 | 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] | 5634 | return LY_EVALID; |
| 5635 | } |
| 5636 | |
Michal Vasko | cafad9d | 2019-11-07 15:20:03 +0100 | [diff] [blame] | 5637 | /* getnext opts */ |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 5638 | getnext_opts = 0; |
Michal Vasko | cafad9d | 2019-11-07 15:20:03 +0100 | [diff] [blame] | 5639 | if (options & LYXP_SCNODE_OUTPUT) { |
| 5640 | getnext_opts |= LYS_GETNEXT_OUTPUT; |
| 5641 | } |
| 5642 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5643 | orig_used = set->used; |
| 5644 | for (i = 0; i < orig_used; ++i) { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 5645 | uint32_t idx; |
| 5646 | |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 5647 | if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) { |
| 5648 | if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5649 | continue; |
| 5650 | } |
| 5651 | |
| 5652 | /* remember context node */ |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 5653 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED; |
Michal Vasko | ec4df48 | 2019-12-16 10:02:18 +0100 | [diff] [blame] | 5654 | } else { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 5655 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5656 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5657 | |
| 5658 | start_parent = set->val.scnodes[i].scnode; |
| 5659 | |
| 5660 | 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] | 5661 | /* 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] | 5662 | * 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] | 5663 | mod_idx = 0; |
Michal Vasko | 9e9f26d | 2020-10-12 16:31:33 +0200 | [diff] [blame] | 5664 | while ((mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) { |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 5665 | iter = NULL; |
Michal Vasko | 509de4d | 2019-12-10 14:51:30 +0100 | [diff] [blame] | 5666 | /* module may not be implemented */ |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 5667 | while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5668 | if (!moveto_scnode_check(iter, NULL, set, ncname, moveto_mod)) { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 5669 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx)); |
| 5670 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5671 | /* we need to prevent these nodes from being considered in this moveto */ |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 5672 | if ((idx < orig_used) && (idx > i)) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 5673 | set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5674 | temp_ctx = 1; |
| 5675 | } |
| 5676 | } |
| 5677 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5678 | } |
| 5679 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 5680 | } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) { |
| 5681 | iter = NULL; |
| 5682 | while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5683 | if (!moveto_scnode_check(iter, start_parent, set, ncname, moveto_mod)) { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 5684 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx)); |
| 5685 | |
| 5686 | if ((idx < orig_used) && (idx > i)) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 5687 | set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5688 | temp_ctx = 1; |
| 5689 | } |
| 5690 | } |
| 5691 | } |
| 5692 | } |
| 5693 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5694 | |
| 5695 | /* correct temporary in_ctx values */ |
| 5696 | if (temp_ctx) { |
| 5697 | for (i = 0; i < orig_used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 5698 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NEW_CTX) { |
| 5699 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5700 | } |
| 5701 | } |
| 5702 | } |
| 5703 | |
| 5704 | return LY_SUCCESS; |
| 5705 | } |
| 5706 | |
| 5707 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5708 | * @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] | 5709 | * Context position aware. |
| 5710 | * |
| 5711 | * @param[in] set Set to use. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5712 | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5713 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 5714 | * @param[in] options XPath options. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5715 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 5716 | */ |
| 5717 | static LY_ERR |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 5718 | 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] | 5719 | { |
| 5720 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5721 | const struct lyd_node *next, *elem, *start; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5722 | struct lyxp_set ret_set; |
| 5723 | LY_ERR rc; |
| 5724 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5725 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5726 | return LY_SUCCESS; |
| 5727 | } |
| 5728 | |
| 5729 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 5730 | 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] | 5731 | return LY_EVALID; |
| 5732 | } |
| 5733 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5734 | /* 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] | 5735 | rc = moveto_node(set, NULL, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5736 | LY_CHECK_RET(rc); |
| 5737 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5738 | /* 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] | 5739 | set_init(&ret_set, set); |
| 5740 | for (i = 0; i < set->used; ++i) { |
| 5741 | |
| 5742 | /* TREE DFS */ |
| 5743 | start = set->val.nodes[i].node; |
| 5744 | for (elem = next = start; elem; elem = next) { |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 5745 | rc = moveto_node_check(elem, start, set, ncname, moveto_mod, options); |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5746 | if (!rc) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5747 | /* add matching node into result set */ |
| 5748 | set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used); |
| 5749 | if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) { |
| 5750 | /* the node is a duplicate, we'll process it later in the set */ |
| 5751 | goto skip_children; |
| 5752 | } |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5753 | } else if (rc == LY_EINCOMPLETE) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5754 | return rc; |
| 5755 | } else if (rc == LY_EINVAL) { |
| 5756 | goto skip_children; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5757 | } |
| 5758 | |
| 5759 | /* TREE DFS NEXT ELEM */ |
| 5760 | /* select element for the next run - children first */ |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 5761 | next = lyd_child(elem); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5762 | if (!next) { |
| 5763 | skip_children: |
| 5764 | /* no children, so try siblings, but only if it's not the start, |
| 5765 | * that is considered to be the root and it's siblings are not traversed */ |
| 5766 | if (elem != start) { |
| 5767 | next = elem->next; |
| 5768 | } else { |
| 5769 | break; |
| 5770 | } |
| 5771 | } |
| 5772 | while (!next) { |
| 5773 | /* no siblings, go back through the parents */ |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 5774 | if (lyd_parent(elem) == start) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5775 | /* we are done, no next element to process */ |
| 5776 | break; |
| 5777 | } |
| 5778 | /* parent is already processed, go to its sibling */ |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 5779 | elem = lyd_parent(elem); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5780 | next = elem->next; |
| 5781 | } |
| 5782 | } |
| 5783 | } |
| 5784 | |
| 5785 | /* make the temporary set the current one */ |
| 5786 | ret_set.ctx_pos = set->ctx_pos; |
| 5787 | ret_set.ctx_size = set->ctx_size; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5788 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5789 | memcpy(set, &ret_set, sizeof *set); |
| 5790 | |
| 5791 | return LY_SUCCESS; |
| 5792 | } |
| 5793 | |
| 5794 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5795 | * @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] | 5796 | * |
| 5797 | * @param[in] set Set to use. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5798 | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5799 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5800 | * @param[in] options XPath options. |
| 5801 | * @return LY_ERR |
| 5802 | */ |
| 5803 | static LY_ERR |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5804 | 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] | 5805 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5806 | uint32_t i, orig_used; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5807 | const struct lysc_node *next, *elem, *start; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5808 | LY_ERR rc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5809 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5810 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5811 | return LY_SUCCESS; |
| 5812 | } |
| 5813 | |
| 5814 | if (set->type != LYXP_SET_SCNODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 5815 | 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] | 5816 | return LY_EVALID; |
| 5817 | } |
| 5818 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5819 | orig_used = set->used; |
| 5820 | for (i = 0; i < orig_used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 5821 | if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) { |
| 5822 | if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5823 | continue; |
| 5824 | } |
| 5825 | |
| 5826 | /* remember context node */ |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 5827 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED; |
Michal Vasko | ec4df48 | 2019-12-16 10:02:18 +0100 | [diff] [blame] | 5828 | } else { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 5829 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5830 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5831 | |
| 5832 | /* TREE DFS */ |
| 5833 | start = set->val.scnodes[i].scnode; |
| 5834 | for (elem = next = start; elem; elem = next) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5835 | if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) { |
| 5836 | /* schema-only nodes, skip root */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5837 | goto next_iter; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5838 | } |
| 5839 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5840 | rc = moveto_scnode_check(elem, start, set, ncname, moveto_mod); |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5841 | if (!rc) { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 5842 | uint32_t idx; |
| 5843 | |
| 5844 | if (lyxp_set_scnode_contains(set, elem, LYXP_NODE_ELEM, i, &idx)) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 5845 | set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5846 | if ((uint32_t)idx > i) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5847 | /* we will process it later in the set */ |
| 5848 | goto skip_children; |
| 5849 | } |
| 5850 | } else { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 5851 | 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] | 5852 | } |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5853 | } else if (rc == LY_EINVAL) { |
| 5854 | goto skip_children; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5855 | } |
| 5856 | |
| 5857 | next_iter: |
| 5858 | /* TREE DFS NEXT ELEM */ |
| 5859 | /* select element for the next run - children first */ |
Michal Vasko | 544e58a | 2021-01-28 14:33:41 +0100 | [diff] [blame] | 5860 | next = lysc_node_child(elem); |
| 5861 | if (next && (next->nodetype == LYS_INPUT) && (options & LYXP_SCNODE_OUTPUT)) { |
| 5862 | next = next->next; |
| 5863 | } else if (next && (next->nodetype == LYS_OUTPUT) && !(options & LYXP_SCNODE_OUTPUT)) { |
| 5864 | next = next->next; |
| 5865 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5866 | if (!next) { |
| 5867 | skip_children: |
| 5868 | /* no children, so try siblings, but only if it's not the start, |
| 5869 | * that is considered to be the root and it's siblings are not traversed */ |
| 5870 | if (elem != start) { |
| 5871 | next = elem->next; |
| 5872 | } else { |
| 5873 | break; |
| 5874 | } |
| 5875 | } |
| 5876 | while (!next) { |
| 5877 | /* no siblings, go back through the parents */ |
| 5878 | if (elem->parent == start) { |
| 5879 | /* we are done, no next element to process */ |
| 5880 | break; |
| 5881 | } |
| 5882 | /* parent is already processed, go to its sibling */ |
| 5883 | elem = elem->parent; |
| 5884 | next = elem->next; |
| 5885 | } |
| 5886 | } |
| 5887 | } |
| 5888 | |
| 5889 | return LY_SUCCESS; |
| 5890 | } |
| 5891 | |
| 5892 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5893 | * @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] | 5894 | * Indirectly context position aware. |
| 5895 | * |
| 5896 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5897 | * @param[in] mod Matching metadata module, NULL for any. |
| 5898 | * @param[in] ncname Matching metadata name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5899 | * @return LY_ERR |
| 5900 | */ |
| 5901 | static LY_ERR |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5902 | moveto_attr(struct lyxp_set *set, const struct lys_module *mod, const char *ncname) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5903 | { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5904 | struct lyd_meta *sub; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5905 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5906 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5907 | return LY_SUCCESS; |
| 5908 | } |
| 5909 | |
| 5910 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 5911 | 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] | 5912 | return LY_EVALID; |
| 5913 | } |
| 5914 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5915 | for (uint32_t i = 0; i < set->used; ) { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 5916 | ly_bool replaced = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5917 | |
| 5918 | /* only attributes of an elem (not dummy) can be in the result, skip all the rest; |
| 5919 | * our attributes are always qualified */ |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5920 | if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5921 | for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5922 | |
| 5923 | /* check "namespace" */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5924 | if (mod && (sub->annotation->module != mod)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5925 | continue; |
| 5926 | } |
| 5927 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5928 | if (!ncname || (sub->name == ncname)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5929 | /* match */ |
| 5930 | if (!replaced) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5931 | set->val.meta[i].meta = sub; |
| 5932 | set->val.meta[i].type = LYXP_NODE_META; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5933 | /* pos does not change */ |
| 5934 | replaced = 1; |
| 5935 | } else { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5936 | 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] | 5937 | } |
| 5938 | ++i; |
| 5939 | } |
| 5940 | } |
| 5941 | } |
| 5942 | |
| 5943 | if (!replaced) { |
| 5944 | /* no match */ |
| 5945 | set_remove_node(set, i); |
| 5946 | } |
| 5947 | } |
| 5948 | |
| 5949 | return LY_SUCCESS; |
| 5950 | } |
| 5951 | |
| 5952 | /** |
| 5953 | * @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] | 5954 | * Result is LYXP_SET_NODE_SET. Context position aware. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5955 | * |
| 5956 | * @param[in,out] set1 Set to use for the result. |
| 5957 | * @param[in] set2 Set that is copied to @p set1. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5958 | * @return LY_ERR |
| 5959 | */ |
| 5960 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5961 | moveto_union(struct lyxp_set *set1, struct lyxp_set *set2) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5962 | { |
| 5963 | LY_ERR rc; |
| 5964 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5965 | 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] | 5966 | 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] | 5967 | return LY_EVALID; |
| 5968 | } |
| 5969 | |
| 5970 | /* set2 is empty or both set1 and set2 */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5971 | if (!set2->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5972 | return LY_SUCCESS; |
| 5973 | } |
| 5974 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5975 | if (!set1->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5976 | memcpy(set1, set2, sizeof *set1); |
| 5977 | /* dynamic memory belongs to set1 now, do not free */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5978 | memset(set2, 0, sizeof *set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5979 | return LY_SUCCESS; |
| 5980 | } |
| 5981 | |
| 5982 | /* we assume sets are sorted */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5983 | assert(!set_sort(set1) && !set_sort(set2)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5984 | |
| 5985 | /* sort, remove duplicates */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5986 | rc = set_sorted_merge(set1, set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5987 | LY_CHECK_RET(rc); |
| 5988 | |
| 5989 | /* final set must be sorted */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5990 | assert(!set_sort(set1)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5991 | |
| 5992 | return LY_SUCCESS; |
| 5993 | } |
| 5994 | |
| 5995 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5996 | * @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] | 5997 | * Context position aware. |
| 5998 | * |
| 5999 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6000 | * @param[in] mod Matching metadata module, NULL for any. |
| 6001 | * @param[in] ncname Matching metadata name in the dictionary, NULL for any. |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 6002 | * @param[in] options XPath options. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6003 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6004 | */ |
| 6005 | static int |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 6006 | 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] | 6007 | { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6008 | struct lyd_meta *sub; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6009 | struct lyxp_set *set_all_desc = NULL; |
| 6010 | LY_ERR rc; |
| 6011 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6012 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6013 | return LY_SUCCESS; |
| 6014 | } |
| 6015 | |
| 6016 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 6017 | 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] | 6018 | return LY_EVALID; |
| 6019 | } |
| 6020 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6021 | /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory, |
| 6022 | * but it likely won't be used much, so it's a waste of time */ |
| 6023 | /* copy the context */ |
| 6024 | set_all_desc = set_copy(set); |
| 6025 | /* get all descendant nodes (the original context nodes are removed) */ |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 6026 | rc = moveto_node_alldesc(set_all_desc, NULL, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6027 | if (rc != LY_SUCCESS) { |
| 6028 | lyxp_set_free(set_all_desc); |
| 6029 | return rc; |
| 6030 | } |
| 6031 | /* prepend the original context nodes */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6032 | rc = moveto_union(set, set_all_desc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6033 | if (rc != LY_SUCCESS) { |
| 6034 | lyxp_set_free(set_all_desc); |
| 6035 | return rc; |
| 6036 | } |
| 6037 | lyxp_set_free(set_all_desc); |
| 6038 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6039 | for (uint32_t i = 0; i < set->used; ) { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6040 | ly_bool replaced = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6041 | |
| 6042 | /* only attributes of an elem can be in the result, skip all the rest, |
| 6043 | * we have all attributes qualified in lyd tree */ |
| 6044 | if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6045 | for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6046 | /* check "namespace" */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6047 | if (mod && (sub->annotation->module != mod)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6048 | continue; |
| 6049 | } |
| 6050 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6051 | if (!ncname || (sub->name == ncname)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6052 | /* match */ |
| 6053 | if (!replaced) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6054 | set->val.meta[i].meta = sub; |
| 6055 | set->val.meta[i].type = LYXP_NODE_META; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6056 | /* pos does not change */ |
| 6057 | replaced = 1; |
| 6058 | } else { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6059 | 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] | 6060 | } |
| 6061 | ++i; |
| 6062 | } |
| 6063 | } |
| 6064 | } |
| 6065 | |
| 6066 | if (!replaced) { |
| 6067 | /* no match */ |
| 6068 | set_remove_node(set, i); |
| 6069 | } |
| 6070 | } |
| 6071 | |
| 6072 | return LY_SUCCESS; |
| 6073 | } |
| 6074 | |
| 6075 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6076 | * @brief Move context @p set to self and al chilren, recursively. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET. |
| 6077 | * Context position aware. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6078 | * |
| 6079 | * @param[in] parent Current parent. |
| 6080 | * @param[in] parent_pos Position of @p parent. |
| 6081 | * @param[in] parent_type Node type of @p parent. |
| 6082 | * @param[in,out] to_set Set to use. |
| 6083 | * @param[in] dup_check_set Set for checking duplicities. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6084 | * @param[in] options XPath options. |
| 6085 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6086 | */ |
| 6087 | static LY_ERR |
| 6088 | 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] | 6089 | 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] | 6090 | { |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6091 | const struct lyd_node *iter, *first; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6092 | LY_ERR rc; |
| 6093 | |
| 6094 | switch (parent_type) { |
| 6095 | case LYXP_NODE_ROOT: |
| 6096 | case LYXP_NODE_ROOT_CONFIG: |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6097 | assert(!parent); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6098 | |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6099 | /* add all top-level nodes as elements */ |
| 6100 | first = to_set->tree; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6101 | break; |
| 6102 | case LYXP_NODE_ELEM: |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6103 | /* add just the text node of this term element node */ |
| 6104 | if (parent->schema->nodetype & (LYD_NODE_TERM | LYD_NODE_ANY)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6105 | if (!set_dup_node_check(dup_check_set, parent, LYXP_NODE_TEXT, -1)) { |
| 6106 | set_insert_node(to_set, parent, parent_pos, LYXP_NODE_TEXT, to_set->used); |
| 6107 | } |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6108 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6109 | } |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6110 | |
| 6111 | /* add all the children of this node */ |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 6112 | first = lyd_child(parent); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6113 | break; |
| 6114 | default: |
| 6115 | LOGINT_RET(parent->schema->module->ctx); |
| 6116 | } |
| 6117 | |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6118 | /* add all top-level nodes as elements */ |
| 6119 | LY_LIST_FOR(first, iter) { |
| 6120 | /* context check */ |
| 6121 | if ((parent_type == LYXP_NODE_ROOT_CONFIG) && (iter->schema->flags & LYS_CONFIG_R)) { |
| 6122 | continue; |
| 6123 | } |
| 6124 | |
| 6125 | /* when check */ |
Michal Vasko | d5cfa6e | 2020-11-23 16:56:08 +0100 | [diff] [blame] | 6126 | 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] | 6127 | return LY_EINCOMPLETE; |
| 6128 | } |
| 6129 | |
| 6130 | if (!set_dup_node_check(dup_check_set, iter, LYXP_NODE_ELEM, -1)) { |
| 6131 | set_insert_node(to_set, iter, 0, LYXP_NODE_ELEM, to_set->used); |
| 6132 | |
| 6133 | /* also add all the children of this node, recursively */ |
| 6134 | rc = moveto_self_add_children_r(iter, 0, LYXP_NODE_ELEM, to_set, dup_check_set, options); |
| 6135 | LY_CHECK_RET(rc); |
| 6136 | } |
| 6137 | } |
| 6138 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6139 | return LY_SUCCESS; |
| 6140 | } |
| 6141 | |
| 6142 | /** |
| 6143 | * @brief Move context @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET |
| 6144 | * (or LYXP_SET_EMPTY). Context position aware. |
| 6145 | * |
| 6146 | * @param[in,out] set Set to use. |
| 6147 | * @param[in] all_desc Whether to go to all descendants ('//') or not ('/'). |
| 6148 | * @param[in] options XPath options. |
| 6149 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6150 | */ |
| 6151 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6152 | 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] | 6153 | { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6154 | struct lyxp_set ret_set; |
| 6155 | LY_ERR rc; |
| 6156 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6157 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6158 | return LY_SUCCESS; |
| 6159 | } |
| 6160 | |
| 6161 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 6162 | 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] | 6163 | return LY_EVALID; |
| 6164 | } |
| 6165 | |
| 6166 | /* nothing to do */ |
| 6167 | if (!all_desc) { |
| 6168 | return LY_SUCCESS; |
| 6169 | } |
| 6170 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6171 | /* add all the children, they get added recursively */ |
| 6172 | set_init(&ret_set, set); |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6173 | for (uint32_t i = 0; i < set->used; ++i) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6174 | /* copy the current node to tmp */ |
| 6175 | set_insert_node(&ret_set, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, ret_set.used); |
| 6176 | |
| 6177 | /* do not touch attributes and text nodes */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6178 | 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] | 6179 | continue; |
| 6180 | } |
| 6181 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6182 | /* add all the children */ |
| 6183 | 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] | 6184 | set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6185 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6186 | lyxp_set_free_content(&ret_set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6187 | return rc; |
| 6188 | } |
| 6189 | } |
| 6190 | |
| 6191 | /* use the temporary set as the current one */ |
| 6192 | ret_set.ctx_pos = set->ctx_pos; |
| 6193 | ret_set.ctx_size = set->ctx_size; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6194 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6195 | memcpy(set, &ret_set, sizeof *set); |
| 6196 | |
| 6197 | return LY_SUCCESS; |
| 6198 | } |
| 6199 | |
| 6200 | /** |
| 6201 | * @brief Move context schema @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_SCNODE_SET |
| 6202 | * (or LYXP_SET_EMPTY). |
| 6203 | * |
| 6204 | * @param[in,out] set Set to use. |
| 6205 | * @param[in] all_desc Whether to go to all descendants ('//') or not ('/'). |
| 6206 | * @param[in] options XPath options. |
| 6207 | * @return LY_ERR |
| 6208 | */ |
| 6209 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6210 | 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] | 6211 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6212 | uint32_t getnext_opts; |
| 6213 | uint32_t mod_idx; |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6214 | const struct lysc_node *iter, *start_parent; |
| 6215 | const struct lys_module *mod; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6216 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6217 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6218 | return LY_SUCCESS; |
| 6219 | } |
| 6220 | |
| 6221 | if (set->type != LYXP_SET_SCNODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 6222 | 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] | 6223 | return LY_EVALID; |
| 6224 | } |
| 6225 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6226 | /* getnext opts */ |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 6227 | getnext_opts = 0; |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6228 | if (options & LYXP_SCNODE_OUTPUT) { |
| 6229 | getnext_opts |= LYS_GETNEXT_OUTPUT; |
| 6230 | } |
| 6231 | |
| 6232 | /* 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] | 6233 | for (uint32_t i = 0; i < set->used; ++i) { |
Michal Vasko | e657f96 | 2020-12-10 12:19:48 +0100 | [diff] [blame] | 6234 | if (!all_desc) { |
| 6235 | /* traverse the start node */ |
| 6236 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START) { |
| 6237 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
| 6238 | } |
| 6239 | continue; |
| 6240 | } |
| 6241 | |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 6242 | if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) { |
| 6243 | if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6244 | continue; |
| 6245 | } |
| 6246 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6247 | /* remember context node */ |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 6248 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED; |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6249 | } else { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 6250 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6251 | } |
| 6252 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6253 | start_parent = set->val.scnodes[i].scnode; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6254 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6255 | if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) { |
| 6256 | /* it can actually be in any module, it's all <running> */ |
| 6257 | mod_idx = 0; |
| 6258 | while ((mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) { |
| 6259 | iter = NULL; |
| 6260 | /* module may not be implemented */ |
| 6261 | while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) { |
| 6262 | /* context check */ |
| 6263 | if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) { |
| 6264 | continue; |
| 6265 | } |
| 6266 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 6267 | 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] | 6268 | /* throw away the insert index, we want to consider that node again, recursively */ |
| 6269 | } |
| 6270 | } |
| 6271 | |
| 6272 | } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) { |
| 6273 | iter = NULL; |
| 6274 | while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6275 | /* context check */ |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6276 | 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] | 6277 | continue; |
| 6278 | } |
| 6279 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 6280 | 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] | 6281 | } |
| 6282 | } |
| 6283 | } |
| 6284 | |
| 6285 | return LY_SUCCESS; |
| 6286 | } |
| 6287 | |
| 6288 | /** |
| 6289 | * @brief Move context @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_NODE_SET |
| 6290 | * (or LYXP_SET_EMPTY). Context position aware. |
| 6291 | * |
| 6292 | * @param[in] set Set to use. |
| 6293 | * @param[in] all_desc Whether to go to all descendants ('//') or not ('/'). |
| 6294 | * @param[in] options XPath options. |
| 6295 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6296 | */ |
| 6297 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6298 | 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] | 6299 | { |
| 6300 | LY_ERR rc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6301 | struct lyd_node *node, *new_node; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6302 | enum lyxp_node_type new_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6303 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6304 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6305 | return LY_SUCCESS; |
| 6306 | } |
| 6307 | |
| 6308 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 6309 | 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] | 6310 | return LY_EVALID; |
| 6311 | } |
| 6312 | |
| 6313 | if (all_desc) { |
| 6314 | /* <path>//.. == <path>//./.. */ |
| 6315 | rc = moveto_self(set, 1, options); |
| 6316 | LY_CHECK_RET(rc); |
| 6317 | } |
| 6318 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6319 | for (uint32_t i = 0; i < set->used; ++i) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6320 | node = set->val.nodes[i].node; |
| 6321 | |
| 6322 | if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 6323 | new_node = lyd_parent(node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6324 | } else if (set->val.nodes[i].type == LYXP_NODE_TEXT) { |
| 6325 | new_node = node; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6326 | } else if (set->val.nodes[i].type == LYXP_NODE_META) { |
| 6327 | new_node = set->val.meta[i].meta->parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6328 | if (!new_node) { |
| 6329 | LOGINT_RET(set->ctx); |
| 6330 | } |
| 6331 | } else { |
| 6332 | /* root does not have a parent */ |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6333 | set_remove_node_none(set, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6334 | continue; |
| 6335 | } |
| 6336 | |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 6337 | /* when check */ |
Michal Vasko | d5cfa6e | 2020-11-23 16:56:08 +0100 | [diff] [blame] | 6338 | if (!(options & LYXP_IGNORE_WHEN) && new_node && lysc_has_when(new_node->schema) && !(new_node->flags & LYD_WHEN_TRUE)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6339 | return LY_EINCOMPLETE; |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 6340 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6341 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6342 | if (!new_node) { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 6343 | /* node already there can also be the root */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6344 | new_type = set->root_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6345 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6346 | } else { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 6347 | /* 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] | 6348 | new_type = LYXP_NODE_ELEM; |
| 6349 | } |
| 6350 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6351 | if (set_dup_node_check(set, new_node, new_type, -1)) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6352 | set_remove_node_none(set, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6353 | } else { |
| 6354 | set_replace_node(set, new_node, 0, new_type, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6355 | } |
| 6356 | } |
| 6357 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6358 | set_remove_nodes_none(set); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6359 | assert(!set_sort(set) && !set_sorted_dup_node_clean(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6360 | |
| 6361 | return LY_SUCCESS; |
| 6362 | } |
| 6363 | |
| 6364 | /** |
| 6365 | * @brief Move context schema @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_SCNODE_SET |
| 6366 | * (or LYXP_SET_EMPTY). |
| 6367 | * |
| 6368 | * @param[in] set Set to use. |
| 6369 | * @param[in] all_desc Whether to go to all descendants ('//') or not ('/'). |
| 6370 | * @param[in] options XPath options. |
| 6371 | * @return LY_ERR |
| 6372 | */ |
| 6373 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6374 | 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] | 6375 | { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 6376 | uint32_t i, orig_used, idx; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6377 | ly_bool temp_ctx = 0; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6378 | const struct lysc_node *node, *new_node; |
| 6379 | enum lyxp_node_type new_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6380 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6381 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6382 | return LY_SUCCESS; |
| 6383 | } |
| 6384 | |
| 6385 | if (set->type != LYXP_SET_SCNODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 6386 | LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6387 | return LY_EVALID; |
| 6388 | } |
| 6389 | |
| 6390 | if (all_desc) { |
| 6391 | /* <path>//.. == <path>//./.. */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6392 | LY_CHECK_RET(moveto_scnode_self(set, 1, options)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6393 | } |
| 6394 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6395 | orig_used = set->used; |
| 6396 | for (i = 0; i < orig_used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 6397 | if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) { |
| 6398 | if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6399 | continue; |
| 6400 | } |
| 6401 | |
| 6402 | /* remember context node */ |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 6403 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED; |
Michal Vasko | ec4df48 | 2019-12-16 10:02:18 +0100 | [diff] [blame] | 6404 | } else { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 6405 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6406 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6407 | |
| 6408 | node = set->val.scnodes[i].scnode; |
| 6409 | |
| 6410 | if (set->val.scnodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6411 | new_node = lysc_data_parent(node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6412 | } else { |
| 6413 | /* root does not have a parent */ |
| 6414 | continue; |
| 6415 | } |
| 6416 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6417 | if (!new_node) { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 6418 | /* node has no parent */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6419 | new_type = set->root_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6420 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6421 | } else { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 6422 | /* 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] | 6423 | new_type = LYXP_NODE_ELEM; |
| 6424 | } |
| 6425 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 6426 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, new_node, new_type, &idx)); |
| 6427 | if ((idx < orig_used) && (idx > i)) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 6428 | set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6429 | temp_ctx = 1; |
| 6430 | } |
| 6431 | } |
| 6432 | |
| 6433 | if (temp_ctx) { |
| 6434 | for (i = 0; i < orig_used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 6435 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NEW_CTX) { |
| 6436 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6437 | } |
| 6438 | } |
| 6439 | } |
| 6440 | |
| 6441 | return LY_SUCCESS; |
| 6442 | } |
| 6443 | |
| 6444 | /** |
| 6445 | * @brief Move context @p set to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'. |
| 6446 | * Result is LYXP_SET_BOOLEAN. Indirectly context position aware. |
| 6447 | * |
| 6448 | * @param[in,out] set1 Set to use for the result. |
| 6449 | * @param[in] set2 Set acting as the second operand for @p op. |
| 6450 | * @param[in] op Comparison operator to process. |
| 6451 | * @param[in] options XPath options. |
| 6452 | * @return LY_ERR |
| 6453 | */ |
| 6454 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6455 | 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] | 6456 | { |
| 6457 | /* |
| 6458 | * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING |
| 6459 | * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING) |
| 6460 | * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 6461 | * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 6462 | * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING |
| 6463 | * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6464 | * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 6465 | * |
| 6466 | * '=' or '!=' |
| 6467 | * BOOLEAN + BOOLEAN |
| 6468 | * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 6469 | * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 6470 | * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 6471 | * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 6472 | * NUMBER + NUMBER |
| 6473 | * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6474 | * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 6475 | * STRING + STRING |
| 6476 | * |
| 6477 | * '<=', '<', '>=', '>' |
| 6478 | * NUMBER + NUMBER |
| 6479 | * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 6480 | * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 6481 | * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 6482 | * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6483 | * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 6484 | * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 6485 | * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6486 | * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6487 | */ |
| 6488 | struct lyxp_set iter1, iter2; |
| 6489 | int result; |
| 6490 | int64_t i; |
| 6491 | LY_ERR rc; |
| 6492 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6493 | memset(&iter1, 0, sizeof iter1); |
| 6494 | memset(&iter2, 0, sizeof iter2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6495 | |
| 6496 | /* iterative evaluation with node-sets */ |
| 6497 | if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) { |
| 6498 | if (set1->type == LYXP_SET_NODE_SET) { |
| 6499 | for (i = 0; i < set1->used; ++i) { |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6500 | /* cast set1 */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6501 | switch (set2->type) { |
| 6502 | case LYXP_SET_NUMBER: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6503 | rc = set_comp_cast(&iter1, set1, LYXP_SET_NUMBER, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6504 | break; |
| 6505 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6506 | rc = set_comp_cast(&iter1, set1, LYXP_SET_BOOLEAN, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6507 | break; |
| 6508 | default: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6509 | rc = set_comp_cast(&iter1, set1, LYXP_SET_STRING, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6510 | break; |
| 6511 | } |
| 6512 | LY_CHECK_RET(rc); |
| 6513 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6514 | /* canonize set2 */ |
| 6515 | LY_CHECK_ERR_RET(rc = set_comp_canonize(&iter2, set2, &set1->val.nodes[i]), lyxp_set_free_content(&iter1), rc); |
| 6516 | |
| 6517 | /* compare recursively */ |
| 6518 | rc = moveto_op_comp(&iter1, &iter2, op, options); |
| 6519 | lyxp_set_free_content(&iter2); |
| 6520 | LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6521 | |
| 6522 | /* lazy evaluation until true */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6523 | if (iter1.val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6524 | set_fill_boolean(set1, 1); |
| 6525 | return LY_SUCCESS; |
| 6526 | } |
| 6527 | } |
| 6528 | } else { |
| 6529 | for (i = 0; i < set2->used; ++i) { |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6530 | /* set set2 */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6531 | switch (set1->type) { |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6532 | case LYXP_SET_NUMBER: |
| 6533 | rc = set_comp_cast(&iter2, set2, LYXP_SET_NUMBER, i); |
| 6534 | break; |
| 6535 | case LYXP_SET_BOOLEAN: |
| 6536 | rc = set_comp_cast(&iter2, set2, LYXP_SET_BOOLEAN, i); |
| 6537 | break; |
| 6538 | default: |
| 6539 | rc = set_comp_cast(&iter2, set2, LYXP_SET_STRING, i); |
| 6540 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6541 | } |
| 6542 | LY_CHECK_RET(rc); |
| 6543 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6544 | /* canonize set1 */ |
| 6545 | 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] | 6546 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6547 | /* compare recursively */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6548 | rc = moveto_op_comp(&iter1, &iter2, op, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6549 | lyxp_set_free_content(&iter2); |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6550 | LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6551 | |
| 6552 | /* lazy evaluation until true */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6553 | if (iter1.val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6554 | set_fill_boolean(set1, 1); |
| 6555 | return LY_SUCCESS; |
| 6556 | } |
| 6557 | } |
| 6558 | } |
| 6559 | |
| 6560 | /* false for all nodes */ |
| 6561 | set_fill_boolean(set1, 0); |
| 6562 | return LY_SUCCESS; |
| 6563 | } |
| 6564 | |
| 6565 | /* first convert properly */ |
| 6566 | if ((op[0] == '=') || (op[0] == '!')) { |
| 6567 | if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6568 | lyxp_set_cast(set1, LYXP_SET_BOOLEAN); |
| 6569 | lyxp_set_cast(set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6570 | } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6571 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6572 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6573 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6574 | LY_CHECK_RET(rc); |
| 6575 | } /* else we have 2 strings */ |
| 6576 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6577 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6578 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6579 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6580 | LY_CHECK_RET(rc); |
| 6581 | } |
| 6582 | |
| 6583 | assert(set1->type == set2->type); |
| 6584 | |
| 6585 | /* compute result */ |
| 6586 | if (op[0] == '=') { |
| 6587 | if (set1->type == LYXP_SET_BOOLEAN) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6588 | result = (set1->val.bln == set2->val.bln); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6589 | } else if (set1->type == LYXP_SET_NUMBER) { |
| 6590 | result = (set1->val.num == set2->val.num); |
| 6591 | } else { |
| 6592 | assert(set1->type == LYXP_SET_STRING); |
Michal Vasko | ac6c72f | 2019-11-14 16:09:34 +0100 | [diff] [blame] | 6593 | result = !strcmp(set1->val.str, set2->val.str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6594 | } |
| 6595 | } else if (op[0] == '!') { |
| 6596 | if (set1->type == LYXP_SET_BOOLEAN) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6597 | result = (set1->val.bln != set2->val.bln); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6598 | } else if (set1->type == LYXP_SET_NUMBER) { |
| 6599 | result = (set1->val.num != set2->val.num); |
| 6600 | } else { |
| 6601 | assert(set1->type == LYXP_SET_STRING); |
Michal Vasko | c205843 | 2020-11-06 17:26:21 +0100 | [diff] [blame] | 6602 | result = strcmp(set1->val.str, set2->val.str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6603 | } |
| 6604 | } else { |
| 6605 | assert(set1->type == LYXP_SET_NUMBER); |
| 6606 | if (op[0] == '<') { |
| 6607 | if (op[1] == '=') { |
| 6608 | result = (set1->val.num <= set2->val.num); |
| 6609 | } else { |
| 6610 | result = (set1->val.num < set2->val.num); |
| 6611 | } |
| 6612 | } else { |
| 6613 | if (op[1] == '=') { |
| 6614 | result = (set1->val.num >= set2->val.num); |
| 6615 | } else { |
| 6616 | result = (set1->val.num > set2->val.num); |
| 6617 | } |
| 6618 | } |
| 6619 | } |
| 6620 | |
| 6621 | /* assign result */ |
| 6622 | if (result) { |
| 6623 | set_fill_boolean(set1, 1); |
| 6624 | } else { |
| 6625 | set_fill_boolean(set1, 0); |
| 6626 | } |
| 6627 | |
| 6628 | return LY_SUCCESS; |
| 6629 | } |
| 6630 | |
| 6631 | /** |
| 6632 | * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div', |
| 6633 | * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware. |
| 6634 | * |
| 6635 | * @param[in,out] set1 Set to use for the result. |
| 6636 | * @param[in] set2 Set acting as the second operand for @p op. |
| 6637 | * @param[in] op Operator to process. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6638 | * @return LY_ERR |
| 6639 | */ |
| 6640 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6641 | 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] | 6642 | { |
| 6643 | LY_ERR rc; |
| 6644 | |
| 6645 | /* unary '-' */ |
| 6646 | if (!set2 && (op[0] == '-')) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6647 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6648 | LY_CHECK_RET(rc); |
| 6649 | set1->val.num *= -1; |
| 6650 | lyxp_set_free(set2); |
| 6651 | return LY_SUCCESS; |
| 6652 | } |
| 6653 | |
| 6654 | assert(set1 && set2); |
| 6655 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6656 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6657 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6658 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6659 | LY_CHECK_RET(rc); |
| 6660 | |
| 6661 | switch (op[0]) { |
| 6662 | /* '+' */ |
| 6663 | case '+': |
| 6664 | set1->val.num += set2->val.num; |
| 6665 | break; |
| 6666 | |
| 6667 | /* '-' */ |
| 6668 | case '-': |
| 6669 | set1->val.num -= set2->val.num; |
| 6670 | break; |
| 6671 | |
| 6672 | /* '*' */ |
| 6673 | case '*': |
| 6674 | set1->val.num *= set2->val.num; |
| 6675 | break; |
| 6676 | |
| 6677 | /* 'div' */ |
| 6678 | case 'd': |
| 6679 | set1->val.num /= set2->val.num; |
| 6680 | break; |
| 6681 | |
| 6682 | /* 'mod' */ |
| 6683 | case 'm': |
| 6684 | set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num); |
| 6685 | break; |
| 6686 | |
| 6687 | default: |
| 6688 | LOGINT_RET(set1->ctx); |
| 6689 | } |
| 6690 | |
| 6691 | return LY_SUCCESS; |
| 6692 | } |
| 6693 | |
| 6694 | /* |
| 6695 | * eval functions |
| 6696 | * |
| 6697 | * They execute a parsed XPath expression on some data subtree. |
| 6698 | */ |
| 6699 | |
| 6700 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6701 | * @brief Evaluate Predicate. Logs directly on error. |
| 6702 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6703 | * [9] Predicate ::= '[' Expr ']' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6704 | * |
| 6705 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6706 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6707 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 6708 | * @param[in] options XPath options. |
| 6709 | * @param[in] parent_pos_pred Whether parent predicate was a positional one. |
| 6710 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6711 | */ |
| 6712 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 6713 | 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] | 6714 | { |
| 6715 | LY_ERR rc; |
Michal Vasko | 1fdd8fa | 2021-01-08 09:21:45 +0100 | [diff] [blame] | 6716 | uint16_t orig_exp; |
| 6717 | uint32_t i, orig_pos, orig_size; |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6718 | int32_t pred_in_ctx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6719 | struct lyxp_set set2; |
| 6720 | struct lyd_node *orig_parent; |
| 6721 | |
| 6722 | /* '[' */ |
| 6723 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 6724 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6725 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6726 | |
| 6727 | if (!set) { |
| 6728 | only_parse: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6729 | rc = eval_expr_select(exp, tok_idx, 0, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6730 | LY_CHECK_RET(rc); |
| 6731 | } else if (set->type == LYXP_SET_NODE_SET) { |
| 6732 | /* 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] | 6733 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6734 | |
| 6735 | /* empty set, nothing to evaluate */ |
| 6736 | if (!set->used) { |
| 6737 | goto only_parse; |
| 6738 | } |
| 6739 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6740 | orig_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6741 | orig_pos = 0; |
| 6742 | orig_size = set->used; |
| 6743 | orig_parent = NULL; |
Michal Vasko | 39dbf35 | 2020-05-21 10:08:59 +0200 | [diff] [blame] | 6744 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6745 | set_init(&set2, set); |
| 6746 | set_insert_node(&set2, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, 0); |
| 6747 | /* remember the node context position for position() and context size for last(), |
| 6748 | * predicates should always be evaluated with respect to the child axis (since we do |
| 6749 | * not support explicit axes) so we assign positions based on their parents */ |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 6750 | if (parent_pos_pred && (lyd_parent(set->val.nodes[i].node) != orig_parent)) { |
| 6751 | orig_parent = lyd_parent(set->val.nodes[i].node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6752 | orig_pos = 1; |
| 6753 | } else { |
| 6754 | ++orig_pos; |
| 6755 | } |
| 6756 | |
| 6757 | set2.ctx_pos = orig_pos; |
| 6758 | set2.ctx_size = orig_size; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6759 | *tok_idx = orig_exp; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6760 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6761 | rc = eval_expr_select(exp, tok_idx, 0, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6762 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6763 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6764 | return rc; |
| 6765 | } |
| 6766 | |
| 6767 | /* number is a position */ |
| 6768 | if (set2.type == LYXP_SET_NUMBER) { |
| 6769 | if ((long long)set2.val.num == orig_pos) { |
| 6770 | set2.val.num = 1; |
| 6771 | } else { |
| 6772 | set2.val.num = 0; |
| 6773 | } |
| 6774 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6775 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6776 | |
| 6777 | /* predicate satisfied or not? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6778 | if (!set2.val.bln) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6779 | set_remove_node_none(set, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6780 | } |
| 6781 | } |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6782 | set_remove_nodes_none(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6783 | |
| 6784 | } else if (set->type == LYXP_SET_SCNODE_SET) { |
| 6785 | for (i = 0; i < set->used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 6786 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6787 | /* there is a currently-valid node */ |
| 6788 | break; |
| 6789 | } |
| 6790 | } |
| 6791 | /* empty set, nothing to evaluate */ |
| 6792 | if (i == set->used) { |
| 6793 | goto only_parse; |
| 6794 | } |
| 6795 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6796 | orig_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6797 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6798 | /* set special in_ctx to all the valid snodes */ |
| 6799 | pred_in_ctx = set_scnode_new_in_ctx(set); |
| 6800 | |
| 6801 | /* use the valid snodes one-by-one */ |
| 6802 | for (i = 0; i < set->used; ++i) { |
| 6803 | if (set->val.scnodes[i].in_ctx != pred_in_ctx) { |
| 6804 | continue; |
| 6805 | } |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 6806 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6807 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6808 | *tok_idx = orig_exp; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6809 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6810 | rc = eval_expr_select(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6811 | LY_CHECK_RET(rc); |
| 6812 | |
| 6813 | set->val.scnodes[i].in_ctx = pred_in_ctx; |
| 6814 | } |
| 6815 | |
| 6816 | /* restore the state as it was before the predicate */ |
| 6817 | for (i = 0; i < set->used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 6818 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
| 6819 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6820 | } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 6821 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6822 | } |
| 6823 | } |
| 6824 | |
| 6825 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6826 | set2.type = LYXP_SET_NODE_SET; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6827 | set_fill_set(&set2, set); |
| 6828 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6829 | rc = eval_expr_select(exp, tok_idx, 0, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6830 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6831 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6832 | return rc; |
| 6833 | } |
| 6834 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6835 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6836 | if (!set2.val.bln) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6837 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6838 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6839 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6840 | } |
| 6841 | |
| 6842 | /* ']' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6843 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6844 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 6845 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6846 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6847 | |
| 6848 | return LY_SUCCESS; |
| 6849 | } |
| 6850 | |
| 6851 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6852 | * @brief Evaluate Literal. Logs directly on error. |
| 6853 | * |
| 6854 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6855 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6856 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 6857 | */ |
| 6858 | static void |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 6859 | 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] | 6860 | { |
| 6861 | if (set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6862 | if (exp->tok_len[*tok_idx] == 2) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6863 | set_fill_string(set, "", 0); |
| 6864 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6865 | 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] | 6866 | } |
| 6867 | } |
| 6868 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 6869 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6870 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6871 | } |
| 6872 | |
| 6873 | /** |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6874 | * @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] | 6875 | * |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6876 | * @param[in] exp Full parsed XPath expression. |
| 6877 | * @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] | 6878 | * @param[in] ctx_node Found schema node as the context for the predicate. |
| 6879 | * @param[in] cur_mod Current module for the expression. |
| 6880 | * @param[in] cur_node Current (original context) node. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6881 | * @param[in] format Format of any prefixes in key names/values. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6882 | * @param[in] prefix_data Format-specific prefix data (see ::ly_resolve_prefix). |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6883 | * @param[out] predicates Parsed predicates. |
| 6884 | * @param[out] pred_type Type of @p predicates. |
| 6885 | * @return LY_SUCCESS on success, |
| 6886 | * @return LY_ERR on any error. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6887 | */ |
| 6888 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 6889 | eval_name_test_try_compile_predicates(const struct lyxp_expr *exp, uint16_t *tok_idx, const struct lysc_node *ctx_node, |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6890 | const struct lys_module *cur_mod, const struct lysc_node *cur_node, LY_PREFIX_FORMAT format, void *prefix_data, |
| 6891 | struct ly_path_predicate **predicates, enum ly_path_pred_type *pred_type) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6892 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6893 | LY_ERR ret = LY_SUCCESS; |
| 6894 | uint16_t key_count, e_idx, pred_idx = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6895 | const struct lysc_node *key; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6896 | size_t pred_len; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6897 | uint32_t prev_lo; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6898 | struct lyxp_expr *exp2 = NULL; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6899 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6900 | assert(ctx_node->nodetype & (LYS_LIST | LYS_LEAFLIST)); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6901 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6902 | if (ctx_node->nodetype == LYS_LIST) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6903 | /* get key count */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6904 | if (ctx_node->flags & LYS_KEYLESS) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6905 | return LY_EINVAL; |
| 6906 | } |
Michal Vasko | 544e58a | 2021-01-28 14:33:41 +0100 | [diff] [blame] | 6907 | 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] | 6908 | assert(key_count); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6909 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6910 | /* learn where the predicates end */ |
| 6911 | e_idx = *tok_idx; |
| 6912 | while (key_count) { |
| 6913 | /* '[' */ |
| 6914 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) { |
| 6915 | return LY_EINVAL; |
| 6916 | } |
| 6917 | ++e_idx; |
| 6918 | |
| 6919 | /* ']' */ |
| 6920 | while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) { |
| 6921 | ++e_idx; |
| 6922 | } |
| 6923 | ++e_idx; |
| 6924 | |
| 6925 | /* another presumably key predicate parsed */ |
| 6926 | --key_count; |
| 6927 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6928 | } else { |
| 6929 | /* learn just where this single predicate ends */ |
| 6930 | e_idx = *tok_idx; |
| 6931 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6932 | /* '[' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6933 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) { |
| 6934 | return LY_EINVAL; |
| 6935 | } |
| 6936 | ++e_idx; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6937 | |
| 6938 | /* ']' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6939 | while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) { |
| 6940 | ++e_idx; |
| 6941 | } |
| 6942 | ++e_idx; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6943 | } |
| 6944 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6945 | /* get the joined length of all the keys predicates/of the single leaf-list predicate */ |
| 6946 | pred_len = (exp->tok_pos[e_idx - 1] + exp->tok_len[e_idx - 1]) - exp->tok_pos[*tok_idx]; |
| 6947 | |
| 6948 | /* turn logging off */ |
| 6949 | prev_lo = ly_log_options(0); |
| 6950 | |
| 6951 | /* parse the predicate(s) */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6952 | LY_CHECK_GOTO(ret = ly_path_parse_predicate(ctx_node->module->ctx, ctx_node, exp->expr + exp->tok_pos[*tok_idx], |
| 6953 | pred_len, LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp2), cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6954 | |
| 6955 | /* compile */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6956 | ret = ly_path_compile_predicate(ctx_node->module->ctx, cur_node, cur_mod, ctx_node, exp2, &pred_idx, format, |
| 6957 | prefix_data, predicates, pred_type); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6958 | LY_CHECK_GOTO(ret, cleanup); |
| 6959 | |
| 6960 | /* success, the predicate must include all the needed information for hash-based search */ |
| 6961 | *tok_idx = e_idx; |
| 6962 | |
| 6963 | cleanup: |
| 6964 | ly_log_options(prev_lo); |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6965 | lyxp_expr_free(ctx_node->module->ctx, exp2); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6966 | return ret; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6967 | } |
| 6968 | |
| 6969 | /** |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6970 | * @brief Search for/check the next schema node that could be the only matching schema node meaning the |
| 6971 | * data node(s) could be found using a single hash-based search. |
| 6972 | * |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 6973 | * @param[in] ctx libyang context. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6974 | * @param[in] node Next context node to check. |
| 6975 | * @param[in] name Expected node name. |
| 6976 | * @param[in] name_len Length of @p name. |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 6977 | * @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] | 6978 | * @param[in] root_type XPath root type. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6979 | * @param[in] format Prefix format. |
| 6980 | * @param[in,out] found Previously found node, is updated. |
| 6981 | * @return LY_SUCCESS on success, |
| 6982 | * @return LY_ENOT if the whole check failed and hashes cannot be used. |
| 6983 | */ |
| 6984 | static LY_ERR |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 6985 | eval_name_test_with_predicate_get_scnode(const struct ly_ctx *ctx, const struct lyd_node *node, const char *name, |
| 6986 | uint16_t name_len, const struct lys_module *moveto_mod, enum lyxp_node_type root_type, LY_PREFIX_FORMAT format, |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6987 | const struct lysc_node **found) |
| 6988 | { |
| 6989 | const struct lysc_node *scnode; |
| 6990 | const struct lys_module *mod; |
| 6991 | uint32_t idx = 0; |
| 6992 | |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 6993 | assert((format == LY_PREF_JSON) || moveto_mod); |
| 6994 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6995 | continue_search: |
Michal Vasko | 7d1d091 | 2020-10-16 08:38:30 +0200 | [diff] [blame] | 6996 | scnode = NULL; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6997 | if (!node) { |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 6998 | if ((format == LY_PREF_JSON) && !moveto_mod) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6999 | /* search all modules for a single match */ |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7000 | while ((mod = ly_ctx_get_module_iter(ctx, &idx))) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7001 | scnode = lys_find_child(NULL, mod, name, name_len, 0, 0); |
| 7002 | if (scnode) { |
| 7003 | /* we have found a match */ |
| 7004 | break; |
| 7005 | } |
| 7006 | } |
| 7007 | |
Michal Vasko | 7d1d091 | 2020-10-16 08:38:30 +0200 | [diff] [blame] | 7008 | if (!scnode) { |
| 7009 | /* all modules searched */ |
| 7010 | idx = 0; |
| 7011 | } |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7012 | } else { |
| 7013 | /* search in top-level */ |
| 7014 | scnode = lys_find_child(NULL, moveto_mod, name, name_len, 0, 0); |
| 7015 | } |
| 7016 | } else if (!*found || (lysc_data_parent(*found) != node->schema)) { |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7017 | if ((format == LY_PREF_JSON) && !moveto_mod) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7018 | /* we must adjust the module to inherit the one from the context node */ |
| 7019 | moveto_mod = node->schema->module; |
| 7020 | } |
| 7021 | |
| 7022 | /* search in children, do not repeat the same search */ |
| 7023 | 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] | 7024 | } /* else skip redundant search */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7025 | |
| 7026 | /* additional context check */ |
| 7027 | if (scnode && (root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) { |
| 7028 | scnode = NULL; |
| 7029 | } |
| 7030 | |
| 7031 | if (scnode) { |
| 7032 | if (*found) { |
| 7033 | /* we found a schema node with the same name but at different level, give up, too complicated |
| 7034 | * (more hash-based searches would be required, not supported) */ |
| 7035 | return LY_ENOT; |
| 7036 | } else { |
| 7037 | /* remember the found schema node and continue to make sure it can be used */ |
| 7038 | *found = scnode; |
| 7039 | } |
| 7040 | } |
| 7041 | |
| 7042 | if (idx) { |
| 7043 | /* continue searching all the following models */ |
| 7044 | goto continue_search; |
| 7045 | } |
| 7046 | |
| 7047 | return LY_SUCCESS; |
| 7048 | } |
| 7049 | |
| 7050 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7051 | * @brief Evaluate NameTest and any following Predicates. Logs directly on error. |
| 7052 | * |
| 7053 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 7054 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 7055 | * [7] NameTest ::= '*' | NCName ':' '*' | QName |
| 7056 | * |
| 7057 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7058 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7059 | * @param[in] attr_axis Whether to search attributes or standard nodes. |
| 7060 | * @param[in] all_desc Whether to search all the descendants or children only. |
| 7061 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7062 | * @param[in] options XPath options. |
| 7063 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7064 | */ |
| 7065 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 7066 | 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] | 7067 | struct lyxp_set *set, uint32_t options) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7068 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7069 | char *path; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7070 | const char *ncname, *ncname_dict = NULL; |
| 7071 | uint16_t ncname_len; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7072 | const struct lys_module *moveto_mod = NULL; |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7073 | const struct lysc_node *scnode = NULL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7074 | struct ly_path_predicate *predicates = NULL; |
| 7075 | enum ly_path_pred_type pred_type = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7076 | LY_ERR rc = LY_SUCCESS; |
| 7077 | |
| 7078 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7079 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7080 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7081 | |
| 7082 | if (!set) { |
| 7083 | goto moveto; |
| 7084 | } |
| 7085 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7086 | ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]]; |
| 7087 | ncname_len = exp->tok_len[*tok_idx - 1]; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7088 | |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7089 | if ((ncname[0] == '*') && (ncname_len == 1)) { |
| 7090 | /* all nodes will match */ |
| 7091 | goto moveto; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7092 | } |
| 7093 | |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7094 | /* parse (and skip) module name */ |
| 7095 | rc = moveto_resolve_model(&ncname, &ncname_len, set, NULL, &moveto_mod); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7096 | LY_CHECK_GOTO(rc, cleanup); |
| 7097 | |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7098 | if (((set->format == LY_PREF_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] | 7099 | /* find the matching schema node in some parent in the context */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7100 | for (uint32_t i = 0; i < set->used; ++i) { |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7101 | if (eval_name_test_with_predicate_get_scnode(set->ctx, set->val.nodes[i].node, ncname, ncname_len, |
| 7102 | moveto_mod, set->root_type, set->format, &scnode)) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7103 | /* check failed */ |
| 7104 | scnode = NULL; |
| 7105 | break; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7106 | } |
| 7107 | } |
| 7108 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7109 | if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) { |
| 7110 | /* try to create the predicates */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7111 | if (eval_name_test_try_compile_predicates(exp, tok_idx, scnode, set->cur_mod, set->cur_node ? |
| 7112 | 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] | 7113 | /* hashes cannot be used */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7114 | scnode = NULL; |
| 7115 | } |
| 7116 | } |
| 7117 | } |
| 7118 | |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7119 | if (!scnode) { |
| 7120 | /* 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] | 7121 | * use dictionary for efficient comparison */ |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 7122 | 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] | 7123 | } |
| 7124 | |
| 7125 | moveto: |
| 7126 | /* move to the attribute(s), data node(s), or schema node(s) */ |
| 7127 | if (attr_axis) { |
| 7128 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 7129 | set_scnode_clear_ctx(set); |
| 7130 | } else { |
| 7131 | if (all_desc) { |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 7132 | rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7133 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7134 | rc = moveto_attr(set, moveto_mod, ncname_dict); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7135 | } |
| 7136 | LY_CHECK_GOTO(rc, cleanup); |
| 7137 | } |
| 7138 | } else { |
| 7139 | if (set && (options & LYXP_SCNODE_ALL)) { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7140 | int64_t i; |
| 7141 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7142 | if (all_desc) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7143 | rc = moveto_scnode_alldesc(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7144 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7145 | rc = moveto_scnode(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7146 | } |
| 7147 | LY_CHECK_GOTO(rc, cleanup); |
| 7148 | |
| 7149 | for (i = set->used - 1; i > -1; --i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 7150 | if (set->val.scnodes[i].in_ctx > LYXP_SET_SCNODE_ATOM) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7151 | break; |
| 7152 | } |
| 7153 | } |
| 7154 | if (i == -1) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7155 | path = lysc_path(set->cur_scnode, LYSC_PATH_LOG, NULL, 0); |
Michal Vasko | 90f12dc | 2020-12-03 14:20:42 +0100 | [diff] [blame] | 7156 | LOGWRN(set->ctx, "Schema node \"%.*s\" not found (\"%.*s\") with context node \"%s\".", |
| 7157 | ncname_len, ncname, (ncname - exp->expr) + ncname_len, exp->expr, path); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7158 | free(path); |
| 7159 | } |
| 7160 | } else { |
| 7161 | if (all_desc) { |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 7162 | rc = moveto_node_alldesc(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7163 | } else { |
| 7164 | if (scnode) { |
| 7165 | /* we can find the nodes using hashes */ |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 7166 | rc = moveto_node_hash(set, scnode, predicates, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7167 | } else { |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 7168 | rc = moveto_node(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7169 | } |
| 7170 | } |
| 7171 | LY_CHECK_GOTO(rc, cleanup); |
| 7172 | } |
| 7173 | } |
| 7174 | |
| 7175 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7176 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 7177 | rc = eval_predicate(exp, tok_idx, set, options, 1); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7178 | LY_CHECK_RET(rc); |
| 7179 | } |
| 7180 | |
| 7181 | cleanup: |
Michal Vasko | db51a8d | 2020-05-27 15:22:29 +0200 | [diff] [blame] | 7182 | if (set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7183 | lydict_remove(set->ctx, ncname_dict); |
Michal Vasko | f7e16e2 | 2020-10-21 09:24:39 +0200 | [diff] [blame] | 7184 | ly_path_predicates_free(set->ctx, pred_type, predicates); |
Michal Vasko | db51a8d | 2020-05-27 15:22:29 +0200 | [diff] [blame] | 7185 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7186 | return rc; |
| 7187 | } |
| 7188 | |
| 7189 | /** |
| 7190 | * @brief Evaluate NodeType and any following Predicates. Logs directly on error. |
| 7191 | * |
| 7192 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 7193 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 7194 | * [8] NodeType ::= 'text' | 'node' |
| 7195 | * |
| 7196 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7197 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7198 | * @param[in] attr_axis Whether to search attributes or standard nodes. |
| 7199 | * @param[in] all_desc Whether to search all the descendants or children only. |
| 7200 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7201 | * @param[in] options XPath options. |
| 7202 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7203 | */ |
| 7204 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 7205 | 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] | 7206 | struct lyxp_set *set, uint32_t options) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7207 | { |
| 7208 | LY_ERR rc; |
| 7209 | |
| 7210 | /* TODO */ |
| 7211 | (void)attr_axis; |
| 7212 | (void)all_desc; |
| 7213 | |
| 7214 | if (set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7215 | assert(exp->tok_len[*tok_idx] == 4); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7216 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 7217 | set_scnode_clear_ctx(set); |
| 7218 | /* just for the debug message below */ |
| 7219 | set = NULL; |
| 7220 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7221 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7222 | rc = xpath_node(NULL, 0, set, options); |
| 7223 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7224 | assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4)); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7225 | rc = xpath_text(NULL, 0, set, options); |
| 7226 | } |
| 7227 | LY_CHECK_RET(rc); |
| 7228 | } |
| 7229 | } |
| 7230 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7231 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7232 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7233 | |
| 7234 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7235 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7236 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7237 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7238 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7239 | |
| 7240 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7241 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7242 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7243 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7244 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7245 | |
| 7246 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7247 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 7248 | rc = eval_predicate(exp, tok_idx, set, options, 1); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7249 | LY_CHECK_RET(rc); |
| 7250 | } |
| 7251 | |
| 7252 | return LY_SUCCESS; |
| 7253 | } |
| 7254 | |
| 7255 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7256 | * @brief Evaluate RelativeLocationPath. Logs directly on error. |
| 7257 | * |
| 7258 | * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step |
| 7259 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7260 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7261 | * |
| 7262 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7263 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7264 | * @param[in] all_desc Whether to search all the descendants or children only. |
| 7265 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7266 | * @param[in] options XPath options. |
| 7267 | * @return LY_ERR (YL_EINCOMPLETE on unresolved when) |
| 7268 | */ |
| 7269 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 7270 | eval_relative_location_path(const struct lyxp_expr *exp, uint16_t *tok_idx, ly_bool all_desc, struct lyxp_set *set, |
| 7271 | uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7272 | { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 7273 | ly_bool attr_axis; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7274 | LY_ERR rc; |
| 7275 | |
| 7276 | goto step; |
| 7277 | do { |
| 7278 | /* evaluate '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7279 | if (exp->tok_len[*tok_idx] == 1) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7280 | all_desc = 0; |
| 7281 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7282 | assert(exp->tok_len[*tok_idx] == 2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7283 | all_desc = 1; |
| 7284 | } |
| 7285 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7286 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7287 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7288 | |
| 7289 | step: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7290 | /* evaluate abbreviated axis '@'? if any */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7291 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7292 | attr_axis = 1; |
| 7293 | |
| 7294 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7295 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7296 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7297 | } else { |
| 7298 | attr_axis = 0; |
| 7299 | } |
| 7300 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7301 | /* Step */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7302 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7303 | case LYXP_TOKEN_DOT: |
| 7304 | /* evaluate '.' */ |
| 7305 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 7306 | rc = moveto_scnode_self(set, all_desc, options); |
| 7307 | } else { |
| 7308 | rc = moveto_self(set, all_desc, options); |
| 7309 | } |
| 7310 | LY_CHECK_RET(rc); |
| 7311 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7312 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7313 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7314 | break; |
| 7315 | |
| 7316 | case LYXP_TOKEN_DDOT: |
| 7317 | /* evaluate '..' */ |
| 7318 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 7319 | rc = moveto_scnode_parent(set, all_desc, options); |
| 7320 | } else { |
| 7321 | rc = moveto_parent(set, all_desc, options); |
| 7322 | } |
| 7323 | LY_CHECK_RET(rc); |
| 7324 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7325 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7326 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7327 | break; |
| 7328 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7329 | case LYXP_TOKEN_NAMETEST: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7330 | /* evaluate NameTest Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7331 | rc = eval_name_test_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7332 | LY_CHECK_RET(rc); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7333 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7334 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7335 | case LYXP_TOKEN_NODETYPE: |
| 7336 | /* evaluate NodeType Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7337 | rc = eval_node_type_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7338 | LY_CHECK_RET(rc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7339 | break; |
| 7340 | |
| 7341 | default: |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 7342 | LOGINT_RET(set ? set->ctx : NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7343 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7344 | } 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] | 7345 | |
| 7346 | return LY_SUCCESS; |
| 7347 | } |
| 7348 | |
| 7349 | /** |
| 7350 | * @brief Evaluate AbsoluteLocationPath. Logs directly on error. |
| 7351 | * |
| 7352 | * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath |
| 7353 | * |
| 7354 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7355 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7356 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7357 | * @param[in] options XPath options. |
| 7358 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7359 | */ |
| 7360 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 7361 | 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] | 7362 | { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 7363 | ly_bool all_desc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7364 | |
| 7365 | if (set) { |
| 7366 | /* no matter what tokens follow, we need to be at the root */ |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 7367 | LY_CHECK_RET(moveto_root(set, options)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7368 | } |
| 7369 | |
| 7370 | /* '/' RelativeLocationPath? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7371 | if (exp->tok_len[*tok_idx] == 1) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7372 | /* evaluate '/' - deferred */ |
| 7373 | all_desc = 0; |
| 7374 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7375 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7376 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7377 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7378 | if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7379 | return LY_SUCCESS; |
| 7380 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7381 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7382 | case LYXP_TOKEN_DOT: |
| 7383 | case LYXP_TOKEN_DDOT: |
| 7384 | case LYXP_TOKEN_AT: |
| 7385 | case LYXP_TOKEN_NAMETEST: |
| 7386 | case LYXP_TOKEN_NODETYPE: |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 7387 | 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] | 7388 | break; |
| 7389 | default: |
| 7390 | break; |
| 7391 | } |
| 7392 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7393 | } else { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 7394 | /* '//' RelativeLocationPath */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7395 | /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */ |
| 7396 | all_desc = 1; |
| 7397 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
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 | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7400 | |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 7401 | 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] | 7402 | } |
| 7403 | |
| 7404 | return LY_SUCCESS; |
| 7405 | } |
| 7406 | |
| 7407 | /** |
| 7408 | * @brief Evaluate FunctionCall. Logs directly on error. |
| 7409 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7410 | * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7411 | * |
| 7412 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7413 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7414 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7415 | * @param[in] options XPath options. |
| 7416 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7417 | */ |
| 7418 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 7419 | 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] | 7420 | { |
| 7421 | LY_ERR rc; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7422 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7423 | 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] | 7424 | uint16_t arg_count = 0, i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7425 | struct lyxp_set **args = NULL, **args_aux; |
| 7426 | |
| 7427 | if (set) { |
| 7428 | /* FunctionName */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7429 | switch (exp->tok_len[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7430 | case 3: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7431 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7432 | xpath_func = &xpath_not; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7433 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7434 | xpath_func = &xpath_sum; |
| 7435 | } |
| 7436 | break; |
| 7437 | case 4: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7438 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7439 | xpath_func = &xpath_lang; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7440 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7441 | xpath_func = &xpath_last; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7442 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7443 | xpath_func = &xpath_name; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7444 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7445 | xpath_func = &xpath_true; |
| 7446 | } |
| 7447 | break; |
| 7448 | case 5: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7449 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7450 | xpath_func = &xpath_count; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7451 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7452 | xpath_func = &xpath_false; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7453 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7454 | xpath_func = &xpath_floor; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7455 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7456 | xpath_func = &xpath_round; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7457 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7458 | xpath_func = &xpath_deref; |
| 7459 | } |
| 7460 | break; |
| 7461 | case 6: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7462 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7463 | xpath_func = &xpath_concat; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7464 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7465 | xpath_func = &xpath_number; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7466 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7467 | xpath_func = &xpath_string; |
| 7468 | } |
| 7469 | break; |
| 7470 | case 7: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7471 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7472 | xpath_func = &xpath_boolean; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7473 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7474 | xpath_func = &xpath_ceiling; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7475 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7476 | xpath_func = &xpath_current; |
| 7477 | } |
| 7478 | break; |
| 7479 | case 8: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7480 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7481 | xpath_func = &xpath_contains; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7482 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7483 | xpath_func = &xpath_position; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7484 | } 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] | 7485 | xpath_func = &xpath_re_match; |
| 7486 | } |
| 7487 | break; |
| 7488 | case 9: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7489 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7490 | xpath_func = &xpath_substring; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7491 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7492 | xpath_func = &xpath_translate; |
| 7493 | } |
| 7494 | break; |
| 7495 | case 10: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7496 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7497 | xpath_func = &xpath_local_name; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7498 | } 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] | 7499 | xpath_func = &xpath_enum_value; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7500 | } 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] | 7501 | xpath_func = &xpath_bit_is_set; |
| 7502 | } |
| 7503 | break; |
| 7504 | case 11: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7505 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7506 | xpath_func = &xpath_starts_with; |
| 7507 | } |
| 7508 | break; |
| 7509 | case 12: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7510 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7511 | xpath_func = &xpath_derived_from; |
| 7512 | } |
| 7513 | break; |
| 7514 | case 13: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7515 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7516 | xpath_func = &xpath_namespace_uri; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7517 | } 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] | 7518 | xpath_func = &xpath_string_length; |
| 7519 | } |
| 7520 | break; |
| 7521 | case 15: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7522 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7523 | xpath_func = &xpath_normalize_space; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7524 | } 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] | 7525 | xpath_func = &xpath_substring_after; |
| 7526 | } |
| 7527 | break; |
| 7528 | case 16: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7529 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7530 | xpath_func = &xpath_substring_before; |
| 7531 | } |
| 7532 | break; |
| 7533 | case 20: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7534 | 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] | 7535 | xpath_func = &xpath_derived_from_or_self; |
| 7536 | } |
| 7537 | break; |
| 7538 | } |
| 7539 | |
| 7540 | if (!xpath_func) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 7541 | 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] | 7542 | return LY_EVALID; |
| 7543 | } |
| 7544 | } |
| 7545 | |
| 7546 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7547 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7548 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7549 | |
| 7550 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7551 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7552 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7553 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7554 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7555 | |
| 7556 | /* ( Expr ( ',' Expr )* )? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7557 | if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7558 | if (set) { |
| 7559 | args = malloc(sizeof *args); |
| 7560 | LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup); |
| 7561 | arg_count = 1; |
| 7562 | args[0] = set_copy(set); |
| 7563 | if (!args[0]) { |
| 7564 | rc = LY_EMEM; |
| 7565 | goto cleanup; |
| 7566 | } |
| 7567 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7568 | rc = eval_expr_select(exp, tok_idx, 0, args[0], options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7569 | LY_CHECK_GOTO(rc, cleanup); |
| 7570 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7571 | rc = eval_expr_select(exp, tok_idx, 0, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7572 | LY_CHECK_GOTO(rc, cleanup); |
| 7573 | } |
| 7574 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7575 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7576 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7577 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7578 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7579 | |
| 7580 | if (set) { |
| 7581 | ++arg_count; |
| 7582 | args_aux = realloc(args, arg_count * sizeof *args); |
| 7583 | LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup); |
| 7584 | args = args_aux; |
| 7585 | args[arg_count - 1] = set_copy(set); |
| 7586 | if (!args[arg_count - 1]) { |
| 7587 | rc = LY_EMEM; |
| 7588 | goto cleanup; |
| 7589 | } |
| 7590 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7591 | 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] | 7592 | LY_CHECK_GOTO(rc, cleanup); |
| 7593 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7594 | rc = eval_expr_select(exp, tok_idx, 0, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7595 | LY_CHECK_GOTO(rc, cleanup); |
| 7596 | } |
| 7597 | } |
| 7598 | |
| 7599 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7600 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7601 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7602 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7603 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7604 | |
| 7605 | if (set) { |
| 7606 | /* evaluate function */ |
| 7607 | rc = xpath_func(args, arg_count, set, options); |
| 7608 | |
| 7609 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7610 | /* merge all nodes from arg evaluations */ |
| 7611 | for (i = 0; i < arg_count; ++i) { |
| 7612 | set_scnode_clear_ctx(args[i]); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7613 | lyxp_set_scnode_merge(set, args[i]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7614 | } |
| 7615 | } |
| 7616 | } else { |
| 7617 | rc = LY_SUCCESS; |
| 7618 | } |
| 7619 | |
| 7620 | cleanup: |
| 7621 | for (i = 0; i < arg_count; ++i) { |
| 7622 | lyxp_set_free(args[i]); |
| 7623 | } |
| 7624 | free(args); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7625 | return rc; |
| 7626 | } |
| 7627 | |
| 7628 | /** |
| 7629 | * @brief Evaluate Number. Logs directly on error. |
| 7630 | * |
| 7631 | * @param[in] ctx Context for errors. |
| 7632 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7633 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7634 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7635 | * @return LY_ERR |
| 7636 | */ |
| 7637 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 7638 | 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] | 7639 | { |
| 7640 | long double num; |
| 7641 | char *endptr; |
| 7642 | |
| 7643 | if (set) { |
| 7644 | errno = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7645 | num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7646 | if (errno) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 7647 | LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]); |
| 7648 | 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] | 7649 | 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] | 7650 | return LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7651 | } 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] | 7652 | LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]); |
| 7653 | LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double.", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7654 | exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7655 | return LY_EVALID; |
| 7656 | } |
| 7657 | |
| 7658 | set_fill_number(set, num); |
| 7659 | } |
| 7660 | |
| 7661 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7662 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7663 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7664 | return LY_SUCCESS; |
| 7665 | } |
| 7666 | |
| 7667 | /** |
| 7668 | * @brief Evaluate PathExpr. Logs directly on error. |
| 7669 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7670 | * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate* |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7671 | * | PrimaryExpr Predicate* '/' RelativeLocationPath |
| 7672 | * | PrimaryExpr Predicate* '//' RelativeLocationPath |
| 7673 | * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7674 | * [10] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7675 | * |
| 7676 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7677 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7678 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7679 | * @param[in] options XPath options. |
| 7680 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7681 | */ |
| 7682 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 7683 | 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] | 7684 | { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 7685 | ly_bool all_desc, parent_pos_pred; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7686 | LY_ERR rc; |
| 7687 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7688 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7689 | case LYXP_TOKEN_PAR1: |
| 7690 | /* '(' Expr ')' */ |
| 7691 | |
| 7692 | /* '(' */ |
| 7693 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7694 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7695 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7696 | |
| 7697 | /* Expr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7698 | rc = eval_expr_select(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7699 | LY_CHECK_RET(rc); |
| 7700 | |
| 7701 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7702 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7703 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7704 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7705 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7706 | |
| 7707 | parent_pos_pred = 0; |
| 7708 | goto predicate; |
| 7709 | |
| 7710 | case LYXP_TOKEN_DOT: |
| 7711 | case LYXP_TOKEN_DDOT: |
| 7712 | case LYXP_TOKEN_AT: |
| 7713 | case LYXP_TOKEN_NAMETEST: |
| 7714 | case LYXP_TOKEN_NODETYPE: |
| 7715 | /* RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7716 | rc = eval_relative_location_path(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7717 | LY_CHECK_RET(rc); |
| 7718 | break; |
| 7719 | |
| 7720 | case LYXP_TOKEN_FUNCNAME: |
| 7721 | /* FunctionCall */ |
| 7722 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7723 | rc = eval_function_call(exp, tok_idx, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7724 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7725 | rc = eval_function_call(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7726 | } |
| 7727 | LY_CHECK_RET(rc); |
| 7728 | |
| 7729 | parent_pos_pred = 1; |
| 7730 | goto predicate; |
| 7731 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 7732 | case LYXP_TOKEN_OPER_PATH: |
| 7733 | case LYXP_TOKEN_OPER_RPATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7734 | /* AbsoluteLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7735 | rc = eval_absolute_location_path(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7736 | LY_CHECK_RET(rc); |
| 7737 | break; |
| 7738 | |
| 7739 | case LYXP_TOKEN_LITERAL: |
| 7740 | /* Literal */ |
| 7741 | if (!set || (options & LYXP_SCNODE_ALL)) { |
| 7742 | if (set) { |
| 7743 | set_scnode_clear_ctx(set); |
| 7744 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7745 | eval_literal(exp, tok_idx, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7746 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7747 | eval_literal(exp, tok_idx, set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7748 | } |
| 7749 | |
| 7750 | parent_pos_pred = 1; |
| 7751 | goto predicate; |
| 7752 | |
| 7753 | case LYXP_TOKEN_NUMBER: |
| 7754 | /* Number */ |
| 7755 | if (!set || (options & LYXP_SCNODE_ALL)) { |
| 7756 | if (set) { |
| 7757 | set_scnode_clear_ctx(set); |
| 7758 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7759 | rc = eval_number(NULL, exp, tok_idx, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7760 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7761 | rc = eval_number(set->ctx, exp, tok_idx, set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7762 | } |
| 7763 | LY_CHECK_RET(rc); |
| 7764 | |
| 7765 | parent_pos_pred = 1; |
| 7766 | goto predicate; |
| 7767 | |
| 7768 | default: |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 7769 | 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] | 7770 | return LY_EVALID; |
| 7771 | } |
| 7772 | |
| 7773 | return LY_SUCCESS; |
| 7774 | |
| 7775 | predicate: |
| 7776 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7777 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 7778 | rc = eval_predicate(exp, tok_idx, set, options, parent_pos_pred); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7779 | LY_CHECK_RET(rc); |
| 7780 | } |
| 7781 | |
| 7782 | /* ('/' or '//') RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7783 | 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] | 7784 | |
| 7785 | /* evaluate '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7786 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7787 | all_desc = 0; |
| 7788 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7789 | all_desc = 1; |
| 7790 | } |
| 7791 | |
| 7792 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7793 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7794 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7795 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7796 | rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7797 | LY_CHECK_RET(rc); |
| 7798 | } |
| 7799 | |
| 7800 | return LY_SUCCESS; |
| 7801 | } |
| 7802 | |
| 7803 | /** |
| 7804 | * @brief Evaluate UnionExpr. Logs directly on error. |
| 7805 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7806 | * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7807 | * |
| 7808 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7809 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7810 | * @param[in] repeat How many times this expression is repeated. |
| 7811 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7812 | * @param[in] options XPath options. |
| 7813 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7814 | */ |
| 7815 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 7816 | 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] | 7817 | { |
| 7818 | LY_ERR rc = LY_SUCCESS; |
| 7819 | struct lyxp_set orig_set, set2; |
| 7820 | uint16_t i; |
| 7821 | |
| 7822 | assert(repeat); |
| 7823 | |
| 7824 | set_init(&orig_set, set); |
| 7825 | set_init(&set2, set); |
| 7826 | |
| 7827 | set_fill_set(&orig_set, set); |
| 7828 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7829 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7830 | LY_CHECK_GOTO(rc, cleanup); |
| 7831 | |
| 7832 | /* ('|' PathExpr)* */ |
| 7833 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7834 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7835 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7836 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7837 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7838 | |
| 7839 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7840 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7841 | LY_CHECK_GOTO(rc, cleanup); |
| 7842 | continue; |
| 7843 | } |
| 7844 | |
| 7845 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7846 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7847 | LY_CHECK_GOTO(rc, cleanup); |
| 7848 | |
| 7849 | /* eval */ |
| 7850 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7851 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7852 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7853 | rc = moveto_union(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7854 | LY_CHECK_GOTO(rc, cleanup); |
| 7855 | } |
| 7856 | } |
| 7857 | |
| 7858 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7859 | lyxp_set_free_content(&orig_set); |
| 7860 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7861 | return rc; |
| 7862 | } |
| 7863 | |
| 7864 | /** |
| 7865 | * @brief Evaluate UnaryExpr. Logs directly on error. |
| 7866 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7867 | * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7868 | * |
| 7869 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7870 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7871 | * @param[in] repeat How many times this expression is repeated. |
| 7872 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7873 | * @param[in] options XPath options. |
| 7874 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7875 | */ |
| 7876 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 7877 | 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] | 7878 | { |
| 7879 | LY_ERR rc; |
| 7880 | uint16_t this_op, i; |
| 7881 | |
| 7882 | assert(repeat); |
| 7883 | |
| 7884 | /* ('-')+ */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7885 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7886 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7887 | 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] | 7888 | |
| 7889 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7890 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7891 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7892 | } |
| 7893 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7894 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7895 | LY_CHECK_RET(rc); |
| 7896 | |
| 7897 | if (set && (repeat % 2)) { |
| 7898 | if (options & LYXP_SCNODE_ALL) { |
| 7899 | warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]); |
| 7900 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7901 | 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] | 7902 | LY_CHECK_RET(rc); |
| 7903 | } |
| 7904 | } |
| 7905 | |
| 7906 | return LY_SUCCESS; |
| 7907 | } |
| 7908 | |
| 7909 | /** |
| 7910 | * @brief Evaluate MultiplicativeExpr. Logs directly on error. |
| 7911 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7912 | * [18] MultiplicativeExpr ::= UnaryExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7913 | * | MultiplicativeExpr '*' UnaryExpr |
| 7914 | * | MultiplicativeExpr 'div' UnaryExpr |
| 7915 | * | MultiplicativeExpr 'mod' UnaryExpr |
| 7916 | * |
| 7917 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7918 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7919 | * @param[in] repeat How many times this expression is repeated. |
| 7920 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7921 | * @param[in] options XPath options. |
| 7922 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7923 | */ |
| 7924 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 7925 | eval_multiplicative_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, |
| 7926 | uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7927 | { |
| 7928 | LY_ERR rc; |
| 7929 | uint16_t this_op; |
| 7930 | struct lyxp_set orig_set, set2; |
| 7931 | uint16_t i; |
| 7932 | |
| 7933 | assert(repeat); |
| 7934 | |
| 7935 | set_init(&orig_set, set); |
| 7936 | set_init(&set2, set); |
| 7937 | |
| 7938 | set_fill_set(&orig_set, set); |
| 7939 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7940 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7941 | LY_CHECK_GOTO(rc, cleanup); |
| 7942 | |
| 7943 | /* ('*' / 'div' / 'mod' UnaryExpr)* */ |
| 7944 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7945 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7946 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7947 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7948 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7949 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7950 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7951 | |
| 7952 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7953 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7954 | LY_CHECK_GOTO(rc, cleanup); |
| 7955 | continue; |
| 7956 | } |
| 7957 | |
| 7958 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7959 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7960 | LY_CHECK_GOTO(rc, cleanup); |
| 7961 | |
| 7962 | /* eval */ |
| 7963 | if (options & LYXP_SCNODE_ALL) { |
| 7964 | 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] | 7965 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7966 | set_scnode_clear_ctx(set); |
| 7967 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7968 | 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] | 7969 | LY_CHECK_GOTO(rc, cleanup); |
| 7970 | } |
| 7971 | } |
| 7972 | |
| 7973 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7974 | lyxp_set_free_content(&orig_set); |
| 7975 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7976 | return rc; |
| 7977 | } |
| 7978 | |
| 7979 | /** |
| 7980 | * @brief Evaluate AdditiveExpr. Logs directly on error. |
| 7981 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7982 | * [17] AdditiveExpr ::= MultiplicativeExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7983 | * | AdditiveExpr '+' MultiplicativeExpr |
| 7984 | * | AdditiveExpr '-' MultiplicativeExpr |
| 7985 | * |
| 7986 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7987 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7988 | * @param[in] repeat How many times this expression is repeated. |
| 7989 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7990 | * @param[in] options XPath options. |
| 7991 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7992 | */ |
| 7993 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 7994 | 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] | 7995 | { |
| 7996 | LY_ERR rc; |
| 7997 | uint16_t this_op; |
| 7998 | struct lyxp_set orig_set, set2; |
| 7999 | uint16_t i; |
| 8000 | |
| 8001 | assert(repeat); |
| 8002 | |
| 8003 | set_init(&orig_set, set); |
| 8004 | set_init(&set2, set); |
| 8005 | |
| 8006 | set_fill_set(&orig_set, set); |
| 8007 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8008 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8009 | LY_CHECK_GOTO(rc, cleanup); |
| 8010 | |
| 8011 | /* ('+' / '-' MultiplicativeExpr)* */ |
| 8012 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8013 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8014 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8015 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8016 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 8017 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8018 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8019 | |
| 8020 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8021 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8022 | LY_CHECK_GOTO(rc, cleanup); |
| 8023 | continue; |
| 8024 | } |
| 8025 | |
| 8026 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8027 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8028 | LY_CHECK_GOTO(rc, cleanup); |
| 8029 | |
| 8030 | /* eval */ |
| 8031 | if (options & LYXP_SCNODE_ALL) { |
| 8032 | 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] | 8033 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8034 | set_scnode_clear_ctx(set); |
| 8035 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8036 | 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] | 8037 | LY_CHECK_GOTO(rc, cleanup); |
| 8038 | } |
| 8039 | } |
| 8040 | |
| 8041 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8042 | lyxp_set_free_content(&orig_set); |
| 8043 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8044 | return rc; |
| 8045 | } |
| 8046 | |
| 8047 | /** |
| 8048 | * @brief Evaluate RelationalExpr. Logs directly on error. |
| 8049 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8050 | * [16] RelationalExpr ::= AdditiveExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8051 | * | RelationalExpr '<' AdditiveExpr |
| 8052 | * | RelationalExpr '>' AdditiveExpr |
| 8053 | * | RelationalExpr '<=' AdditiveExpr |
| 8054 | * | RelationalExpr '>=' AdditiveExpr |
| 8055 | * |
| 8056 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8057 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8058 | * @param[in] repeat How many times this expression is repeated. |
| 8059 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8060 | * @param[in] options XPath options. |
| 8061 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8062 | */ |
| 8063 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 8064 | 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] | 8065 | { |
| 8066 | LY_ERR rc; |
| 8067 | uint16_t this_op; |
| 8068 | struct lyxp_set orig_set, set2; |
| 8069 | uint16_t i; |
| 8070 | |
| 8071 | assert(repeat); |
| 8072 | |
| 8073 | set_init(&orig_set, set); |
| 8074 | set_init(&set2, set); |
| 8075 | |
| 8076 | set_fill_set(&orig_set, set); |
| 8077 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8078 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8079 | LY_CHECK_GOTO(rc, cleanup); |
| 8080 | |
| 8081 | /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */ |
| 8082 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8083 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8084 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8085 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8086 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 8087 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8088 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8089 | |
| 8090 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8091 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8092 | LY_CHECK_GOTO(rc, cleanup); |
| 8093 | continue; |
| 8094 | } |
| 8095 | |
| 8096 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8097 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8098 | LY_CHECK_GOTO(rc, cleanup); |
| 8099 | |
| 8100 | /* eval */ |
| 8101 | if (options & LYXP_SCNODE_ALL) { |
| 8102 | 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] | 8103 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8104 | set_scnode_clear_ctx(set); |
| 8105 | } else { |
| 8106 | rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options); |
| 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 EqualityExpr. Logs directly on error. |
| 8119 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8120 | * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8121 | * | EqualityExpr '!=' RelationalExpr |
| 8122 | * |
| 8123 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8124 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8125 | * @param[in] repeat How many times this expression is repeated. |
| 8126 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8127 | * @param[in] options XPath options. |
| 8128 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8129 | */ |
| 8130 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 8131 | 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] | 8132 | { |
| 8133 | LY_ERR rc; |
| 8134 | uint16_t this_op; |
| 8135 | struct lyxp_set orig_set, set2; |
| 8136 | uint16_t i; |
| 8137 | |
| 8138 | assert(repeat); |
| 8139 | |
| 8140 | set_init(&orig_set, set); |
| 8141 | set_init(&set2, set); |
| 8142 | |
| 8143 | set_fill_set(&orig_set, set); |
| 8144 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8145 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8146 | LY_CHECK_GOTO(rc, cleanup); |
| 8147 | |
| 8148 | /* ('=' / '!=' RelationalExpr)* */ |
| 8149 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8150 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8151 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8152 | assert((exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL) || (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_NEQUAL)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8153 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 8154 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8155 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8156 | |
| 8157 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8158 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8159 | LY_CHECK_GOTO(rc, cleanup); |
| 8160 | continue; |
| 8161 | } |
| 8162 | |
| 8163 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8164 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8165 | LY_CHECK_GOTO(rc, cleanup); |
| 8166 | |
| 8167 | /* eval */ |
| 8168 | if (options & LYXP_SCNODE_ALL) { |
| 8169 | 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] | 8170 | warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1); |
| 8171 | 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] | 8172 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8173 | set_scnode_clear_ctx(set); |
| 8174 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8175 | rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options); |
| 8176 | LY_CHECK_GOTO(rc, cleanup); |
| 8177 | } |
| 8178 | } |
| 8179 | |
| 8180 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8181 | lyxp_set_free_content(&orig_set); |
| 8182 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8183 | return rc; |
| 8184 | } |
| 8185 | |
| 8186 | /** |
| 8187 | * @brief Evaluate AndExpr. Logs directly on error. |
| 8188 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8189 | * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8190 | * |
| 8191 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8192 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8193 | * @param[in] repeat How many times this expression is repeated. |
| 8194 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8195 | * @param[in] options XPath options. |
| 8196 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8197 | */ |
| 8198 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 8199 | 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] | 8200 | { |
| 8201 | LY_ERR rc; |
| 8202 | struct lyxp_set orig_set, set2; |
| 8203 | uint16_t i; |
| 8204 | |
| 8205 | assert(repeat); |
| 8206 | |
| 8207 | set_init(&orig_set, set); |
| 8208 | set_init(&set2, set); |
| 8209 | |
| 8210 | set_fill_set(&orig_set, set); |
| 8211 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8212 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8213 | LY_CHECK_GOTO(rc, cleanup); |
| 8214 | |
| 8215 | /* cast to boolean, we know that will be the final result */ |
| 8216 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 8217 | set_scnode_clear_ctx(set); |
| 8218 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8219 | lyxp_set_cast(set, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8220 | } |
| 8221 | |
| 8222 | /* ('and' EqualityExpr)* */ |
| 8223 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8224 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG); |
| 8225 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || !set->val.bln ? "skipped" : "parsed"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 8226 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8227 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8228 | |
| 8229 | /* lazy evaluation */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8230 | if (!set || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) { |
| 8231 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8232 | LY_CHECK_GOTO(rc, cleanup); |
| 8233 | continue; |
| 8234 | } |
| 8235 | |
| 8236 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8237 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8238 | LY_CHECK_GOTO(rc, cleanup); |
| 8239 | |
| 8240 | /* eval - just get boolean value actually */ |
| 8241 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 8242 | set_scnode_clear_ctx(&set2); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 8243 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8244 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8245 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8246 | set_fill_set(set, &set2); |
| 8247 | } |
| 8248 | } |
| 8249 | |
| 8250 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8251 | lyxp_set_free_content(&orig_set); |
| 8252 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8253 | return rc; |
| 8254 | } |
| 8255 | |
| 8256 | /** |
| 8257 | * @brief Evaluate OrExpr. Logs directly on error. |
| 8258 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8259 | * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8260 | * |
| 8261 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8262 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8263 | * @param[in] repeat How many times this expression is repeated. |
| 8264 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8265 | * @param[in] options XPath options. |
| 8266 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8267 | */ |
| 8268 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 8269 | 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] | 8270 | { |
| 8271 | LY_ERR rc; |
| 8272 | struct lyxp_set orig_set, set2; |
| 8273 | uint16_t i; |
| 8274 | |
| 8275 | assert(repeat); |
| 8276 | |
| 8277 | set_init(&orig_set, set); |
| 8278 | set_init(&set2, set); |
| 8279 | |
| 8280 | set_fill_set(&orig_set, set); |
| 8281 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8282 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8283 | LY_CHECK_GOTO(rc, cleanup); |
| 8284 | |
| 8285 | /* cast to boolean, we know that will be the final result */ |
| 8286 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 8287 | set_scnode_clear_ctx(set); |
| 8288 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8289 | lyxp_set_cast(set, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8290 | } |
| 8291 | |
| 8292 | /* ('or' AndExpr)* */ |
| 8293 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8294 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG); |
| 8295 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || set->val.bln ? "skipped" : "parsed"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 8296 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8297 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8298 | |
| 8299 | /* lazy evaluation */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8300 | if (!set || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) { |
| 8301 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8302 | LY_CHECK_GOTO(rc, cleanup); |
| 8303 | continue; |
| 8304 | } |
| 8305 | |
| 8306 | set_fill_set(&set2, &orig_set); |
| 8307 | /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one), |
| 8308 | * but it does not matter */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8309 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8310 | LY_CHECK_GOTO(rc, cleanup); |
| 8311 | |
| 8312 | /* eval - just get boolean value actually */ |
| 8313 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 8314 | set_scnode_clear_ctx(&set2); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 8315 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8316 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8317 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8318 | set_fill_set(set, &set2); |
| 8319 | } |
| 8320 | } |
| 8321 | |
| 8322 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8323 | lyxp_set_free_content(&orig_set); |
| 8324 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8325 | return rc; |
| 8326 | } |
| 8327 | |
| 8328 | /** |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8329 | * @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] | 8330 | * |
| 8331 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8332 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8333 | * @param[in] etype Expression type to evaluate. |
| 8334 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8335 | * @param[in] options XPath options. |
| 8336 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8337 | */ |
| 8338 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 8339 | eval_expr_select(const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype, struct lyxp_set *set, |
| 8340 | uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8341 | { |
| 8342 | uint16_t i, count; |
| 8343 | enum lyxp_expr_type next_etype; |
| 8344 | LY_ERR rc; |
| 8345 | |
| 8346 | /* process operator repeats */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8347 | if (!exp->repeat[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8348 | next_etype = LYXP_EXPR_NONE; |
| 8349 | } else { |
| 8350 | /* find etype repeat */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 8351 | for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i) {} |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8352 | |
| 8353 | /* select one-priority lower because etype expression called us */ |
| 8354 | if (i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8355 | next_etype = exp->repeat[*tok_idx][i - 1]; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8356 | /* count repeats for that expression */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 8357 | 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] | 8358 | } else { |
| 8359 | next_etype = LYXP_EXPR_NONE; |
| 8360 | } |
| 8361 | } |
| 8362 | |
| 8363 | /* decide what expression are we parsing based on the repeat */ |
| 8364 | switch (next_etype) { |
| 8365 | case LYXP_EXPR_OR: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8366 | rc = eval_or_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8367 | break; |
| 8368 | case LYXP_EXPR_AND: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8369 | rc = eval_and_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8370 | break; |
| 8371 | case LYXP_EXPR_EQUALITY: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8372 | rc = eval_equality_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8373 | break; |
| 8374 | case LYXP_EXPR_RELATIONAL: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8375 | rc = eval_relational_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8376 | break; |
| 8377 | case LYXP_EXPR_ADDITIVE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8378 | rc = eval_additive_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8379 | break; |
| 8380 | case LYXP_EXPR_MULTIPLICATIVE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8381 | rc = eval_multiplicative_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8382 | break; |
| 8383 | case LYXP_EXPR_UNARY: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8384 | rc = eval_unary_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8385 | break; |
| 8386 | case LYXP_EXPR_UNION: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8387 | rc = eval_union_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8388 | break; |
| 8389 | case LYXP_EXPR_NONE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8390 | rc = eval_path_expr(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8391 | break; |
| 8392 | default: |
| 8393 | LOGINT_RET(set->ctx); |
| 8394 | } |
| 8395 | |
| 8396 | return rc; |
| 8397 | } |
| 8398 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8399 | /** |
| 8400 | * @brief Get root type. |
| 8401 | * |
| 8402 | * @param[in] ctx_node Context node. |
| 8403 | * @param[in] ctx_scnode Schema context node. |
| 8404 | * @param[in] options XPath options. |
| 8405 | * @return Root type. |
| 8406 | */ |
| 8407 | static enum lyxp_node_type |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 8408 | 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] | 8409 | { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 8410 | const struct lysc_node *op; |
| 8411 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8412 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8413 | /* schema */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 8414 | 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] | 8415 | |
| 8416 | if (op || (options & LYXP_SCNODE)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8417 | /* general root that can access everything */ |
| 8418 | return LYXP_NODE_ROOT; |
| 8419 | } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) { |
| 8420 | /* root context node can access only config data (because we said so, it is unspecified) */ |
| 8421 | return LYXP_NODE_ROOT_CONFIG; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8422 | } |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 8423 | return LYXP_NODE_ROOT; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8424 | } |
| 8425 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8426 | /* data */ |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 8427 | op = ctx_node ? ctx_node->schema : NULL; |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 8428 | 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] | 8429 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8430 | if (op || !(options & LYXP_SCHEMA)) { |
| 8431 | /* general root that can access everything */ |
| 8432 | return LYXP_NODE_ROOT; |
| 8433 | } else if (!ctx_node || (ctx_node->schema->flags & LYS_CONFIG_W)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8434 | /* root context node can access only config data (because we said so, it is unspecified) */ |
| 8435 | return LYXP_NODE_ROOT_CONFIG; |
| 8436 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8437 | return LYXP_NODE_ROOT; |
| 8438 | } |
| 8439 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8440 | LY_ERR |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 8441 | lyxp_eval(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod, |
| 8442 | LY_PREFIX_FORMAT format, void *prefix_data, const struct lyd_node *ctx_node, const struct lyd_node *tree, |
| 8443 | struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8444 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8445 | uint16_t tok_idx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8446 | LY_ERR rc; |
| 8447 | |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 8448 | LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL); |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8449 | if (!cur_mod && ((format == LY_PREF_SCHEMA) || (format == LY_PREF_SCHEMA_RESOLVED))) { |
| 8450 | LOGARG(NULL, "Current module must be set if schema format is used."); |
| 8451 | return LY_EINVAL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8452 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8453 | |
Michal Vasko | d3bb12f | 2020-12-04 14:33:09 +0100 | [diff] [blame] | 8454 | if (tree) { |
| 8455 | /* adjust the pointer to be the first top-level sibling */ |
| 8456 | while (tree->parent) { |
| 8457 | tree = lyd_parent(tree); |
| 8458 | } |
| 8459 | tree = lyd_first_sibling(tree); |
| 8460 | } |
| 8461 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8462 | /* prepare set for evaluation */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8463 | memset(set, 0, sizeof *set); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8464 | set->type = LYXP_SET_NODE_SET; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8465 | set->root_type = lyxp_get_root_type(ctx_node, NULL, options); |
| 8466 | set_insert_node(set, (struct lyd_node *)ctx_node, 0, ctx_node ? LYXP_NODE_ELEM : set->root_type, 0); |
| 8467 | |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 8468 | set->ctx = (struct ly_ctx *)ctx; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8469 | set->cur_node = ctx_node; |
| 8470 | for (set->context_op = ctx_node ? ctx_node->schema : NULL; |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 8471 | set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); |
| 8472 | set->context_op = set->context_op->parent) {} |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 8473 | set->tree = tree; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8474 | set->cur_mod = cur_mod; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8475 | set->format = format; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8476 | set->prefix_data = prefix_data; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8477 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 8478 | LOG_LOCSET(NULL, set->cur_node, NULL, NULL); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 8479 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8480 | /* evaluate */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8481 | rc = eval_expr_select(exp, &tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8482 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8483 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8484 | } |
| 8485 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 8486 | LOG_LOCBACK(0, 1, 0, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8487 | return rc; |
| 8488 | } |
| 8489 | |
| 8490 | #if 0 |
| 8491 | |
| 8492 | /* full xml printing of set elements, not used currently */ |
| 8493 | |
| 8494 | void |
| 8495 | lyxp_set_print_xml(FILE *f, struct lyxp_set *set) |
| 8496 | { |
| 8497 | uint32_t i; |
| 8498 | char *str_num; |
| 8499 | struct lyout out; |
| 8500 | |
| 8501 | memset(&out, 0, sizeof out); |
| 8502 | |
| 8503 | out.type = LYOUT_STREAM; |
| 8504 | out.method.f = f; |
| 8505 | |
| 8506 | switch (set->type) { |
| 8507 | case LYXP_SET_EMPTY: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8508 | ly_print_(&out, "Empty XPath set\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8509 | break; |
| 8510 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8511 | ly_print_(&out, "Boolean XPath set:\n"); |
| 8512 | ly_print_(&out, "%s\n\n", set->value.bool ? "true" : "false"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8513 | break; |
| 8514 | case LYXP_SET_STRING: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8515 | ly_print_(&out, "String XPath set:\n"); |
| 8516 | ly_print_(&out, "\"%s\"\n\n", set->value.str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8517 | break; |
| 8518 | case LYXP_SET_NUMBER: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8519 | ly_print_(&out, "Number XPath set:\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8520 | |
| 8521 | if (isnan(set->value.num)) { |
| 8522 | str_num = strdup("NaN"); |
| 8523 | } else if ((set->value.num == 0) || (set->value.num == -0.0f)) { |
| 8524 | str_num = strdup("0"); |
| 8525 | } else if (isinf(set->value.num) && !signbit(set->value.num)) { |
| 8526 | str_num = strdup("Infinity"); |
| 8527 | } else if (isinf(set->value.num) && signbit(set->value.num)) { |
| 8528 | str_num = strdup("-Infinity"); |
| 8529 | } else if ((long long)set->value.num == set->value.num) { |
| 8530 | if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) { |
| 8531 | str_num = NULL; |
| 8532 | } |
| 8533 | } else { |
| 8534 | if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) { |
| 8535 | str_num = NULL; |
| 8536 | } |
| 8537 | } |
| 8538 | if (!str_num) { |
| 8539 | LOGMEM; |
| 8540 | return; |
| 8541 | } |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8542 | ly_print_(&out, "%s\n\n", str_num); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8543 | free(str_num); |
| 8544 | break; |
| 8545 | case LYXP_SET_NODE_SET: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8546 | ly_print_(&out, "Node XPath set:\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8547 | |
| 8548 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8549 | ly_print_(&out, "%d. ", i + 1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8550 | switch (set->node_type[i]) { |
| 8551 | case LYXP_NODE_ROOT_ALL: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8552 | ly_print_(&out, "ROOT all\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8553 | break; |
| 8554 | case LYXP_NODE_ROOT_CONFIG: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8555 | ly_print_(&out, "ROOT config\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8556 | break; |
| 8557 | case LYXP_NODE_ROOT_STATE: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8558 | ly_print_(&out, "ROOT state\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8559 | break; |
| 8560 | case LYXP_NODE_ROOT_NOTIF: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8561 | 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] | 8562 | break; |
| 8563 | case LYXP_NODE_ROOT_RPC: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8564 | 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] | 8565 | break; |
| 8566 | case LYXP_NODE_ROOT_OUTPUT: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8567 | 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] | 8568 | break; |
| 8569 | case LYXP_NODE_ELEM: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8570 | ly_print_(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8571 | xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT); |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8572 | ly_print_(&out, "\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8573 | break; |
| 8574 | case LYXP_NODE_TEXT: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8575 | 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] | 8576 | break; |
| 8577 | case LYXP_NODE_ATTR: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8578 | 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] | 8579 | break; |
| 8580 | } |
| 8581 | } |
| 8582 | break; |
| 8583 | } |
| 8584 | } |
| 8585 | |
| 8586 | #endif |
| 8587 | |
| 8588 | LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8589 | lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8590 | { |
| 8591 | long double num; |
| 8592 | char *str; |
| 8593 | LY_ERR rc; |
| 8594 | |
| 8595 | if (!set || (set->type == target)) { |
| 8596 | return LY_SUCCESS; |
| 8597 | } |
| 8598 | |
| 8599 | /* it's not possible to convert anything into a node set */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8600 | assert(target != LYXP_SET_NODE_SET); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8601 | |
| 8602 | if (set->type == LYXP_SET_SCNODE_SET) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8603 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8604 | return LY_EINVAL; |
| 8605 | } |
| 8606 | |
| 8607 | /* to STRING */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8608 | 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] | 8609 | switch (set->type) { |
| 8610 | case LYXP_SET_NUMBER: |
| 8611 | if (isnan(set->val.num)) { |
| 8612 | set->val.str = strdup("NaN"); |
| 8613 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 8614 | } else if ((set->val.num == 0) || (set->val.num == -0.0f)) { |
| 8615 | set->val.str = strdup("0"); |
| 8616 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 8617 | } else if (isinf(set->val.num) && !signbit(set->val.num)) { |
| 8618 | set->val.str = strdup("Infinity"); |
| 8619 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 8620 | } else if (isinf(set->val.num) && signbit(set->val.num)) { |
| 8621 | set->val.str = strdup("-Infinity"); |
| 8622 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 8623 | } else if ((long long)set->val.num == set->val.num) { |
| 8624 | if (asprintf(&str, "%lld", (long long)set->val.num) == -1) { |
| 8625 | LOGMEM_RET(set->ctx); |
| 8626 | } |
| 8627 | set->val.str = str; |
| 8628 | } else { |
| 8629 | if (asprintf(&str, "%03.1Lf", set->val.num) == -1) { |
| 8630 | LOGMEM_RET(set->ctx); |
| 8631 | } |
| 8632 | set->val.str = str; |
| 8633 | } |
| 8634 | break; |
| 8635 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8636 | if (set->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8637 | set->val.str = strdup("true"); |
| 8638 | } else { |
| 8639 | set->val.str = strdup("false"); |
| 8640 | } |
| 8641 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM); |
| 8642 | break; |
| 8643 | case LYXP_SET_NODE_SET: |
| 8644 | assert(set->used); |
| 8645 | |
| 8646 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8647 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8648 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8649 | rc = cast_node_set_to_string(set, &str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8650 | LY_CHECK_RET(rc); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8651 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8652 | set->val.str = str; |
| 8653 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8654 | default: |
| 8655 | LOGINT_RET(set->ctx); |
| 8656 | } |
| 8657 | set->type = LYXP_SET_STRING; |
| 8658 | } |
| 8659 | |
| 8660 | /* to NUMBER */ |
| 8661 | if (target == LYXP_SET_NUMBER) { |
| 8662 | switch (set->type) { |
| 8663 | case LYXP_SET_STRING: |
| 8664 | num = cast_string_to_number(set->val.str); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8665 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8666 | set->val.num = num; |
| 8667 | break; |
| 8668 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8669 | if (set->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8670 | set->val.num = 1; |
| 8671 | } else { |
| 8672 | set->val.num = 0; |
| 8673 | } |
| 8674 | break; |
| 8675 | default: |
| 8676 | LOGINT_RET(set->ctx); |
| 8677 | } |
| 8678 | set->type = LYXP_SET_NUMBER; |
| 8679 | } |
| 8680 | |
| 8681 | /* to BOOLEAN */ |
| 8682 | if (target == LYXP_SET_BOOLEAN) { |
| 8683 | switch (set->type) { |
| 8684 | case LYXP_SET_NUMBER: |
| 8685 | 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] | 8686 | set->val.bln = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8687 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8688 | set->val.bln = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8689 | } |
| 8690 | break; |
| 8691 | case LYXP_SET_STRING: |
| 8692 | if (set->val.str[0]) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8693 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8694 | set->val.bln = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8695 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8696 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8697 | set->val.bln = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8698 | } |
| 8699 | break; |
| 8700 | case LYXP_SET_NODE_SET: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8701 | if (set->used) { |
| 8702 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8703 | set->val.bln = 1; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8704 | } else { |
| 8705 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8706 | set->val.bln = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8707 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8708 | break; |
| 8709 | default: |
| 8710 | LOGINT_RET(set->ctx); |
| 8711 | } |
| 8712 | set->type = LYXP_SET_BOOLEAN; |
| 8713 | } |
| 8714 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8715 | return LY_SUCCESS; |
| 8716 | } |
| 8717 | |
| 8718 | LY_ERR |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 8719 | lyxp_atomize(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod, |
| 8720 | LY_PREFIX_FORMAT format, void *prefix_data, const struct lysc_node *ctx_scnode, struct lyxp_set *set, |
| 8721 | uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8722 | { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 8723 | LY_ERR ret; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8724 | uint16_t tok_idx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8725 | |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 8726 | LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL); |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8727 | if (!cur_mod && ((format == LY_PREF_SCHEMA) || (format == LY_PREF_SCHEMA_RESOLVED))) { |
| 8728 | LOGARG(NULL, "Current module must be set if schema format is used."); |
| 8729 | return LY_EINVAL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8730 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8731 | |
| 8732 | /* prepare set for evaluation */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8733 | memset(set, 0, sizeof *set); |
| 8734 | set->type = LYXP_SET_SCNODE_SET; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8735 | set->root_type = lyxp_get_root_type(NULL, ctx_scnode, options); |
| 8736 | 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] | 8737 | set->val.scnodes[0].in_ctx = LYXP_SET_SCNODE_START; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8738 | |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 8739 | set->ctx = (struct ly_ctx *)ctx; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8740 | set->cur_scnode = ctx_scnode; |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 8741 | for (set->context_op = ctx_scnode; |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 8742 | set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); |
| 8743 | set->context_op = set->context_op->parent) {} |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8744 | set->cur_mod = cur_mod; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8745 | set->format = format; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8746 | set->prefix_data = prefix_data; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8747 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 8748 | LOG_LOCSET(set->cur_scnode, NULL, NULL, NULL); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 8749 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8750 | /* evaluate */ |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 8751 | ret = eval_expr_select(exp, &tok_idx, 0, set, options); |
| 8752 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 8753 | LOG_LOCBACK(1, 0, 0, 0); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 8754 | return ret; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8755 | } |
Michal Vasko | d43d71a | 2020-08-07 14:54:58 +0200 | [diff] [blame] | 8756 | |
| 8757 | API const char * |
| 8758 | lyxp_get_expr(const struct lyxp_expr *path) |
| 8759 | { |
| 8760 | if (!path) { |
| 8761 | return NULL; |
| 8762 | } |
| 8763 | |
| 8764 | return path->expr; |
| 8765 | } |