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 | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 138 | uint16_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 | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 196 | if ((item->node->schema->nodetype == LYS_LIST) && |
| 197 | (((struct lyd_node_inner *)item->node)->child->schema->nodetype == LYS_LEAF)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 198 | 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] | 199 | item->node->schema->name, LYD_CANON_VALUE(lyd_child(item->node))); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 200 | } else if (((struct lyd_node_inner *)item->node)->schema->nodetype == LYS_LEAFLIST) { |
| 201 | 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] | 202 | item->node->schema->name, LYD_CANON_VALUE(item->node)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 203 | } else { |
| 204 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s", i + 1, item->pos, item->node->schema->name); |
| 205 | } |
| 206 | break; |
| 207 | case LYXP_NODE_TEXT: |
| 208 | if (item->node->schema->nodetype & LYS_ANYDATA) { |
| 209 | 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] | 210 | item->node->schema->nodetype == LYS_ANYXML ? "anyxml" : "anydata"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 211 | } else { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 212 | 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] | 213 | } |
| 214 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 215 | case LYXP_NODE_META: |
| 216 | 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] | 217 | set->val.meta[i].meta->value); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 218 | break; |
| 219 | } |
| 220 | } |
| 221 | break; |
| 222 | |
| 223 | case LYXP_SET_SCNODE_SET: |
| 224 | LOGDBG(LY_LDGXPATH, "set SCNODE SET:"); |
| 225 | for (i = 0; i < set->used; ++i) { |
| 226 | sitem = &set->val.scnodes[i]; |
| 227 | |
| 228 | switch (sitem->type) { |
| 229 | case LYXP_NODE_ROOT: |
| 230 | LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT", i + 1, sitem->in_ctx); |
| 231 | break; |
| 232 | case LYXP_NODE_ROOT_CONFIG: |
| 233 | LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT CONFIG", i + 1, sitem->in_ctx); |
| 234 | break; |
| 235 | case LYXP_NODE_ELEM: |
| 236 | LOGDBG(LY_LDGXPATH, "\t%d (%u): ELEM %s", i + 1, sitem->in_ctx, sitem->scnode->name); |
| 237 | break; |
| 238 | default: |
| 239 | LOGINT(NULL); |
| 240 | break; |
| 241 | } |
| 242 | } |
| 243 | break; |
| 244 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 245 | case LYXP_SET_BOOLEAN: |
| 246 | LOGDBG(LY_LDGXPATH, "set BOOLEAN"); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 247 | LOGDBG(LY_LDGXPATH, "\t%s", (set->val.bln ? "true" : "false")); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 248 | break; |
| 249 | |
| 250 | case LYXP_SET_STRING: |
| 251 | LOGDBG(LY_LDGXPATH, "set STRING"); |
| 252 | LOGDBG(LY_LDGXPATH, "\t%s", set->val.str); |
| 253 | break; |
| 254 | |
| 255 | case LYXP_SET_NUMBER: |
| 256 | LOGDBG(LY_LDGXPATH, "set NUMBER"); |
| 257 | |
| 258 | if (isnan(set->val.num)) { |
| 259 | str = strdup("NaN"); |
| 260 | } else if ((set->val.num == 0) || (set->val.num == -0.0f)) { |
| 261 | str = strdup("0"); |
| 262 | } else if (isinf(set->val.num) && !signbit(set->val.num)) { |
| 263 | str = strdup("Infinity"); |
| 264 | } else if (isinf(set->val.num) && signbit(set->val.num)) { |
| 265 | str = strdup("-Infinity"); |
| 266 | } else if ((long long)set->val.num == set->val.num) { |
| 267 | if (asprintf(&str, "%lld", (long long)set->val.num) == -1) { |
| 268 | str = NULL; |
| 269 | } |
| 270 | } else { |
| 271 | if (asprintf(&str, "%03.1Lf", set->val.num) == -1) { |
| 272 | str = NULL; |
| 273 | } |
| 274 | } |
| 275 | LY_CHECK_ERR_RET(!str, LOGMEM(NULL), ); |
| 276 | |
| 277 | LOGDBG(LY_LDGXPATH, "\t%s", str); |
| 278 | free(str); |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | #endif |
| 283 | |
| 284 | /** |
| 285 | * @brief Realloc the string \p str. |
| 286 | * |
| 287 | * @param[in] ctx libyang context for logging. |
| 288 | * @param[in] needed How much free space is required. |
| 289 | * @param[in,out] str Pointer to the string to use. |
| 290 | * @param[in,out] used Used bytes in \p str. |
| 291 | * @param[in,out] size Allocated bytes in \p str. |
| 292 | * @return LY_ERR |
| 293 | */ |
| 294 | static LY_ERR |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 295 | 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] | 296 | { |
| 297 | if (*size - *used < needed) { |
| 298 | do { |
| 299 | if ((UINT16_MAX - *size) < LYXP_STRING_CAST_SIZE_STEP) { |
| 300 | LOGERR(ctx, LY_EINVAL, "XPath string length limit (%u) reached.", UINT16_MAX); |
| 301 | return LY_EINVAL; |
| 302 | } |
| 303 | *size += LYXP_STRING_CAST_SIZE_STEP; |
| 304 | } while (*size - *used < needed); |
| 305 | *str = ly_realloc(*str, *size * sizeof(char)); |
| 306 | LY_CHECK_ERR_RET(!(*str), LOGMEM(ctx), LY_EMEM); |
| 307 | } |
| 308 | |
| 309 | return LY_SUCCESS; |
| 310 | } |
| 311 | |
| 312 | /** |
| 313 | * @brief Cast nodes recursively to one string @p str. |
| 314 | * |
| 315 | * @param[in] node Node to cast. |
| 316 | * @param[in] fake_cont Whether to put the data into a "fake" container. |
| 317 | * @param[in] root_type Type of the XPath root. |
| 318 | * @param[in] indent Current indent. |
| 319 | * @param[in,out] str Resulting string. |
| 320 | * @param[in,out] used Used bytes in @p str. |
| 321 | * @param[in,out] size Allocated bytes in @p str. |
| 322 | * @return LY_ERR |
| 323 | */ |
| 324 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 325 | 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] | 326 | uint16_t *used, uint16_t *size) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 327 | { |
Radek Krejci | 7f769d7 | 2020-07-11 23:13:56 +0200 | [diff] [blame] | 328 | char *buf, *line, *ptr = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 329 | const char *value_str; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 330 | const struct lyd_node *child; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 331 | struct lyd_node *tree; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 332 | struct lyd_node_any *any; |
| 333 | LY_ERR rc; |
| 334 | |
| 335 | if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) { |
| 336 | return LY_SUCCESS; |
| 337 | } |
| 338 | |
| 339 | if (fake_cont) { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 340 | rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 341 | LY_CHECK_RET(rc); |
| 342 | strcpy(*str + (*used - 1), "\n"); |
| 343 | ++(*used); |
| 344 | |
| 345 | ++indent; |
| 346 | } |
| 347 | |
| 348 | switch (node->schema->nodetype) { |
| 349 | case LYS_CONTAINER: |
| 350 | case LYS_LIST: |
| 351 | case LYS_RPC: |
| 352 | case LYS_NOTIF: |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 353 | rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 354 | LY_CHECK_RET(rc); |
| 355 | strcpy(*str + (*used - 1), "\n"); |
| 356 | ++(*used); |
| 357 | |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 358 | for (child = lyd_child(node); child; child = child->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 359 | rc = cast_string_recursive(child, 0, root_type, indent + 1, str, used, size); |
| 360 | LY_CHECK_RET(rc); |
| 361 | } |
| 362 | |
| 363 | break; |
| 364 | |
| 365 | case LYS_LEAF: |
| 366 | case LYS_LEAFLIST: |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 367 | value_str = LYD_CANON_VALUE(node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 368 | |
| 369 | /* print indent */ |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 370 | 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] | 371 | memset(*str + (*used - 1), ' ', indent * 2); |
| 372 | *used += indent * 2; |
| 373 | |
| 374 | /* print value */ |
| 375 | if (*used == 1) { |
| 376 | sprintf(*str + (*used - 1), "%s", value_str); |
| 377 | *used += strlen(value_str); |
| 378 | } else { |
| 379 | sprintf(*str + (*used - 1), "%s\n", value_str); |
| 380 | *used += strlen(value_str) + 1; |
| 381 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 382 | |
| 383 | break; |
| 384 | |
| 385 | case LYS_ANYXML: |
| 386 | case LYS_ANYDATA: |
| 387 | any = (struct lyd_node_any *)node; |
| 388 | if (!(void *)any->value.tree) { |
| 389 | /* no content */ |
| 390 | buf = strdup(""); |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 391 | LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_CTX(node)), LY_EMEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 392 | } else { |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 393 | struct ly_out *out; |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 394 | |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 395 | if (any->value_type == LYD_ANYDATA_LYB) { |
| 396 | /* try to parse it into a data tree */ |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 397 | 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] | 398 | /* successfully parsed */ |
| 399 | free(any->value.mem); |
| 400 | any->value.tree = tree; |
| 401 | any->value_type = LYD_ANYDATA_DATATREE; |
| 402 | } |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 403 | /* 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] | 404 | } |
| 405 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 406 | switch (any->value_type) { |
| 407 | case LYD_ANYDATA_STRING: |
| 408 | case LYD_ANYDATA_XML: |
| 409 | case LYD_ANYDATA_JSON: |
| 410 | buf = strdup(any->value.json); |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 411 | LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_CTX(node)), LY_EMEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 412 | break; |
| 413 | case LYD_ANYDATA_DATATREE: |
Radek Krejci | 84ce7b1 | 2020-06-11 17:28:25 +0200 | [diff] [blame] | 414 | LY_CHECK_RET(ly_out_new_memory(&buf, 0, &out)); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 415 | rc = lyd_print_all(out, any->value.tree, LYD_XML, 0); |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 416 | ly_out_free(out, NULL, 0); |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 417 | LY_CHECK_RET(rc < 0, -rc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 418 | break; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 419 | case LYD_ANYDATA_LYB: |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 420 | LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot convert LYB anydata into string."); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 421 | return LY_EINVAL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 422 | } |
| 423 | } |
| 424 | |
| 425 | line = strtok_r(buf, "\n", &ptr); |
| 426 | do { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 427 | 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] | 428 | if (rc != LY_SUCCESS) { |
| 429 | free(buf); |
| 430 | return rc; |
| 431 | } |
| 432 | memset(*str + (*used - 1), ' ', indent * 2); |
| 433 | *used += indent * 2; |
| 434 | |
| 435 | strcpy(*str + (*used - 1), line); |
| 436 | *used += strlen(line); |
| 437 | |
| 438 | strcpy(*str + (*used - 1), "\n"); |
| 439 | *used += 1; |
| 440 | } while ((line = strtok_r(NULL, "\n", &ptr))); |
| 441 | |
| 442 | free(buf); |
| 443 | break; |
| 444 | |
| 445 | default: |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 446 | LOGINT_RET(LYD_CTX(node)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | if (fake_cont) { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 450 | rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 451 | LY_CHECK_RET(rc); |
| 452 | strcpy(*str + (*used - 1), "\n"); |
| 453 | ++(*used); |
| 454 | |
| 455 | --indent; |
| 456 | } |
| 457 | |
| 458 | return LY_SUCCESS; |
| 459 | } |
| 460 | |
| 461 | /** |
| 462 | * @brief Cast an element into a string. |
| 463 | * |
| 464 | * @param[in] node Node to cast. |
| 465 | * @param[in] fake_cont Whether to put the data into a "fake" container. |
| 466 | * @param[in] root_type Type of the XPath root. |
| 467 | * @param[out] str Element cast to dynamically-allocated string. |
| 468 | * @return LY_ERR |
| 469 | */ |
| 470 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 471 | 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] | 472 | { |
| 473 | uint16_t used, size; |
| 474 | LY_ERR rc; |
| 475 | |
| 476 | *str = malloc(LYXP_STRING_CAST_SIZE_START * sizeof(char)); |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 477 | LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_CTX(node)), LY_EMEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 478 | (*str)[0] = '\0'; |
| 479 | used = 1; |
| 480 | size = LYXP_STRING_CAST_SIZE_START; |
| 481 | |
| 482 | rc = cast_string_recursive(node, fake_cont, root_type, 0, str, &used, &size); |
| 483 | if (rc != LY_SUCCESS) { |
| 484 | free(*str); |
| 485 | return rc; |
| 486 | } |
| 487 | |
| 488 | if (size > used) { |
| 489 | *str = ly_realloc(*str, used * sizeof(char)); |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 490 | LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_CTX(node)), LY_EMEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 491 | } |
| 492 | return LY_SUCCESS; |
| 493 | } |
| 494 | |
| 495 | /** |
| 496 | * @brief Cast a LYXP_SET_NODE_SET set into a string. |
| 497 | * Context position aware. |
| 498 | * |
| 499 | * @param[in] set Set to cast. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 500 | * @param[out] str Cast dynamically-allocated string. |
| 501 | * @return LY_ERR |
| 502 | */ |
| 503 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 504 | cast_node_set_to_string(struct lyxp_set *set, char **str) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 505 | { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 506 | switch (set->val.nodes[0].type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 507 | case LYXP_NODE_NONE: |
| 508 | /* invalid */ |
| 509 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 510 | case LYXP_NODE_ROOT: |
| 511 | case LYXP_NODE_ROOT_CONFIG: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 512 | 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] | 513 | case LYXP_NODE_ELEM: |
| 514 | case LYXP_NODE_TEXT: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 515 | 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] | 516 | case LYXP_NODE_META: |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 517 | *str = strdup(set->val.meta[0].meta->value.canonical); |
| 518 | if (!*str) { |
| 519 | LOGMEM_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 520 | } |
| 521 | return LY_SUCCESS; |
| 522 | } |
| 523 | |
| 524 | LOGINT_RET(set->ctx); |
| 525 | } |
| 526 | |
| 527 | /** |
| 528 | * @brief Cast a string into an XPath number. |
| 529 | * |
| 530 | * @param[in] str String to use. |
| 531 | * @return Cast number. |
| 532 | */ |
| 533 | static long double |
| 534 | cast_string_to_number(const char *str) |
| 535 | { |
| 536 | long double num; |
| 537 | char *ptr; |
| 538 | |
| 539 | errno = 0; |
| 540 | num = strtold(str, &ptr); |
| 541 | if (errno || *ptr) { |
| 542 | num = NAN; |
| 543 | } |
| 544 | return num; |
| 545 | } |
| 546 | |
| 547 | /** |
| 548 | * @brief Callback for checking value equality. |
| 549 | * |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 550 | * Implementation of ::values_equal_cb. |
| 551 | * |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 552 | * @param[in] val1_p First value. |
| 553 | * @param[in] val2_p Second value. |
| 554 | * @param[in] mod Whether hash table is being modified. |
| 555 | * @param[in] cb_data Callback data. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 556 | * @return Boolean value whether values are equal or not. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 557 | */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 558 | static ly_bool |
| 559 | 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] | 560 | { |
| 561 | struct lyxp_set_hash_node *val1, *val2; |
| 562 | |
| 563 | val1 = (struct lyxp_set_hash_node *)val1_p; |
| 564 | val2 = (struct lyxp_set_hash_node *)val2_p; |
| 565 | |
| 566 | if ((val1->node == val2->node) && (val1->type == val2->type)) { |
| 567 | return 1; |
| 568 | } |
| 569 | |
| 570 | return 0; |
| 571 | } |
| 572 | |
| 573 | /** |
| 574 | * @brief Insert node and its hash into set. |
| 575 | * |
| 576 | * @param[in] set et to insert to. |
| 577 | * @param[in] node Node with hash. |
| 578 | * @param[in] type Node type. |
| 579 | */ |
| 580 | static void |
| 581 | set_insert_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type) |
| 582 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 583 | LY_ERR r; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 584 | uint32_t i, hash; |
| 585 | struct lyxp_set_hash_node hnode; |
| 586 | |
| 587 | if (!set->ht && (set->used >= LYD_HT_MIN_ITEMS)) { |
| 588 | /* create hash table and add all the nodes */ |
| 589 | set->ht = lyht_new(1, sizeof(struct lyxp_set_hash_node), set_values_equal_cb, NULL, 1); |
| 590 | for (i = 0; i < set->used; ++i) { |
| 591 | hnode.node = set->val.nodes[i].node; |
| 592 | hnode.type = set->val.nodes[i].type; |
| 593 | |
| 594 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 595 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 596 | hash = dict_hash_multi(hash, NULL, 0); |
| 597 | |
| 598 | r = lyht_insert(set->ht, &hnode, hash, NULL); |
| 599 | assert(!r); |
| 600 | (void)r; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 601 | |
Michal Vasko | 9d6befd | 2019-12-11 14:56:56 +0100 | [diff] [blame] | 602 | if (hnode.node == node) { |
| 603 | /* it was just added, do not add it twice */ |
| 604 | node = NULL; |
| 605 | } |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | if (set->ht && node) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 610 | /* add the new node into hash table */ |
| 611 | hnode.node = node; |
| 612 | hnode.type = type; |
| 613 | |
| 614 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 615 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 616 | hash = dict_hash_multi(hash, NULL, 0); |
| 617 | |
| 618 | r = lyht_insert(set->ht, &hnode, hash, NULL); |
| 619 | assert(!r); |
| 620 | (void)r; |
| 621 | } |
| 622 | } |
| 623 | |
| 624 | /** |
| 625 | * @brief Remove node and its hash from set. |
| 626 | * |
| 627 | * @param[in] set Set to remove from. |
| 628 | * @param[in] node Node to remove. |
| 629 | * @param[in] type Node type. |
| 630 | */ |
| 631 | static void |
| 632 | set_remove_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type) |
| 633 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 634 | LY_ERR r; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 635 | struct lyxp_set_hash_node hnode; |
| 636 | uint32_t hash; |
| 637 | |
| 638 | if (set->ht) { |
| 639 | hnode.node = node; |
| 640 | hnode.type = type; |
| 641 | |
| 642 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 643 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 644 | hash = dict_hash_multi(hash, NULL, 0); |
| 645 | |
| 646 | r = lyht_remove(set->ht, &hnode, hash); |
| 647 | assert(!r); |
| 648 | (void)r; |
| 649 | |
| 650 | if (!set->ht->used) { |
| 651 | lyht_free(set->ht); |
| 652 | set->ht = NULL; |
| 653 | } |
| 654 | } |
| 655 | } |
| 656 | |
| 657 | /** |
| 658 | * @brief Check whether node is in set based on its hash. |
| 659 | * |
| 660 | * @param[in] set Set to search in. |
| 661 | * @param[in] node Node to search for. |
| 662 | * @param[in] type Node type. |
| 663 | * @param[in] skip_idx Index in @p set to skip. |
| 664 | * @return LY_ERR |
| 665 | */ |
| 666 | static LY_ERR |
| 667 | set_dup_node_hash_check(const struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type, int skip_idx) |
| 668 | { |
| 669 | struct lyxp_set_hash_node hnode, *match_p; |
| 670 | uint32_t hash; |
| 671 | |
| 672 | hnode.node = node; |
| 673 | hnode.type = type; |
| 674 | |
| 675 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 676 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 677 | hash = dict_hash_multi(hash, NULL, 0); |
| 678 | |
| 679 | if (!lyht_find(set->ht, &hnode, hash, (void **)&match_p)) { |
| 680 | if ((skip_idx > -1) && (set->val.nodes[skip_idx].node == match_p->node) && (set->val.nodes[skip_idx].type == match_p->type)) { |
| 681 | /* we found it on the index that should be skipped, find another */ |
| 682 | hnode = *match_p; |
| 683 | if (lyht_find_next(set->ht, &hnode, hash, (void **)&match_p)) { |
| 684 | /* none other found */ |
| 685 | return LY_SUCCESS; |
| 686 | } |
| 687 | } |
| 688 | |
| 689 | return LY_EEXIST; |
| 690 | } |
| 691 | |
| 692 | /* not found */ |
| 693 | return LY_SUCCESS; |
| 694 | } |
| 695 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 696 | void |
| 697 | lyxp_set_free_content(struct lyxp_set *set) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 698 | { |
| 699 | if (!set) { |
| 700 | return; |
| 701 | } |
| 702 | |
| 703 | if (set->type == LYXP_SET_NODE_SET) { |
| 704 | free(set->val.nodes); |
| 705 | lyht_free(set->ht); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 706 | } else if (set->type == LYXP_SET_SCNODE_SET) { |
| 707 | free(set->val.scnodes); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 708 | lyht_free(set->ht); |
| 709 | } else { |
| 710 | if (set->type == LYXP_SET_STRING) { |
| 711 | free(set->val.str); |
| 712 | } |
| 713 | set->type = LYXP_SET_NODE_SET; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 714 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 715 | |
| 716 | set->val.nodes = NULL; |
| 717 | set->used = 0; |
| 718 | set->size = 0; |
| 719 | set->ht = NULL; |
| 720 | set->ctx_pos = 0; |
| 721 | set->ctx_pos = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 722 | } |
| 723 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 724 | /** |
| 725 | * @brief Free dynamically-allocated set. |
| 726 | * |
| 727 | * @param[in] set Set to free. |
| 728 | */ |
| 729 | static void |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 730 | lyxp_set_free(struct lyxp_set *set) |
| 731 | { |
| 732 | if (!set) { |
| 733 | return; |
| 734 | } |
| 735 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 736 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 737 | free(set); |
| 738 | } |
| 739 | |
| 740 | /** |
| 741 | * @brief Initialize set context. |
| 742 | * |
| 743 | * @param[in] new Set to initialize. |
| 744 | * @param[in] set Arbitrary initialized set. |
| 745 | */ |
| 746 | static void |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 747 | set_init(struct lyxp_set *new, const struct lyxp_set *set) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 748 | { |
| 749 | memset(new, 0, sizeof *new); |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 750 | if (set) { |
| 751 | new->ctx = set->ctx; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 752 | new->cur_node = set->cur_node; |
Michal Vasko | 588112f | 2019-12-10 14:51:53 +0100 | [diff] [blame] | 753 | new->root_type = set->root_type; |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 754 | new->context_op = set->context_op; |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 755 | new->tree = set->tree; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 756 | new->cur_mod = set->cur_mod; |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 757 | new->format = set->format; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 758 | new->prefix_data = set->prefix_data; |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 759 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | /** |
| 763 | * @brief Create a deep copy of a set. |
| 764 | * |
| 765 | * @param[in] set Set to copy. |
| 766 | * @return Copy of @p set. |
| 767 | */ |
| 768 | static struct lyxp_set * |
| 769 | set_copy(struct lyxp_set *set) |
| 770 | { |
| 771 | struct lyxp_set *ret; |
| 772 | uint16_t i; |
| 773 | |
| 774 | if (!set) { |
| 775 | return NULL; |
| 776 | } |
| 777 | |
| 778 | ret = malloc(sizeof *ret); |
| 779 | LY_CHECK_ERR_RET(!ret, LOGMEM(set->ctx), NULL); |
| 780 | set_init(ret, set); |
| 781 | |
| 782 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 783 | ret->type = set->type; |
| 784 | |
| 785 | for (i = 0; i < set->used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 786 | if ((set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) || |
| 787 | (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START)) { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 788 | uint32_t idx; |
| 789 | LY_CHECK_ERR_RET(lyxp_set_scnode_insert_node(ret, set->val.scnodes[i].scnode, set->val.scnodes[i].type, &idx), |
| 790 | lyxp_set_free(ret), NULL); |
Michal Vasko | 3f27c52 | 2020-01-06 08:37:49 +0100 | [diff] [blame] | 791 | /* coverity seems to think scnodes can be NULL */ |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 792 | if (!ret->val.scnodes) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 793 | lyxp_set_free(ret); |
| 794 | return NULL; |
| 795 | } |
Michal Vasko | ba71654 | 2019-12-16 10:01:58 +0100 | [diff] [blame] | 796 | ret->val.scnodes[idx].in_ctx = set->val.scnodes[i].in_ctx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 797 | } |
| 798 | } |
| 799 | } else if (set->type == LYXP_SET_NODE_SET) { |
| 800 | ret->type = set->type; |
| 801 | ret->val.nodes = malloc(set->used * sizeof *ret->val.nodes); |
| 802 | LY_CHECK_ERR_RET(!ret->val.nodes, LOGMEM(set->ctx); free(ret), NULL); |
| 803 | memcpy(ret->val.nodes, set->val.nodes, set->used * sizeof *ret->val.nodes); |
| 804 | |
| 805 | ret->used = ret->size = set->used; |
| 806 | ret->ctx_pos = set->ctx_pos; |
| 807 | ret->ctx_size = set->ctx_size; |
| 808 | ret->ht = lyht_dup(set->ht); |
| 809 | } else { |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 810 | memcpy(ret, set, sizeof *ret); |
| 811 | if (set->type == LYXP_SET_STRING) { |
| 812 | ret->val.str = strdup(set->val.str); |
| 813 | LY_CHECK_ERR_RET(!ret->val.str, LOGMEM(set->ctx); free(ret), NULL); |
| 814 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 815 | } |
| 816 | |
| 817 | return ret; |
| 818 | } |
| 819 | |
| 820 | /** |
| 821 | * @brief Fill XPath set with a string. Any current data are disposed of. |
| 822 | * |
| 823 | * @param[in] set Set to fill. |
| 824 | * @param[in] string String to fill into \p set. |
| 825 | * @param[in] str_len Length of \p string. 0 is a valid value! |
| 826 | */ |
| 827 | static void |
| 828 | set_fill_string(struct lyxp_set *set, const char *string, uint16_t str_len) |
| 829 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 830 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 831 | |
| 832 | set->type = LYXP_SET_STRING; |
| 833 | if ((str_len == 0) && (string[0] != '\0')) { |
| 834 | string = ""; |
| 835 | } |
| 836 | set->val.str = strndup(string, str_len); |
| 837 | } |
| 838 | |
| 839 | /** |
| 840 | * @brief Fill XPath set with a number. Any current data are disposed of. |
| 841 | * |
| 842 | * @param[in] set Set to fill. |
| 843 | * @param[in] number Number to fill into \p set. |
| 844 | */ |
| 845 | static void |
| 846 | set_fill_number(struct lyxp_set *set, long double number) |
| 847 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 848 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 849 | |
| 850 | set->type = LYXP_SET_NUMBER; |
| 851 | set->val.num = number; |
| 852 | } |
| 853 | |
| 854 | /** |
| 855 | * @brief Fill XPath set with a boolean. Any current data are disposed of. |
| 856 | * |
| 857 | * @param[in] set Set to fill. |
| 858 | * @param[in] boolean Boolean to fill into \p set. |
| 859 | */ |
| 860 | static void |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 861 | set_fill_boolean(struct lyxp_set *set, ly_bool boolean) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 862 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 863 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 864 | |
| 865 | set->type = LYXP_SET_BOOLEAN; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 866 | set->val.bln = boolean; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 867 | } |
| 868 | |
| 869 | /** |
| 870 | * @brief Fill XPath set with the value from another set (deep assign). |
| 871 | * Any current data are disposed of. |
| 872 | * |
| 873 | * @param[in] trg Set to fill. |
| 874 | * @param[in] src Source set to copy into \p trg. |
| 875 | */ |
| 876 | static void |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 877 | set_fill_set(struct lyxp_set *trg, const struct lyxp_set *src) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 878 | { |
| 879 | if (!trg || !src) { |
| 880 | return; |
| 881 | } |
| 882 | |
| 883 | if (trg->type == LYXP_SET_NODE_SET) { |
| 884 | free(trg->val.nodes); |
| 885 | } else if (trg->type == LYXP_SET_STRING) { |
| 886 | free(trg->val.str); |
| 887 | } |
| 888 | set_init(trg, src); |
| 889 | |
| 890 | if (src->type == LYXP_SET_SCNODE_SET) { |
| 891 | trg->type = LYXP_SET_SCNODE_SET; |
| 892 | trg->used = src->used; |
| 893 | trg->size = src->used; |
| 894 | |
| 895 | trg->val.scnodes = ly_realloc(trg->val.scnodes, trg->size * sizeof *trg->val.scnodes); |
| 896 | LY_CHECK_ERR_RET(!trg->val.scnodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), ); |
| 897 | memcpy(trg->val.scnodes, src->val.scnodes, src->used * sizeof *src->val.scnodes); |
| 898 | } else if (src->type == LYXP_SET_BOOLEAN) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 899 | set_fill_boolean(trg, src->val.bln); |
Michal Vasko | 44f3d2c | 2020-08-24 09:49:38 +0200 | [diff] [blame] | 900 | } else if (src->type == LYXP_SET_NUMBER) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 901 | set_fill_number(trg, src->val.num); |
| 902 | } else if (src->type == LYXP_SET_STRING) { |
| 903 | set_fill_string(trg, src->val.str, strlen(src->val.str)); |
| 904 | } else { |
| 905 | if (trg->type == LYXP_SET_NODE_SET) { |
| 906 | free(trg->val.nodes); |
| 907 | } else if (trg->type == LYXP_SET_STRING) { |
| 908 | free(trg->val.str); |
| 909 | } |
| 910 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 911 | assert(src->type == LYXP_SET_NODE_SET); |
| 912 | |
| 913 | trg->type = LYXP_SET_NODE_SET; |
| 914 | trg->used = src->used; |
| 915 | trg->size = src->used; |
| 916 | trg->ctx_pos = src->ctx_pos; |
| 917 | trg->ctx_size = src->ctx_size; |
| 918 | |
| 919 | trg->val.nodes = malloc(trg->used * sizeof *trg->val.nodes); |
| 920 | LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), ); |
| 921 | memcpy(trg->val.nodes, src->val.nodes, src->used * sizeof *src->val.nodes); |
| 922 | if (src->ht) { |
| 923 | trg->ht = lyht_dup(src->ht); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 924 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 925 | trg->ht = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 926 | } |
| 927 | } |
| 928 | } |
| 929 | |
| 930 | /** |
| 931 | * @brief Clear context of all schema nodes. |
| 932 | * |
| 933 | * @param[in] set Set to clear. |
| 934 | */ |
| 935 | static void |
| 936 | set_scnode_clear_ctx(struct lyxp_set *set) |
| 937 | { |
| 938 | uint32_t i; |
| 939 | |
| 940 | for (i = 0; i < set->used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 941 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
| 942 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM; |
| 943 | } else if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START) { |
| 944 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 945 | } |
| 946 | } |
| 947 | } |
| 948 | |
| 949 | /** |
| 950 | * @brief Remove a node from a set. Removing last node changes |
| 951 | * set into LYXP_SET_EMPTY. Context position aware. |
| 952 | * |
| 953 | * @param[in] set Set to use. |
| 954 | * @param[in] idx Index from @p set of the node to be removed. |
| 955 | */ |
| 956 | static void |
| 957 | set_remove_node(struct lyxp_set *set, uint32_t idx) |
| 958 | { |
| 959 | assert(set && (set->type == LYXP_SET_NODE_SET)); |
| 960 | assert(idx < set->used); |
| 961 | |
| 962 | set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
| 963 | |
| 964 | --set->used; |
| 965 | if (set->used) { |
| 966 | memmove(&set->val.nodes[idx], &set->val.nodes[idx + 1], |
| 967 | (set->used - idx) * sizeof *set->val.nodes); |
| 968 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 969 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 970 | } |
| 971 | } |
| 972 | |
| 973 | /** |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 974 | * @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] | 975 | * |
| 976 | * @param[in] set Set to use. |
| 977 | * @param[in] idx Index from @p set of the node to be removed. |
| 978 | */ |
| 979 | static void |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 980 | set_remove_node_none(struct lyxp_set *set, uint32_t idx) |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 981 | { |
| 982 | assert(set && (set->type == LYXP_SET_NODE_SET)); |
| 983 | assert(idx < set->used); |
| 984 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 985 | if (set->val.nodes[idx].type == LYXP_NODE_ELEM) { |
| 986 | set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
| 987 | } |
| 988 | set->val.nodes[idx].type = LYXP_NODE_NONE; |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 989 | } |
| 990 | |
| 991 | /** |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 992 | * @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] | 993 | * set into LYXP_SET_EMPTY. Context position aware. |
| 994 | * |
| 995 | * @param[in] set Set to consolidate. |
| 996 | */ |
| 997 | static void |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 998 | set_remove_nodes_none(struct lyxp_set *set) |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 999 | { |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 1000 | uint16_t i, orig_used, end = 0; |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1001 | int32_t start; |
| 1002 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1003 | assert(set); |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1004 | |
| 1005 | orig_used = set->used; |
| 1006 | set->used = 0; |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 1007 | for (i = 0; i < orig_used; ) { |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1008 | start = -1; |
| 1009 | do { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1010 | if ((set->val.nodes[i].type != LYXP_NODE_NONE) && (start == -1)) { |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1011 | start = i; |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1012 | } else if ((start > -1) && (set->val.nodes[i].type == LYXP_NODE_NONE)) { |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1013 | end = i; |
| 1014 | ++i; |
| 1015 | break; |
| 1016 | } |
| 1017 | |
| 1018 | ++i; |
| 1019 | if (i == orig_used) { |
| 1020 | end = i; |
| 1021 | } |
| 1022 | } while (i < orig_used); |
| 1023 | |
| 1024 | if (start > -1) { |
| 1025 | /* move the whole chunk of valid nodes together */ |
| 1026 | if (set->used != (unsigned)start) { |
| 1027 | memmove(&set->val.nodes[set->used], &set->val.nodes[start], (end - start) * sizeof *set->val.nodes); |
| 1028 | } |
| 1029 | set->used += end - start; |
| 1030 | } |
| 1031 | } |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1032 | } |
| 1033 | |
| 1034 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1035 | * @brief Check for duplicates in a node set. |
| 1036 | * |
| 1037 | * @param[in] set Set to check. |
| 1038 | * @param[in] node Node to look for in @p set. |
| 1039 | * @param[in] node_type Type of @p node. |
| 1040 | * @param[in] skip_idx Index from @p set to skip. |
| 1041 | * @return LY_ERR |
| 1042 | */ |
| 1043 | static LY_ERR |
| 1044 | set_dup_node_check(const struct lyxp_set *set, const struct lyd_node *node, enum lyxp_node_type node_type, int skip_idx) |
| 1045 | { |
| 1046 | uint32_t i; |
| 1047 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1048 | if (set->ht && node) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1049 | return set_dup_node_hash_check(set, (struct lyd_node *)node, node_type, skip_idx); |
| 1050 | } |
| 1051 | |
| 1052 | for (i = 0; i < set->used; ++i) { |
| 1053 | if ((skip_idx > -1) && (i == (unsigned)skip_idx)) { |
| 1054 | continue; |
| 1055 | } |
| 1056 | |
| 1057 | if ((set->val.nodes[i].node == node) && (set->val.nodes[i].type == node_type)) { |
| 1058 | return LY_EEXIST; |
| 1059 | } |
| 1060 | } |
| 1061 | |
| 1062 | return LY_SUCCESS; |
| 1063 | } |
| 1064 | |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 1065 | ly_bool |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1066 | lyxp_set_scnode_contains(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, int skip_idx, |
| 1067 | uint32_t *index_p) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1068 | { |
| 1069 | uint32_t i; |
| 1070 | |
| 1071 | for (i = 0; i < set->used; ++i) { |
| 1072 | if ((skip_idx > -1) && (i == (unsigned)skip_idx)) { |
| 1073 | continue; |
| 1074 | } |
| 1075 | |
| 1076 | 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] | 1077 | if (index_p) { |
| 1078 | *index_p = i; |
| 1079 | } |
| 1080 | return 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1081 | } |
| 1082 | } |
| 1083 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1084 | return 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1085 | } |
| 1086 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 1087 | void |
| 1088 | lyxp_set_scnode_merge(struct lyxp_set *set1, struct lyxp_set *set2) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1089 | { |
| 1090 | uint32_t orig_used, i, j; |
| 1091 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1092 | 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] | 1093 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1094 | if (!set2->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1095 | return; |
| 1096 | } |
| 1097 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1098 | if (!set1->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1099 | memcpy(set1, set2, sizeof *set1); |
| 1100 | return; |
| 1101 | } |
| 1102 | |
| 1103 | if (set1->used + set2->used > set1->size) { |
| 1104 | set1->size = set1->used + set2->used; |
| 1105 | set1->val.scnodes = ly_realloc(set1->val.scnodes, set1->size * sizeof *set1->val.scnodes); |
| 1106 | LY_CHECK_ERR_RET(!set1->val.scnodes, LOGMEM(set1->ctx), ); |
| 1107 | } |
| 1108 | |
| 1109 | orig_used = set1->used; |
| 1110 | |
| 1111 | for (i = 0; i < set2->used; ++i) { |
| 1112 | for (j = 0; j < orig_used; ++j) { |
| 1113 | /* detect duplicities */ |
| 1114 | if (set1->val.scnodes[j].scnode == set2->val.scnodes[i].scnode) { |
| 1115 | break; |
| 1116 | } |
| 1117 | } |
| 1118 | |
Michal Vasko | 0587bce | 2020-12-10 12:19:21 +0100 | [diff] [blame] | 1119 | if (j < orig_used) { |
| 1120 | /* node is there, but update its status if needed */ |
| 1121 | if (set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_START_USED) { |
| 1122 | set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx; |
| 1123 | } |
| 1124 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1125 | memcpy(&set1->val.scnodes[set1->used], &set2->val.scnodes[i], sizeof *set2->val.scnodes); |
| 1126 | ++set1->used; |
| 1127 | } |
| 1128 | } |
| 1129 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1130 | lyxp_set_free_content(set2); |
| 1131 | set2->type = LYXP_SET_SCNODE_SET; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1132 | } |
| 1133 | |
| 1134 | /** |
| 1135 | * @brief Insert a node into a set. Context position aware. |
| 1136 | * |
| 1137 | * @param[in] set Set to use. |
| 1138 | * @param[in] node Node to insert to @p set. |
| 1139 | * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting. |
| 1140 | * @param[in] node_type Node type of @p node. |
| 1141 | * @param[in] idx Index in @p set to insert into. |
| 1142 | */ |
| 1143 | static void |
| 1144 | set_insert_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx) |
| 1145 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1146 | assert(set && (set->type == LYXP_SET_NODE_SET)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1147 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1148 | if (!set->size) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1149 | /* first item */ |
| 1150 | if (idx) { |
| 1151 | /* no real harm done, but it is a bug */ |
| 1152 | LOGINT(set->ctx); |
| 1153 | idx = 0; |
| 1154 | } |
| 1155 | set->val.nodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.nodes); |
| 1156 | LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), ); |
| 1157 | set->type = LYXP_SET_NODE_SET; |
| 1158 | set->used = 0; |
| 1159 | set->size = LYXP_SET_SIZE_START; |
| 1160 | set->ctx_pos = 1; |
| 1161 | set->ctx_size = 1; |
| 1162 | set->ht = NULL; |
| 1163 | } else { |
| 1164 | /* not an empty set */ |
| 1165 | if (set->used == set->size) { |
| 1166 | |
| 1167 | /* set is full */ |
| 1168 | set->val.nodes = ly_realloc(set->val.nodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.nodes); |
| 1169 | LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), ); |
| 1170 | set->size += LYXP_SET_SIZE_STEP; |
| 1171 | } |
| 1172 | |
| 1173 | if (idx > set->used) { |
| 1174 | LOGINT(set->ctx); |
| 1175 | idx = set->used; |
| 1176 | } |
| 1177 | |
| 1178 | /* make space for the new node */ |
| 1179 | if (idx < set->used) { |
| 1180 | memmove(&set->val.nodes[idx + 1], &set->val.nodes[idx], (set->used - idx) * sizeof *set->val.nodes); |
| 1181 | } |
| 1182 | } |
| 1183 | |
| 1184 | /* finally assign the value */ |
| 1185 | set->val.nodes[idx].node = (struct lyd_node *)node; |
| 1186 | set->val.nodes[idx].type = node_type; |
| 1187 | set->val.nodes[idx].pos = pos; |
| 1188 | ++set->used; |
| 1189 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1190 | if (set->val.nodes[idx].type == LYXP_NODE_ELEM) { |
| 1191 | set_insert_node_hash(set, (struct lyd_node *)node, node_type); |
| 1192 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1193 | } |
| 1194 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1195 | LY_ERR |
| 1196 | 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] | 1197 | { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1198 | uint32_t index; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1199 | |
| 1200 | assert(set->type == LYXP_SET_SCNODE_SET); |
| 1201 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1202 | if (lyxp_set_scnode_contains(set, node, node_type, -1, &index)) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1203 | set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1204 | } else { |
| 1205 | if (set->used == set->size) { |
| 1206 | 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] | 1207 | LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), LY_EMEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1208 | set->size += LYXP_SET_SIZE_STEP; |
| 1209 | } |
| 1210 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1211 | index = set->used; |
| 1212 | set->val.scnodes[index].scnode = (struct lysc_node *)node; |
| 1213 | set->val.scnodes[index].type = node_type; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1214 | set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1215 | ++set->used; |
| 1216 | } |
| 1217 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1218 | if (index_p) { |
| 1219 | *index_p = index; |
| 1220 | } |
| 1221 | |
| 1222 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1223 | } |
| 1224 | |
| 1225 | /** |
| 1226 | * @brief Replace a node in a set with another. Context position aware. |
| 1227 | * |
| 1228 | * @param[in] set Set to use. |
| 1229 | * @param[in] node Node to insert to @p set. |
| 1230 | * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting. |
| 1231 | * @param[in] node_type Node type of @p node. |
| 1232 | * @param[in] idx Index in @p set of the node to replace. |
| 1233 | */ |
| 1234 | static void |
| 1235 | set_replace_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx) |
| 1236 | { |
| 1237 | assert(set && (idx < set->used)); |
| 1238 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1239 | if (set->val.nodes[idx].type == LYXP_NODE_ELEM) { |
| 1240 | set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
| 1241 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1242 | set->val.nodes[idx].node = (struct lyd_node *)node; |
| 1243 | set->val.nodes[idx].type = node_type; |
| 1244 | set->val.nodes[idx].pos = pos; |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1245 | if (set->val.nodes[idx].type == LYXP_NODE_ELEM) { |
| 1246 | set_insert_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
| 1247 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1248 | } |
| 1249 | |
| 1250 | /** |
| 1251 | * @brief Set all nodes with ctx 1 to a new unique context value. |
| 1252 | * |
| 1253 | * @param[in] set Set to modify. |
| 1254 | * @return New context value. |
| 1255 | */ |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 1256 | static int32_t |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1257 | set_scnode_new_in_ctx(struct lyxp_set *set) |
| 1258 | { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 1259 | uint32_t i; |
| 1260 | int32_t ret_ctx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1261 | |
| 1262 | assert(set->type == LYXP_SET_SCNODE_SET); |
| 1263 | |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1264 | ret_ctx = LYXP_SET_SCNODE_ATOM_PRED_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1265 | retry: |
| 1266 | for (i = 0; i < set->used; ++i) { |
| 1267 | if (set->val.scnodes[i].in_ctx >= ret_ctx) { |
| 1268 | ret_ctx = set->val.scnodes[i].in_ctx + 1; |
| 1269 | goto retry; |
| 1270 | } |
| 1271 | } |
| 1272 | for (i = 0; i < set->used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1273 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1274 | set->val.scnodes[i].in_ctx = ret_ctx; |
| 1275 | } |
| 1276 | } |
| 1277 | |
| 1278 | return ret_ctx; |
| 1279 | } |
| 1280 | |
| 1281 | /** |
| 1282 | * @brief Get unique @p node position in the data. |
| 1283 | * |
| 1284 | * @param[in] node Node to find. |
| 1285 | * @param[in] node_type Node type of @p node. |
| 1286 | * @param[in] root Root node. |
| 1287 | * @param[in] root_type Type of the XPath @p root node. |
| 1288 | * @param[in] prev Node that we think is before @p node in DFS from @p root. Can optionally |
| 1289 | * be used to increase efficiency and start the DFS from this node. |
| 1290 | * @param[in] prev_pos Node @p prev position. Optional, but must be set if @p prev is set. |
| 1291 | * @return Node position. |
| 1292 | */ |
| 1293 | static uint32_t |
| 1294 | 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] | 1295 | 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] | 1296 | { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1297 | const struct lyd_node *elem = NULL, *top_sibling; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1298 | uint32_t pos = 1; |
Michal Vasko | fb6c9dd | 2020-11-18 18:18:47 +0100 | [diff] [blame] | 1299 | ly_bool found = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1300 | |
| 1301 | assert(prev && prev_pos && !root->prev->next); |
| 1302 | |
| 1303 | if ((node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ROOT_CONFIG)) { |
| 1304 | return 0; |
| 1305 | } |
| 1306 | |
| 1307 | if (*prev) { |
| 1308 | /* start from the previous element instead from the root */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1309 | pos = *prev_pos; |
Michal Vasko | fb6c9dd | 2020-11-18 18:18:47 +0100 | [diff] [blame] | 1310 | 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] | 1311 | goto dfs_search; |
| 1312 | } |
| 1313 | |
Michal Vasko | fb6c9dd | 2020-11-18 18:18:47 +0100 | [diff] [blame] | 1314 | LY_LIST_FOR(root, top_sibling) { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1315 | LYD_TREE_DFS_BEGIN(top_sibling, elem) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1316 | dfs_search: |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1317 | if (*prev && !elem) { |
| 1318 | /* resume previous DFS */ |
| 1319 | elem = LYD_TREE_DFS_next = (struct lyd_node *)*prev; |
| 1320 | LYD_TREE_DFS_continue = 0; |
| 1321 | } |
| 1322 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1323 | 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] | 1324 | /* skip */ |
| 1325 | LYD_TREE_DFS_continue = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1326 | } else { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1327 | if (elem == node) { |
Michal Vasko | fb6c9dd | 2020-11-18 18:18:47 +0100 | [diff] [blame] | 1328 | found = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1329 | break; |
| 1330 | } |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1331 | ++pos; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1332 | } |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1333 | |
| 1334 | LYD_TREE_DFS_END(top_sibling, elem); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1335 | } |
| 1336 | |
| 1337 | /* node found */ |
Michal Vasko | fb6c9dd | 2020-11-18 18:18:47 +0100 | [diff] [blame] | 1338 | if (found) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1339 | break; |
| 1340 | } |
| 1341 | } |
| 1342 | |
Michal Vasko | fb6c9dd | 2020-11-18 18:18:47 +0100 | [diff] [blame] | 1343 | if (!found) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1344 | if (!(*prev)) { |
| 1345 | /* we went from root and failed to find it, cannot be */ |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1346 | LOGINT(LYD_CTX(node)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1347 | return 0; |
| 1348 | } else { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1349 | /* start the search again from the beginning */ |
| 1350 | *prev = root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1351 | |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1352 | top_sibling = root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1353 | pos = 1; |
| 1354 | goto dfs_search; |
| 1355 | } |
| 1356 | } |
| 1357 | |
| 1358 | /* remember the last found node for next time */ |
| 1359 | *prev = node; |
| 1360 | *prev_pos = pos; |
| 1361 | |
| 1362 | return pos; |
| 1363 | } |
| 1364 | |
| 1365 | /** |
| 1366 | * @brief Assign (fill) missing node positions. |
| 1367 | * |
| 1368 | * @param[in] set Set to fill positions in. |
| 1369 | * @param[in] root Context root node. |
| 1370 | * @param[in] root_type Context root type. |
| 1371 | * @return LY_ERR |
| 1372 | */ |
| 1373 | static LY_ERR |
| 1374 | set_assign_pos(struct lyxp_set *set, const struct lyd_node *root, enum lyxp_node_type root_type) |
| 1375 | { |
| 1376 | const struct lyd_node *prev = NULL, *tmp_node; |
| 1377 | uint32_t i, tmp_pos = 0; |
| 1378 | |
| 1379 | for (i = 0; i < set->used; ++i) { |
| 1380 | if (!set->val.nodes[i].pos) { |
| 1381 | tmp_node = NULL; |
| 1382 | switch (set->val.nodes[i].type) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1383 | case LYXP_NODE_META: |
| 1384 | tmp_node = set->val.meta[i].meta->parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1385 | if (!tmp_node) { |
| 1386 | LOGINT_RET(root->schema->module->ctx); |
| 1387 | } |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1388 | /* fall through */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1389 | case LYXP_NODE_ELEM: |
| 1390 | case LYXP_NODE_TEXT: |
| 1391 | if (!tmp_node) { |
| 1392 | tmp_node = set->val.nodes[i].node; |
| 1393 | } |
| 1394 | set->val.nodes[i].pos = get_node_pos(tmp_node, set->val.nodes[i].type, root, root_type, &prev, &tmp_pos); |
| 1395 | break; |
| 1396 | default: |
| 1397 | /* all roots have position 0 */ |
| 1398 | break; |
| 1399 | } |
| 1400 | } |
| 1401 | } |
| 1402 | |
| 1403 | return LY_SUCCESS; |
| 1404 | } |
| 1405 | |
| 1406 | /** |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1407 | * @brief Get unique @p meta position in the parent metadata. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1408 | * |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1409 | * @param[in] meta Metadata to use. |
| 1410 | * @return Metadata position. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1411 | */ |
| 1412 | static uint16_t |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1413 | get_meta_pos(struct lyd_meta *meta) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1414 | { |
| 1415 | uint16_t pos = 0; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1416 | struct lyd_meta *meta2; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1417 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1418 | for (meta2 = meta->parent->meta; meta2 && (meta2 != meta); meta2 = meta2->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1419 | ++pos; |
| 1420 | } |
| 1421 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1422 | assert(meta2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1423 | return pos; |
| 1424 | } |
| 1425 | |
| 1426 | /** |
| 1427 | * @brief Compare 2 nodes in respect to XPath document order. |
| 1428 | * |
| 1429 | * @param[in] item1 1st node. |
| 1430 | * @param[in] item2 2nd node. |
| 1431 | * @return If 1st > 2nd returns 1, 1st == 2nd returns 0, and 1st < 2nd returns -1. |
| 1432 | */ |
| 1433 | static int |
| 1434 | set_sort_compare(struct lyxp_set_node *item1, struct lyxp_set_node *item2) |
| 1435 | { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1436 | uint32_t meta_pos1 = 0, meta_pos2 = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1437 | |
| 1438 | if (item1->pos < item2->pos) { |
| 1439 | return -1; |
| 1440 | } |
| 1441 | |
| 1442 | if (item1->pos > item2->pos) { |
| 1443 | return 1; |
| 1444 | } |
| 1445 | |
| 1446 | /* node positions are equal, the fun case */ |
| 1447 | |
| 1448 | /* 1st ELEM - == - 2nd TEXT, 1st TEXT - == - 2nd ELEM */ |
| 1449 | /* special case since text nodes are actually saved as their parents */ |
| 1450 | if ((item1->node == item2->node) && (item1->type != item2->type)) { |
| 1451 | if (item1->type == LYXP_NODE_ELEM) { |
| 1452 | assert(item2->type == LYXP_NODE_TEXT); |
| 1453 | return -1; |
| 1454 | } else { |
| 1455 | assert((item1->type == LYXP_NODE_TEXT) && (item2->type == LYXP_NODE_ELEM)); |
| 1456 | return 1; |
| 1457 | } |
| 1458 | } |
| 1459 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1460 | /* we need meta positions now */ |
| 1461 | if (item1->type == LYXP_NODE_META) { |
| 1462 | meta_pos1 = get_meta_pos((struct lyd_meta *)item1->node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1463 | } |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1464 | if (item2->type == LYXP_NODE_META) { |
| 1465 | meta_pos2 = get_meta_pos((struct lyd_meta *)item2->node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1466 | } |
| 1467 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1468 | /* 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] | 1469 | /* check for duplicates */ |
| 1470 | if (item1->node == item2->node) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1471 | 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] | 1472 | return 0; |
| 1473 | } |
| 1474 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1475 | /* 1st ELEM - 2nd TEXT, 1st ELEM - any pos - 2nd META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1476 | /* elem is always first, 2nd node is after it */ |
| 1477 | if (item1->type == LYXP_NODE_ELEM) { |
| 1478 | assert(item2->type != LYXP_NODE_ELEM); |
| 1479 | return -1; |
| 1480 | } |
| 1481 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1482 | /* 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] | 1483 | /* 2nd is before 1st */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1484 | if (((item1->type == LYXP_NODE_TEXT) && |
| 1485 | ((item2->type == LYXP_NODE_ELEM) || (item2->type == LYXP_NODE_META))) || |
| 1486 | ((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_ELEM)) || |
| 1487 | (((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_META)) && |
| 1488 | (meta_pos1 > meta_pos2))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1489 | return 1; |
| 1490 | } |
| 1491 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1492 | /* 1st META - any pos - 2nd TEXT, 1st META <pos< - 2nd META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1493 | /* 2nd is after 1st */ |
| 1494 | return -1; |
| 1495 | } |
| 1496 | |
| 1497 | /** |
| 1498 | * @brief Set cast for comparisons. |
| 1499 | * |
| 1500 | * @param[in] trg Target set to cast source into. |
| 1501 | * @param[in] src Source set. |
| 1502 | * @param[in] type Target set type. |
| 1503 | * @param[in] src_idx Source set node index. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1504 | * @return LY_ERR |
| 1505 | */ |
| 1506 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1507 | 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] | 1508 | { |
| 1509 | assert(src->type == LYXP_SET_NODE_SET); |
| 1510 | |
| 1511 | set_init(trg, src); |
| 1512 | |
| 1513 | /* insert node into target set */ |
| 1514 | set_insert_node(trg, src->val.nodes[src_idx].node, src->val.nodes[src_idx].pos, src->val.nodes[src_idx].type, 0); |
| 1515 | |
| 1516 | /* cast target set appropriately */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1517 | return lyxp_set_cast(trg, type); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1518 | } |
| 1519 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1520 | /** |
| 1521 | * @brief Set content canonization for comparisons. |
| 1522 | * |
| 1523 | * @param[in] trg Target set to put the canononized source into. |
| 1524 | * @param[in] src Source set. |
| 1525 | * @param[in] xp_node Source XPath node/meta to use for canonization. |
| 1526 | * @return LY_ERR |
| 1527 | */ |
| 1528 | static LY_ERR |
| 1529 | set_comp_canonize(struct lyxp_set *trg, const struct lyxp_set *src, const struct lyxp_set_node *xp_node) |
| 1530 | { |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 1531 | const struct lysc_type *type = NULL; |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1532 | struct lyd_value val; |
| 1533 | struct ly_err_item *err = NULL; |
| 1534 | char *str, *ptr; |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1535 | LY_ERR rc; |
| 1536 | |
| 1537 | /* is there anything to canonize even? */ |
| 1538 | if ((src->type == LYXP_SET_NUMBER) || (src->type == LYXP_SET_STRING)) { |
| 1539 | /* do we have a type to use for canonization? */ |
| 1540 | if ((xp_node->type == LYXP_NODE_ELEM) && (xp_node->node->schema->nodetype & LYD_NODE_TERM)) { |
| 1541 | type = ((struct lyd_node_term *)xp_node->node)->value.realtype; |
| 1542 | } else if (xp_node->type == LYXP_NODE_META) { |
| 1543 | type = ((struct lyd_meta *)xp_node->node)->value.realtype; |
| 1544 | } |
| 1545 | } |
| 1546 | if (!type) { |
| 1547 | goto fill; |
| 1548 | } |
| 1549 | |
| 1550 | if (src->type == LYXP_SET_NUMBER) { |
| 1551 | /* canonize number */ |
| 1552 | if (asprintf(&str, "%Lf", src->val.num) == -1) { |
| 1553 | LOGMEM(src->ctx); |
| 1554 | return LY_EMEM; |
| 1555 | } |
| 1556 | } else { |
| 1557 | /* canonize string */ |
| 1558 | str = strdup(src->val.str); |
| 1559 | } |
| 1560 | |
| 1561 | /* ignore errors, the value may not satisfy schema constraints */ |
Michal Vasko | 0fdb0d9 | 2020-11-06 17:25:50 +0100 | [diff] [blame] | 1562 | 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] | 1563 | LYD_HINT_DATA, xp_node->node->schema, &val, NULL, &err); |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1564 | ly_err_free(err); |
| 1565 | if (rc) { |
| 1566 | /* invalid value */ |
| 1567 | free(str); |
| 1568 | goto fill; |
| 1569 | } |
| 1570 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1571 | /* use the canonized value */ |
| 1572 | set_init(trg, src); |
| 1573 | trg->type = src->type; |
| 1574 | if (src->type == LYXP_SET_NUMBER) { |
Michal Vasko | 0fdb0d9 | 2020-11-06 17:25:50 +0100 | [diff] [blame] | 1575 | trg->val.num = strtold(val.canonical, &ptr); |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1576 | LY_CHECK_ERR_RET(ptr[0], LOGINT(src->ctx), LY_EINT); |
| 1577 | } else { |
Michal Vasko | 0fdb0d9 | 2020-11-06 17:25:50 +0100 | [diff] [blame] | 1578 | trg->val.str = strdup(val.canonical); |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1579 | } |
| 1580 | type->plugin->free(src->ctx, &val); |
| 1581 | return LY_SUCCESS; |
| 1582 | |
| 1583 | fill: |
| 1584 | /* no canonization needed/possible */ |
| 1585 | set_fill_set(trg, src); |
| 1586 | return LY_SUCCESS; |
| 1587 | } |
| 1588 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1589 | #ifndef NDEBUG |
| 1590 | |
| 1591 | /** |
| 1592 | * @brief Bubble sort @p set into XPath document order. |
| 1593 | * Context position aware. Unused in the 'Release' build target. |
| 1594 | * |
| 1595 | * @param[in] set Set to sort. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1596 | * @return How many times the whole set was traversed - 1 (if set was sorted, returns 0). |
| 1597 | */ |
| 1598 | static int |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1599 | set_sort(struct lyxp_set *set) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1600 | { |
| 1601 | uint32_t i, j; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 1602 | int ret = 0, cmp; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 1603 | ly_bool inverted, change; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1604 | const struct lyd_node *root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1605 | struct lyxp_set_node item; |
| 1606 | struct lyxp_set_hash_node hnode; |
| 1607 | uint64_t hash; |
| 1608 | |
Michal Vasko | 3cf8fbf | 2020-05-27 15:21:21 +0200 | [diff] [blame] | 1609 | if ((set->type != LYXP_SET_NODE_SET) || (set->used < 2)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1610 | return 0; |
| 1611 | } |
| 1612 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1613 | /* find first top-level node to be used as anchor for positions */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 1614 | for (root = set->cur_node; root->parent; root = (const struct lyd_node *)root->parent) {} |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 1615 | for ( ; root->prev->next; root = root->prev) {} |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1616 | |
| 1617 | /* fill positions */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1618 | if (set_assign_pos(set, root, set->root_type)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1619 | return -1; |
| 1620 | } |
| 1621 | |
| 1622 | LOGDBG(LY_LDGXPATH, "SORT BEGIN"); |
| 1623 | print_set_debug(set); |
| 1624 | |
| 1625 | for (i = 0; i < set->used; ++i) { |
| 1626 | inverted = 0; |
| 1627 | change = 0; |
| 1628 | |
| 1629 | for (j = 1; j < set->used - i; ++j) { |
| 1630 | /* compare node positions */ |
| 1631 | if (inverted) { |
| 1632 | cmp = set_sort_compare(&set->val.nodes[j], &set->val.nodes[j - 1]); |
| 1633 | } else { |
| 1634 | cmp = set_sort_compare(&set->val.nodes[j - 1], &set->val.nodes[j]); |
| 1635 | } |
| 1636 | |
| 1637 | /* swap if needed */ |
| 1638 | if ((inverted && (cmp < 0)) || (!inverted && (cmp > 0))) { |
| 1639 | change = 1; |
| 1640 | |
| 1641 | item = set->val.nodes[j - 1]; |
| 1642 | set->val.nodes[j - 1] = set->val.nodes[j]; |
| 1643 | set->val.nodes[j] = item; |
| 1644 | } else { |
| 1645 | /* whether node_pos1 should be smaller than node_pos2 or the other way around */ |
| 1646 | inverted = !inverted; |
| 1647 | } |
| 1648 | } |
| 1649 | |
| 1650 | ++ret; |
| 1651 | |
| 1652 | if (!change) { |
| 1653 | break; |
| 1654 | } |
| 1655 | } |
| 1656 | |
| 1657 | LOGDBG(LY_LDGXPATH, "SORT END %d", ret); |
| 1658 | print_set_debug(set); |
| 1659 | |
| 1660 | /* check node hashes */ |
| 1661 | if (set->used >= LYD_HT_MIN_ITEMS) { |
| 1662 | assert(set->ht); |
| 1663 | for (i = 0; i < set->used; ++i) { |
| 1664 | hnode.node = set->val.nodes[i].node; |
| 1665 | hnode.type = set->val.nodes[i].type; |
| 1666 | |
| 1667 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 1668 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 1669 | hash = dict_hash_multi(hash, NULL, 0); |
| 1670 | |
| 1671 | assert(!lyht_find(set->ht, &hnode, hash, NULL)); |
| 1672 | } |
| 1673 | } |
| 1674 | |
| 1675 | return ret - 1; |
| 1676 | } |
| 1677 | |
| 1678 | /** |
| 1679 | * @brief Remove duplicate entries in a sorted node set. |
| 1680 | * |
| 1681 | * @param[in] set Sorted set to check. |
| 1682 | * @return LY_ERR (LY_EEXIST if some duplicates are found) |
| 1683 | */ |
| 1684 | static LY_ERR |
| 1685 | set_sorted_dup_node_clean(struct lyxp_set *set) |
| 1686 | { |
| 1687 | uint32_t i = 0; |
| 1688 | LY_ERR ret = LY_SUCCESS; |
| 1689 | |
| 1690 | if (set->used > 1) { |
| 1691 | while (i < set->used - 1) { |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1692 | if ((set->val.nodes[i].node == set->val.nodes[i + 1].node) && |
| 1693 | (set->val.nodes[i].type == set->val.nodes[i + 1].type)) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1694 | set_remove_node_none(set, i + 1); |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1695 | ret = LY_EEXIST; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1696 | } |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1697 | ++i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1698 | } |
| 1699 | } |
| 1700 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1701 | set_remove_nodes_none(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1702 | return ret; |
| 1703 | } |
| 1704 | |
| 1705 | #endif |
| 1706 | |
| 1707 | /** |
| 1708 | * @brief Merge 2 sorted sets into one. |
| 1709 | * |
| 1710 | * @param[in,out] trg Set to merge into. Duplicates are removed. |
| 1711 | * @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] | 1712 | * @return LY_ERR |
| 1713 | */ |
| 1714 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1715 | set_sorted_merge(struct lyxp_set *trg, struct lyxp_set *src) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1716 | { |
| 1717 | uint32_t i, j, k, count, dup_count; |
| 1718 | int cmp; |
| 1719 | const struct lyd_node *root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1720 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1721 | 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] | 1722 | return LY_EINVAL; |
| 1723 | } |
| 1724 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1725 | if (!src->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1726 | return LY_SUCCESS; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1727 | } else if (!trg->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1728 | set_fill_set(trg, src); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1729 | lyxp_set_free_content(src); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1730 | return LY_SUCCESS; |
| 1731 | } |
| 1732 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1733 | /* find first top-level node to be used as anchor for positions */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 1734 | for (root = trg->cur_node; root->parent; root = (const struct lyd_node *)root->parent) {} |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 1735 | for ( ; root->prev->next; root = root->prev) {} |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1736 | |
| 1737 | /* fill positions */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1738 | 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] | 1739 | return LY_EINT; |
| 1740 | } |
| 1741 | |
| 1742 | #ifndef NDEBUG |
| 1743 | LOGDBG(LY_LDGXPATH, "MERGE target"); |
| 1744 | print_set_debug(trg); |
| 1745 | LOGDBG(LY_LDGXPATH, "MERGE source"); |
| 1746 | print_set_debug(src); |
| 1747 | #endif |
| 1748 | |
| 1749 | /* make memory for the merge (duplicates are not detected yet, so space |
| 1750 | * will likely be wasted on them, too bad) */ |
| 1751 | if (trg->size - trg->used < src->used) { |
| 1752 | trg->size = trg->used + src->used; |
| 1753 | |
| 1754 | trg->val.nodes = ly_realloc(trg->val.nodes, trg->size * sizeof *trg->val.nodes); |
| 1755 | LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx), LY_EMEM); |
| 1756 | } |
| 1757 | |
| 1758 | i = 0; |
| 1759 | j = 0; |
| 1760 | count = 0; |
| 1761 | dup_count = 0; |
| 1762 | do { |
| 1763 | cmp = set_sort_compare(&src->val.nodes[i], &trg->val.nodes[j]); |
| 1764 | if (!cmp) { |
| 1765 | if (!count) { |
| 1766 | /* duplicate, just skip it */ |
| 1767 | ++i; |
| 1768 | ++j; |
| 1769 | } else { |
| 1770 | /* we are copying something already, so let's copy the duplicate too, |
| 1771 | * we are hoping that afterwards there are some more nodes to |
| 1772 | * copy and this way we can copy them all together */ |
| 1773 | ++count; |
| 1774 | ++dup_count; |
| 1775 | ++i; |
| 1776 | ++j; |
| 1777 | } |
| 1778 | } else if (cmp < 0) { |
| 1779 | /* inserting src node into trg, just remember it for now */ |
| 1780 | ++count; |
| 1781 | ++i; |
| 1782 | |
| 1783 | /* insert the hash now */ |
| 1784 | set_insert_node_hash(trg, src->val.nodes[i - 1].node, src->val.nodes[i - 1].type); |
| 1785 | } else if (count) { |
| 1786 | copy_nodes: |
| 1787 | /* time to actually copy the nodes, we have found the largest block of nodes */ |
| 1788 | memmove(&trg->val.nodes[j + (count - dup_count)], |
| 1789 | &trg->val.nodes[j], |
| 1790 | (trg->used - j) * sizeof *trg->val.nodes); |
| 1791 | memcpy(&trg->val.nodes[j - dup_count], &src->val.nodes[i - count], count * sizeof *src->val.nodes); |
| 1792 | |
| 1793 | trg->used += count - dup_count; |
| 1794 | /* do not change i, except the copying above, we are basically doing exactly what is in the else branch below */ |
| 1795 | j += count - dup_count; |
| 1796 | |
| 1797 | count = 0; |
| 1798 | dup_count = 0; |
| 1799 | } else { |
| 1800 | ++j; |
| 1801 | } |
| 1802 | } while ((i < src->used) && (j < trg->used)); |
| 1803 | |
| 1804 | if ((i < src->used) || count) { |
| 1805 | /* insert all the hashes first */ |
| 1806 | for (k = i; k < src->used; ++k) { |
| 1807 | set_insert_node_hash(trg, src->val.nodes[k].node, src->val.nodes[k].type); |
| 1808 | } |
| 1809 | |
| 1810 | /* loop ended, but we need to copy something at trg end */ |
| 1811 | count += src->used - i; |
| 1812 | i = src->used; |
| 1813 | goto copy_nodes; |
| 1814 | } |
| 1815 | |
| 1816 | /* we are inserting hashes before the actual node insert, which causes |
| 1817 | * situations when there were initially not enough items for a hash table, |
| 1818 | * but even after some were inserted, hash table was not created (during |
| 1819 | * insertion the number of items is not updated yet) */ |
| 1820 | if (!trg->ht && (trg->used >= LYD_HT_MIN_ITEMS)) { |
| 1821 | set_insert_node_hash(trg, NULL, 0); |
| 1822 | } |
| 1823 | |
| 1824 | #ifndef NDEBUG |
| 1825 | LOGDBG(LY_LDGXPATH, "MERGE result"); |
| 1826 | print_set_debug(trg); |
| 1827 | #endif |
| 1828 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1829 | lyxp_set_free_content(src); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1830 | return LY_SUCCESS; |
| 1831 | } |
| 1832 | |
| 1833 | /* |
| 1834 | * (re)parse functions |
| 1835 | * |
| 1836 | * Parse functions parse the expression into |
| 1837 | * tokens (syntactic analysis). |
| 1838 | * |
| 1839 | * Reparse functions perform semantic analysis |
| 1840 | * (do not save the result, just a check) of |
| 1841 | * the expression and fill repeat indices. |
| 1842 | */ |
| 1843 | |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1844 | LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1845 | 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] | 1846 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1847 | if (exp->used == tok_idx) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1848 | if (ctx) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1849 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF); |
| 1850 | } |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1851 | return LY_EINCOMPLETE; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1852 | } |
| 1853 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1854 | if (want_tok && (exp->tokens[tok_idx] != want_tok)) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1855 | if (ctx) { |
Michal Vasko | 0b468e6 | 2020-10-19 09:33:04 +0200 | [diff] [blame] | 1856 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK2, lyxp_print_token(exp->tokens[tok_idx]), |
| 1857 | &exp->expr[exp->tok_pos[tok_idx]], lyxp_print_token(want_tok)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1858 | } |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1859 | return LY_ENOT; |
| 1860 | } |
| 1861 | |
| 1862 | return LY_SUCCESS; |
| 1863 | } |
| 1864 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1865 | LY_ERR |
| 1866 | lyxp_next_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok) |
| 1867 | { |
| 1868 | LY_CHECK_RET(lyxp_check_token(ctx, exp, *tok_idx, want_tok)); |
| 1869 | |
| 1870 | /* skip the token */ |
| 1871 | ++(*tok_idx); |
| 1872 | |
| 1873 | return LY_SUCCESS; |
| 1874 | } |
| 1875 | |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1876 | /* just like lyxp_check_token() but tests for 2 tokens */ |
| 1877 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 1878 | 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] | 1879 | enum lyxp_token want_tok2) |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1880 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1881 | if (exp->used == tok_idx) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1882 | if (ctx) { |
| 1883 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF); |
| 1884 | } |
| 1885 | return LY_EINCOMPLETE; |
| 1886 | } |
| 1887 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1888 | 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] | 1889 | if (ctx) { |
Michal Vasko | 0b468e6 | 2020-10-19 09:33:04 +0200 | [diff] [blame] | 1890 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[tok_idx]), |
| 1891 | &exp->expr[exp->tok_pos[tok_idx]]); |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1892 | } |
| 1893 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1894 | } |
| 1895 | |
| 1896 | return LY_SUCCESS; |
| 1897 | } |
| 1898 | |
| 1899 | /** |
| 1900 | * @brief Stack operation push on the repeat array. |
| 1901 | * |
| 1902 | * @param[in] exp Expression to use. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1903 | * @param[in] tok_idx Position in the expresion \p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1904 | * @param[in] repeat_op_idx Index from \p exp of the operator token. This value is pushed. |
| 1905 | */ |
| 1906 | static void |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1907 | exp_repeat_push(struct lyxp_expr *exp, uint16_t tok_idx, uint16_t repeat_op_idx) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1908 | { |
| 1909 | uint16_t i; |
| 1910 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1911 | if (exp->repeat[tok_idx]) { |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 1912 | for (i = 0; exp->repeat[tok_idx][i]; ++i) {} |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1913 | exp->repeat[tok_idx] = realloc(exp->repeat[tok_idx], (i + 2) * sizeof *exp->repeat[tok_idx]); |
| 1914 | LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), ); |
| 1915 | exp->repeat[tok_idx][i] = repeat_op_idx; |
| 1916 | exp->repeat[tok_idx][i + 1] = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1917 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1918 | exp->repeat[tok_idx] = calloc(2, sizeof *exp->repeat[tok_idx]); |
| 1919 | LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), ); |
| 1920 | exp->repeat[tok_idx][0] = repeat_op_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1921 | } |
| 1922 | } |
| 1923 | |
| 1924 | /** |
| 1925 | * @brief Reparse Predicate. Logs directly on error. |
| 1926 | * |
| 1927 | * [7] Predicate ::= '[' Expr ']' |
| 1928 | * |
| 1929 | * @param[in] ctx Context for logging. |
| 1930 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1931 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1932 | * @return LY_ERR |
| 1933 | */ |
| 1934 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1935 | 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] | 1936 | { |
| 1937 | LY_ERR rc; |
| 1938 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1939 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1940 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1941 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1942 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1943 | rc = reparse_or_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1944 | LY_CHECK_RET(rc); |
| 1945 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1946 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1947 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1948 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1949 | |
| 1950 | return LY_SUCCESS; |
| 1951 | } |
| 1952 | |
| 1953 | /** |
| 1954 | * @brief Reparse RelativeLocationPath. Logs directly on error. |
| 1955 | * |
| 1956 | * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step |
| 1957 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 1958 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 1959 | * |
| 1960 | * @param[in] ctx Context for logging. |
| 1961 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1962 | * @param[in] tok_idx Position in the expression \p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1963 | * @return LY_ERR (LY_EINCOMPLETE on forward reference) |
| 1964 | */ |
| 1965 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1966 | 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] | 1967 | { |
| 1968 | LY_ERR rc; |
| 1969 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1970 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1971 | LY_CHECK_RET(rc); |
| 1972 | |
| 1973 | goto step; |
| 1974 | do { |
| 1975 | /* '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1976 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1977 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1978 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1979 | LY_CHECK_RET(rc); |
| 1980 | step: |
| 1981 | /* Step */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1982 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1983 | case LYXP_TOKEN_DOT: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1984 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1985 | break; |
| 1986 | |
| 1987 | case LYXP_TOKEN_DDOT: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1988 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1989 | break; |
| 1990 | |
| 1991 | case LYXP_TOKEN_AT: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1992 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1993 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1994 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1995 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1996 | if ((exp->tokens[*tok_idx] != LYXP_TOKEN_NAMETEST) && (exp->tokens[*tok_idx] != LYXP_TOKEN_NODETYPE)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1997 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1998 | 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] | 1999 | return LY_EVALID; |
| 2000 | } |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 2001 | /* fall through */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2002 | case LYXP_TOKEN_NAMETEST: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2003 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2004 | goto reparse_predicate; |
| 2005 | break; |
| 2006 | |
| 2007 | case LYXP_TOKEN_NODETYPE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2008 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2009 | |
| 2010 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2011 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2012 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2013 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2014 | |
| 2015 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2016 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2017 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2018 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2019 | |
| 2020 | reparse_predicate: |
| 2021 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2022 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 2023 | rc = reparse_predicate(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2024 | LY_CHECK_RET(rc); |
| 2025 | } |
| 2026 | break; |
| 2027 | default: |
| 2028 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2029 | 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] | 2030 | return LY_EVALID; |
| 2031 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2032 | } 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] | 2033 | |
| 2034 | return LY_SUCCESS; |
| 2035 | } |
| 2036 | |
| 2037 | /** |
| 2038 | * @brief Reparse AbsoluteLocationPath. Logs directly on error. |
| 2039 | * |
| 2040 | * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath |
| 2041 | * |
| 2042 | * @param[in] ctx Context for logging. |
| 2043 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2044 | * @param[in] tok_idx Position in the expression \p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2045 | * @return LY_ERR |
| 2046 | */ |
| 2047 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2048 | 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] | 2049 | { |
| 2050 | LY_ERR rc; |
| 2051 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2052 | LY_CHECK_RET(exp_check_token2(ctx, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2053 | |
| 2054 | /* '/' RelativeLocationPath? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2055 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2056 | /* '/' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2057 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2058 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2059 | if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2060 | return LY_SUCCESS; |
| 2061 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2062 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2063 | case LYXP_TOKEN_DOT: |
| 2064 | case LYXP_TOKEN_DDOT: |
| 2065 | case LYXP_TOKEN_AT: |
| 2066 | case LYXP_TOKEN_NAMETEST: |
| 2067 | case LYXP_TOKEN_NODETYPE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2068 | rc = reparse_relative_location_path(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2069 | LY_CHECK_RET(rc); |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 2070 | /* fall through */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2071 | default: |
| 2072 | break; |
| 2073 | } |
| 2074 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2075 | } else { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 2076 | /* '//' RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2077 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2078 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2079 | rc = reparse_relative_location_path(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2080 | LY_CHECK_RET(rc); |
| 2081 | } |
| 2082 | |
| 2083 | return LY_SUCCESS; |
| 2084 | } |
| 2085 | |
| 2086 | /** |
| 2087 | * @brief Reparse FunctionCall. Logs directly on error. |
| 2088 | * |
| 2089 | * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')' |
| 2090 | * |
| 2091 | * @param[in] ctx Context for logging. |
| 2092 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2093 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2094 | * @return LY_ERR |
| 2095 | */ |
| 2096 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2097 | 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] | 2098 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2099 | int8_t min_arg_count = -1; |
| 2100 | uint32_t arg_count, max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2101 | uint16_t func_tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2102 | LY_ERR rc; |
| 2103 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2104 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2105 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2106 | func_tok_idx = *tok_idx; |
| 2107 | switch (exp->tok_len[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2108 | case 3: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2109 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2110 | min_arg_count = 1; |
| 2111 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2112 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2113 | min_arg_count = 1; |
| 2114 | max_arg_count = 1; |
| 2115 | } |
| 2116 | break; |
| 2117 | case 4: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2118 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2119 | min_arg_count = 1; |
| 2120 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2121 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2122 | min_arg_count = 0; |
| 2123 | max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2124 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2125 | min_arg_count = 0; |
| 2126 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2127 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2128 | min_arg_count = 0; |
| 2129 | max_arg_count = 0; |
| 2130 | } |
| 2131 | break; |
| 2132 | case 5: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2133 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2134 | min_arg_count = 1; |
| 2135 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2136 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2137 | min_arg_count = 0; |
| 2138 | max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2139 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 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]], "round", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2143 | min_arg_count = 1; |
| 2144 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2145 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2146 | min_arg_count = 1; |
| 2147 | max_arg_count = 1; |
| 2148 | } |
| 2149 | break; |
| 2150 | case 6: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2151 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2152 | min_arg_count = 2; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2153 | max_arg_count = UINT32_MAX; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2154 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2155 | min_arg_count = 0; |
| 2156 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2157 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2158 | min_arg_count = 0; |
| 2159 | max_arg_count = 1; |
| 2160 | } |
| 2161 | break; |
| 2162 | case 7: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2163 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 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]], "ceiling", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2167 | min_arg_count = 1; |
| 2168 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2169 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2170 | min_arg_count = 0; |
| 2171 | max_arg_count = 0; |
| 2172 | } |
| 2173 | break; |
| 2174 | case 8: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2175 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2176 | min_arg_count = 2; |
| 2177 | max_arg_count = 2; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2178 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2179 | min_arg_count = 0; |
| 2180 | max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2181 | } 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] | 2182 | min_arg_count = 2; |
| 2183 | max_arg_count = 2; |
| 2184 | } |
| 2185 | break; |
| 2186 | case 9: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2187 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2188 | min_arg_count = 2; |
| 2189 | max_arg_count = 3; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2190 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2191 | min_arg_count = 3; |
| 2192 | max_arg_count = 3; |
| 2193 | } |
| 2194 | break; |
| 2195 | case 10: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2196 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2197 | min_arg_count = 0; |
| 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]], "enum-value", 10)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2200 | min_arg_count = 1; |
| 2201 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2202 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2203 | min_arg_count = 2; |
| 2204 | max_arg_count = 2; |
| 2205 | } |
| 2206 | break; |
| 2207 | case 11: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2208 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2209 | min_arg_count = 2; |
| 2210 | max_arg_count = 2; |
| 2211 | } |
| 2212 | break; |
| 2213 | case 12: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2214 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2215 | min_arg_count = 2; |
| 2216 | max_arg_count = 2; |
| 2217 | } |
| 2218 | break; |
| 2219 | case 13: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2220 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2221 | min_arg_count = 0; |
| 2222 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2223 | } 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] | 2224 | min_arg_count = 0; |
| 2225 | max_arg_count = 1; |
| 2226 | } |
| 2227 | break; |
| 2228 | case 15: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2229 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2230 | min_arg_count = 0; |
| 2231 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2232 | } 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] | 2233 | min_arg_count = 2; |
| 2234 | max_arg_count = 2; |
| 2235 | } |
| 2236 | break; |
| 2237 | case 16: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2238 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2239 | min_arg_count = 2; |
| 2240 | max_arg_count = 2; |
| 2241 | } |
| 2242 | break; |
| 2243 | case 20: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2244 | 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] | 2245 | min_arg_count = 2; |
| 2246 | max_arg_count = 2; |
| 2247 | } |
| 2248 | break; |
| 2249 | } |
| 2250 | if (min_arg_count == -1) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2251 | LOGVAL(ctx, LY_VLOG_NONE, NULL, 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] | 2252 | return LY_EINVAL; |
| 2253 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2254 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2255 | |
| 2256 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2257 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2258 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2259 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2260 | |
| 2261 | /* ( Expr ( ',' Expr )* )? */ |
| 2262 | arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2263 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2264 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2265 | if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2266 | ++arg_count; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2267 | rc = reparse_or_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2268 | LY_CHECK_RET(rc); |
| 2269 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2270 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) { |
| 2271 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2272 | |
| 2273 | ++arg_count; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2274 | rc = reparse_or_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2275 | LY_CHECK_RET(rc); |
| 2276 | } |
| 2277 | |
| 2278 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2279 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2280 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2281 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2282 | |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 2283 | if ((arg_count < (uint32_t)min_arg_count) || (arg_count > max_arg_count)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2284 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INARGCOUNT, arg_count, exp->tok_len[func_tok_idx], |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2285 | &exp->expr[exp->tok_pos[func_tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2286 | return LY_EVALID; |
| 2287 | } |
| 2288 | |
| 2289 | return LY_SUCCESS; |
| 2290 | } |
| 2291 | |
| 2292 | /** |
| 2293 | * @brief Reparse PathExpr. Logs directly on error. |
| 2294 | * |
| 2295 | * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate* |
| 2296 | * | PrimaryExpr Predicate* '/' RelativeLocationPath |
| 2297 | * | PrimaryExpr Predicate* '//' RelativeLocationPath |
| 2298 | * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath |
| 2299 | * [8] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall |
| 2300 | * |
| 2301 | * @param[in] ctx Context for logging. |
| 2302 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2303 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2304 | * @return LY_ERR |
| 2305 | */ |
| 2306 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2307 | 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] | 2308 | { |
| 2309 | LY_ERR rc; |
| 2310 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2311 | if (lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 2312 | return LY_EVALID; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2313 | } |
| 2314 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2315 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2316 | case LYXP_TOKEN_PAR1: |
| 2317 | /* '(' Expr ')' Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2318 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2319 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2320 | rc = reparse_or_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2321 | LY_CHECK_RET(rc); |
| 2322 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2323 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2324 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2325 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2326 | goto predicate; |
| 2327 | break; |
| 2328 | case LYXP_TOKEN_DOT: |
| 2329 | case LYXP_TOKEN_DDOT: |
| 2330 | case LYXP_TOKEN_AT: |
| 2331 | case LYXP_TOKEN_NAMETEST: |
| 2332 | case LYXP_TOKEN_NODETYPE: |
| 2333 | /* RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2334 | rc = reparse_relative_location_path(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2335 | LY_CHECK_RET(rc); |
| 2336 | break; |
| 2337 | case LYXP_TOKEN_FUNCNAME: |
| 2338 | /* FunctionCall */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2339 | rc = reparse_function_call(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2340 | LY_CHECK_RET(rc); |
| 2341 | goto predicate; |
| 2342 | break; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2343 | case LYXP_TOKEN_OPER_PATH: |
| 2344 | case LYXP_TOKEN_OPER_RPATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2345 | /* AbsoluteLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2346 | rc = reparse_absolute_location_path(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2347 | LY_CHECK_RET(rc); |
| 2348 | break; |
| 2349 | case LYXP_TOKEN_LITERAL: |
| 2350 | /* Literal */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2351 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2352 | goto predicate; |
| 2353 | break; |
| 2354 | case LYXP_TOKEN_NUMBER: |
| 2355 | /* Number */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2356 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2357 | goto predicate; |
| 2358 | break; |
| 2359 | default: |
| 2360 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2361 | lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2362 | return LY_EVALID; |
| 2363 | } |
| 2364 | |
| 2365 | return LY_SUCCESS; |
| 2366 | |
| 2367 | predicate: |
| 2368 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2369 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 2370 | rc = reparse_predicate(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2371 | LY_CHECK_RET(rc); |
| 2372 | } |
| 2373 | |
| 2374 | /* ('/' or '//') RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2375 | if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2376 | |
| 2377 | /* '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2378 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2379 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2380 | rc = reparse_relative_location_path(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2381 | LY_CHECK_RET(rc); |
| 2382 | } |
| 2383 | |
| 2384 | return LY_SUCCESS; |
| 2385 | } |
| 2386 | |
| 2387 | /** |
| 2388 | * @brief Reparse UnaryExpr. Logs directly on error. |
| 2389 | * |
| 2390 | * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr |
| 2391 | * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr |
| 2392 | * |
| 2393 | * @param[in] ctx Context for logging. |
| 2394 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2395 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2396 | * @return LY_ERR |
| 2397 | */ |
| 2398 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2399 | 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] | 2400 | { |
| 2401 | uint16_t prev_exp; |
| 2402 | LY_ERR rc; |
| 2403 | |
| 2404 | /* ('-')* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2405 | prev_exp = *tok_idx; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2406 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) && |
| 2407 | (exp->expr[exp->tok_pos[*tok_idx]] == '-')) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2408 | exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2409 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2410 | } |
| 2411 | |
| 2412 | /* PathExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2413 | prev_exp = *tok_idx; |
| 2414 | rc = reparse_path_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2415 | LY_CHECK_RET(rc); |
| 2416 | |
| 2417 | /* ('|' PathExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2418 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_UNI)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2419 | exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2420 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2421 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2422 | rc = reparse_path_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2423 | LY_CHECK_RET(rc); |
| 2424 | } |
| 2425 | |
| 2426 | return LY_SUCCESS; |
| 2427 | } |
| 2428 | |
| 2429 | /** |
| 2430 | * @brief Reparse AdditiveExpr. Logs directly on error. |
| 2431 | * |
| 2432 | * [15] AdditiveExpr ::= MultiplicativeExpr |
| 2433 | * | AdditiveExpr '+' MultiplicativeExpr |
| 2434 | * | AdditiveExpr '-' MultiplicativeExpr |
| 2435 | * [16] MultiplicativeExpr ::= UnaryExpr |
| 2436 | * | MultiplicativeExpr '*' UnaryExpr |
| 2437 | * | MultiplicativeExpr 'div' UnaryExpr |
| 2438 | * | MultiplicativeExpr 'mod' UnaryExpr |
| 2439 | * |
| 2440 | * @param[in] ctx Context for logging. |
| 2441 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2442 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2443 | * @return LY_ERR |
| 2444 | */ |
| 2445 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2446 | 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] | 2447 | { |
| 2448 | uint16_t prev_add_exp, prev_mul_exp; |
| 2449 | LY_ERR rc; |
| 2450 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2451 | prev_add_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2452 | goto reparse_multiplicative_expr; |
| 2453 | |
| 2454 | /* ('+' / '-' MultiplicativeExpr)* */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2455 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) && |
| 2456 | ((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] | 2457 | exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2458 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2459 | |
| 2460 | reparse_multiplicative_expr: |
| 2461 | /* UnaryExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2462 | prev_mul_exp = *tok_idx; |
| 2463 | rc = reparse_unary_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2464 | LY_CHECK_RET(rc); |
| 2465 | |
| 2466 | /* ('*' / 'div' / 'mod' UnaryExpr)* */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2467 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) && |
| 2468 | ((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] | 2469 | exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2470 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2471 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2472 | rc = reparse_unary_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2473 | LY_CHECK_RET(rc); |
| 2474 | } |
| 2475 | } |
| 2476 | |
| 2477 | return LY_SUCCESS; |
| 2478 | } |
| 2479 | |
| 2480 | /** |
| 2481 | * @brief Reparse EqualityExpr. Logs directly on error. |
| 2482 | * |
| 2483 | * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr |
| 2484 | * | EqualityExpr '!=' RelationalExpr |
| 2485 | * [14] RelationalExpr ::= AdditiveExpr |
| 2486 | * | RelationalExpr '<' AdditiveExpr |
| 2487 | * | RelationalExpr '>' AdditiveExpr |
| 2488 | * | RelationalExpr '<=' AdditiveExpr |
| 2489 | * | RelationalExpr '>=' AdditiveExpr |
| 2490 | * |
| 2491 | * @param[in] ctx Context for logging. |
| 2492 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2493 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2494 | * @return LY_ERR |
| 2495 | */ |
| 2496 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2497 | 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] | 2498 | { |
| 2499 | uint16_t prev_eq_exp, prev_rel_exp; |
| 2500 | LY_ERR rc; |
| 2501 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2502 | prev_eq_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2503 | goto reparse_additive_expr; |
| 2504 | |
| 2505 | /* ('=' / '!=' RelationalExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2506 | 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] | 2507 | exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2508 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2509 | |
| 2510 | reparse_additive_expr: |
| 2511 | /* AdditiveExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2512 | prev_rel_exp = *tok_idx; |
| 2513 | rc = reparse_additive_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2514 | LY_CHECK_RET(rc); |
| 2515 | |
| 2516 | /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2517 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_COMP)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2518 | exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2519 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2520 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2521 | rc = reparse_additive_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2522 | LY_CHECK_RET(rc); |
| 2523 | } |
| 2524 | } |
| 2525 | |
| 2526 | return LY_SUCCESS; |
| 2527 | } |
| 2528 | |
| 2529 | /** |
| 2530 | * @brief Reparse OrExpr. Logs directly on error. |
| 2531 | * |
| 2532 | * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr |
| 2533 | * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr |
| 2534 | * |
| 2535 | * @param[in] ctx Context for logging. |
| 2536 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2537 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2538 | * @return LY_ERR |
| 2539 | */ |
| 2540 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2541 | 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] | 2542 | { |
| 2543 | uint16_t prev_or_exp, prev_and_exp; |
| 2544 | LY_ERR rc; |
| 2545 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2546 | prev_or_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2547 | goto reparse_equality_expr; |
| 2548 | |
| 2549 | /* ('or' AndExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2550 | 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] | 2551 | exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2552 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2553 | |
| 2554 | reparse_equality_expr: |
| 2555 | /* EqualityExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2556 | prev_and_exp = *tok_idx; |
| 2557 | rc = reparse_equality_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2558 | LY_CHECK_RET(rc); |
| 2559 | |
| 2560 | /* ('and' EqualityExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2561 | 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] | 2562 | exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2563 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2564 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2565 | rc = reparse_equality_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2566 | LY_CHECK_RET(rc); |
| 2567 | } |
| 2568 | } |
| 2569 | |
| 2570 | return LY_SUCCESS; |
| 2571 | } |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2572 | |
| 2573 | /** |
| 2574 | * @brief Parse NCName. |
| 2575 | * |
| 2576 | * @param[in] ncname Name to parse. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2577 | * @return Length of @p ncname valid bytes. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2578 | */ |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2579 | static long int |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2580 | parse_ncname(const char *ncname) |
| 2581 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2582 | uint32_t uc; |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2583 | size_t size; |
| 2584 | long int len = 0; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2585 | |
| 2586 | LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0); |
| 2587 | if (!is_xmlqnamestartchar(uc) || (uc == ':')) { |
| 2588 | return len; |
| 2589 | } |
| 2590 | |
| 2591 | do { |
| 2592 | len += size; |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 2593 | if (!*ncname) { |
| 2594 | break; |
| 2595 | } |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2596 | LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2597 | } while (is_xmlqnamechar(uc) && (uc != ':')); |
| 2598 | |
| 2599 | return len; |
| 2600 | } |
| 2601 | |
| 2602 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2603 | * @brief Add @p token into the expression @p exp. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2604 | * |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2605 | * @param[in] ctx Context for logging. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2606 | * @param[in] exp Expression to use. |
| 2607 | * @param[in] token Token to add. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2608 | * @param[in] tok_pos Token position in the XPath expression. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2609 | * @param[in] tok_len Token length in the XPath expression. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2610 | * @return LY_ERR |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2611 | */ |
| 2612 | static LY_ERR |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 2613 | 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] | 2614 | { |
| 2615 | uint32_t prev; |
| 2616 | |
| 2617 | if (exp->used == exp->size) { |
| 2618 | prev = exp->size; |
| 2619 | exp->size += LYXP_EXPR_SIZE_STEP; |
| 2620 | if (prev > exp->size) { |
| 2621 | LOGINT(ctx); |
| 2622 | return LY_EINT; |
| 2623 | } |
| 2624 | |
| 2625 | exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens); |
| 2626 | LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM); |
| 2627 | exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos); |
| 2628 | LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM); |
| 2629 | exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len); |
| 2630 | LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM); |
| 2631 | } |
| 2632 | |
| 2633 | exp->tokens[exp->used] = token; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2634 | exp->tok_pos[exp->used] = tok_pos; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2635 | exp->tok_len[exp->used] = tok_len; |
| 2636 | ++exp->used; |
| 2637 | return LY_SUCCESS; |
| 2638 | } |
| 2639 | |
| 2640 | void |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 2641 | lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr) |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2642 | { |
| 2643 | uint16_t i; |
| 2644 | |
| 2645 | if (!expr) { |
| 2646 | return; |
| 2647 | } |
| 2648 | |
| 2649 | lydict_remove(ctx, expr->expr); |
| 2650 | free(expr->tokens); |
| 2651 | free(expr->tok_pos); |
| 2652 | free(expr->tok_len); |
| 2653 | if (expr->repeat) { |
| 2654 | for (i = 0; i < expr->used; ++i) { |
| 2655 | free(expr->repeat[i]); |
| 2656 | } |
| 2657 | } |
| 2658 | free(expr->repeat); |
| 2659 | free(expr); |
| 2660 | } |
| 2661 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2662 | LY_ERR |
| 2663 | 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] | 2664 | { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2665 | LY_ERR ret = LY_SUCCESS; |
| 2666 | struct lyxp_expr *expr; |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2667 | size_t parsed = 0, tok_len; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2668 | enum lyxp_token tok_type; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 2669 | ly_bool prev_function_check = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2670 | uint16_t tok_idx = 0; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2671 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2672 | assert(expr_p); |
| 2673 | |
| 2674 | if (!expr_str[0]) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2675 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF); |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2676 | return LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2677 | } |
| 2678 | |
| 2679 | if (!expr_len) { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2680 | expr_len = strlen(expr_str); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2681 | } |
| 2682 | if (expr_len > UINT16_MAX) { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2683 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "XPath expression cannot be longer than %ud characters.", UINT16_MAX); |
| 2684 | return LY_EVALID; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2685 | } |
| 2686 | |
| 2687 | /* init lyxp_expr structure */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2688 | expr = calloc(1, sizeof *expr); |
| 2689 | LY_CHECK_ERR_GOTO(!expr, LOGMEM(ctx); ret = LY_EMEM, error); |
| 2690 | LY_CHECK_GOTO(ret = lydict_insert(ctx, expr_str, expr_len, &expr->expr), error); |
| 2691 | expr->used = 0; |
| 2692 | expr->size = LYXP_EXPR_SIZE_START; |
| 2693 | expr->tokens = malloc(expr->size * sizeof *expr->tokens); |
| 2694 | LY_CHECK_ERR_GOTO(!expr->tokens, LOGMEM(ctx); ret = LY_EMEM, error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2695 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2696 | expr->tok_pos = malloc(expr->size * sizeof *expr->tok_pos); |
| 2697 | 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] | 2698 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2699 | expr->tok_len = malloc(expr->size * sizeof *expr->tok_len); |
| 2700 | 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] | 2701 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2702 | /* make expr 0-terminated */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2703 | expr_str = expr->expr; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2704 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2705 | while (is_xmlws(expr_str[parsed])) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2706 | ++parsed; |
| 2707 | } |
| 2708 | |
| 2709 | do { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2710 | if (expr_str[parsed] == '(') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2711 | |
| 2712 | /* '(' */ |
| 2713 | tok_len = 1; |
| 2714 | tok_type = LYXP_TOKEN_PAR1; |
| 2715 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2716 | 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] | 2717 | /* it is a NodeType/FunctionName after all */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2718 | if (((expr->tok_len[expr->used - 1] == 4) && |
| 2719 | (!strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "node", 4) || |
| 2720 | !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "text", 4))) || |
| 2721 | ((expr->tok_len[expr->used - 1] == 7) && |
| 2722 | !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "comment", 7))) { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2723 | expr->tokens[expr->used - 1] = LYXP_TOKEN_NODETYPE; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2724 | } else { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2725 | expr->tokens[expr->used - 1] = LYXP_TOKEN_FUNCNAME; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2726 | } |
| 2727 | prev_function_check = 0; |
| 2728 | } |
| 2729 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2730 | } else if (expr_str[parsed] == ')') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2731 | |
| 2732 | /* ')' */ |
| 2733 | tok_len = 1; |
| 2734 | tok_type = LYXP_TOKEN_PAR2; |
| 2735 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2736 | } else if (expr_str[parsed] == '[') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2737 | |
| 2738 | /* '[' */ |
| 2739 | tok_len = 1; |
| 2740 | tok_type = LYXP_TOKEN_BRACK1; |
| 2741 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2742 | } else if (expr_str[parsed] == ']') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2743 | |
| 2744 | /* ']' */ |
| 2745 | tok_len = 1; |
| 2746 | tok_type = LYXP_TOKEN_BRACK2; |
| 2747 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2748 | } else if (!strncmp(&expr_str[parsed], "..", 2)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2749 | |
| 2750 | /* '..' */ |
| 2751 | tok_len = 2; |
| 2752 | tok_type = LYXP_TOKEN_DDOT; |
| 2753 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2754 | } else if ((expr_str[parsed] == '.') && (!isdigit(expr_str[parsed + 1]))) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2755 | |
| 2756 | /* '.' */ |
| 2757 | tok_len = 1; |
| 2758 | tok_type = LYXP_TOKEN_DOT; |
| 2759 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2760 | } else if (expr_str[parsed] == '@') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2761 | |
| 2762 | /* '@' */ |
| 2763 | tok_len = 1; |
| 2764 | tok_type = LYXP_TOKEN_AT; |
| 2765 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2766 | } else if (expr_str[parsed] == ',') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2767 | |
| 2768 | /* ',' */ |
| 2769 | tok_len = 1; |
| 2770 | tok_type = LYXP_TOKEN_COMMA; |
| 2771 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2772 | } else if (expr_str[parsed] == '\'') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2773 | |
| 2774 | /* Literal with ' */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2775 | for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\''); ++tok_len) {} |
| 2776 | LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0', |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2777 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID, |
| 2778 | error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2779 | ++tok_len; |
| 2780 | tok_type = LYXP_TOKEN_LITERAL; |
| 2781 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2782 | } else if (expr_str[parsed] == '\"') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2783 | |
| 2784 | /* Literal with " */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2785 | for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\"'); ++tok_len) {} |
| 2786 | LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0', |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2787 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID, |
| 2788 | error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2789 | ++tok_len; |
| 2790 | tok_type = LYXP_TOKEN_LITERAL; |
| 2791 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2792 | } else if ((expr_str[parsed] == '.') || (isdigit(expr_str[parsed]))) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2793 | |
| 2794 | /* Number */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2795 | for (tok_len = 0; isdigit(expr_str[parsed + tok_len]); ++tok_len) {} |
| 2796 | if (expr_str[parsed + tok_len] == '.') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2797 | ++tok_len; |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2798 | for ( ; isdigit(expr_str[parsed + tok_len]); ++tok_len) {} |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2799 | } |
| 2800 | tok_type = LYXP_TOKEN_NUMBER; |
| 2801 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2802 | } else if (expr_str[parsed] == '/') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2803 | |
| 2804 | /* Operator '/', '//' */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2805 | if (!strncmp(&expr_str[parsed], "//", 2)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2806 | tok_len = 2; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2807 | tok_type = LYXP_TOKEN_OPER_RPATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2808 | } else { |
| 2809 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2810 | tok_type = LYXP_TOKEN_OPER_PATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2811 | } |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2812 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2813 | } else if (!strncmp(&expr_str[parsed], "!=", 2)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2814 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2815 | /* Operator '!=' */ |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2816 | tok_len = 2; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2817 | tok_type = LYXP_TOKEN_OPER_NEQUAL; |
| 2818 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2819 | } else if (!strncmp(&expr_str[parsed], "<=", 2) || !strncmp(&expr_str[parsed], ">=", 2)) { |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2820 | |
| 2821 | /* Operator '<=', '>=' */ |
| 2822 | tok_len = 2; |
| 2823 | tok_type = LYXP_TOKEN_OPER_COMP; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2824 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2825 | } else if (expr_str[parsed] == '|') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2826 | |
| 2827 | /* Operator '|' */ |
| 2828 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2829 | tok_type = LYXP_TOKEN_OPER_UNI; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2830 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2831 | } else if ((expr_str[parsed] == '+') || (expr_str[parsed] == '-')) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2832 | |
| 2833 | /* Operator '+', '-' */ |
| 2834 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2835 | tok_type = LYXP_TOKEN_OPER_MATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2836 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2837 | } else if (expr_str[parsed] == '=') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2838 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2839 | /* Operator '=' */ |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2840 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2841 | tok_type = LYXP_TOKEN_OPER_EQUAL; |
| 2842 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2843 | } else if ((expr_str[parsed] == '<') || (expr_str[parsed] == '>')) { |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2844 | |
| 2845 | /* Operator '<', '>' */ |
| 2846 | tok_len = 1; |
| 2847 | tok_type = LYXP_TOKEN_OPER_COMP; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2848 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2849 | } else if (expr->used && (expr->tokens[expr->used - 1] != LYXP_TOKEN_AT) && |
| 2850 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_PAR1) && |
| 2851 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_BRACK1) && |
| 2852 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_COMMA) && |
| 2853 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_LOG) && |
| 2854 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_EQUAL) && |
| 2855 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_NEQUAL) && |
| 2856 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_COMP) && |
| 2857 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_MATH) && |
| 2858 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_UNI) && |
| 2859 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_PATH) && |
| 2860 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_RPATH)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2861 | |
| 2862 | /* Operator '*', 'or', 'and', 'mod', or 'div' */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2863 | if (expr_str[parsed] == '*') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2864 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2865 | tok_type = LYXP_TOKEN_OPER_MATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2866 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2867 | } else if (!strncmp(&expr_str[parsed], "or", 2)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2868 | tok_len = 2; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2869 | tok_type = LYXP_TOKEN_OPER_LOG; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2870 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2871 | } else if (!strncmp(&expr_str[parsed], "and", 3)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2872 | tok_len = 3; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2873 | tok_type = LYXP_TOKEN_OPER_LOG; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2874 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2875 | } 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] | 2876 | tok_len = 3; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2877 | tok_type = LYXP_TOKEN_OPER_MATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2878 | |
| 2879 | } else if (prev_function_check) { |
Michal Vasko | 5307857 | 2019-05-24 08:50:15 +0200 | [diff] [blame] | 2880 | LOGVAL(ctx, LY_VLOG_NONE, NULL, 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] | 2881 | 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] | 2882 | ret = LY_EVALID; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2883 | goto error; |
| 2884 | } else { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2885 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INEXPR, parsed + 1, expr_str); |
| 2886 | ret = LY_EVALID; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2887 | goto error; |
| 2888 | } |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2889 | } else if (expr_str[parsed] == '*') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2890 | |
| 2891 | /* NameTest '*' */ |
| 2892 | tok_len = 1; |
| 2893 | tok_type = LYXP_TOKEN_NAMETEST; |
| 2894 | |
| 2895 | } else { |
| 2896 | |
| 2897 | /* NameTest (NCName ':' '*' | QName) or NodeType/FunctionName */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2898 | long int ncname_len = parse_ncname(&expr_str[parsed]); |
| 2899 | LY_CHECK_ERR_GOTO(ncname_len < 0, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2900 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INEXPR, parsed - ncname_len + 1, expr_str); ret = LY_EVALID, |
| 2901 | error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2902 | tok_len = ncname_len; |
| 2903 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2904 | if (expr_str[parsed + tok_len] == ':') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2905 | ++tok_len; |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2906 | if (expr_str[parsed + tok_len] == '*') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2907 | ++tok_len; |
| 2908 | } else { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2909 | ncname_len = parse_ncname(&expr_str[parsed + tok_len]); |
| 2910 | LY_CHECK_ERR_GOTO(ncname_len < 0, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2911 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INEXPR, parsed - ncname_len + 1, expr_str); ret = LY_EVALID, |
| 2912 | error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2913 | tok_len += ncname_len; |
| 2914 | } |
| 2915 | /* remove old flag to prevent ambiguities */ |
| 2916 | prev_function_check = 0; |
| 2917 | tok_type = LYXP_TOKEN_NAMETEST; |
| 2918 | } else { |
| 2919 | /* there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */ |
| 2920 | prev_function_check = 1; |
| 2921 | tok_type = LYXP_TOKEN_NAMETEST; |
| 2922 | } |
| 2923 | } |
| 2924 | |
| 2925 | /* store the token, move on to the next one */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2926 | 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] | 2927 | parsed += tok_len; |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2928 | while (is_xmlws(expr_str[parsed])) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2929 | ++parsed; |
| 2930 | } |
| 2931 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2932 | } while (expr_str[parsed]); |
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 | if (reparse) { |
| 2935 | /* prealloc repeat */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2936 | expr->repeat = calloc(expr->size, sizeof *expr->repeat); |
| 2937 | LY_CHECK_ERR_GOTO(!expr->repeat, LOGMEM(ctx); ret = LY_EMEM, error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2938 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2939 | /* fill repeat */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2940 | LY_CHECK_ERR_GOTO(reparse_or_expr(ctx, expr, &tok_idx), ret = LY_EVALID, error); |
| 2941 | if (expr->used > tok_idx) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2942 | LOGVAL(ctx, LY_VLOG_NONE, NULL, 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] | 2943 | &expr->expr[expr->tok_pos[tok_idx]]); |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2944 | ret = LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2945 | goto error; |
| 2946 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2947 | } |
| 2948 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2949 | print_expr_struct_debug(expr); |
| 2950 | *expr_p = expr; |
| 2951 | return LY_SUCCESS; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2952 | |
| 2953 | error: |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2954 | lyxp_expr_free(ctx, expr); |
| 2955 | return ret; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2956 | } |
| 2957 | |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2958 | LY_ERR |
| 2959 | 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] | 2960 | { |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2961 | LY_ERR ret = LY_SUCCESS; |
| 2962 | struct lyxp_expr *dup = NULL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2963 | uint32_t i, j; |
| 2964 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 2965 | if (!exp) { |
| 2966 | goto cleanup; |
| 2967 | } |
| 2968 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2969 | dup = calloc(1, sizeof *dup); |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2970 | LY_CHECK_ERR_GOTO(!dup, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2971 | |
| 2972 | dup->tokens = malloc(exp->used * sizeof *dup->tokens); |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2973 | LY_CHECK_ERR_GOTO(!dup->tokens, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2974 | memcpy(dup->tokens, exp->tokens, exp->used * sizeof *dup->tokens); |
| 2975 | |
| 2976 | dup->tok_pos = malloc(exp->used * sizeof *dup->tok_pos); |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2977 | 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] | 2978 | memcpy(dup->tok_pos, exp->tok_pos, exp->used * sizeof *dup->tok_pos); |
| 2979 | |
| 2980 | dup->tok_len = malloc(exp->used * sizeof *dup->tok_len); |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2981 | 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] | 2982 | memcpy(dup->tok_len, exp->tok_len, exp->used * sizeof *dup->tok_len); |
| 2983 | |
| 2984 | dup->repeat = malloc(exp->used * sizeof *dup->repeat); |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2985 | LY_CHECK_ERR_GOTO(!dup->repeat, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2986 | for (i = 0; i < exp->used; ++i) { |
| 2987 | if (!exp->repeat[i]) { |
| 2988 | dup->repeat[i] = NULL; |
| 2989 | } else { |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 2990 | for (j = 0; exp->repeat[i][j]; ++j) {} |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2991 | /* the ending 0 as well */ |
| 2992 | ++j; |
| 2993 | |
Michal Vasko | 99c7164 | 2020-07-03 13:33:36 +0200 | [diff] [blame] | 2994 | dup->repeat[i] = malloc(j * sizeof **dup->repeat); |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2995 | 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] | 2996 | memcpy(dup->repeat[i], exp->repeat[i], j * sizeof **dup->repeat); |
| 2997 | dup->repeat[i][j - 1] = 0; |
| 2998 | } |
| 2999 | } |
| 3000 | |
| 3001 | dup->used = exp->used; |
| 3002 | dup->size = exp->used; |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 3003 | 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] | 3004 | |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 3005 | cleanup: |
| 3006 | if (ret) { |
| 3007 | lyxp_expr_free(ctx, dup); |
| 3008 | } else { |
| 3009 | *dup_p = dup; |
| 3010 | } |
| 3011 | return ret; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3012 | } |
| 3013 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3014 | /* |
| 3015 | * warn functions |
| 3016 | * |
| 3017 | * Warn functions check specific reasonable conditions for schema XPath |
| 3018 | * and print a warning if they are not satisfied. |
| 3019 | */ |
| 3020 | |
| 3021 | /** |
| 3022 | * @brief Get the last-added schema node that is currently in the context. |
| 3023 | * |
| 3024 | * @param[in] set Set to search in. |
| 3025 | * @return Last-added schema context node, NULL if no node is in context. |
| 3026 | */ |
| 3027 | static struct lysc_node * |
| 3028 | warn_get_scnode_in_ctx(struct lyxp_set *set) |
| 3029 | { |
| 3030 | uint32_t i; |
| 3031 | |
| 3032 | if (!set || (set->type != LYXP_SET_SCNODE_SET)) { |
| 3033 | return NULL; |
| 3034 | } |
| 3035 | |
| 3036 | i = set->used; |
| 3037 | do { |
| 3038 | --i; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 3039 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3040 | /* if there are more, simply return the first found (last added) */ |
| 3041 | return set->val.scnodes[i].scnode; |
| 3042 | } |
| 3043 | } while (i); |
| 3044 | |
| 3045 | return NULL; |
| 3046 | } |
| 3047 | |
| 3048 | /** |
| 3049 | * @brief Test whether a type is numeric - integer type or decimal64. |
| 3050 | * |
| 3051 | * @param[in] type Type to test. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3052 | * @return Boolean value whether @p type is numeric type or not. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3053 | */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3054 | static ly_bool |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3055 | warn_is_numeric_type(struct lysc_type *type) |
| 3056 | { |
| 3057 | struct lysc_type_union *uni; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3058 | ly_bool ret; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3059 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3060 | |
| 3061 | switch (type->basetype) { |
| 3062 | case LY_TYPE_DEC64: |
| 3063 | case LY_TYPE_INT8: |
| 3064 | case LY_TYPE_UINT8: |
| 3065 | case LY_TYPE_INT16: |
| 3066 | case LY_TYPE_UINT16: |
| 3067 | case LY_TYPE_INT32: |
| 3068 | case LY_TYPE_UINT32: |
| 3069 | case LY_TYPE_INT64: |
| 3070 | case LY_TYPE_UINT64: |
| 3071 | return 1; |
| 3072 | case LY_TYPE_UNION: |
| 3073 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3074 | LY_ARRAY_FOR(uni->types, u) { |
| 3075 | ret = warn_is_numeric_type(uni->types[u]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3076 | if (ret) { |
| 3077 | /* found a suitable type */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3078 | return ret; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3079 | } |
| 3080 | } |
| 3081 | /* did not find any suitable type */ |
| 3082 | return 0; |
| 3083 | case LY_TYPE_LEAFREF: |
| 3084 | return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype); |
| 3085 | default: |
| 3086 | return 0; |
| 3087 | } |
| 3088 | } |
| 3089 | |
| 3090 | /** |
| 3091 | * @brief Test whether a type is string-like - no integers, decimal64 or binary. |
| 3092 | * |
| 3093 | * @param[in] type Type to test. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3094 | * @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] | 3095 | */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3096 | static ly_bool |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3097 | warn_is_string_type(struct lysc_type *type) |
| 3098 | { |
| 3099 | struct lysc_type_union *uni; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3100 | ly_bool ret; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3101 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3102 | |
| 3103 | switch (type->basetype) { |
| 3104 | case LY_TYPE_BITS: |
| 3105 | case LY_TYPE_ENUM: |
| 3106 | case LY_TYPE_IDENT: |
| 3107 | case LY_TYPE_INST: |
| 3108 | case LY_TYPE_STRING: |
| 3109 | return 1; |
| 3110 | case LY_TYPE_UNION: |
| 3111 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3112 | LY_ARRAY_FOR(uni->types, u) { |
| 3113 | ret = warn_is_string_type(uni->types[u]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3114 | if (ret) { |
| 3115 | /* found a suitable type */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3116 | return ret; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3117 | } |
| 3118 | } |
| 3119 | /* did not find any suitable type */ |
| 3120 | return 0; |
| 3121 | case LY_TYPE_LEAFREF: |
| 3122 | return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype); |
| 3123 | default: |
| 3124 | return 0; |
| 3125 | } |
| 3126 | } |
| 3127 | |
| 3128 | /** |
| 3129 | * @brief Test whether a type is one specific type. |
| 3130 | * |
| 3131 | * @param[in] type Type to test. |
| 3132 | * @param[in] base Expected type. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3133 | * @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] | 3134 | */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3135 | static ly_bool |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3136 | warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base) |
| 3137 | { |
| 3138 | struct lysc_type_union *uni; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3139 | ly_bool ret; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3140 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3141 | |
| 3142 | if (type->basetype == base) { |
| 3143 | return 1; |
| 3144 | } else if (type->basetype == LY_TYPE_UNION) { |
| 3145 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3146 | LY_ARRAY_FOR(uni->types, u) { |
| 3147 | ret = warn_is_specific_type(uni->types[u], base); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3148 | if (ret) { |
| 3149 | /* found a suitable type */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3150 | return ret; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3151 | } |
| 3152 | } |
| 3153 | /* did not find any suitable type */ |
| 3154 | return 0; |
| 3155 | } else if (type->basetype == LY_TYPE_LEAFREF) { |
| 3156 | return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base); |
| 3157 | } |
| 3158 | |
| 3159 | return 0; |
| 3160 | } |
| 3161 | |
| 3162 | /** |
| 3163 | * @brief Get next type of a (union) type. |
| 3164 | * |
| 3165 | * @param[in] type Base type. |
| 3166 | * @param[in] prev_type Previously returned type. |
| 3167 | * @return Next type or NULL. |
| 3168 | */ |
| 3169 | static struct lysc_type * |
| 3170 | warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type) |
| 3171 | { |
| 3172 | struct lysc_type_union *uni; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3173 | ly_bool found = 0; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3174 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3175 | |
| 3176 | switch (type->basetype) { |
| 3177 | case LY_TYPE_UNION: |
| 3178 | uni = (struct lysc_type_union *)type; |
| 3179 | if (!prev_type) { |
| 3180 | return uni->types[0]; |
| 3181 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3182 | LY_ARRAY_FOR(uni->types, u) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3183 | if (found) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3184 | return uni->types[u]; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3185 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3186 | if (prev_type == uni->types[u]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3187 | found = 1; |
| 3188 | } |
| 3189 | } |
| 3190 | return NULL; |
| 3191 | default: |
| 3192 | if (prev_type) { |
| 3193 | assert(type == prev_type); |
| 3194 | return NULL; |
| 3195 | } else { |
| 3196 | return type; |
| 3197 | } |
| 3198 | } |
| 3199 | } |
| 3200 | |
| 3201 | /** |
| 3202 | * @brief Test whether 2 types have a common type. |
| 3203 | * |
| 3204 | * @param[in] type1 First type. |
| 3205 | * @param[in] type2 Second type. |
| 3206 | * @return 1 if they do, 0 otherwise. |
| 3207 | */ |
| 3208 | static int |
| 3209 | warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2) |
| 3210 | { |
| 3211 | struct lysc_type *t1, *rt1, *t2, *rt2; |
| 3212 | |
| 3213 | t1 = NULL; |
| 3214 | while ((t1 = warn_is_equal_type_next_type(type1, t1))) { |
| 3215 | if (t1->basetype == LY_TYPE_LEAFREF) { |
| 3216 | rt1 = ((struct lysc_type_leafref *)t1)->realtype; |
| 3217 | } else { |
| 3218 | rt1 = t1; |
| 3219 | } |
| 3220 | |
| 3221 | t2 = NULL; |
| 3222 | while ((t2 = warn_is_equal_type_next_type(type2, t2))) { |
| 3223 | if (t2->basetype == LY_TYPE_LEAFREF) { |
| 3224 | rt2 = ((struct lysc_type_leafref *)t2)->realtype; |
| 3225 | } else { |
| 3226 | rt2 = t2; |
| 3227 | } |
| 3228 | |
| 3229 | if (rt2->basetype == rt1->basetype) { |
| 3230 | /* match found */ |
| 3231 | return 1; |
| 3232 | } |
| 3233 | } |
| 3234 | } |
| 3235 | |
| 3236 | return 0; |
| 3237 | } |
| 3238 | |
| 3239 | /** |
| 3240 | * @brief Check both operands of comparison operators. |
| 3241 | * |
| 3242 | * @param[in] ctx Context for errors. |
| 3243 | * @param[in] set1 First operand set. |
| 3244 | * @param[in] set2 Second operand set. |
| 3245 | * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!='). |
| 3246 | * @param[in] expr Start of the expression to print with the warning. |
| 3247 | * @param[in] tok_pos Token position. |
| 3248 | */ |
| 3249 | static void |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3250 | 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] | 3251 | { |
| 3252 | struct lysc_node_leaf *node1, *node2; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3253 | ly_bool leaves = 1, warning = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3254 | |
| 3255 | node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1); |
| 3256 | node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2); |
| 3257 | |
| 3258 | if (!node1 && !node2) { |
| 3259 | /* no node-sets involved, nothing to do */ |
| 3260 | return; |
| 3261 | } |
| 3262 | |
| 3263 | if (node1) { |
| 3264 | if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3265 | LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name); |
| 3266 | warning = 1; |
| 3267 | leaves = 0; |
| 3268 | } else if (numbers_only && !warn_is_numeric_type(node1->type)) { |
| 3269 | LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name); |
| 3270 | warning = 1; |
| 3271 | } |
| 3272 | } |
| 3273 | |
| 3274 | if (node2) { |
| 3275 | if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3276 | LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name); |
| 3277 | warning = 1; |
| 3278 | leaves = 0; |
| 3279 | } else if (numbers_only && !warn_is_numeric_type(node2->type)) { |
| 3280 | LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name); |
| 3281 | warning = 1; |
| 3282 | } |
| 3283 | } |
| 3284 | |
| 3285 | if (node1 && node2 && leaves && !numbers_only) { |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3286 | if ((warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type)) || |
| 3287 | (!warn_is_numeric_type(node1->type) && warn_is_numeric_type(node2->type)) || |
| 3288 | (!warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type) && |
| 3289 | !warn_is_equal_type(node1->type, node2->type))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3290 | LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name); |
| 3291 | warning = 1; |
| 3292 | } |
| 3293 | } |
| 3294 | |
| 3295 | if (warning) { |
| 3296 | LOGWRN(ctx, "Previous warning generated by XPath subexpression[%u] \"%.20s\".", tok_pos, expr + tok_pos); |
| 3297 | } |
| 3298 | } |
| 3299 | |
| 3300 | /** |
| 3301 | * @brief Check that a value is valid for a leaf. If not applicable, does nothing. |
| 3302 | * |
| 3303 | * @param[in] exp Parsed XPath expression. |
| 3304 | * @param[in] set Set with the leaf/leaf-list. |
| 3305 | * @param[in] val_exp Index of the value (literal/number) in @p exp. |
| 3306 | * @param[in] equal_exp Index of the start of the equality expression in @p exp. |
| 3307 | * @param[in] last_equal_exp Index of the end of the equality expression in @p exp. |
| 3308 | */ |
| 3309 | static void |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 3310 | warn_equality_value(const struct lyxp_expr *exp, struct lyxp_set *set, uint16_t val_exp, uint16_t equal_exp, |
| 3311 | uint16_t last_equal_exp) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3312 | { |
| 3313 | struct lysc_node *scnode; |
| 3314 | struct lysc_type *type; |
| 3315 | char *value; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3316 | struct lyd_value storage; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3317 | LY_ERR rc; |
| 3318 | struct ly_err_item *err = NULL; |
| 3319 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3320 | if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && |
| 3321 | ((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] | 3322 | /* check that the node can have the specified value */ |
| 3323 | if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) { |
| 3324 | value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2); |
| 3325 | } else { |
| 3326 | value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]); |
| 3327 | } |
| 3328 | if (!value) { |
| 3329 | LOGMEM(set->ctx); |
| 3330 | return; |
| 3331 | } |
| 3332 | |
| 3333 | if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) { |
| 3334 | 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] | 3335 | " 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] | 3336 | 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] | 3337 | (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] | 3338 | exp->expr + exp->tok_pos[equal_exp]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3339 | } |
| 3340 | |
| 3341 | type = ((struct lysc_node_leaf *)scnode)->type; |
| 3342 | if (type->basetype != LY_TYPE_IDENT) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 3343 | 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] | 3344 | LYD_HINT_DATA, scnode, &storage, NULL, &err); |
Michal Vasko | bf42e83 | 2020-11-23 16:59:42 +0100 | [diff] [blame] | 3345 | if (rc == LY_EINCOMPLETE) { |
| 3346 | rc = LY_SUCCESS; |
| 3347 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3348 | |
| 3349 | if (err) { |
| 3350 | LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg); |
| 3351 | ly_err_free(err); |
| 3352 | } else if (rc != LY_SUCCESS) { |
| 3353 | LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value); |
| 3354 | } |
| 3355 | if (rc != LY_SUCCESS) { |
| 3356 | 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] | 3357 | (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] | 3358 | exp->expr + exp->tok_pos[equal_exp]); |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3359 | } else { |
| 3360 | type->plugin->free(set->ctx, &storage); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3361 | } |
| 3362 | } |
| 3363 | free(value); |
| 3364 | } |
| 3365 | } |
| 3366 | |
| 3367 | /* |
| 3368 | * XPath functions |
| 3369 | */ |
| 3370 | |
| 3371 | /** |
| 3372 | * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN |
| 3373 | * depending on whether the first node bit value from the second argument is set. |
| 3374 | * |
| 3375 | * @param[in] args Array of arguments. |
| 3376 | * @param[in] arg_count Count of elements in @p args. |
| 3377 | * @param[in,out] set Context and result set at the same time. |
| 3378 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3379 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3380 | */ |
| 3381 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3382 | 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] | 3383 | { |
| 3384 | struct lyd_node_term *leaf; |
| 3385 | struct lysc_node_leaf *sleaf; |
| 3386 | struct lysc_type_bits *bits; |
| 3387 | LY_ERR rc = LY_SUCCESS; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3388 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3389 | |
| 3390 | if (options & LYXP_SCNODE_ALL) { |
| 3391 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3392 | 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] | 3393 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3394 | 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] | 3395 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) { |
| 3396 | 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] | 3397 | } |
| 3398 | |
| 3399 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 3400 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3401 | 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] | 3402 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3403 | 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] | 3404 | } |
| 3405 | } |
| 3406 | set_scnode_clear_ctx(set); |
| 3407 | return rc; |
| 3408 | } |
| 3409 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3410 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 3411 | LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), |
| 3412 | "bit-is-set(node-set, string)"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3413 | return LY_EVALID; |
| 3414 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3415 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3416 | LY_CHECK_RET(rc); |
| 3417 | |
| 3418 | set_fill_boolean(set, 0); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3419 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3420 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3421 | if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && |
| 3422 | (((struct lysc_node_leaf *)leaf->schema)->type->basetype == LY_TYPE_BITS)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3423 | bits = (struct lysc_type_bits *)((struct lysc_node_leaf *)leaf->schema)->type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3424 | LY_ARRAY_FOR(bits->bits, u) { |
| 3425 | if (!strcmp(bits->bits[u].name, args[1]->val.str)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3426 | set_fill_boolean(set, 1); |
| 3427 | break; |
| 3428 | } |
| 3429 | } |
| 3430 | } |
| 3431 | } |
| 3432 | |
| 3433 | return LY_SUCCESS; |
| 3434 | } |
| 3435 | |
| 3436 | /** |
| 3437 | * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN |
| 3438 | * with the argument converted to boolean. |
| 3439 | * |
| 3440 | * @param[in] args Array of arguments. |
| 3441 | * @param[in] arg_count Count of elements in @p args. |
| 3442 | * @param[in,out] set Context and result set at the same time. |
| 3443 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3444 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3445 | */ |
| 3446 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3447 | 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] | 3448 | { |
| 3449 | LY_ERR rc; |
| 3450 | |
| 3451 | if (options & LYXP_SCNODE_ALL) { |
| 3452 | set_scnode_clear_ctx(set); |
| 3453 | return LY_SUCCESS; |
| 3454 | } |
| 3455 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3456 | rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3457 | LY_CHECK_RET(rc); |
| 3458 | set_fill_set(set, args[0]); |
| 3459 | |
| 3460 | return LY_SUCCESS; |
| 3461 | } |
| 3462 | |
| 3463 | /** |
| 3464 | * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER |
| 3465 | * with the first argument rounded up to the nearest integer. |
| 3466 | * |
| 3467 | * @param[in] args Array of arguments. |
| 3468 | * @param[in] arg_count Count of elements in @p args. |
| 3469 | * @param[in,out] set Context and result set at the same time. |
| 3470 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3471 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3472 | */ |
| 3473 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3474 | 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] | 3475 | { |
| 3476 | struct lysc_node_leaf *sleaf; |
| 3477 | LY_ERR rc = LY_SUCCESS; |
| 3478 | |
| 3479 | if (options & LYXP_SCNODE_ALL) { |
| 3480 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3481 | 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] | 3482 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3483 | 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] | 3484 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) { |
| 3485 | 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] | 3486 | } |
| 3487 | set_scnode_clear_ctx(set); |
| 3488 | return rc; |
| 3489 | } |
| 3490 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3491 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3492 | LY_CHECK_RET(rc); |
| 3493 | if ((long long)args[0]->val.num != args[0]->val.num) { |
| 3494 | set_fill_number(set, ((long long)args[0]->val.num) + 1); |
| 3495 | } else { |
| 3496 | set_fill_number(set, args[0]->val.num); |
| 3497 | } |
| 3498 | |
| 3499 | return LY_SUCCESS; |
| 3500 | } |
| 3501 | |
| 3502 | /** |
| 3503 | * @brief Execute the XPath concat(string, string, string*) function. |
| 3504 | * Returns LYXP_SET_STRING with the concatenation of all the arguments. |
| 3505 | * |
| 3506 | * @param[in] args Array of arguments. |
| 3507 | * @param[in] arg_count Count of elements in @p args. |
| 3508 | * @param[in,out] set Context and result set at the same time. |
| 3509 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3510 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3511 | */ |
| 3512 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3513 | xpath_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] | 3514 | { |
| 3515 | uint16_t i; |
| 3516 | char *str = NULL; |
| 3517 | size_t used = 1; |
| 3518 | LY_ERR rc = LY_SUCCESS; |
| 3519 | struct lysc_node_leaf *sleaf; |
| 3520 | |
| 3521 | if (options & LYXP_SCNODE_ALL) { |
| 3522 | for (i = 0; i < arg_count; ++i) { |
| 3523 | if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) { |
| 3524 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3525 | LOGWRN(set->ctx, "Argument #%u of %s is a %s node \"%s\".", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3526 | i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3527 | } else if (!warn_is_string_type(sleaf->type)) { |
Radek Krejci | 70124c8 | 2020-08-14 22:17:03 +0200 | [diff] [blame] | 3528 | 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] | 3529 | } |
| 3530 | } |
| 3531 | } |
| 3532 | set_scnode_clear_ctx(set); |
| 3533 | return rc; |
| 3534 | } |
| 3535 | |
| 3536 | for (i = 0; i < arg_count; ++i) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3537 | rc = lyxp_set_cast(args[i], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3538 | if (rc != LY_SUCCESS) { |
| 3539 | free(str); |
| 3540 | return rc; |
| 3541 | } |
| 3542 | |
| 3543 | str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char)); |
| 3544 | LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM); |
| 3545 | strcpy(str + used - 1, args[i]->val.str); |
| 3546 | used += strlen(args[i]->val.str); |
| 3547 | } |
| 3548 | |
| 3549 | /* free, kind of */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3550 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3551 | set->type = LYXP_SET_STRING; |
| 3552 | set->val.str = str; |
| 3553 | |
| 3554 | return LY_SUCCESS; |
| 3555 | } |
| 3556 | |
| 3557 | /** |
| 3558 | * @brief Execute the XPath contains(string, string) function. |
| 3559 | * Returns LYXP_SET_BOOLEAN whether the second argument can |
| 3560 | * be found in the first or not. |
| 3561 | * |
| 3562 | * @param[in] args Array of arguments. |
| 3563 | * @param[in] arg_count Count of elements in @p args. |
| 3564 | * @param[in,out] set Context and result set at the same time. |
| 3565 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3566 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3567 | */ |
| 3568 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3569 | 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] | 3570 | { |
| 3571 | struct lysc_node_leaf *sleaf; |
| 3572 | LY_ERR rc = LY_SUCCESS; |
| 3573 | |
| 3574 | if (options & LYXP_SCNODE_ALL) { |
| 3575 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3576 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3577 | 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] | 3578 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3579 | 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] | 3580 | } |
| 3581 | } |
| 3582 | |
| 3583 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 3584 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3585 | 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] | 3586 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3587 | 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] | 3588 | } |
| 3589 | } |
| 3590 | set_scnode_clear_ctx(set); |
| 3591 | return rc; |
| 3592 | } |
| 3593 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3594 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3595 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3596 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3597 | LY_CHECK_RET(rc); |
| 3598 | |
| 3599 | if (strstr(args[0]->val.str, args[1]->val.str)) { |
| 3600 | set_fill_boolean(set, 1); |
| 3601 | } else { |
| 3602 | set_fill_boolean(set, 0); |
| 3603 | } |
| 3604 | |
| 3605 | return LY_SUCCESS; |
| 3606 | } |
| 3607 | |
| 3608 | /** |
| 3609 | * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER |
| 3610 | * with the size of the node-set from the argument. |
| 3611 | * |
| 3612 | * @param[in] args Array of arguments. |
| 3613 | * @param[in] arg_count Count of elements in @p args. |
| 3614 | * @param[in,out] set Context and result set at the same time. |
| 3615 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3616 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3617 | */ |
| 3618 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3619 | 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] | 3620 | { |
| 3621 | struct lysc_node *scnode = NULL; |
| 3622 | LY_ERR rc = LY_SUCCESS; |
| 3623 | |
| 3624 | if (options & LYXP_SCNODE_ALL) { |
| 3625 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(scnode = warn_get_scnode_in_ctx(args[0]))) { |
| 3626 | 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] | 3627 | } |
| 3628 | set_scnode_clear_ctx(set); |
| 3629 | return rc; |
| 3630 | } |
| 3631 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3632 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 3633 | LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, 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] | 3634 | return LY_EVALID; |
| 3635 | } |
| 3636 | |
| 3637 | set_fill_number(set, args[0]->used); |
| 3638 | return LY_SUCCESS; |
| 3639 | } |
| 3640 | |
| 3641 | /** |
| 3642 | * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET |
| 3643 | * with the context with the intial node. |
| 3644 | * |
| 3645 | * @param[in] args Array of arguments. |
| 3646 | * @param[in] arg_count Count of elements in @p args. |
| 3647 | * @param[in,out] set Context and result set at the same time. |
| 3648 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3649 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3650 | */ |
| 3651 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3652 | 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] | 3653 | { |
| 3654 | if (arg_count || args) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 3655 | LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INARGCOUNT, arg_count, "current()"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3656 | return LY_EVALID; |
| 3657 | } |
| 3658 | |
| 3659 | if (options & LYXP_SCNODE_ALL) { |
| 3660 | set_scnode_clear_ctx(set); |
| 3661 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 3662 | 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] | 3663 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3664 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3665 | |
| 3666 | /* position is filled later */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 3667 | set_insert_node(set, set->cur_node, 0, LYXP_NODE_ELEM, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3668 | } |
| 3669 | |
| 3670 | return LY_SUCCESS; |
| 3671 | } |
| 3672 | |
| 3673 | /** |
| 3674 | * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either |
| 3675 | * leafref or instance-identifier target node(s). |
| 3676 | * |
| 3677 | * @param[in] args Array of arguments. |
| 3678 | * @param[in] arg_count Count of elements in @p args. |
| 3679 | * @param[in,out] set Context and result set at the same time. |
| 3680 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3681 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3682 | */ |
| 3683 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3684 | 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] | 3685 | { |
| 3686 | struct lyd_node_term *leaf; |
Michal Vasko | 42e497c | 2020-01-06 08:38:25 +0100 | [diff] [blame] | 3687 | struct lysc_node_leaf *sleaf = NULL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3688 | struct lysc_type_leafref *lref; |
Michal Vasko | ae9e4cb | 2019-09-25 08:43:05 +0200 | [diff] [blame] | 3689 | const struct lysc_node *target; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3690 | struct ly_path *p; |
| 3691 | struct lyd_node *node; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3692 | char *errmsg = NULL; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 3693 | uint8_t oper; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3694 | LY_ERR rc = LY_SUCCESS; |
| 3695 | |
| 3696 | if (options & LYXP_SCNODE_ALL) { |
| 3697 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3698 | 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] | 3699 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3700 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3701 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) && !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) { |
| 3702 | 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] | 3703 | __func__, sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3704 | } |
| 3705 | set_scnode_clear_ctx(set); |
Michal Vasko | 42e497c | 2020-01-06 08:38:25 +0100 | [diff] [blame] | 3706 | if (sleaf && (sleaf->type->basetype == LY_TYPE_LEAFREF)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3707 | lref = (struct lysc_type_leafref *)sleaf->type; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 3708 | oper = lysc_is_output((struct lysc_node *)sleaf) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3709 | |
| 3710 | /* it was already evaluated on schema, it must succeed */ |
Michal Vasko | c0792ca | 2020-12-01 12:03:21 +0100 | [diff] [blame] | 3711 | rc = ly_path_compile(set->ctx, lref->cur_mod, (struct lysc_node *)sleaf, lref->path, |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 3712 | LY_PATH_LREF_TRUE, oper, LY_PATH_TARGET_MANY, LY_PREF_SCHEMA_RESOLVED, lref->prefixes, NULL, &p); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3713 | assert(!rc); |
| 3714 | |
| 3715 | /* get the target node */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3716 | target = p[LY_ARRAY_COUNT(p) - 1].node; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3717 | ly_path_free(set->ctx, p); |
| 3718 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 3719 | 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] | 3720 | } |
| 3721 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3722 | return rc; |
| 3723 | } |
| 3724 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3725 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 3726 | LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, 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] | 3727 | return LY_EVALID; |
| 3728 | } |
| 3729 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3730 | lyxp_set_free_content(set); |
| 3731 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3732 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
| 3733 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 3734 | if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 3735 | if (sleaf->type->basetype == LY_TYPE_LEAFREF) { |
| 3736 | /* find leafref target */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3737 | if (ly_type_find_leafref((struct lysc_type_leafref *)sleaf->type, (struct lyd_node *)leaf, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3738 | &leaf->value, set->tree, &node, &errmsg)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3739 | LOGERR(set->ctx, LY_EVALID, errmsg); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3740 | free(errmsg); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3741 | return LY_EVALID; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3742 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3743 | } else { |
| 3744 | assert(sleaf->type->basetype == LY_TYPE_INST); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3745 | if (ly_path_eval(leaf->value.target, set->tree, &node)) { |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3746 | 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] | 3747 | LYD_CANON_VALUE(leaf)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3748 | return LY_EVALID; |
| 3749 | } |
| 3750 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3751 | |
| 3752 | /* insert it */ |
| 3753 | set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3754 | } |
| 3755 | } |
| 3756 | |
| 3757 | return LY_SUCCESS; |
| 3758 | } |
| 3759 | |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3760 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3761 | 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] | 3762 | { |
| 3763 | uint16_t i; |
| 3764 | LY_ARRAY_COUNT_TYPE u; |
| 3765 | struct lyd_node_term *leaf; |
| 3766 | struct lysc_node_leaf *sleaf; |
| 3767 | struct lyd_meta *meta; |
| 3768 | struct lyd_value data = {0}, *val; |
| 3769 | struct ly_err_item *err = NULL; |
| 3770 | LY_ERR rc = LY_SUCCESS; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3771 | ly_bool found; |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3772 | |
| 3773 | if (options & LYXP_SCNODE_ALL) { |
| 3774 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3775 | LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", func); |
| 3776 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3777 | 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] | 3778 | sleaf->name); |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3779 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) { |
| 3780 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", func, sleaf->name); |
| 3781 | } |
| 3782 | |
| 3783 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 3784 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3785 | 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] | 3786 | sleaf->name); |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3787 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3788 | LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", func, sleaf->name); |
| 3789 | } |
| 3790 | } |
| 3791 | set_scnode_clear_ctx(set); |
| 3792 | return rc; |
| 3793 | } |
| 3794 | |
| 3795 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 3796 | LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3797 | "derived-from(-or-self)(node-set, string)"); |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3798 | return LY_EVALID; |
| 3799 | } |
| 3800 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
| 3801 | LY_CHECK_RET(rc); |
| 3802 | |
| 3803 | set_fill_boolean(set, 0); |
| 3804 | found = 0; |
| 3805 | for (i = 0; i < args[0]->used; ++i) { |
| 3806 | if ((args[0]->val.nodes[i].type != LYXP_NODE_ELEM) && (args[0]->val.nodes[i].type != LYXP_NODE_META)) { |
| 3807 | continue; |
| 3808 | } |
| 3809 | |
| 3810 | if (args[0]->val.nodes[i].type == LYXP_NODE_ELEM) { |
| 3811 | leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node; |
| 3812 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 3813 | val = &leaf->value; |
| 3814 | if (!(sleaf->nodetype & LYD_NODE_TERM) || (leaf->value.realtype->basetype != LY_TYPE_IDENT)) { |
| 3815 | /* uninteresting */ |
| 3816 | continue; |
| 3817 | } |
| 3818 | |
| 3819 | /* store args[1] as ident */ |
| 3820 | 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] | 3821 | 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] | 3822 | } else { |
| 3823 | meta = args[0]->val.meta[i].meta; |
| 3824 | val = &meta->value; |
| 3825 | if (val->realtype->basetype != LY_TYPE_IDENT) { |
| 3826 | /* uninteresting */ |
| 3827 | continue; |
| 3828 | } |
| 3829 | |
| 3830 | /* store args[1] as ident */ |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 3831 | 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] | 3832 | 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] | 3833 | } |
| 3834 | |
| 3835 | if (err) { |
Michal Vasko | 177d0ed | 2020-11-23 16:43:03 +0100 | [diff] [blame] | 3836 | ly_err_print(set->ctx, err); |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3837 | ly_err_free(err); |
| 3838 | } |
| 3839 | LY_CHECK_RET(rc); |
| 3840 | |
| 3841 | /* finally check the identity itself */ |
| 3842 | if (self_match && (data.ident == val->ident)) { |
| 3843 | set_fill_boolean(set, 1); |
| 3844 | found = 1; |
| 3845 | } |
| 3846 | if (!found) { |
| 3847 | LY_ARRAY_FOR(data.ident->derived, u) { |
| 3848 | if (data.ident->derived[u] == val->ident) { |
| 3849 | set_fill_boolean(set, 1); |
| 3850 | found = 1; |
| 3851 | break; |
| 3852 | } |
| 3853 | } |
| 3854 | } |
| 3855 | |
| 3856 | /* free temporary value */ |
| 3857 | val->realtype->plugin->free(set->ctx, &data); |
| 3858 | if (found) { |
| 3859 | break; |
| 3860 | } |
| 3861 | } |
| 3862 | |
| 3863 | return LY_SUCCESS; |
| 3864 | } |
| 3865 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3866 | /** |
| 3867 | * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending |
| 3868 | * on whether the first argument nodes contain a node of an identity derived from the second |
| 3869 | * argument identity. |
| 3870 | * |
| 3871 | * @param[in] args Array of arguments. |
| 3872 | * @param[in] arg_count Count of elements in @p args. |
| 3873 | * @param[in,out] set Context and result set at the same time. |
| 3874 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3875 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3876 | */ |
| 3877 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3878 | 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] | 3879 | { |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3880 | return xpath_derived_(args, set, options, 0, __func__); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3881 | } |
| 3882 | |
| 3883 | /** |
| 3884 | * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending |
| 3885 | * on whether the first argument nodes contain a node of an identity that either is or is derived from |
| 3886 | * the second argument identity. |
| 3887 | * |
| 3888 | * @param[in] args Array of arguments. |
| 3889 | * @param[in] arg_count Count of elements in @p args. |
| 3890 | * @param[in,out] set Context and result set at the same time. |
| 3891 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3892 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3893 | */ |
| 3894 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3895 | 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] | 3896 | { |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3897 | return xpath_derived_(args, set, options, 1, __func__); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3898 | } |
| 3899 | |
| 3900 | /** |
| 3901 | * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER |
| 3902 | * with the integer value of the first node's enum value, otherwise NaN. |
| 3903 | * |
| 3904 | * @param[in] args Array of arguments. |
| 3905 | * @param[in] arg_count Count of elements in @p args. |
| 3906 | * @param[in,out] set Context and result set at the same time. |
| 3907 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3908 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3909 | */ |
| 3910 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3911 | 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] | 3912 | { |
| 3913 | struct lyd_node_term *leaf; |
| 3914 | struct lysc_node_leaf *sleaf; |
| 3915 | LY_ERR rc = LY_SUCCESS; |
| 3916 | |
| 3917 | if (options & LYXP_SCNODE_ALL) { |
| 3918 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3919 | 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] | 3920 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3921 | 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] | 3922 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) { |
| 3923 | 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] | 3924 | } |
| 3925 | set_scnode_clear_ctx(set); |
| 3926 | return rc; |
| 3927 | } |
| 3928 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3929 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 3930 | LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, 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] | 3931 | return LY_EVALID; |
| 3932 | } |
| 3933 | |
| 3934 | set_fill_number(set, NAN); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3935 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3936 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
| 3937 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 3938 | if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) { |
| 3939 | set_fill_number(set, leaf->value.enum_item->value); |
| 3940 | } |
| 3941 | } |
| 3942 | |
| 3943 | return LY_SUCCESS; |
| 3944 | } |
| 3945 | |
| 3946 | /** |
| 3947 | * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN |
| 3948 | * with false value. |
| 3949 | * |
| 3950 | * @param[in] args Array of arguments. |
| 3951 | * @param[in] arg_count Count of elements in @p args. |
| 3952 | * @param[in,out] set Context and result set at the same time. |
| 3953 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3954 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3955 | */ |
| 3956 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3957 | 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] | 3958 | { |
| 3959 | if (options & LYXP_SCNODE_ALL) { |
| 3960 | set_scnode_clear_ctx(set); |
| 3961 | return LY_SUCCESS; |
| 3962 | } |
| 3963 | |
| 3964 | set_fill_boolean(set, 0); |
| 3965 | return LY_SUCCESS; |
| 3966 | } |
| 3967 | |
| 3968 | /** |
| 3969 | * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER |
| 3970 | * with the first argument floored (truncated). |
| 3971 | * |
| 3972 | * @param[in] args Array of arguments. |
| 3973 | * @param[in] arg_count Count of elements in @p args. |
| 3974 | * @param[in,out] set Context and result set at the same time. |
| 3975 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3976 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3977 | */ |
| 3978 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3979 | 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] | 3980 | { |
| 3981 | LY_ERR rc; |
| 3982 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3983 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3984 | LY_CHECK_RET(rc); |
| 3985 | if (isfinite(args[0]->val.num)) { |
| 3986 | set_fill_number(set, (long long)args[0]->val.num); |
| 3987 | } |
| 3988 | |
| 3989 | return LY_SUCCESS; |
| 3990 | } |
| 3991 | |
| 3992 | /** |
| 3993 | * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN |
| 3994 | * whether the language of the text matches the one from the argument. |
| 3995 | * |
| 3996 | * @param[in] args Array of arguments. |
| 3997 | * @param[in] arg_count Count of elements in @p args. |
| 3998 | * @param[in,out] set Context and result set at the same time. |
| 3999 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4000 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4001 | */ |
| 4002 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4003 | 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] | 4004 | { |
| 4005 | const struct lyd_node *node; |
| 4006 | struct lysc_node_leaf *sleaf; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4007 | struct lyd_meta *meta = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4008 | const char *val; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4009 | LY_ERR rc = LY_SUCCESS; |
| 4010 | |
| 4011 | if (options & LYXP_SCNODE_ALL) { |
| 4012 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4013 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4014 | 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] | 4015 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4016 | 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] | 4017 | } |
| 4018 | } |
| 4019 | set_scnode_clear_ctx(set); |
| 4020 | return rc; |
| 4021 | } |
| 4022 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4023 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4024 | LY_CHECK_RET(rc); |
| 4025 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4026 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 4027 | LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INCTX, print_set_type(set), "lang(string)"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4028 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4029 | } else if (!set->used) { |
| 4030 | set_fill_boolean(set, 0); |
| 4031 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4032 | } |
| 4033 | |
| 4034 | switch (set->val.nodes[0].type) { |
| 4035 | case LYXP_NODE_ELEM: |
| 4036 | case LYXP_NODE_TEXT: |
| 4037 | node = set->val.nodes[0].node; |
| 4038 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4039 | case LYXP_NODE_META: |
| 4040 | node = set->val.meta[0].meta->parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4041 | break; |
| 4042 | default: |
| 4043 | /* nothing to do with roots */ |
| 4044 | set_fill_boolean(set, 0); |
| 4045 | return LY_SUCCESS; |
| 4046 | } |
| 4047 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4048 | /* find lang metadata */ |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 4049 | for ( ; node; node = (struct lyd_node *)node->parent) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4050 | for (meta = node->meta; meta; meta = meta->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4051 | /* annotations */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4052 | 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] | 4053 | break; |
| 4054 | } |
| 4055 | } |
| 4056 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4057 | if (meta) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4058 | break; |
| 4059 | } |
| 4060 | } |
| 4061 | |
| 4062 | /* compare languages */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4063 | if (!meta) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4064 | set_fill_boolean(set, 0); |
| 4065 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4066 | uint64_t i; |
| 4067 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 4068 | val = meta->value.canonical; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4069 | for (i = 0; args[0]->val.str[i]; ++i) { |
| 4070 | if (tolower(args[0]->val.str[i]) != tolower(val[i])) { |
| 4071 | set_fill_boolean(set, 0); |
| 4072 | break; |
| 4073 | } |
| 4074 | } |
| 4075 | if (!args[0]->val.str[i]) { |
| 4076 | if (!val[i] || (val[i] == '-')) { |
| 4077 | set_fill_boolean(set, 1); |
| 4078 | } else { |
| 4079 | set_fill_boolean(set, 0); |
| 4080 | } |
| 4081 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4082 | } |
| 4083 | |
| 4084 | return LY_SUCCESS; |
| 4085 | } |
| 4086 | |
| 4087 | /** |
| 4088 | * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER |
| 4089 | * with the context size. |
| 4090 | * |
| 4091 | * @param[in] args Array of arguments. |
| 4092 | * @param[in] arg_count Count of elements in @p args. |
| 4093 | * @param[in,out] set Context and result set at the same time. |
| 4094 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4095 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4096 | */ |
| 4097 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4098 | 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] | 4099 | { |
| 4100 | if (options & LYXP_SCNODE_ALL) { |
| 4101 | set_scnode_clear_ctx(set); |
| 4102 | return LY_SUCCESS; |
| 4103 | } |
| 4104 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4105 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 4106 | LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INCTX, print_set_type(set), "last()"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4107 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4108 | } else if (!set->used) { |
| 4109 | set_fill_number(set, 0); |
| 4110 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4111 | } |
| 4112 | |
| 4113 | set_fill_number(set, set->ctx_size); |
| 4114 | return LY_SUCCESS; |
| 4115 | } |
| 4116 | |
| 4117 | /** |
| 4118 | * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING |
| 4119 | * with the node name without namespace from the argument or the context. |
| 4120 | * |
| 4121 | * @param[in] args Array of arguments. |
| 4122 | * @param[in] arg_count Count of elements in @p args. |
| 4123 | * @param[in,out] set Context and result set at the same time. |
| 4124 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4125 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4126 | */ |
| 4127 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4128 | 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] | 4129 | { |
| 4130 | struct lyxp_set_node *item; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 4131 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4132 | /* suppress unused variable warning */ |
| 4133 | (void)options; |
| 4134 | |
| 4135 | if (options & LYXP_SCNODE_ALL) { |
| 4136 | set_scnode_clear_ctx(set); |
| 4137 | return LY_SUCCESS; |
| 4138 | } |
| 4139 | |
| 4140 | if (arg_count) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4141 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 4142 | LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), |
| 4143 | "local-name(node-set?)"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4144 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4145 | } else if (!args[0]->used) { |
| 4146 | set_fill_string(set, "", 0); |
| 4147 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4148 | } |
| 4149 | |
| 4150 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4151 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4152 | |
| 4153 | item = &args[0]->val.nodes[0]; |
| 4154 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4155 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 4156 | LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INCTX, print_set_type(set), "local-name(node-set?)"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4157 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4158 | } else if (!set->used) { |
| 4159 | set_fill_string(set, "", 0); |
| 4160 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4161 | } |
| 4162 | |
| 4163 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4164 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4165 | |
| 4166 | item = &set->val.nodes[0]; |
| 4167 | } |
| 4168 | |
| 4169 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4170 | case LYXP_NODE_NONE: |
| 4171 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4172 | case LYXP_NODE_ROOT: |
| 4173 | case LYXP_NODE_ROOT_CONFIG: |
| 4174 | case LYXP_NODE_TEXT: |
| 4175 | set_fill_string(set, "", 0); |
| 4176 | break; |
| 4177 | case LYXP_NODE_ELEM: |
| 4178 | set_fill_string(set, item->node->schema->name, strlen(item->node->schema->name)); |
| 4179 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4180 | case LYXP_NODE_META: |
| 4181 | 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] | 4182 | break; |
| 4183 | } |
| 4184 | |
| 4185 | return LY_SUCCESS; |
| 4186 | } |
| 4187 | |
| 4188 | /** |
| 4189 | * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING |
| 4190 | * with the node name fully qualified (with namespace) from the argument or the context. |
| 4191 | * |
| 4192 | * @param[in] args Array of arguments. |
| 4193 | * @param[in] arg_count Count of elements in @p args. |
| 4194 | * @param[in,out] set Context and result set at the same time. |
| 4195 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4196 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4197 | */ |
| 4198 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4199 | 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] | 4200 | { |
| 4201 | struct lyxp_set_node *item; |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 4202 | struct lys_module *mod = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4203 | char *str; |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 4204 | const char *name = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4205 | |
| 4206 | if (options & LYXP_SCNODE_ALL) { |
| 4207 | set_scnode_clear_ctx(set); |
| 4208 | return LY_SUCCESS; |
| 4209 | } |
| 4210 | |
| 4211 | if (arg_count) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4212 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 4213 | LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, 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] | 4214 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4215 | } else if (!args[0]->used) { |
| 4216 | set_fill_string(set, "", 0); |
| 4217 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4218 | } |
| 4219 | |
| 4220 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4221 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4222 | |
| 4223 | item = &args[0]->val.nodes[0]; |
| 4224 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4225 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 4226 | LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INCTX, print_set_type(set), "name(node-set?)"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4227 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4228 | } else if (!set->used) { |
| 4229 | set_fill_string(set, "", 0); |
| 4230 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4231 | } |
| 4232 | |
| 4233 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4234 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4235 | |
| 4236 | item = &set->val.nodes[0]; |
| 4237 | } |
| 4238 | |
| 4239 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4240 | case LYXP_NODE_NONE: |
| 4241 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4242 | case LYXP_NODE_ROOT: |
| 4243 | case LYXP_NODE_ROOT_CONFIG: |
| 4244 | case LYXP_NODE_TEXT: |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 4245 | /* keep NULL */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4246 | break; |
| 4247 | case LYXP_NODE_ELEM: |
| 4248 | mod = item->node->schema->module; |
| 4249 | name = item->node->schema->name; |
| 4250 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4251 | case LYXP_NODE_META: |
| 4252 | mod = ((struct lyd_meta *)item->node)->annotation->module; |
| 4253 | name = ((struct lyd_meta *)item->node)->name; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4254 | break; |
| 4255 | } |
| 4256 | |
| 4257 | if (mod && name) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 4258 | 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] | 4259 | LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM); |
| 4260 | set_fill_string(set, str, strlen(str)); |
| 4261 | free(str); |
| 4262 | } else { |
| 4263 | set_fill_string(set, "", 0); |
| 4264 | } |
| 4265 | |
| 4266 | return LY_SUCCESS; |
| 4267 | } |
| 4268 | |
| 4269 | /** |
| 4270 | * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING |
| 4271 | * with the namespace of the node from the argument or the context. |
| 4272 | * |
| 4273 | * @param[in] args Array of arguments. |
| 4274 | * @param[in] arg_count Count of elements in @p args. |
| 4275 | * @param[in,out] set Context and result set at the same time. |
| 4276 | * @param[in] options XPath options. |
| 4277 | * @return LY_ERR (LY_EINVAL for wrong arguments on schema) |
| 4278 | */ |
| 4279 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4280 | 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] | 4281 | { |
| 4282 | struct lyxp_set_node *item; |
| 4283 | struct lys_module *mod; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 4284 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4285 | /* suppress unused variable warning */ |
| 4286 | (void)options; |
| 4287 | |
| 4288 | if (options & LYXP_SCNODE_ALL) { |
| 4289 | set_scnode_clear_ctx(set); |
| 4290 | return LY_SUCCESS; |
| 4291 | } |
| 4292 | |
| 4293 | if (arg_count) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4294 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 4295 | LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 4296 | "namespace-uri(node-set?)"); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4297 | return LY_EVALID; |
| 4298 | } else if (!args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4299 | set_fill_string(set, "", 0); |
| 4300 | return LY_SUCCESS; |
| 4301 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4302 | |
| 4303 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4304 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4305 | |
| 4306 | item = &args[0]->val.nodes[0]; |
| 4307 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4308 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 4309 | LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INCTX, print_set_type(set), "namespace-uri(node-set?)"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4310 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4311 | } else if (!set->used) { |
| 4312 | set_fill_string(set, "", 0); |
| 4313 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4314 | } |
| 4315 | |
| 4316 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4317 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4318 | |
| 4319 | item = &set->val.nodes[0]; |
| 4320 | } |
| 4321 | |
| 4322 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4323 | case LYXP_NODE_NONE: |
| 4324 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4325 | case LYXP_NODE_ROOT: |
| 4326 | case LYXP_NODE_ROOT_CONFIG: |
| 4327 | case LYXP_NODE_TEXT: |
| 4328 | set_fill_string(set, "", 0); |
| 4329 | break; |
| 4330 | case LYXP_NODE_ELEM: |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4331 | case LYXP_NODE_META: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4332 | if (item->type == LYXP_NODE_ELEM) { |
| 4333 | mod = item->node->schema->module; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4334 | } else { /* LYXP_NODE_META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4335 | /* annotations */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4336 | mod = ((struct lyd_meta *)item->node)->annotation->module; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4337 | } |
| 4338 | |
| 4339 | set_fill_string(set, mod->ns, strlen(mod->ns)); |
| 4340 | break; |
| 4341 | } |
| 4342 | |
| 4343 | return LY_SUCCESS; |
| 4344 | } |
| 4345 | |
| 4346 | /** |
| 4347 | * @brief Execute the XPath node() function (node type). Returns LYXP_SET_NODE_SET |
| 4348 | * with only nodes from the context. In practice it either leaves the context |
| 4349 | * as it is or returns an empty node set. |
| 4350 | * |
| 4351 | * @param[in] args Array of arguments. |
| 4352 | * @param[in] arg_count Count of elements in @p args. |
| 4353 | * @param[in,out] set Context and result set at the same time. |
| 4354 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4355 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4356 | */ |
| 4357 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4358 | 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] | 4359 | { |
| 4360 | if (options & LYXP_SCNODE_ALL) { |
| 4361 | set_scnode_clear_ctx(set); |
| 4362 | return LY_SUCCESS; |
| 4363 | } |
| 4364 | |
| 4365 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4366 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4367 | } |
| 4368 | return LY_SUCCESS; |
| 4369 | } |
| 4370 | |
| 4371 | /** |
| 4372 | * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING |
| 4373 | * with normalized value (no leading, trailing, double white spaces) of the node |
| 4374 | * from the argument or the context. |
| 4375 | * |
| 4376 | * @param[in] args Array of arguments. |
| 4377 | * @param[in] arg_count Count of elements in @p args. |
| 4378 | * @param[in,out] set Context and result set at the same time. |
| 4379 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4380 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4381 | */ |
| 4382 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4383 | 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] | 4384 | { |
| 4385 | uint16_t i, new_used; |
| 4386 | char *new; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 4387 | ly_bool have_spaces = 0, space_before = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4388 | struct lysc_node_leaf *sleaf; |
| 4389 | LY_ERR rc = LY_SUCCESS; |
| 4390 | |
| 4391 | if (options & LYXP_SCNODE_ALL) { |
| 4392 | if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4393 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4394 | 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] | 4395 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4396 | 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] | 4397 | } |
| 4398 | } |
| 4399 | set_scnode_clear_ctx(set); |
| 4400 | return rc; |
| 4401 | } |
| 4402 | |
| 4403 | if (arg_count) { |
| 4404 | set_fill_set(set, args[0]); |
| 4405 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4406 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4407 | LY_CHECK_RET(rc); |
| 4408 | |
| 4409 | /* is there any normalization necessary? */ |
| 4410 | for (i = 0; set->val.str[i]; ++i) { |
| 4411 | if (is_xmlws(set->val.str[i])) { |
| 4412 | if ((i == 0) || space_before || (!set->val.str[i + 1])) { |
| 4413 | have_spaces = 1; |
| 4414 | break; |
| 4415 | } |
| 4416 | space_before = 1; |
| 4417 | } else { |
| 4418 | space_before = 0; |
| 4419 | } |
| 4420 | } |
| 4421 | |
| 4422 | /* yep, there is */ |
| 4423 | if (have_spaces) { |
| 4424 | /* it's enough, at least one character will go, makes space for ending '\0' */ |
| 4425 | new = malloc(strlen(set->val.str) * sizeof(char)); |
| 4426 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 4427 | new_used = 0; |
| 4428 | |
| 4429 | space_before = 0; |
| 4430 | for (i = 0; set->val.str[i]; ++i) { |
| 4431 | if (is_xmlws(set->val.str[i])) { |
| 4432 | if ((i == 0) || space_before) { |
| 4433 | space_before = 1; |
| 4434 | continue; |
| 4435 | } else { |
| 4436 | space_before = 1; |
| 4437 | } |
| 4438 | } else { |
| 4439 | space_before = 0; |
| 4440 | } |
| 4441 | |
| 4442 | new[new_used] = (space_before ? ' ' : set->val.str[i]); |
| 4443 | ++new_used; |
| 4444 | } |
| 4445 | |
| 4446 | /* at worst there is one trailing space now */ |
| 4447 | if (new_used && is_xmlws(new[new_used - 1])) { |
| 4448 | --new_used; |
| 4449 | } |
| 4450 | |
| 4451 | new = ly_realloc(new, (new_used + 1) * sizeof(char)); |
| 4452 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 4453 | new[new_used] = '\0'; |
| 4454 | |
| 4455 | free(set->val.str); |
| 4456 | set->val.str = new; |
| 4457 | } |
| 4458 | |
| 4459 | return LY_SUCCESS; |
| 4460 | } |
| 4461 | |
| 4462 | /** |
| 4463 | * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN |
| 4464 | * with the argument converted to boolean and logically inverted. |
| 4465 | * |
| 4466 | * @param[in] args Array of arguments. |
| 4467 | * @param[in] arg_count Count of elements in @p args. |
| 4468 | * @param[in,out] set Context and result set at the same time. |
| 4469 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4470 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4471 | */ |
| 4472 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4473 | 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] | 4474 | { |
| 4475 | if (options & LYXP_SCNODE_ALL) { |
| 4476 | set_scnode_clear_ctx(set); |
| 4477 | return LY_SUCCESS; |
| 4478 | } |
| 4479 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4480 | lyxp_set_cast(args[0], LYXP_SET_BOOLEAN); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4481 | if (args[0]->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4482 | set_fill_boolean(set, 0); |
| 4483 | } else { |
| 4484 | set_fill_boolean(set, 1); |
| 4485 | } |
| 4486 | |
| 4487 | return LY_SUCCESS; |
| 4488 | } |
| 4489 | |
| 4490 | /** |
| 4491 | * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER |
| 4492 | * with the number representation of either the argument or the context. |
| 4493 | * |
| 4494 | * @param[in] args Array of arguments. |
| 4495 | * @param[in] arg_count Count of elements in @p args. |
| 4496 | * @param[in,out] set Context and result set at the same time. |
| 4497 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4498 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4499 | */ |
| 4500 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4501 | 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] | 4502 | { |
| 4503 | LY_ERR rc; |
| 4504 | |
| 4505 | if (options & LYXP_SCNODE_ALL) { |
| 4506 | set_scnode_clear_ctx(set); |
| 4507 | return LY_SUCCESS; |
| 4508 | } |
| 4509 | |
| 4510 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4511 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4512 | LY_CHECK_RET(rc); |
| 4513 | set_fill_set(set, args[0]); |
| 4514 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4515 | rc = lyxp_set_cast(set, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4516 | LY_CHECK_RET(rc); |
| 4517 | } |
| 4518 | |
| 4519 | return LY_SUCCESS; |
| 4520 | } |
| 4521 | |
| 4522 | /** |
| 4523 | * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER |
| 4524 | * with the context position. |
| 4525 | * |
| 4526 | * @param[in] args Array of arguments. |
| 4527 | * @param[in] arg_count Count of elements in @p args. |
| 4528 | * @param[in,out] set Context and result set at the same time. |
| 4529 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4530 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4531 | */ |
| 4532 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4533 | 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] | 4534 | { |
| 4535 | if (options & LYXP_SCNODE_ALL) { |
| 4536 | set_scnode_clear_ctx(set); |
| 4537 | return LY_SUCCESS; |
| 4538 | } |
| 4539 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4540 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 4541 | LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INCTX, print_set_type(set), "position()"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4542 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4543 | } else if (!set->used) { |
| 4544 | set_fill_number(set, 0); |
| 4545 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4546 | } |
| 4547 | |
| 4548 | set_fill_number(set, set->ctx_pos); |
| 4549 | |
| 4550 | /* UNUSED in 'Release' build type */ |
| 4551 | (void)options; |
| 4552 | return LY_SUCCESS; |
| 4553 | } |
| 4554 | |
| 4555 | /** |
| 4556 | * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN |
| 4557 | * depending on whether the second argument regex matches the first argument string. For details refer to |
| 4558 | * YANG 1.1 RFC section 10.2.1. |
| 4559 | * |
| 4560 | * @param[in] args Array of arguments. |
| 4561 | * @param[in] arg_count Count of elements in @p args. |
| 4562 | * @param[in,out] set Context and result set at the same time. |
| 4563 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4564 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4565 | */ |
| 4566 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4567 | 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] | 4568 | { |
| 4569 | struct lysc_pattern **patterns = NULL, **pattern; |
| 4570 | struct lysc_node_leaf *sleaf; |
| 4571 | char *path; |
| 4572 | LY_ERR rc = LY_SUCCESS; |
| 4573 | struct ly_err_item *err; |
| 4574 | |
| 4575 | if (options & LYXP_SCNODE_ALL) { |
| 4576 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4577 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4578 | 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] | 4579 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4580 | 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] | 4581 | } |
| 4582 | } |
| 4583 | |
| 4584 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4585 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4586 | 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] | 4587 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4588 | 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] | 4589 | } |
| 4590 | } |
| 4591 | set_scnode_clear_ctx(set); |
| 4592 | return rc; |
| 4593 | } |
| 4594 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4595 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4596 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4597 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4598 | LY_CHECK_RET(rc); |
| 4599 | |
| 4600 | LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM); |
| 4601 | *pattern = malloc(sizeof **pattern); |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 4602 | path = lyd_path(set->cur_node, LYD_PATH_LOG, NULL, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4603 | rc = lys_compile_type_pattern_check(set->ctx, path, args[1]->val.str, &(*pattern)->code); |
| 4604 | free(path); |
| 4605 | if (rc != LY_SUCCESS) { |
| 4606 | LY_ARRAY_FREE(patterns); |
| 4607 | return rc; |
| 4608 | } |
| 4609 | |
| 4610 | rc = ly_type_validate_patterns(patterns, args[0]->val.str, strlen(args[0]->val.str), &err); |
| 4611 | pcre2_code_free((*pattern)->code); |
| 4612 | free(*pattern); |
| 4613 | LY_ARRAY_FREE(patterns); |
| 4614 | if (rc && (rc != LY_EVALID)) { |
Michal Vasko | 177d0ed | 2020-11-23 16:43:03 +0100 | [diff] [blame] | 4615 | ly_err_print(set->ctx, err); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4616 | ly_err_free(err); |
| 4617 | return rc; |
| 4618 | } |
| 4619 | |
| 4620 | if (rc == LY_EVALID) { |
| 4621 | ly_err_free(err); |
| 4622 | set_fill_boolean(set, 0); |
| 4623 | } else { |
| 4624 | set_fill_boolean(set, 1); |
| 4625 | } |
| 4626 | |
| 4627 | return LY_SUCCESS; |
| 4628 | } |
| 4629 | |
| 4630 | /** |
| 4631 | * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER |
| 4632 | * with the rounded first argument. For details refer to |
| 4633 | * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round. |
| 4634 | * |
| 4635 | * @param[in] args Array of arguments. |
| 4636 | * @param[in] arg_count Count of elements in @p args. |
| 4637 | * @param[in,out] set Context and result set at the same time. |
| 4638 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4639 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4640 | */ |
| 4641 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4642 | 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] | 4643 | { |
| 4644 | struct lysc_node_leaf *sleaf; |
| 4645 | LY_ERR rc = LY_SUCCESS; |
| 4646 | |
| 4647 | if (options & LYXP_SCNODE_ALL) { |
| 4648 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4649 | 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] | 4650 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4651 | 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] | 4652 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) { |
| 4653 | 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] | 4654 | } |
| 4655 | set_scnode_clear_ctx(set); |
| 4656 | return rc; |
| 4657 | } |
| 4658 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4659 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4660 | LY_CHECK_RET(rc); |
| 4661 | |
| 4662 | /* cover only the cases where floor can't be used */ |
| 4663 | if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) { |
| 4664 | set_fill_number(set, -0.0f); |
| 4665 | } else { |
| 4666 | args[0]->val.num += 0.5; |
| 4667 | rc = xpath_floor(args, 1, args[0], options); |
| 4668 | LY_CHECK_RET(rc); |
| 4669 | set_fill_number(set, args[0]->val.num); |
| 4670 | } |
| 4671 | |
| 4672 | return LY_SUCCESS; |
| 4673 | } |
| 4674 | |
| 4675 | /** |
| 4676 | * @brief Execute the XPath starts-with(string, string) function. |
| 4677 | * Returns LYXP_SET_BOOLEAN whether the second argument is |
| 4678 | * the prefix of the first or not. |
| 4679 | * |
| 4680 | * @param[in] args Array of arguments. |
| 4681 | * @param[in] arg_count Count of elements in @p args. |
| 4682 | * @param[in,out] set Context and result set at the same time. |
| 4683 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4684 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4685 | */ |
| 4686 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4687 | 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] | 4688 | { |
| 4689 | struct lysc_node_leaf *sleaf; |
| 4690 | LY_ERR rc = LY_SUCCESS; |
| 4691 | |
| 4692 | if (options & LYXP_SCNODE_ALL) { |
| 4693 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4694 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4695 | 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] | 4696 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4697 | 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] | 4698 | } |
| 4699 | } |
| 4700 | |
| 4701 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4702 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4703 | 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] | 4704 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4705 | 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] | 4706 | } |
| 4707 | } |
| 4708 | set_scnode_clear_ctx(set); |
| 4709 | return rc; |
| 4710 | } |
| 4711 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4712 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4713 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4714 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4715 | LY_CHECK_RET(rc); |
| 4716 | |
| 4717 | if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) { |
| 4718 | set_fill_boolean(set, 0); |
| 4719 | } else { |
| 4720 | set_fill_boolean(set, 1); |
| 4721 | } |
| 4722 | |
| 4723 | return LY_SUCCESS; |
| 4724 | } |
| 4725 | |
| 4726 | /** |
| 4727 | * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING |
| 4728 | * with the string representation of either the argument or the context. |
| 4729 | * |
| 4730 | * @param[in] args Array of arguments. |
| 4731 | * @param[in] arg_count Count of elements in @p args. |
| 4732 | * @param[in,out] set Context and result set at the same time. |
| 4733 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4734 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4735 | */ |
| 4736 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4737 | 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] | 4738 | { |
| 4739 | LY_ERR rc; |
| 4740 | |
| 4741 | if (options & LYXP_SCNODE_ALL) { |
| 4742 | set_scnode_clear_ctx(set); |
| 4743 | return LY_SUCCESS; |
| 4744 | } |
| 4745 | |
| 4746 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4747 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4748 | LY_CHECK_RET(rc); |
| 4749 | set_fill_set(set, args[0]); |
| 4750 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4751 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4752 | LY_CHECK_RET(rc); |
| 4753 | } |
| 4754 | |
| 4755 | return LY_SUCCESS; |
| 4756 | } |
| 4757 | |
| 4758 | /** |
| 4759 | * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER |
| 4760 | * with the length of the string in either the argument or the context. |
| 4761 | * |
| 4762 | * @param[in] args Array of arguments. |
| 4763 | * @param[in] arg_count Count of elements in @p args. |
| 4764 | * @param[in,out] set Context and result set at the same time. |
| 4765 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4766 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4767 | */ |
| 4768 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4769 | 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] | 4770 | { |
| 4771 | struct lysc_node_leaf *sleaf; |
| 4772 | LY_ERR rc = LY_SUCCESS; |
| 4773 | |
| 4774 | if (options & LYXP_SCNODE_ALL) { |
| 4775 | if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4776 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4777 | 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] | 4778 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4779 | 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] | 4780 | } |
| 4781 | } |
| 4782 | if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) { |
| 4783 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4784 | 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] | 4785 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4786 | 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] | 4787 | } |
| 4788 | } |
| 4789 | set_scnode_clear_ctx(set); |
| 4790 | return rc; |
| 4791 | } |
| 4792 | |
| 4793 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4794 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4795 | LY_CHECK_RET(rc); |
| 4796 | set_fill_number(set, strlen(args[0]->val.str)); |
| 4797 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4798 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4799 | LY_CHECK_RET(rc); |
| 4800 | set_fill_number(set, strlen(set->val.str)); |
| 4801 | } |
| 4802 | |
| 4803 | return LY_SUCCESS; |
| 4804 | } |
| 4805 | |
| 4806 | /** |
| 4807 | * @brief Execute the XPath substring(string, number, number?) function. |
| 4808 | * Returns LYXP_SET_STRING substring of the first argument starting |
| 4809 | * on the second argument index ending on the third argument index, |
| 4810 | * indexed from 1. For exact definition refer to |
| 4811 | * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring. |
| 4812 | * |
| 4813 | * @param[in] args Array of arguments. |
| 4814 | * @param[in] arg_count Count of elements in @p args. |
| 4815 | * @param[in,out] set Context and result set at the same time. |
| 4816 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4817 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4818 | */ |
| 4819 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4820 | 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] | 4821 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4822 | int32_t start, len; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4823 | uint16_t str_start, str_len, pos; |
| 4824 | struct lysc_node_leaf *sleaf; |
| 4825 | LY_ERR rc = LY_SUCCESS; |
| 4826 | |
| 4827 | if (options & LYXP_SCNODE_ALL) { |
| 4828 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4829 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4830 | 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] | 4831 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4832 | 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] | 4833 | } |
| 4834 | } |
| 4835 | |
| 4836 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4837 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4838 | 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] | 4839 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 4840 | 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] | 4841 | } |
| 4842 | } |
| 4843 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 4844 | if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET) && |
| 4845 | (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4846 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4847 | 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] | 4848 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 4849 | 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] | 4850 | } |
| 4851 | } |
| 4852 | set_scnode_clear_ctx(set); |
| 4853 | return rc; |
| 4854 | } |
| 4855 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4856 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4857 | LY_CHECK_RET(rc); |
| 4858 | |
| 4859 | /* start */ |
| 4860 | if (xpath_round(&args[1], 1, args[1], options)) { |
| 4861 | return -1; |
| 4862 | } |
| 4863 | if (isfinite(args[1]->val.num)) { |
| 4864 | start = args[1]->val.num - 1; |
| 4865 | } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4866 | start = INT32_MIN; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4867 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4868 | start = INT32_MAX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4869 | } |
| 4870 | |
| 4871 | /* len */ |
| 4872 | if (arg_count == 3) { |
| 4873 | rc = xpath_round(&args[2], 1, args[2], options); |
| 4874 | LY_CHECK_RET(rc); |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4875 | if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4876 | len = 0; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4877 | } else if (isfinite(args[2]->val.num)) { |
| 4878 | len = args[2]->val.num; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4879 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4880 | len = INT32_MAX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4881 | } |
| 4882 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4883 | len = INT32_MAX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4884 | } |
| 4885 | |
| 4886 | /* find matching character positions */ |
| 4887 | str_start = 0; |
| 4888 | str_len = 0; |
| 4889 | for (pos = 0; args[0]->val.str[pos]; ++pos) { |
| 4890 | if (pos < start) { |
| 4891 | ++str_start; |
| 4892 | } else if (pos < start + len) { |
| 4893 | ++str_len; |
| 4894 | } else { |
| 4895 | break; |
| 4896 | } |
| 4897 | } |
| 4898 | |
| 4899 | set_fill_string(set, args[0]->val.str + str_start, str_len); |
| 4900 | return LY_SUCCESS; |
| 4901 | } |
| 4902 | |
| 4903 | /** |
| 4904 | * @brief Execute the XPath substring-after(string, string) function. |
| 4905 | * Returns LYXP_SET_STRING with the string succeeding the occurance |
| 4906 | * of the second argument in the first or an empty string. |
| 4907 | * |
| 4908 | * @param[in] args Array of arguments. |
| 4909 | * @param[in] arg_count Count of elements in @p args. |
| 4910 | * @param[in,out] set Context and result set at the same time. |
| 4911 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4912 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4913 | */ |
| 4914 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4915 | 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] | 4916 | { |
| 4917 | char *ptr; |
| 4918 | struct lysc_node_leaf *sleaf; |
| 4919 | LY_ERR rc = LY_SUCCESS; |
| 4920 | |
| 4921 | if (options & LYXP_SCNODE_ALL) { |
| 4922 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4923 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4924 | 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] | 4925 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4926 | 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] | 4927 | } |
| 4928 | } |
| 4929 | |
| 4930 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4931 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4932 | 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] | 4933 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4934 | 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] | 4935 | } |
| 4936 | } |
| 4937 | set_scnode_clear_ctx(set); |
| 4938 | return rc; |
| 4939 | } |
| 4940 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4941 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4942 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4943 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4944 | LY_CHECK_RET(rc); |
| 4945 | |
| 4946 | ptr = strstr(args[0]->val.str, args[1]->val.str); |
| 4947 | if (ptr) { |
| 4948 | set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str))); |
| 4949 | } else { |
| 4950 | set_fill_string(set, "", 0); |
| 4951 | } |
| 4952 | |
| 4953 | return LY_SUCCESS; |
| 4954 | } |
| 4955 | |
| 4956 | /** |
| 4957 | * @brief Execute the XPath substring-before(string, string) function. |
| 4958 | * Returns LYXP_SET_STRING with the string preceding the occurance |
| 4959 | * of the second argument in the first or an empty string. |
| 4960 | * |
| 4961 | * @param[in] args Array of arguments. |
| 4962 | * @param[in] arg_count Count of elements in @p args. |
| 4963 | * @param[in,out] set Context and result set at the same time. |
| 4964 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4965 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4966 | */ |
| 4967 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4968 | 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] | 4969 | { |
| 4970 | char *ptr; |
| 4971 | struct lysc_node_leaf *sleaf; |
| 4972 | LY_ERR rc = LY_SUCCESS; |
| 4973 | |
| 4974 | if (options & LYXP_SCNODE_ALL) { |
| 4975 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4976 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4977 | 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] | 4978 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4979 | 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] | 4980 | } |
| 4981 | } |
| 4982 | |
| 4983 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4984 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4985 | 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] | 4986 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4987 | 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] | 4988 | } |
| 4989 | } |
| 4990 | set_scnode_clear_ctx(set); |
| 4991 | return rc; |
| 4992 | } |
| 4993 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4994 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4995 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4996 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4997 | LY_CHECK_RET(rc); |
| 4998 | |
| 4999 | ptr = strstr(args[0]->val.str, args[1]->val.str); |
| 5000 | if (ptr) { |
| 5001 | set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str); |
| 5002 | } else { |
| 5003 | set_fill_string(set, "", 0); |
| 5004 | } |
| 5005 | |
| 5006 | return LY_SUCCESS; |
| 5007 | } |
| 5008 | |
| 5009 | /** |
| 5010 | * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER |
| 5011 | * with the sum of all the nodes in the context. |
| 5012 | * |
| 5013 | * @param[in] args Array of arguments. |
| 5014 | * @param[in] arg_count Count of elements in @p args. |
| 5015 | * @param[in,out] set Context and result set at the same time. |
| 5016 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5017 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5018 | */ |
| 5019 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5020 | 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] | 5021 | { |
| 5022 | long double num; |
| 5023 | char *str; |
| 5024 | uint16_t i; |
| 5025 | struct lyxp_set set_item; |
| 5026 | struct lysc_node_leaf *sleaf; |
| 5027 | LY_ERR rc = LY_SUCCESS; |
| 5028 | |
| 5029 | if (options & LYXP_SCNODE_ALL) { |
| 5030 | if (args[0]->type == LYXP_SET_SCNODE_SET) { |
| 5031 | for (i = 0; i < args[0]->used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 5032 | 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] | 5033 | sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode; |
| 5034 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5035 | 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] | 5036 | lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5037 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 5038 | 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] | 5039 | } |
| 5040 | } |
| 5041 | } |
| 5042 | } |
| 5043 | set_scnode_clear_ctx(set); |
| 5044 | return rc; |
| 5045 | } |
| 5046 | |
| 5047 | set_fill_number(set, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5048 | |
| 5049 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5050 | LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, 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] | 5051 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5052 | } else if (!args[0]->used) { |
| 5053 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5054 | } |
| 5055 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5056 | set_init(&set_item, set); |
| 5057 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5058 | set_item.type = LYXP_SET_NODE_SET; |
| 5059 | set_item.val.nodes = malloc(sizeof *set_item.val.nodes); |
| 5060 | LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM); |
| 5061 | |
| 5062 | set_item.used = 1; |
| 5063 | set_item.size = 1; |
| 5064 | |
| 5065 | for (i = 0; i < args[0]->used; ++i) { |
| 5066 | set_item.val.nodes[0] = args[0]->val.nodes[i]; |
| 5067 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5068 | rc = cast_node_set_to_string(&set_item, &str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5069 | LY_CHECK_RET(rc); |
| 5070 | num = cast_string_to_number(str); |
| 5071 | free(str); |
| 5072 | set->val.num += num; |
| 5073 | } |
| 5074 | |
| 5075 | free(set_item.val.nodes); |
| 5076 | |
| 5077 | return LY_SUCCESS; |
| 5078 | } |
| 5079 | |
| 5080 | /** |
| 5081 | * @brief Execute the XPath text() function (node type). Returns LYXP_SET_NODE_SET |
| 5082 | * with the text content of the nodes in the context. |
| 5083 | * |
| 5084 | * @param[in] args Array of arguments. |
| 5085 | * @param[in] arg_count Count of elements in @p args. |
| 5086 | * @param[in,out] set Context and result set at the same time. |
| 5087 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5088 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5089 | */ |
| 5090 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5091 | 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] | 5092 | { |
| 5093 | uint32_t i; |
| 5094 | |
| 5095 | if (options & LYXP_SCNODE_ALL) { |
| 5096 | set_scnode_clear_ctx(set); |
| 5097 | return LY_SUCCESS; |
| 5098 | } |
| 5099 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5100 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5101 | LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INCTX, print_set_type(set), "text()"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5102 | return LY_EVALID; |
| 5103 | } |
| 5104 | |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 5105 | for (i = 0; i < set->used; ) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5106 | switch (set->val.nodes[i].type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 5107 | case LYXP_NODE_NONE: |
| 5108 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5109 | case LYXP_NODE_ELEM: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5110 | if (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 5111 | set->val.nodes[i].type = LYXP_NODE_TEXT; |
| 5112 | ++i; |
| 5113 | break; |
| 5114 | } |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 5115 | /* fall through */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5116 | case LYXP_NODE_ROOT: |
| 5117 | case LYXP_NODE_ROOT_CONFIG: |
| 5118 | case LYXP_NODE_TEXT: |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5119 | case LYXP_NODE_META: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5120 | set_remove_node(set, i); |
| 5121 | break; |
| 5122 | } |
| 5123 | } |
| 5124 | |
| 5125 | return LY_SUCCESS; |
| 5126 | } |
| 5127 | |
| 5128 | /** |
| 5129 | * @brief Execute the XPath translate(string, string, string) function. |
| 5130 | * Returns LYXP_SET_STRING with the first argument with the characters |
| 5131 | * from the second argument replaced by those on the corresponding |
| 5132 | * positions in the third argument. |
| 5133 | * |
| 5134 | * @param[in] args Array of arguments. |
| 5135 | * @param[in] arg_count Count of elements in @p args. |
| 5136 | * @param[in,out] set Context and result set at the same time. |
| 5137 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5138 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5139 | */ |
| 5140 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5141 | 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] | 5142 | { |
| 5143 | uint16_t i, j, new_used; |
| 5144 | char *new; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 5145 | ly_bool have_removed; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5146 | struct lysc_node_leaf *sleaf; |
| 5147 | LY_ERR rc = LY_SUCCESS; |
| 5148 | |
| 5149 | if (options & LYXP_SCNODE_ALL) { |
| 5150 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 5151 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5152 | 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] | 5153 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5154 | 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] | 5155 | } |
| 5156 | } |
| 5157 | |
| 5158 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 5159 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5160 | 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] | 5161 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5162 | 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] | 5163 | } |
| 5164 | } |
| 5165 | |
| 5166 | if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) { |
| 5167 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5168 | 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] | 5169 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5170 | 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] | 5171 | } |
| 5172 | } |
| 5173 | set_scnode_clear_ctx(set); |
| 5174 | return rc; |
| 5175 | } |
| 5176 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5177 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5178 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5179 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5180 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5181 | rc = lyxp_set_cast(args[2], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5182 | LY_CHECK_RET(rc); |
| 5183 | |
| 5184 | new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char)); |
| 5185 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 5186 | new_used = 0; |
| 5187 | |
| 5188 | have_removed = 0; |
| 5189 | for (i = 0; args[0]->val.str[i]; ++i) { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 5190 | ly_bool found = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5191 | |
| 5192 | for (j = 0; args[1]->val.str[j]; ++j) { |
| 5193 | if (args[0]->val.str[i] == args[1]->val.str[j]) { |
| 5194 | /* removing this char */ |
| 5195 | if (j >= strlen(args[2]->val.str)) { |
| 5196 | have_removed = 1; |
| 5197 | found = 1; |
| 5198 | break; |
| 5199 | } |
| 5200 | /* replacing this char */ |
| 5201 | new[new_used] = args[2]->val.str[j]; |
| 5202 | ++new_used; |
| 5203 | found = 1; |
| 5204 | break; |
| 5205 | } |
| 5206 | } |
| 5207 | |
| 5208 | /* copying this char */ |
| 5209 | if (!found) { |
| 5210 | new[new_used] = args[0]->val.str[i]; |
| 5211 | ++new_used; |
| 5212 | } |
| 5213 | } |
| 5214 | |
| 5215 | if (have_removed) { |
| 5216 | new = ly_realloc(new, (new_used + 1) * sizeof(char)); |
| 5217 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 5218 | } |
| 5219 | new[new_used] = '\0'; |
| 5220 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5221 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5222 | set->type = LYXP_SET_STRING; |
| 5223 | set->val.str = new; |
| 5224 | |
| 5225 | return LY_SUCCESS; |
| 5226 | } |
| 5227 | |
| 5228 | /** |
| 5229 | * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN |
| 5230 | * with true value. |
| 5231 | * |
| 5232 | * @param[in] args Array of arguments. |
| 5233 | * @param[in] arg_count Count of elements in @p args. |
| 5234 | * @param[in,out] set Context and result set at the same time. |
| 5235 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5236 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5237 | */ |
| 5238 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5239 | 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] | 5240 | { |
| 5241 | if (options & LYXP_SCNODE_ALL) { |
| 5242 | set_scnode_clear_ctx(set); |
| 5243 | return LY_SUCCESS; |
| 5244 | } |
| 5245 | |
| 5246 | set_fill_boolean(set, 1); |
| 5247 | return LY_SUCCESS; |
| 5248 | } |
| 5249 | |
| 5250 | /* |
| 5251 | * moveto functions |
| 5252 | * |
| 5253 | * They and only they actually change the context (set). |
| 5254 | */ |
| 5255 | |
| 5256 | /** |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5257 | * @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] | 5258 | * |
Michal Vasko | 2104e9f | 2020-03-06 08:23:25 +0100 | [diff] [blame] | 5259 | * XPath @p set is expected to be a (sc)node set! |
| 5260 | * |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5261 | * @param[in,out] qname Qualified node name. If includes prefix, it is skipped. |
| 5262 | * @param[in,out] qname_len Length of @p qname, is updated accordingly. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5263 | * @param[in] set Set with general XPath context. |
| 5264 | * @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] | 5265 | * @param[out] moveto_mod Expected module of a matching node. |
| 5266 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5267 | */ |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5268 | static LY_ERR |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5269 | moveto_resolve_model(const char **qname, uint16_t *qname_len, const struct lyxp_set *set, |
| 5270 | const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5271 | { |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 5272 | const struct lys_module *mod = NULL; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5273 | const char *ptr; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5274 | size_t pref_len; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5275 | |
Michal Vasko | 2104e9f | 2020-03-06 08:23:25 +0100 | [diff] [blame] | 5276 | assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET)); |
| 5277 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5278 | if ((ptr = ly_strnchr(*qname, ':', *qname_len))) { |
| 5279 | /* specific module */ |
| 5280 | pref_len = ptr - *qname; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5281 | 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] | 5282 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5283 | /* check for errors and non-implemented modules, as they are not valid */ |
Juraj Vijtiuk | d75faa6 | 2019-11-26 14:10:10 +0100 | [diff] [blame] | 5284 | if (!mod || !mod->implemented) { |
Michal Vasko | 2104e9f | 2020-03-06 08:23:25 +0100 | [diff] [blame] | 5285 | if (set->type == LYXP_SET_SCNODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5286 | LOGVAL(set->ctx, LY_VLOG_LYSC, set->cur_scnode, LY_VCODE_XP_INMOD, pref_len, *qname); |
Michal Vasko | 2104e9f | 2020-03-06 08:23:25 +0100 | [diff] [blame] | 5287 | } else { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5288 | LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INMOD, pref_len, *qname); |
Michal Vasko | 2104e9f | 2020-03-06 08:23:25 +0100 | [diff] [blame] | 5289 | } |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5290 | return LY_EVALID; |
| 5291 | } |
Juraj Vijtiuk | d75faa6 | 2019-11-26 14:10:10 +0100 | [diff] [blame] | 5292 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5293 | *qname += pref_len + 1; |
| 5294 | *qname_len -= pref_len + 1; |
| 5295 | } else if (((*qname)[0] == '*') && (*qname_len == 1)) { |
| 5296 | /* all modules - special case */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5297 | mod = NULL; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5298 | } else { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5299 | switch (set->format) { |
| 5300 | case LY_PREF_SCHEMA: |
| 5301 | case LY_PREF_SCHEMA_RESOLVED: |
| 5302 | /* current module */ |
| 5303 | mod = set->cur_mod; |
| 5304 | break; |
| 5305 | case LY_PREF_JSON: |
| 5306 | /* inherit parent (context node) module */ |
| 5307 | if (ctx_scnode) { |
| 5308 | mod = ctx_scnode->module; |
| 5309 | } else { |
| 5310 | mod = NULL; |
| 5311 | } |
| 5312 | break; |
| 5313 | case LY_PREF_XML: |
| 5314 | /* not defined */ |
| 5315 | LOGINT_RET(set->ctx); |
| 5316 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5317 | } |
| 5318 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5319 | *moveto_mod = mod; |
| 5320 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5321 | } |
| 5322 | |
| 5323 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5324 | * @brief Move context @p set to the root. Handles absolute path. |
| 5325 | * Result is LYXP_SET_NODE_SET. |
| 5326 | * |
| 5327 | * @param[in,out] set Set to use. |
| 5328 | * @param[in] options Xpath options. |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 5329 | * @return LY_ERR value. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5330 | */ |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 5331 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5332 | moveto_root(struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5333 | { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5334 | if (!set) { |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 5335 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5336 | } |
| 5337 | |
| 5338 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5339 | set_scnode_clear_ctx(set); |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 5340 | 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] | 5341 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5342 | set->type = LYXP_SET_NODE_SET; |
| 5343 | set->used = 0; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5344 | set_insert_node(set, NULL, 0, set->root_type, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5345 | } |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 5346 | |
| 5347 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5348 | } |
| 5349 | |
| 5350 | /** |
| 5351 | * @brief Check @p node as a part of NameTest processing. |
| 5352 | * |
| 5353 | * @param[in] node Node to check. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5354 | * @param[in] ctx_node Context node. |
| 5355 | * @param[in] set Set to read general context from. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5356 | * @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] | 5357 | * @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] | 5358 | * @param[in] options XPath options. |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5359 | * @return LY_ERR (LY_ENOT if node does not match, LY_EINCOMPLETE on unresolved when, |
| 5360 | * LY_EINVAL if netither node nor any children match) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5361 | */ |
| 5362 | static LY_ERR |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5363 | 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] | 5364 | 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] | 5365 | { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5366 | if (!moveto_mod && (!node_name || strcmp(node_name, "*"))) { |
| 5367 | switch (set->format) { |
| 5368 | case LY_PREF_SCHEMA: |
| 5369 | case LY_PREF_SCHEMA_RESOLVED: |
| 5370 | /* use current module */ |
| 5371 | moveto_mod = set->cur_mod; |
| 5372 | break; |
| 5373 | case LY_PREF_JSON: |
| 5374 | /* inherit module of the context node, if any */ |
| 5375 | if (ctx_node) { |
| 5376 | moveto_mod = ctx_node->schema->module; |
| 5377 | } |
| 5378 | break; |
| 5379 | case LY_PREF_XML: |
| 5380 | /* not defined */ |
| 5381 | LOGINT(set->ctx); |
| 5382 | return LY_EINVAL; |
| 5383 | } |
| 5384 | } |
| 5385 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5386 | /* module check */ |
| 5387 | if (moveto_mod && (node->schema->module != moveto_mod)) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5388 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5389 | } |
| 5390 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5391 | /* context check */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5392 | 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] | 5393 | return LY_EINVAL; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5394 | } else if (set->context_op && (node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && |
| 5395 | (node->schema != set->context_op)) { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5396 | return LY_EINVAL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5397 | } |
| 5398 | |
| 5399 | /* name check */ |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5400 | if (node_name && strcmp(node_name, "*") && (node->schema->name != node_name)) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5401 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5402 | } |
| 5403 | |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 5404 | /* when check */ |
Michal Vasko | d5cfa6e | 2020-11-23 16:56:08 +0100 | [diff] [blame] | 5405 | 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] | 5406 | return LY_EINCOMPLETE; |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 5407 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5408 | |
| 5409 | /* match */ |
| 5410 | return LY_SUCCESS; |
| 5411 | } |
| 5412 | |
| 5413 | /** |
| 5414 | * @brief Check @p node as a part of schema NameTest processing. |
| 5415 | * |
| 5416 | * @param[in] node Schema node to check. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5417 | * @param[in] ctx_scnode Context node. |
| 5418 | * @param[in] set Set to read general context from. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5419 | * @param[in] node_name Node name in the dictionary to move to, NULL for any nodes. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5420 | * @param[in] moveto_mod Expected module of the node, NULL for no prefix. |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5421 | * @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] | 5422 | */ |
| 5423 | static LY_ERR |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5424 | 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] | 5425 | const char *node_name, const struct lys_module *moveto_mod) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5426 | { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5427 | if (!moveto_mod && (!node_name || strcmp(node_name, "*"))) { |
| 5428 | switch (set->format) { |
| 5429 | case LY_PREF_SCHEMA: |
| 5430 | case LY_PREF_SCHEMA_RESOLVED: |
| 5431 | /* use current module */ |
| 5432 | moveto_mod = set->cur_mod; |
| 5433 | break; |
| 5434 | case LY_PREF_JSON: |
| 5435 | /* inherit module of the context node, if any */ |
| 5436 | if (ctx_scnode) { |
| 5437 | moveto_mod = ctx_scnode->module; |
| 5438 | } |
| 5439 | break; |
| 5440 | case LY_PREF_XML: |
| 5441 | /* not defined */ |
| 5442 | LOGINT(set->ctx); |
| 5443 | return LY_EINVAL; |
| 5444 | } |
| 5445 | } |
| 5446 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5447 | /* module check */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5448 | if (moveto_mod && (node->module != moveto_mod)) { |
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 | /* context check */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5453 | 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] | 5454 | return LY_EINVAL; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5455 | } 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] | 5456 | return LY_EINVAL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5457 | } |
| 5458 | |
| 5459 | /* name check */ |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5460 | if (node_name && strcmp(node_name, "*") && (node->name != node_name)) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5461 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5462 | } |
| 5463 | |
| 5464 | /* match */ |
| 5465 | return LY_SUCCESS; |
| 5466 | } |
| 5467 | |
| 5468 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5469 | * @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] | 5470 | * |
| 5471 | * @param[in,out] set Set to use. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5472 | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5473 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 5474 | * @param[in] options XPath options. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5475 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 5476 | */ |
| 5477 | static LY_ERR |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 5478 | 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] | 5479 | { |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 5480 | uint32_t i; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5481 | const struct lyd_node *siblings, *sub, *ctx_node; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5482 | LY_ERR rc; |
| 5483 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5484 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5485 | return LY_SUCCESS; |
| 5486 | } |
| 5487 | |
| 5488 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5489 | LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5490 | return LY_EVALID; |
| 5491 | } |
| 5492 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5493 | for (i = 0; i < set->used; ) { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 5494 | ly_bool replaced = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5495 | |
| 5496 | 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] | 5497 | assert(!set->val.nodes[i].node); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5498 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5499 | /* search in all the trees */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5500 | ctx_node = NULL; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5501 | siblings = set->tree; |
Michal Vasko | 5bfd4be | 2020-06-23 13:26:19 +0200 | [diff] [blame] | 5502 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5503 | /* search in children */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5504 | ctx_node = set->val.nodes[i].node; |
| 5505 | siblings = lyd_child(ctx_node); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5506 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5507 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5508 | for (sub = siblings; sub; sub = sub->next) { |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 5509 | rc = moveto_node_check(sub, ctx_node, set, ncname, moveto_mod, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5510 | if (rc == LY_SUCCESS) { |
| 5511 | if (!replaced) { |
| 5512 | set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i); |
| 5513 | replaced = 1; |
| 5514 | } else { |
| 5515 | set_insert_node(set, sub, 0, LYXP_NODE_ELEM, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5516 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5517 | ++i; |
| 5518 | } else if (rc == LY_EINCOMPLETE) { |
| 5519 | return rc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5520 | } |
| 5521 | } |
| 5522 | |
| 5523 | if (!replaced) { |
| 5524 | /* no match */ |
| 5525 | set_remove_node(set, i); |
| 5526 | } |
| 5527 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5528 | |
| 5529 | return LY_SUCCESS; |
| 5530 | } |
| 5531 | |
| 5532 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5533 | * @brief Move context @p set to a node using hashes. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY). |
| 5534 | * Context position aware. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5535 | * |
| 5536 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5537 | * @param[in] scnode Matching node schema. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5538 | * @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] | 5539 | * @param[in] options XPath options. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5540 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 5541 | */ |
| 5542 | static LY_ERR |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 5543 | moveto_node_hash(struct lyxp_set *set, const struct lysc_node *scnode, const struct ly_path_predicate *predicates, |
| 5544 | uint32_t options) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5545 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5546 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5547 | uint32_t i; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5548 | const struct lyd_node *siblings; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5549 | struct lyd_node *sub, *inst = NULL; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5550 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5551 | assert(scnode && (!(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) || predicates)); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5552 | |
| 5553 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5554 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5555 | } |
| 5556 | |
| 5557 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5558 | LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5559 | ret = LY_EVALID; |
| 5560 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5561 | } |
| 5562 | |
| 5563 | /* context check for all the nodes since we have the schema node */ |
| 5564 | if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) { |
| 5565 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5566 | goto cleanup; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 5567 | } else if (set->context_op && (scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && |
| 5568 | (scnode != set->context_op)) { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5569 | lyxp_set_free_content(set); |
| 5570 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5571 | } |
| 5572 | |
| 5573 | /* create specific data instance if needed */ |
| 5574 | if (scnode->nodetype == LYS_LIST) { |
| 5575 | LY_CHECK_GOTO(ret = lyd_create_list(scnode, predicates, &inst), cleanup); |
| 5576 | } else if (scnode->nodetype == LYS_LEAFLIST) { |
| 5577 | 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] | 5578 | } |
| 5579 | |
| 5580 | for (i = 0; i < set->used; ) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5581 | siblings = NULL; |
| 5582 | |
| 5583 | if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) { |
| 5584 | assert(!set->val.nodes[i].node); |
| 5585 | |
| 5586 | /* search in all the trees */ |
| 5587 | siblings = set->tree; |
| 5588 | } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
| 5589 | /* search in children */ |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 5590 | siblings = lyd_child(set->val.nodes[i].node); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5591 | } |
| 5592 | |
| 5593 | /* find the node using hashes */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5594 | if (inst) { |
| 5595 | lyd_find_sibling_first(siblings, inst, &sub); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5596 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5597 | lyd_find_sibling_val(siblings, scnode, NULL, 0, &sub); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5598 | } |
| 5599 | |
| 5600 | /* when check */ |
Michal Vasko | d5cfa6e | 2020-11-23 16:56:08 +0100 | [diff] [blame] | 5601 | 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] | 5602 | ret = LY_EINCOMPLETE; |
| 5603 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5604 | } |
| 5605 | |
| 5606 | if (sub) { |
| 5607 | /* pos filled later */ |
Michal Vasko | cb1b7c0 | 2020-07-03 13:38:12 +0200 | [diff] [blame] | 5608 | set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5609 | ++i; |
Michal Vasko | cb1b7c0 | 2020-07-03 13:38:12 +0200 | [diff] [blame] | 5610 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5611 | /* no match */ |
| 5612 | set_remove_node(set, i); |
| 5613 | } |
| 5614 | } |
| 5615 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5616 | cleanup: |
| 5617 | lyd_free_tree(inst); |
| 5618 | return ret; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5619 | } |
| 5620 | |
| 5621 | /** |
| 5622 | * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY). |
| 5623 | * |
| 5624 | * @param[in,out] set Set to use. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5625 | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5626 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5627 | * @param[in] options XPath options. |
| 5628 | * @return LY_ERR |
| 5629 | */ |
| 5630 | static LY_ERR |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5631 | 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] | 5632 | { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 5633 | ly_bool temp_ctx = 0; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5634 | uint32_t getnext_opts; |
| 5635 | uint32_t orig_used, i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5636 | uint32_t mod_idx; |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 5637 | const struct lysc_node *iter, *start_parent; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5638 | const struct lys_module *mod; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5639 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5640 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5641 | return LY_SUCCESS; |
| 5642 | } |
| 5643 | |
| 5644 | if (set->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5645 | LOGVAL(set->ctx, LY_VLOG_LYSC, set->cur_scnode, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5646 | return LY_EVALID; |
| 5647 | } |
| 5648 | |
Michal Vasko | cafad9d | 2019-11-07 15:20:03 +0100 | [diff] [blame] | 5649 | /* getnext opts */ |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 5650 | getnext_opts = 0; |
Michal Vasko | cafad9d | 2019-11-07 15:20:03 +0100 | [diff] [blame] | 5651 | if (options & LYXP_SCNODE_OUTPUT) { |
| 5652 | getnext_opts |= LYS_GETNEXT_OUTPUT; |
| 5653 | } |
| 5654 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5655 | orig_used = set->used; |
| 5656 | for (i = 0; i < orig_used; ++i) { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 5657 | uint32_t idx; |
| 5658 | |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 5659 | if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) { |
| 5660 | if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5661 | continue; |
| 5662 | } |
| 5663 | |
| 5664 | /* remember context node */ |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 5665 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED; |
Michal Vasko | ec4df48 | 2019-12-16 10:02:18 +0100 | [diff] [blame] | 5666 | } else { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 5667 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5668 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5669 | |
| 5670 | start_parent = set->val.scnodes[i].scnode; |
| 5671 | |
| 5672 | 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] | 5673 | /* 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] | 5674 | * 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] | 5675 | mod_idx = 0; |
Michal Vasko | 9e9f26d | 2020-10-12 16:31:33 +0200 | [diff] [blame] | 5676 | 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] | 5677 | iter = NULL; |
Michal Vasko | 509de4d | 2019-12-10 14:51:30 +0100 | [diff] [blame] | 5678 | /* module may not be implemented */ |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 5679 | while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5680 | if (!moveto_scnode_check(iter, NULL, set, ncname, moveto_mod)) { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 5681 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx)); |
| 5682 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5683 | /* we need to prevent these nodes from being considered in this moveto */ |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 5684 | if ((idx < orig_used) && (idx > i)) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 5685 | set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5686 | temp_ctx = 1; |
| 5687 | } |
| 5688 | } |
| 5689 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5690 | } |
| 5691 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 5692 | } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) { |
| 5693 | iter = NULL; |
| 5694 | while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5695 | if (!moveto_scnode_check(iter, start_parent, set, ncname, moveto_mod)) { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 5696 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx)); |
| 5697 | |
| 5698 | if ((idx < orig_used) && (idx > i)) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 5699 | set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5700 | temp_ctx = 1; |
| 5701 | } |
| 5702 | } |
| 5703 | } |
| 5704 | } |
| 5705 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5706 | |
| 5707 | /* correct temporary in_ctx values */ |
| 5708 | if (temp_ctx) { |
| 5709 | for (i = 0; i < orig_used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 5710 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NEW_CTX) { |
| 5711 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5712 | } |
| 5713 | } |
| 5714 | } |
| 5715 | |
| 5716 | return LY_SUCCESS; |
| 5717 | } |
| 5718 | |
| 5719 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5720 | * @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] | 5721 | * Context position aware. |
| 5722 | * |
| 5723 | * @param[in] set Set to use. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5724 | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5725 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 5726 | * @param[in] options XPath options. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5727 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 5728 | */ |
| 5729 | static LY_ERR |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 5730 | 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] | 5731 | { |
| 5732 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5733 | const struct lyd_node *next, *elem, *start; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5734 | struct lyxp_set ret_set; |
| 5735 | LY_ERR rc; |
| 5736 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5737 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5738 | return LY_SUCCESS; |
| 5739 | } |
| 5740 | |
| 5741 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5742 | LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5743 | return LY_EVALID; |
| 5744 | } |
| 5745 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5746 | /* 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] | 5747 | rc = moveto_node(set, NULL, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5748 | LY_CHECK_RET(rc); |
| 5749 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5750 | /* 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] | 5751 | set_init(&ret_set, set); |
| 5752 | for (i = 0; i < set->used; ++i) { |
| 5753 | |
| 5754 | /* TREE DFS */ |
| 5755 | start = set->val.nodes[i].node; |
| 5756 | for (elem = next = start; elem; elem = next) { |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 5757 | rc = moveto_node_check(elem, start, set, ncname, moveto_mod, options); |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5758 | if (!rc) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5759 | /* add matching node into result set */ |
| 5760 | set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used); |
| 5761 | if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) { |
| 5762 | /* the node is a duplicate, we'll process it later in the set */ |
| 5763 | goto skip_children; |
| 5764 | } |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5765 | } else if (rc == LY_EINCOMPLETE) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5766 | return rc; |
| 5767 | } else if (rc == LY_EINVAL) { |
| 5768 | goto skip_children; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5769 | } |
| 5770 | |
| 5771 | /* TREE DFS NEXT ELEM */ |
| 5772 | /* select element for the next run - children first */ |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 5773 | next = lyd_child(elem); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5774 | if (!next) { |
| 5775 | skip_children: |
| 5776 | /* no children, so try siblings, but only if it's not the start, |
| 5777 | * that is considered to be the root and it's siblings are not traversed */ |
| 5778 | if (elem != start) { |
| 5779 | next = elem->next; |
| 5780 | } else { |
| 5781 | break; |
| 5782 | } |
| 5783 | } |
| 5784 | while (!next) { |
| 5785 | /* no siblings, go back through the parents */ |
| 5786 | if ((struct lyd_node *)elem->parent == start) { |
| 5787 | /* we are done, no next element to process */ |
| 5788 | break; |
| 5789 | } |
| 5790 | /* parent is already processed, go to its sibling */ |
| 5791 | elem = (struct lyd_node *)elem->parent; |
| 5792 | next = elem->next; |
| 5793 | } |
| 5794 | } |
| 5795 | } |
| 5796 | |
| 5797 | /* make the temporary set the current one */ |
| 5798 | ret_set.ctx_pos = set->ctx_pos; |
| 5799 | ret_set.ctx_size = set->ctx_size; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5800 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5801 | memcpy(set, &ret_set, sizeof *set); |
| 5802 | |
| 5803 | return LY_SUCCESS; |
| 5804 | } |
| 5805 | |
| 5806 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5807 | * @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] | 5808 | * |
| 5809 | * @param[in] set Set to use. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5810 | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5811 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5812 | * @param[in] options XPath options. |
| 5813 | * @return LY_ERR |
| 5814 | */ |
| 5815 | static LY_ERR |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5816 | 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] | 5817 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5818 | uint32_t i, orig_used; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5819 | const struct lysc_node *next, *elem, *start; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5820 | LY_ERR rc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5821 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5822 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5823 | return LY_SUCCESS; |
| 5824 | } |
| 5825 | |
| 5826 | if (set->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5827 | LOGVAL(set->ctx, LY_VLOG_LYSC, set->cur_scnode, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5828 | return LY_EVALID; |
| 5829 | } |
| 5830 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5831 | orig_used = set->used; |
| 5832 | for (i = 0; i < orig_used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 5833 | if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) { |
| 5834 | if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5835 | continue; |
| 5836 | } |
| 5837 | |
| 5838 | /* remember context node */ |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 5839 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED; |
Michal Vasko | ec4df48 | 2019-12-16 10:02:18 +0100 | [diff] [blame] | 5840 | } else { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 5841 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5842 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5843 | |
| 5844 | /* TREE DFS */ |
| 5845 | start = set->val.scnodes[i].scnode; |
| 5846 | for (elem = next = start; elem; elem = next) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5847 | if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) { |
| 5848 | /* schema-only nodes, skip root */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5849 | goto next_iter; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5850 | } |
| 5851 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5852 | rc = moveto_scnode_check(elem, start, set, ncname, moveto_mod); |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5853 | if (!rc) { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 5854 | uint32_t idx; |
| 5855 | |
| 5856 | if (lyxp_set_scnode_contains(set, elem, LYXP_NODE_ELEM, i, &idx)) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 5857 | set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5858 | if ((uint32_t)idx > i) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5859 | /* we will process it later in the set */ |
| 5860 | goto skip_children; |
| 5861 | } |
| 5862 | } else { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 5863 | 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] | 5864 | } |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5865 | } else if (rc == LY_EINVAL) { |
| 5866 | goto skip_children; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5867 | } |
| 5868 | |
| 5869 | next_iter: |
| 5870 | /* TREE DFS NEXT ELEM */ |
| 5871 | /* select element for the next run - children first */ |
| 5872 | next = lysc_node_children(elem, options & LYXP_SCNODE_OUTPUT ? LYS_CONFIG_R : LYS_CONFIG_W); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5873 | if (!next) { |
| 5874 | skip_children: |
| 5875 | /* no children, so try siblings, but only if it's not the start, |
| 5876 | * that is considered to be the root and it's siblings are not traversed */ |
| 5877 | if (elem != start) { |
| 5878 | next = elem->next; |
| 5879 | } else { |
| 5880 | break; |
| 5881 | } |
| 5882 | } |
| 5883 | while (!next) { |
| 5884 | /* no siblings, go back through the parents */ |
| 5885 | if (elem->parent == start) { |
| 5886 | /* we are done, no next element to process */ |
| 5887 | break; |
| 5888 | } |
| 5889 | /* parent is already processed, go to its sibling */ |
| 5890 | elem = elem->parent; |
| 5891 | next = elem->next; |
| 5892 | } |
| 5893 | } |
| 5894 | } |
| 5895 | |
| 5896 | return LY_SUCCESS; |
| 5897 | } |
| 5898 | |
| 5899 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5900 | * @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] | 5901 | * Indirectly context position aware. |
| 5902 | * |
| 5903 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5904 | * @param[in] mod Matching metadata module, NULL for any. |
| 5905 | * @param[in] ncname Matching metadata name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5906 | * @return LY_ERR |
| 5907 | */ |
| 5908 | static LY_ERR |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5909 | 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] | 5910 | { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5911 | struct lyd_meta *sub; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5912 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5913 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5914 | return LY_SUCCESS; |
| 5915 | } |
| 5916 | |
| 5917 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5918 | LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5919 | return LY_EVALID; |
| 5920 | } |
| 5921 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5922 | for (uint32_t i = 0; i < set->used; ) { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 5923 | ly_bool replaced = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5924 | |
| 5925 | /* only attributes of an elem (not dummy) can be in the result, skip all the rest; |
| 5926 | * our attributes are always qualified */ |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5927 | if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5928 | for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5929 | |
| 5930 | /* check "namespace" */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5931 | if (mod && (sub->annotation->module != mod)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5932 | continue; |
| 5933 | } |
| 5934 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5935 | if (!ncname || (sub->name == ncname)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5936 | /* match */ |
| 5937 | if (!replaced) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5938 | set->val.meta[i].meta = sub; |
| 5939 | set->val.meta[i].type = LYXP_NODE_META; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5940 | /* pos does not change */ |
| 5941 | replaced = 1; |
| 5942 | } else { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5943 | 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] | 5944 | } |
| 5945 | ++i; |
| 5946 | } |
| 5947 | } |
| 5948 | } |
| 5949 | |
| 5950 | if (!replaced) { |
| 5951 | /* no match */ |
| 5952 | set_remove_node(set, i); |
| 5953 | } |
| 5954 | } |
| 5955 | |
| 5956 | return LY_SUCCESS; |
| 5957 | } |
| 5958 | |
| 5959 | /** |
| 5960 | * @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] | 5961 | * Result is LYXP_SET_NODE_SET. Context position aware. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5962 | * |
| 5963 | * @param[in,out] set1 Set to use for the result. |
| 5964 | * @param[in] set2 Set that is copied to @p set1. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5965 | * @return LY_ERR |
| 5966 | */ |
| 5967 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5968 | moveto_union(struct lyxp_set *set1, struct lyxp_set *set2) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5969 | { |
| 5970 | LY_ERR rc; |
| 5971 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5972 | if ((set1->type != LYXP_SET_NODE_SET) || (set2->type != LYXP_SET_NODE_SET)) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5973 | LOGVAL(set1->ctx, LY_VLOG_LYD, set1->cur_node, 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] | 5974 | return LY_EVALID; |
| 5975 | } |
| 5976 | |
| 5977 | /* set2 is empty or both set1 and set2 */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5978 | if (!set2->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5979 | return LY_SUCCESS; |
| 5980 | } |
| 5981 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5982 | if (!set1->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5983 | memcpy(set1, set2, sizeof *set1); |
| 5984 | /* dynamic memory belongs to set1 now, do not free */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5985 | memset(set2, 0, sizeof *set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5986 | return LY_SUCCESS; |
| 5987 | } |
| 5988 | |
| 5989 | /* we assume sets are sorted */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5990 | assert(!set_sort(set1) && !set_sort(set2)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5991 | |
| 5992 | /* sort, remove duplicates */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5993 | rc = set_sorted_merge(set1, set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5994 | LY_CHECK_RET(rc); |
| 5995 | |
| 5996 | /* final set must be sorted */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5997 | assert(!set_sort(set1)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5998 | |
| 5999 | return LY_SUCCESS; |
| 6000 | } |
| 6001 | |
| 6002 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6003 | * @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] | 6004 | * Context position aware. |
| 6005 | * |
| 6006 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6007 | * @param[in] mod Matching metadata module, NULL for any. |
| 6008 | * @param[in] ncname Matching metadata name in the dictionary, NULL for any. |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 6009 | * @param[in] options XPath options. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6010 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6011 | */ |
| 6012 | static int |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 6013 | 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] | 6014 | { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6015 | struct lyd_meta *sub; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6016 | struct lyxp_set *set_all_desc = NULL; |
| 6017 | LY_ERR rc; |
| 6018 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6019 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6020 | return LY_SUCCESS; |
| 6021 | } |
| 6022 | |
| 6023 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6024 | LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6025 | return LY_EVALID; |
| 6026 | } |
| 6027 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6028 | /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory, |
| 6029 | * but it likely won't be used much, so it's a waste of time */ |
| 6030 | /* copy the context */ |
| 6031 | set_all_desc = set_copy(set); |
| 6032 | /* get all descendant nodes (the original context nodes are removed) */ |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 6033 | rc = moveto_node_alldesc(set_all_desc, NULL, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6034 | if (rc != LY_SUCCESS) { |
| 6035 | lyxp_set_free(set_all_desc); |
| 6036 | return rc; |
| 6037 | } |
| 6038 | /* prepend the original context nodes */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6039 | rc = moveto_union(set, set_all_desc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6040 | if (rc != LY_SUCCESS) { |
| 6041 | lyxp_set_free(set_all_desc); |
| 6042 | return rc; |
| 6043 | } |
| 6044 | lyxp_set_free(set_all_desc); |
| 6045 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6046 | for (uint32_t i = 0; i < set->used; ) { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6047 | ly_bool replaced = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6048 | |
| 6049 | /* only attributes of an elem can be in the result, skip all the rest, |
| 6050 | * we have all attributes qualified in lyd tree */ |
| 6051 | if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6052 | for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6053 | /* check "namespace" */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6054 | if (mod && (sub->annotation->module != mod)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6055 | continue; |
| 6056 | } |
| 6057 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6058 | if (!ncname || (sub->name == ncname)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6059 | /* match */ |
| 6060 | if (!replaced) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6061 | set->val.meta[i].meta = sub; |
| 6062 | set->val.meta[i].type = LYXP_NODE_META; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6063 | /* pos does not change */ |
| 6064 | replaced = 1; |
| 6065 | } else { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6066 | 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] | 6067 | } |
| 6068 | ++i; |
| 6069 | } |
| 6070 | } |
| 6071 | } |
| 6072 | |
| 6073 | if (!replaced) { |
| 6074 | /* no match */ |
| 6075 | set_remove_node(set, i); |
| 6076 | } |
| 6077 | } |
| 6078 | |
| 6079 | return LY_SUCCESS; |
| 6080 | } |
| 6081 | |
| 6082 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6083 | * @brief Move context @p set to self and al chilren, recursively. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET. |
| 6084 | * Context position aware. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6085 | * |
| 6086 | * @param[in] parent Current parent. |
| 6087 | * @param[in] parent_pos Position of @p parent. |
| 6088 | * @param[in] parent_type Node type of @p parent. |
| 6089 | * @param[in,out] to_set Set to use. |
| 6090 | * @param[in] dup_check_set Set for checking duplicities. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6091 | * @param[in] options XPath options. |
| 6092 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6093 | */ |
| 6094 | static LY_ERR |
| 6095 | 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] | 6096 | 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] | 6097 | { |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6098 | const struct lyd_node *iter, *first; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6099 | LY_ERR rc; |
| 6100 | |
| 6101 | switch (parent_type) { |
| 6102 | case LYXP_NODE_ROOT: |
| 6103 | case LYXP_NODE_ROOT_CONFIG: |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6104 | assert(!parent); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6105 | |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6106 | /* add all top-level nodes as elements */ |
| 6107 | first = to_set->tree; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6108 | break; |
| 6109 | case LYXP_NODE_ELEM: |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6110 | /* add just the text node of this term element node */ |
| 6111 | if (parent->schema->nodetype & (LYD_NODE_TERM | LYD_NODE_ANY)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6112 | if (!set_dup_node_check(dup_check_set, parent, LYXP_NODE_TEXT, -1)) { |
| 6113 | set_insert_node(to_set, parent, parent_pos, LYXP_NODE_TEXT, to_set->used); |
| 6114 | } |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6115 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6116 | } |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6117 | |
| 6118 | /* add all the children of this node */ |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 6119 | first = lyd_child(parent); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6120 | break; |
| 6121 | default: |
| 6122 | LOGINT_RET(parent->schema->module->ctx); |
| 6123 | } |
| 6124 | |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6125 | /* add all top-level nodes as elements */ |
| 6126 | LY_LIST_FOR(first, iter) { |
| 6127 | /* context check */ |
| 6128 | if ((parent_type == LYXP_NODE_ROOT_CONFIG) && (iter->schema->flags & LYS_CONFIG_R)) { |
| 6129 | continue; |
| 6130 | } |
| 6131 | |
| 6132 | /* when check */ |
Michal Vasko | d5cfa6e | 2020-11-23 16:56:08 +0100 | [diff] [blame] | 6133 | 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] | 6134 | return LY_EINCOMPLETE; |
| 6135 | } |
| 6136 | |
| 6137 | if (!set_dup_node_check(dup_check_set, iter, LYXP_NODE_ELEM, -1)) { |
| 6138 | set_insert_node(to_set, iter, 0, LYXP_NODE_ELEM, to_set->used); |
| 6139 | |
| 6140 | /* also add all the children of this node, recursively */ |
| 6141 | rc = moveto_self_add_children_r(iter, 0, LYXP_NODE_ELEM, to_set, dup_check_set, options); |
| 6142 | LY_CHECK_RET(rc); |
| 6143 | } |
| 6144 | } |
| 6145 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6146 | return LY_SUCCESS; |
| 6147 | } |
| 6148 | |
| 6149 | /** |
| 6150 | * @brief Move context @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET |
| 6151 | * (or LYXP_SET_EMPTY). Context position aware. |
| 6152 | * |
| 6153 | * @param[in,out] set Set to use. |
| 6154 | * @param[in] all_desc Whether to go to all descendants ('//') or not ('/'). |
| 6155 | * @param[in] options XPath options. |
| 6156 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6157 | */ |
| 6158 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6159 | 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] | 6160 | { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6161 | struct lyxp_set ret_set; |
| 6162 | LY_ERR rc; |
| 6163 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6164 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6165 | return LY_SUCCESS; |
| 6166 | } |
| 6167 | |
| 6168 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6169 | LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6170 | return LY_EVALID; |
| 6171 | } |
| 6172 | |
| 6173 | /* nothing to do */ |
| 6174 | if (!all_desc) { |
| 6175 | return LY_SUCCESS; |
| 6176 | } |
| 6177 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6178 | /* add all the children, they get added recursively */ |
| 6179 | set_init(&ret_set, set); |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6180 | for (uint32_t i = 0; i < set->used; ++i) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6181 | /* copy the current node to tmp */ |
| 6182 | set_insert_node(&ret_set, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, ret_set.used); |
| 6183 | |
| 6184 | /* do not touch attributes and text nodes */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6185 | 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] | 6186 | continue; |
| 6187 | } |
| 6188 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6189 | /* add all the children */ |
| 6190 | 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] | 6191 | set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6192 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6193 | lyxp_set_free_content(&ret_set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6194 | return rc; |
| 6195 | } |
| 6196 | } |
| 6197 | |
| 6198 | /* use the temporary set as the current one */ |
| 6199 | ret_set.ctx_pos = set->ctx_pos; |
| 6200 | ret_set.ctx_size = set->ctx_size; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6201 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6202 | memcpy(set, &ret_set, sizeof *set); |
| 6203 | |
| 6204 | return LY_SUCCESS; |
| 6205 | } |
| 6206 | |
| 6207 | /** |
| 6208 | * @brief Move context schema @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_SCNODE_SET |
| 6209 | * (or LYXP_SET_EMPTY). |
| 6210 | * |
| 6211 | * @param[in,out] set Set to use. |
| 6212 | * @param[in] all_desc Whether to go to all descendants ('//') or not ('/'). |
| 6213 | * @param[in] options XPath options. |
| 6214 | * @return LY_ERR |
| 6215 | */ |
| 6216 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6217 | 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] | 6218 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6219 | uint32_t getnext_opts; |
| 6220 | uint32_t mod_idx; |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6221 | const struct lysc_node *iter, *start_parent; |
| 6222 | const struct lys_module *mod; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6223 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6224 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6225 | return LY_SUCCESS; |
| 6226 | } |
| 6227 | |
| 6228 | if (set->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6229 | LOGVAL(set->ctx, LY_VLOG_LYSC, set->cur_scnode, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6230 | return LY_EVALID; |
| 6231 | } |
| 6232 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6233 | /* getnext opts */ |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 6234 | getnext_opts = 0; |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6235 | if (options & LYXP_SCNODE_OUTPUT) { |
| 6236 | getnext_opts |= LYS_GETNEXT_OUTPUT; |
| 6237 | } |
| 6238 | |
| 6239 | /* 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] | 6240 | for (uint32_t i = 0; i < set->used; ++i) { |
Michal Vasko | e657f96 | 2020-12-10 12:19:48 +0100 | [diff] [blame] | 6241 | if (!all_desc) { |
| 6242 | /* traverse the start node */ |
| 6243 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START) { |
| 6244 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
| 6245 | } |
| 6246 | continue; |
| 6247 | } |
| 6248 | |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 6249 | if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) { |
| 6250 | if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6251 | continue; |
| 6252 | } |
| 6253 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6254 | /* remember context node */ |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 6255 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED; |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6256 | } else { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 6257 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6258 | } |
| 6259 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6260 | start_parent = set->val.scnodes[i].scnode; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6261 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6262 | if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) { |
| 6263 | /* it can actually be in any module, it's all <running> */ |
| 6264 | mod_idx = 0; |
| 6265 | while ((mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) { |
| 6266 | iter = NULL; |
| 6267 | /* module may not be implemented */ |
| 6268 | while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) { |
| 6269 | /* context check */ |
| 6270 | if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) { |
| 6271 | continue; |
| 6272 | } |
| 6273 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 6274 | 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] | 6275 | /* throw away the insert index, we want to consider that node again, recursively */ |
| 6276 | } |
| 6277 | } |
| 6278 | |
| 6279 | } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) { |
| 6280 | iter = NULL; |
| 6281 | while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6282 | /* context check */ |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6283 | 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] | 6284 | continue; |
| 6285 | } |
| 6286 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 6287 | 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] | 6288 | } |
| 6289 | } |
| 6290 | } |
| 6291 | |
| 6292 | return LY_SUCCESS; |
| 6293 | } |
| 6294 | |
| 6295 | /** |
| 6296 | * @brief Move context @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_NODE_SET |
| 6297 | * (or LYXP_SET_EMPTY). Context position aware. |
| 6298 | * |
| 6299 | * @param[in] set Set to use. |
| 6300 | * @param[in] all_desc Whether to go to all descendants ('//') or not ('/'). |
| 6301 | * @param[in] options XPath options. |
| 6302 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6303 | */ |
| 6304 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6305 | 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] | 6306 | { |
| 6307 | LY_ERR rc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6308 | struct lyd_node *node, *new_node; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6309 | enum lyxp_node_type new_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6310 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6311 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6312 | return LY_SUCCESS; |
| 6313 | } |
| 6314 | |
| 6315 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6316 | LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6317 | return LY_EVALID; |
| 6318 | } |
| 6319 | |
| 6320 | if (all_desc) { |
| 6321 | /* <path>//.. == <path>//./.. */ |
| 6322 | rc = moveto_self(set, 1, options); |
| 6323 | LY_CHECK_RET(rc); |
| 6324 | } |
| 6325 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6326 | for (uint32_t i = 0; i < set->used; ++i) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6327 | node = set->val.nodes[i].node; |
| 6328 | |
| 6329 | if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
| 6330 | new_node = (struct lyd_node *)node->parent; |
| 6331 | } else if (set->val.nodes[i].type == LYXP_NODE_TEXT) { |
| 6332 | new_node = node; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6333 | } else if (set->val.nodes[i].type == LYXP_NODE_META) { |
| 6334 | new_node = set->val.meta[i].meta->parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6335 | if (!new_node) { |
| 6336 | LOGINT_RET(set->ctx); |
| 6337 | } |
| 6338 | } else { |
| 6339 | /* root does not have a parent */ |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6340 | set_remove_node_none(set, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6341 | continue; |
| 6342 | } |
| 6343 | |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 6344 | /* when check */ |
Michal Vasko | d5cfa6e | 2020-11-23 16:56:08 +0100 | [diff] [blame] | 6345 | 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] | 6346 | return LY_EINCOMPLETE; |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 6347 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6348 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6349 | if (!new_node) { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 6350 | /* node already there can also be the root */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6351 | new_type = set->root_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6352 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6353 | } else { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 6354 | /* 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] | 6355 | new_type = LYXP_NODE_ELEM; |
| 6356 | } |
| 6357 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6358 | if (set_dup_node_check(set, new_node, new_type, -1)) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6359 | set_remove_node_none(set, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6360 | } else { |
| 6361 | set_replace_node(set, new_node, 0, new_type, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6362 | } |
| 6363 | } |
| 6364 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6365 | set_remove_nodes_none(set); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6366 | assert(!set_sort(set) && !set_sorted_dup_node_clean(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6367 | |
| 6368 | return LY_SUCCESS; |
| 6369 | } |
| 6370 | |
| 6371 | /** |
| 6372 | * @brief Move context schema @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_SCNODE_SET |
| 6373 | * (or LYXP_SET_EMPTY). |
| 6374 | * |
| 6375 | * @param[in] set Set to use. |
| 6376 | * @param[in] all_desc Whether to go to all descendants ('//') or not ('/'). |
| 6377 | * @param[in] options XPath options. |
| 6378 | * @return LY_ERR |
| 6379 | */ |
| 6380 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6381 | 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] | 6382 | { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 6383 | uint32_t i, orig_used, idx; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6384 | ly_bool temp_ctx = 0; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6385 | const struct lysc_node *node, *new_node; |
| 6386 | enum lyxp_node_type new_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6387 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6388 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6389 | return LY_SUCCESS; |
| 6390 | } |
| 6391 | |
| 6392 | if (set->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6393 | LOGVAL(set->ctx, LY_VLOG_LYSC, set->cur_scnode, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6394 | return LY_EVALID; |
| 6395 | } |
| 6396 | |
| 6397 | if (all_desc) { |
| 6398 | /* <path>//.. == <path>//./.. */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6399 | LY_CHECK_RET(moveto_scnode_self(set, 1, options)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6400 | } |
| 6401 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6402 | orig_used = set->used; |
| 6403 | for (i = 0; i < orig_used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 6404 | if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) { |
| 6405 | if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6406 | continue; |
| 6407 | } |
| 6408 | |
| 6409 | /* remember context node */ |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 6410 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED; |
Michal Vasko | ec4df48 | 2019-12-16 10:02:18 +0100 | [diff] [blame] | 6411 | } else { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 6412 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6413 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6414 | |
| 6415 | node = set->val.scnodes[i].scnode; |
| 6416 | |
| 6417 | if (set->val.scnodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6418 | new_node = lysc_data_parent(node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6419 | } else { |
| 6420 | /* root does not have a parent */ |
| 6421 | continue; |
| 6422 | } |
| 6423 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6424 | if (!new_node) { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 6425 | /* node has no parent */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6426 | new_type = set->root_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6427 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6428 | } else { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 6429 | /* node has a standard parent (it can equal the root, it's not the root yet since they are fake) */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6430 | new_type = LYXP_NODE_ELEM; |
| 6431 | } |
| 6432 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 6433 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, new_node, new_type, &idx)); |
| 6434 | if ((idx < orig_used) && (idx > i)) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 6435 | set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6436 | temp_ctx = 1; |
| 6437 | } |
| 6438 | } |
| 6439 | |
| 6440 | if (temp_ctx) { |
| 6441 | for (i = 0; i < orig_used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 6442 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NEW_CTX) { |
| 6443 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6444 | } |
| 6445 | } |
| 6446 | } |
| 6447 | |
| 6448 | return LY_SUCCESS; |
| 6449 | } |
| 6450 | |
| 6451 | /** |
| 6452 | * @brief Move context @p set to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'. |
| 6453 | * Result is LYXP_SET_BOOLEAN. Indirectly context position aware. |
| 6454 | * |
| 6455 | * @param[in,out] set1 Set to use for the result. |
| 6456 | * @param[in] set2 Set acting as the second operand for @p op. |
| 6457 | * @param[in] op Comparison operator to process. |
| 6458 | * @param[in] options XPath options. |
| 6459 | * @return LY_ERR |
| 6460 | */ |
| 6461 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6462 | 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] | 6463 | { |
| 6464 | /* |
| 6465 | * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING |
| 6466 | * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING) |
| 6467 | * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 6468 | * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 6469 | * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING |
| 6470 | * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6471 | * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 6472 | * |
| 6473 | * '=' or '!=' |
| 6474 | * BOOLEAN + BOOLEAN |
| 6475 | * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 6476 | * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 6477 | * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 6478 | * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 6479 | * NUMBER + NUMBER |
| 6480 | * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6481 | * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 6482 | * STRING + STRING |
| 6483 | * |
| 6484 | * '<=', '<', '>=', '>' |
| 6485 | * NUMBER + NUMBER |
| 6486 | * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 6487 | * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 6488 | * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 6489 | * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6490 | * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 6491 | * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 6492 | * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6493 | * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6494 | */ |
| 6495 | struct lyxp_set iter1, iter2; |
| 6496 | int result; |
| 6497 | int64_t i; |
| 6498 | LY_ERR rc; |
| 6499 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6500 | memset(&iter1, 0, sizeof iter1); |
| 6501 | memset(&iter2, 0, sizeof iter2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6502 | |
| 6503 | /* iterative evaluation with node-sets */ |
| 6504 | if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) { |
| 6505 | if (set1->type == LYXP_SET_NODE_SET) { |
| 6506 | for (i = 0; i < set1->used; ++i) { |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6507 | /* cast set1 */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6508 | switch (set2->type) { |
| 6509 | case LYXP_SET_NUMBER: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6510 | rc = set_comp_cast(&iter1, set1, LYXP_SET_NUMBER, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6511 | break; |
| 6512 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6513 | rc = set_comp_cast(&iter1, set1, LYXP_SET_BOOLEAN, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6514 | break; |
| 6515 | default: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6516 | rc = set_comp_cast(&iter1, set1, LYXP_SET_STRING, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6517 | break; |
| 6518 | } |
| 6519 | LY_CHECK_RET(rc); |
| 6520 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6521 | /* canonize set2 */ |
| 6522 | LY_CHECK_ERR_RET(rc = set_comp_canonize(&iter2, set2, &set1->val.nodes[i]), lyxp_set_free_content(&iter1), rc); |
| 6523 | |
| 6524 | /* compare recursively */ |
| 6525 | rc = moveto_op_comp(&iter1, &iter2, op, options); |
| 6526 | lyxp_set_free_content(&iter2); |
| 6527 | LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6528 | |
| 6529 | /* lazy evaluation until true */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6530 | if (iter1.val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6531 | set_fill_boolean(set1, 1); |
| 6532 | return LY_SUCCESS; |
| 6533 | } |
| 6534 | } |
| 6535 | } else { |
| 6536 | for (i = 0; i < set2->used; ++i) { |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6537 | /* set set2 */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6538 | switch (set1->type) { |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6539 | case LYXP_SET_NUMBER: |
| 6540 | rc = set_comp_cast(&iter2, set2, LYXP_SET_NUMBER, i); |
| 6541 | break; |
| 6542 | case LYXP_SET_BOOLEAN: |
| 6543 | rc = set_comp_cast(&iter2, set2, LYXP_SET_BOOLEAN, i); |
| 6544 | break; |
| 6545 | default: |
| 6546 | rc = set_comp_cast(&iter2, set2, LYXP_SET_STRING, i); |
| 6547 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6548 | } |
| 6549 | LY_CHECK_RET(rc); |
| 6550 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6551 | /* canonize set1 */ |
| 6552 | 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] | 6553 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6554 | /* compare recursively */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6555 | rc = moveto_op_comp(&iter1, &iter2, op, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6556 | lyxp_set_free_content(&iter2); |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6557 | LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6558 | |
| 6559 | /* lazy evaluation until true */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6560 | if (iter1.val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6561 | set_fill_boolean(set1, 1); |
| 6562 | return LY_SUCCESS; |
| 6563 | } |
| 6564 | } |
| 6565 | } |
| 6566 | |
| 6567 | /* false for all nodes */ |
| 6568 | set_fill_boolean(set1, 0); |
| 6569 | return LY_SUCCESS; |
| 6570 | } |
| 6571 | |
| 6572 | /* first convert properly */ |
| 6573 | if ((op[0] == '=') || (op[0] == '!')) { |
| 6574 | if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6575 | lyxp_set_cast(set1, LYXP_SET_BOOLEAN); |
| 6576 | lyxp_set_cast(set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6577 | } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6578 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6579 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6580 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6581 | LY_CHECK_RET(rc); |
| 6582 | } /* else we have 2 strings */ |
| 6583 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6584 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6585 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6586 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6587 | LY_CHECK_RET(rc); |
| 6588 | } |
| 6589 | |
| 6590 | assert(set1->type == set2->type); |
| 6591 | |
| 6592 | /* compute result */ |
| 6593 | if (op[0] == '=') { |
| 6594 | if (set1->type == LYXP_SET_BOOLEAN) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6595 | result = (set1->val.bln == set2->val.bln); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6596 | } else if (set1->type == LYXP_SET_NUMBER) { |
| 6597 | result = (set1->val.num == set2->val.num); |
| 6598 | } else { |
| 6599 | assert(set1->type == LYXP_SET_STRING); |
Michal Vasko | ac6c72f | 2019-11-14 16:09:34 +0100 | [diff] [blame] | 6600 | result = !strcmp(set1->val.str, set2->val.str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6601 | } |
| 6602 | } else if (op[0] == '!') { |
| 6603 | if (set1->type == LYXP_SET_BOOLEAN) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6604 | result = (set1->val.bln != set2->val.bln); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6605 | } else if (set1->type == LYXP_SET_NUMBER) { |
| 6606 | result = (set1->val.num != set2->val.num); |
| 6607 | } else { |
| 6608 | assert(set1->type == LYXP_SET_STRING); |
Michal Vasko | c205843 | 2020-11-06 17:26:21 +0100 | [diff] [blame] | 6609 | result = strcmp(set1->val.str, set2->val.str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6610 | } |
| 6611 | } else { |
| 6612 | assert(set1->type == LYXP_SET_NUMBER); |
| 6613 | if (op[0] == '<') { |
| 6614 | if (op[1] == '=') { |
| 6615 | result = (set1->val.num <= set2->val.num); |
| 6616 | } else { |
| 6617 | result = (set1->val.num < set2->val.num); |
| 6618 | } |
| 6619 | } else { |
| 6620 | if (op[1] == '=') { |
| 6621 | result = (set1->val.num >= set2->val.num); |
| 6622 | } else { |
| 6623 | result = (set1->val.num > set2->val.num); |
| 6624 | } |
| 6625 | } |
| 6626 | } |
| 6627 | |
| 6628 | /* assign result */ |
| 6629 | if (result) { |
| 6630 | set_fill_boolean(set1, 1); |
| 6631 | } else { |
| 6632 | set_fill_boolean(set1, 0); |
| 6633 | } |
| 6634 | |
| 6635 | return LY_SUCCESS; |
| 6636 | } |
| 6637 | |
| 6638 | /** |
| 6639 | * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div', |
| 6640 | * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware. |
| 6641 | * |
| 6642 | * @param[in,out] set1 Set to use for the result. |
| 6643 | * @param[in] set2 Set acting as the second operand for @p op. |
| 6644 | * @param[in] op Operator to process. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6645 | * @return LY_ERR |
| 6646 | */ |
| 6647 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6648 | 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] | 6649 | { |
| 6650 | LY_ERR rc; |
| 6651 | |
| 6652 | /* unary '-' */ |
| 6653 | if (!set2 && (op[0] == '-')) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6654 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6655 | LY_CHECK_RET(rc); |
| 6656 | set1->val.num *= -1; |
| 6657 | lyxp_set_free(set2); |
| 6658 | return LY_SUCCESS; |
| 6659 | } |
| 6660 | |
| 6661 | assert(set1 && set2); |
| 6662 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6663 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6664 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6665 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6666 | LY_CHECK_RET(rc); |
| 6667 | |
| 6668 | switch (op[0]) { |
| 6669 | /* '+' */ |
| 6670 | case '+': |
| 6671 | set1->val.num += set2->val.num; |
| 6672 | break; |
| 6673 | |
| 6674 | /* '-' */ |
| 6675 | case '-': |
| 6676 | set1->val.num -= set2->val.num; |
| 6677 | break; |
| 6678 | |
| 6679 | /* '*' */ |
| 6680 | case '*': |
| 6681 | set1->val.num *= set2->val.num; |
| 6682 | break; |
| 6683 | |
| 6684 | /* 'div' */ |
| 6685 | case 'd': |
| 6686 | set1->val.num /= set2->val.num; |
| 6687 | break; |
| 6688 | |
| 6689 | /* 'mod' */ |
| 6690 | case 'm': |
| 6691 | set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num); |
| 6692 | break; |
| 6693 | |
| 6694 | default: |
| 6695 | LOGINT_RET(set1->ctx); |
| 6696 | } |
| 6697 | |
| 6698 | return LY_SUCCESS; |
| 6699 | } |
| 6700 | |
| 6701 | /* |
| 6702 | * eval functions |
| 6703 | * |
| 6704 | * They execute a parsed XPath expression on some data subtree. |
| 6705 | */ |
| 6706 | |
| 6707 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6708 | * @brief Evaluate Predicate. Logs directly on error. |
| 6709 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6710 | * [9] Predicate ::= '[' Expr ']' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6711 | * |
| 6712 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6713 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6714 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 6715 | * @param[in] options XPath options. |
| 6716 | * @param[in] parent_pos_pred Whether parent predicate was a positional one. |
| 6717 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6718 | */ |
| 6719 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 6720 | 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] | 6721 | { |
| 6722 | LY_ERR rc; |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 6723 | uint16_t i, orig_exp; |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6724 | uint32_t orig_pos, orig_size; |
| 6725 | int32_t pred_in_ctx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6726 | struct lyxp_set set2; |
| 6727 | struct lyd_node *orig_parent; |
| 6728 | |
| 6729 | /* '[' */ |
| 6730 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 6731 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6732 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6733 | |
| 6734 | if (!set) { |
| 6735 | only_parse: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6736 | rc = eval_expr_select(exp, tok_idx, 0, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6737 | LY_CHECK_RET(rc); |
| 6738 | } else if (set->type == LYXP_SET_NODE_SET) { |
| 6739 | /* 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] | 6740 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6741 | |
| 6742 | /* empty set, nothing to evaluate */ |
| 6743 | if (!set->used) { |
| 6744 | goto only_parse; |
| 6745 | } |
| 6746 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6747 | orig_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6748 | orig_pos = 0; |
| 6749 | orig_size = set->used; |
| 6750 | orig_parent = NULL; |
Michal Vasko | 39dbf35 | 2020-05-21 10:08:59 +0200 | [diff] [blame] | 6751 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6752 | set_init(&set2, set); |
| 6753 | set_insert_node(&set2, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, 0); |
| 6754 | /* remember the node context position for position() and context size for last(), |
| 6755 | * predicates should always be evaluated with respect to the child axis (since we do |
| 6756 | * not support explicit axes) so we assign positions based on their parents */ |
| 6757 | if (parent_pos_pred && ((struct lyd_node *)set->val.nodes[i].node->parent != orig_parent)) { |
| 6758 | orig_parent = (struct lyd_node *)set->val.nodes[i].node->parent; |
| 6759 | orig_pos = 1; |
| 6760 | } else { |
| 6761 | ++orig_pos; |
| 6762 | } |
| 6763 | |
| 6764 | set2.ctx_pos = orig_pos; |
| 6765 | set2.ctx_size = orig_size; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6766 | *tok_idx = orig_exp; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6767 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6768 | rc = eval_expr_select(exp, tok_idx, 0, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6769 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6770 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6771 | return rc; |
| 6772 | } |
| 6773 | |
| 6774 | /* number is a position */ |
| 6775 | if (set2.type == LYXP_SET_NUMBER) { |
| 6776 | if ((long long)set2.val.num == orig_pos) { |
| 6777 | set2.val.num = 1; |
| 6778 | } else { |
| 6779 | set2.val.num = 0; |
| 6780 | } |
| 6781 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6782 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6783 | |
| 6784 | /* predicate satisfied or not? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6785 | if (!set2.val.bln) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6786 | set_remove_node_none(set, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6787 | } |
| 6788 | } |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6789 | set_remove_nodes_none(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6790 | |
| 6791 | } else if (set->type == LYXP_SET_SCNODE_SET) { |
| 6792 | for (i = 0; i < set->used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 6793 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6794 | /* there is a currently-valid node */ |
| 6795 | break; |
| 6796 | } |
| 6797 | } |
| 6798 | /* empty set, nothing to evaluate */ |
| 6799 | if (i == set->used) { |
| 6800 | goto only_parse; |
| 6801 | } |
| 6802 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6803 | orig_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6804 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6805 | /* set special in_ctx to all the valid snodes */ |
| 6806 | pred_in_ctx = set_scnode_new_in_ctx(set); |
| 6807 | |
| 6808 | /* use the valid snodes one-by-one */ |
| 6809 | for (i = 0; i < set->used; ++i) { |
| 6810 | if (set->val.scnodes[i].in_ctx != pred_in_ctx) { |
| 6811 | continue; |
| 6812 | } |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 6813 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6814 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6815 | *tok_idx = orig_exp; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6816 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6817 | rc = eval_expr_select(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6818 | LY_CHECK_RET(rc); |
| 6819 | |
| 6820 | set->val.scnodes[i].in_ctx = pred_in_ctx; |
| 6821 | } |
| 6822 | |
| 6823 | /* restore the state as it was before the predicate */ |
| 6824 | for (i = 0; i < set->used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 6825 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
| 6826 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6827 | } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 6828 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6829 | } |
| 6830 | } |
| 6831 | |
| 6832 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6833 | set2.type = LYXP_SET_NODE_SET; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6834 | set_fill_set(&set2, set); |
| 6835 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6836 | rc = eval_expr_select(exp, tok_idx, 0, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6837 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6838 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6839 | return rc; |
| 6840 | } |
| 6841 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6842 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6843 | if (!set2.val.bln) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6844 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6845 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6846 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6847 | } |
| 6848 | |
| 6849 | /* ']' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6850 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6851 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 6852 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6853 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6854 | |
| 6855 | return LY_SUCCESS; |
| 6856 | } |
| 6857 | |
| 6858 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6859 | * @brief Evaluate Literal. Logs directly on error. |
| 6860 | * |
| 6861 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6862 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6863 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 6864 | */ |
| 6865 | static void |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 6866 | 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] | 6867 | { |
| 6868 | if (set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6869 | if (exp->tok_len[*tok_idx] == 2) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6870 | set_fill_string(set, "", 0); |
| 6871 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6872 | 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] | 6873 | } |
| 6874 | } |
| 6875 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 6876 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6877 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6878 | } |
| 6879 | |
| 6880 | /** |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6881 | * @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] | 6882 | * |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6883 | * @param[in] exp Full parsed XPath expression. |
| 6884 | * @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] | 6885 | * @param[in] ctx_node Found schema node as the context for the predicate. |
| 6886 | * @param[in] cur_mod Current module for the expression. |
| 6887 | * @param[in] cur_node Current (original context) node. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6888 | * @param[in] format Format of any prefixes in key names/values. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6889 | * @param[in] prefix_data Format-specific prefix data (see ::ly_resolve_prefix). |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6890 | * @param[out] predicates Parsed predicates. |
| 6891 | * @param[out] pred_type Type of @p predicates. |
| 6892 | * @return LY_SUCCESS on success, |
| 6893 | * @return LY_ERR on any error. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6894 | */ |
| 6895 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 6896 | 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] | 6897 | const struct lys_module *cur_mod, const struct lysc_node *cur_node, LY_PREFIX_FORMAT format, void *prefix_data, |
| 6898 | struct ly_path_predicate **predicates, enum ly_path_pred_type *pred_type) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6899 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6900 | LY_ERR ret = LY_SUCCESS; |
| 6901 | uint16_t key_count, e_idx, pred_idx = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6902 | const struct lysc_node *key; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6903 | size_t pred_len; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6904 | uint32_t prev_lo; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6905 | struct lyxp_expr *exp2 = NULL; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6906 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6907 | assert(ctx_node->nodetype & (LYS_LIST | LYS_LEAFLIST)); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6908 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6909 | if (ctx_node->nodetype == LYS_LIST) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6910 | /* get key count */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6911 | if (ctx_node->flags & LYS_KEYLESS) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6912 | return LY_EINVAL; |
| 6913 | } |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6914 | for (key_count = 0, key = lysc_node_children(ctx_node, 0); key && (key->flags & LYS_KEY); key = key->next, ++key_count) {} |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6915 | assert(key_count); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6916 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6917 | /* learn where the predicates end */ |
| 6918 | e_idx = *tok_idx; |
| 6919 | while (key_count) { |
| 6920 | /* '[' */ |
| 6921 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) { |
| 6922 | return LY_EINVAL; |
| 6923 | } |
| 6924 | ++e_idx; |
| 6925 | |
| 6926 | /* ']' */ |
| 6927 | while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) { |
| 6928 | ++e_idx; |
| 6929 | } |
| 6930 | ++e_idx; |
| 6931 | |
| 6932 | /* another presumably key predicate parsed */ |
| 6933 | --key_count; |
| 6934 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6935 | } else { |
| 6936 | /* learn just where this single predicate ends */ |
| 6937 | e_idx = *tok_idx; |
| 6938 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6939 | /* '[' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6940 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) { |
| 6941 | return LY_EINVAL; |
| 6942 | } |
| 6943 | ++e_idx; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6944 | |
| 6945 | /* ']' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6946 | while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) { |
| 6947 | ++e_idx; |
| 6948 | } |
| 6949 | ++e_idx; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6950 | } |
| 6951 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6952 | /* get the joined length of all the keys predicates/of the single leaf-list predicate */ |
| 6953 | pred_len = (exp->tok_pos[e_idx - 1] + exp->tok_len[e_idx - 1]) - exp->tok_pos[*tok_idx]; |
| 6954 | |
| 6955 | /* turn logging off */ |
| 6956 | prev_lo = ly_log_options(0); |
| 6957 | |
| 6958 | /* parse the predicate(s) */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6959 | LY_CHECK_GOTO(ret = ly_path_parse_predicate(ctx_node->module->ctx, ctx_node, exp->expr + exp->tok_pos[*tok_idx], |
| 6960 | pred_len, LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp2), cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6961 | |
| 6962 | /* compile */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6963 | ret = ly_path_compile_predicate(ctx_node->module->ctx, cur_node, cur_mod, ctx_node, exp2, &pred_idx, format, |
| 6964 | prefix_data, predicates, pred_type); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6965 | LY_CHECK_GOTO(ret, cleanup); |
| 6966 | |
| 6967 | /* success, the predicate must include all the needed information for hash-based search */ |
| 6968 | *tok_idx = e_idx; |
| 6969 | |
| 6970 | cleanup: |
| 6971 | ly_log_options(prev_lo); |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6972 | lyxp_expr_free(ctx_node->module->ctx, exp2); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6973 | return ret; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6974 | } |
| 6975 | |
| 6976 | /** |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6977 | * @brief Search for/check the next schema node that could be the only matching schema node meaning the |
| 6978 | * data node(s) could be found using a single hash-based search. |
| 6979 | * |
| 6980 | * @param[in] node Next context node to check. |
| 6981 | * @param[in] name Expected node name. |
| 6982 | * @param[in] name_len Length of @p name. |
| 6983 | * @param[in] moveto_mod Expected node module. |
| 6984 | * @param[in] root_type XPath root type. |
| 6985 | * @param[in] no_prefix Whether the node name was unprefixed. |
| 6986 | * @param[in] format Prefix format. |
| 6987 | * @param[in,out] found Previously found node, is updated. |
| 6988 | * @return LY_SUCCESS on success, |
| 6989 | * @return LY_ENOT if the whole check failed and hashes cannot be used. |
| 6990 | */ |
| 6991 | static LY_ERR |
| 6992 | eval_name_test_with_predicate_get_scnode(const struct lyd_node *node, const char *name, uint16_t name_len, |
| 6993 | const struct lys_module *moveto_mod, enum lyxp_node_type root_type, ly_bool no_prefix, LY_PREFIX_FORMAT format, |
| 6994 | const struct lysc_node **found) |
| 6995 | { |
| 6996 | const struct lysc_node *scnode; |
| 6997 | const struct lys_module *mod; |
| 6998 | uint32_t idx = 0; |
| 6999 | |
| 7000 | continue_search: |
Michal Vasko | 7d1d091 | 2020-10-16 08:38:30 +0200 | [diff] [blame] | 7001 | scnode = NULL; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7002 | if (!node) { |
| 7003 | if (no_prefix && (format == LY_PREF_JSON)) { |
| 7004 | /* search all modules for a single match */ |
| 7005 | while ((mod = ly_ctx_get_module_iter(moveto_mod->ctx, &idx))) { |
| 7006 | scnode = lys_find_child(NULL, mod, name, name_len, 0, 0); |
| 7007 | if (scnode) { |
| 7008 | /* we have found a match */ |
| 7009 | break; |
| 7010 | } |
| 7011 | } |
| 7012 | |
Michal Vasko | 7d1d091 | 2020-10-16 08:38:30 +0200 | [diff] [blame] | 7013 | if (!scnode) { |
| 7014 | /* all modules searched */ |
| 7015 | idx = 0; |
| 7016 | } |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7017 | } else { |
| 7018 | /* search in top-level */ |
| 7019 | scnode = lys_find_child(NULL, moveto_mod, name, name_len, 0, 0); |
| 7020 | } |
| 7021 | } else if (!*found || (lysc_data_parent(*found) != node->schema)) { |
| 7022 | if (no_prefix && (format == LY_PREF_JSON)) { |
| 7023 | /* we must adjust the module to inherit the one from the context node */ |
| 7024 | moveto_mod = node->schema->module; |
| 7025 | } |
| 7026 | |
| 7027 | /* search in children, do not repeat the same search */ |
| 7028 | 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] | 7029 | } /* else skip redundant search */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7030 | |
| 7031 | /* additional context check */ |
| 7032 | if (scnode && (root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) { |
| 7033 | scnode = NULL; |
| 7034 | } |
| 7035 | |
| 7036 | if (scnode) { |
| 7037 | if (*found) { |
| 7038 | /* we found a schema node with the same name but at different level, give up, too complicated |
| 7039 | * (more hash-based searches would be required, not supported) */ |
| 7040 | return LY_ENOT; |
| 7041 | } else { |
| 7042 | /* remember the found schema node and continue to make sure it can be used */ |
| 7043 | *found = scnode; |
| 7044 | } |
| 7045 | } |
| 7046 | |
| 7047 | if (idx) { |
| 7048 | /* continue searching all the following models */ |
| 7049 | goto continue_search; |
| 7050 | } |
| 7051 | |
| 7052 | return LY_SUCCESS; |
| 7053 | } |
| 7054 | |
| 7055 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7056 | * @brief Evaluate NameTest and any following Predicates. Logs directly on error. |
| 7057 | * |
| 7058 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 7059 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 7060 | * [7] NameTest ::= '*' | NCName ':' '*' | QName |
| 7061 | * |
| 7062 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7063 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7064 | * @param[in] attr_axis Whether to search attributes or standard nodes. |
| 7065 | * @param[in] all_desc Whether to search all the descendants or children only. |
| 7066 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7067 | * @param[in] options XPath options. |
| 7068 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7069 | */ |
| 7070 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 7071 | 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] | 7072 | struct lyxp_set *set, uint32_t options) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7073 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7074 | char *path; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7075 | const char *ncname, *ncname_dict = NULL; |
| 7076 | uint16_t ncname_len; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7077 | const struct lys_module *moveto_mod = NULL; |
| 7078 | const struct lysc_node *scnode = NULL, *ctx_scnode; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7079 | struct ly_path_predicate *predicates = NULL; |
| 7080 | enum ly_path_pred_type pred_type = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7081 | LY_ERR rc = LY_SUCCESS; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7082 | ly_bool no_prefix; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7083 | |
| 7084 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7085 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7086 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7087 | |
| 7088 | if (!set) { |
| 7089 | goto moveto; |
| 7090 | } |
| 7091 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7092 | ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]]; |
| 7093 | ncname_len = exp->tok_len[*tok_idx - 1]; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7094 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7095 | /* get the first schema context node */ |
| 7096 | if (set->used) { |
| 7097 | if (set->type == LYXP_SET_NODE_SET) { |
| 7098 | ctx_scnode = set->val.nodes[0].node ? set->val.nodes[0].node->schema : NULL; |
| 7099 | } else { |
| 7100 | ctx_scnode = set->val.scnodes[0].scnode; |
| 7101 | } |
| 7102 | } else { |
| 7103 | ctx_scnode = NULL; |
| 7104 | } |
| 7105 | |
| 7106 | /* parse (and skip) module name, use any context node (if available) just so we get some moveto_mod if there |
| 7107 | * is no prefix for ::LY_PREF_JSON */ |
| 7108 | rc = moveto_resolve_model(&ncname, &ncname_len, set, ctx_scnode, &moveto_mod); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7109 | LY_CHECK_GOTO(rc, cleanup); |
| 7110 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7111 | if (ncname_len == exp->tok_len[*tok_idx - 1]) { |
| 7112 | /* remember that there is no prefix */ |
| 7113 | no_prefix = 1; |
| 7114 | } else { |
| 7115 | no_prefix = 0; |
| 7116 | } |
| 7117 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7118 | if (moveto_mod && !attr_axis && !all_desc && (set->type == LYXP_SET_NODE_SET)) { |
| 7119 | /* find the matching schema node in some parent in the context */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7120 | for (uint32_t i = 0; i < set->used; ++i) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7121 | if (eval_name_test_with_predicate_get_scnode(set->val.nodes[i].node, ncname, ncname_len, moveto_mod, |
| 7122 | set->root_type, no_prefix, set->format, &scnode)) { |
| 7123 | /* check failed */ |
| 7124 | scnode = NULL; |
| 7125 | break; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7126 | } |
| 7127 | } |
| 7128 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7129 | if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) { |
| 7130 | /* try to create the predicates */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7131 | if (eval_name_test_try_compile_predicates(exp, tok_idx, scnode, set->cur_mod, set->cur_node ? |
| 7132 | 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] | 7133 | /* hashes cannot be used */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7134 | scnode = NULL; |
| 7135 | } |
| 7136 | } |
| 7137 | } |
| 7138 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7139 | if (!scnode && moveto_mod) { |
| 7140 | /* we are not able to match based on a schema node and not all the modules match, |
| 7141 | * use dictionary for efficient comparison */ |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 7142 | 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] | 7143 | } |
| 7144 | |
Michal Vasko | 97ea163 | 2020-11-23 17:00:09 +0100 | [diff] [blame] | 7145 | if ((set->format == LY_PREF_JSON) && no_prefix) { |
| 7146 | /* do not set moveto_mod if there is no explicit prefix for JSON, it should be inherited from |
| 7147 | * the specific context node */ |
| 7148 | moveto_mod = NULL; |
| 7149 | } |
| 7150 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7151 | moveto: |
| 7152 | /* move to the attribute(s), data node(s), or schema node(s) */ |
| 7153 | if (attr_axis) { |
| 7154 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 7155 | set_scnode_clear_ctx(set); |
| 7156 | } else { |
| 7157 | if (all_desc) { |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 7158 | rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7159 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7160 | rc = moveto_attr(set, moveto_mod, ncname_dict); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7161 | } |
| 7162 | LY_CHECK_GOTO(rc, cleanup); |
| 7163 | } |
| 7164 | } else { |
| 7165 | if (set && (options & LYXP_SCNODE_ALL)) { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7166 | int64_t i; |
| 7167 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7168 | if (all_desc) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7169 | rc = moveto_scnode_alldesc(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7170 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7171 | rc = moveto_scnode(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7172 | } |
| 7173 | LY_CHECK_GOTO(rc, cleanup); |
| 7174 | |
| 7175 | for (i = set->used - 1; i > -1; --i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 7176 | if (set->val.scnodes[i].in_ctx > LYXP_SET_SCNODE_ATOM) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7177 | break; |
| 7178 | } |
| 7179 | } |
| 7180 | if (i == -1) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7181 | path = lysc_path(set->cur_scnode, LYSC_PATH_LOG, NULL, 0); |
Michal Vasko | 90f12dc | 2020-12-03 14:20:42 +0100 | [diff] [blame] | 7182 | LOGWRN(set->ctx, "Schema node \"%.*s\" not found (\"%.*s\") with context node \"%s\".", |
| 7183 | ncname_len, ncname, (ncname - exp->expr) + ncname_len, exp->expr, path); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7184 | free(path); |
| 7185 | } |
| 7186 | } else { |
| 7187 | if (all_desc) { |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 7188 | rc = moveto_node_alldesc(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7189 | } else { |
| 7190 | if (scnode) { |
| 7191 | /* we can find the nodes using hashes */ |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 7192 | rc = moveto_node_hash(set, scnode, predicates, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7193 | } else { |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 7194 | rc = moveto_node(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7195 | } |
| 7196 | } |
| 7197 | LY_CHECK_GOTO(rc, cleanup); |
| 7198 | } |
| 7199 | } |
| 7200 | |
| 7201 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7202 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 7203 | rc = eval_predicate(exp, tok_idx, set, options, 1); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7204 | LY_CHECK_RET(rc); |
| 7205 | } |
| 7206 | |
| 7207 | cleanup: |
Michal Vasko | db51a8d | 2020-05-27 15:22:29 +0200 | [diff] [blame] | 7208 | if (set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7209 | lydict_remove(set->ctx, ncname_dict); |
Michal Vasko | f7e16e2 | 2020-10-21 09:24:39 +0200 | [diff] [blame] | 7210 | ly_path_predicates_free(set->ctx, pred_type, predicates); |
Michal Vasko | db51a8d | 2020-05-27 15:22:29 +0200 | [diff] [blame] | 7211 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7212 | return rc; |
| 7213 | } |
| 7214 | |
| 7215 | /** |
| 7216 | * @brief Evaluate NodeType and any following Predicates. Logs directly on error. |
| 7217 | * |
| 7218 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 7219 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 7220 | * [8] NodeType ::= 'text' | 'node' |
| 7221 | * |
| 7222 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7223 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7224 | * @param[in] attr_axis Whether to search attributes or standard nodes. |
| 7225 | * @param[in] all_desc Whether to search all the descendants or children only. |
| 7226 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7227 | * @param[in] options XPath options. |
| 7228 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7229 | */ |
| 7230 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 7231 | 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] | 7232 | struct lyxp_set *set, uint32_t options) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7233 | { |
| 7234 | LY_ERR rc; |
| 7235 | |
| 7236 | /* TODO */ |
| 7237 | (void)attr_axis; |
| 7238 | (void)all_desc; |
| 7239 | |
| 7240 | if (set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7241 | assert(exp->tok_len[*tok_idx] == 4); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7242 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 7243 | set_scnode_clear_ctx(set); |
| 7244 | /* just for the debug message below */ |
| 7245 | set = NULL; |
| 7246 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7247 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7248 | rc = xpath_node(NULL, 0, set, options); |
| 7249 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7250 | assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4)); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7251 | rc = xpath_text(NULL, 0, set, options); |
| 7252 | } |
| 7253 | LY_CHECK_RET(rc); |
| 7254 | } |
| 7255 | } |
| 7256 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7257 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7258 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7259 | |
| 7260 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7261 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7262 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7263 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7264 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7265 | |
| 7266 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7267 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7268 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7269 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7270 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7271 | |
| 7272 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7273 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 7274 | rc = eval_predicate(exp, tok_idx, set, options, 1); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7275 | LY_CHECK_RET(rc); |
| 7276 | } |
| 7277 | |
| 7278 | return LY_SUCCESS; |
| 7279 | } |
| 7280 | |
| 7281 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7282 | * @brief Evaluate RelativeLocationPath. Logs directly on error. |
| 7283 | * |
| 7284 | * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step |
| 7285 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7286 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7287 | * |
| 7288 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7289 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7290 | * @param[in] all_desc Whether to search all the descendants or children only. |
| 7291 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7292 | * @param[in] options XPath options. |
| 7293 | * @return LY_ERR (YL_EINCOMPLETE on unresolved when) |
| 7294 | */ |
| 7295 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 7296 | eval_relative_location_path(const struct lyxp_expr *exp, uint16_t *tok_idx, ly_bool all_desc, struct lyxp_set *set, |
| 7297 | uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7298 | { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 7299 | ly_bool attr_axis; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7300 | LY_ERR rc; |
| 7301 | |
| 7302 | goto step; |
| 7303 | do { |
| 7304 | /* evaluate '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7305 | if (exp->tok_len[*tok_idx] == 1) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7306 | all_desc = 0; |
| 7307 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7308 | assert(exp->tok_len[*tok_idx] == 2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7309 | all_desc = 1; |
| 7310 | } |
| 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 | |
| 7315 | step: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7316 | /* evaluate abbreviated axis '@'? if any */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7317 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7318 | attr_axis = 1; |
| 7319 | |
| 7320 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7321 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7322 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7323 | } else { |
| 7324 | attr_axis = 0; |
| 7325 | } |
| 7326 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7327 | /* Step */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7328 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7329 | case LYXP_TOKEN_DOT: |
| 7330 | /* evaluate '.' */ |
| 7331 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 7332 | rc = moveto_scnode_self(set, all_desc, options); |
| 7333 | } else { |
| 7334 | rc = moveto_self(set, all_desc, options); |
| 7335 | } |
| 7336 | LY_CHECK_RET(rc); |
| 7337 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7338 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7339 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7340 | break; |
| 7341 | |
| 7342 | case LYXP_TOKEN_DDOT: |
| 7343 | /* evaluate '..' */ |
| 7344 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 7345 | rc = moveto_scnode_parent(set, all_desc, options); |
| 7346 | } else { |
| 7347 | rc = moveto_parent(set, all_desc, options); |
| 7348 | } |
| 7349 | LY_CHECK_RET(rc); |
| 7350 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7351 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7352 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7353 | break; |
| 7354 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7355 | case LYXP_TOKEN_NAMETEST: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7356 | /* evaluate NameTest Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7357 | 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] | 7358 | LY_CHECK_RET(rc); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7359 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7360 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7361 | case LYXP_TOKEN_NODETYPE: |
| 7362 | /* evaluate NodeType Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7363 | 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] | 7364 | LY_CHECK_RET(rc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7365 | break; |
| 7366 | |
| 7367 | default: |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 7368 | LOGINT_RET(set ? set->ctx : NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7369 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7370 | } 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] | 7371 | |
| 7372 | return LY_SUCCESS; |
| 7373 | } |
| 7374 | |
| 7375 | /** |
| 7376 | * @brief Evaluate AbsoluteLocationPath. Logs directly on error. |
| 7377 | * |
| 7378 | * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath |
| 7379 | * |
| 7380 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7381 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7382 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7383 | * @param[in] options XPath options. |
| 7384 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7385 | */ |
| 7386 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 7387 | 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] | 7388 | { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 7389 | ly_bool all_desc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7390 | |
| 7391 | if (set) { |
| 7392 | /* no matter what tokens follow, we need to be at the root */ |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 7393 | LY_CHECK_RET(moveto_root(set, options)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7394 | } |
| 7395 | |
| 7396 | /* '/' RelativeLocationPath? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7397 | if (exp->tok_len[*tok_idx] == 1) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7398 | /* evaluate '/' - deferred */ |
| 7399 | all_desc = 0; |
| 7400 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7401 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7402 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7403 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7404 | if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7405 | return LY_SUCCESS; |
| 7406 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7407 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7408 | case LYXP_TOKEN_DOT: |
| 7409 | case LYXP_TOKEN_DDOT: |
| 7410 | case LYXP_TOKEN_AT: |
| 7411 | case LYXP_TOKEN_NAMETEST: |
| 7412 | case LYXP_TOKEN_NODETYPE: |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 7413 | 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] | 7414 | break; |
| 7415 | default: |
| 7416 | break; |
| 7417 | } |
| 7418 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7419 | } else { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 7420 | /* '//' RelativeLocationPath */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7421 | /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */ |
| 7422 | all_desc = 1; |
| 7423 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7424 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7425 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7426 | |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 7427 | 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] | 7428 | } |
| 7429 | |
| 7430 | return LY_SUCCESS; |
| 7431 | } |
| 7432 | |
| 7433 | /** |
| 7434 | * @brief Evaluate FunctionCall. Logs directly on error. |
| 7435 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7436 | * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7437 | * |
| 7438 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7439 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7440 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7441 | * @param[in] options XPath options. |
| 7442 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7443 | */ |
| 7444 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 7445 | 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] | 7446 | { |
| 7447 | LY_ERR rc; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7448 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7449 | 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] | 7450 | uint16_t arg_count = 0, i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7451 | struct lyxp_set **args = NULL, **args_aux; |
| 7452 | |
| 7453 | if (set) { |
| 7454 | /* FunctionName */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7455 | switch (exp->tok_len[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7456 | case 3: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7457 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7458 | xpath_func = &xpath_not; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7459 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7460 | xpath_func = &xpath_sum; |
| 7461 | } |
| 7462 | break; |
| 7463 | case 4: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7464 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7465 | xpath_func = &xpath_lang; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7466 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7467 | xpath_func = &xpath_last; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7468 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7469 | xpath_func = &xpath_name; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7470 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7471 | xpath_func = &xpath_true; |
| 7472 | } |
| 7473 | break; |
| 7474 | case 5: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7475 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7476 | xpath_func = &xpath_count; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7477 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7478 | xpath_func = &xpath_false; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7479 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7480 | xpath_func = &xpath_floor; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7481 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7482 | xpath_func = &xpath_round; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7483 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7484 | xpath_func = &xpath_deref; |
| 7485 | } |
| 7486 | break; |
| 7487 | case 6: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7488 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7489 | xpath_func = &xpath_concat; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7490 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7491 | xpath_func = &xpath_number; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7492 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7493 | xpath_func = &xpath_string; |
| 7494 | } |
| 7495 | break; |
| 7496 | case 7: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7497 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7498 | xpath_func = &xpath_boolean; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7499 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7500 | xpath_func = &xpath_ceiling; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7501 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7502 | xpath_func = &xpath_current; |
| 7503 | } |
| 7504 | break; |
| 7505 | case 8: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7506 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7507 | xpath_func = &xpath_contains; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7508 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7509 | xpath_func = &xpath_position; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7510 | } 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] | 7511 | xpath_func = &xpath_re_match; |
| 7512 | } |
| 7513 | break; |
| 7514 | case 9: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7515 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7516 | xpath_func = &xpath_substring; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7517 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7518 | xpath_func = &xpath_translate; |
| 7519 | } |
| 7520 | break; |
| 7521 | case 10: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7522 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7523 | xpath_func = &xpath_local_name; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7524 | } 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] | 7525 | xpath_func = &xpath_enum_value; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7526 | } 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] | 7527 | xpath_func = &xpath_bit_is_set; |
| 7528 | } |
| 7529 | break; |
| 7530 | case 11: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7531 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7532 | xpath_func = &xpath_starts_with; |
| 7533 | } |
| 7534 | break; |
| 7535 | case 12: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7536 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7537 | xpath_func = &xpath_derived_from; |
| 7538 | } |
| 7539 | break; |
| 7540 | case 13: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7541 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7542 | xpath_func = &xpath_namespace_uri; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7543 | } 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] | 7544 | xpath_func = &xpath_string_length; |
| 7545 | } |
| 7546 | break; |
| 7547 | case 15: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7548 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7549 | xpath_func = &xpath_normalize_space; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7550 | } 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] | 7551 | xpath_func = &xpath_substring_after; |
| 7552 | } |
| 7553 | break; |
| 7554 | case 16: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7555 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7556 | xpath_func = &xpath_substring_before; |
| 7557 | } |
| 7558 | break; |
| 7559 | case 20: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7560 | 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] | 7561 | xpath_func = &xpath_derived_from_or_self; |
| 7562 | } |
| 7563 | break; |
| 7564 | } |
| 7565 | |
| 7566 | if (!xpath_func) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7567 | LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, 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] | 7568 | return LY_EVALID; |
| 7569 | } |
| 7570 | } |
| 7571 | |
| 7572 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7573 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7574 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7575 | |
| 7576 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7577 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7578 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7579 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7580 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7581 | |
| 7582 | /* ( Expr ( ',' Expr )* )? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7583 | if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7584 | if (set) { |
| 7585 | args = malloc(sizeof *args); |
| 7586 | LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup); |
| 7587 | arg_count = 1; |
| 7588 | args[0] = set_copy(set); |
| 7589 | if (!args[0]) { |
| 7590 | rc = LY_EMEM; |
| 7591 | goto cleanup; |
| 7592 | } |
| 7593 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7594 | rc = eval_expr_select(exp, tok_idx, 0, args[0], options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7595 | LY_CHECK_GOTO(rc, cleanup); |
| 7596 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7597 | rc = eval_expr_select(exp, tok_idx, 0, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7598 | LY_CHECK_GOTO(rc, cleanup); |
| 7599 | } |
| 7600 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7601 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7602 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7603 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7604 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7605 | |
| 7606 | if (set) { |
| 7607 | ++arg_count; |
| 7608 | args_aux = realloc(args, arg_count * sizeof *args); |
| 7609 | LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup); |
| 7610 | args = args_aux; |
| 7611 | args[arg_count - 1] = set_copy(set); |
| 7612 | if (!args[arg_count - 1]) { |
| 7613 | rc = LY_EMEM; |
| 7614 | goto cleanup; |
| 7615 | } |
| 7616 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7617 | 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] | 7618 | LY_CHECK_GOTO(rc, cleanup); |
| 7619 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7620 | rc = eval_expr_select(exp, tok_idx, 0, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7621 | LY_CHECK_GOTO(rc, cleanup); |
| 7622 | } |
| 7623 | } |
| 7624 | |
| 7625 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7626 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7627 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7628 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7629 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7630 | |
| 7631 | if (set) { |
| 7632 | /* evaluate function */ |
| 7633 | rc = xpath_func(args, arg_count, set, options); |
| 7634 | |
| 7635 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7636 | /* merge all nodes from arg evaluations */ |
| 7637 | for (i = 0; i < arg_count; ++i) { |
| 7638 | set_scnode_clear_ctx(args[i]); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7639 | lyxp_set_scnode_merge(set, args[i]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7640 | } |
| 7641 | } |
| 7642 | } else { |
| 7643 | rc = LY_SUCCESS; |
| 7644 | } |
| 7645 | |
| 7646 | cleanup: |
| 7647 | for (i = 0; i < arg_count; ++i) { |
| 7648 | lyxp_set_free(args[i]); |
| 7649 | } |
| 7650 | free(args); |
| 7651 | |
| 7652 | return rc; |
| 7653 | } |
| 7654 | |
| 7655 | /** |
| 7656 | * @brief Evaluate Number. Logs directly on error. |
| 7657 | * |
| 7658 | * @param[in] ctx Context for errors. |
| 7659 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7660 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7661 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7662 | * @return LY_ERR |
| 7663 | */ |
| 7664 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 7665 | 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] | 7666 | { |
| 7667 | long double num; |
| 7668 | char *endptr; |
| 7669 | |
| 7670 | if (set) { |
| 7671 | errno = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7672 | num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7673 | if (errno) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7674 | LOGVAL(ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]); |
| 7675 | LOGVAL(ctx, LY_VLOG_LYD, set->cur_node, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double (%s).", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7676 | 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] | 7677 | return LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7678 | } else if (endptr - &exp->expr[exp->tok_pos[*tok_idx]] != exp->tok_len[*tok_idx]) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7679 | LOGVAL(ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]); |
| 7680 | LOGVAL(ctx, LY_VLOG_LYD, set->cur_node, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double.", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7681 | exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7682 | return LY_EVALID; |
| 7683 | } |
| 7684 | |
| 7685 | set_fill_number(set, num); |
| 7686 | } |
| 7687 | |
| 7688 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7689 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7690 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7691 | return LY_SUCCESS; |
| 7692 | } |
| 7693 | |
| 7694 | /** |
| 7695 | * @brief Evaluate PathExpr. Logs directly on error. |
| 7696 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7697 | * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate* |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7698 | * | PrimaryExpr Predicate* '/' RelativeLocationPath |
| 7699 | * | PrimaryExpr Predicate* '//' RelativeLocationPath |
| 7700 | * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7701 | * [10] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7702 | * |
| 7703 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7704 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7705 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7706 | * @param[in] options XPath options. |
| 7707 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7708 | */ |
| 7709 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 7710 | 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] | 7711 | { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 7712 | ly_bool all_desc, parent_pos_pred; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7713 | LY_ERR rc; |
| 7714 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7715 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7716 | case LYXP_TOKEN_PAR1: |
| 7717 | /* '(' Expr ')' */ |
| 7718 | |
| 7719 | /* '(' */ |
| 7720 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7721 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7722 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7723 | |
| 7724 | /* Expr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7725 | rc = eval_expr_select(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7726 | LY_CHECK_RET(rc); |
| 7727 | |
| 7728 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7729 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7730 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7731 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7732 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7733 | |
| 7734 | parent_pos_pred = 0; |
| 7735 | goto predicate; |
| 7736 | |
| 7737 | case LYXP_TOKEN_DOT: |
| 7738 | case LYXP_TOKEN_DDOT: |
| 7739 | case LYXP_TOKEN_AT: |
| 7740 | case LYXP_TOKEN_NAMETEST: |
| 7741 | case LYXP_TOKEN_NODETYPE: |
| 7742 | /* RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7743 | rc = eval_relative_location_path(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7744 | LY_CHECK_RET(rc); |
| 7745 | break; |
| 7746 | |
| 7747 | case LYXP_TOKEN_FUNCNAME: |
| 7748 | /* FunctionCall */ |
| 7749 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7750 | rc = eval_function_call(exp, tok_idx, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7751 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7752 | rc = eval_function_call(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7753 | } |
| 7754 | LY_CHECK_RET(rc); |
| 7755 | |
| 7756 | parent_pos_pred = 1; |
| 7757 | goto predicate; |
| 7758 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 7759 | case LYXP_TOKEN_OPER_PATH: |
| 7760 | case LYXP_TOKEN_OPER_RPATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7761 | /* AbsoluteLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7762 | rc = eval_absolute_location_path(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7763 | LY_CHECK_RET(rc); |
| 7764 | break; |
| 7765 | |
| 7766 | case LYXP_TOKEN_LITERAL: |
| 7767 | /* Literal */ |
| 7768 | if (!set || (options & LYXP_SCNODE_ALL)) { |
| 7769 | if (set) { |
| 7770 | set_scnode_clear_ctx(set); |
| 7771 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7772 | eval_literal(exp, tok_idx, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7773 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7774 | eval_literal(exp, tok_idx, set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7775 | } |
| 7776 | |
| 7777 | parent_pos_pred = 1; |
| 7778 | goto predicate; |
| 7779 | |
| 7780 | case LYXP_TOKEN_NUMBER: |
| 7781 | /* Number */ |
| 7782 | if (!set || (options & LYXP_SCNODE_ALL)) { |
| 7783 | if (set) { |
| 7784 | set_scnode_clear_ctx(set); |
| 7785 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7786 | rc = eval_number(NULL, exp, tok_idx, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7787 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7788 | rc = eval_number(set->ctx, exp, tok_idx, set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7789 | } |
| 7790 | LY_CHECK_RET(rc); |
| 7791 | |
| 7792 | parent_pos_pred = 1; |
| 7793 | goto predicate; |
| 7794 | |
| 7795 | default: |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7796 | LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[*tok_idx]), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7797 | &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7798 | return LY_EVALID; |
| 7799 | } |
| 7800 | |
| 7801 | return LY_SUCCESS; |
| 7802 | |
| 7803 | predicate: |
| 7804 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7805 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 7806 | rc = eval_predicate(exp, tok_idx, set, options, parent_pos_pred); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7807 | LY_CHECK_RET(rc); |
| 7808 | } |
| 7809 | |
| 7810 | /* ('/' or '//') RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7811 | 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] | 7812 | |
| 7813 | /* evaluate '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7814 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7815 | all_desc = 0; |
| 7816 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7817 | all_desc = 1; |
| 7818 | } |
| 7819 | |
| 7820 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7821 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7822 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7823 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7824 | rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7825 | LY_CHECK_RET(rc); |
| 7826 | } |
| 7827 | |
| 7828 | return LY_SUCCESS; |
| 7829 | } |
| 7830 | |
| 7831 | /** |
| 7832 | * @brief Evaluate UnionExpr. Logs directly on error. |
| 7833 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7834 | * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7835 | * |
| 7836 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7837 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7838 | * @param[in] repeat How many times this expression is repeated. |
| 7839 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7840 | * @param[in] options XPath options. |
| 7841 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7842 | */ |
| 7843 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 7844 | 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] | 7845 | { |
| 7846 | LY_ERR rc = LY_SUCCESS; |
| 7847 | struct lyxp_set orig_set, set2; |
| 7848 | uint16_t i; |
| 7849 | |
| 7850 | assert(repeat); |
| 7851 | |
| 7852 | set_init(&orig_set, set); |
| 7853 | set_init(&set2, set); |
| 7854 | |
| 7855 | set_fill_set(&orig_set, set); |
| 7856 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7857 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7858 | LY_CHECK_GOTO(rc, cleanup); |
| 7859 | |
| 7860 | /* ('|' PathExpr)* */ |
| 7861 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7862 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7863 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7864 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7865 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7866 | |
| 7867 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7868 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7869 | LY_CHECK_GOTO(rc, cleanup); |
| 7870 | continue; |
| 7871 | } |
| 7872 | |
| 7873 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7874 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7875 | LY_CHECK_GOTO(rc, cleanup); |
| 7876 | |
| 7877 | /* eval */ |
| 7878 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7879 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7880 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7881 | rc = moveto_union(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7882 | LY_CHECK_GOTO(rc, cleanup); |
| 7883 | } |
| 7884 | } |
| 7885 | |
| 7886 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7887 | lyxp_set_free_content(&orig_set); |
| 7888 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7889 | return rc; |
| 7890 | } |
| 7891 | |
| 7892 | /** |
| 7893 | * @brief Evaluate UnaryExpr. Logs directly on error. |
| 7894 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7895 | * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7896 | * |
| 7897 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7898 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7899 | * @param[in] repeat How many times this expression is repeated. |
| 7900 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7901 | * @param[in] options XPath options. |
| 7902 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7903 | */ |
| 7904 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 7905 | 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] | 7906 | { |
| 7907 | LY_ERR rc; |
| 7908 | uint16_t this_op, i; |
| 7909 | |
| 7910 | assert(repeat); |
| 7911 | |
| 7912 | /* ('-')+ */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7913 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7914 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7915 | 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] | 7916 | |
| 7917 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7918 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7919 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7920 | } |
| 7921 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7922 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7923 | LY_CHECK_RET(rc); |
| 7924 | |
| 7925 | if (set && (repeat % 2)) { |
| 7926 | if (options & LYXP_SCNODE_ALL) { |
| 7927 | warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]); |
| 7928 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7929 | 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] | 7930 | LY_CHECK_RET(rc); |
| 7931 | } |
| 7932 | } |
| 7933 | |
| 7934 | return LY_SUCCESS; |
| 7935 | } |
| 7936 | |
| 7937 | /** |
| 7938 | * @brief Evaluate MultiplicativeExpr. Logs directly on error. |
| 7939 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7940 | * [18] MultiplicativeExpr ::= UnaryExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7941 | * | MultiplicativeExpr '*' UnaryExpr |
| 7942 | * | MultiplicativeExpr 'div' UnaryExpr |
| 7943 | * | MultiplicativeExpr 'mod' UnaryExpr |
| 7944 | * |
| 7945 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7946 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7947 | * @param[in] repeat How many times this expression is repeated. |
| 7948 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7949 | * @param[in] options XPath options. |
| 7950 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7951 | */ |
| 7952 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 7953 | eval_multiplicative_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, |
| 7954 | uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7955 | { |
| 7956 | LY_ERR rc; |
| 7957 | uint16_t this_op; |
| 7958 | struct lyxp_set orig_set, set2; |
| 7959 | uint16_t i; |
| 7960 | |
| 7961 | assert(repeat); |
| 7962 | |
| 7963 | set_init(&orig_set, set); |
| 7964 | set_init(&set2, set); |
| 7965 | |
| 7966 | set_fill_set(&orig_set, set); |
| 7967 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7968 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7969 | LY_CHECK_GOTO(rc, cleanup); |
| 7970 | |
| 7971 | /* ('*' / 'div' / 'mod' UnaryExpr)* */ |
| 7972 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7973 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7974 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7975 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7976 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7977 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7978 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7979 | |
| 7980 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7981 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7982 | LY_CHECK_GOTO(rc, cleanup); |
| 7983 | continue; |
| 7984 | } |
| 7985 | |
| 7986 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7987 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7988 | LY_CHECK_GOTO(rc, cleanup); |
| 7989 | |
| 7990 | /* eval */ |
| 7991 | if (options & LYXP_SCNODE_ALL) { |
| 7992 | 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] | 7993 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7994 | set_scnode_clear_ctx(set); |
| 7995 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7996 | 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] | 7997 | LY_CHECK_GOTO(rc, cleanup); |
| 7998 | } |
| 7999 | } |
| 8000 | |
| 8001 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8002 | lyxp_set_free_content(&orig_set); |
| 8003 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8004 | return rc; |
| 8005 | } |
| 8006 | |
| 8007 | /** |
| 8008 | * @brief Evaluate AdditiveExpr. Logs directly on error. |
| 8009 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8010 | * [17] AdditiveExpr ::= MultiplicativeExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8011 | * | AdditiveExpr '+' MultiplicativeExpr |
| 8012 | * | AdditiveExpr '-' MultiplicativeExpr |
| 8013 | * |
| 8014 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8015 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8016 | * @param[in] repeat How many times this expression is repeated. |
| 8017 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8018 | * @param[in] options XPath options. |
| 8019 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8020 | */ |
| 8021 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 8022 | 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] | 8023 | { |
| 8024 | LY_ERR rc; |
| 8025 | uint16_t this_op; |
| 8026 | struct lyxp_set orig_set, set2; |
| 8027 | uint16_t i; |
| 8028 | |
| 8029 | assert(repeat); |
| 8030 | |
| 8031 | set_init(&orig_set, set); |
| 8032 | set_init(&set2, set); |
| 8033 | |
| 8034 | set_fill_set(&orig_set, set); |
| 8035 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8036 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8037 | LY_CHECK_GOTO(rc, cleanup); |
| 8038 | |
| 8039 | /* ('+' / '-' MultiplicativeExpr)* */ |
| 8040 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8041 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8042 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8043 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8044 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 8045 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8046 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8047 | |
| 8048 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8049 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8050 | LY_CHECK_GOTO(rc, cleanup); |
| 8051 | continue; |
| 8052 | } |
| 8053 | |
| 8054 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8055 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8056 | LY_CHECK_GOTO(rc, cleanup); |
| 8057 | |
| 8058 | /* eval */ |
| 8059 | if (options & LYXP_SCNODE_ALL) { |
| 8060 | 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] | 8061 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8062 | set_scnode_clear_ctx(set); |
| 8063 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8064 | 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] | 8065 | LY_CHECK_GOTO(rc, cleanup); |
| 8066 | } |
| 8067 | } |
| 8068 | |
| 8069 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8070 | lyxp_set_free_content(&orig_set); |
| 8071 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8072 | return rc; |
| 8073 | } |
| 8074 | |
| 8075 | /** |
| 8076 | * @brief Evaluate RelationalExpr. Logs directly on error. |
| 8077 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8078 | * [16] RelationalExpr ::= AdditiveExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8079 | * | RelationalExpr '<' AdditiveExpr |
| 8080 | * | RelationalExpr '>' AdditiveExpr |
| 8081 | * | RelationalExpr '<=' AdditiveExpr |
| 8082 | * | RelationalExpr '>=' AdditiveExpr |
| 8083 | * |
| 8084 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8085 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8086 | * @param[in] repeat How many times this expression is repeated. |
| 8087 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8088 | * @param[in] options XPath options. |
| 8089 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8090 | */ |
| 8091 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 8092 | 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] | 8093 | { |
| 8094 | LY_ERR rc; |
| 8095 | uint16_t this_op; |
| 8096 | struct lyxp_set orig_set, set2; |
| 8097 | uint16_t i; |
| 8098 | |
| 8099 | assert(repeat); |
| 8100 | |
| 8101 | set_init(&orig_set, set); |
| 8102 | set_init(&set2, set); |
| 8103 | |
| 8104 | set_fill_set(&orig_set, set); |
| 8105 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8106 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8107 | LY_CHECK_GOTO(rc, cleanup); |
| 8108 | |
| 8109 | /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */ |
| 8110 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8111 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8112 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8113 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8114 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 8115 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8116 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8117 | |
| 8118 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8119 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8120 | LY_CHECK_GOTO(rc, cleanup); |
| 8121 | continue; |
| 8122 | } |
| 8123 | |
| 8124 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8125 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8126 | LY_CHECK_GOTO(rc, cleanup); |
| 8127 | |
| 8128 | /* eval */ |
| 8129 | if (options & LYXP_SCNODE_ALL) { |
| 8130 | 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] | 8131 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8132 | set_scnode_clear_ctx(set); |
| 8133 | } else { |
| 8134 | rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options); |
| 8135 | LY_CHECK_GOTO(rc, cleanup); |
| 8136 | } |
| 8137 | } |
| 8138 | |
| 8139 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8140 | lyxp_set_free_content(&orig_set); |
| 8141 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8142 | return rc; |
| 8143 | } |
| 8144 | |
| 8145 | /** |
| 8146 | * @brief Evaluate EqualityExpr. Logs directly on error. |
| 8147 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8148 | * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8149 | * | EqualityExpr '!=' RelationalExpr |
| 8150 | * |
| 8151 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8152 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8153 | * @param[in] repeat How many times this expression is repeated. |
| 8154 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8155 | * @param[in] options XPath options. |
| 8156 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8157 | */ |
| 8158 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 8159 | 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] | 8160 | { |
| 8161 | LY_ERR rc; |
| 8162 | uint16_t this_op; |
| 8163 | struct lyxp_set orig_set, set2; |
| 8164 | uint16_t i; |
| 8165 | |
| 8166 | assert(repeat); |
| 8167 | |
| 8168 | set_init(&orig_set, set); |
| 8169 | set_init(&set2, set); |
| 8170 | |
| 8171 | set_fill_set(&orig_set, set); |
| 8172 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8173 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8174 | LY_CHECK_GOTO(rc, cleanup); |
| 8175 | |
| 8176 | /* ('=' / '!=' RelationalExpr)* */ |
| 8177 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8178 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8179 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8180 | 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] | 8181 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 8182 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8183 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8184 | |
| 8185 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8186 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8187 | LY_CHECK_GOTO(rc, cleanup); |
| 8188 | continue; |
| 8189 | } |
| 8190 | |
| 8191 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8192 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8193 | LY_CHECK_GOTO(rc, cleanup); |
| 8194 | |
| 8195 | /* eval */ |
| 8196 | if (options & LYXP_SCNODE_ALL) { |
| 8197 | 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] | 8198 | warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1); |
| 8199 | 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] | 8200 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8201 | set_scnode_clear_ctx(set); |
| 8202 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8203 | rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options); |
| 8204 | LY_CHECK_GOTO(rc, cleanup); |
| 8205 | } |
| 8206 | } |
| 8207 | |
| 8208 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8209 | lyxp_set_free_content(&orig_set); |
| 8210 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8211 | return rc; |
| 8212 | } |
| 8213 | |
| 8214 | /** |
| 8215 | * @brief Evaluate AndExpr. Logs directly on error. |
| 8216 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8217 | * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8218 | * |
| 8219 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8220 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8221 | * @param[in] repeat How many times this expression is repeated. |
| 8222 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8223 | * @param[in] options XPath options. |
| 8224 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8225 | */ |
| 8226 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 8227 | 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] | 8228 | { |
| 8229 | LY_ERR rc; |
| 8230 | struct lyxp_set orig_set, set2; |
| 8231 | uint16_t i; |
| 8232 | |
| 8233 | assert(repeat); |
| 8234 | |
| 8235 | set_init(&orig_set, set); |
| 8236 | set_init(&set2, set); |
| 8237 | |
| 8238 | set_fill_set(&orig_set, set); |
| 8239 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8240 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8241 | LY_CHECK_GOTO(rc, cleanup); |
| 8242 | |
| 8243 | /* cast to boolean, we know that will be the final result */ |
| 8244 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 8245 | set_scnode_clear_ctx(set); |
| 8246 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8247 | lyxp_set_cast(set, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8248 | } |
| 8249 | |
| 8250 | /* ('and' EqualityExpr)* */ |
| 8251 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8252 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG); |
| 8253 | 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] | 8254 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8255 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8256 | |
| 8257 | /* lazy evaluation */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8258 | if (!set || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) { |
| 8259 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8260 | LY_CHECK_GOTO(rc, cleanup); |
| 8261 | continue; |
| 8262 | } |
| 8263 | |
| 8264 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8265 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8266 | LY_CHECK_GOTO(rc, cleanup); |
| 8267 | |
| 8268 | /* eval - just get boolean value actually */ |
| 8269 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 8270 | set_scnode_clear_ctx(&set2); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 8271 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8272 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8273 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8274 | set_fill_set(set, &set2); |
| 8275 | } |
| 8276 | } |
| 8277 | |
| 8278 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8279 | lyxp_set_free_content(&orig_set); |
| 8280 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8281 | return rc; |
| 8282 | } |
| 8283 | |
| 8284 | /** |
| 8285 | * @brief Evaluate OrExpr. Logs directly on error. |
| 8286 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8287 | * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8288 | * |
| 8289 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8290 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8291 | * @param[in] repeat How many times this expression is repeated. |
| 8292 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8293 | * @param[in] options XPath options. |
| 8294 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8295 | */ |
| 8296 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 8297 | 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] | 8298 | { |
| 8299 | LY_ERR rc; |
| 8300 | struct lyxp_set orig_set, set2; |
| 8301 | uint16_t i; |
| 8302 | |
| 8303 | assert(repeat); |
| 8304 | |
| 8305 | set_init(&orig_set, set); |
| 8306 | set_init(&set2, set); |
| 8307 | |
| 8308 | set_fill_set(&orig_set, set); |
| 8309 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8310 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8311 | LY_CHECK_GOTO(rc, cleanup); |
| 8312 | |
| 8313 | /* cast to boolean, we know that will be the final result */ |
| 8314 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 8315 | set_scnode_clear_ctx(set); |
| 8316 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8317 | lyxp_set_cast(set, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8318 | } |
| 8319 | |
| 8320 | /* ('or' AndExpr)* */ |
| 8321 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8322 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG); |
| 8323 | 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] | 8324 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8325 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8326 | |
| 8327 | /* lazy evaluation */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8328 | if (!set || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) { |
| 8329 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8330 | LY_CHECK_GOTO(rc, cleanup); |
| 8331 | continue; |
| 8332 | } |
| 8333 | |
| 8334 | set_fill_set(&set2, &orig_set); |
| 8335 | /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one), |
| 8336 | * but it does not matter */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8337 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8338 | LY_CHECK_GOTO(rc, cleanup); |
| 8339 | |
| 8340 | /* eval - just get boolean value actually */ |
| 8341 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 8342 | set_scnode_clear_ctx(&set2); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 8343 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8344 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8345 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8346 | set_fill_set(set, &set2); |
| 8347 | } |
| 8348 | } |
| 8349 | |
| 8350 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8351 | lyxp_set_free_content(&orig_set); |
| 8352 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8353 | return rc; |
| 8354 | } |
| 8355 | |
| 8356 | /** |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8357 | * @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] | 8358 | * |
| 8359 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8360 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8361 | * @param[in] etype Expression type to evaluate. |
| 8362 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8363 | * @param[in] options XPath options. |
| 8364 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8365 | */ |
| 8366 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 8367 | eval_expr_select(const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype, struct lyxp_set *set, |
| 8368 | uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8369 | { |
| 8370 | uint16_t i, count; |
| 8371 | enum lyxp_expr_type next_etype; |
| 8372 | LY_ERR rc; |
| 8373 | |
| 8374 | /* process operator repeats */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8375 | if (!exp->repeat[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8376 | next_etype = LYXP_EXPR_NONE; |
| 8377 | } else { |
| 8378 | /* find etype repeat */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 8379 | for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i) {} |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8380 | |
| 8381 | /* select one-priority lower because etype expression called us */ |
| 8382 | if (i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8383 | next_etype = exp->repeat[*tok_idx][i - 1]; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8384 | /* count repeats for that expression */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 8385 | 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] | 8386 | } else { |
| 8387 | next_etype = LYXP_EXPR_NONE; |
| 8388 | } |
| 8389 | } |
| 8390 | |
| 8391 | /* decide what expression are we parsing based on the repeat */ |
| 8392 | switch (next_etype) { |
| 8393 | case LYXP_EXPR_OR: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8394 | rc = eval_or_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8395 | break; |
| 8396 | case LYXP_EXPR_AND: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8397 | rc = eval_and_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8398 | break; |
| 8399 | case LYXP_EXPR_EQUALITY: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8400 | rc = eval_equality_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8401 | break; |
| 8402 | case LYXP_EXPR_RELATIONAL: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8403 | rc = eval_relational_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8404 | break; |
| 8405 | case LYXP_EXPR_ADDITIVE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8406 | rc = eval_additive_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8407 | break; |
| 8408 | case LYXP_EXPR_MULTIPLICATIVE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8409 | rc = eval_multiplicative_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8410 | break; |
| 8411 | case LYXP_EXPR_UNARY: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8412 | rc = eval_unary_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8413 | break; |
| 8414 | case LYXP_EXPR_UNION: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8415 | rc = eval_union_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8416 | break; |
| 8417 | case LYXP_EXPR_NONE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8418 | rc = eval_path_expr(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8419 | break; |
| 8420 | default: |
| 8421 | LOGINT_RET(set->ctx); |
| 8422 | } |
| 8423 | |
| 8424 | return rc; |
| 8425 | } |
| 8426 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8427 | /** |
| 8428 | * @brief Get root type. |
| 8429 | * |
| 8430 | * @param[in] ctx_node Context node. |
| 8431 | * @param[in] ctx_scnode Schema context node. |
| 8432 | * @param[in] options XPath options. |
| 8433 | * @return Root type. |
| 8434 | */ |
| 8435 | static enum lyxp_node_type |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 8436 | 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] | 8437 | { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 8438 | const struct lysc_node *op; |
| 8439 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8440 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8441 | /* schema */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 8442 | 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] | 8443 | |
| 8444 | if (op || (options & LYXP_SCNODE)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8445 | /* general root that can access everything */ |
| 8446 | return LYXP_NODE_ROOT; |
| 8447 | } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) { |
| 8448 | /* root context node can access only config data (because we said so, it is unspecified) */ |
| 8449 | return LYXP_NODE_ROOT_CONFIG; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8450 | } |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 8451 | return LYXP_NODE_ROOT; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8452 | } |
| 8453 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8454 | /* data */ |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 8455 | op = ctx_node ? ctx_node->schema : NULL; |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 8456 | 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] | 8457 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8458 | if (op || !(options & LYXP_SCHEMA)) { |
| 8459 | /* general root that can access everything */ |
| 8460 | return LYXP_NODE_ROOT; |
| 8461 | } else if (!ctx_node || (ctx_node->schema->flags & LYS_CONFIG_W)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8462 | /* root context node can access only config data (because we said so, it is unspecified) */ |
| 8463 | return LYXP_NODE_ROOT_CONFIG; |
| 8464 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8465 | return LYXP_NODE_ROOT; |
| 8466 | } |
| 8467 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8468 | LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 8469 | lyxp_eval(const struct lyxp_expr *exp, const struct lys_module *cur_mod, LY_PREFIX_FORMAT format, void *prefix_data, |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8470 | const struct lyd_node *ctx_node, const struct lyd_node *tree, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8471 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8472 | uint16_t tok_idx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8473 | LY_ERR rc; |
| 8474 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8475 | LY_CHECK_ARG_RET(NULL, exp, set, LY_EINVAL); |
| 8476 | if (!cur_mod && ((format == LY_PREF_SCHEMA) || (format == LY_PREF_SCHEMA_RESOLVED))) { |
| 8477 | LOGARG(NULL, "Current module must be set if schema format is used."); |
| 8478 | return LY_EINVAL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8479 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8480 | |
Michal Vasko | d3bb12f | 2020-12-04 14:33:09 +0100 | [diff] [blame] | 8481 | if (tree) { |
| 8482 | /* adjust the pointer to be the first top-level sibling */ |
| 8483 | while (tree->parent) { |
| 8484 | tree = lyd_parent(tree); |
| 8485 | } |
| 8486 | tree = lyd_first_sibling(tree); |
| 8487 | } |
| 8488 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8489 | /* prepare set for evaluation */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8490 | memset(set, 0, sizeof *set); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8491 | set->type = LYXP_SET_NODE_SET; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8492 | set->root_type = lyxp_get_root_type(ctx_node, NULL, options); |
| 8493 | set_insert_node(set, (struct lyd_node *)ctx_node, 0, ctx_node ? LYXP_NODE_ELEM : set->root_type, 0); |
| 8494 | |
| 8495 | set->ctx = (struct ly_ctx *)(cur_mod ? cur_mod->ctx : (ctx_node ? LYD_CTX(ctx_node) : (tree ? LYD_CTX(tree) : NULL))); |
| 8496 | set->cur_node = ctx_node; |
| 8497 | for (set->context_op = ctx_node ? ctx_node->schema : NULL; |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 8498 | set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); |
| 8499 | set->context_op = set->context_op->parent) {} |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 8500 | set->tree = tree; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8501 | set->cur_mod = cur_mod; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8502 | set->format = format; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8503 | set->prefix_data = prefix_data; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8504 | |
| 8505 | /* evaluate */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8506 | rc = eval_expr_select(exp, &tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8507 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8508 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8509 | } |
| 8510 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8511 | return rc; |
| 8512 | } |
| 8513 | |
| 8514 | #if 0 |
| 8515 | |
| 8516 | /* full xml printing of set elements, not used currently */ |
| 8517 | |
| 8518 | void |
| 8519 | lyxp_set_print_xml(FILE *f, struct lyxp_set *set) |
| 8520 | { |
| 8521 | uint32_t i; |
| 8522 | char *str_num; |
| 8523 | struct lyout out; |
| 8524 | |
| 8525 | memset(&out, 0, sizeof out); |
| 8526 | |
| 8527 | out.type = LYOUT_STREAM; |
| 8528 | out.method.f = f; |
| 8529 | |
| 8530 | switch (set->type) { |
| 8531 | case LYXP_SET_EMPTY: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8532 | ly_print_(&out, "Empty XPath set\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8533 | break; |
| 8534 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8535 | ly_print_(&out, "Boolean XPath set:\n"); |
| 8536 | ly_print_(&out, "%s\n\n", set->value.bool ? "true" : "false"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8537 | break; |
| 8538 | case LYXP_SET_STRING: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8539 | ly_print_(&out, "String XPath set:\n"); |
| 8540 | ly_print_(&out, "\"%s\"\n\n", set->value.str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8541 | break; |
| 8542 | case LYXP_SET_NUMBER: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8543 | ly_print_(&out, "Number XPath set:\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8544 | |
| 8545 | if (isnan(set->value.num)) { |
| 8546 | str_num = strdup("NaN"); |
| 8547 | } else if ((set->value.num == 0) || (set->value.num == -0.0f)) { |
| 8548 | str_num = strdup("0"); |
| 8549 | } else if (isinf(set->value.num) && !signbit(set->value.num)) { |
| 8550 | str_num = strdup("Infinity"); |
| 8551 | } else if (isinf(set->value.num) && signbit(set->value.num)) { |
| 8552 | str_num = strdup("-Infinity"); |
| 8553 | } else if ((long long)set->value.num == set->value.num) { |
| 8554 | if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) { |
| 8555 | str_num = NULL; |
| 8556 | } |
| 8557 | } else { |
| 8558 | if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) { |
| 8559 | str_num = NULL; |
| 8560 | } |
| 8561 | } |
| 8562 | if (!str_num) { |
| 8563 | LOGMEM; |
| 8564 | return; |
| 8565 | } |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8566 | ly_print_(&out, "%s\n\n", str_num); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8567 | free(str_num); |
| 8568 | break; |
| 8569 | case LYXP_SET_NODE_SET: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8570 | ly_print_(&out, "Node XPath set:\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8571 | |
| 8572 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8573 | ly_print_(&out, "%d. ", i + 1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8574 | switch (set->node_type[i]) { |
| 8575 | case LYXP_NODE_ROOT_ALL: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8576 | ly_print_(&out, "ROOT all\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8577 | break; |
| 8578 | case LYXP_NODE_ROOT_CONFIG: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8579 | ly_print_(&out, "ROOT config\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8580 | break; |
| 8581 | case LYXP_NODE_ROOT_STATE: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8582 | ly_print_(&out, "ROOT state\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8583 | break; |
| 8584 | case LYXP_NODE_ROOT_NOTIF: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8585 | 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] | 8586 | break; |
| 8587 | case LYXP_NODE_ROOT_RPC: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8588 | 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] | 8589 | break; |
| 8590 | case LYXP_NODE_ROOT_OUTPUT: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8591 | 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] | 8592 | break; |
| 8593 | case LYXP_NODE_ELEM: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8594 | ly_print_(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8595 | xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT); |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8596 | ly_print_(&out, "\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8597 | break; |
| 8598 | case LYXP_NODE_TEXT: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8599 | 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] | 8600 | break; |
| 8601 | case LYXP_NODE_ATTR: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8602 | 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] | 8603 | break; |
| 8604 | } |
| 8605 | } |
| 8606 | break; |
| 8607 | } |
| 8608 | } |
| 8609 | |
| 8610 | #endif |
| 8611 | |
| 8612 | LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8613 | lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8614 | { |
| 8615 | long double num; |
| 8616 | char *str; |
| 8617 | LY_ERR rc; |
| 8618 | |
| 8619 | if (!set || (set->type == target)) { |
| 8620 | return LY_SUCCESS; |
| 8621 | } |
| 8622 | |
| 8623 | /* it's not possible to convert anything into a node set */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8624 | assert(target != LYXP_SET_NODE_SET); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8625 | |
| 8626 | if (set->type == LYXP_SET_SCNODE_SET) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8627 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8628 | return LY_EINVAL; |
| 8629 | } |
| 8630 | |
| 8631 | /* to STRING */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8632 | 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] | 8633 | switch (set->type) { |
| 8634 | case LYXP_SET_NUMBER: |
| 8635 | if (isnan(set->val.num)) { |
| 8636 | set->val.str = strdup("NaN"); |
| 8637 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 8638 | } else if ((set->val.num == 0) || (set->val.num == -0.0f)) { |
| 8639 | set->val.str = strdup("0"); |
| 8640 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 8641 | } else if (isinf(set->val.num) && !signbit(set->val.num)) { |
| 8642 | set->val.str = strdup("Infinity"); |
| 8643 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 8644 | } else if (isinf(set->val.num) && signbit(set->val.num)) { |
| 8645 | set->val.str = strdup("-Infinity"); |
| 8646 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 8647 | } else if ((long long)set->val.num == set->val.num) { |
| 8648 | if (asprintf(&str, "%lld", (long long)set->val.num) == -1) { |
| 8649 | LOGMEM_RET(set->ctx); |
| 8650 | } |
| 8651 | set->val.str = str; |
| 8652 | } else { |
| 8653 | if (asprintf(&str, "%03.1Lf", set->val.num) == -1) { |
| 8654 | LOGMEM_RET(set->ctx); |
| 8655 | } |
| 8656 | set->val.str = str; |
| 8657 | } |
| 8658 | break; |
| 8659 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8660 | if (set->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8661 | set->val.str = strdup("true"); |
| 8662 | } else { |
| 8663 | set->val.str = strdup("false"); |
| 8664 | } |
| 8665 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM); |
| 8666 | break; |
| 8667 | case LYXP_SET_NODE_SET: |
| 8668 | assert(set->used); |
| 8669 | |
| 8670 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8671 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8672 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8673 | rc = cast_node_set_to_string(set, &str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8674 | LY_CHECK_RET(rc); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8675 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8676 | set->val.str = str; |
| 8677 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8678 | default: |
| 8679 | LOGINT_RET(set->ctx); |
| 8680 | } |
| 8681 | set->type = LYXP_SET_STRING; |
| 8682 | } |
| 8683 | |
| 8684 | /* to NUMBER */ |
| 8685 | if (target == LYXP_SET_NUMBER) { |
| 8686 | switch (set->type) { |
| 8687 | case LYXP_SET_STRING: |
| 8688 | num = cast_string_to_number(set->val.str); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8689 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8690 | set->val.num = num; |
| 8691 | break; |
| 8692 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8693 | if (set->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8694 | set->val.num = 1; |
| 8695 | } else { |
| 8696 | set->val.num = 0; |
| 8697 | } |
| 8698 | break; |
| 8699 | default: |
| 8700 | LOGINT_RET(set->ctx); |
| 8701 | } |
| 8702 | set->type = LYXP_SET_NUMBER; |
| 8703 | } |
| 8704 | |
| 8705 | /* to BOOLEAN */ |
| 8706 | if (target == LYXP_SET_BOOLEAN) { |
| 8707 | switch (set->type) { |
| 8708 | case LYXP_SET_NUMBER: |
| 8709 | 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] | 8710 | set->val.bln = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8711 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8712 | set->val.bln = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8713 | } |
| 8714 | break; |
| 8715 | case LYXP_SET_STRING: |
| 8716 | if (set->val.str[0]) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8717 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8718 | set->val.bln = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8719 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8720 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8721 | set->val.bln = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8722 | } |
| 8723 | break; |
| 8724 | case LYXP_SET_NODE_SET: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8725 | if (set->used) { |
| 8726 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8727 | set->val.bln = 1; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8728 | } else { |
| 8729 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8730 | set->val.bln = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8731 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8732 | break; |
| 8733 | default: |
| 8734 | LOGINT_RET(set->ctx); |
| 8735 | } |
| 8736 | set->type = LYXP_SET_BOOLEAN; |
| 8737 | } |
| 8738 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8739 | return LY_SUCCESS; |
| 8740 | } |
| 8741 | |
| 8742 | LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 8743 | lyxp_atomize(const struct lyxp_expr *exp, const struct lys_module *cur_mod, LY_PREFIX_FORMAT format, void *prefix_data, |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8744 | const struct lysc_node *ctx_scnode, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8745 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8746 | uint16_t tok_idx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8747 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8748 | LY_CHECK_ARG_RET(NULL, exp, set, LY_EINVAL); |
| 8749 | if (!cur_mod && ((format == LY_PREF_SCHEMA) || (format == LY_PREF_SCHEMA_RESOLVED))) { |
| 8750 | LOGARG(NULL, "Current module must be set if schema format is used."); |
| 8751 | return LY_EINVAL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8752 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8753 | |
| 8754 | /* prepare set for evaluation */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8755 | memset(set, 0, sizeof *set); |
| 8756 | set->type = LYXP_SET_SCNODE_SET; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8757 | set->root_type = lyxp_get_root_type(NULL, ctx_scnode, options); |
| 8758 | 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] | 8759 | set->val.scnodes[0].in_ctx = LYXP_SET_SCNODE_START; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8760 | |
| 8761 | set->ctx = cur_mod ? cur_mod->ctx : (ctx_scnode ? ctx_scnode->module->ctx : NULL); |
| 8762 | set->cur_scnode = ctx_scnode; |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 8763 | for (set->context_op = ctx_scnode; |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 8764 | set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); |
| 8765 | set->context_op = set->context_op->parent) {} |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8766 | set->cur_mod = cur_mod; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8767 | set->format = format; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8768 | set->prefix_data = prefix_data; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8769 | |
| 8770 | /* evaluate */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8771 | return eval_expr_select(exp, &tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8772 | } |
Michal Vasko | d43d71a | 2020-08-07 14:54:58 +0200 | [diff] [blame] | 8773 | |
| 8774 | API const char * |
| 8775 | lyxp_get_expr(const struct lyxp_expr *path) |
| 8776 | { |
| 8777 | if (!path) { |
| 8778 | return NULL; |
| 8779 | } |
| 8780 | |
| 8781 | return path->expr; |
| 8782 | } |