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" |
Radek Krejci | 7711410 | 2021-03-10 15:21:57 +0100 | [diff] [blame] | 43 | #include "tree_data.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 44 | #include "tree_data_internal.h" |
Radek Krejci | 859a15a | 2021-03-05 20:56:59 +0100 | [diff] [blame] | 45 | #include "tree_edit.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 46 | #include "tree_schema_internal.h" |
| 47 | #include "xml.h" |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 48 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 49 | 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] | 50 | static LY_ERR eval_expr_select(const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype, |
| 51 | struct lyxp_set *set, uint32_t options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 52 | |
| 53 | /** |
| 54 | * @brief Print the type of an XPath \p set. |
| 55 | * |
| 56 | * @param[in] set Set to use. |
| 57 | * @return Set type string. |
| 58 | */ |
| 59 | static const char * |
| 60 | print_set_type(struct lyxp_set *set) |
| 61 | { |
| 62 | switch (set->type) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 63 | case LYXP_SET_NODE_SET: |
| 64 | return "node set"; |
| 65 | case LYXP_SET_SCNODE_SET: |
| 66 | return "schema node set"; |
| 67 | case LYXP_SET_BOOLEAN: |
| 68 | return "boolean"; |
| 69 | case LYXP_SET_NUMBER: |
| 70 | return "number"; |
| 71 | case LYXP_SET_STRING: |
| 72 | return "string"; |
| 73 | } |
| 74 | |
| 75 | return NULL; |
| 76 | } |
| 77 | |
Michal Vasko | 24cddf8 | 2020-06-01 08:17:01 +0200 | [diff] [blame] | 78 | const char * |
| 79 | lyxp_print_token(enum lyxp_token tok) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 80 | { |
| 81 | switch (tok) { |
| 82 | case LYXP_TOKEN_PAR1: |
| 83 | return "("; |
| 84 | case LYXP_TOKEN_PAR2: |
| 85 | return ")"; |
| 86 | case LYXP_TOKEN_BRACK1: |
| 87 | return "["; |
| 88 | case LYXP_TOKEN_BRACK2: |
| 89 | return "]"; |
| 90 | case LYXP_TOKEN_DOT: |
| 91 | return "."; |
| 92 | case LYXP_TOKEN_DDOT: |
| 93 | return ".."; |
| 94 | case LYXP_TOKEN_AT: |
| 95 | return "@"; |
| 96 | case LYXP_TOKEN_COMMA: |
| 97 | return ","; |
| 98 | case LYXP_TOKEN_NAMETEST: |
| 99 | return "NameTest"; |
| 100 | case LYXP_TOKEN_NODETYPE: |
| 101 | return "NodeType"; |
| 102 | case LYXP_TOKEN_FUNCNAME: |
| 103 | return "FunctionName"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 104 | case LYXP_TOKEN_OPER_LOG: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 105 | return "Operator(Logic)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 106 | case LYXP_TOKEN_OPER_EQUAL: |
| 107 | return "Operator(Equal)"; |
| 108 | case LYXP_TOKEN_OPER_NEQUAL: |
| 109 | return "Operator(Non-equal)"; |
| 110 | case LYXP_TOKEN_OPER_COMP: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 111 | return "Operator(Comparison)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 112 | case LYXP_TOKEN_OPER_MATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 113 | return "Operator(Math)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 114 | case LYXP_TOKEN_OPER_UNI: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 115 | return "Operator(Union)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 116 | case LYXP_TOKEN_OPER_PATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 117 | return "Operator(Path)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 118 | case LYXP_TOKEN_OPER_RPATH: |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 119 | return "Operator(Recursive Path)"; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 120 | case LYXP_TOKEN_LITERAL: |
| 121 | return "Literal"; |
| 122 | case LYXP_TOKEN_NUMBER: |
| 123 | return "Number"; |
| 124 | default: |
| 125 | LOGINT(NULL); |
| 126 | return ""; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * @brief Print the whole expression \p exp to debug output. |
| 132 | * |
| 133 | * @param[in] exp Expression to use. |
| 134 | */ |
| 135 | static void |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 136 | print_expr_struct_debug(const struct lyxp_expr *exp) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 137 | { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 138 | #define MSG_BUFFER_SIZE 128 |
| 139 | char tmp[MSG_BUFFER_SIZE]; |
Michal Vasko | 1fdd8fa | 2021-01-08 09:21:45 +0100 | [diff] [blame] | 140 | uint32_t i, j; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 141 | |
Radek Krejci | 52b6d51 | 2020-10-12 12:33:17 +0200 | [diff] [blame] | 142 | if (!exp || (ly_ll < LY_LLDBG)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 143 | return; |
| 144 | } |
| 145 | |
| 146 | LOGDBG(LY_LDGXPATH, "expression \"%s\":", exp->expr); |
| 147 | for (i = 0; i < exp->used; ++i) { |
Michal Vasko | 24cddf8 | 2020-06-01 08:17:01 +0200 | [diff] [blame] | 148 | 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] | 149 | &exp->expr[exp->tok_pos[i]]); |
Michal Vasko | 2304955 | 2021-03-04 15:57:44 +0100 | [diff] [blame] | 150 | if (exp->repeat && exp->repeat[i]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 151 | sprintf(tmp + strlen(tmp), " (repeat %d", exp->repeat[i][0]); |
| 152 | for (j = 1; exp->repeat[i][j]; ++j) { |
| 153 | sprintf(tmp + strlen(tmp), ", %d", exp->repeat[i][j]); |
| 154 | } |
| 155 | strcat(tmp, ")"); |
| 156 | } |
| 157 | LOGDBG(LY_LDGXPATH, tmp); |
| 158 | } |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 159 | #undef MSG_BUFFER_SIZE |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | #ifndef NDEBUG |
| 163 | |
| 164 | /** |
| 165 | * @brief Print XPath set content to debug output. |
| 166 | * |
| 167 | * @param[in] set Set to print. |
| 168 | */ |
| 169 | static void |
| 170 | print_set_debug(struct lyxp_set *set) |
| 171 | { |
| 172 | uint32_t i; |
| 173 | char *str; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 174 | struct lyxp_set_node *item; |
| 175 | struct lyxp_set_scnode *sitem; |
| 176 | |
Radek Krejci | 52b6d51 | 2020-10-12 12:33:17 +0200 | [diff] [blame] | 177 | if (ly_ll < LY_LLDBG) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 178 | return; |
| 179 | } |
| 180 | |
| 181 | switch (set->type) { |
| 182 | case LYXP_SET_NODE_SET: |
| 183 | LOGDBG(LY_LDGXPATH, "set NODE SET:"); |
| 184 | for (i = 0; i < set->used; ++i) { |
| 185 | item = &set->val.nodes[i]; |
| 186 | |
| 187 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 188 | case LYXP_NODE_NONE: |
| 189 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): NONE", i + 1, item->pos); |
| 190 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 191 | case LYXP_NODE_ROOT: |
| 192 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT", i + 1, item->pos); |
| 193 | break; |
| 194 | case LYXP_NODE_ROOT_CONFIG: |
| 195 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT CONFIG", i + 1, item->pos); |
| 196 | break; |
| 197 | case LYXP_NODE_ELEM: |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 198 | if ((item->node->schema->nodetype == LYS_LIST) && (lyd_child(item->node)->schema->nodetype == LYS_LEAF)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 199 | 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] | 200 | item->node->schema->name, LYD_CANON_VALUE(lyd_child(item->node))); |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 201 | } else if (item->node->schema->nodetype == LYS_LEAFLIST) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 202 | 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] | 203 | item->node->schema->name, LYD_CANON_VALUE(item->node)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 204 | } else { |
| 205 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s", i + 1, item->pos, item->node->schema->name); |
| 206 | } |
| 207 | break; |
| 208 | case LYXP_NODE_TEXT: |
| 209 | if (item->node->schema->nodetype & LYS_ANYDATA) { |
| 210 | 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] | 211 | item->node->schema->nodetype == LYS_ANYXML ? "anyxml" : "anydata"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 212 | } else { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 213 | 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] | 214 | } |
| 215 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 216 | case LYXP_NODE_META: |
| 217 | 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] | 218 | set->val.meta[i].meta->value); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 219 | break; |
| 220 | } |
| 221 | } |
| 222 | break; |
| 223 | |
| 224 | case LYXP_SET_SCNODE_SET: |
| 225 | LOGDBG(LY_LDGXPATH, "set SCNODE SET:"); |
| 226 | for (i = 0; i < set->used; ++i) { |
| 227 | sitem = &set->val.scnodes[i]; |
| 228 | |
| 229 | switch (sitem->type) { |
| 230 | case LYXP_NODE_ROOT: |
| 231 | LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT", i + 1, sitem->in_ctx); |
| 232 | break; |
| 233 | case LYXP_NODE_ROOT_CONFIG: |
| 234 | LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT CONFIG", i + 1, sitem->in_ctx); |
| 235 | break; |
| 236 | case LYXP_NODE_ELEM: |
| 237 | LOGDBG(LY_LDGXPATH, "\t%d (%u): ELEM %s", i + 1, sitem->in_ctx, sitem->scnode->name); |
| 238 | break; |
| 239 | default: |
| 240 | LOGINT(NULL); |
| 241 | break; |
| 242 | } |
| 243 | } |
| 244 | break; |
| 245 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 246 | case LYXP_SET_BOOLEAN: |
| 247 | LOGDBG(LY_LDGXPATH, "set BOOLEAN"); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 248 | LOGDBG(LY_LDGXPATH, "\t%s", (set->val.bln ? "true" : "false")); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 249 | break; |
| 250 | |
| 251 | case LYXP_SET_STRING: |
| 252 | LOGDBG(LY_LDGXPATH, "set STRING"); |
| 253 | LOGDBG(LY_LDGXPATH, "\t%s", set->val.str); |
| 254 | break; |
| 255 | |
| 256 | case LYXP_SET_NUMBER: |
| 257 | LOGDBG(LY_LDGXPATH, "set NUMBER"); |
| 258 | |
| 259 | if (isnan(set->val.num)) { |
| 260 | str = strdup("NaN"); |
| 261 | } else if ((set->val.num == 0) || (set->val.num == -0.0f)) { |
| 262 | str = strdup("0"); |
| 263 | } else if (isinf(set->val.num) && !signbit(set->val.num)) { |
| 264 | str = strdup("Infinity"); |
| 265 | } else if (isinf(set->val.num) && signbit(set->val.num)) { |
| 266 | str = strdup("-Infinity"); |
| 267 | } else if ((long long)set->val.num == set->val.num) { |
| 268 | if (asprintf(&str, "%lld", (long long)set->val.num) == -1) { |
| 269 | str = NULL; |
| 270 | } |
| 271 | } else { |
| 272 | if (asprintf(&str, "%03.1Lf", set->val.num) == -1) { |
| 273 | str = NULL; |
| 274 | } |
| 275 | } |
| 276 | LY_CHECK_ERR_RET(!str, LOGMEM(NULL), ); |
| 277 | |
| 278 | LOGDBG(LY_LDGXPATH, "\t%s", str); |
| 279 | free(str); |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | #endif |
| 284 | |
| 285 | /** |
| 286 | * @brief Realloc the string \p str. |
| 287 | * |
| 288 | * @param[in] ctx libyang context for logging. |
| 289 | * @param[in] needed How much free space is required. |
| 290 | * @param[in,out] str Pointer to the string to use. |
| 291 | * @param[in,out] used Used bytes in \p str. |
| 292 | * @param[in,out] size Allocated bytes in \p str. |
| 293 | * @return LY_ERR |
| 294 | */ |
| 295 | static LY_ERR |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 296 | 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] | 297 | { |
| 298 | if (*size - *used < needed) { |
| 299 | do { |
| 300 | if ((UINT16_MAX - *size) < LYXP_STRING_CAST_SIZE_STEP) { |
| 301 | LOGERR(ctx, LY_EINVAL, "XPath string length limit (%u) reached.", UINT16_MAX); |
| 302 | return LY_EINVAL; |
| 303 | } |
| 304 | *size += LYXP_STRING_CAST_SIZE_STEP; |
| 305 | } while (*size - *used < needed); |
| 306 | *str = ly_realloc(*str, *size * sizeof(char)); |
| 307 | LY_CHECK_ERR_RET(!(*str), LOGMEM(ctx), LY_EMEM); |
| 308 | } |
| 309 | |
| 310 | return LY_SUCCESS; |
| 311 | } |
| 312 | |
| 313 | /** |
| 314 | * @brief Cast nodes recursively to one string @p str. |
| 315 | * |
| 316 | * @param[in] node Node to cast. |
| 317 | * @param[in] fake_cont Whether to put the data into a "fake" container. |
| 318 | * @param[in] root_type Type of the XPath root. |
| 319 | * @param[in] indent Current indent. |
| 320 | * @param[in,out] str Resulting string. |
| 321 | * @param[in,out] used Used bytes in @p str. |
| 322 | * @param[in,out] size Allocated bytes in @p str. |
| 323 | * @return LY_ERR |
| 324 | */ |
| 325 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 326 | 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] | 327 | uint16_t *used, uint16_t *size) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 328 | { |
Radek Krejci | 7f769d7 | 2020-07-11 23:13:56 +0200 | [diff] [blame] | 329 | char *buf, *line, *ptr = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 330 | const char *value_str; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 331 | const struct lyd_node *child; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 332 | struct lyd_node *tree; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 333 | struct lyd_node_any *any; |
| 334 | LY_ERR rc; |
| 335 | |
| 336 | if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) { |
| 337 | return LY_SUCCESS; |
| 338 | } |
| 339 | |
| 340 | if (fake_cont) { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 341 | rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 342 | LY_CHECK_RET(rc); |
| 343 | strcpy(*str + (*used - 1), "\n"); |
| 344 | ++(*used); |
| 345 | |
| 346 | ++indent; |
| 347 | } |
| 348 | |
| 349 | switch (node->schema->nodetype) { |
| 350 | case LYS_CONTAINER: |
| 351 | case LYS_LIST: |
| 352 | case LYS_RPC: |
| 353 | case LYS_NOTIF: |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 354 | rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 355 | LY_CHECK_RET(rc); |
| 356 | strcpy(*str + (*used - 1), "\n"); |
| 357 | ++(*used); |
| 358 | |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 359 | for (child = lyd_child(node); child; child = child->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 360 | rc = cast_string_recursive(child, 0, root_type, indent + 1, str, used, size); |
| 361 | LY_CHECK_RET(rc); |
| 362 | } |
| 363 | |
| 364 | break; |
| 365 | |
| 366 | case LYS_LEAF: |
| 367 | case LYS_LEAFLIST: |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 368 | value_str = LYD_CANON_VALUE(node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 369 | |
| 370 | /* print indent */ |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 371 | 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] | 372 | memset(*str + (*used - 1), ' ', indent * 2); |
| 373 | *used += indent * 2; |
| 374 | |
| 375 | /* print value */ |
| 376 | if (*used == 1) { |
| 377 | sprintf(*str + (*used - 1), "%s", value_str); |
| 378 | *used += strlen(value_str); |
| 379 | } else { |
| 380 | sprintf(*str + (*used - 1), "%s\n", value_str); |
| 381 | *used += strlen(value_str) + 1; |
| 382 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 383 | |
| 384 | break; |
| 385 | |
| 386 | case LYS_ANYXML: |
| 387 | case LYS_ANYDATA: |
| 388 | any = (struct lyd_node_any *)node; |
| 389 | if (!(void *)any->value.tree) { |
| 390 | /* no content */ |
| 391 | buf = strdup(""); |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 392 | LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_CTX(node)), LY_EMEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 393 | } else { |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 394 | struct ly_out *out; |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 395 | |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 396 | if (any->value_type == LYD_ANYDATA_LYB) { |
| 397 | /* try to parse it into a data tree */ |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 398 | 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] | 399 | /* successfully parsed */ |
| 400 | free(any->value.mem); |
| 401 | any->value.tree = tree; |
| 402 | any->value_type = LYD_ANYDATA_DATATREE; |
| 403 | } |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 404 | /* 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] | 405 | } |
| 406 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 407 | switch (any->value_type) { |
| 408 | case LYD_ANYDATA_STRING: |
| 409 | case LYD_ANYDATA_XML: |
| 410 | case LYD_ANYDATA_JSON: |
| 411 | buf = strdup(any->value.json); |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 412 | LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_CTX(node)), LY_EMEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 413 | break; |
| 414 | case LYD_ANYDATA_DATATREE: |
Radek Krejci | 84ce7b1 | 2020-06-11 17:28:25 +0200 | [diff] [blame] | 415 | LY_CHECK_RET(ly_out_new_memory(&buf, 0, &out)); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 416 | rc = lyd_print_all(out, any->value.tree, LYD_XML, 0); |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 417 | ly_out_free(out, NULL, 0); |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 418 | LY_CHECK_RET(rc < 0, -rc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 419 | break; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 420 | case LYD_ANYDATA_LYB: |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 421 | LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot convert LYB anydata into string."); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 422 | return LY_EINVAL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 423 | } |
| 424 | } |
| 425 | |
| 426 | line = strtok_r(buf, "\n", &ptr); |
| 427 | do { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 428 | 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] | 429 | if (rc != LY_SUCCESS) { |
| 430 | free(buf); |
| 431 | return rc; |
| 432 | } |
| 433 | memset(*str + (*used - 1), ' ', indent * 2); |
| 434 | *used += indent * 2; |
| 435 | |
| 436 | strcpy(*str + (*used - 1), line); |
| 437 | *used += strlen(line); |
| 438 | |
| 439 | strcpy(*str + (*used - 1), "\n"); |
| 440 | *used += 1; |
| 441 | } while ((line = strtok_r(NULL, "\n", &ptr))); |
| 442 | |
| 443 | free(buf); |
| 444 | break; |
| 445 | |
| 446 | default: |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 447 | LOGINT_RET(LYD_CTX(node)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | if (fake_cont) { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 451 | rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 452 | LY_CHECK_RET(rc); |
| 453 | strcpy(*str + (*used - 1), "\n"); |
| 454 | ++(*used); |
| 455 | |
| 456 | --indent; |
| 457 | } |
| 458 | |
| 459 | return LY_SUCCESS; |
| 460 | } |
| 461 | |
| 462 | /** |
| 463 | * @brief Cast an element into a string. |
| 464 | * |
| 465 | * @param[in] node Node to cast. |
| 466 | * @param[in] fake_cont Whether to put the data into a "fake" container. |
| 467 | * @param[in] root_type Type of the XPath root. |
| 468 | * @param[out] str Element cast to dynamically-allocated string. |
| 469 | * @return LY_ERR |
| 470 | */ |
| 471 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 472 | 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] | 473 | { |
| 474 | uint16_t used, size; |
| 475 | LY_ERR rc; |
| 476 | |
| 477 | *str = malloc(LYXP_STRING_CAST_SIZE_START * sizeof(char)); |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 478 | LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_CTX(node)), LY_EMEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 479 | (*str)[0] = '\0'; |
| 480 | used = 1; |
| 481 | size = LYXP_STRING_CAST_SIZE_START; |
| 482 | |
| 483 | rc = cast_string_recursive(node, fake_cont, root_type, 0, str, &used, &size); |
| 484 | if (rc != LY_SUCCESS) { |
| 485 | free(*str); |
| 486 | return rc; |
| 487 | } |
| 488 | |
| 489 | if (size > used) { |
| 490 | *str = ly_realloc(*str, used * sizeof(char)); |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 491 | LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_CTX(node)), LY_EMEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 492 | } |
| 493 | return LY_SUCCESS; |
| 494 | } |
| 495 | |
| 496 | /** |
| 497 | * @brief Cast a LYXP_SET_NODE_SET set into a string. |
| 498 | * Context position aware. |
| 499 | * |
| 500 | * @param[in] set Set to cast. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 501 | * @param[out] str Cast dynamically-allocated string. |
| 502 | * @return LY_ERR |
| 503 | */ |
| 504 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 505 | cast_node_set_to_string(struct lyxp_set *set, char **str) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 506 | { |
Michal Vasko | aa67706 | 2021-03-09 13:52:53 +0100 | [diff] [blame] | 507 | if (!set->used) { |
| 508 | *str = strdup(""); |
| 509 | if (!*str) { |
| 510 | LOGMEM_RET(set->ctx); |
| 511 | } |
| 512 | return LY_SUCCESS; |
| 513 | } |
| 514 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 515 | switch (set->val.nodes[0].type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 516 | case LYXP_NODE_NONE: |
| 517 | /* invalid */ |
| 518 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 519 | case LYXP_NODE_ROOT: |
| 520 | case LYXP_NODE_ROOT_CONFIG: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 521 | 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] | 522 | case LYXP_NODE_ELEM: |
| 523 | case LYXP_NODE_TEXT: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 524 | 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] | 525 | case LYXP_NODE_META: |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 526 | *str = strdup(set->val.meta[0].meta->value.canonical); |
| 527 | if (!*str) { |
| 528 | LOGMEM_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 529 | } |
| 530 | return LY_SUCCESS; |
| 531 | } |
| 532 | |
| 533 | LOGINT_RET(set->ctx); |
| 534 | } |
| 535 | |
| 536 | /** |
| 537 | * @brief Cast a string into an XPath number. |
| 538 | * |
| 539 | * @param[in] str String to use. |
| 540 | * @return Cast number. |
| 541 | */ |
| 542 | static long double |
| 543 | cast_string_to_number(const char *str) |
| 544 | { |
| 545 | long double num; |
| 546 | char *ptr; |
| 547 | |
| 548 | errno = 0; |
| 549 | num = strtold(str, &ptr); |
| 550 | if (errno || *ptr) { |
| 551 | num = NAN; |
| 552 | } |
| 553 | return num; |
| 554 | } |
| 555 | |
| 556 | /** |
| 557 | * @brief Callback for checking value equality. |
| 558 | * |
Michal Vasko | 62524a9 | 2021-02-26 10:08:50 +0100 | [diff] [blame] | 559 | * Implementation of ::lyht_value_equal_cb. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 560 | * |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 561 | * @param[in] val1_p First value. |
| 562 | * @param[in] val2_p Second value. |
| 563 | * @param[in] mod Whether hash table is being modified. |
| 564 | * @param[in] cb_data Callback data. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 565 | * @return Boolean value whether values are equal or not. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 566 | */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 567 | static ly_bool |
| 568 | 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] | 569 | { |
| 570 | struct lyxp_set_hash_node *val1, *val2; |
| 571 | |
| 572 | val1 = (struct lyxp_set_hash_node *)val1_p; |
| 573 | val2 = (struct lyxp_set_hash_node *)val2_p; |
| 574 | |
| 575 | if ((val1->node == val2->node) && (val1->type == val2->type)) { |
| 576 | return 1; |
| 577 | } |
| 578 | |
| 579 | return 0; |
| 580 | } |
| 581 | |
| 582 | /** |
| 583 | * @brief Insert node and its hash into set. |
| 584 | * |
| 585 | * @param[in] set et to insert to. |
| 586 | * @param[in] node Node with hash. |
| 587 | * @param[in] type Node type. |
| 588 | */ |
| 589 | static void |
| 590 | set_insert_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type) |
| 591 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 592 | LY_ERR r; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 593 | uint32_t i, hash; |
| 594 | struct lyxp_set_hash_node hnode; |
| 595 | |
| 596 | if (!set->ht && (set->used >= LYD_HT_MIN_ITEMS)) { |
| 597 | /* create hash table and add all the nodes */ |
| 598 | set->ht = lyht_new(1, sizeof(struct lyxp_set_hash_node), set_values_equal_cb, NULL, 1); |
| 599 | for (i = 0; i < set->used; ++i) { |
| 600 | hnode.node = set->val.nodes[i].node; |
| 601 | hnode.type = set->val.nodes[i].type; |
| 602 | |
| 603 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 604 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 605 | hash = dict_hash_multi(hash, NULL, 0); |
| 606 | |
| 607 | r = lyht_insert(set->ht, &hnode, hash, NULL); |
| 608 | assert(!r); |
| 609 | (void)r; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 610 | |
Michal Vasko | 9d6befd | 2019-12-11 14:56:56 +0100 | [diff] [blame] | 611 | if (hnode.node == node) { |
| 612 | /* it was just added, do not add it twice */ |
| 613 | node = NULL; |
| 614 | } |
| 615 | } |
| 616 | } |
| 617 | |
| 618 | if (set->ht && node) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 619 | /* add the new node into hash table */ |
| 620 | hnode.node = node; |
| 621 | hnode.type = type; |
| 622 | |
| 623 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 624 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 625 | hash = dict_hash_multi(hash, NULL, 0); |
| 626 | |
| 627 | r = lyht_insert(set->ht, &hnode, hash, NULL); |
| 628 | assert(!r); |
| 629 | (void)r; |
| 630 | } |
| 631 | } |
| 632 | |
| 633 | /** |
| 634 | * @brief Remove node and its hash from set. |
| 635 | * |
| 636 | * @param[in] set Set to remove from. |
| 637 | * @param[in] node Node to remove. |
| 638 | * @param[in] type Node type. |
| 639 | */ |
| 640 | static void |
| 641 | set_remove_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type) |
| 642 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 643 | LY_ERR r; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 644 | struct lyxp_set_hash_node hnode; |
| 645 | uint32_t hash; |
| 646 | |
| 647 | if (set->ht) { |
| 648 | hnode.node = node; |
| 649 | hnode.type = type; |
| 650 | |
| 651 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 652 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 653 | hash = dict_hash_multi(hash, NULL, 0); |
| 654 | |
| 655 | r = lyht_remove(set->ht, &hnode, hash); |
| 656 | assert(!r); |
| 657 | (void)r; |
| 658 | |
| 659 | if (!set->ht->used) { |
| 660 | lyht_free(set->ht); |
| 661 | set->ht = NULL; |
| 662 | } |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | /** |
| 667 | * @brief Check whether node is in set based on its hash. |
| 668 | * |
| 669 | * @param[in] set Set to search in. |
| 670 | * @param[in] node Node to search for. |
| 671 | * @param[in] type Node type. |
| 672 | * @param[in] skip_idx Index in @p set to skip. |
| 673 | * @return LY_ERR |
| 674 | */ |
| 675 | static LY_ERR |
| 676 | set_dup_node_hash_check(const struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type, int skip_idx) |
| 677 | { |
| 678 | struct lyxp_set_hash_node hnode, *match_p; |
| 679 | uint32_t hash; |
| 680 | |
| 681 | hnode.node = node; |
| 682 | hnode.type = type; |
| 683 | |
| 684 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 685 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 686 | hash = dict_hash_multi(hash, NULL, 0); |
| 687 | |
| 688 | if (!lyht_find(set->ht, &hnode, hash, (void **)&match_p)) { |
| 689 | if ((skip_idx > -1) && (set->val.nodes[skip_idx].node == match_p->node) && (set->val.nodes[skip_idx].type == match_p->type)) { |
| 690 | /* we found it on the index that should be skipped, find another */ |
| 691 | hnode = *match_p; |
| 692 | if (lyht_find_next(set->ht, &hnode, hash, (void **)&match_p)) { |
| 693 | /* none other found */ |
| 694 | return LY_SUCCESS; |
| 695 | } |
| 696 | } |
| 697 | |
| 698 | return LY_EEXIST; |
| 699 | } |
| 700 | |
| 701 | /* not found */ |
| 702 | return LY_SUCCESS; |
| 703 | } |
| 704 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 705 | void |
| 706 | lyxp_set_free_content(struct lyxp_set *set) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 707 | { |
| 708 | if (!set) { |
| 709 | return; |
| 710 | } |
| 711 | |
| 712 | if (set->type == LYXP_SET_NODE_SET) { |
| 713 | free(set->val.nodes); |
| 714 | lyht_free(set->ht); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 715 | } else if (set->type == LYXP_SET_SCNODE_SET) { |
| 716 | free(set->val.scnodes); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 717 | lyht_free(set->ht); |
| 718 | } else { |
| 719 | if (set->type == LYXP_SET_STRING) { |
| 720 | free(set->val.str); |
| 721 | } |
| 722 | set->type = LYXP_SET_NODE_SET; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 723 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 724 | |
| 725 | set->val.nodes = NULL; |
| 726 | set->used = 0; |
| 727 | set->size = 0; |
| 728 | set->ht = NULL; |
| 729 | set->ctx_pos = 0; |
| 730 | set->ctx_pos = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 731 | } |
| 732 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 733 | /** |
| 734 | * @brief Free dynamically-allocated set. |
| 735 | * |
| 736 | * @param[in] set Set to free. |
| 737 | */ |
| 738 | static void |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 739 | lyxp_set_free(struct lyxp_set *set) |
| 740 | { |
| 741 | if (!set) { |
| 742 | return; |
| 743 | } |
| 744 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 745 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 746 | free(set); |
| 747 | } |
| 748 | |
| 749 | /** |
| 750 | * @brief Initialize set context. |
| 751 | * |
| 752 | * @param[in] new Set to initialize. |
| 753 | * @param[in] set Arbitrary initialized set. |
| 754 | */ |
| 755 | static void |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 756 | set_init(struct lyxp_set *new, const struct lyxp_set *set) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 757 | { |
| 758 | memset(new, 0, sizeof *new); |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 759 | if (set) { |
| 760 | new->ctx = set->ctx; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 761 | new->cur_node = set->cur_node; |
Michal Vasko | 588112f | 2019-12-10 14:51:53 +0100 | [diff] [blame] | 762 | new->root_type = set->root_type; |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 763 | new->context_op = set->context_op; |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 764 | new->tree = set->tree; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 765 | new->cur_mod = set->cur_mod; |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 766 | new->format = set->format; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 767 | new->prefix_data = set->prefix_data; |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 768 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 769 | } |
| 770 | |
| 771 | /** |
| 772 | * @brief Create a deep copy of a set. |
| 773 | * |
| 774 | * @param[in] set Set to copy. |
| 775 | * @return Copy of @p set. |
| 776 | */ |
| 777 | static struct lyxp_set * |
| 778 | set_copy(struct lyxp_set *set) |
| 779 | { |
| 780 | struct lyxp_set *ret; |
Michal Vasko | 1fdd8fa | 2021-01-08 09:21:45 +0100 | [diff] [blame] | 781 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 782 | |
| 783 | if (!set) { |
| 784 | return NULL; |
| 785 | } |
| 786 | |
| 787 | ret = malloc(sizeof *ret); |
| 788 | LY_CHECK_ERR_RET(!ret, LOGMEM(set->ctx), NULL); |
| 789 | set_init(ret, set); |
| 790 | |
| 791 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 792 | ret->type = set->type; |
| 793 | |
| 794 | for (i = 0; i < set->used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 795 | if ((set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) || |
| 796 | (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START)) { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 797 | uint32_t idx; |
| 798 | LY_CHECK_ERR_RET(lyxp_set_scnode_insert_node(ret, set->val.scnodes[i].scnode, set->val.scnodes[i].type, &idx), |
| 799 | lyxp_set_free(ret), NULL); |
Michal Vasko | 3f27c52 | 2020-01-06 08:37:49 +0100 | [diff] [blame] | 800 | /* coverity seems to think scnodes can be NULL */ |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 801 | if (!ret->val.scnodes) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 802 | lyxp_set_free(ret); |
| 803 | return NULL; |
| 804 | } |
Michal Vasko | ba71654 | 2019-12-16 10:01:58 +0100 | [diff] [blame] | 805 | ret->val.scnodes[idx].in_ctx = set->val.scnodes[i].in_ctx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 806 | } |
| 807 | } |
| 808 | } else if (set->type == LYXP_SET_NODE_SET) { |
| 809 | ret->type = set->type; |
| 810 | ret->val.nodes = malloc(set->used * sizeof *ret->val.nodes); |
| 811 | LY_CHECK_ERR_RET(!ret->val.nodes, LOGMEM(set->ctx); free(ret), NULL); |
| 812 | memcpy(ret->val.nodes, set->val.nodes, set->used * sizeof *ret->val.nodes); |
| 813 | |
| 814 | ret->used = ret->size = set->used; |
| 815 | ret->ctx_pos = set->ctx_pos; |
| 816 | ret->ctx_size = set->ctx_size; |
Michal Vasko | 4a04e54 | 2021-02-01 08:58:30 +0100 | [diff] [blame] | 817 | if (set->ht) { |
| 818 | ret->ht = lyht_dup(set->ht); |
| 819 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 820 | } else { |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 821 | memcpy(ret, set, sizeof *ret); |
| 822 | if (set->type == LYXP_SET_STRING) { |
| 823 | ret->val.str = strdup(set->val.str); |
| 824 | LY_CHECK_ERR_RET(!ret->val.str, LOGMEM(set->ctx); free(ret), NULL); |
| 825 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 826 | } |
| 827 | |
| 828 | return ret; |
| 829 | } |
| 830 | |
| 831 | /** |
| 832 | * @brief Fill XPath set with a string. Any current data are disposed of. |
| 833 | * |
| 834 | * @param[in] set Set to fill. |
| 835 | * @param[in] string String to fill into \p set. |
| 836 | * @param[in] str_len Length of \p string. 0 is a valid value! |
| 837 | */ |
| 838 | static void |
| 839 | set_fill_string(struct lyxp_set *set, const char *string, uint16_t str_len) |
| 840 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 841 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 842 | |
| 843 | set->type = LYXP_SET_STRING; |
| 844 | if ((str_len == 0) && (string[0] != '\0')) { |
| 845 | string = ""; |
| 846 | } |
| 847 | set->val.str = strndup(string, str_len); |
| 848 | } |
| 849 | |
| 850 | /** |
| 851 | * @brief Fill XPath set with a number. Any current data are disposed of. |
| 852 | * |
| 853 | * @param[in] set Set to fill. |
| 854 | * @param[in] number Number to fill into \p set. |
| 855 | */ |
| 856 | static void |
| 857 | set_fill_number(struct lyxp_set *set, long double number) |
| 858 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 859 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 860 | |
| 861 | set->type = LYXP_SET_NUMBER; |
| 862 | set->val.num = number; |
| 863 | } |
| 864 | |
| 865 | /** |
| 866 | * @brief Fill XPath set with a boolean. Any current data are disposed of. |
| 867 | * |
| 868 | * @param[in] set Set to fill. |
| 869 | * @param[in] boolean Boolean to fill into \p set. |
| 870 | */ |
| 871 | static void |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 872 | set_fill_boolean(struct lyxp_set *set, ly_bool boolean) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 873 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 874 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 875 | |
| 876 | set->type = LYXP_SET_BOOLEAN; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 877 | set->val.bln = boolean; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 878 | } |
| 879 | |
| 880 | /** |
| 881 | * @brief Fill XPath set with the value from another set (deep assign). |
| 882 | * Any current data are disposed of. |
| 883 | * |
| 884 | * @param[in] trg Set to fill. |
| 885 | * @param[in] src Source set to copy into \p trg. |
| 886 | */ |
| 887 | static void |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 888 | set_fill_set(struct lyxp_set *trg, const struct lyxp_set *src) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 889 | { |
| 890 | if (!trg || !src) { |
| 891 | return; |
| 892 | } |
| 893 | |
| 894 | if (trg->type == LYXP_SET_NODE_SET) { |
| 895 | free(trg->val.nodes); |
| 896 | } else if (trg->type == LYXP_SET_STRING) { |
| 897 | free(trg->val.str); |
| 898 | } |
| 899 | set_init(trg, src); |
| 900 | |
| 901 | if (src->type == LYXP_SET_SCNODE_SET) { |
| 902 | trg->type = LYXP_SET_SCNODE_SET; |
| 903 | trg->used = src->used; |
| 904 | trg->size = src->used; |
| 905 | |
| 906 | trg->val.scnodes = ly_realloc(trg->val.scnodes, trg->size * sizeof *trg->val.scnodes); |
| 907 | LY_CHECK_ERR_RET(!trg->val.scnodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), ); |
| 908 | memcpy(trg->val.scnodes, src->val.scnodes, src->used * sizeof *src->val.scnodes); |
| 909 | } else if (src->type == LYXP_SET_BOOLEAN) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 910 | set_fill_boolean(trg, src->val.bln); |
Michal Vasko | 44f3d2c | 2020-08-24 09:49:38 +0200 | [diff] [blame] | 911 | } else if (src->type == LYXP_SET_NUMBER) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 912 | set_fill_number(trg, src->val.num); |
| 913 | } else if (src->type == LYXP_SET_STRING) { |
| 914 | set_fill_string(trg, src->val.str, strlen(src->val.str)); |
| 915 | } else { |
| 916 | if (trg->type == LYXP_SET_NODE_SET) { |
| 917 | free(trg->val.nodes); |
| 918 | } else if (trg->type == LYXP_SET_STRING) { |
| 919 | free(trg->val.str); |
| 920 | } |
| 921 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 922 | assert(src->type == LYXP_SET_NODE_SET); |
| 923 | |
| 924 | trg->type = LYXP_SET_NODE_SET; |
| 925 | trg->used = src->used; |
| 926 | trg->size = src->used; |
| 927 | trg->ctx_pos = src->ctx_pos; |
| 928 | trg->ctx_size = src->ctx_size; |
| 929 | |
| 930 | trg->val.nodes = malloc(trg->used * sizeof *trg->val.nodes); |
| 931 | LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), ); |
| 932 | memcpy(trg->val.nodes, src->val.nodes, src->used * sizeof *src->val.nodes); |
| 933 | if (src->ht) { |
| 934 | trg->ht = lyht_dup(src->ht); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 935 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 936 | trg->ht = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 937 | } |
| 938 | } |
| 939 | } |
| 940 | |
| 941 | /** |
| 942 | * @brief Clear context of all schema nodes. |
| 943 | * |
| 944 | * @param[in] set Set to clear. |
| 945 | */ |
| 946 | static void |
| 947 | set_scnode_clear_ctx(struct lyxp_set *set) |
| 948 | { |
| 949 | uint32_t i; |
| 950 | |
| 951 | for (i = 0; i < set->used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 952 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
| 953 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM; |
| 954 | } else if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START) { |
| 955 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 956 | } |
| 957 | } |
| 958 | } |
| 959 | |
| 960 | /** |
| 961 | * @brief Remove a node from a set. Removing last node changes |
| 962 | * set into LYXP_SET_EMPTY. Context position aware. |
| 963 | * |
| 964 | * @param[in] set Set to use. |
| 965 | * @param[in] idx Index from @p set of the node to be removed. |
| 966 | */ |
| 967 | static void |
| 968 | set_remove_node(struct lyxp_set *set, uint32_t idx) |
| 969 | { |
| 970 | assert(set && (set->type == LYXP_SET_NODE_SET)); |
| 971 | assert(idx < set->used); |
| 972 | |
| 973 | set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
| 974 | |
| 975 | --set->used; |
| 976 | if (set->used) { |
| 977 | memmove(&set->val.nodes[idx], &set->val.nodes[idx + 1], |
| 978 | (set->used - idx) * sizeof *set->val.nodes); |
| 979 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 980 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 981 | } |
| 982 | } |
| 983 | |
| 984 | /** |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 985 | * @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] | 986 | * |
| 987 | * @param[in] set Set to use. |
| 988 | * @param[in] idx Index from @p set of the node to be removed. |
| 989 | */ |
| 990 | static void |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 991 | set_remove_node_none(struct lyxp_set *set, uint32_t idx) |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 992 | { |
| 993 | assert(set && (set->type == LYXP_SET_NODE_SET)); |
| 994 | assert(idx < set->used); |
| 995 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 996 | if (set->val.nodes[idx].type == LYXP_NODE_ELEM) { |
| 997 | set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
| 998 | } |
| 999 | set->val.nodes[idx].type = LYXP_NODE_NONE; |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1000 | } |
| 1001 | |
| 1002 | /** |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1003 | * @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] | 1004 | * set into LYXP_SET_EMPTY. Context position aware. |
| 1005 | * |
| 1006 | * @param[in] set Set to consolidate. |
| 1007 | */ |
| 1008 | static void |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1009 | set_remove_nodes_none(struct lyxp_set *set) |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1010 | { |
Michal Vasko | 1fdd8fa | 2021-01-08 09:21:45 +0100 | [diff] [blame] | 1011 | uint32_t i, orig_used, end = 0; |
| 1012 | int64_t start; |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1013 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1014 | assert(set); |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1015 | |
| 1016 | orig_used = set->used; |
| 1017 | set->used = 0; |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 1018 | for (i = 0; i < orig_used; ) { |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1019 | start = -1; |
| 1020 | do { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1021 | if ((set->val.nodes[i].type != LYXP_NODE_NONE) && (start == -1)) { |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1022 | start = i; |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1023 | } else if ((start > -1) && (set->val.nodes[i].type == LYXP_NODE_NONE)) { |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1024 | end = i; |
| 1025 | ++i; |
| 1026 | break; |
| 1027 | } |
| 1028 | |
| 1029 | ++i; |
| 1030 | if (i == orig_used) { |
| 1031 | end = i; |
| 1032 | } |
| 1033 | } while (i < orig_used); |
| 1034 | |
| 1035 | if (start > -1) { |
| 1036 | /* move the whole chunk of valid nodes together */ |
| 1037 | if (set->used != (unsigned)start) { |
| 1038 | memmove(&set->val.nodes[set->used], &set->val.nodes[start], (end - start) * sizeof *set->val.nodes); |
| 1039 | } |
| 1040 | set->used += end - start; |
| 1041 | } |
| 1042 | } |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1043 | } |
| 1044 | |
| 1045 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1046 | * @brief Check for duplicates in a node set. |
| 1047 | * |
| 1048 | * @param[in] set Set to check. |
| 1049 | * @param[in] node Node to look for in @p set. |
| 1050 | * @param[in] node_type Type of @p node. |
| 1051 | * @param[in] skip_idx Index from @p set to skip. |
| 1052 | * @return LY_ERR |
| 1053 | */ |
| 1054 | static LY_ERR |
| 1055 | set_dup_node_check(const struct lyxp_set *set, const struct lyd_node *node, enum lyxp_node_type node_type, int skip_idx) |
| 1056 | { |
| 1057 | uint32_t i; |
| 1058 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1059 | if (set->ht && node) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1060 | return set_dup_node_hash_check(set, (struct lyd_node *)node, node_type, skip_idx); |
| 1061 | } |
| 1062 | |
| 1063 | for (i = 0; i < set->used; ++i) { |
| 1064 | if ((skip_idx > -1) && (i == (unsigned)skip_idx)) { |
| 1065 | continue; |
| 1066 | } |
| 1067 | |
| 1068 | if ((set->val.nodes[i].node == node) && (set->val.nodes[i].type == node_type)) { |
| 1069 | return LY_EEXIST; |
| 1070 | } |
| 1071 | } |
| 1072 | |
| 1073 | return LY_SUCCESS; |
| 1074 | } |
| 1075 | |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 1076 | ly_bool |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1077 | lyxp_set_scnode_contains(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, int skip_idx, |
| 1078 | uint32_t *index_p) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1079 | { |
| 1080 | uint32_t i; |
| 1081 | |
| 1082 | for (i = 0; i < set->used; ++i) { |
| 1083 | if ((skip_idx > -1) && (i == (unsigned)skip_idx)) { |
| 1084 | continue; |
| 1085 | } |
| 1086 | |
| 1087 | 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] | 1088 | if (index_p) { |
| 1089 | *index_p = i; |
| 1090 | } |
| 1091 | return 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1092 | } |
| 1093 | } |
| 1094 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1095 | return 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1096 | } |
| 1097 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 1098 | void |
| 1099 | lyxp_set_scnode_merge(struct lyxp_set *set1, struct lyxp_set *set2) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1100 | { |
| 1101 | uint32_t orig_used, i, j; |
| 1102 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1103 | 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] | 1104 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1105 | if (!set2->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1106 | return; |
| 1107 | } |
| 1108 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1109 | if (!set1->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1110 | memcpy(set1, set2, sizeof *set1); |
| 1111 | return; |
| 1112 | } |
| 1113 | |
| 1114 | if (set1->used + set2->used > set1->size) { |
| 1115 | set1->size = set1->used + set2->used; |
| 1116 | set1->val.scnodes = ly_realloc(set1->val.scnodes, set1->size * sizeof *set1->val.scnodes); |
| 1117 | LY_CHECK_ERR_RET(!set1->val.scnodes, LOGMEM(set1->ctx), ); |
| 1118 | } |
| 1119 | |
| 1120 | orig_used = set1->used; |
| 1121 | |
| 1122 | for (i = 0; i < set2->used; ++i) { |
| 1123 | for (j = 0; j < orig_used; ++j) { |
| 1124 | /* detect duplicities */ |
| 1125 | if (set1->val.scnodes[j].scnode == set2->val.scnodes[i].scnode) { |
| 1126 | break; |
| 1127 | } |
| 1128 | } |
| 1129 | |
Michal Vasko | 0587bce | 2020-12-10 12:19:21 +0100 | [diff] [blame] | 1130 | if (j < orig_used) { |
| 1131 | /* node is there, but update its status if needed */ |
| 1132 | if (set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_START_USED) { |
| 1133 | set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx; |
| 1134 | } |
| 1135 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1136 | memcpy(&set1->val.scnodes[set1->used], &set2->val.scnodes[i], sizeof *set2->val.scnodes); |
| 1137 | ++set1->used; |
| 1138 | } |
| 1139 | } |
| 1140 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1141 | lyxp_set_free_content(set2); |
| 1142 | set2->type = LYXP_SET_SCNODE_SET; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1143 | } |
| 1144 | |
| 1145 | /** |
| 1146 | * @brief Insert a node into a set. Context position aware. |
| 1147 | * |
| 1148 | * @param[in] set Set to use. |
| 1149 | * @param[in] node Node to insert to @p set. |
| 1150 | * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting. |
| 1151 | * @param[in] node_type Node type of @p node. |
| 1152 | * @param[in] idx Index in @p set to insert into. |
| 1153 | */ |
| 1154 | static void |
| 1155 | set_insert_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx) |
| 1156 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1157 | assert(set && (set->type == LYXP_SET_NODE_SET)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1158 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1159 | if (!set->size) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1160 | /* first item */ |
| 1161 | if (idx) { |
| 1162 | /* no real harm done, but it is a bug */ |
| 1163 | LOGINT(set->ctx); |
| 1164 | idx = 0; |
| 1165 | } |
| 1166 | set->val.nodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.nodes); |
| 1167 | LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), ); |
| 1168 | set->type = LYXP_SET_NODE_SET; |
| 1169 | set->used = 0; |
| 1170 | set->size = LYXP_SET_SIZE_START; |
| 1171 | set->ctx_pos = 1; |
| 1172 | set->ctx_size = 1; |
| 1173 | set->ht = NULL; |
| 1174 | } else { |
| 1175 | /* not an empty set */ |
| 1176 | if (set->used == set->size) { |
| 1177 | |
| 1178 | /* set is full */ |
| 1179 | set->val.nodes = ly_realloc(set->val.nodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.nodes); |
| 1180 | LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), ); |
| 1181 | set->size += LYXP_SET_SIZE_STEP; |
| 1182 | } |
| 1183 | |
| 1184 | if (idx > set->used) { |
| 1185 | LOGINT(set->ctx); |
| 1186 | idx = set->used; |
| 1187 | } |
| 1188 | |
| 1189 | /* make space for the new node */ |
| 1190 | if (idx < set->used) { |
| 1191 | memmove(&set->val.nodes[idx + 1], &set->val.nodes[idx], (set->used - idx) * sizeof *set->val.nodes); |
| 1192 | } |
| 1193 | } |
| 1194 | |
| 1195 | /* finally assign the value */ |
| 1196 | set->val.nodes[idx].node = (struct lyd_node *)node; |
| 1197 | set->val.nodes[idx].type = node_type; |
| 1198 | set->val.nodes[idx].pos = pos; |
| 1199 | ++set->used; |
| 1200 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1201 | if (set->val.nodes[idx].type == LYXP_NODE_ELEM) { |
| 1202 | set_insert_node_hash(set, (struct lyd_node *)node, node_type); |
| 1203 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1204 | } |
| 1205 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1206 | LY_ERR |
| 1207 | 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] | 1208 | { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1209 | uint32_t index; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1210 | |
| 1211 | assert(set->type == LYXP_SET_SCNODE_SET); |
| 1212 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1213 | if (lyxp_set_scnode_contains(set, node, node_type, -1, &index)) { |
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 | } else { |
| 1216 | if (set->used == set->size) { |
| 1217 | 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] | 1218 | LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), LY_EMEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1219 | set->size += LYXP_SET_SIZE_STEP; |
| 1220 | } |
| 1221 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1222 | index = set->used; |
| 1223 | set->val.scnodes[index].scnode = (struct lysc_node *)node; |
| 1224 | set->val.scnodes[index].type = node_type; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1225 | set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1226 | ++set->used; |
| 1227 | } |
| 1228 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 1229 | if (index_p) { |
| 1230 | *index_p = index; |
| 1231 | } |
| 1232 | |
| 1233 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1234 | } |
| 1235 | |
| 1236 | /** |
| 1237 | * @brief Replace a node in a set with another. Context position aware. |
| 1238 | * |
| 1239 | * @param[in] set Set to use. |
| 1240 | * @param[in] node Node to insert to @p set. |
| 1241 | * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting. |
| 1242 | * @param[in] node_type Node type of @p node. |
| 1243 | * @param[in] idx Index in @p set of the node to replace. |
| 1244 | */ |
| 1245 | static void |
| 1246 | set_replace_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx) |
| 1247 | { |
| 1248 | assert(set && (idx < set->used)); |
| 1249 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1250 | if (set->val.nodes[idx].type == LYXP_NODE_ELEM) { |
| 1251 | set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
| 1252 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1253 | set->val.nodes[idx].node = (struct lyd_node *)node; |
| 1254 | set->val.nodes[idx].type = node_type; |
| 1255 | set->val.nodes[idx].pos = pos; |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1256 | if (set->val.nodes[idx].type == LYXP_NODE_ELEM) { |
| 1257 | set_insert_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
| 1258 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1259 | } |
| 1260 | |
| 1261 | /** |
| 1262 | * @brief Set all nodes with ctx 1 to a new unique context value. |
| 1263 | * |
| 1264 | * @param[in] set Set to modify. |
| 1265 | * @return New context value. |
| 1266 | */ |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 1267 | static int32_t |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1268 | set_scnode_new_in_ctx(struct lyxp_set *set) |
| 1269 | { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 1270 | uint32_t i; |
| 1271 | int32_t ret_ctx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1272 | |
| 1273 | assert(set->type == LYXP_SET_SCNODE_SET); |
| 1274 | |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1275 | ret_ctx = LYXP_SET_SCNODE_ATOM_PRED_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1276 | retry: |
| 1277 | for (i = 0; i < set->used; ++i) { |
| 1278 | if (set->val.scnodes[i].in_ctx >= ret_ctx) { |
| 1279 | ret_ctx = set->val.scnodes[i].in_ctx + 1; |
| 1280 | goto retry; |
| 1281 | } |
| 1282 | } |
| 1283 | for (i = 0; i < set->used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1284 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1285 | set->val.scnodes[i].in_ctx = ret_ctx; |
| 1286 | } |
| 1287 | } |
| 1288 | |
| 1289 | return ret_ctx; |
| 1290 | } |
| 1291 | |
| 1292 | /** |
| 1293 | * @brief Get unique @p node position in the data. |
| 1294 | * |
| 1295 | * @param[in] node Node to find. |
| 1296 | * @param[in] node_type Node type of @p node. |
| 1297 | * @param[in] root Root node. |
| 1298 | * @param[in] root_type Type of the XPath @p root node. |
| 1299 | * @param[in] prev Node that we think is before @p node in DFS from @p root. Can optionally |
| 1300 | * be used to increase efficiency and start the DFS from this node. |
| 1301 | * @param[in] prev_pos Node @p prev position. Optional, but must be set if @p prev is set. |
| 1302 | * @return Node position. |
| 1303 | */ |
| 1304 | static uint32_t |
| 1305 | 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] | 1306 | 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] | 1307 | { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1308 | const struct lyd_node *elem = NULL, *top_sibling; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1309 | uint32_t pos = 1; |
Michal Vasko | fb6c9dd | 2020-11-18 18:18:47 +0100 | [diff] [blame] | 1310 | ly_bool found = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1311 | |
| 1312 | assert(prev && prev_pos && !root->prev->next); |
| 1313 | |
| 1314 | if ((node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ROOT_CONFIG)) { |
| 1315 | return 0; |
| 1316 | } |
| 1317 | |
| 1318 | if (*prev) { |
| 1319 | /* start from the previous element instead from the root */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1320 | pos = *prev_pos; |
Michal Vasko | fb6c9dd | 2020-11-18 18:18:47 +0100 | [diff] [blame] | 1321 | 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] | 1322 | goto dfs_search; |
| 1323 | } |
| 1324 | |
Michal Vasko | fb6c9dd | 2020-11-18 18:18:47 +0100 | [diff] [blame] | 1325 | LY_LIST_FOR(root, top_sibling) { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1326 | LYD_TREE_DFS_BEGIN(top_sibling, elem) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1327 | dfs_search: |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1328 | if (*prev && !elem) { |
| 1329 | /* resume previous DFS */ |
| 1330 | elem = LYD_TREE_DFS_next = (struct lyd_node *)*prev; |
| 1331 | LYD_TREE_DFS_continue = 0; |
| 1332 | } |
| 1333 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1334 | 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] | 1335 | /* skip */ |
| 1336 | LYD_TREE_DFS_continue = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1337 | } else { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1338 | if (elem == node) { |
Michal Vasko | fb6c9dd | 2020-11-18 18:18:47 +0100 | [diff] [blame] | 1339 | found = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1340 | break; |
| 1341 | } |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1342 | ++pos; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1343 | } |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1344 | |
| 1345 | LYD_TREE_DFS_END(top_sibling, elem); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1346 | } |
| 1347 | |
| 1348 | /* node found */ |
Michal Vasko | fb6c9dd | 2020-11-18 18:18:47 +0100 | [diff] [blame] | 1349 | if (found) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1350 | break; |
| 1351 | } |
| 1352 | } |
| 1353 | |
Michal Vasko | fb6c9dd | 2020-11-18 18:18:47 +0100 | [diff] [blame] | 1354 | if (!found) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1355 | if (!(*prev)) { |
| 1356 | /* we went from root and failed to find it, cannot be */ |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1357 | LOGINT(LYD_CTX(node)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1358 | return 0; |
| 1359 | } else { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1360 | /* start the search again from the beginning */ |
| 1361 | *prev = root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1362 | |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1363 | top_sibling = root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1364 | pos = 1; |
| 1365 | goto dfs_search; |
| 1366 | } |
| 1367 | } |
| 1368 | |
| 1369 | /* remember the last found node for next time */ |
| 1370 | *prev = node; |
| 1371 | *prev_pos = pos; |
| 1372 | |
| 1373 | return pos; |
| 1374 | } |
| 1375 | |
| 1376 | /** |
| 1377 | * @brief Assign (fill) missing node positions. |
| 1378 | * |
| 1379 | * @param[in] set Set to fill positions in. |
| 1380 | * @param[in] root Context root node. |
| 1381 | * @param[in] root_type Context root type. |
| 1382 | * @return LY_ERR |
| 1383 | */ |
| 1384 | static LY_ERR |
| 1385 | set_assign_pos(struct lyxp_set *set, const struct lyd_node *root, enum lyxp_node_type root_type) |
| 1386 | { |
| 1387 | const struct lyd_node *prev = NULL, *tmp_node; |
| 1388 | uint32_t i, tmp_pos = 0; |
| 1389 | |
| 1390 | for (i = 0; i < set->used; ++i) { |
| 1391 | if (!set->val.nodes[i].pos) { |
| 1392 | tmp_node = NULL; |
| 1393 | switch (set->val.nodes[i].type) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1394 | case LYXP_NODE_META: |
| 1395 | tmp_node = set->val.meta[i].meta->parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1396 | if (!tmp_node) { |
| 1397 | LOGINT_RET(root->schema->module->ctx); |
| 1398 | } |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 1399 | /* fall through */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1400 | case LYXP_NODE_ELEM: |
| 1401 | case LYXP_NODE_TEXT: |
| 1402 | if (!tmp_node) { |
| 1403 | tmp_node = set->val.nodes[i].node; |
| 1404 | } |
| 1405 | set->val.nodes[i].pos = get_node_pos(tmp_node, set->val.nodes[i].type, root, root_type, &prev, &tmp_pos); |
| 1406 | break; |
| 1407 | default: |
| 1408 | /* all roots have position 0 */ |
| 1409 | break; |
| 1410 | } |
| 1411 | } |
| 1412 | } |
| 1413 | |
| 1414 | return LY_SUCCESS; |
| 1415 | } |
| 1416 | |
| 1417 | /** |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1418 | * @brief Get unique @p meta position in the parent metadata. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1419 | * |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1420 | * @param[in] meta Metadata to use. |
| 1421 | * @return Metadata position. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1422 | */ |
| 1423 | static uint16_t |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1424 | get_meta_pos(struct lyd_meta *meta) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1425 | { |
| 1426 | uint16_t pos = 0; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1427 | struct lyd_meta *meta2; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1428 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1429 | for (meta2 = meta->parent->meta; meta2 && (meta2 != meta); meta2 = meta2->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1430 | ++pos; |
| 1431 | } |
| 1432 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1433 | assert(meta2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1434 | return pos; |
| 1435 | } |
| 1436 | |
| 1437 | /** |
| 1438 | * @brief Compare 2 nodes in respect to XPath document order. |
| 1439 | * |
| 1440 | * @param[in] item1 1st node. |
| 1441 | * @param[in] item2 2nd node. |
| 1442 | * @return If 1st > 2nd returns 1, 1st == 2nd returns 0, and 1st < 2nd returns -1. |
| 1443 | */ |
| 1444 | static int |
| 1445 | set_sort_compare(struct lyxp_set_node *item1, struct lyxp_set_node *item2) |
| 1446 | { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1447 | uint32_t meta_pos1 = 0, meta_pos2 = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1448 | |
| 1449 | if (item1->pos < item2->pos) { |
| 1450 | return -1; |
| 1451 | } |
| 1452 | |
| 1453 | if (item1->pos > item2->pos) { |
| 1454 | return 1; |
| 1455 | } |
| 1456 | |
| 1457 | /* node positions are equal, the fun case */ |
| 1458 | |
| 1459 | /* 1st ELEM - == - 2nd TEXT, 1st TEXT - == - 2nd ELEM */ |
| 1460 | /* special case since text nodes are actually saved as their parents */ |
| 1461 | if ((item1->node == item2->node) && (item1->type != item2->type)) { |
| 1462 | if (item1->type == LYXP_NODE_ELEM) { |
| 1463 | assert(item2->type == LYXP_NODE_TEXT); |
| 1464 | return -1; |
| 1465 | } else { |
| 1466 | assert((item1->type == LYXP_NODE_TEXT) && (item2->type == LYXP_NODE_ELEM)); |
| 1467 | return 1; |
| 1468 | } |
| 1469 | } |
| 1470 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1471 | /* we need meta positions now */ |
| 1472 | if (item1->type == LYXP_NODE_META) { |
| 1473 | meta_pos1 = get_meta_pos((struct lyd_meta *)item1->node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1474 | } |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1475 | if (item2->type == LYXP_NODE_META) { |
| 1476 | meta_pos2 = get_meta_pos((struct lyd_meta *)item2->node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1477 | } |
| 1478 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1479 | /* 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] | 1480 | /* check for duplicates */ |
| 1481 | if (item1->node == item2->node) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1482 | 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] | 1483 | return 0; |
| 1484 | } |
| 1485 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1486 | /* 1st ELEM - 2nd TEXT, 1st ELEM - any pos - 2nd META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1487 | /* elem is always first, 2nd node is after it */ |
| 1488 | if (item1->type == LYXP_NODE_ELEM) { |
| 1489 | assert(item2->type != LYXP_NODE_ELEM); |
| 1490 | return -1; |
| 1491 | } |
| 1492 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1493 | /* 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] | 1494 | /* 2nd is before 1st */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1495 | if (((item1->type == LYXP_NODE_TEXT) && |
| 1496 | ((item2->type == LYXP_NODE_ELEM) || (item2->type == LYXP_NODE_META))) || |
| 1497 | ((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_ELEM)) || |
| 1498 | (((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_META)) && |
| 1499 | (meta_pos1 > meta_pos2))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1500 | return 1; |
| 1501 | } |
| 1502 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1503 | /* 1st META - any pos - 2nd TEXT, 1st META <pos< - 2nd META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1504 | /* 2nd is after 1st */ |
| 1505 | return -1; |
| 1506 | } |
| 1507 | |
| 1508 | /** |
| 1509 | * @brief Set cast for comparisons. |
| 1510 | * |
| 1511 | * @param[in] trg Target set to cast source into. |
| 1512 | * @param[in] src Source set. |
| 1513 | * @param[in] type Target set type. |
| 1514 | * @param[in] src_idx Source set node index. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1515 | * @return LY_ERR |
| 1516 | */ |
| 1517 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1518 | 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] | 1519 | { |
| 1520 | assert(src->type == LYXP_SET_NODE_SET); |
| 1521 | |
| 1522 | set_init(trg, src); |
| 1523 | |
| 1524 | /* insert node into target set */ |
| 1525 | set_insert_node(trg, src->val.nodes[src_idx].node, src->val.nodes[src_idx].pos, src->val.nodes[src_idx].type, 0); |
| 1526 | |
| 1527 | /* cast target set appropriately */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1528 | return lyxp_set_cast(trg, type); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1529 | } |
| 1530 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1531 | /** |
| 1532 | * @brief Set content canonization for comparisons. |
| 1533 | * |
| 1534 | * @param[in] trg Target set to put the canononized source into. |
| 1535 | * @param[in] src Source set. |
| 1536 | * @param[in] xp_node Source XPath node/meta to use for canonization. |
| 1537 | * @return LY_ERR |
| 1538 | */ |
| 1539 | static LY_ERR |
| 1540 | set_comp_canonize(struct lyxp_set *trg, const struct lyxp_set *src, const struct lyxp_set_node *xp_node) |
| 1541 | { |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 1542 | const struct lysc_type *type = NULL; |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1543 | struct lyd_value val; |
| 1544 | struct ly_err_item *err = NULL; |
| 1545 | char *str, *ptr; |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1546 | LY_ERR rc; |
| 1547 | |
| 1548 | /* is there anything to canonize even? */ |
| 1549 | if ((src->type == LYXP_SET_NUMBER) || (src->type == LYXP_SET_STRING)) { |
| 1550 | /* do we have a type to use for canonization? */ |
| 1551 | if ((xp_node->type == LYXP_NODE_ELEM) && (xp_node->node->schema->nodetype & LYD_NODE_TERM)) { |
| 1552 | type = ((struct lyd_node_term *)xp_node->node)->value.realtype; |
| 1553 | } else if (xp_node->type == LYXP_NODE_META) { |
| 1554 | type = ((struct lyd_meta *)xp_node->node)->value.realtype; |
| 1555 | } |
| 1556 | } |
| 1557 | if (!type) { |
| 1558 | goto fill; |
| 1559 | } |
| 1560 | |
| 1561 | if (src->type == LYXP_SET_NUMBER) { |
| 1562 | /* canonize number */ |
| 1563 | if (asprintf(&str, "%Lf", src->val.num) == -1) { |
| 1564 | LOGMEM(src->ctx); |
| 1565 | return LY_EMEM; |
| 1566 | } |
| 1567 | } else { |
| 1568 | /* canonize string */ |
| 1569 | str = strdup(src->val.str); |
| 1570 | } |
| 1571 | |
| 1572 | /* ignore errors, the value may not satisfy schema constraints */ |
Radek Krejci | 0b01330 | 2021-03-29 15:22:32 +0200 | [diff] [blame] | 1573 | rc = type->plugin->store(src->ctx, type, str, strlen(str), LYPLG_TYPE_STORE_DYNAMIC, src->format, src->prefix_data, |
Michal Vasko | 405cc9e | 2020-12-01 12:01:27 +0100 | [diff] [blame] | 1574 | LYD_HINT_DATA, xp_node->node->schema, &val, NULL, &err); |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1575 | ly_err_free(err); |
| 1576 | if (rc) { |
| 1577 | /* invalid value */ |
Radek IÅ¡a | 4846022 | 2021-03-02 15:18:32 +0100 | [diff] [blame] | 1578 | /* function store automaticaly dealloc value when fail */ |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1579 | goto fill; |
| 1580 | } |
| 1581 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1582 | /* use the canonized value */ |
| 1583 | set_init(trg, src); |
| 1584 | trg->type = src->type; |
| 1585 | if (src->type == LYXP_SET_NUMBER) { |
Michal Vasko | 0fdb0d9 | 2020-11-06 17:25:50 +0100 | [diff] [blame] | 1586 | trg->val.num = strtold(val.canonical, &ptr); |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1587 | LY_CHECK_ERR_RET(ptr[0], LOGINT(src->ctx), LY_EINT); |
| 1588 | } else { |
Michal Vasko | 0fdb0d9 | 2020-11-06 17:25:50 +0100 | [diff] [blame] | 1589 | trg->val.str = strdup(val.canonical); |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1590 | } |
| 1591 | type->plugin->free(src->ctx, &val); |
| 1592 | return LY_SUCCESS; |
| 1593 | |
| 1594 | fill: |
| 1595 | /* no canonization needed/possible */ |
| 1596 | set_fill_set(trg, src); |
| 1597 | return LY_SUCCESS; |
| 1598 | } |
| 1599 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1600 | #ifndef NDEBUG |
| 1601 | |
| 1602 | /** |
| 1603 | * @brief Bubble sort @p set into XPath document order. |
| 1604 | * Context position aware. Unused in the 'Release' build target. |
| 1605 | * |
| 1606 | * @param[in] set Set to sort. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1607 | * @return How many times the whole set was traversed - 1 (if set was sorted, returns 0). |
| 1608 | */ |
| 1609 | static int |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1610 | set_sort(struct lyxp_set *set) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1611 | { |
| 1612 | uint32_t i, j; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 1613 | int ret = 0, cmp; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 1614 | ly_bool inverted, change; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1615 | const struct lyd_node *root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1616 | struct lyxp_set_node item; |
| 1617 | struct lyxp_set_hash_node hnode; |
| 1618 | uint64_t hash; |
| 1619 | |
Michal Vasko | 3cf8fbf | 2020-05-27 15:21:21 +0200 | [diff] [blame] | 1620 | if ((set->type != LYXP_SET_NODE_SET) || (set->used < 2)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1621 | return 0; |
| 1622 | } |
| 1623 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1624 | /* find first top-level node to be used as anchor for positions */ |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 1625 | for (root = set->cur_node; root->parent; root = lyd_parent(root)) {} |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 1626 | for ( ; root->prev->next; root = root->prev) {} |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1627 | |
| 1628 | /* fill positions */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1629 | if (set_assign_pos(set, root, set->root_type)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1630 | return -1; |
| 1631 | } |
| 1632 | |
| 1633 | LOGDBG(LY_LDGXPATH, "SORT BEGIN"); |
| 1634 | print_set_debug(set); |
| 1635 | |
| 1636 | for (i = 0; i < set->used; ++i) { |
| 1637 | inverted = 0; |
| 1638 | change = 0; |
| 1639 | |
| 1640 | for (j = 1; j < set->used - i; ++j) { |
| 1641 | /* compare node positions */ |
| 1642 | if (inverted) { |
| 1643 | cmp = set_sort_compare(&set->val.nodes[j], &set->val.nodes[j - 1]); |
| 1644 | } else { |
| 1645 | cmp = set_sort_compare(&set->val.nodes[j - 1], &set->val.nodes[j]); |
| 1646 | } |
| 1647 | |
| 1648 | /* swap if needed */ |
| 1649 | if ((inverted && (cmp < 0)) || (!inverted && (cmp > 0))) { |
| 1650 | change = 1; |
| 1651 | |
| 1652 | item = set->val.nodes[j - 1]; |
| 1653 | set->val.nodes[j - 1] = set->val.nodes[j]; |
| 1654 | set->val.nodes[j] = item; |
| 1655 | } else { |
| 1656 | /* whether node_pos1 should be smaller than node_pos2 or the other way around */ |
| 1657 | inverted = !inverted; |
| 1658 | } |
| 1659 | } |
| 1660 | |
| 1661 | ++ret; |
| 1662 | |
| 1663 | if (!change) { |
| 1664 | break; |
| 1665 | } |
| 1666 | } |
| 1667 | |
| 1668 | LOGDBG(LY_LDGXPATH, "SORT END %d", ret); |
| 1669 | print_set_debug(set); |
| 1670 | |
| 1671 | /* check node hashes */ |
| 1672 | if (set->used >= LYD_HT_MIN_ITEMS) { |
| 1673 | assert(set->ht); |
| 1674 | for (i = 0; i < set->used; ++i) { |
| 1675 | hnode.node = set->val.nodes[i].node; |
| 1676 | hnode.type = set->val.nodes[i].type; |
| 1677 | |
| 1678 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 1679 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 1680 | hash = dict_hash_multi(hash, NULL, 0); |
| 1681 | |
| 1682 | assert(!lyht_find(set->ht, &hnode, hash, NULL)); |
| 1683 | } |
| 1684 | } |
| 1685 | |
| 1686 | return ret - 1; |
| 1687 | } |
| 1688 | |
| 1689 | /** |
| 1690 | * @brief Remove duplicate entries in a sorted node set. |
| 1691 | * |
| 1692 | * @param[in] set Sorted set to check. |
| 1693 | * @return LY_ERR (LY_EEXIST if some duplicates are found) |
| 1694 | */ |
| 1695 | static LY_ERR |
| 1696 | set_sorted_dup_node_clean(struct lyxp_set *set) |
| 1697 | { |
| 1698 | uint32_t i = 0; |
| 1699 | LY_ERR ret = LY_SUCCESS; |
| 1700 | |
| 1701 | if (set->used > 1) { |
| 1702 | while (i < set->used - 1) { |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1703 | if ((set->val.nodes[i].node == set->val.nodes[i + 1].node) && |
| 1704 | (set->val.nodes[i].type == set->val.nodes[i + 1].type)) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1705 | set_remove_node_none(set, i + 1); |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1706 | ret = LY_EEXIST; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1707 | } |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1708 | ++i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1709 | } |
| 1710 | } |
| 1711 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1712 | set_remove_nodes_none(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1713 | return ret; |
| 1714 | } |
| 1715 | |
| 1716 | #endif |
| 1717 | |
| 1718 | /** |
| 1719 | * @brief Merge 2 sorted sets into one. |
| 1720 | * |
| 1721 | * @param[in,out] trg Set to merge into. Duplicates are removed. |
| 1722 | * @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] | 1723 | * @return LY_ERR |
| 1724 | */ |
| 1725 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1726 | set_sorted_merge(struct lyxp_set *trg, struct lyxp_set *src) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1727 | { |
| 1728 | uint32_t i, j, k, count, dup_count; |
| 1729 | int cmp; |
| 1730 | const struct lyd_node *root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1731 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1732 | 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] | 1733 | return LY_EINVAL; |
| 1734 | } |
| 1735 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1736 | if (!src->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1737 | return LY_SUCCESS; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1738 | } else if (!trg->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1739 | set_fill_set(trg, src); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1740 | lyxp_set_free_content(src); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1741 | return LY_SUCCESS; |
| 1742 | } |
| 1743 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1744 | /* find first top-level node to be used as anchor for positions */ |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 1745 | for (root = trg->cur_node; root->parent; root = lyd_parent(root)) {} |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 1746 | for ( ; root->prev->next; root = root->prev) {} |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1747 | |
| 1748 | /* fill positions */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1749 | 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] | 1750 | return LY_EINT; |
| 1751 | } |
| 1752 | |
| 1753 | #ifndef NDEBUG |
| 1754 | LOGDBG(LY_LDGXPATH, "MERGE target"); |
| 1755 | print_set_debug(trg); |
| 1756 | LOGDBG(LY_LDGXPATH, "MERGE source"); |
| 1757 | print_set_debug(src); |
| 1758 | #endif |
| 1759 | |
| 1760 | /* make memory for the merge (duplicates are not detected yet, so space |
| 1761 | * will likely be wasted on them, too bad) */ |
| 1762 | if (trg->size - trg->used < src->used) { |
| 1763 | trg->size = trg->used + src->used; |
| 1764 | |
| 1765 | trg->val.nodes = ly_realloc(trg->val.nodes, trg->size * sizeof *trg->val.nodes); |
| 1766 | LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx), LY_EMEM); |
| 1767 | } |
| 1768 | |
| 1769 | i = 0; |
| 1770 | j = 0; |
| 1771 | count = 0; |
| 1772 | dup_count = 0; |
| 1773 | do { |
| 1774 | cmp = set_sort_compare(&src->val.nodes[i], &trg->val.nodes[j]); |
| 1775 | if (!cmp) { |
| 1776 | if (!count) { |
| 1777 | /* duplicate, just skip it */ |
| 1778 | ++i; |
| 1779 | ++j; |
| 1780 | } else { |
| 1781 | /* we are copying something already, so let's copy the duplicate too, |
| 1782 | * we are hoping that afterwards there are some more nodes to |
| 1783 | * copy and this way we can copy them all together */ |
| 1784 | ++count; |
| 1785 | ++dup_count; |
| 1786 | ++i; |
| 1787 | ++j; |
| 1788 | } |
| 1789 | } else if (cmp < 0) { |
| 1790 | /* inserting src node into trg, just remember it for now */ |
| 1791 | ++count; |
| 1792 | ++i; |
| 1793 | |
| 1794 | /* insert the hash now */ |
| 1795 | set_insert_node_hash(trg, src->val.nodes[i - 1].node, src->val.nodes[i - 1].type); |
| 1796 | } else if (count) { |
| 1797 | copy_nodes: |
| 1798 | /* time to actually copy the nodes, we have found the largest block of nodes */ |
| 1799 | memmove(&trg->val.nodes[j + (count - dup_count)], |
| 1800 | &trg->val.nodes[j], |
| 1801 | (trg->used - j) * sizeof *trg->val.nodes); |
| 1802 | memcpy(&trg->val.nodes[j - dup_count], &src->val.nodes[i - count], count * sizeof *src->val.nodes); |
| 1803 | |
| 1804 | trg->used += count - dup_count; |
| 1805 | /* do not change i, except the copying above, we are basically doing exactly what is in the else branch below */ |
| 1806 | j += count - dup_count; |
| 1807 | |
| 1808 | count = 0; |
| 1809 | dup_count = 0; |
| 1810 | } else { |
| 1811 | ++j; |
| 1812 | } |
| 1813 | } while ((i < src->used) && (j < trg->used)); |
| 1814 | |
| 1815 | if ((i < src->used) || count) { |
| 1816 | /* insert all the hashes first */ |
| 1817 | for (k = i; k < src->used; ++k) { |
| 1818 | set_insert_node_hash(trg, src->val.nodes[k].node, src->val.nodes[k].type); |
| 1819 | } |
| 1820 | |
| 1821 | /* loop ended, but we need to copy something at trg end */ |
| 1822 | count += src->used - i; |
| 1823 | i = src->used; |
| 1824 | goto copy_nodes; |
| 1825 | } |
| 1826 | |
| 1827 | /* we are inserting hashes before the actual node insert, which causes |
| 1828 | * situations when there were initially not enough items for a hash table, |
| 1829 | * but even after some were inserted, hash table was not created (during |
| 1830 | * insertion the number of items is not updated yet) */ |
| 1831 | if (!trg->ht && (trg->used >= LYD_HT_MIN_ITEMS)) { |
| 1832 | set_insert_node_hash(trg, NULL, 0); |
| 1833 | } |
| 1834 | |
| 1835 | #ifndef NDEBUG |
| 1836 | LOGDBG(LY_LDGXPATH, "MERGE result"); |
| 1837 | print_set_debug(trg); |
| 1838 | #endif |
| 1839 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1840 | lyxp_set_free_content(src); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1841 | return LY_SUCCESS; |
| 1842 | } |
| 1843 | |
| 1844 | /* |
| 1845 | * (re)parse functions |
| 1846 | * |
| 1847 | * Parse functions parse the expression into |
| 1848 | * tokens (syntactic analysis). |
| 1849 | * |
| 1850 | * Reparse functions perform semantic analysis |
| 1851 | * (do not save the result, just a check) of |
| 1852 | * the expression and fill repeat indices. |
| 1853 | */ |
| 1854 | |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1855 | LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1856 | 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] | 1857 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1858 | if (exp->used == tok_idx) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1859 | if (ctx) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1860 | LOGVAL(ctx, LY_VCODE_XP_EOF); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1861 | } |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1862 | return LY_EINCOMPLETE; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1863 | } |
| 1864 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1865 | if (want_tok && (exp->tokens[tok_idx] != want_tok)) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1866 | if (ctx) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1867 | LOGVAL(ctx, LY_VCODE_XP_INTOK2, lyxp_print_token(exp->tokens[tok_idx]), |
Michal Vasko | 0b468e6 | 2020-10-19 09:33:04 +0200 | [diff] [blame] | 1868 | &exp->expr[exp->tok_pos[tok_idx]], lyxp_print_token(want_tok)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1869 | } |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1870 | return LY_ENOT; |
| 1871 | } |
| 1872 | |
| 1873 | return LY_SUCCESS; |
| 1874 | } |
| 1875 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1876 | LY_ERR |
| 1877 | lyxp_next_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok) |
| 1878 | { |
| 1879 | LY_CHECK_RET(lyxp_check_token(ctx, exp, *tok_idx, want_tok)); |
| 1880 | |
| 1881 | /* skip the token */ |
| 1882 | ++(*tok_idx); |
| 1883 | |
| 1884 | return LY_SUCCESS; |
| 1885 | } |
| 1886 | |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1887 | /* just like lyxp_check_token() but tests for 2 tokens */ |
| 1888 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 1889 | 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] | 1890 | enum lyxp_token want_tok2) |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1891 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1892 | if (exp->used == tok_idx) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1893 | if (ctx) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1894 | LOGVAL(ctx, LY_VCODE_XP_EOF); |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1895 | } |
| 1896 | return LY_EINCOMPLETE; |
| 1897 | } |
| 1898 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1899 | 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] | 1900 | if (ctx) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 1901 | LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[tok_idx]), |
Michal Vasko | 0b468e6 | 2020-10-19 09:33:04 +0200 | [diff] [blame] | 1902 | &exp->expr[exp->tok_pos[tok_idx]]); |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1903 | } |
| 1904 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1905 | } |
| 1906 | |
| 1907 | return LY_SUCCESS; |
| 1908 | } |
| 1909 | |
| 1910 | /** |
| 1911 | * @brief Stack operation push on the repeat array. |
| 1912 | * |
| 1913 | * @param[in] exp Expression to use. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1914 | * @param[in] tok_idx Position in the expresion \p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1915 | * @param[in] repeat_op_idx Index from \p exp of the operator token. This value is pushed. |
| 1916 | */ |
| 1917 | static void |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1918 | 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] | 1919 | { |
| 1920 | uint16_t i; |
| 1921 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1922 | if (exp->repeat[tok_idx]) { |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 1923 | for (i = 0; exp->repeat[tok_idx][i]; ++i) {} |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1924 | exp->repeat[tok_idx] = realloc(exp->repeat[tok_idx], (i + 2) * sizeof *exp->repeat[tok_idx]); |
| 1925 | LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), ); |
| 1926 | exp->repeat[tok_idx][i] = repeat_op_idx; |
| 1927 | exp->repeat[tok_idx][i + 1] = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1928 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1929 | exp->repeat[tok_idx] = calloc(2, sizeof *exp->repeat[tok_idx]); |
| 1930 | LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), ); |
| 1931 | exp->repeat[tok_idx][0] = repeat_op_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1932 | } |
| 1933 | } |
| 1934 | |
| 1935 | /** |
| 1936 | * @brief Reparse Predicate. Logs directly on error. |
| 1937 | * |
| 1938 | * [7] Predicate ::= '[' Expr ']' |
| 1939 | * |
| 1940 | * @param[in] ctx Context for logging. |
| 1941 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1942 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1943 | * @return LY_ERR |
| 1944 | */ |
| 1945 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1946 | 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] | 1947 | { |
| 1948 | LY_ERR rc; |
| 1949 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1950 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1951 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1952 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1953 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1954 | rc = reparse_or_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1955 | LY_CHECK_RET(rc); |
| 1956 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1957 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1958 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1959 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1960 | |
| 1961 | return LY_SUCCESS; |
| 1962 | } |
| 1963 | |
| 1964 | /** |
| 1965 | * @brief Reparse RelativeLocationPath. Logs directly on error. |
| 1966 | * |
| 1967 | * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step |
| 1968 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 1969 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 1970 | * |
| 1971 | * @param[in] ctx Context for logging. |
| 1972 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1973 | * @param[in] tok_idx Position in the expression \p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1974 | * @return LY_ERR (LY_EINCOMPLETE on forward reference) |
| 1975 | */ |
| 1976 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1977 | 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] | 1978 | { |
| 1979 | LY_ERR rc; |
| 1980 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1981 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1982 | LY_CHECK_RET(rc); |
| 1983 | |
| 1984 | goto step; |
| 1985 | do { |
| 1986 | /* '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1987 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1988 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1989 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1990 | LY_CHECK_RET(rc); |
| 1991 | step: |
| 1992 | /* Step */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1993 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1994 | case LYXP_TOKEN_DOT: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1995 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1996 | break; |
| 1997 | |
| 1998 | case LYXP_TOKEN_DDOT: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1999 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2000 | break; |
| 2001 | |
| 2002 | case LYXP_TOKEN_AT: |
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 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2005 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2006 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2007 | if ((exp->tokens[*tok_idx] != LYXP_TOKEN_NAMETEST) && (exp->tokens[*tok_idx] != LYXP_TOKEN_NODETYPE)) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2008 | LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2009 | return LY_EVALID; |
| 2010 | } |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 2011 | /* fall through */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2012 | case LYXP_TOKEN_NAMETEST: |
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 | goto reparse_predicate; |
| 2015 | break; |
| 2016 | |
| 2017 | case LYXP_TOKEN_NODETYPE: |
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 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2021 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2022 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2023 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2024 | |
| 2025 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2026 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2027 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2028 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2029 | |
| 2030 | reparse_predicate: |
| 2031 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2032 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 2033 | rc = reparse_predicate(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2034 | LY_CHECK_RET(rc); |
| 2035 | } |
| 2036 | break; |
| 2037 | default: |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2038 | LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2039 | return LY_EVALID; |
| 2040 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2041 | } 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] | 2042 | |
| 2043 | return LY_SUCCESS; |
| 2044 | } |
| 2045 | |
| 2046 | /** |
| 2047 | * @brief Reparse AbsoluteLocationPath. Logs directly on error. |
| 2048 | * |
| 2049 | * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath |
| 2050 | * |
| 2051 | * @param[in] ctx Context for logging. |
| 2052 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2053 | * @param[in] tok_idx Position in the expression \p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2054 | * @return LY_ERR |
| 2055 | */ |
| 2056 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2057 | 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] | 2058 | { |
| 2059 | LY_ERR rc; |
| 2060 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2061 | 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] | 2062 | |
| 2063 | /* '/' RelativeLocationPath? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2064 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2065 | /* '/' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2066 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2067 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2068 | if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2069 | return LY_SUCCESS; |
| 2070 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2071 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2072 | case LYXP_TOKEN_DOT: |
| 2073 | case LYXP_TOKEN_DDOT: |
| 2074 | case LYXP_TOKEN_AT: |
| 2075 | case LYXP_TOKEN_NAMETEST: |
| 2076 | case LYXP_TOKEN_NODETYPE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2077 | rc = reparse_relative_location_path(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2078 | LY_CHECK_RET(rc); |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 2079 | /* fall through */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2080 | default: |
| 2081 | break; |
| 2082 | } |
| 2083 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2084 | } else { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 2085 | /* '//' RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2086 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2087 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2088 | rc = reparse_relative_location_path(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2089 | LY_CHECK_RET(rc); |
| 2090 | } |
| 2091 | |
| 2092 | return LY_SUCCESS; |
| 2093 | } |
| 2094 | |
| 2095 | /** |
| 2096 | * @brief Reparse FunctionCall. Logs directly on error. |
| 2097 | * |
| 2098 | * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')' |
| 2099 | * |
| 2100 | * @param[in] ctx Context for logging. |
| 2101 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2102 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2103 | * @return LY_ERR |
| 2104 | */ |
| 2105 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2106 | 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] | 2107 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2108 | int8_t min_arg_count = -1; |
| 2109 | uint32_t arg_count, max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2110 | uint16_t func_tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2111 | LY_ERR rc; |
| 2112 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2113 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2114 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2115 | func_tok_idx = *tok_idx; |
| 2116 | switch (exp->tok_len[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2117 | case 3: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2118 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) { |
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]], "sum", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2122 | min_arg_count = 1; |
| 2123 | max_arg_count = 1; |
| 2124 | } |
| 2125 | break; |
| 2126 | case 4: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2127 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2128 | min_arg_count = 1; |
| 2129 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2130 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2131 | min_arg_count = 0; |
| 2132 | max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2133 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2134 | min_arg_count = 0; |
| 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]], "true", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2137 | min_arg_count = 0; |
| 2138 | max_arg_count = 0; |
| 2139 | } |
| 2140 | break; |
| 2141 | case 5: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2142 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 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]], "false", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2146 | min_arg_count = 0; |
| 2147 | max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2148 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2149 | min_arg_count = 1; |
| 2150 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2151 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2152 | min_arg_count = 1; |
| 2153 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2154 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2155 | min_arg_count = 1; |
| 2156 | max_arg_count = 1; |
| 2157 | } |
| 2158 | break; |
| 2159 | case 6: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2160 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2161 | min_arg_count = 2; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2162 | max_arg_count = UINT32_MAX; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2163 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2164 | min_arg_count = 0; |
| 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]], "string", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2167 | min_arg_count = 0; |
| 2168 | max_arg_count = 1; |
| 2169 | } |
| 2170 | break; |
| 2171 | case 7: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2172 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2173 | min_arg_count = 1; |
| 2174 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2175 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2176 | min_arg_count = 1; |
| 2177 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2178 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2179 | min_arg_count = 0; |
| 2180 | max_arg_count = 0; |
| 2181 | } |
| 2182 | break; |
| 2183 | case 8: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2184 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2185 | min_arg_count = 2; |
| 2186 | max_arg_count = 2; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2187 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2188 | min_arg_count = 0; |
| 2189 | max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2190 | } 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] | 2191 | min_arg_count = 2; |
| 2192 | max_arg_count = 2; |
| 2193 | } |
| 2194 | break; |
| 2195 | case 9: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2196 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2197 | min_arg_count = 2; |
| 2198 | max_arg_count = 3; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2199 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2200 | min_arg_count = 3; |
| 2201 | max_arg_count = 3; |
| 2202 | } |
| 2203 | break; |
| 2204 | case 10: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2205 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2206 | min_arg_count = 0; |
| 2207 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2208 | } 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] | 2209 | min_arg_count = 1; |
| 2210 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2211 | } 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] | 2212 | min_arg_count = 2; |
| 2213 | max_arg_count = 2; |
| 2214 | } |
| 2215 | break; |
| 2216 | case 11: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2217 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2218 | min_arg_count = 2; |
| 2219 | max_arg_count = 2; |
| 2220 | } |
| 2221 | break; |
| 2222 | case 12: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2223 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2224 | min_arg_count = 2; |
| 2225 | max_arg_count = 2; |
| 2226 | } |
| 2227 | break; |
| 2228 | case 13: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2229 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) { |
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]], "string-length", 13)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2233 | min_arg_count = 0; |
| 2234 | max_arg_count = 1; |
| 2235 | } |
| 2236 | break; |
| 2237 | case 15: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2238 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2239 | min_arg_count = 0; |
| 2240 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2241 | } 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] | 2242 | min_arg_count = 2; |
| 2243 | max_arg_count = 2; |
| 2244 | } |
| 2245 | break; |
| 2246 | case 16: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2247 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2248 | min_arg_count = 2; |
| 2249 | max_arg_count = 2; |
| 2250 | } |
| 2251 | break; |
| 2252 | case 20: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2253 | 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] | 2254 | min_arg_count = 2; |
| 2255 | max_arg_count = 2; |
| 2256 | } |
| 2257 | break; |
| 2258 | } |
| 2259 | if (min_arg_count == -1) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2260 | LOGVAL(ctx, LY_VCODE_XP_INFUNC, exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2261 | return LY_EINVAL; |
| 2262 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2263 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2264 | |
| 2265 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2266 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2267 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2268 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2269 | |
| 2270 | /* ( Expr ( ',' Expr )* )? */ |
| 2271 | arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2272 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2273 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2274 | if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2275 | ++arg_count; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2276 | rc = reparse_or_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2277 | LY_CHECK_RET(rc); |
| 2278 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2279 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) { |
| 2280 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2281 | |
| 2282 | ++arg_count; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2283 | rc = reparse_or_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2284 | LY_CHECK_RET(rc); |
| 2285 | } |
| 2286 | |
| 2287 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2288 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2289 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2290 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2291 | |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 2292 | if ((arg_count < (uint32_t)min_arg_count) || (arg_count > max_arg_count)) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2293 | LOGVAL(ctx, LY_VCODE_XP_INARGCOUNT, arg_count, exp->tok_len[func_tok_idx], &exp->expr[exp->tok_pos[func_tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2294 | return LY_EVALID; |
| 2295 | } |
| 2296 | |
| 2297 | return LY_SUCCESS; |
| 2298 | } |
| 2299 | |
| 2300 | /** |
| 2301 | * @brief Reparse PathExpr. Logs directly on error. |
| 2302 | * |
| 2303 | * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate* |
| 2304 | * | PrimaryExpr Predicate* '/' RelativeLocationPath |
| 2305 | * | PrimaryExpr Predicate* '//' RelativeLocationPath |
| 2306 | * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath |
| 2307 | * [8] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall |
| 2308 | * |
| 2309 | * @param[in] ctx Context for logging. |
| 2310 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2311 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2312 | * @return LY_ERR |
| 2313 | */ |
| 2314 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2315 | 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] | 2316 | { |
| 2317 | LY_ERR rc; |
| 2318 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2319 | if (lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 2320 | return LY_EVALID; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2321 | } |
| 2322 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2323 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2324 | case LYXP_TOKEN_PAR1: |
| 2325 | /* '(' Expr ')' Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2326 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2327 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2328 | rc = reparse_or_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2329 | LY_CHECK_RET(rc); |
| 2330 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2331 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2332 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2333 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2334 | goto predicate; |
| 2335 | break; |
| 2336 | case LYXP_TOKEN_DOT: |
| 2337 | case LYXP_TOKEN_DDOT: |
| 2338 | case LYXP_TOKEN_AT: |
| 2339 | case LYXP_TOKEN_NAMETEST: |
| 2340 | case LYXP_TOKEN_NODETYPE: |
| 2341 | /* RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2342 | rc = reparse_relative_location_path(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2343 | LY_CHECK_RET(rc); |
| 2344 | break; |
| 2345 | case LYXP_TOKEN_FUNCNAME: |
| 2346 | /* FunctionCall */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2347 | rc = reparse_function_call(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2348 | LY_CHECK_RET(rc); |
| 2349 | goto predicate; |
| 2350 | break; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2351 | case LYXP_TOKEN_OPER_PATH: |
| 2352 | case LYXP_TOKEN_OPER_RPATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2353 | /* AbsoluteLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2354 | rc = reparse_absolute_location_path(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2355 | LY_CHECK_RET(rc); |
| 2356 | break; |
| 2357 | case LYXP_TOKEN_LITERAL: |
| 2358 | /* Literal */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2359 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2360 | goto predicate; |
| 2361 | break; |
| 2362 | case LYXP_TOKEN_NUMBER: |
| 2363 | /* Number */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2364 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2365 | goto predicate; |
| 2366 | break; |
| 2367 | default: |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2368 | LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2369 | return LY_EVALID; |
| 2370 | } |
| 2371 | |
| 2372 | return LY_SUCCESS; |
| 2373 | |
| 2374 | predicate: |
| 2375 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2376 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 2377 | rc = reparse_predicate(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2378 | LY_CHECK_RET(rc); |
| 2379 | } |
| 2380 | |
| 2381 | /* ('/' or '//') RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2382 | 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] | 2383 | |
| 2384 | /* '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2385 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2386 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2387 | rc = reparse_relative_location_path(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2388 | LY_CHECK_RET(rc); |
| 2389 | } |
| 2390 | |
| 2391 | return LY_SUCCESS; |
| 2392 | } |
| 2393 | |
| 2394 | /** |
| 2395 | * @brief Reparse UnaryExpr. Logs directly on error. |
| 2396 | * |
| 2397 | * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr |
| 2398 | * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr |
| 2399 | * |
| 2400 | * @param[in] ctx Context for logging. |
| 2401 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2402 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2403 | * @return LY_ERR |
| 2404 | */ |
| 2405 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2406 | 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] | 2407 | { |
| 2408 | uint16_t prev_exp; |
| 2409 | LY_ERR rc; |
| 2410 | |
| 2411 | /* ('-')* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2412 | prev_exp = *tok_idx; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2413 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) && |
| 2414 | (exp->expr[exp->tok_pos[*tok_idx]] == '-')) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2415 | exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2416 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2417 | } |
| 2418 | |
| 2419 | /* PathExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2420 | prev_exp = *tok_idx; |
| 2421 | rc = reparse_path_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2422 | LY_CHECK_RET(rc); |
| 2423 | |
| 2424 | /* ('|' PathExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2425 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_UNI)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2426 | exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2427 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2428 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2429 | rc = reparse_path_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2430 | LY_CHECK_RET(rc); |
| 2431 | } |
| 2432 | |
| 2433 | return LY_SUCCESS; |
| 2434 | } |
| 2435 | |
| 2436 | /** |
| 2437 | * @brief Reparse AdditiveExpr. Logs directly on error. |
| 2438 | * |
| 2439 | * [15] AdditiveExpr ::= MultiplicativeExpr |
| 2440 | * | AdditiveExpr '+' MultiplicativeExpr |
| 2441 | * | AdditiveExpr '-' MultiplicativeExpr |
| 2442 | * [16] MultiplicativeExpr ::= UnaryExpr |
| 2443 | * | MultiplicativeExpr '*' UnaryExpr |
| 2444 | * | MultiplicativeExpr 'div' UnaryExpr |
| 2445 | * | MultiplicativeExpr 'mod' UnaryExpr |
| 2446 | * |
| 2447 | * @param[in] ctx Context for logging. |
| 2448 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2449 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2450 | * @return LY_ERR |
| 2451 | */ |
| 2452 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2453 | 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] | 2454 | { |
| 2455 | uint16_t prev_add_exp, prev_mul_exp; |
| 2456 | LY_ERR rc; |
| 2457 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2458 | prev_add_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2459 | goto reparse_multiplicative_expr; |
| 2460 | |
| 2461 | /* ('+' / '-' MultiplicativeExpr)* */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2462 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) && |
| 2463 | ((exp->expr[exp->tok_pos[*tok_idx]] == '+') || (exp->expr[exp->tok_pos[*tok_idx]] == '-'))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2464 | exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2465 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2466 | |
| 2467 | reparse_multiplicative_expr: |
| 2468 | /* UnaryExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2469 | prev_mul_exp = *tok_idx; |
| 2470 | rc = reparse_unary_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2471 | LY_CHECK_RET(rc); |
| 2472 | |
| 2473 | /* ('*' / 'div' / 'mod' UnaryExpr)* */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2474 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) && |
| 2475 | ((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] | 2476 | exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2477 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2478 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2479 | rc = reparse_unary_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2480 | LY_CHECK_RET(rc); |
| 2481 | } |
| 2482 | } |
| 2483 | |
| 2484 | return LY_SUCCESS; |
| 2485 | } |
| 2486 | |
| 2487 | /** |
| 2488 | * @brief Reparse EqualityExpr. Logs directly on error. |
| 2489 | * |
| 2490 | * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr |
| 2491 | * | EqualityExpr '!=' RelationalExpr |
| 2492 | * [14] RelationalExpr ::= AdditiveExpr |
| 2493 | * | RelationalExpr '<' AdditiveExpr |
| 2494 | * | RelationalExpr '>' AdditiveExpr |
| 2495 | * | RelationalExpr '<=' AdditiveExpr |
| 2496 | * | RelationalExpr '>=' AdditiveExpr |
| 2497 | * |
| 2498 | * @param[in] ctx Context for logging. |
| 2499 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2500 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2501 | * @return LY_ERR |
| 2502 | */ |
| 2503 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2504 | 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] | 2505 | { |
| 2506 | uint16_t prev_eq_exp, prev_rel_exp; |
| 2507 | LY_ERR rc; |
| 2508 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2509 | prev_eq_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2510 | goto reparse_additive_expr; |
| 2511 | |
| 2512 | /* ('=' / '!=' RelationalExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2513 | 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] | 2514 | exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2515 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2516 | |
| 2517 | reparse_additive_expr: |
| 2518 | /* AdditiveExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2519 | prev_rel_exp = *tok_idx; |
| 2520 | rc = reparse_additive_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2521 | LY_CHECK_RET(rc); |
| 2522 | |
| 2523 | /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2524 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_COMP)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2525 | exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2526 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2527 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2528 | rc = reparse_additive_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2529 | LY_CHECK_RET(rc); |
| 2530 | } |
| 2531 | } |
| 2532 | |
| 2533 | return LY_SUCCESS; |
| 2534 | } |
| 2535 | |
| 2536 | /** |
| 2537 | * @brief Reparse OrExpr. Logs directly on error. |
| 2538 | * |
| 2539 | * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr |
| 2540 | * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr |
| 2541 | * |
| 2542 | * @param[in] ctx Context for logging. |
| 2543 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2544 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2545 | * @return LY_ERR |
| 2546 | */ |
| 2547 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2548 | 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] | 2549 | { |
| 2550 | uint16_t prev_or_exp, prev_and_exp; |
| 2551 | LY_ERR rc; |
| 2552 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2553 | prev_or_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2554 | goto reparse_equality_expr; |
| 2555 | |
| 2556 | /* ('or' AndExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2557 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_LOG) && (exp->tok_len[*tok_idx] == 2)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2558 | exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2559 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2560 | |
| 2561 | reparse_equality_expr: |
| 2562 | /* EqualityExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2563 | prev_and_exp = *tok_idx; |
| 2564 | rc = reparse_equality_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2565 | LY_CHECK_RET(rc); |
| 2566 | |
| 2567 | /* ('and' EqualityExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2568 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_LOG) && (exp->tok_len[*tok_idx] == 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2569 | exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2570 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2571 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2572 | rc = reparse_equality_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2573 | LY_CHECK_RET(rc); |
| 2574 | } |
| 2575 | } |
| 2576 | |
| 2577 | return LY_SUCCESS; |
| 2578 | } |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2579 | |
| 2580 | /** |
| 2581 | * @brief Parse NCName. |
| 2582 | * |
| 2583 | * @param[in] ncname Name to parse. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2584 | * @return Length of @p ncname valid bytes. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2585 | */ |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2586 | static long int |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2587 | parse_ncname(const char *ncname) |
| 2588 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2589 | uint32_t uc; |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2590 | size_t size; |
| 2591 | long int len = 0; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2592 | |
| 2593 | LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0); |
| 2594 | if (!is_xmlqnamestartchar(uc) || (uc == ':')) { |
| 2595 | return len; |
| 2596 | } |
| 2597 | |
| 2598 | do { |
| 2599 | len += size; |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 2600 | if (!*ncname) { |
| 2601 | break; |
| 2602 | } |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2603 | LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2604 | } while (is_xmlqnamechar(uc) && (uc != ':')); |
| 2605 | |
| 2606 | return len; |
| 2607 | } |
| 2608 | |
| 2609 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2610 | * @brief Add @p token into the expression @p exp. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2611 | * |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2612 | * @param[in] ctx Context for logging. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2613 | * @param[in] exp Expression to use. |
| 2614 | * @param[in] token Token to add. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2615 | * @param[in] tok_pos Token position in the XPath expression. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2616 | * @param[in] tok_len Token length in the XPath expression. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2617 | * @return LY_ERR |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2618 | */ |
| 2619 | static LY_ERR |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 2620 | exp_add_token(const struct ly_ctx *ctx, struct lyxp_expr *exp, enum lyxp_token token, uint16_t tok_pos, uint16_t tok_len) |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2621 | { |
| 2622 | uint32_t prev; |
| 2623 | |
| 2624 | if (exp->used == exp->size) { |
| 2625 | prev = exp->size; |
| 2626 | exp->size += LYXP_EXPR_SIZE_STEP; |
| 2627 | if (prev > exp->size) { |
| 2628 | LOGINT(ctx); |
| 2629 | return LY_EINT; |
| 2630 | } |
| 2631 | |
| 2632 | exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens); |
| 2633 | LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM); |
| 2634 | exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos); |
| 2635 | LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM); |
| 2636 | exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len); |
| 2637 | LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM); |
| 2638 | } |
| 2639 | |
| 2640 | exp->tokens[exp->used] = token; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2641 | exp->tok_pos[exp->used] = tok_pos; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2642 | exp->tok_len[exp->used] = tok_len; |
| 2643 | ++exp->used; |
| 2644 | return LY_SUCCESS; |
| 2645 | } |
| 2646 | |
| 2647 | void |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 2648 | lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr) |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2649 | { |
| 2650 | uint16_t i; |
| 2651 | |
| 2652 | if (!expr) { |
| 2653 | return; |
| 2654 | } |
| 2655 | |
| 2656 | lydict_remove(ctx, expr->expr); |
| 2657 | free(expr->tokens); |
| 2658 | free(expr->tok_pos); |
| 2659 | free(expr->tok_len); |
| 2660 | if (expr->repeat) { |
| 2661 | for (i = 0; i < expr->used; ++i) { |
| 2662 | free(expr->repeat[i]); |
| 2663 | } |
| 2664 | } |
| 2665 | free(expr->repeat); |
| 2666 | free(expr); |
| 2667 | } |
| 2668 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2669 | LY_ERR |
| 2670 | lyxp_expr_parse(const struct ly_ctx *ctx, const char *expr_str, size_t expr_len, ly_bool reparse, struct lyxp_expr **expr_p) |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2671 | { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2672 | LY_ERR ret = LY_SUCCESS; |
| 2673 | struct lyxp_expr *expr; |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2674 | size_t parsed = 0, tok_len; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2675 | enum lyxp_token tok_type; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 2676 | ly_bool prev_function_check = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2677 | uint16_t tok_idx = 0; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2678 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2679 | assert(expr_p); |
| 2680 | |
| 2681 | if (!expr_str[0]) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2682 | LOGVAL(ctx, LY_VCODE_XP_EOF); |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2683 | return LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2684 | } |
| 2685 | |
| 2686 | if (!expr_len) { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2687 | expr_len = strlen(expr_str); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2688 | } |
| 2689 | if (expr_len > UINT16_MAX) { |
Michal Vasko | 623ac7b | 2021-04-09 12:44:23 +0200 | [diff] [blame] | 2690 | LOGVAL(ctx, LYVE_XPATH, "XPath expression cannot be longer than %u characters.", UINT16_MAX); |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2691 | return LY_EVALID; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2692 | } |
| 2693 | |
| 2694 | /* init lyxp_expr structure */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2695 | expr = calloc(1, sizeof *expr); |
| 2696 | LY_CHECK_ERR_GOTO(!expr, LOGMEM(ctx); ret = LY_EMEM, error); |
| 2697 | LY_CHECK_GOTO(ret = lydict_insert(ctx, expr_str, expr_len, &expr->expr), error); |
| 2698 | expr->used = 0; |
| 2699 | expr->size = LYXP_EXPR_SIZE_START; |
| 2700 | expr->tokens = malloc(expr->size * sizeof *expr->tokens); |
| 2701 | LY_CHECK_ERR_GOTO(!expr->tokens, LOGMEM(ctx); ret = LY_EMEM, error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2702 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2703 | expr->tok_pos = malloc(expr->size * sizeof *expr->tok_pos); |
| 2704 | LY_CHECK_ERR_GOTO(!expr->tok_pos, LOGMEM(ctx); ret = LY_EMEM, error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2705 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2706 | expr->tok_len = malloc(expr->size * sizeof *expr->tok_len); |
| 2707 | LY_CHECK_ERR_GOTO(!expr->tok_len, LOGMEM(ctx); ret = LY_EMEM, error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2708 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2709 | /* make expr 0-terminated */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2710 | expr_str = expr->expr; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2711 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2712 | while (is_xmlws(expr_str[parsed])) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2713 | ++parsed; |
| 2714 | } |
| 2715 | |
| 2716 | do { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2717 | if (expr_str[parsed] == '(') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2718 | |
| 2719 | /* '(' */ |
| 2720 | tok_len = 1; |
| 2721 | tok_type = LYXP_TOKEN_PAR1; |
| 2722 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2723 | if (prev_function_check && expr->used && (expr->tokens[expr->used - 1] == LYXP_TOKEN_NAMETEST)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2724 | /* it is a NodeType/FunctionName after all */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2725 | if (((expr->tok_len[expr->used - 1] == 4) && |
| 2726 | (!strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "node", 4) || |
| 2727 | !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "text", 4))) || |
| 2728 | ((expr->tok_len[expr->used - 1] == 7) && |
| 2729 | !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "comment", 7))) { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2730 | expr->tokens[expr->used - 1] = LYXP_TOKEN_NODETYPE; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2731 | } else { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2732 | expr->tokens[expr->used - 1] = LYXP_TOKEN_FUNCNAME; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2733 | } |
| 2734 | prev_function_check = 0; |
| 2735 | } |
| 2736 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2737 | } else if (expr_str[parsed] == ')') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2738 | |
| 2739 | /* ')' */ |
| 2740 | tok_len = 1; |
| 2741 | tok_type = LYXP_TOKEN_PAR2; |
| 2742 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2743 | } else if (expr_str[parsed] == '[') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2744 | |
| 2745 | /* '[' */ |
| 2746 | tok_len = 1; |
| 2747 | tok_type = LYXP_TOKEN_BRACK1; |
| 2748 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2749 | } else if (expr_str[parsed] == ']') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2750 | |
| 2751 | /* ']' */ |
| 2752 | tok_len = 1; |
| 2753 | tok_type = LYXP_TOKEN_BRACK2; |
| 2754 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2755 | } else if (!strncmp(&expr_str[parsed], "..", 2)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2756 | |
| 2757 | /* '..' */ |
| 2758 | tok_len = 2; |
| 2759 | tok_type = LYXP_TOKEN_DDOT; |
| 2760 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2761 | } else if ((expr_str[parsed] == '.') && (!isdigit(expr_str[parsed + 1]))) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2762 | |
| 2763 | /* '.' */ |
| 2764 | tok_len = 1; |
| 2765 | tok_type = LYXP_TOKEN_DOT; |
| 2766 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2767 | } else if (expr_str[parsed] == '@') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2768 | |
| 2769 | /* '@' */ |
| 2770 | tok_len = 1; |
| 2771 | tok_type = LYXP_TOKEN_AT; |
| 2772 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2773 | } else if (expr_str[parsed] == ',') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2774 | |
| 2775 | /* ',' */ |
| 2776 | tok_len = 1; |
| 2777 | tok_type = LYXP_TOKEN_COMMA; |
| 2778 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2779 | } else if (expr_str[parsed] == '\'') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2780 | |
| 2781 | /* Literal with ' */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2782 | for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\''); ++tok_len) {} |
| 2783 | LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0', |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2784 | LOGVAL(ctx, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2785 | error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2786 | ++tok_len; |
| 2787 | tok_type = LYXP_TOKEN_LITERAL; |
| 2788 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2789 | } else if (expr_str[parsed] == '\"') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2790 | |
| 2791 | /* Literal with " */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2792 | for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\"'); ++tok_len) {} |
| 2793 | LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0', |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2794 | LOGVAL(ctx, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2795 | error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2796 | ++tok_len; |
| 2797 | tok_type = LYXP_TOKEN_LITERAL; |
| 2798 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2799 | } else if ((expr_str[parsed] == '.') || (isdigit(expr_str[parsed]))) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2800 | |
| 2801 | /* Number */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2802 | for (tok_len = 0; isdigit(expr_str[parsed + tok_len]); ++tok_len) {} |
| 2803 | if (expr_str[parsed + tok_len] == '.') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2804 | ++tok_len; |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2805 | for ( ; isdigit(expr_str[parsed + tok_len]); ++tok_len) {} |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2806 | } |
| 2807 | tok_type = LYXP_TOKEN_NUMBER; |
| 2808 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2809 | } else if (expr_str[parsed] == '/') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2810 | |
| 2811 | /* Operator '/', '//' */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2812 | if (!strncmp(&expr_str[parsed], "//", 2)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2813 | tok_len = 2; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2814 | tok_type = LYXP_TOKEN_OPER_RPATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2815 | } else { |
| 2816 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2817 | tok_type = LYXP_TOKEN_OPER_PATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2818 | } |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2819 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2820 | } else if (!strncmp(&expr_str[parsed], "!=", 2)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2821 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2822 | /* Operator '!=' */ |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2823 | tok_len = 2; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2824 | tok_type = LYXP_TOKEN_OPER_NEQUAL; |
| 2825 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2826 | } else if (!strncmp(&expr_str[parsed], "<=", 2) || !strncmp(&expr_str[parsed], ">=", 2)) { |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2827 | |
| 2828 | /* Operator '<=', '>=' */ |
| 2829 | tok_len = 2; |
| 2830 | tok_type = LYXP_TOKEN_OPER_COMP; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2831 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2832 | } else if (expr_str[parsed] == '|') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2833 | |
| 2834 | /* Operator '|' */ |
| 2835 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2836 | tok_type = LYXP_TOKEN_OPER_UNI; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2837 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2838 | } else if ((expr_str[parsed] == '+') || (expr_str[parsed] == '-')) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2839 | |
| 2840 | /* Operator '+', '-' */ |
| 2841 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2842 | tok_type = LYXP_TOKEN_OPER_MATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2843 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2844 | } else if (expr_str[parsed] == '=') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2845 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2846 | /* Operator '=' */ |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2847 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2848 | tok_type = LYXP_TOKEN_OPER_EQUAL; |
| 2849 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2850 | } else if ((expr_str[parsed] == '<') || (expr_str[parsed] == '>')) { |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2851 | |
| 2852 | /* Operator '<', '>' */ |
| 2853 | tok_len = 1; |
| 2854 | tok_type = LYXP_TOKEN_OPER_COMP; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2855 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2856 | } else if (expr->used && (expr->tokens[expr->used - 1] != LYXP_TOKEN_AT) && |
| 2857 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_PAR1) && |
| 2858 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_BRACK1) && |
| 2859 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_COMMA) && |
| 2860 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_LOG) && |
| 2861 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_EQUAL) && |
| 2862 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_NEQUAL) && |
| 2863 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_COMP) && |
| 2864 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_MATH) && |
| 2865 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_UNI) && |
| 2866 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_PATH) && |
| 2867 | (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_RPATH)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2868 | |
| 2869 | /* Operator '*', 'or', 'and', 'mod', or 'div' */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2870 | if (expr_str[parsed] == '*') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2871 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2872 | tok_type = LYXP_TOKEN_OPER_MATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2873 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2874 | } else if (!strncmp(&expr_str[parsed], "or", 2)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2875 | tok_len = 2; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2876 | tok_type = LYXP_TOKEN_OPER_LOG; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2877 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2878 | } else if (!strncmp(&expr_str[parsed], "and", 3)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2879 | tok_len = 3; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2880 | tok_type = LYXP_TOKEN_OPER_LOG; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2881 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2882 | } 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] | 2883 | tok_len = 3; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2884 | tok_type = LYXP_TOKEN_OPER_MATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2885 | |
| 2886 | } else if (prev_function_check) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2887 | LOGVAL(ctx, LYVE_XPATH, "Invalid character 0x%x ('%c'), perhaps \"%.*s\" is supposed to be a function call.", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2888 | 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] | 2889 | ret = LY_EVALID; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2890 | goto error; |
| 2891 | } else { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2892 | LOGVAL(ctx, LY_VCODE_XP_INEXPR, parsed + 1, expr_str); |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2893 | ret = LY_EVALID; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2894 | goto error; |
| 2895 | } |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2896 | } else if (expr_str[parsed] == '*') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2897 | |
| 2898 | /* NameTest '*' */ |
| 2899 | tok_len = 1; |
| 2900 | tok_type = LYXP_TOKEN_NAMETEST; |
| 2901 | |
| 2902 | } else { |
| 2903 | |
| 2904 | /* NameTest (NCName ':' '*' | QName) or NodeType/FunctionName */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2905 | long int ncname_len = parse_ncname(&expr_str[parsed]); |
| 2906 | LY_CHECK_ERR_GOTO(ncname_len < 0, |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2907 | LOGVAL(ctx, LY_VCODE_XP_INEXPR, parsed - ncname_len + 1, expr_str); ret = LY_EVALID, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2908 | error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2909 | tok_len = ncname_len; |
| 2910 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2911 | if (expr_str[parsed + tok_len] == ':') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2912 | ++tok_len; |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2913 | if (expr_str[parsed + tok_len] == '*') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2914 | ++tok_len; |
| 2915 | } else { |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2916 | ncname_len = parse_ncname(&expr_str[parsed + tok_len]); |
| 2917 | LY_CHECK_ERR_GOTO(ncname_len < 0, |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2918 | LOGVAL(ctx, LY_VCODE_XP_INEXPR, parsed - ncname_len + 1, expr_str); ret = LY_EVALID, |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2919 | error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2920 | tok_len += ncname_len; |
| 2921 | } |
| 2922 | /* remove old flag to prevent ambiguities */ |
| 2923 | prev_function_check = 0; |
| 2924 | tok_type = LYXP_TOKEN_NAMETEST; |
| 2925 | } else { |
| 2926 | /* there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */ |
| 2927 | prev_function_check = 1; |
| 2928 | tok_type = LYXP_TOKEN_NAMETEST; |
| 2929 | } |
| 2930 | } |
| 2931 | |
| 2932 | /* store the token, move on to the next one */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2933 | 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] | 2934 | parsed += tok_len; |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2935 | while (is_xmlws(expr_str[parsed])) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2936 | ++parsed; |
| 2937 | } |
| 2938 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2939 | } while (expr_str[parsed]); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2940 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2941 | if (reparse) { |
| 2942 | /* prealloc repeat */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2943 | expr->repeat = calloc(expr->size, sizeof *expr->repeat); |
| 2944 | LY_CHECK_ERR_GOTO(!expr->repeat, LOGMEM(ctx); ret = LY_EMEM, error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2945 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2946 | /* fill repeat */ |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2947 | LY_CHECK_ERR_GOTO(reparse_or_expr(ctx, expr, &tok_idx), ret = LY_EVALID, error); |
| 2948 | if (expr->used > tok_idx) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 2949 | LOGVAL(ctx, LYVE_XPATH, "Unparsed characters \"%s\" left at the end of an XPath expression.", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 2950 | &expr->expr[expr->tok_pos[tok_idx]]); |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2951 | ret = LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2952 | goto error; |
| 2953 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2954 | } |
| 2955 | |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2956 | print_expr_struct_debug(expr); |
| 2957 | *expr_p = expr; |
| 2958 | return LY_SUCCESS; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2959 | |
| 2960 | error: |
Radek Krejci | f03a9e2 | 2020-09-18 20:09:31 +0200 | [diff] [blame] | 2961 | lyxp_expr_free(ctx, expr); |
| 2962 | return ret; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2963 | } |
| 2964 | |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2965 | LY_ERR |
| 2966 | 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] | 2967 | { |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2968 | LY_ERR ret = LY_SUCCESS; |
| 2969 | struct lyxp_expr *dup = NULL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2970 | uint32_t i, j; |
| 2971 | |
Michal Vasko | 7f45cf2 | 2020-10-01 12:49:44 +0200 | [diff] [blame] | 2972 | if (!exp) { |
| 2973 | goto cleanup; |
| 2974 | } |
| 2975 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2976 | dup = calloc(1, sizeof *dup); |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2977 | LY_CHECK_ERR_GOTO(!dup, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2978 | |
| 2979 | dup->tokens = malloc(exp->used * sizeof *dup->tokens); |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2980 | LY_CHECK_ERR_GOTO(!dup->tokens, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2981 | memcpy(dup->tokens, exp->tokens, exp->used * sizeof *dup->tokens); |
| 2982 | |
| 2983 | dup->tok_pos = malloc(exp->used * sizeof *dup->tok_pos); |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2984 | 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] | 2985 | memcpy(dup->tok_pos, exp->tok_pos, exp->used * sizeof *dup->tok_pos); |
| 2986 | |
| 2987 | dup->tok_len = malloc(exp->used * sizeof *dup->tok_len); |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2988 | 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] | 2989 | memcpy(dup->tok_len, exp->tok_len, exp->used * sizeof *dup->tok_len); |
| 2990 | |
| 2991 | dup->repeat = malloc(exp->used * sizeof *dup->repeat); |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 2992 | LY_CHECK_ERR_GOTO(!dup->repeat, LOGMEM(ctx); ret = LY_EMEM, cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2993 | for (i = 0; i < exp->used; ++i) { |
| 2994 | if (!exp->repeat[i]) { |
| 2995 | dup->repeat[i] = NULL; |
| 2996 | } else { |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 2997 | for (j = 0; exp->repeat[i][j]; ++j) {} |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2998 | /* the ending 0 as well */ |
| 2999 | ++j; |
| 3000 | |
Michal Vasko | 99c7164 | 2020-07-03 13:33:36 +0200 | [diff] [blame] | 3001 | dup->repeat[i] = malloc(j * sizeof **dup->repeat); |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 3002 | 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] | 3003 | memcpy(dup->repeat[i], exp->repeat[i], j * sizeof **dup->repeat); |
| 3004 | dup->repeat[i][j - 1] = 0; |
| 3005 | } |
| 3006 | } |
| 3007 | |
| 3008 | dup->used = exp->used; |
| 3009 | dup->size = exp->used; |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 3010 | 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] | 3011 | |
Michal Vasko | 1734be9 | 2020-09-22 08:55:10 +0200 | [diff] [blame] | 3012 | cleanup: |
| 3013 | if (ret) { |
| 3014 | lyxp_expr_free(ctx, dup); |
| 3015 | } else { |
| 3016 | *dup_p = dup; |
| 3017 | } |
| 3018 | return ret; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3019 | } |
| 3020 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3021 | /* |
| 3022 | * warn functions |
| 3023 | * |
| 3024 | * Warn functions check specific reasonable conditions for schema XPath |
| 3025 | * and print a warning if they are not satisfied. |
| 3026 | */ |
| 3027 | |
| 3028 | /** |
| 3029 | * @brief Get the last-added schema node that is currently in the context. |
| 3030 | * |
| 3031 | * @param[in] set Set to search in. |
| 3032 | * @return Last-added schema context node, NULL if no node is in context. |
| 3033 | */ |
| 3034 | static struct lysc_node * |
| 3035 | warn_get_scnode_in_ctx(struct lyxp_set *set) |
| 3036 | { |
| 3037 | uint32_t i; |
| 3038 | |
| 3039 | if (!set || (set->type != LYXP_SET_SCNODE_SET)) { |
| 3040 | return NULL; |
| 3041 | } |
| 3042 | |
| 3043 | i = set->used; |
| 3044 | do { |
| 3045 | --i; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 3046 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3047 | /* if there are more, simply return the first found (last added) */ |
| 3048 | return set->val.scnodes[i].scnode; |
| 3049 | } |
| 3050 | } while (i); |
| 3051 | |
| 3052 | return NULL; |
| 3053 | } |
| 3054 | |
| 3055 | /** |
| 3056 | * @brief Test whether a type is numeric - integer type or decimal64. |
| 3057 | * |
| 3058 | * @param[in] type Type to test. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3059 | * @return Boolean value whether @p type is numeric type or not. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3060 | */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3061 | static ly_bool |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3062 | warn_is_numeric_type(struct lysc_type *type) |
| 3063 | { |
| 3064 | struct lysc_type_union *uni; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3065 | ly_bool ret; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3066 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3067 | |
| 3068 | switch (type->basetype) { |
| 3069 | case LY_TYPE_DEC64: |
| 3070 | case LY_TYPE_INT8: |
| 3071 | case LY_TYPE_UINT8: |
| 3072 | case LY_TYPE_INT16: |
| 3073 | case LY_TYPE_UINT16: |
| 3074 | case LY_TYPE_INT32: |
| 3075 | case LY_TYPE_UINT32: |
| 3076 | case LY_TYPE_INT64: |
| 3077 | case LY_TYPE_UINT64: |
| 3078 | return 1; |
| 3079 | case LY_TYPE_UNION: |
| 3080 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3081 | LY_ARRAY_FOR(uni->types, u) { |
| 3082 | ret = warn_is_numeric_type(uni->types[u]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3083 | if (ret) { |
| 3084 | /* found a suitable type */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3085 | return ret; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3086 | } |
| 3087 | } |
| 3088 | /* did not find any suitable type */ |
| 3089 | return 0; |
| 3090 | case LY_TYPE_LEAFREF: |
| 3091 | return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype); |
| 3092 | default: |
| 3093 | return 0; |
| 3094 | } |
| 3095 | } |
| 3096 | |
| 3097 | /** |
| 3098 | * @brief Test whether a type is string-like - no integers, decimal64 or binary. |
| 3099 | * |
| 3100 | * @param[in] type Type to test. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3101 | * @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] | 3102 | */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3103 | static ly_bool |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3104 | warn_is_string_type(struct lysc_type *type) |
| 3105 | { |
| 3106 | struct lysc_type_union *uni; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3107 | ly_bool ret; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3108 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3109 | |
| 3110 | switch (type->basetype) { |
| 3111 | case LY_TYPE_BITS: |
| 3112 | case LY_TYPE_ENUM: |
| 3113 | case LY_TYPE_IDENT: |
| 3114 | case LY_TYPE_INST: |
| 3115 | case LY_TYPE_STRING: |
| 3116 | return 1; |
| 3117 | case LY_TYPE_UNION: |
| 3118 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3119 | LY_ARRAY_FOR(uni->types, u) { |
| 3120 | ret = warn_is_string_type(uni->types[u]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3121 | if (ret) { |
| 3122 | /* found a suitable type */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3123 | return ret; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3124 | } |
| 3125 | } |
| 3126 | /* did not find any suitable type */ |
| 3127 | return 0; |
| 3128 | case LY_TYPE_LEAFREF: |
| 3129 | return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype); |
| 3130 | default: |
| 3131 | return 0; |
| 3132 | } |
| 3133 | } |
| 3134 | |
| 3135 | /** |
| 3136 | * @brief Test whether a type is one specific type. |
| 3137 | * |
| 3138 | * @param[in] type Type to test. |
| 3139 | * @param[in] base Expected type. |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3140 | * @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] | 3141 | */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3142 | static ly_bool |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3143 | warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base) |
| 3144 | { |
| 3145 | struct lysc_type_union *uni; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3146 | ly_bool ret; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3147 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3148 | |
| 3149 | if (type->basetype == base) { |
| 3150 | return 1; |
| 3151 | } else if (type->basetype == LY_TYPE_UNION) { |
| 3152 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3153 | LY_ARRAY_FOR(uni->types, u) { |
| 3154 | ret = warn_is_specific_type(uni->types[u], base); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3155 | if (ret) { |
| 3156 | /* found a suitable type */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3157 | return ret; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3158 | } |
| 3159 | } |
| 3160 | /* did not find any suitable type */ |
| 3161 | return 0; |
| 3162 | } else if (type->basetype == LY_TYPE_LEAFREF) { |
| 3163 | return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base); |
| 3164 | } |
| 3165 | |
| 3166 | return 0; |
| 3167 | } |
| 3168 | |
| 3169 | /** |
| 3170 | * @brief Get next type of a (union) type. |
| 3171 | * |
| 3172 | * @param[in] type Base type. |
| 3173 | * @param[in] prev_type Previously returned type. |
| 3174 | * @return Next type or NULL. |
| 3175 | */ |
| 3176 | static struct lysc_type * |
| 3177 | warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type) |
| 3178 | { |
| 3179 | struct lysc_type_union *uni; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3180 | ly_bool found = 0; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3181 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3182 | |
| 3183 | switch (type->basetype) { |
| 3184 | case LY_TYPE_UNION: |
| 3185 | uni = (struct lysc_type_union *)type; |
| 3186 | if (!prev_type) { |
| 3187 | return uni->types[0]; |
| 3188 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3189 | LY_ARRAY_FOR(uni->types, u) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3190 | if (found) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3191 | return uni->types[u]; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3192 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3193 | if (prev_type == uni->types[u]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3194 | found = 1; |
| 3195 | } |
| 3196 | } |
| 3197 | return NULL; |
| 3198 | default: |
| 3199 | if (prev_type) { |
| 3200 | assert(type == prev_type); |
| 3201 | return NULL; |
| 3202 | } else { |
| 3203 | return type; |
| 3204 | } |
| 3205 | } |
| 3206 | } |
| 3207 | |
| 3208 | /** |
| 3209 | * @brief Test whether 2 types have a common type. |
| 3210 | * |
| 3211 | * @param[in] type1 First type. |
| 3212 | * @param[in] type2 Second type. |
| 3213 | * @return 1 if they do, 0 otherwise. |
| 3214 | */ |
| 3215 | static int |
| 3216 | warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2) |
| 3217 | { |
| 3218 | struct lysc_type *t1, *rt1, *t2, *rt2; |
| 3219 | |
| 3220 | t1 = NULL; |
| 3221 | while ((t1 = warn_is_equal_type_next_type(type1, t1))) { |
| 3222 | if (t1->basetype == LY_TYPE_LEAFREF) { |
| 3223 | rt1 = ((struct lysc_type_leafref *)t1)->realtype; |
| 3224 | } else { |
| 3225 | rt1 = t1; |
| 3226 | } |
| 3227 | |
| 3228 | t2 = NULL; |
| 3229 | while ((t2 = warn_is_equal_type_next_type(type2, t2))) { |
| 3230 | if (t2->basetype == LY_TYPE_LEAFREF) { |
| 3231 | rt2 = ((struct lysc_type_leafref *)t2)->realtype; |
| 3232 | } else { |
| 3233 | rt2 = t2; |
| 3234 | } |
| 3235 | |
| 3236 | if (rt2->basetype == rt1->basetype) { |
| 3237 | /* match found */ |
| 3238 | return 1; |
| 3239 | } |
| 3240 | } |
| 3241 | } |
| 3242 | |
| 3243 | return 0; |
| 3244 | } |
| 3245 | |
| 3246 | /** |
| 3247 | * @brief Check both operands of comparison operators. |
| 3248 | * |
| 3249 | * @param[in] ctx Context for errors. |
| 3250 | * @param[in] set1 First operand set. |
| 3251 | * @param[in] set2 Second operand set. |
| 3252 | * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!='). |
| 3253 | * @param[in] expr Start of the expression to print with the warning. |
| 3254 | * @param[in] tok_pos Token position. |
| 3255 | */ |
| 3256 | static void |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3257 | 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] | 3258 | { |
| 3259 | struct lysc_node_leaf *node1, *node2; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3260 | ly_bool leaves = 1, warning = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3261 | |
| 3262 | node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1); |
| 3263 | node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2); |
| 3264 | |
| 3265 | if (!node1 && !node2) { |
| 3266 | /* no node-sets involved, nothing to do */ |
| 3267 | return; |
| 3268 | } |
| 3269 | |
| 3270 | if (node1) { |
| 3271 | if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3272 | LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name); |
| 3273 | warning = 1; |
| 3274 | leaves = 0; |
| 3275 | } else if (numbers_only && !warn_is_numeric_type(node1->type)) { |
| 3276 | LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name); |
| 3277 | warning = 1; |
| 3278 | } |
| 3279 | } |
| 3280 | |
| 3281 | if (node2) { |
| 3282 | if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3283 | LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name); |
| 3284 | warning = 1; |
| 3285 | leaves = 0; |
| 3286 | } else if (numbers_only && !warn_is_numeric_type(node2->type)) { |
| 3287 | LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name); |
| 3288 | warning = 1; |
| 3289 | } |
| 3290 | } |
| 3291 | |
| 3292 | if (node1 && node2 && leaves && !numbers_only) { |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3293 | if ((warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type)) || |
| 3294 | (!warn_is_numeric_type(node1->type) && warn_is_numeric_type(node2->type)) || |
| 3295 | (!warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type) && |
| 3296 | !warn_is_equal_type(node1->type, node2->type))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3297 | LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name); |
| 3298 | warning = 1; |
| 3299 | } |
| 3300 | } |
| 3301 | |
| 3302 | if (warning) { |
| 3303 | LOGWRN(ctx, "Previous warning generated by XPath subexpression[%u] \"%.20s\".", tok_pos, expr + tok_pos); |
| 3304 | } |
| 3305 | } |
| 3306 | |
| 3307 | /** |
| 3308 | * @brief Check that a value is valid for a leaf. If not applicable, does nothing. |
| 3309 | * |
| 3310 | * @param[in] exp Parsed XPath expression. |
| 3311 | * @param[in] set Set with the leaf/leaf-list. |
| 3312 | * @param[in] val_exp Index of the value (literal/number) in @p exp. |
| 3313 | * @param[in] equal_exp Index of the start of the equality expression in @p exp. |
| 3314 | * @param[in] last_equal_exp Index of the end of the equality expression in @p exp. |
| 3315 | */ |
| 3316 | static void |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 3317 | warn_equality_value(const struct lyxp_expr *exp, struct lyxp_set *set, uint16_t val_exp, uint16_t equal_exp, |
| 3318 | uint16_t last_equal_exp) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3319 | { |
| 3320 | struct lysc_node *scnode; |
| 3321 | struct lysc_type *type; |
| 3322 | char *value; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3323 | struct lyd_value storage; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3324 | LY_ERR rc; |
| 3325 | struct ly_err_item *err = NULL; |
| 3326 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3327 | if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && |
| 3328 | ((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] | 3329 | /* check that the node can have the specified value */ |
| 3330 | if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) { |
| 3331 | value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2); |
| 3332 | } else { |
| 3333 | value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]); |
| 3334 | } |
| 3335 | if (!value) { |
| 3336 | LOGMEM(set->ctx); |
| 3337 | return; |
| 3338 | } |
| 3339 | |
| 3340 | if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) { |
| 3341 | 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] | 3342 | " 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] | 3343 | 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] | 3344 | (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] | 3345 | exp->expr + exp->tok_pos[equal_exp]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3346 | } |
| 3347 | |
| 3348 | type = ((struct lysc_node_leaf *)scnode)->type; |
| 3349 | if (type->basetype != LY_TYPE_IDENT) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 3350 | 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] | 3351 | LYD_HINT_DATA, scnode, &storage, NULL, &err); |
Michal Vasko | bf42e83 | 2020-11-23 16:59:42 +0100 | [diff] [blame] | 3352 | if (rc == LY_EINCOMPLETE) { |
| 3353 | rc = LY_SUCCESS; |
| 3354 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3355 | |
| 3356 | if (err) { |
| 3357 | LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg); |
| 3358 | ly_err_free(err); |
| 3359 | } else if (rc != LY_SUCCESS) { |
| 3360 | LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value); |
| 3361 | } |
| 3362 | if (rc != LY_SUCCESS) { |
| 3363 | 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] | 3364 | (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] | 3365 | exp->expr + exp->tok_pos[equal_exp]); |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3366 | } else { |
| 3367 | type->plugin->free(set->ctx, &storage); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3368 | } |
| 3369 | } |
| 3370 | free(value); |
| 3371 | } |
| 3372 | } |
| 3373 | |
| 3374 | /* |
| 3375 | * XPath functions |
| 3376 | */ |
| 3377 | |
| 3378 | /** |
| 3379 | * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN |
| 3380 | * depending on whether the first node bit value from the second argument is set. |
| 3381 | * |
| 3382 | * @param[in] args Array of arguments. |
| 3383 | * @param[in] arg_count Count of elements in @p args. |
| 3384 | * @param[in,out] set Context and result set at the same time. |
| 3385 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3386 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3387 | */ |
| 3388 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3389 | 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] | 3390 | { |
| 3391 | struct lyd_node_term *leaf; |
| 3392 | struct lysc_node_leaf *sleaf; |
| 3393 | struct lysc_type_bits *bits; |
| 3394 | LY_ERR rc = LY_SUCCESS; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3395 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3396 | |
| 3397 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3398 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3399 | LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__); |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3400 | } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3401 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3402 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 3403 | sleaf->name); |
| 3404 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) { |
| 3405 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"bits\".", __func__, sleaf->name); |
| 3406 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3407 | } |
| 3408 | |
| 3409 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 3410 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3411 | LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 3412 | sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3413 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3414 | 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] | 3415 | } |
| 3416 | } |
| 3417 | set_scnode_clear_ctx(set); |
| 3418 | return rc; |
| 3419 | } |
| 3420 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3421 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 3422 | LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "bit-is-set(node-set, string)"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3423 | return LY_EVALID; |
| 3424 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3425 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3426 | LY_CHECK_RET(rc); |
| 3427 | |
| 3428 | set_fill_boolean(set, 0); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3429 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3430 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3431 | if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && |
| 3432 | (((struct lysc_node_leaf *)leaf->schema)->type->basetype == LY_TYPE_BITS)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3433 | bits = (struct lysc_type_bits *)((struct lysc_node_leaf *)leaf->schema)->type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3434 | LY_ARRAY_FOR(bits->bits, u) { |
| 3435 | if (!strcmp(bits->bits[u].name, args[1]->val.str)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3436 | set_fill_boolean(set, 1); |
| 3437 | break; |
| 3438 | } |
| 3439 | } |
| 3440 | } |
| 3441 | } |
| 3442 | |
| 3443 | return LY_SUCCESS; |
| 3444 | } |
| 3445 | |
| 3446 | /** |
| 3447 | * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN |
| 3448 | * with the argument converted to boolean. |
| 3449 | * |
| 3450 | * @param[in] args Array of arguments. |
| 3451 | * @param[in] arg_count Count of elements in @p args. |
| 3452 | * @param[in,out] set Context and result set at the same time. |
| 3453 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3454 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3455 | */ |
| 3456 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3457 | 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] | 3458 | { |
| 3459 | LY_ERR rc; |
| 3460 | |
| 3461 | if (options & LYXP_SCNODE_ALL) { |
| 3462 | set_scnode_clear_ctx(set); |
| 3463 | return LY_SUCCESS; |
| 3464 | } |
| 3465 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3466 | rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3467 | LY_CHECK_RET(rc); |
| 3468 | set_fill_set(set, args[0]); |
| 3469 | |
| 3470 | return LY_SUCCESS; |
| 3471 | } |
| 3472 | |
| 3473 | /** |
| 3474 | * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER |
| 3475 | * with the first argument rounded up to the nearest integer. |
| 3476 | * |
| 3477 | * @param[in] args Array of arguments. |
| 3478 | * @param[in] arg_count Count of elements in @p args. |
| 3479 | * @param[in,out] set Context and result set at the same time. |
| 3480 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3481 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3482 | */ |
| 3483 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3484 | xpath_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] | 3485 | { |
| 3486 | struct lysc_node_leaf *sleaf; |
| 3487 | LY_ERR rc = LY_SUCCESS; |
| 3488 | |
| 3489 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3490 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3491 | LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__); |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3492 | } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3493 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3494 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 3495 | sleaf->name); |
| 3496 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) { |
| 3497 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name); |
| 3498 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3499 | } |
| 3500 | set_scnode_clear_ctx(set); |
| 3501 | return rc; |
| 3502 | } |
| 3503 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3504 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3505 | LY_CHECK_RET(rc); |
| 3506 | if ((long long)args[0]->val.num != args[0]->val.num) { |
| 3507 | set_fill_number(set, ((long long)args[0]->val.num) + 1); |
| 3508 | } else { |
| 3509 | set_fill_number(set, args[0]->val.num); |
| 3510 | } |
| 3511 | |
| 3512 | return LY_SUCCESS; |
| 3513 | } |
| 3514 | |
| 3515 | /** |
| 3516 | * @brief Execute the XPath concat(string, string, string*) function. |
| 3517 | * Returns LYXP_SET_STRING with the concatenation of all the arguments. |
| 3518 | * |
| 3519 | * @param[in] args Array of arguments. |
| 3520 | * @param[in] arg_count Count of elements in @p args. |
| 3521 | * @param[in,out] set Context and result set at the same time. |
| 3522 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3523 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3524 | */ |
| 3525 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3526 | 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] | 3527 | { |
| 3528 | uint16_t i; |
| 3529 | char *str = NULL; |
| 3530 | size_t used = 1; |
| 3531 | LY_ERR rc = LY_SUCCESS; |
| 3532 | struct lysc_node_leaf *sleaf; |
| 3533 | |
| 3534 | if (options & LYXP_SCNODE_ALL) { |
| 3535 | for (i = 0; i < arg_count; ++i) { |
| 3536 | if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) { |
| 3537 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3538 | LOGWRN(set->ctx, "Argument #%u of %s is a %s node \"%s\".", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 3539 | i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3540 | } else if (!warn_is_string_type(sleaf->type)) { |
Radek Krejci | 70124c8 | 2020-08-14 22:17:03 +0200 | [diff] [blame] | 3541 | 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] | 3542 | } |
| 3543 | } |
| 3544 | } |
| 3545 | set_scnode_clear_ctx(set); |
| 3546 | return rc; |
| 3547 | } |
| 3548 | |
| 3549 | for (i = 0; i < arg_count; ++i) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3550 | rc = lyxp_set_cast(args[i], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3551 | if (rc != LY_SUCCESS) { |
| 3552 | free(str); |
| 3553 | return rc; |
| 3554 | } |
| 3555 | |
| 3556 | str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char)); |
| 3557 | LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM); |
| 3558 | strcpy(str + used - 1, args[i]->val.str); |
| 3559 | used += strlen(args[i]->val.str); |
| 3560 | } |
| 3561 | |
| 3562 | /* free, kind of */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3563 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3564 | set->type = LYXP_SET_STRING; |
| 3565 | set->val.str = str; |
| 3566 | |
| 3567 | return LY_SUCCESS; |
| 3568 | } |
| 3569 | |
| 3570 | /** |
| 3571 | * @brief Execute the XPath contains(string, string) function. |
| 3572 | * Returns LYXP_SET_BOOLEAN whether the second argument can |
| 3573 | * be found in the first or not. |
| 3574 | * |
| 3575 | * @param[in] args Array of arguments. |
| 3576 | * @param[in] arg_count Count of elements in @p args. |
| 3577 | * @param[in,out] set Context and result set at the same time. |
| 3578 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3579 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3580 | */ |
| 3581 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3582 | 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] | 3583 | { |
| 3584 | struct lysc_node_leaf *sleaf; |
| 3585 | LY_ERR rc = LY_SUCCESS; |
| 3586 | |
| 3587 | if (options & LYXP_SCNODE_ALL) { |
| 3588 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3589 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3590 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 3591 | sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3592 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3593 | 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] | 3594 | } |
| 3595 | } |
| 3596 | |
| 3597 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 3598 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3599 | LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 3600 | sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3601 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3602 | 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] | 3603 | } |
| 3604 | } |
| 3605 | set_scnode_clear_ctx(set); |
| 3606 | return rc; |
| 3607 | } |
| 3608 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3609 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3610 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3611 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3612 | LY_CHECK_RET(rc); |
| 3613 | |
| 3614 | if (strstr(args[0]->val.str, args[1]->val.str)) { |
| 3615 | set_fill_boolean(set, 1); |
| 3616 | } else { |
| 3617 | set_fill_boolean(set, 0); |
| 3618 | } |
| 3619 | |
| 3620 | return LY_SUCCESS; |
| 3621 | } |
| 3622 | |
| 3623 | /** |
| 3624 | * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER |
| 3625 | * with the size of the node-set from the argument. |
| 3626 | * |
| 3627 | * @param[in] args Array of arguments. |
| 3628 | * @param[in] arg_count Count of elements in @p args. |
| 3629 | * @param[in,out] set Context and result set at the same time. |
| 3630 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3631 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3632 | */ |
| 3633 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3634 | 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] | 3635 | { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3636 | LY_ERR rc = LY_SUCCESS; |
| 3637 | |
| 3638 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3639 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3640 | 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] | 3641 | } |
| 3642 | set_scnode_clear_ctx(set); |
| 3643 | return rc; |
| 3644 | } |
| 3645 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3646 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 3647 | LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "count(node-set)"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3648 | return LY_EVALID; |
| 3649 | } |
| 3650 | |
| 3651 | set_fill_number(set, args[0]->used); |
| 3652 | return LY_SUCCESS; |
| 3653 | } |
| 3654 | |
| 3655 | /** |
| 3656 | * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET |
| 3657 | * with the context with the intial node. |
| 3658 | * |
| 3659 | * @param[in] args Array of arguments. |
| 3660 | * @param[in] arg_count Count of elements in @p args. |
| 3661 | * @param[in,out] set Context and result set at the same time. |
| 3662 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3663 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3664 | */ |
| 3665 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3666 | 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] | 3667 | { |
| 3668 | if (arg_count || args) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 3669 | LOGVAL(set->ctx, LY_VCODE_XP_INARGCOUNT, arg_count, "current()"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3670 | return LY_EVALID; |
| 3671 | } |
| 3672 | |
| 3673 | if (options & LYXP_SCNODE_ALL) { |
| 3674 | set_scnode_clear_ctx(set); |
| 3675 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 3676 | 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] | 3677 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3678 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3679 | |
| 3680 | /* position is filled later */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 3681 | set_insert_node(set, set->cur_node, 0, LYXP_NODE_ELEM, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3682 | } |
| 3683 | |
| 3684 | return LY_SUCCESS; |
| 3685 | } |
| 3686 | |
| 3687 | /** |
| 3688 | * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either |
| 3689 | * leafref or instance-identifier target node(s). |
| 3690 | * |
| 3691 | * @param[in] args Array of arguments. |
| 3692 | * @param[in] arg_count Count of elements in @p args. |
| 3693 | * @param[in,out] set Context and result set at the same time. |
| 3694 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3695 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3696 | */ |
| 3697 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3698 | 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] | 3699 | { |
| 3700 | struct lyd_node_term *leaf; |
Michal Vasko | 42e497c | 2020-01-06 08:38:25 +0100 | [diff] [blame] | 3701 | struct lysc_node_leaf *sleaf = NULL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3702 | struct lysc_type_leafref *lref; |
Michal Vasko | ae9e4cb | 2019-09-25 08:43:05 +0200 | [diff] [blame] | 3703 | const struct lysc_node *target; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3704 | struct ly_path *p; |
| 3705 | struct lyd_node *node; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3706 | char *errmsg = NULL; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 3707 | uint8_t oper; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3708 | LY_ERR rc = LY_SUCCESS; |
| 3709 | |
| 3710 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3711 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3712 | LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__); |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3713 | } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3714 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3715 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 3716 | sleaf->name); |
| 3717 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) && !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) { |
| 3718 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"leafref\" nor \"instance-identifier\".", |
| 3719 | __func__, sleaf->name); |
| 3720 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3721 | } |
| 3722 | set_scnode_clear_ctx(set); |
Michal Vasko | 42e497c | 2020-01-06 08:38:25 +0100 | [diff] [blame] | 3723 | if (sleaf && (sleaf->type->basetype == LY_TYPE_LEAFREF)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3724 | lref = (struct lysc_type_leafref *)sleaf->type; |
Michal Vasko | d1e53b9 | 2021-01-28 13:11:06 +0100 | [diff] [blame] | 3725 | oper = (sleaf->flags & LYS_IS_OUTPUT) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3726 | |
| 3727 | /* it was already evaluated on schema, it must succeed */ |
Radek Krejci | d5d3743 | 2021-03-12 13:46:40 +0100 | [diff] [blame] | 3728 | rc = ly_path_compile(set->ctx, lref->cur_mod, &sleaf->node, NULL, lref->path, LY_PATH_LREF_TRUE, oper, |
Michal Vasko | 14ed9cd | 2021-01-28 14:16:25 +0100 | [diff] [blame] | 3729 | LY_PATH_TARGET_MANY, LY_PREF_SCHEMA_RESOLVED, lref->prefixes, NULL, &p); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3730 | assert(!rc); |
| 3731 | |
| 3732 | /* get the target node */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3733 | target = p[LY_ARRAY_COUNT(p) - 1].node; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3734 | ly_path_free(set->ctx, p); |
| 3735 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 3736 | 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] | 3737 | } |
| 3738 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3739 | return rc; |
| 3740 | } |
| 3741 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3742 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 3743 | LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "deref(node-set)"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3744 | return LY_EVALID; |
| 3745 | } |
| 3746 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3747 | lyxp_set_free_content(set); |
| 3748 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3749 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
| 3750 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 3751 | if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 3752 | if (sleaf->type->basetype == LY_TYPE_LEAFREF) { |
| 3753 | /* find leafref target */ |
Radek Krejci | 0b01330 | 2021-03-29 15:22:32 +0200 | [diff] [blame] | 3754 | if (lyplg_type_resolve_leafref((struct lysc_type_leafref *)sleaf->type, &leaf->node, &leaf->value, set->tree, |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 3755 | &node, &errmsg)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3756 | LOGERR(set->ctx, LY_EVALID, errmsg); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3757 | free(errmsg); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3758 | return LY_EVALID; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3759 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3760 | } else { |
| 3761 | assert(sleaf->type->basetype == LY_TYPE_INST); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3762 | if (ly_path_eval(leaf->value.target, set->tree, &node)) { |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3763 | 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] | 3764 | LYD_CANON_VALUE(leaf)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3765 | return LY_EVALID; |
| 3766 | } |
| 3767 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3768 | |
| 3769 | /* insert it */ |
| 3770 | set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3771 | } |
| 3772 | } |
| 3773 | |
| 3774 | return LY_SUCCESS; |
| 3775 | } |
| 3776 | |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3777 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3778 | 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] | 3779 | { |
Michal Vasko | 1fdd8fa | 2021-01-08 09:21:45 +0100 | [diff] [blame] | 3780 | uint32_t i; |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3781 | LY_ARRAY_COUNT_TYPE u; |
| 3782 | struct lyd_node_term *leaf; |
| 3783 | struct lysc_node_leaf *sleaf; |
| 3784 | struct lyd_meta *meta; |
| 3785 | struct lyd_value data = {0}, *val; |
| 3786 | struct ly_err_item *err = NULL; |
| 3787 | LY_ERR rc = LY_SUCCESS; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 3788 | ly_bool found; |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3789 | |
| 3790 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3791 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3792 | LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", func); |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3793 | } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3794 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3795 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype), |
| 3796 | sleaf->name); |
| 3797 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) { |
| 3798 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", func, sleaf->name); |
| 3799 | } |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3800 | } |
| 3801 | |
| 3802 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 3803 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3804 | 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] | 3805 | sleaf->name); |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3806 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3807 | LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", func, sleaf->name); |
| 3808 | } |
| 3809 | } |
| 3810 | set_scnode_clear_ctx(set); |
| 3811 | return rc; |
| 3812 | } |
| 3813 | |
| 3814 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 3815 | LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "derived-from(-or-self)(node-set, string)"); |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3816 | return LY_EVALID; |
| 3817 | } |
| 3818 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
| 3819 | LY_CHECK_RET(rc); |
| 3820 | |
| 3821 | set_fill_boolean(set, 0); |
| 3822 | found = 0; |
| 3823 | for (i = 0; i < args[0]->used; ++i) { |
| 3824 | if ((args[0]->val.nodes[i].type != LYXP_NODE_ELEM) && (args[0]->val.nodes[i].type != LYXP_NODE_META)) { |
| 3825 | continue; |
| 3826 | } |
| 3827 | |
| 3828 | if (args[0]->val.nodes[i].type == LYXP_NODE_ELEM) { |
| 3829 | leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node; |
| 3830 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 3831 | val = &leaf->value; |
| 3832 | if (!(sleaf->nodetype & LYD_NODE_TERM) || (leaf->value.realtype->basetype != LY_TYPE_IDENT)) { |
| 3833 | /* uninteresting */ |
| 3834 | continue; |
| 3835 | } |
| 3836 | |
| 3837 | /* store args[1] as ident */ |
| 3838 | 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] | 3839 | 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] | 3840 | } else { |
| 3841 | meta = args[0]->val.meta[i].meta; |
| 3842 | val = &meta->value; |
| 3843 | if (val->realtype->basetype != LY_TYPE_IDENT) { |
| 3844 | /* uninteresting */ |
| 3845 | continue; |
| 3846 | } |
| 3847 | |
| 3848 | /* store args[1] as ident */ |
Michal Vasko | feca4fb | 2020-10-05 08:58:40 +0200 | [diff] [blame] | 3849 | 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] | 3850 | 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] | 3851 | } |
| 3852 | |
| 3853 | if (err) { |
Michal Vasko | 177d0ed | 2020-11-23 16:43:03 +0100 | [diff] [blame] | 3854 | ly_err_print(set->ctx, err); |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3855 | ly_err_free(err); |
| 3856 | } |
| 3857 | LY_CHECK_RET(rc); |
| 3858 | |
| 3859 | /* finally check the identity itself */ |
| 3860 | if (self_match && (data.ident == val->ident)) { |
| 3861 | set_fill_boolean(set, 1); |
| 3862 | found = 1; |
| 3863 | } |
| 3864 | if (!found) { |
| 3865 | LY_ARRAY_FOR(data.ident->derived, u) { |
| 3866 | if (data.ident->derived[u] == val->ident) { |
| 3867 | set_fill_boolean(set, 1); |
| 3868 | found = 1; |
| 3869 | break; |
| 3870 | } |
| 3871 | } |
| 3872 | } |
| 3873 | |
| 3874 | /* free temporary value */ |
| 3875 | val->realtype->plugin->free(set->ctx, &data); |
| 3876 | if (found) { |
| 3877 | break; |
| 3878 | } |
| 3879 | } |
| 3880 | |
| 3881 | return LY_SUCCESS; |
| 3882 | } |
| 3883 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3884 | /** |
| 3885 | * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending |
| 3886 | * on whether the first argument nodes contain a node of an identity derived from the second |
| 3887 | * argument identity. |
| 3888 | * |
| 3889 | * @param[in] args Array of arguments. |
| 3890 | * @param[in] arg_count Count of elements in @p args. |
| 3891 | * @param[in,out] set Context and result set at the same time. |
| 3892 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3893 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3894 | */ |
| 3895 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3896 | 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] | 3897 | { |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3898 | return xpath_derived_(args, set, options, 0, __func__); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3899 | } |
| 3900 | |
| 3901 | /** |
| 3902 | * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending |
| 3903 | * on whether the first argument nodes contain a node of an identity that either is or is derived from |
| 3904 | * the second argument identity. |
| 3905 | * |
| 3906 | * @param[in] args Array of arguments. |
| 3907 | * @param[in] arg_count Count of elements in @p args. |
| 3908 | * @param[in,out] set Context and result set at the same time. |
| 3909 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3910 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3911 | */ |
| 3912 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3913 | 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] | 3914 | { |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3915 | return xpath_derived_(args, set, options, 1, __func__); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3916 | } |
| 3917 | |
| 3918 | /** |
| 3919 | * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER |
| 3920 | * with the integer value of the first node's enum value, otherwise NaN. |
| 3921 | * |
| 3922 | * @param[in] args Array of arguments. |
| 3923 | * @param[in] arg_count Count of elements in @p args. |
| 3924 | * @param[in,out] set Context and result set at the same time. |
| 3925 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3926 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3927 | */ |
| 3928 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3929 | 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] | 3930 | { |
| 3931 | struct lyd_node_term *leaf; |
| 3932 | struct lysc_node_leaf *sleaf; |
| 3933 | LY_ERR rc = LY_SUCCESS; |
| 3934 | |
| 3935 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3936 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3937 | LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__); |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 3938 | } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3939 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3940 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 3941 | sleaf->name); |
| 3942 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) { |
| 3943 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"enumeration\".", __func__, sleaf->name); |
| 3944 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3945 | } |
| 3946 | set_scnode_clear_ctx(set); |
| 3947 | return rc; |
| 3948 | } |
| 3949 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3950 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 3951 | LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "enum-value(node-set)"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3952 | return LY_EVALID; |
| 3953 | } |
| 3954 | |
| 3955 | set_fill_number(set, NAN); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3956 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3957 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
| 3958 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 3959 | if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) { |
| 3960 | set_fill_number(set, leaf->value.enum_item->value); |
| 3961 | } |
| 3962 | } |
| 3963 | |
| 3964 | return LY_SUCCESS; |
| 3965 | } |
| 3966 | |
| 3967 | /** |
| 3968 | * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN |
| 3969 | * with false value. |
| 3970 | * |
| 3971 | * @param[in] args Array of arguments. |
| 3972 | * @param[in] arg_count Count of elements in @p args. |
| 3973 | * @param[in,out] set Context and result set at the same time. |
| 3974 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3975 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3976 | */ |
| 3977 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3978 | 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] | 3979 | { |
| 3980 | if (options & LYXP_SCNODE_ALL) { |
| 3981 | set_scnode_clear_ctx(set); |
| 3982 | return LY_SUCCESS; |
| 3983 | } |
| 3984 | |
| 3985 | set_fill_boolean(set, 0); |
| 3986 | return LY_SUCCESS; |
| 3987 | } |
| 3988 | |
| 3989 | /** |
| 3990 | * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER |
| 3991 | * with the first argument floored (truncated). |
| 3992 | * |
| 3993 | * @param[in] args Array of arguments. |
| 3994 | * @param[in] arg_count Count of elements in @p args. |
| 3995 | * @param[in,out] set Context and result set at the same time. |
| 3996 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3997 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3998 | */ |
| 3999 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4000 | 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] | 4001 | { |
| 4002 | LY_ERR rc; |
| 4003 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4004 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4005 | LY_CHECK_RET(rc); |
| 4006 | if (isfinite(args[0]->val.num)) { |
| 4007 | set_fill_number(set, (long long)args[0]->val.num); |
| 4008 | } |
| 4009 | |
| 4010 | return LY_SUCCESS; |
| 4011 | } |
| 4012 | |
| 4013 | /** |
| 4014 | * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN |
| 4015 | * whether the language of the text matches the one from the argument. |
| 4016 | * |
| 4017 | * @param[in] args Array of arguments. |
| 4018 | * @param[in] arg_count Count of elements in @p args. |
| 4019 | * @param[in,out] set Context and result set at the same time. |
| 4020 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4021 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4022 | */ |
| 4023 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4024 | 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] | 4025 | { |
| 4026 | const struct lyd_node *node; |
| 4027 | struct lysc_node_leaf *sleaf; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4028 | struct lyd_meta *meta = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4029 | const char *val; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4030 | LY_ERR rc = LY_SUCCESS; |
| 4031 | |
| 4032 | if (options & LYXP_SCNODE_ALL) { |
| 4033 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4034 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4035 | 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] | 4036 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4037 | 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] | 4038 | } |
| 4039 | } |
| 4040 | set_scnode_clear_ctx(set); |
| 4041 | return rc; |
| 4042 | } |
| 4043 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4044 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4045 | LY_CHECK_RET(rc); |
| 4046 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4047 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4048 | LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "lang(string)"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4049 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4050 | } else if (!set->used) { |
| 4051 | set_fill_boolean(set, 0); |
| 4052 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4053 | } |
| 4054 | |
| 4055 | switch (set->val.nodes[0].type) { |
| 4056 | case LYXP_NODE_ELEM: |
| 4057 | case LYXP_NODE_TEXT: |
| 4058 | node = set->val.nodes[0].node; |
| 4059 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4060 | case LYXP_NODE_META: |
| 4061 | node = set->val.meta[0].meta->parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4062 | break; |
| 4063 | default: |
| 4064 | /* nothing to do with roots */ |
| 4065 | set_fill_boolean(set, 0); |
| 4066 | return LY_SUCCESS; |
| 4067 | } |
| 4068 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4069 | /* find lang metadata */ |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 4070 | for ( ; node; node = lyd_parent(node)) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4071 | for (meta = node->meta; meta; meta = meta->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4072 | /* annotations */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4073 | 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] | 4074 | break; |
| 4075 | } |
| 4076 | } |
| 4077 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4078 | if (meta) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4079 | break; |
| 4080 | } |
| 4081 | } |
| 4082 | |
| 4083 | /* compare languages */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4084 | if (!meta) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4085 | set_fill_boolean(set, 0); |
| 4086 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4087 | uint64_t i; |
| 4088 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 4089 | val = meta->value.canonical; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4090 | for (i = 0; args[0]->val.str[i]; ++i) { |
| 4091 | if (tolower(args[0]->val.str[i]) != tolower(val[i])) { |
| 4092 | set_fill_boolean(set, 0); |
| 4093 | break; |
| 4094 | } |
| 4095 | } |
| 4096 | if (!args[0]->val.str[i]) { |
| 4097 | if (!val[i] || (val[i] == '-')) { |
| 4098 | set_fill_boolean(set, 1); |
| 4099 | } else { |
| 4100 | set_fill_boolean(set, 0); |
| 4101 | } |
| 4102 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4103 | } |
| 4104 | |
| 4105 | return LY_SUCCESS; |
| 4106 | } |
| 4107 | |
| 4108 | /** |
| 4109 | * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER |
| 4110 | * with the context size. |
| 4111 | * |
| 4112 | * @param[in] args Array of arguments. |
| 4113 | * @param[in] arg_count Count of elements in @p args. |
| 4114 | * @param[in,out] set Context and result set at the same time. |
| 4115 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4116 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4117 | */ |
| 4118 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4119 | 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] | 4120 | { |
| 4121 | if (options & LYXP_SCNODE_ALL) { |
| 4122 | set_scnode_clear_ctx(set); |
| 4123 | return LY_SUCCESS; |
| 4124 | } |
| 4125 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4126 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4127 | LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "last()"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4128 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4129 | } else if (!set->used) { |
| 4130 | set_fill_number(set, 0); |
| 4131 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4132 | } |
| 4133 | |
| 4134 | set_fill_number(set, set->ctx_size); |
| 4135 | return LY_SUCCESS; |
| 4136 | } |
| 4137 | |
| 4138 | /** |
| 4139 | * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING |
| 4140 | * with the node name without namespace from the argument or the context. |
| 4141 | * |
| 4142 | * @param[in] args Array of arguments. |
| 4143 | * @param[in] arg_count Count of elements in @p args. |
| 4144 | * @param[in,out] set Context and result set at the same time. |
| 4145 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4146 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4147 | */ |
| 4148 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4149 | 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] | 4150 | { |
| 4151 | struct lyxp_set_node *item; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 4152 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4153 | /* suppress unused variable warning */ |
| 4154 | (void)options; |
| 4155 | |
| 4156 | if (options & LYXP_SCNODE_ALL) { |
| 4157 | set_scnode_clear_ctx(set); |
| 4158 | return LY_SUCCESS; |
| 4159 | } |
| 4160 | |
| 4161 | if (arg_count) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4162 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4163 | LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 4164 | "local-name(node-set?)"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4165 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4166 | } else if (!args[0]->used) { |
| 4167 | set_fill_string(set, "", 0); |
| 4168 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4169 | } |
| 4170 | |
| 4171 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4172 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4173 | |
| 4174 | item = &args[0]->val.nodes[0]; |
| 4175 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4176 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4177 | LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "local-name(node-set?)"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4178 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4179 | } else if (!set->used) { |
| 4180 | set_fill_string(set, "", 0); |
| 4181 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4182 | } |
| 4183 | |
| 4184 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4185 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4186 | |
| 4187 | item = &set->val.nodes[0]; |
| 4188 | } |
| 4189 | |
| 4190 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4191 | case LYXP_NODE_NONE: |
| 4192 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4193 | case LYXP_NODE_ROOT: |
| 4194 | case LYXP_NODE_ROOT_CONFIG: |
| 4195 | case LYXP_NODE_TEXT: |
| 4196 | set_fill_string(set, "", 0); |
| 4197 | break; |
| 4198 | case LYXP_NODE_ELEM: |
| 4199 | set_fill_string(set, item->node->schema->name, strlen(item->node->schema->name)); |
| 4200 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4201 | case LYXP_NODE_META: |
| 4202 | 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] | 4203 | break; |
| 4204 | } |
| 4205 | |
| 4206 | return LY_SUCCESS; |
| 4207 | } |
| 4208 | |
| 4209 | /** |
| 4210 | * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING |
| 4211 | * with the node name fully qualified (with namespace) from the argument or the context. |
| 4212 | * |
| 4213 | * @param[in] args Array of arguments. |
| 4214 | * @param[in] arg_count Count of elements in @p args. |
| 4215 | * @param[in,out] set Context and result set at the same time. |
| 4216 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4217 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4218 | */ |
| 4219 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4220 | 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] | 4221 | { |
| 4222 | struct lyxp_set_node *item; |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 4223 | struct lys_module *mod = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4224 | char *str; |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 4225 | const char *name = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4226 | |
| 4227 | if (options & LYXP_SCNODE_ALL) { |
| 4228 | set_scnode_clear_ctx(set); |
| 4229 | return LY_SUCCESS; |
| 4230 | } |
| 4231 | |
| 4232 | if (arg_count) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4233 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4234 | LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "name(node-set?)"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4235 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4236 | } else if (!args[0]->used) { |
| 4237 | set_fill_string(set, "", 0); |
| 4238 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4239 | } |
| 4240 | |
| 4241 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4242 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4243 | |
| 4244 | item = &args[0]->val.nodes[0]; |
| 4245 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4246 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4247 | LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "name(node-set?)"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4248 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4249 | } else if (!set->used) { |
| 4250 | set_fill_string(set, "", 0); |
| 4251 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4252 | } |
| 4253 | |
| 4254 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4255 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4256 | |
| 4257 | item = &set->val.nodes[0]; |
| 4258 | } |
| 4259 | |
| 4260 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4261 | case LYXP_NODE_NONE: |
| 4262 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4263 | case LYXP_NODE_ROOT: |
| 4264 | case LYXP_NODE_ROOT_CONFIG: |
| 4265 | case LYXP_NODE_TEXT: |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 4266 | /* keep NULL */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4267 | break; |
| 4268 | case LYXP_NODE_ELEM: |
| 4269 | mod = item->node->schema->module; |
| 4270 | name = item->node->schema->name; |
| 4271 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4272 | case LYXP_NODE_META: |
| 4273 | mod = ((struct lyd_meta *)item->node)->annotation->module; |
| 4274 | name = ((struct lyd_meta *)item->node)->name; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4275 | break; |
| 4276 | } |
| 4277 | |
| 4278 | if (mod && name) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 4279 | 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] | 4280 | LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM); |
| 4281 | set_fill_string(set, str, strlen(str)); |
| 4282 | free(str); |
| 4283 | } else { |
| 4284 | set_fill_string(set, "", 0); |
| 4285 | } |
| 4286 | |
| 4287 | return LY_SUCCESS; |
| 4288 | } |
| 4289 | |
| 4290 | /** |
| 4291 | * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING |
| 4292 | * with the namespace of the node from the argument or the context. |
| 4293 | * |
| 4294 | * @param[in] args Array of arguments. |
| 4295 | * @param[in] arg_count Count of elements in @p args. |
| 4296 | * @param[in,out] set Context and result set at the same time. |
| 4297 | * @param[in] options XPath options. |
| 4298 | * @return LY_ERR (LY_EINVAL for wrong arguments on schema) |
| 4299 | */ |
| 4300 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4301 | 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] | 4302 | { |
| 4303 | struct lyxp_set_node *item; |
| 4304 | struct lys_module *mod; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 4305 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4306 | /* suppress unused variable warning */ |
| 4307 | (void)options; |
| 4308 | |
| 4309 | if (options & LYXP_SCNODE_ALL) { |
| 4310 | set_scnode_clear_ctx(set); |
| 4311 | return LY_SUCCESS; |
| 4312 | } |
| 4313 | |
| 4314 | if (arg_count) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4315 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4316 | LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 4317 | "namespace-uri(node-set?)"); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4318 | return LY_EVALID; |
| 4319 | } else if (!args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4320 | set_fill_string(set, "", 0); |
| 4321 | return LY_SUCCESS; |
| 4322 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4323 | |
| 4324 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4325 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4326 | |
| 4327 | item = &args[0]->val.nodes[0]; |
| 4328 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4329 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4330 | LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "namespace-uri(node-set?)"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4331 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4332 | } else if (!set->used) { |
| 4333 | set_fill_string(set, "", 0); |
| 4334 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4335 | } |
| 4336 | |
| 4337 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4338 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4339 | |
| 4340 | item = &set->val.nodes[0]; |
| 4341 | } |
| 4342 | |
| 4343 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4344 | case LYXP_NODE_NONE: |
| 4345 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4346 | case LYXP_NODE_ROOT: |
| 4347 | case LYXP_NODE_ROOT_CONFIG: |
| 4348 | case LYXP_NODE_TEXT: |
| 4349 | set_fill_string(set, "", 0); |
| 4350 | break; |
| 4351 | case LYXP_NODE_ELEM: |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4352 | case LYXP_NODE_META: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4353 | if (item->type == LYXP_NODE_ELEM) { |
| 4354 | mod = item->node->schema->module; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4355 | } else { /* LYXP_NODE_META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4356 | /* annotations */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4357 | mod = ((struct lyd_meta *)item->node)->annotation->module; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4358 | } |
| 4359 | |
| 4360 | set_fill_string(set, mod->ns, strlen(mod->ns)); |
| 4361 | break; |
| 4362 | } |
| 4363 | |
| 4364 | return LY_SUCCESS; |
| 4365 | } |
| 4366 | |
| 4367 | /** |
| 4368 | * @brief Execute the XPath node() function (node type). Returns LYXP_SET_NODE_SET |
| 4369 | * with only nodes from the context. In practice it either leaves the context |
| 4370 | * as it is or returns an empty node set. |
| 4371 | * |
| 4372 | * @param[in] args Array of arguments. |
| 4373 | * @param[in] arg_count Count of elements in @p args. |
| 4374 | * @param[in,out] set Context and result set at the same time. |
| 4375 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4376 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4377 | */ |
| 4378 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4379 | 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] | 4380 | { |
| 4381 | if (options & LYXP_SCNODE_ALL) { |
| 4382 | set_scnode_clear_ctx(set); |
| 4383 | return LY_SUCCESS; |
| 4384 | } |
| 4385 | |
| 4386 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4387 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4388 | } |
| 4389 | return LY_SUCCESS; |
| 4390 | } |
| 4391 | |
| 4392 | /** |
| 4393 | * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING |
| 4394 | * with normalized value (no leading, trailing, double white spaces) of the node |
| 4395 | * from the argument or the context. |
| 4396 | * |
| 4397 | * @param[in] args Array of arguments. |
| 4398 | * @param[in] arg_count Count of elements in @p args. |
| 4399 | * @param[in,out] set Context and result set at the same time. |
| 4400 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4401 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4402 | */ |
| 4403 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4404 | 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] | 4405 | { |
| 4406 | uint16_t i, new_used; |
| 4407 | char *new; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 4408 | ly_bool have_spaces = 0, space_before = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4409 | struct lysc_node_leaf *sleaf; |
| 4410 | LY_ERR rc = LY_SUCCESS; |
| 4411 | |
| 4412 | if (options & LYXP_SCNODE_ALL) { |
| 4413 | if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4414 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4415 | 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] | 4416 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4417 | 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] | 4418 | } |
| 4419 | } |
| 4420 | set_scnode_clear_ctx(set); |
| 4421 | return rc; |
| 4422 | } |
| 4423 | |
| 4424 | if (arg_count) { |
| 4425 | set_fill_set(set, args[0]); |
| 4426 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4427 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4428 | LY_CHECK_RET(rc); |
| 4429 | |
| 4430 | /* is there any normalization necessary? */ |
| 4431 | for (i = 0; set->val.str[i]; ++i) { |
| 4432 | if (is_xmlws(set->val.str[i])) { |
| 4433 | if ((i == 0) || space_before || (!set->val.str[i + 1])) { |
| 4434 | have_spaces = 1; |
| 4435 | break; |
| 4436 | } |
| 4437 | space_before = 1; |
| 4438 | } else { |
| 4439 | space_before = 0; |
| 4440 | } |
| 4441 | } |
| 4442 | |
| 4443 | /* yep, there is */ |
| 4444 | if (have_spaces) { |
| 4445 | /* it's enough, at least one character will go, makes space for ending '\0' */ |
| 4446 | new = malloc(strlen(set->val.str) * sizeof(char)); |
| 4447 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 4448 | new_used = 0; |
| 4449 | |
| 4450 | space_before = 0; |
| 4451 | for (i = 0; set->val.str[i]; ++i) { |
| 4452 | if (is_xmlws(set->val.str[i])) { |
| 4453 | if ((i == 0) || space_before) { |
| 4454 | space_before = 1; |
| 4455 | continue; |
| 4456 | } else { |
| 4457 | space_before = 1; |
| 4458 | } |
| 4459 | } else { |
| 4460 | space_before = 0; |
| 4461 | } |
| 4462 | |
| 4463 | new[new_used] = (space_before ? ' ' : set->val.str[i]); |
| 4464 | ++new_used; |
| 4465 | } |
| 4466 | |
| 4467 | /* at worst there is one trailing space now */ |
| 4468 | if (new_used && is_xmlws(new[new_used - 1])) { |
| 4469 | --new_used; |
| 4470 | } |
| 4471 | |
| 4472 | new = ly_realloc(new, (new_used + 1) * sizeof(char)); |
| 4473 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 4474 | new[new_used] = '\0'; |
| 4475 | |
| 4476 | free(set->val.str); |
| 4477 | set->val.str = new; |
| 4478 | } |
| 4479 | |
| 4480 | return LY_SUCCESS; |
| 4481 | } |
| 4482 | |
| 4483 | /** |
| 4484 | * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN |
| 4485 | * with the argument converted to boolean and logically inverted. |
| 4486 | * |
| 4487 | * @param[in] args Array of arguments. |
| 4488 | * @param[in] arg_count Count of elements in @p args. |
| 4489 | * @param[in,out] set Context and result set at the same time. |
| 4490 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4491 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4492 | */ |
| 4493 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4494 | xpath_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] | 4495 | { |
| 4496 | if (options & LYXP_SCNODE_ALL) { |
| 4497 | set_scnode_clear_ctx(set); |
| 4498 | return LY_SUCCESS; |
| 4499 | } |
| 4500 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4501 | lyxp_set_cast(args[0], LYXP_SET_BOOLEAN); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4502 | if (args[0]->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4503 | set_fill_boolean(set, 0); |
| 4504 | } else { |
| 4505 | set_fill_boolean(set, 1); |
| 4506 | } |
| 4507 | |
| 4508 | return LY_SUCCESS; |
| 4509 | } |
| 4510 | |
| 4511 | /** |
| 4512 | * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER |
| 4513 | * with the number representation of either the argument or the context. |
| 4514 | * |
| 4515 | * @param[in] args Array of arguments. |
| 4516 | * @param[in] arg_count Count of elements in @p args. |
| 4517 | * @param[in,out] set Context and result set at the same time. |
| 4518 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4519 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4520 | */ |
| 4521 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4522 | 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] | 4523 | { |
| 4524 | LY_ERR rc; |
| 4525 | |
| 4526 | if (options & LYXP_SCNODE_ALL) { |
| 4527 | set_scnode_clear_ctx(set); |
| 4528 | return LY_SUCCESS; |
| 4529 | } |
| 4530 | |
| 4531 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4532 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4533 | LY_CHECK_RET(rc); |
| 4534 | set_fill_set(set, args[0]); |
| 4535 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4536 | rc = lyxp_set_cast(set, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4537 | LY_CHECK_RET(rc); |
| 4538 | } |
| 4539 | |
| 4540 | return LY_SUCCESS; |
| 4541 | } |
| 4542 | |
| 4543 | /** |
| 4544 | * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER |
| 4545 | * with the context position. |
| 4546 | * |
| 4547 | * @param[in] args Array of arguments. |
| 4548 | * @param[in] arg_count Count of elements in @p args. |
| 4549 | * @param[in,out] set Context and result set at the same time. |
| 4550 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4551 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4552 | */ |
| 4553 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4554 | 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] | 4555 | { |
| 4556 | if (options & LYXP_SCNODE_ALL) { |
| 4557 | set_scnode_clear_ctx(set); |
| 4558 | return LY_SUCCESS; |
| 4559 | } |
| 4560 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4561 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4562 | LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "position()"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4563 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4564 | } else if (!set->used) { |
| 4565 | set_fill_number(set, 0); |
| 4566 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4567 | } |
| 4568 | |
| 4569 | set_fill_number(set, set->ctx_pos); |
| 4570 | |
| 4571 | /* UNUSED in 'Release' build type */ |
| 4572 | (void)options; |
| 4573 | return LY_SUCCESS; |
| 4574 | } |
| 4575 | |
| 4576 | /** |
| 4577 | * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN |
| 4578 | * depending on whether the second argument regex matches the first argument string. For details refer to |
| 4579 | * YANG 1.1 RFC section 10.2.1. |
| 4580 | * |
| 4581 | * @param[in] args Array of arguments. |
| 4582 | * @param[in] arg_count Count of elements in @p args. |
| 4583 | * @param[in,out] set Context and result set at the same time. |
| 4584 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4585 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4586 | */ |
| 4587 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4588 | 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] | 4589 | { |
| 4590 | struct lysc_pattern **patterns = NULL, **pattern; |
| 4591 | struct lysc_node_leaf *sleaf; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4592 | LY_ERR rc = LY_SUCCESS; |
| 4593 | struct ly_err_item *err; |
| 4594 | |
| 4595 | if (options & LYXP_SCNODE_ALL) { |
| 4596 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4597 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4598 | 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] | 4599 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4600 | 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] | 4601 | } |
| 4602 | } |
| 4603 | |
| 4604 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4605 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4606 | 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] | 4607 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4608 | 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] | 4609 | } |
| 4610 | } |
| 4611 | set_scnode_clear_ctx(set); |
| 4612 | return rc; |
| 4613 | } |
| 4614 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4615 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4616 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4617 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4618 | LY_CHECK_RET(rc); |
| 4619 | |
| 4620 | LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM); |
Radek IÅ¡a | 45802b5 | 2021-02-09 09:21:58 +0100 | [diff] [blame] | 4621 | *pattern = calloc(1, sizeof **pattern); |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 4622 | LOG_LOCSET(NULL, set->cur_node, NULL, NULL); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 4623 | rc = lys_compile_type_pattern_check(set->ctx, args[1]->val.str, &(*pattern)->code); |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 4624 | LOG_LOCBACK(0, 1, 0, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4625 | if (rc != LY_SUCCESS) { |
| 4626 | LY_ARRAY_FREE(patterns); |
| 4627 | return rc; |
| 4628 | } |
| 4629 | |
Radek Krejci | 0b01330 | 2021-03-29 15:22:32 +0200 | [diff] [blame] | 4630 | rc = lyplg_type_validate_patterns(patterns, args[0]->val.str, strlen(args[0]->val.str), &err); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4631 | pcre2_code_free((*pattern)->code); |
| 4632 | free(*pattern); |
| 4633 | LY_ARRAY_FREE(patterns); |
| 4634 | if (rc && (rc != LY_EVALID)) { |
Michal Vasko | 177d0ed | 2020-11-23 16:43:03 +0100 | [diff] [blame] | 4635 | ly_err_print(set->ctx, err); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4636 | ly_err_free(err); |
| 4637 | return rc; |
| 4638 | } |
| 4639 | |
| 4640 | if (rc == LY_EVALID) { |
| 4641 | ly_err_free(err); |
| 4642 | set_fill_boolean(set, 0); |
| 4643 | } else { |
| 4644 | set_fill_boolean(set, 1); |
| 4645 | } |
| 4646 | |
| 4647 | return LY_SUCCESS; |
| 4648 | } |
| 4649 | |
| 4650 | /** |
| 4651 | * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER |
| 4652 | * with the rounded first argument. For details refer to |
| 4653 | * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round. |
| 4654 | * |
| 4655 | * @param[in] args Array of arguments. |
| 4656 | * @param[in] arg_count Count of elements in @p args. |
| 4657 | * @param[in,out] set Context and result set at the same time. |
| 4658 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4659 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4660 | */ |
| 4661 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4662 | 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] | 4663 | { |
| 4664 | struct lysc_node_leaf *sleaf; |
| 4665 | LY_ERR rc = LY_SUCCESS; |
| 4666 | |
| 4667 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 4668 | if (args[0]->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4669 | LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__); |
Michal Vasko | 5676f4e | 2021-04-06 17:14:45 +0200 | [diff] [blame] | 4670 | } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4671 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4672 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), |
| 4673 | sleaf->name); |
| 4674 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) { |
| 4675 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name); |
| 4676 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4677 | } |
| 4678 | set_scnode_clear_ctx(set); |
| 4679 | return rc; |
| 4680 | } |
| 4681 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4682 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4683 | LY_CHECK_RET(rc); |
| 4684 | |
| 4685 | /* cover only the cases where floor can't be used */ |
| 4686 | if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) { |
| 4687 | set_fill_number(set, -0.0f); |
| 4688 | } else { |
| 4689 | args[0]->val.num += 0.5; |
| 4690 | rc = xpath_floor(args, 1, args[0], options); |
| 4691 | LY_CHECK_RET(rc); |
| 4692 | set_fill_number(set, args[0]->val.num); |
| 4693 | } |
| 4694 | |
| 4695 | return LY_SUCCESS; |
| 4696 | } |
| 4697 | |
| 4698 | /** |
| 4699 | * @brief Execute the XPath starts-with(string, string) function. |
| 4700 | * Returns LYXP_SET_BOOLEAN whether the second argument is |
| 4701 | * the prefix of the first or not. |
| 4702 | * |
| 4703 | * @param[in] args Array of arguments. |
| 4704 | * @param[in] arg_count Count of elements in @p args. |
| 4705 | * @param[in,out] set Context and result set at the same time. |
| 4706 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4707 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4708 | */ |
| 4709 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4710 | 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] | 4711 | { |
| 4712 | struct lysc_node_leaf *sleaf; |
| 4713 | LY_ERR rc = LY_SUCCESS; |
| 4714 | |
| 4715 | if (options & LYXP_SCNODE_ALL) { |
| 4716 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4717 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4718 | 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] | 4719 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4720 | 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] | 4721 | } |
| 4722 | } |
| 4723 | |
| 4724 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4725 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4726 | 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] | 4727 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4728 | 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] | 4729 | } |
| 4730 | } |
| 4731 | set_scnode_clear_ctx(set); |
| 4732 | return rc; |
| 4733 | } |
| 4734 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4735 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4736 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4737 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4738 | LY_CHECK_RET(rc); |
| 4739 | |
| 4740 | if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) { |
| 4741 | set_fill_boolean(set, 0); |
| 4742 | } else { |
| 4743 | set_fill_boolean(set, 1); |
| 4744 | } |
| 4745 | |
| 4746 | return LY_SUCCESS; |
| 4747 | } |
| 4748 | |
| 4749 | /** |
| 4750 | * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING |
| 4751 | * with the string representation of either the argument or the context. |
| 4752 | * |
| 4753 | * @param[in] args Array of arguments. |
| 4754 | * @param[in] arg_count Count of elements in @p args. |
| 4755 | * @param[in,out] set Context and result set at the same time. |
| 4756 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4757 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4758 | */ |
| 4759 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4760 | xpath_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] | 4761 | { |
| 4762 | LY_ERR rc; |
| 4763 | |
| 4764 | if (options & LYXP_SCNODE_ALL) { |
| 4765 | set_scnode_clear_ctx(set); |
| 4766 | return LY_SUCCESS; |
| 4767 | } |
| 4768 | |
| 4769 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4770 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4771 | LY_CHECK_RET(rc); |
| 4772 | set_fill_set(set, args[0]); |
| 4773 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4774 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4775 | LY_CHECK_RET(rc); |
| 4776 | } |
| 4777 | |
| 4778 | return LY_SUCCESS; |
| 4779 | } |
| 4780 | |
| 4781 | /** |
| 4782 | * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER |
| 4783 | * with the length of the string in either the argument or the context. |
| 4784 | * |
| 4785 | * @param[in] args Array of arguments. |
| 4786 | * @param[in] arg_count Count of elements in @p args. |
| 4787 | * @param[in,out] set Context and result set at the same time. |
| 4788 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4789 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4790 | */ |
| 4791 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4792 | 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] | 4793 | { |
| 4794 | struct lysc_node_leaf *sleaf; |
| 4795 | LY_ERR rc = LY_SUCCESS; |
| 4796 | |
| 4797 | if (options & LYXP_SCNODE_ALL) { |
| 4798 | if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4799 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4800 | 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] | 4801 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4802 | 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] | 4803 | } |
| 4804 | } |
| 4805 | if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) { |
| 4806 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4807 | 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] | 4808 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4809 | 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] | 4810 | } |
| 4811 | } |
| 4812 | set_scnode_clear_ctx(set); |
| 4813 | return rc; |
| 4814 | } |
| 4815 | |
| 4816 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4817 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4818 | LY_CHECK_RET(rc); |
| 4819 | set_fill_number(set, strlen(args[0]->val.str)); |
| 4820 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4821 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4822 | LY_CHECK_RET(rc); |
| 4823 | set_fill_number(set, strlen(set->val.str)); |
| 4824 | } |
| 4825 | |
| 4826 | return LY_SUCCESS; |
| 4827 | } |
| 4828 | |
| 4829 | /** |
| 4830 | * @brief Execute the XPath substring(string, number, number?) function. |
| 4831 | * Returns LYXP_SET_STRING substring of the first argument starting |
| 4832 | * on the second argument index ending on the third argument index, |
| 4833 | * indexed from 1. For exact definition refer to |
| 4834 | * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring. |
| 4835 | * |
| 4836 | * @param[in] args Array of arguments. |
| 4837 | * @param[in] arg_count Count of elements in @p args. |
| 4838 | * @param[in,out] set Context and result set at the same time. |
| 4839 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4840 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4841 | */ |
| 4842 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4843 | 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] | 4844 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4845 | int32_t start, len; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4846 | uint16_t str_start, str_len, pos; |
| 4847 | struct lysc_node_leaf *sleaf; |
| 4848 | LY_ERR rc = LY_SUCCESS; |
| 4849 | |
| 4850 | if (options & LYXP_SCNODE_ALL) { |
| 4851 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4852 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4853 | 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] | 4854 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4855 | 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] | 4856 | } |
| 4857 | } |
| 4858 | |
| 4859 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4860 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4861 | 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] | 4862 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 4863 | 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] | 4864 | } |
| 4865 | } |
| 4866 | |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 4867 | if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET) && |
| 4868 | (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4869 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4870 | 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] | 4871 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 4872 | 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] | 4873 | } |
| 4874 | } |
| 4875 | set_scnode_clear_ctx(set); |
| 4876 | return rc; |
| 4877 | } |
| 4878 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4879 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4880 | LY_CHECK_RET(rc); |
| 4881 | |
| 4882 | /* start */ |
| 4883 | if (xpath_round(&args[1], 1, args[1], options)) { |
| 4884 | return -1; |
| 4885 | } |
| 4886 | if (isfinite(args[1]->val.num)) { |
| 4887 | start = args[1]->val.num - 1; |
| 4888 | } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4889 | start = INT32_MIN; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4890 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4891 | start = INT32_MAX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4892 | } |
| 4893 | |
| 4894 | /* len */ |
| 4895 | if (arg_count == 3) { |
| 4896 | rc = xpath_round(&args[2], 1, args[2], options); |
| 4897 | LY_CHECK_RET(rc); |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4898 | if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4899 | len = 0; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4900 | } else if (isfinite(args[2]->val.num)) { |
| 4901 | len = args[2]->val.num; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4902 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4903 | len = INT32_MAX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4904 | } |
| 4905 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4906 | len = INT32_MAX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4907 | } |
| 4908 | |
| 4909 | /* find matching character positions */ |
| 4910 | str_start = 0; |
| 4911 | str_len = 0; |
| 4912 | for (pos = 0; args[0]->val.str[pos]; ++pos) { |
| 4913 | if (pos < start) { |
| 4914 | ++str_start; |
| 4915 | } else if (pos < start + len) { |
| 4916 | ++str_len; |
| 4917 | } else { |
| 4918 | break; |
| 4919 | } |
| 4920 | } |
| 4921 | |
| 4922 | set_fill_string(set, args[0]->val.str + str_start, str_len); |
| 4923 | return LY_SUCCESS; |
| 4924 | } |
| 4925 | |
| 4926 | /** |
| 4927 | * @brief Execute the XPath substring-after(string, string) function. |
| 4928 | * Returns LYXP_SET_STRING with the string succeeding the occurance |
| 4929 | * of the second argument in the first or an empty string. |
| 4930 | * |
| 4931 | * @param[in] args Array of arguments. |
| 4932 | * @param[in] arg_count Count of elements in @p args. |
| 4933 | * @param[in,out] set Context and result set at the same time. |
| 4934 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4935 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4936 | */ |
| 4937 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4938 | 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] | 4939 | { |
| 4940 | char *ptr; |
| 4941 | struct lysc_node_leaf *sleaf; |
| 4942 | LY_ERR rc = LY_SUCCESS; |
| 4943 | |
| 4944 | if (options & LYXP_SCNODE_ALL) { |
| 4945 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4946 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4947 | 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] | 4948 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4949 | 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] | 4950 | } |
| 4951 | } |
| 4952 | |
| 4953 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4954 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4955 | 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] | 4956 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4957 | 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] | 4958 | } |
| 4959 | } |
| 4960 | set_scnode_clear_ctx(set); |
| 4961 | return rc; |
| 4962 | } |
| 4963 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4964 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4965 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4966 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4967 | LY_CHECK_RET(rc); |
| 4968 | |
| 4969 | ptr = strstr(args[0]->val.str, args[1]->val.str); |
| 4970 | if (ptr) { |
| 4971 | set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str))); |
| 4972 | } else { |
| 4973 | set_fill_string(set, "", 0); |
| 4974 | } |
| 4975 | |
| 4976 | return LY_SUCCESS; |
| 4977 | } |
| 4978 | |
| 4979 | /** |
| 4980 | * @brief Execute the XPath substring-before(string, string) function. |
| 4981 | * Returns LYXP_SET_STRING with the string preceding the occurance |
| 4982 | * of the second argument in the first or an empty string. |
| 4983 | * |
| 4984 | * @param[in] args Array of arguments. |
| 4985 | * @param[in] arg_count Count of elements in @p args. |
| 4986 | * @param[in,out] set Context and result set at the same time. |
| 4987 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4988 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4989 | */ |
| 4990 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4991 | 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] | 4992 | { |
| 4993 | char *ptr; |
| 4994 | struct lysc_node_leaf *sleaf; |
| 4995 | LY_ERR rc = LY_SUCCESS; |
| 4996 | |
| 4997 | if (options & LYXP_SCNODE_ALL) { |
| 4998 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4999 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5000 | 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] | 5001 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5002 | 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] | 5003 | } |
| 5004 | } |
| 5005 | |
| 5006 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 5007 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5008 | 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] | 5009 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5010 | 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] | 5011 | } |
| 5012 | } |
| 5013 | set_scnode_clear_ctx(set); |
| 5014 | return rc; |
| 5015 | } |
| 5016 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5017 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5018 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5019 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5020 | LY_CHECK_RET(rc); |
| 5021 | |
| 5022 | ptr = strstr(args[0]->val.str, args[1]->val.str); |
| 5023 | if (ptr) { |
| 5024 | set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str); |
| 5025 | } else { |
| 5026 | set_fill_string(set, "", 0); |
| 5027 | } |
| 5028 | |
| 5029 | return LY_SUCCESS; |
| 5030 | } |
| 5031 | |
| 5032 | /** |
| 5033 | * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER |
| 5034 | * with the sum of all the nodes in the context. |
| 5035 | * |
| 5036 | * @param[in] args Array of arguments. |
| 5037 | * @param[in] arg_count Count of elements in @p args. |
| 5038 | * @param[in,out] set Context and result set at the same time. |
| 5039 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5040 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5041 | */ |
| 5042 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5043 | xpath_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] | 5044 | { |
| 5045 | long double num; |
| 5046 | char *str; |
Michal Vasko | 1fdd8fa | 2021-01-08 09:21:45 +0100 | [diff] [blame] | 5047 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5048 | struct lyxp_set set_item; |
| 5049 | struct lysc_node_leaf *sleaf; |
| 5050 | LY_ERR rc = LY_SUCCESS; |
| 5051 | |
| 5052 | if (options & LYXP_SCNODE_ALL) { |
| 5053 | if (args[0]->type == LYXP_SET_SCNODE_SET) { |
| 5054 | for (i = 0; i < args[0]->used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 5055 | 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] | 5056 | sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode; |
| 5057 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5058 | 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] | 5059 | lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5060 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 5061 | 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] | 5062 | } |
| 5063 | } |
| 5064 | } |
| 5065 | } |
| 5066 | set_scnode_clear_ctx(set); |
| 5067 | return rc; |
| 5068 | } |
| 5069 | |
| 5070 | set_fill_number(set, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5071 | |
| 5072 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 5073 | LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "sum(node-set)"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5074 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5075 | } else if (!args[0]->used) { |
| 5076 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5077 | } |
| 5078 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5079 | set_init(&set_item, set); |
| 5080 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5081 | set_item.type = LYXP_SET_NODE_SET; |
| 5082 | set_item.val.nodes = malloc(sizeof *set_item.val.nodes); |
| 5083 | LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM); |
| 5084 | |
| 5085 | set_item.used = 1; |
| 5086 | set_item.size = 1; |
| 5087 | |
| 5088 | for (i = 0; i < args[0]->used; ++i) { |
| 5089 | set_item.val.nodes[0] = args[0]->val.nodes[i]; |
| 5090 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5091 | rc = cast_node_set_to_string(&set_item, &str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5092 | LY_CHECK_RET(rc); |
| 5093 | num = cast_string_to_number(str); |
| 5094 | free(str); |
| 5095 | set->val.num += num; |
| 5096 | } |
| 5097 | |
| 5098 | free(set_item.val.nodes); |
| 5099 | |
| 5100 | return LY_SUCCESS; |
| 5101 | } |
| 5102 | |
| 5103 | /** |
| 5104 | * @brief Execute the XPath text() function (node type). Returns LYXP_SET_NODE_SET |
| 5105 | * with the text content of the nodes in the context. |
| 5106 | * |
| 5107 | * @param[in] args Array of arguments. |
| 5108 | * @param[in] arg_count Count of elements in @p args. |
| 5109 | * @param[in,out] set Context and result set at the same time. |
| 5110 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5111 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5112 | */ |
| 5113 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5114 | 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] | 5115 | { |
| 5116 | uint32_t i; |
| 5117 | |
| 5118 | if (options & LYXP_SCNODE_ALL) { |
| 5119 | set_scnode_clear_ctx(set); |
| 5120 | return LY_SUCCESS; |
| 5121 | } |
| 5122 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5123 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 5124 | LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "text()"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5125 | return LY_EVALID; |
| 5126 | } |
| 5127 | |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 5128 | for (i = 0; i < set->used; ) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5129 | switch (set->val.nodes[i].type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 5130 | case LYXP_NODE_NONE: |
| 5131 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5132 | case LYXP_NODE_ELEM: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5133 | if (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 5134 | set->val.nodes[i].type = LYXP_NODE_TEXT; |
| 5135 | ++i; |
| 5136 | break; |
| 5137 | } |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 5138 | /* fall through */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5139 | case LYXP_NODE_ROOT: |
| 5140 | case LYXP_NODE_ROOT_CONFIG: |
| 5141 | case LYXP_NODE_TEXT: |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5142 | case LYXP_NODE_META: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5143 | set_remove_node(set, i); |
| 5144 | break; |
| 5145 | } |
| 5146 | } |
| 5147 | |
| 5148 | return LY_SUCCESS; |
| 5149 | } |
| 5150 | |
| 5151 | /** |
| 5152 | * @brief Execute the XPath translate(string, string, string) function. |
| 5153 | * Returns LYXP_SET_STRING with the first argument with the characters |
| 5154 | * from the second argument replaced by those on the corresponding |
| 5155 | * positions in the third argument. |
| 5156 | * |
| 5157 | * @param[in] args Array of arguments. |
| 5158 | * @param[in] arg_count Count of elements in @p args. |
| 5159 | * @param[in,out] set Context and result set at the same time. |
| 5160 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5161 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5162 | */ |
| 5163 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5164 | xpath_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] | 5165 | { |
| 5166 | uint16_t i, j, new_used; |
| 5167 | char *new; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 5168 | ly_bool have_removed; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5169 | struct lysc_node_leaf *sleaf; |
| 5170 | LY_ERR rc = LY_SUCCESS; |
| 5171 | |
| 5172 | if (options & LYXP_SCNODE_ALL) { |
| 5173 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 5174 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5175 | 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] | 5176 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5177 | 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] | 5178 | } |
| 5179 | } |
| 5180 | |
| 5181 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 5182 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5183 | 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] | 5184 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5185 | 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] | 5186 | } |
| 5187 | } |
| 5188 | |
| 5189 | if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) { |
| 5190 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5191 | 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] | 5192 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5193 | 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] | 5194 | } |
| 5195 | } |
| 5196 | set_scnode_clear_ctx(set); |
| 5197 | return rc; |
| 5198 | } |
| 5199 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5200 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5201 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5202 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5203 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5204 | rc = lyxp_set_cast(args[2], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5205 | LY_CHECK_RET(rc); |
| 5206 | |
| 5207 | new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char)); |
| 5208 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 5209 | new_used = 0; |
| 5210 | |
| 5211 | have_removed = 0; |
| 5212 | for (i = 0; args[0]->val.str[i]; ++i) { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 5213 | ly_bool found = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5214 | |
| 5215 | for (j = 0; args[1]->val.str[j]; ++j) { |
| 5216 | if (args[0]->val.str[i] == args[1]->val.str[j]) { |
| 5217 | /* removing this char */ |
| 5218 | if (j >= strlen(args[2]->val.str)) { |
| 5219 | have_removed = 1; |
| 5220 | found = 1; |
| 5221 | break; |
| 5222 | } |
| 5223 | /* replacing this char */ |
| 5224 | new[new_used] = args[2]->val.str[j]; |
| 5225 | ++new_used; |
| 5226 | found = 1; |
| 5227 | break; |
| 5228 | } |
| 5229 | } |
| 5230 | |
| 5231 | /* copying this char */ |
| 5232 | if (!found) { |
| 5233 | new[new_used] = args[0]->val.str[i]; |
| 5234 | ++new_used; |
| 5235 | } |
| 5236 | } |
| 5237 | |
| 5238 | if (have_removed) { |
| 5239 | new = ly_realloc(new, (new_used + 1) * sizeof(char)); |
| 5240 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 5241 | } |
| 5242 | new[new_used] = '\0'; |
| 5243 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5244 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5245 | set->type = LYXP_SET_STRING; |
| 5246 | set->val.str = new; |
| 5247 | |
| 5248 | return LY_SUCCESS; |
| 5249 | } |
| 5250 | |
| 5251 | /** |
| 5252 | * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN |
| 5253 | * with true value. |
| 5254 | * |
| 5255 | * @param[in] args Array of arguments. |
| 5256 | * @param[in] arg_count Count of elements in @p args. |
| 5257 | * @param[in,out] set Context and result set at the same time. |
| 5258 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5259 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5260 | */ |
| 5261 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5262 | 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] | 5263 | { |
| 5264 | if (options & LYXP_SCNODE_ALL) { |
| 5265 | set_scnode_clear_ctx(set); |
| 5266 | return LY_SUCCESS; |
| 5267 | } |
| 5268 | |
| 5269 | set_fill_boolean(set, 1); |
| 5270 | return LY_SUCCESS; |
| 5271 | } |
| 5272 | |
| 5273 | /* |
| 5274 | * moveto functions |
| 5275 | * |
| 5276 | * They and only they actually change the context (set). |
| 5277 | */ |
| 5278 | |
| 5279 | /** |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5280 | * @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] | 5281 | * |
Michal Vasko | 2104e9f | 2020-03-06 08:23:25 +0100 | [diff] [blame] | 5282 | * XPath @p set is expected to be a (sc)node set! |
| 5283 | * |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5284 | * @param[in,out] qname Qualified node name. If includes prefix, it is skipped. |
| 5285 | * @param[in,out] qname_len Length of @p qname, is updated accordingly. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5286 | * @param[in] set Set with general XPath context. |
| 5287 | * @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] | 5288 | * @param[out] moveto_mod Expected module of a matching node. |
| 5289 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5290 | */ |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5291 | static LY_ERR |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5292 | moveto_resolve_model(const char **qname, uint16_t *qname_len, const struct lyxp_set *set, |
| 5293 | const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5294 | { |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 5295 | const struct lys_module *mod = NULL; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5296 | const char *ptr; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5297 | size_t pref_len; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5298 | |
Michal Vasko | 2104e9f | 2020-03-06 08:23:25 +0100 | [diff] [blame] | 5299 | assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET)); |
| 5300 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5301 | if ((ptr = ly_strnchr(*qname, ':', *qname_len))) { |
| 5302 | /* specific module */ |
| 5303 | pref_len = ptr - *qname; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5304 | 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] | 5305 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5306 | /* check for errors and non-implemented modules, as they are not valid */ |
Juraj Vijtiuk | d75faa6 | 2019-11-26 14:10:10 +0100 | [diff] [blame] | 5307 | if (!mod || !mod->implemented) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 5308 | LOGVAL(set->ctx, LY_VCODE_XP_INMOD, pref_len, *qname); |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5309 | return LY_EVALID; |
| 5310 | } |
Juraj Vijtiuk | d75faa6 | 2019-11-26 14:10:10 +0100 | [diff] [blame] | 5311 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5312 | *qname += pref_len + 1; |
| 5313 | *qname_len -= pref_len + 1; |
| 5314 | } else if (((*qname)[0] == '*') && (*qname_len == 1)) { |
| 5315 | /* all modules - special case */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5316 | mod = NULL; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5317 | } else { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5318 | switch (set->format) { |
| 5319 | case LY_PREF_SCHEMA: |
| 5320 | case LY_PREF_SCHEMA_RESOLVED: |
| 5321 | /* current module */ |
| 5322 | mod = set->cur_mod; |
| 5323 | break; |
| 5324 | case LY_PREF_JSON: |
| 5325 | /* inherit parent (context node) module */ |
| 5326 | if (ctx_scnode) { |
| 5327 | mod = ctx_scnode->module; |
| 5328 | } else { |
| 5329 | mod = NULL; |
| 5330 | } |
| 5331 | break; |
| 5332 | case LY_PREF_XML: |
| 5333 | /* not defined */ |
| 5334 | LOGINT_RET(set->ctx); |
| 5335 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5336 | } |
| 5337 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5338 | *moveto_mod = mod; |
| 5339 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5340 | } |
| 5341 | |
| 5342 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5343 | * @brief Move context @p set to the root. Handles absolute path. |
| 5344 | * Result is LYXP_SET_NODE_SET. |
| 5345 | * |
| 5346 | * @param[in,out] set Set to use. |
| 5347 | * @param[in] options Xpath options. |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 5348 | * @return LY_ERR value. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5349 | */ |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 5350 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5351 | moveto_root(struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5352 | { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5353 | if (!set) { |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 5354 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5355 | } |
| 5356 | |
| 5357 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5358 | set_scnode_clear_ctx(set); |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 5359 | 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] | 5360 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5361 | set->type = LYXP_SET_NODE_SET; |
| 5362 | set->used = 0; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5363 | set_insert_node(set, NULL, 0, set->root_type, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5364 | } |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 5365 | |
| 5366 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5367 | } |
| 5368 | |
| 5369 | /** |
| 5370 | * @brief Check @p node as a part of NameTest processing. |
| 5371 | * |
| 5372 | * @param[in] node Node to check. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5373 | * @param[in] ctx_node Context node. |
| 5374 | * @param[in] set Set to read general context from. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5375 | * @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] | 5376 | * @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] | 5377 | * @param[in] options XPath options. |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5378 | * @return LY_ERR (LY_ENOT if node does not match, LY_EINCOMPLETE on unresolved when, |
| 5379 | * LY_EINVAL if netither node nor any children match) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5380 | */ |
| 5381 | static LY_ERR |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5382 | 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] | 5383 | 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] | 5384 | { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5385 | if (!moveto_mod && (!node_name || strcmp(node_name, "*"))) { |
| 5386 | switch (set->format) { |
| 5387 | case LY_PREF_SCHEMA: |
| 5388 | case LY_PREF_SCHEMA_RESOLVED: |
| 5389 | /* use current module */ |
| 5390 | moveto_mod = set->cur_mod; |
| 5391 | break; |
| 5392 | case LY_PREF_JSON: |
| 5393 | /* inherit module of the context node, if any */ |
| 5394 | if (ctx_node) { |
| 5395 | moveto_mod = ctx_node->schema->module; |
| 5396 | } |
| 5397 | break; |
| 5398 | case LY_PREF_XML: |
| 5399 | /* not defined */ |
| 5400 | LOGINT(set->ctx); |
| 5401 | return LY_EINVAL; |
| 5402 | } |
| 5403 | } |
| 5404 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5405 | /* module check */ |
| 5406 | if (moveto_mod && (node->schema->module != moveto_mod)) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5407 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5408 | } |
| 5409 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5410 | /* context check */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5411 | 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] | 5412 | return LY_EINVAL; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5413 | } else if (set->context_op && (node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && |
| 5414 | (node->schema != set->context_op)) { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5415 | return LY_EINVAL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5416 | } |
| 5417 | |
| 5418 | /* name check */ |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5419 | if (node_name && strcmp(node_name, "*") && (node->schema->name != node_name)) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5420 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5421 | } |
| 5422 | |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 5423 | /* when check */ |
Michal Vasko | d5cfa6e | 2020-11-23 16:56:08 +0100 | [diff] [blame] | 5424 | 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] | 5425 | return LY_EINCOMPLETE; |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 5426 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5427 | |
| 5428 | /* match */ |
| 5429 | return LY_SUCCESS; |
| 5430 | } |
| 5431 | |
| 5432 | /** |
| 5433 | * @brief Check @p node as a part of schema NameTest processing. |
| 5434 | * |
| 5435 | * @param[in] node Schema node to check. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5436 | * @param[in] ctx_scnode Context node. |
| 5437 | * @param[in] set Set to read general context from. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5438 | * @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] | 5439 | * @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] | 5440 | * @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] | 5441 | */ |
| 5442 | static LY_ERR |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5443 | 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] | 5444 | const char *node_name, const struct lys_module *moveto_mod) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5445 | { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5446 | if (!moveto_mod && (!node_name || strcmp(node_name, "*"))) { |
| 5447 | switch (set->format) { |
| 5448 | case LY_PREF_SCHEMA: |
| 5449 | case LY_PREF_SCHEMA_RESOLVED: |
| 5450 | /* use current module */ |
| 5451 | moveto_mod = set->cur_mod; |
| 5452 | break; |
| 5453 | case LY_PREF_JSON: |
| 5454 | /* inherit module of the context node, if any */ |
| 5455 | if (ctx_scnode) { |
| 5456 | moveto_mod = ctx_scnode->module; |
| 5457 | } |
| 5458 | break; |
| 5459 | case LY_PREF_XML: |
| 5460 | /* not defined */ |
| 5461 | LOGINT(set->ctx); |
| 5462 | return LY_EINVAL; |
| 5463 | } |
| 5464 | } |
| 5465 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5466 | /* module check */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5467 | if (moveto_mod && (node->module != moveto_mod)) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5468 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5469 | } |
| 5470 | |
| 5471 | /* context check */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5472 | 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] | 5473 | return LY_EINVAL; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5474 | } 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] | 5475 | return LY_EINVAL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5476 | } |
| 5477 | |
| 5478 | /* name check */ |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5479 | if (node_name && strcmp(node_name, "*") && (node->name != node_name)) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5480 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5481 | } |
| 5482 | |
| 5483 | /* match */ |
| 5484 | return LY_SUCCESS; |
| 5485 | } |
| 5486 | |
| 5487 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5488 | * @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] | 5489 | * |
| 5490 | * @param[in,out] set Set to use. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5491 | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5492 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 5493 | * @param[in] options XPath options. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5494 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 5495 | */ |
| 5496 | static LY_ERR |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 5497 | 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] | 5498 | { |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 5499 | uint32_t i; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5500 | const struct lyd_node *siblings, *sub, *ctx_node; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5501 | LY_ERR rc; |
| 5502 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5503 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5504 | return LY_SUCCESS; |
| 5505 | } |
| 5506 | |
| 5507 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 5508 | LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5509 | return LY_EVALID; |
| 5510 | } |
| 5511 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5512 | for (i = 0; i < set->used; ) { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 5513 | ly_bool replaced = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5514 | |
| 5515 | 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] | 5516 | assert(!set->val.nodes[i].node); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5517 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5518 | /* search in all the trees */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5519 | ctx_node = NULL; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5520 | siblings = set->tree; |
Michal Vasko | 5bfd4be | 2020-06-23 13:26:19 +0200 | [diff] [blame] | 5521 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5522 | /* search in children */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5523 | ctx_node = set->val.nodes[i].node; |
| 5524 | siblings = lyd_child(ctx_node); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5525 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5526 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5527 | for (sub = siblings; sub; sub = sub->next) { |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 5528 | rc = moveto_node_check(sub, ctx_node, set, ncname, moveto_mod, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5529 | if (rc == LY_SUCCESS) { |
| 5530 | if (!replaced) { |
| 5531 | set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i); |
| 5532 | replaced = 1; |
| 5533 | } else { |
| 5534 | set_insert_node(set, sub, 0, LYXP_NODE_ELEM, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5535 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5536 | ++i; |
| 5537 | } else if (rc == LY_EINCOMPLETE) { |
| 5538 | return rc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5539 | } |
| 5540 | } |
| 5541 | |
| 5542 | if (!replaced) { |
| 5543 | /* no match */ |
| 5544 | set_remove_node(set, i); |
| 5545 | } |
| 5546 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5547 | |
| 5548 | return LY_SUCCESS; |
| 5549 | } |
| 5550 | |
| 5551 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5552 | * @brief Move context @p set to a node using hashes. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY). |
| 5553 | * Context position aware. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5554 | * |
| 5555 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5556 | * @param[in] scnode Matching node schema. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5557 | * @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] | 5558 | * @param[in] options XPath options. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5559 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 5560 | */ |
| 5561 | static LY_ERR |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 5562 | moveto_node_hash(struct lyxp_set *set, const struct lysc_node *scnode, const struct ly_path_predicate *predicates, |
| 5563 | uint32_t options) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5564 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5565 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5566 | uint32_t i; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5567 | const struct lyd_node *siblings; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5568 | struct lyd_node *sub, *inst = NULL; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5569 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5570 | assert(scnode && (!(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) || predicates)); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5571 | |
| 5572 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5573 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5574 | } |
| 5575 | |
| 5576 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 5577 | LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5578 | ret = LY_EVALID; |
| 5579 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5580 | } |
| 5581 | |
| 5582 | /* context check for all the nodes since we have the schema node */ |
| 5583 | if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) { |
| 5584 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5585 | goto cleanup; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 5586 | } else if (set->context_op && (scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && |
| 5587 | (scnode != set->context_op)) { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5588 | lyxp_set_free_content(set); |
| 5589 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5590 | } |
| 5591 | |
| 5592 | /* create specific data instance if needed */ |
| 5593 | if (scnode->nodetype == LYS_LIST) { |
| 5594 | LY_CHECK_GOTO(ret = lyd_create_list(scnode, predicates, &inst), cleanup); |
| 5595 | } else if (scnode->nodetype == LYS_LEAFLIST) { |
| 5596 | 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] | 5597 | } |
| 5598 | |
| 5599 | for (i = 0; i < set->used; ) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5600 | siblings = NULL; |
| 5601 | |
| 5602 | if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) { |
| 5603 | assert(!set->val.nodes[i].node); |
| 5604 | |
| 5605 | /* search in all the trees */ |
| 5606 | siblings = set->tree; |
| 5607 | } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
| 5608 | /* search in children */ |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 5609 | siblings = lyd_child(set->val.nodes[i].node); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5610 | } |
| 5611 | |
| 5612 | /* find the node using hashes */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5613 | if (inst) { |
| 5614 | lyd_find_sibling_first(siblings, inst, &sub); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5615 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5616 | lyd_find_sibling_val(siblings, scnode, NULL, 0, &sub); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5617 | } |
| 5618 | |
| 5619 | /* when check */ |
Michal Vasko | d5cfa6e | 2020-11-23 16:56:08 +0100 | [diff] [blame] | 5620 | 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] | 5621 | ret = LY_EINCOMPLETE; |
| 5622 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5623 | } |
| 5624 | |
| 5625 | if (sub) { |
| 5626 | /* pos filled later */ |
Michal Vasko | cb1b7c0 | 2020-07-03 13:38:12 +0200 | [diff] [blame] | 5627 | set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5628 | ++i; |
Michal Vasko | cb1b7c0 | 2020-07-03 13:38:12 +0200 | [diff] [blame] | 5629 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5630 | /* no match */ |
| 5631 | set_remove_node(set, i); |
| 5632 | } |
| 5633 | } |
| 5634 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5635 | cleanup: |
| 5636 | lyd_free_tree(inst); |
| 5637 | return ret; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5638 | } |
| 5639 | |
| 5640 | /** |
| 5641 | * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY). |
| 5642 | * |
| 5643 | * @param[in,out] set Set to use. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5644 | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5645 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5646 | * @param[in] options XPath options. |
| 5647 | * @return LY_ERR |
| 5648 | */ |
| 5649 | static LY_ERR |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5650 | 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] | 5651 | { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 5652 | ly_bool temp_ctx = 0; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5653 | uint32_t getnext_opts; |
| 5654 | uint32_t orig_used, i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5655 | uint32_t mod_idx; |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 5656 | const struct lysc_node *iter, *start_parent; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5657 | const struct lys_module *mod; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5658 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5659 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5660 | return LY_SUCCESS; |
| 5661 | } |
| 5662 | |
| 5663 | if (set->type != LYXP_SET_SCNODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 5664 | LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5665 | return LY_EVALID; |
| 5666 | } |
| 5667 | |
Michal Vasko | cafad9d | 2019-11-07 15:20:03 +0100 | [diff] [blame] | 5668 | /* getnext opts */ |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 5669 | getnext_opts = 0; |
Michal Vasko | cafad9d | 2019-11-07 15:20:03 +0100 | [diff] [blame] | 5670 | if (options & LYXP_SCNODE_OUTPUT) { |
| 5671 | getnext_opts |= LYS_GETNEXT_OUTPUT; |
| 5672 | } |
| 5673 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5674 | orig_used = set->used; |
| 5675 | for (i = 0; i < orig_used; ++i) { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 5676 | uint32_t idx; |
| 5677 | |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 5678 | if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) { |
| 5679 | if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5680 | continue; |
| 5681 | } |
| 5682 | |
| 5683 | /* remember context node */ |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 5684 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED; |
Michal Vasko | ec4df48 | 2019-12-16 10:02:18 +0100 | [diff] [blame] | 5685 | } else { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 5686 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5687 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5688 | |
| 5689 | start_parent = set->val.scnodes[i].scnode; |
| 5690 | |
| 5691 | 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] | 5692 | /* 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] | 5693 | * 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] | 5694 | mod_idx = 0; |
Michal Vasko | 9e9f26d | 2020-10-12 16:31:33 +0200 | [diff] [blame] | 5695 | 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] | 5696 | iter = NULL; |
Michal Vasko | 509de4d | 2019-12-10 14:51:30 +0100 | [diff] [blame] | 5697 | /* module may not be implemented */ |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 5698 | while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5699 | if (!moveto_scnode_check(iter, NULL, set, ncname, moveto_mod)) { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 5700 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx)); |
| 5701 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5702 | /* we need to prevent these nodes from being considered in this moveto */ |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 5703 | if ((idx < orig_used) && (idx > i)) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 5704 | set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5705 | temp_ctx = 1; |
| 5706 | } |
| 5707 | } |
| 5708 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5709 | } |
| 5710 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 5711 | } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) { |
| 5712 | iter = NULL; |
| 5713 | while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5714 | if (!moveto_scnode_check(iter, start_parent, set, ncname, moveto_mod)) { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 5715 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx)); |
| 5716 | |
| 5717 | if ((idx < orig_used) && (idx > i)) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 5718 | set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5719 | temp_ctx = 1; |
| 5720 | } |
| 5721 | } |
| 5722 | } |
| 5723 | } |
| 5724 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5725 | |
| 5726 | /* correct temporary in_ctx values */ |
| 5727 | if (temp_ctx) { |
| 5728 | for (i = 0; i < orig_used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 5729 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NEW_CTX) { |
| 5730 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5731 | } |
| 5732 | } |
| 5733 | } |
| 5734 | |
| 5735 | return LY_SUCCESS; |
| 5736 | } |
| 5737 | |
| 5738 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5739 | * @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] | 5740 | * Context position aware. |
| 5741 | * |
| 5742 | * @param[in] set Set to use. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5743 | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5744 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 5745 | * @param[in] options XPath options. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5746 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 5747 | */ |
| 5748 | static LY_ERR |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 5749 | 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] | 5750 | { |
| 5751 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5752 | const struct lyd_node *next, *elem, *start; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5753 | struct lyxp_set ret_set; |
| 5754 | LY_ERR rc; |
| 5755 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5756 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5757 | return LY_SUCCESS; |
| 5758 | } |
| 5759 | |
| 5760 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 5761 | LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5762 | return LY_EVALID; |
| 5763 | } |
| 5764 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5765 | /* 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] | 5766 | rc = moveto_node(set, NULL, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5767 | LY_CHECK_RET(rc); |
| 5768 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5769 | /* 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] | 5770 | set_init(&ret_set, set); |
| 5771 | for (i = 0; i < set->used; ++i) { |
| 5772 | |
| 5773 | /* TREE DFS */ |
| 5774 | start = set->val.nodes[i].node; |
| 5775 | for (elem = next = start; elem; elem = next) { |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 5776 | rc = moveto_node_check(elem, start, set, ncname, moveto_mod, options); |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5777 | if (!rc) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5778 | /* add matching node into result set */ |
| 5779 | set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used); |
| 5780 | if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) { |
| 5781 | /* the node is a duplicate, we'll process it later in the set */ |
| 5782 | goto skip_children; |
| 5783 | } |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5784 | } else if (rc == LY_EINCOMPLETE) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5785 | return rc; |
| 5786 | } else if (rc == LY_EINVAL) { |
| 5787 | goto skip_children; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5788 | } |
| 5789 | |
| 5790 | /* TREE DFS NEXT ELEM */ |
| 5791 | /* select element for the next run - children first */ |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 5792 | next = lyd_child(elem); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5793 | if (!next) { |
| 5794 | skip_children: |
| 5795 | /* no children, so try siblings, but only if it's not the start, |
| 5796 | * that is considered to be the root and it's siblings are not traversed */ |
| 5797 | if (elem != start) { |
| 5798 | next = elem->next; |
| 5799 | } else { |
| 5800 | break; |
| 5801 | } |
| 5802 | } |
| 5803 | while (!next) { |
| 5804 | /* no siblings, go back through the parents */ |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 5805 | if (lyd_parent(elem) == start) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5806 | /* we are done, no next element to process */ |
| 5807 | break; |
| 5808 | } |
| 5809 | /* parent is already processed, go to its sibling */ |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 5810 | elem = lyd_parent(elem); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5811 | next = elem->next; |
| 5812 | } |
| 5813 | } |
| 5814 | } |
| 5815 | |
| 5816 | /* make the temporary set the current one */ |
| 5817 | ret_set.ctx_pos = set->ctx_pos; |
| 5818 | ret_set.ctx_size = set->ctx_size; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5819 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5820 | memcpy(set, &ret_set, sizeof *set); |
| 5821 | |
| 5822 | return LY_SUCCESS; |
| 5823 | } |
| 5824 | |
| 5825 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5826 | * @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] | 5827 | * |
| 5828 | * @param[in] set Set to use. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5829 | * @param[in] moveto_mod Matching node module, NULL for no prefix. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5830 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5831 | * @param[in] options XPath options. |
| 5832 | * @return LY_ERR |
| 5833 | */ |
| 5834 | static LY_ERR |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5835 | 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] | 5836 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5837 | uint32_t i, orig_used; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5838 | const struct lysc_node *next, *elem, *start; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5839 | LY_ERR rc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5840 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5841 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5842 | return LY_SUCCESS; |
| 5843 | } |
| 5844 | |
| 5845 | if (set->type != LYXP_SET_SCNODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 5846 | LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5847 | return LY_EVALID; |
| 5848 | } |
| 5849 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5850 | orig_used = set->used; |
| 5851 | for (i = 0; i < orig_used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 5852 | if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) { |
| 5853 | if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5854 | continue; |
| 5855 | } |
| 5856 | |
| 5857 | /* remember context node */ |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 5858 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED; |
Michal Vasko | ec4df48 | 2019-12-16 10:02:18 +0100 | [diff] [blame] | 5859 | } else { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 5860 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5861 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5862 | |
| 5863 | /* TREE DFS */ |
| 5864 | start = set->val.scnodes[i].scnode; |
| 5865 | for (elem = next = start; elem; elem = next) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5866 | if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) { |
| 5867 | /* schema-only nodes, skip root */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5868 | goto next_iter; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5869 | } |
| 5870 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 5871 | rc = moveto_scnode_check(elem, start, set, ncname, moveto_mod); |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5872 | if (!rc) { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 5873 | uint32_t idx; |
| 5874 | |
| 5875 | if (lyxp_set_scnode_contains(set, elem, LYXP_NODE_ELEM, i, &idx)) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 5876 | set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5877 | if ((uint32_t)idx > i) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5878 | /* we will process it later in the set */ |
| 5879 | goto skip_children; |
| 5880 | } |
| 5881 | } else { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 5882 | 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] | 5883 | } |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5884 | } else if (rc == LY_EINVAL) { |
| 5885 | goto skip_children; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5886 | } |
| 5887 | |
| 5888 | next_iter: |
| 5889 | /* TREE DFS NEXT ELEM */ |
| 5890 | /* select element for the next run - children first */ |
Michal Vasko | 544e58a | 2021-01-28 14:33:41 +0100 | [diff] [blame] | 5891 | next = lysc_node_child(elem); |
| 5892 | if (next && (next->nodetype == LYS_INPUT) && (options & LYXP_SCNODE_OUTPUT)) { |
| 5893 | next = next->next; |
| 5894 | } else if (next && (next->nodetype == LYS_OUTPUT) && !(options & LYXP_SCNODE_OUTPUT)) { |
| 5895 | next = next->next; |
| 5896 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5897 | if (!next) { |
| 5898 | skip_children: |
| 5899 | /* no children, so try siblings, but only if it's not the start, |
| 5900 | * that is considered to be the root and it's siblings are not traversed */ |
| 5901 | if (elem != start) { |
| 5902 | next = elem->next; |
| 5903 | } else { |
| 5904 | break; |
| 5905 | } |
| 5906 | } |
| 5907 | while (!next) { |
| 5908 | /* no siblings, go back through the parents */ |
| 5909 | if (elem->parent == start) { |
| 5910 | /* we are done, no next element to process */ |
| 5911 | break; |
| 5912 | } |
| 5913 | /* parent is already processed, go to its sibling */ |
| 5914 | elem = elem->parent; |
| 5915 | next = elem->next; |
| 5916 | } |
| 5917 | } |
| 5918 | } |
| 5919 | |
| 5920 | return LY_SUCCESS; |
| 5921 | } |
| 5922 | |
| 5923 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5924 | * @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] | 5925 | * Indirectly context position aware. |
| 5926 | * |
| 5927 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5928 | * @param[in] mod Matching metadata module, NULL for any. |
| 5929 | * @param[in] ncname Matching metadata name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5930 | * @return LY_ERR |
| 5931 | */ |
| 5932 | static LY_ERR |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5933 | 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] | 5934 | { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5935 | struct lyd_meta *sub; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5936 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5937 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5938 | return LY_SUCCESS; |
| 5939 | } |
| 5940 | |
| 5941 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 5942 | LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5943 | return LY_EVALID; |
| 5944 | } |
| 5945 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5946 | for (uint32_t i = 0; i < set->used; ) { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 5947 | ly_bool replaced = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5948 | |
| 5949 | /* only attributes of an elem (not dummy) can be in the result, skip all the rest; |
| 5950 | * our attributes are always qualified */ |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5951 | if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5952 | for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5953 | |
| 5954 | /* check "namespace" */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5955 | if (mod && (sub->annotation->module != mod)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5956 | continue; |
| 5957 | } |
| 5958 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5959 | if (!ncname || (sub->name == ncname)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5960 | /* match */ |
| 5961 | if (!replaced) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5962 | set->val.meta[i].meta = sub; |
| 5963 | set->val.meta[i].type = LYXP_NODE_META; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5964 | /* pos does not change */ |
| 5965 | replaced = 1; |
| 5966 | } else { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5967 | 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] | 5968 | } |
| 5969 | ++i; |
| 5970 | } |
| 5971 | } |
| 5972 | } |
| 5973 | |
| 5974 | if (!replaced) { |
| 5975 | /* no match */ |
| 5976 | set_remove_node(set, i); |
| 5977 | } |
| 5978 | } |
| 5979 | |
| 5980 | return LY_SUCCESS; |
| 5981 | } |
| 5982 | |
| 5983 | /** |
| 5984 | * @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] | 5985 | * Result is LYXP_SET_NODE_SET. Context position aware. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5986 | * |
| 5987 | * @param[in,out] set1 Set to use for the result. |
| 5988 | * @param[in] set2 Set that is copied to @p set1. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5989 | * @return LY_ERR |
| 5990 | */ |
| 5991 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5992 | moveto_union(struct lyxp_set *set1, struct lyxp_set *set2) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5993 | { |
| 5994 | LY_ERR rc; |
| 5995 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5996 | if ((set1->type != LYXP_SET_NODE_SET) || (set2->type != LYXP_SET_NODE_SET)) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 5997 | LOGVAL(set1->ctx, LY_VCODE_XP_INOP_2, "union", print_set_type(set1), print_set_type(set2)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5998 | return LY_EVALID; |
| 5999 | } |
| 6000 | |
| 6001 | /* set2 is empty or both set1 and set2 */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6002 | if (!set2->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6003 | return LY_SUCCESS; |
| 6004 | } |
| 6005 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6006 | if (!set1->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6007 | memcpy(set1, set2, sizeof *set1); |
| 6008 | /* dynamic memory belongs to set1 now, do not free */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6009 | memset(set2, 0, sizeof *set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6010 | return LY_SUCCESS; |
| 6011 | } |
| 6012 | |
| 6013 | /* we assume sets are sorted */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6014 | assert(!set_sort(set1) && !set_sort(set2)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6015 | |
| 6016 | /* sort, remove duplicates */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6017 | rc = set_sorted_merge(set1, set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6018 | LY_CHECK_RET(rc); |
| 6019 | |
| 6020 | /* final set must be sorted */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6021 | assert(!set_sort(set1)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6022 | |
| 6023 | return LY_SUCCESS; |
| 6024 | } |
| 6025 | |
| 6026 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6027 | * @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] | 6028 | * Context position aware. |
| 6029 | * |
| 6030 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6031 | * @param[in] mod Matching metadata module, NULL for any. |
| 6032 | * @param[in] ncname Matching metadata name in the dictionary, NULL for any. |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 6033 | * @param[in] options XPath options. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6034 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6035 | */ |
| 6036 | static int |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 6037 | 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] | 6038 | { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6039 | struct lyd_meta *sub; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6040 | struct lyxp_set *set_all_desc = NULL; |
| 6041 | LY_ERR rc; |
| 6042 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6043 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6044 | return LY_SUCCESS; |
| 6045 | } |
| 6046 | |
| 6047 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 6048 | LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6049 | return LY_EVALID; |
| 6050 | } |
| 6051 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6052 | /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory, |
| 6053 | * but it likely won't be used much, so it's a waste of time */ |
| 6054 | /* copy the context */ |
| 6055 | set_all_desc = set_copy(set); |
| 6056 | /* get all descendant nodes (the original context nodes are removed) */ |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 6057 | rc = moveto_node_alldesc(set_all_desc, NULL, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6058 | if (rc != LY_SUCCESS) { |
| 6059 | lyxp_set_free(set_all_desc); |
| 6060 | return rc; |
| 6061 | } |
| 6062 | /* prepend the original context nodes */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6063 | rc = moveto_union(set, set_all_desc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6064 | if (rc != LY_SUCCESS) { |
| 6065 | lyxp_set_free(set_all_desc); |
| 6066 | return rc; |
| 6067 | } |
| 6068 | lyxp_set_free(set_all_desc); |
| 6069 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6070 | for (uint32_t i = 0; i < set->used; ) { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6071 | ly_bool replaced = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6072 | |
| 6073 | /* only attributes of an elem can be in the result, skip all the rest, |
| 6074 | * we have all attributes qualified in lyd tree */ |
| 6075 | if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6076 | for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6077 | /* check "namespace" */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6078 | if (mod && (sub->annotation->module != mod)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6079 | continue; |
| 6080 | } |
| 6081 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6082 | if (!ncname || (sub->name == ncname)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6083 | /* match */ |
| 6084 | if (!replaced) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6085 | set->val.meta[i].meta = sub; |
| 6086 | set->val.meta[i].type = LYXP_NODE_META; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6087 | /* pos does not change */ |
| 6088 | replaced = 1; |
| 6089 | } else { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6090 | 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] | 6091 | } |
| 6092 | ++i; |
| 6093 | } |
| 6094 | } |
| 6095 | } |
| 6096 | |
| 6097 | if (!replaced) { |
| 6098 | /* no match */ |
| 6099 | set_remove_node(set, i); |
| 6100 | } |
| 6101 | } |
| 6102 | |
| 6103 | return LY_SUCCESS; |
| 6104 | } |
| 6105 | |
| 6106 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6107 | * @brief Move context @p set to self and al chilren, recursively. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET. |
| 6108 | * Context position aware. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6109 | * |
| 6110 | * @param[in] parent Current parent. |
| 6111 | * @param[in] parent_pos Position of @p parent. |
| 6112 | * @param[in] parent_type Node type of @p parent. |
| 6113 | * @param[in,out] to_set Set to use. |
| 6114 | * @param[in] dup_check_set Set for checking duplicities. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6115 | * @param[in] options XPath options. |
| 6116 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6117 | */ |
| 6118 | static LY_ERR |
| 6119 | 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] | 6120 | 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] | 6121 | { |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6122 | const struct lyd_node *iter, *first; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6123 | LY_ERR rc; |
| 6124 | |
| 6125 | switch (parent_type) { |
| 6126 | case LYXP_NODE_ROOT: |
| 6127 | case LYXP_NODE_ROOT_CONFIG: |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6128 | assert(!parent); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6129 | |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6130 | /* add all top-level nodes as elements */ |
| 6131 | first = to_set->tree; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6132 | break; |
| 6133 | case LYXP_NODE_ELEM: |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6134 | /* add just the text node of this term element node */ |
| 6135 | if (parent->schema->nodetype & (LYD_NODE_TERM | LYD_NODE_ANY)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6136 | if (!set_dup_node_check(dup_check_set, parent, LYXP_NODE_TEXT, -1)) { |
| 6137 | set_insert_node(to_set, parent, parent_pos, LYXP_NODE_TEXT, to_set->used); |
| 6138 | } |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6139 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6140 | } |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6141 | |
| 6142 | /* add all the children of this node */ |
Radek Krejci | a1c1e54 | 2020-09-29 16:06:52 +0200 | [diff] [blame] | 6143 | first = lyd_child(parent); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6144 | break; |
| 6145 | default: |
| 6146 | LOGINT_RET(parent->schema->module->ctx); |
| 6147 | } |
| 6148 | |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6149 | /* add all top-level nodes as elements */ |
| 6150 | LY_LIST_FOR(first, iter) { |
| 6151 | /* context check */ |
| 6152 | if ((parent_type == LYXP_NODE_ROOT_CONFIG) && (iter->schema->flags & LYS_CONFIG_R)) { |
| 6153 | continue; |
| 6154 | } |
| 6155 | |
| 6156 | /* when check */ |
Michal Vasko | d5cfa6e | 2020-11-23 16:56:08 +0100 | [diff] [blame] | 6157 | 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] | 6158 | return LY_EINCOMPLETE; |
| 6159 | } |
| 6160 | |
| 6161 | if (!set_dup_node_check(dup_check_set, iter, LYXP_NODE_ELEM, -1)) { |
| 6162 | set_insert_node(to_set, iter, 0, LYXP_NODE_ELEM, to_set->used); |
| 6163 | |
| 6164 | /* also add all the children of this node, recursively */ |
| 6165 | rc = moveto_self_add_children_r(iter, 0, LYXP_NODE_ELEM, to_set, dup_check_set, options); |
| 6166 | LY_CHECK_RET(rc); |
| 6167 | } |
| 6168 | } |
| 6169 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6170 | return LY_SUCCESS; |
| 6171 | } |
| 6172 | |
| 6173 | /** |
| 6174 | * @brief Move context @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET |
| 6175 | * (or LYXP_SET_EMPTY). Context position aware. |
| 6176 | * |
| 6177 | * @param[in,out] set Set to use. |
| 6178 | * @param[in] all_desc Whether to go to all descendants ('//') or not ('/'). |
| 6179 | * @param[in] options XPath options. |
| 6180 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6181 | */ |
| 6182 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6183 | 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] | 6184 | { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6185 | struct lyxp_set ret_set; |
| 6186 | LY_ERR rc; |
| 6187 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6188 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6189 | return LY_SUCCESS; |
| 6190 | } |
| 6191 | |
| 6192 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 6193 | LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6194 | return LY_EVALID; |
| 6195 | } |
| 6196 | |
| 6197 | /* nothing to do */ |
| 6198 | if (!all_desc) { |
| 6199 | return LY_SUCCESS; |
| 6200 | } |
| 6201 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6202 | /* add all the children, they get added recursively */ |
| 6203 | set_init(&ret_set, set); |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6204 | for (uint32_t i = 0; i < set->used; ++i) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6205 | /* copy the current node to tmp */ |
| 6206 | set_insert_node(&ret_set, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, ret_set.used); |
| 6207 | |
| 6208 | /* do not touch attributes and text nodes */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6209 | 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] | 6210 | continue; |
| 6211 | } |
| 6212 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6213 | /* add all the children */ |
| 6214 | 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] | 6215 | set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6216 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6217 | lyxp_set_free_content(&ret_set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6218 | return rc; |
| 6219 | } |
| 6220 | } |
| 6221 | |
| 6222 | /* use the temporary set as the current one */ |
| 6223 | ret_set.ctx_pos = set->ctx_pos; |
| 6224 | ret_set.ctx_size = set->ctx_size; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6225 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6226 | memcpy(set, &ret_set, sizeof *set); |
| 6227 | |
| 6228 | return LY_SUCCESS; |
| 6229 | } |
| 6230 | |
| 6231 | /** |
| 6232 | * @brief Move context schema @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_SCNODE_SET |
| 6233 | * (or LYXP_SET_EMPTY). |
| 6234 | * |
| 6235 | * @param[in,out] set Set to use. |
| 6236 | * @param[in] all_desc Whether to go to all descendants ('//') or not ('/'). |
| 6237 | * @param[in] options XPath options. |
| 6238 | * @return LY_ERR |
| 6239 | */ |
| 6240 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6241 | 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] | 6242 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6243 | uint32_t getnext_opts; |
| 6244 | uint32_t mod_idx; |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6245 | const struct lysc_node *iter, *start_parent; |
| 6246 | const struct lys_module *mod; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6247 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6248 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6249 | return LY_SUCCESS; |
| 6250 | } |
| 6251 | |
| 6252 | if (set->type != LYXP_SET_SCNODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 6253 | LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6254 | return LY_EVALID; |
| 6255 | } |
| 6256 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6257 | /* getnext opts */ |
Michal Vasko | 7b1ad1a | 2020-11-02 15:41:27 +0100 | [diff] [blame] | 6258 | getnext_opts = 0; |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6259 | if (options & LYXP_SCNODE_OUTPUT) { |
| 6260 | getnext_opts |= LYS_GETNEXT_OUTPUT; |
| 6261 | } |
| 6262 | |
| 6263 | /* 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] | 6264 | for (uint32_t i = 0; i < set->used; ++i) { |
Michal Vasko | e657f96 | 2020-12-10 12:19:48 +0100 | [diff] [blame] | 6265 | if (!all_desc) { |
| 6266 | /* traverse the start node */ |
| 6267 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START) { |
| 6268 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
| 6269 | } |
| 6270 | continue; |
| 6271 | } |
| 6272 | |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 6273 | if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) { |
| 6274 | if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6275 | continue; |
| 6276 | } |
| 6277 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6278 | /* remember context node */ |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 6279 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED; |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6280 | } else { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 6281 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6282 | } |
| 6283 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6284 | start_parent = set->val.scnodes[i].scnode; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6285 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6286 | if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) { |
| 6287 | /* it can actually be in any module, it's all <running> */ |
| 6288 | mod_idx = 0; |
| 6289 | while ((mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) { |
| 6290 | iter = NULL; |
| 6291 | /* module may not be implemented */ |
| 6292 | while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) { |
| 6293 | /* context check */ |
| 6294 | if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) { |
| 6295 | continue; |
| 6296 | } |
| 6297 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 6298 | 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] | 6299 | /* throw away the insert index, we want to consider that node again, recursively */ |
| 6300 | } |
| 6301 | } |
| 6302 | |
| 6303 | } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) { |
| 6304 | iter = NULL; |
| 6305 | while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6306 | /* context check */ |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6307 | 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] | 6308 | continue; |
| 6309 | } |
| 6310 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 6311 | 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] | 6312 | } |
| 6313 | } |
| 6314 | } |
| 6315 | |
| 6316 | return LY_SUCCESS; |
| 6317 | } |
| 6318 | |
| 6319 | /** |
| 6320 | * @brief Move context @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_NODE_SET |
| 6321 | * (or LYXP_SET_EMPTY). Context position aware. |
| 6322 | * |
| 6323 | * @param[in] set Set to use. |
| 6324 | * @param[in] all_desc Whether to go to all descendants ('//') or not ('/'). |
| 6325 | * @param[in] options XPath options. |
| 6326 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6327 | */ |
| 6328 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6329 | 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] | 6330 | { |
| 6331 | LY_ERR rc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6332 | struct lyd_node *node, *new_node; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6333 | enum lyxp_node_type new_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6334 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6335 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6336 | return LY_SUCCESS; |
| 6337 | } |
| 6338 | |
| 6339 | if (set->type != LYXP_SET_NODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 6340 | LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6341 | return LY_EVALID; |
| 6342 | } |
| 6343 | |
| 6344 | if (all_desc) { |
| 6345 | /* <path>//.. == <path>//./.. */ |
| 6346 | rc = moveto_self(set, 1, options); |
| 6347 | LY_CHECK_RET(rc); |
| 6348 | } |
| 6349 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6350 | for (uint32_t i = 0; i < set->used; ++i) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6351 | node = set->val.nodes[i].node; |
| 6352 | |
| 6353 | if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 6354 | new_node = lyd_parent(node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6355 | } else if (set->val.nodes[i].type == LYXP_NODE_TEXT) { |
| 6356 | new_node = node; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6357 | } else if (set->val.nodes[i].type == LYXP_NODE_META) { |
| 6358 | new_node = set->val.meta[i].meta->parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6359 | if (!new_node) { |
| 6360 | LOGINT_RET(set->ctx); |
| 6361 | } |
| 6362 | } else { |
| 6363 | /* root does not have a parent */ |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6364 | set_remove_node_none(set, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6365 | continue; |
| 6366 | } |
| 6367 | |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 6368 | /* when check */ |
Michal Vasko | d5cfa6e | 2020-11-23 16:56:08 +0100 | [diff] [blame] | 6369 | 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] | 6370 | return LY_EINCOMPLETE; |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 6371 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6372 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6373 | if (!new_node) { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 6374 | /* node already there can also be the root */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6375 | new_type = set->root_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6376 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6377 | } else { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 6378 | /* 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] | 6379 | new_type = LYXP_NODE_ELEM; |
| 6380 | } |
| 6381 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6382 | if (set_dup_node_check(set, new_node, new_type, -1)) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6383 | set_remove_node_none(set, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6384 | } else { |
| 6385 | set_replace_node(set, new_node, 0, new_type, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6386 | } |
| 6387 | } |
| 6388 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6389 | set_remove_nodes_none(set); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6390 | assert(!set_sort(set) && !set_sorted_dup_node_clean(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6391 | |
| 6392 | return LY_SUCCESS; |
| 6393 | } |
| 6394 | |
| 6395 | /** |
| 6396 | * @brief Move context schema @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_SCNODE_SET |
| 6397 | * (or LYXP_SET_EMPTY). |
| 6398 | * |
| 6399 | * @param[in] set Set to use. |
| 6400 | * @param[in] all_desc Whether to go to all descendants ('//') or not ('/'). |
| 6401 | * @param[in] options XPath options. |
| 6402 | * @return LY_ERR |
| 6403 | */ |
| 6404 | static LY_ERR |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6405 | 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] | 6406 | { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 6407 | uint32_t i, orig_used, idx; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 6408 | ly_bool temp_ctx = 0; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6409 | const struct lysc_node *node, *new_node; |
| 6410 | enum lyxp_node_type new_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6411 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6412 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6413 | return LY_SUCCESS; |
| 6414 | } |
| 6415 | |
| 6416 | if (set->type != LYXP_SET_SCNODE_SET) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 6417 | LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6418 | return LY_EVALID; |
| 6419 | } |
| 6420 | |
| 6421 | if (all_desc) { |
| 6422 | /* <path>//.. == <path>//./.. */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6423 | LY_CHECK_RET(moveto_scnode_self(set, 1, options)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6424 | } |
| 6425 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6426 | orig_used = set->used; |
| 6427 | for (i = 0; i < orig_used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 6428 | if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) { |
| 6429 | if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6430 | continue; |
| 6431 | } |
| 6432 | |
| 6433 | /* remember context node */ |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 6434 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED; |
Michal Vasko | ec4df48 | 2019-12-16 10:02:18 +0100 | [diff] [blame] | 6435 | } else { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 6436 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6437 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6438 | |
| 6439 | node = set->val.scnodes[i].scnode; |
| 6440 | |
| 6441 | if (set->val.scnodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6442 | new_node = lysc_data_parent(node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6443 | } else { |
| 6444 | /* root does not have a parent */ |
| 6445 | continue; |
| 6446 | } |
| 6447 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6448 | if (!new_node) { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 6449 | /* node has no parent */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6450 | new_type = set->root_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6451 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6452 | } else { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 6453 | /* 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] | 6454 | new_type = LYXP_NODE_ELEM; |
| 6455 | } |
| 6456 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame] | 6457 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, new_node, new_type, &idx)); |
| 6458 | if ((idx < orig_used) && (idx > i)) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 6459 | set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6460 | temp_ctx = 1; |
| 6461 | } |
| 6462 | } |
| 6463 | |
| 6464 | if (temp_ctx) { |
| 6465 | for (i = 0; i < orig_used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 6466 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NEW_CTX) { |
| 6467 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6468 | } |
| 6469 | } |
| 6470 | } |
| 6471 | |
| 6472 | return LY_SUCCESS; |
| 6473 | } |
| 6474 | |
| 6475 | /** |
| 6476 | * @brief Move context @p set to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'. |
| 6477 | * Result is LYXP_SET_BOOLEAN. Indirectly context position aware. |
| 6478 | * |
| 6479 | * @param[in,out] set1 Set to use for the result. |
| 6480 | * @param[in] set2 Set acting as the second operand for @p op. |
| 6481 | * @param[in] op Comparison operator to process. |
| 6482 | * @param[in] options XPath options. |
| 6483 | * @return LY_ERR |
| 6484 | */ |
| 6485 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6486 | 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] | 6487 | { |
| 6488 | /* |
| 6489 | * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING |
| 6490 | * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING) |
| 6491 | * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 6492 | * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 6493 | * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING |
| 6494 | * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6495 | * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 6496 | * |
| 6497 | * '=' or '!=' |
| 6498 | * BOOLEAN + BOOLEAN |
| 6499 | * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 6500 | * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 6501 | * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 6502 | * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 6503 | * NUMBER + NUMBER |
| 6504 | * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6505 | * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 6506 | * STRING + STRING |
| 6507 | * |
| 6508 | * '<=', '<', '>=', '>' |
| 6509 | * NUMBER + NUMBER |
| 6510 | * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 6511 | * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 6512 | * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 6513 | * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6514 | * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 6515 | * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 6516 | * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6517 | * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6518 | */ |
| 6519 | struct lyxp_set iter1, iter2; |
| 6520 | int result; |
| 6521 | int64_t i; |
| 6522 | LY_ERR rc; |
| 6523 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6524 | memset(&iter1, 0, sizeof iter1); |
| 6525 | memset(&iter2, 0, sizeof iter2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6526 | |
| 6527 | /* iterative evaluation with node-sets */ |
| 6528 | if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) { |
| 6529 | if (set1->type == LYXP_SET_NODE_SET) { |
| 6530 | for (i = 0; i < set1->used; ++i) { |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6531 | /* cast set1 */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6532 | switch (set2->type) { |
| 6533 | case LYXP_SET_NUMBER: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6534 | rc = set_comp_cast(&iter1, set1, LYXP_SET_NUMBER, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6535 | break; |
| 6536 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6537 | rc = set_comp_cast(&iter1, set1, LYXP_SET_BOOLEAN, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6538 | break; |
| 6539 | default: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6540 | rc = set_comp_cast(&iter1, set1, LYXP_SET_STRING, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6541 | break; |
| 6542 | } |
| 6543 | LY_CHECK_RET(rc); |
| 6544 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6545 | /* canonize set2 */ |
| 6546 | LY_CHECK_ERR_RET(rc = set_comp_canonize(&iter2, set2, &set1->val.nodes[i]), lyxp_set_free_content(&iter1), rc); |
| 6547 | |
| 6548 | /* compare recursively */ |
| 6549 | rc = moveto_op_comp(&iter1, &iter2, op, options); |
| 6550 | lyxp_set_free_content(&iter2); |
| 6551 | LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6552 | |
| 6553 | /* lazy evaluation until true */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6554 | if (iter1.val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6555 | set_fill_boolean(set1, 1); |
| 6556 | return LY_SUCCESS; |
| 6557 | } |
| 6558 | } |
| 6559 | } else { |
| 6560 | for (i = 0; i < set2->used; ++i) { |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6561 | /* set set2 */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6562 | switch (set1->type) { |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6563 | case LYXP_SET_NUMBER: |
| 6564 | rc = set_comp_cast(&iter2, set2, LYXP_SET_NUMBER, i); |
| 6565 | break; |
| 6566 | case LYXP_SET_BOOLEAN: |
| 6567 | rc = set_comp_cast(&iter2, set2, LYXP_SET_BOOLEAN, i); |
| 6568 | break; |
| 6569 | default: |
| 6570 | rc = set_comp_cast(&iter2, set2, LYXP_SET_STRING, i); |
| 6571 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6572 | } |
| 6573 | LY_CHECK_RET(rc); |
| 6574 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6575 | /* canonize set1 */ |
| 6576 | 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] | 6577 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6578 | /* compare recursively */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6579 | rc = moveto_op_comp(&iter1, &iter2, op, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6580 | lyxp_set_free_content(&iter2); |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6581 | LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6582 | |
| 6583 | /* lazy evaluation until true */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6584 | if (iter1.val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6585 | set_fill_boolean(set1, 1); |
| 6586 | return LY_SUCCESS; |
| 6587 | } |
| 6588 | } |
| 6589 | } |
| 6590 | |
| 6591 | /* false for all nodes */ |
| 6592 | set_fill_boolean(set1, 0); |
| 6593 | return LY_SUCCESS; |
| 6594 | } |
| 6595 | |
| 6596 | /* first convert properly */ |
| 6597 | if ((op[0] == '=') || (op[0] == '!')) { |
| 6598 | if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6599 | lyxp_set_cast(set1, LYXP_SET_BOOLEAN); |
| 6600 | lyxp_set_cast(set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6601 | } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6602 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6603 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6604 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6605 | LY_CHECK_RET(rc); |
| 6606 | } /* else we have 2 strings */ |
| 6607 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6608 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6609 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6610 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6611 | LY_CHECK_RET(rc); |
| 6612 | } |
| 6613 | |
| 6614 | assert(set1->type == set2->type); |
| 6615 | |
| 6616 | /* compute result */ |
| 6617 | if (op[0] == '=') { |
| 6618 | if (set1->type == LYXP_SET_BOOLEAN) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6619 | result = (set1->val.bln == set2->val.bln); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6620 | } else if (set1->type == LYXP_SET_NUMBER) { |
| 6621 | result = (set1->val.num == set2->val.num); |
| 6622 | } else { |
| 6623 | assert(set1->type == LYXP_SET_STRING); |
Michal Vasko | ac6c72f | 2019-11-14 16:09:34 +0100 | [diff] [blame] | 6624 | result = !strcmp(set1->val.str, set2->val.str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6625 | } |
| 6626 | } else if (op[0] == '!') { |
| 6627 | if (set1->type == LYXP_SET_BOOLEAN) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6628 | result = (set1->val.bln != set2->val.bln); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6629 | } else if (set1->type == LYXP_SET_NUMBER) { |
| 6630 | result = (set1->val.num != set2->val.num); |
| 6631 | } else { |
| 6632 | assert(set1->type == LYXP_SET_STRING); |
Michal Vasko | c205843 | 2020-11-06 17:26:21 +0100 | [diff] [blame] | 6633 | result = strcmp(set1->val.str, set2->val.str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6634 | } |
| 6635 | } else { |
| 6636 | assert(set1->type == LYXP_SET_NUMBER); |
| 6637 | if (op[0] == '<') { |
| 6638 | if (op[1] == '=') { |
| 6639 | result = (set1->val.num <= set2->val.num); |
| 6640 | } else { |
| 6641 | result = (set1->val.num < set2->val.num); |
| 6642 | } |
| 6643 | } else { |
| 6644 | if (op[1] == '=') { |
| 6645 | result = (set1->val.num >= set2->val.num); |
| 6646 | } else { |
| 6647 | result = (set1->val.num > set2->val.num); |
| 6648 | } |
| 6649 | } |
| 6650 | } |
| 6651 | |
| 6652 | /* assign result */ |
| 6653 | if (result) { |
| 6654 | set_fill_boolean(set1, 1); |
| 6655 | } else { |
| 6656 | set_fill_boolean(set1, 0); |
| 6657 | } |
| 6658 | |
| 6659 | return LY_SUCCESS; |
| 6660 | } |
| 6661 | |
| 6662 | /** |
| 6663 | * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div', |
| 6664 | * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware. |
| 6665 | * |
| 6666 | * @param[in,out] set1 Set to use for the result. |
| 6667 | * @param[in] set2 Set acting as the second operand for @p op. |
| 6668 | * @param[in] op Operator to process. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6669 | * @return LY_ERR |
| 6670 | */ |
| 6671 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6672 | 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] | 6673 | { |
| 6674 | LY_ERR rc; |
| 6675 | |
| 6676 | /* unary '-' */ |
| 6677 | if (!set2 && (op[0] == '-')) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6678 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6679 | LY_CHECK_RET(rc); |
| 6680 | set1->val.num *= -1; |
| 6681 | lyxp_set_free(set2); |
| 6682 | return LY_SUCCESS; |
| 6683 | } |
| 6684 | |
| 6685 | assert(set1 && set2); |
| 6686 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6687 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6688 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6689 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6690 | LY_CHECK_RET(rc); |
| 6691 | |
| 6692 | switch (op[0]) { |
| 6693 | /* '+' */ |
| 6694 | case '+': |
| 6695 | set1->val.num += set2->val.num; |
| 6696 | break; |
| 6697 | |
| 6698 | /* '-' */ |
| 6699 | case '-': |
| 6700 | set1->val.num -= set2->val.num; |
| 6701 | break; |
| 6702 | |
| 6703 | /* '*' */ |
| 6704 | case '*': |
| 6705 | set1->val.num *= set2->val.num; |
| 6706 | break; |
| 6707 | |
| 6708 | /* 'div' */ |
| 6709 | case 'd': |
| 6710 | set1->val.num /= set2->val.num; |
| 6711 | break; |
| 6712 | |
| 6713 | /* 'mod' */ |
| 6714 | case 'm': |
| 6715 | set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num); |
| 6716 | break; |
| 6717 | |
| 6718 | default: |
| 6719 | LOGINT_RET(set1->ctx); |
| 6720 | } |
| 6721 | |
| 6722 | return LY_SUCCESS; |
| 6723 | } |
| 6724 | |
| 6725 | /* |
| 6726 | * eval functions |
| 6727 | * |
| 6728 | * They execute a parsed XPath expression on some data subtree. |
| 6729 | */ |
| 6730 | |
| 6731 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6732 | * @brief Evaluate Predicate. Logs directly on error. |
| 6733 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6734 | * [9] Predicate ::= '[' Expr ']' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6735 | * |
| 6736 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6737 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6738 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 6739 | * @param[in] options XPath options. |
| 6740 | * @param[in] parent_pos_pred Whether parent predicate was a positional one. |
| 6741 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6742 | */ |
| 6743 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 6744 | 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] | 6745 | { |
| 6746 | LY_ERR rc; |
Michal Vasko | 1fdd8fa | 2021-01-08 09:21:45 +0100 | [diff] [blame] | 6747 | uint16_t orig_exp; |
| 6748 | uint32_t i, orig_pos, orig_size; |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6749 | int32_t pred_in_ctx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6750 | struct lyxp_set set2; |
| 6751 | struct lyd_node *orig_parent; |
| 6752 | |
| 6753 | /* '[' */ |
| 6754 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 6755 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6756 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6757 | |
| 6758 | if (!set) { |
| 6759 | only_parse: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6760 | rc = eval_expr_select(exp, tok_idx, 0, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6761 | LY_CHECK_RET(rc); |
| 6762 | } else if (set->type == LYXP_SET_NODE_SET) { |
| 6763 | /* 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] | 6764 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6765 | |
| 6766 | /* empty set, nothing to evaluate */ |
| 6767 | if (!set->used) { |
| 6768 | goto only_parse; |
| 6769 | } |
| 6770 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6771 | orig_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6772 | orig_pos = 0; |
| 6773 | orig_size = set->used; |
| 6774 | orig_parent = NULL; |
Michal Vasko | 39dbf35 | 2020-05-21 10:08:59 +0200 | [diff] [blame] | 6775 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6776 | set_init(&set2, set); |
| 6777 | set_insert_node(&set2, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, 0); |
| 6778 | /* remember the node context position for position() and context size for last(), |
| 6779 | * predicates should always be evaluated with respect to the child axis (since we do |
| 6780 | * not support explicit axes) so we assign positions based on their parents */ |
Michal Vasko | 9e68508 | 2021-01-29 14:49:09 +0100 | [diff] [blame] | 6781 | if (parent_pos_pred && (lyd_parent(set->val.nodes[i].node) != orig_parent)) { |
| 6782 | orig_parent = lyd_parent(set->val.nodes[i].node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6783 | orig_pos = 1; |
| 6784 | } else { |
| 6785 | ++orig_pos; |
| 6786 | } |
| 6787 | |
| 6788 | set2.ctx_pos = orig_pos; |
| 6789 | set2.ctx_size = orig_size; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6790 | *tok_idx = orig_exp; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6791 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6792 | rc = eval_expr_select(exp, tok_idx, 0, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6793 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6794 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6795 | return rc; |
| 6796 | } |
| 6797 | |
| 6798 | /* number is a position */ |
| 6799 | if (set2.type == LYXP_SET_NUMBER) { |
| 6800 | if ((long long)set2.val.num == orig_pos) { |
| 6801 | set2.val.num = 1; |
| 6802 | } else { |
| 6803 | set2.val.num = 0; |
| 6804 | } |
| 6805 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6806 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6807 | |
| 6808 | /* predicate satisfied or not? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6809 | if (!set2.val.bln) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6810 | set_remove_node_none(set, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6811 | } |
| 6812 | } |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6813 | set_remove_nodes_none(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6814 | |
| 6815 | } else if (set->type == LYXP_SET_SCNODE_SET) { |
| 6816 | for (i = 0; i < set->used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 6817 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6818 | /* there is a currently-valid node */ |
| 6819 | break; |
| 6820 | } |
| 6821 | } |
| 6822 | /* empty set, nothing to evaluate */ |
| 6823 | if (i == set->used) { |
| 6824 | goto only_parse; |
| 6825 | } |
| 6826 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6827 | orig_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6828 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6829 | /* set special in_ctx to all the valid snodes */ |
| 6830 | pred_in_ctx = set_scnode_new_in_ctx(set); |
| 6831 | |
| 6832 | /* use the valid snodes one-by-one */ |
| 6833 | for (i = 0; i < set->used; ++i) { |
| 6834 | if (set->val.scnodes[i].in_ctx != pred_in_ctx) { |
| 6835 | continue; |
| 6836 | } |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 6837 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6838 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6839 | *tok_idx = orig_exp; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6840 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6841 | rc = eval_expr_select(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6842 | LY_CHECK_RET(rc); |
| 6843 | |
| 6844 | set->val.scnodes[i].in_ctx = pred_in_ctx; |
| 6845 | } |
| 6846 | |
| 6847 | /* restore the state as it was before the predicate */ |
| 6848 | for (i = 0; i < set->used; ++i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 6849 | if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) { |
| 6850 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6851 | } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 6852 | set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6853 | } |
| 6854 | } |
| 6855 | |
| 6856 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6857 | set2.type = LYXP_SET_NODE_SET; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6858 | set_fill_set(&set2, set); |
| 6859 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6860 | rc = eval_expr_select(exp, tok_idx, 0, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6861 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6862 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6863 | return rc; |
| 6864 | } |
| 6865 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6866 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6867 | if (!set2.val.bln) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6868 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6869 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6870 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6871 | } |
| 6872 | |
| 6873 | /* ']' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6874 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 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 | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6878 | |
| 6879 | return LY_SUCCESS; |
| 6880 | } |
| 6881 | |
| 6882 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6883 | * @brief Evaluate Literal. Logs directly on error. |
| 6884 | * |
| 6885 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6886 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6887 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 6888 | */ |
| 6889 | static void |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 6890 | 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] | 6891 | { |
| 6892 | if (set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6893 | if (exp->tok_len[*tok_idx] == 2) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6894 | set_fill_string(set, "", 0); |
| 6895 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6896 | 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] | 6897 | } |
| 6898 | } |
| 6899 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 6900 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6901 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6902 | } |
| 6903 | |
| 6904 | /** |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6905 | * @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] | 6906 | * |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6907 | * @param[in] exp Full parsed XPath expression. |
| 6908 | * @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] | 6909 | * @param[in] ctx_node Found schema node as the context for the predicate. |
| 6910 | * @param[in] cur_mod Current module for the expression. |
| 6911 | * @param[in] cur_node Current (original context) node. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6912 | * @param[in] format Format of any prefixes in key names/values. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6913 | * @param[in] prefix_data Format-specific prefix data (see ::ly_resolve_prefix). |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6914 | * @param[out] predicates Parsed predicates. |
| 6915 | * @param[out] pred_type Type of @p predicates. |
| 6916 | * @return LY_SUCCESS on success, |
| 6917 | * @return LY_ERR on any error. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6918 | */ |
| 6919 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 6920 | 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] | 6921 | const struct lys_module *cur_mod, const struct lysc_node *cur_node, LY_PREFIX_FORMAT format, void *prefix_data, |
| 6922 | struct ly_path_predicate **predicates, enum ly_path_pred_type *pred_type) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6923 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6924 | LY_ERR ret = LY_SUCCESS; |
| 6925 | uint16_t key_count, e_idx, pred_idx = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6926 | const struct lysc_node *key; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6927 | size_t pred_len; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6928 | uint32_t prev_lo; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6929 | struct lyxp_expr *exp2 = NULL; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6930 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6931 | assert(ctx_node->nodetype & (LYS_LIST | LYS_LEAFLIST)); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6932 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6933 | if (ctx_node->nodetype == LYS_LIST) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6934 | /* get key count */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6935 | if (ctx_node->flags & LYS_KEYLESS) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6936 | return LY_EINVAL; |
| 6937 | } |
Michal Vasko | 544e58a | 2021-01-28 14:33:41 +0100 | [diff] [blame] | 6938 | for (key_count = 0, key = lysc_node_child(ctx_node); key && (key->flags & LYS_KEY); key = key->next, ++key_count) {} |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6939 | assert(key_count); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6940 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6941 | /* learn where the predicates end */ |
| 6942 | e_idx = *tok_idx; |
| 6943 | while (key_count) { |
| 6944 | /* '[' */ |
| 6945 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) { |
| 6946 | return LY_EINVAL; |
| 6947 | } |
| 6948 | ++e_idx; |
| 6949 | |
Michal Vasko | 3354d27 | 2021-04-06 09:40:06 +0200 | [diff] [blame] | 6950 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_NAMETEST)) { |
| 6951 | /* definitely not a key */ |
| 6952 | return LY_EINVAL; |
| 6953 | } |
| 6954 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6955 | /* ']' */ |
| 6956 | while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) { |
| 6957 | ++e_idx; |
| 6958 | } |
| 6959 | ++e_idx; |
| 6960 | |
| 6961 | /* another presumably key predicate parsed */ |
| 6962 | --key_count; |
| 6963 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6964 | } else { |
| 6965 | /* learn just where this single predicate ends */ |
| 6966 | e_idx = *tok_idx; |
| 6967 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6968 | /* '[' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6969 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) { |
| 6970 | return LY_EINVAL; |
| 6971 | } |
| 6972 | ++e_idx; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6973 | |
Michal Vasko | 3354d27 | 2021-04-06 09:40:06 +0200 | [diff] [blame] | 6974 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_DOT)) { |
| 6975 | /* definitely not the value */ |
| 6976 | return LY_EINVAL; |
| 6977 | } |
| 6978 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6979 | /* ']' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6980 | while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) { |
| 6981 | ++e_idx; |
| 6982 | } |
| 6983 | ++e_idx; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6984 | } |
| 6985 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6986 | /* get the joined length of all the keys predicates/of the single leaf-list predicate */ |
| 6987 | pred_len = (exp->tok_pos[e_idx - 1] + exp->tok_len[e_idx - 1]) - exp->tok_pos[*tok_idx]; |
| 6988 | |
| 6989 | /* turn logging off */ |
| 6990 | prev_lo = ly_log_options(0); |
| 6991 | |
| 6992 | /* parse the predicate(s) */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6993 | LY_CHECK_GOTO(ret = ly_path_parse_predicate(ctx_node->module->ctx, ctx_node, exp->expr + exp->tok_pos[*tok_idx], |
| 6994 | pred_len, LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp2), cleanup); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6995 | |
| 6996 | /* compile */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 6997 | ret = ly_path_compile_predicate(ctx_node->module->ctx, cur_node, cur_mod, ctx_node, exp2, &pred_idx, format, |
| 6998 | prefix_data, predicates, pred_type); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6999 | LY_CHECK_GOTO(ret, cleanup); |
| 7000 | |
| 7001 | /* success, the predicate must include all the needed information for hash-based search */ |
| 7002 | *tok_idx = e_idx; |
| 7003 | |
| 7004 | cleanup: |
| 7005 | ly_log_options(prev_lo); |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7006 | lyxp_expr_free(ctx_node->module->ctx, exp2); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7007 | return ret; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7008 | } |
| 7009 | |
| 7010 | /** |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7011 | * @brief Search for/check the next schema node that could be the only matching schema node meaning the |
| 7012 | * data node(s) could be found using a single hash-based search. |
| 7013 | * |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7014 | * @param[in] ctx libyang context. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7015 | * @param[in] node Next context node to check. |
| 7016 | * @param[in] name Expected node name. |
| 7017 | * @param[in] name_len Length of @p name. |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7018 | * @param[in] moveto_mod Expected node module, can be NULL for JSON format with no prefix. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7019 | * @param[in] root_type XPath root type. |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7020 | * @param[in] format Prefix format. |
| 7021 | * @param[in,out] found Previously found node, is updated. |
| 7022 | * @return LY_SUCCESS on success, |
| 7023 | * @return LY_ENOT if the whole check failed and hashes cannot be used. |
| 7024 | */ |
| 7025 | static LY_ERR |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7026 | eval_name_test_with_predicate_get_scnode(const struct ly_ctx *ctx, const struct lyd_node *node, const char *name, |
| 7027 | uint16_t name_len, const struct lys_module *moveto_mod, enum lyxp_node_type root_type, LY_PREFIX_FORMAT format, |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7028 | const struct lysc_node **found) |
| 7029 | { |
| 7030 | const struct lysc_node *scnode; |
| 7031 | const struct lys_module *mod; |
| 7032 | uint32_t idx = 0; |
| 7033 | |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7034 | assert((format == LY_PREF_JSON) || moveto_mod); |
| 7035 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7036 | continue_search: |
Michal Vasko | 7d1d091 | 2020-10-16 08:38:30 +0200 | [diff] [blame] | 7037 | scnode = NULL; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7038 | if (!node) { |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7039 | if ((format == LY_PREF_JSON) && !moveto_mod) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7040 | /* search all modules for a single match */ |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7041 | while ((mod = ly_ctx_get_module_iter(ctx, &idx))) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7042 | scnode = lys_find_child(NULL, mod, name, name_len, 0, 0); |
| 7043 | if (scnode) { |
| 7044 | /* we have found a match */ |
| 7045 | break; |
| 7046 | } |
| 7047 | } |
| 7048 | |
Michal Vasko | 7d1d091 | 2020-10-16 08:38:30 +0200 | [diff] [blame] | 7049 | if (!scnode) { |
| 7050 | /* all modules searched */ |
| 7051 | idx = 0; |
| 7052 | } |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7053 | } else { |
| 7054 | /* search in top-level */ |
| 7055 | scnode = lys_find_child(NULL, moveto_mod, name, name_len, 0, 0); |
| 7056 | } |
| 7057 | } else if (!*found || (lysc_data_parent(*found) != node->schema)) { |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7058 | if ((format == LY_PREF_JSON) && !moveto_mod) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7059 | /* we must adjust the module to inherit the one from the context node */ |
| 7060 | moveto_mod = node->schema->module; |
| 7061 | } |
| 7062 | |
| 7063 | /* search in children, do not repeat the same search */ |
| 7064 | 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] | 7065 | } /* else skip redundant search */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7066 | |
| 7067 | /* additional context check */ |
| 7068 | if (scnode && (root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) { |
| 7069 | scnode = NULL; |
| 7070 | } |
| 7071 | |
| 7072 | if (scnode) { |
| 7073 | if (*found) { |
| 7074 | /* we found a schema node with the same name but at different level, give up, too complicated |
| 7075 | * (more hash-based searches would be required, not supported) */ |
| 7076 | return LY_ENOT; |
| 7077 | } else { |
| 7078 | /* remember the found schema node and continue to make sure it can be used */ |
| 7079 | *found = scnode; |
| 7080 | } |
| 7081 | } |
| 7082 | |
| 7083 | if (idx) { |
| 7084 | /* continue searching all the following models */ |
| 7085 | goto continue_search; |
| 7086 | } |
| 7087 | |
| 7088 | return LY_SUCCESS; |
| 7089 | } |
| 7090 | |
| 7091 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7092 | * @brief Evaluate NameTest and any following Predicates. Logs directly on error. |
| 7093 | * |
| 7094 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 7095 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 7096 | * [7] NameTest ::= '*' | NCName ':' '*' | QName |
| 7097 | * |
| 7098 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7099 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7100 | * @param[in] attr_axis Whether to search attributes or standard nodes. |
| 7101 | * @param[in] all_desc Whether to search all the descendants or children only. |
| 7102 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7103 | * @param[in] options XPath options. |
| 7104 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7105 | */ |
| 7106 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 7107 | 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] | 7108 | struct lyxp_set *set, uint32_t options) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7109 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7110 | char *path; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7111 | const char *ncname, *ncname_dict = NULL; |
| 7112 | uint16_t ncname_len; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7113 | const struct lys_module *moveto_mod = NULL; |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7114 | const struct lysc_node *scnode = NULL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7115 | struct ly_path_predicate *predicates = NULL; |
| 7116 | enum ly_path_pred_type pred_type = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7117 | LY_ERR rc = LY_SUCCESS; |
| 7118 | |
| 7119 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7120 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7121 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7122 | |
| 7123 | if (!set) { |
| 7124 | goto moveto; |
| 7125 | } |
| 7126 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7127 | ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]]; |
| 7128 | ncname_len = exp->tok_len[*tok_idx - 1]; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7129 | |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7130 | if ((ncname[0] == '*') && (ncname_len == 1)) { |
| 7131 | /* all nodes will match */ |
| 7132 | goto moveto; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7133 | } |
| 7134 | |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7135 | /* parse (and skip) module name */ |
| 7136 | rc = moveto_resolve_model(&ncname, &ncname_len, set, NULL, &moveto_mod); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7137 | LY_CHECK_GOTO(rc, cleanup); |
| 7138 | |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7139 | if (((set->format == LY_PREF_JSON) || moveto_mod) && !attr_axis && !all_desc && (set->type == LYXP_SET_NODE_SET)) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7140 | /* find the matching schema node in some parent in the context */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7141 | for (uint32_t i = 0; i < set->used; ++i) { |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7142 | if (eval_name_test_with_predicate_get_scnode(set->ctx, set->val.nodes[i].node, ncname, ncname_len, |
| 7143 | moveto_mod, set->root_type, set->format, &scnode)) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7144 | /* check failed */ |
| 7145 | scnode = NULL; |
| 7146 | break; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7147 | } |
| 7148 | } |
| 7149 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7150 | if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) { |
| 7151 | /* try to create the predicates */ |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7152 | if (eval_name_test_try_compile_predicates(exp, tok_idx, scnode, set->cur_mod, set->cur_node ? |
| 7153 | 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] | 7154 | /* hashes cannot be used */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7155 | scnode = NULL; |
| 7156 | } |
| 7157 | } |
| 7158 | } |
| 7159 | |
Michal Vasko | c71c37b | 2021-01-11 13:40:02 +0100 | [diff] [blame] | 7160 | if (!scnode) { |
| 7161 | /* we are not able to match based on a schema node and not all the modules match ("*"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7162 | * use dictionary for efficient comparison */ |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 7163 | 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] | 7164 | } |
| 7165 | |
| 7166 | moveto: |
| 7167 | /* move to the attribute(s), data node(s), or schema node(s) */ |
| 7168 | if (attr_axis) { |
| 7169 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 7170 | set_scnode_clear_ctx(set); |
| 7171 | } else { |
| 7172 | if (all_desc) { |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 7173 | rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7174 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7175 | rc = moveto_attr(set, moveto_mod, ncname_dict); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7176 | } |
| 7177 | LY_CHECK_GOTO(rc, cleanup); |
| 7178 | } |
| 7179 | } else { |
| 7180 | if (set && (options & LYXP_SCNODE_ALL)) { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7181 | int64_t i; |
| 7182 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7183 | if (all_desc) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7184 | rc = moveto_scnode_alldesc(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7185 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7186 | rc = moveto_scnode(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7187 | } |
| 7188 | LY_CHECK_GOTO(rc, cleanup); |
| 7189 | |
| 7190 | for (i = set->used - 1; i > -1; --i) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 7191 | if (set->val.scnodes[i].in_ctx > LYXP_SET_SCNODE_ATOM) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7192 | break; |
| 7193 | } |
| 7194 | } |
| 7195 | if (i == -1) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 7196 | path = lysc_path(set->cur_scnode, LYSC_PATH_LOG, NULL, 0); |
Michal Vasko | 90f12dc | 2020-12-03 14:20:42 +0100 | [diff] [blame] | 7197 | LOGWRN(set->ctx, "Schema node \"%.*s\" not found (\"%.*s\") with context node \"%s\".", |
| 7198 | ncname_len, ncname, (ncname - exp->expr) + ncname_len, exp->expr, path); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7199 | free(path); |
| 7200 | } |
| 7201 | } else { |
| 7202 | if (all_desc) { |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 7203 | rc = moveto_node_alldesc(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7204 | } else { |
| 7205 | if (scnode) { |
| 7206 | /* we can find the nodes using hashes */ |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 7207 | rc = moveto_node_hash(set, scnode, predicates, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7208 | } else { |
Michal Vasko | cdad712 | 2020-11-09 21:04:44 +0100 | [diff] [blame] | 7209 | rc = moveto_node(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7210 | } |
| 7211 | } |
| 7212 | LY_CHECK_GOTO(rc, cleanup); |
| 7213 | } |
| 7214 | } |
| 7215 | |
| 7216 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7217 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 7218 | rc = eval_predicate(exp, tok_idx, set, options, 1); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7219 | LY_CHECK_RET(rc); |
| 7220 | } |
| 7221 | |
| 7222 | cleanup: |
Michal Vasko | db51a8d | 2020-05-27 15:22:29 +0200 | [diff] [blame] | 7223 | if (set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7224 | lydict_remove(set->ctx, ncname_dict); |
Michal Vasko | f7e16e2 | 2020-10-21 09:24:39 +0200 | [diff] [blame] | 7225 | ly_path_predicates_free(set->ctx, pred_type, predicates); |
Michal Vasko | db51a8d | 2020-05-27 15:22:29 +0200 | [diff] [blame] | 7226 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7227 | return rc; |
| 7228 | } |
| 7229 | |
| 7230 | /** |
| 7231 | * @brief Evaluate NodeType and any following Predicates. Logs directly on error. |
| 7232 | * |
| 7233 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 7234 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 7235 | * [8] NodeType ::= 'text' | 'node' |
| 7236 | * |
| 7237 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7238 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7239 | * @param[in] attr_axis Whether to search attributes or standard nodes. |
| 7240 | * @param[in] all_desc Whether to search all the descendants or children only. |
| 7241 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7242 | * @param[in] options XPath options. |
| 7243 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7244 | */ |
| 7245 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 7246 | 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] | 7247 | struct lyxp_set *set, uint32_t options) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7248 | { |
| 7249 | LY_ERR rc; |
| 7250 | |
| 7251 | /* TODO */ |
| 7252 | (void)attr_axis; |
| 7253 | (void)all_desc; |
| 7254 | |
| 7255 | if (set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7256 | assert(exp->tok_len[*tok_idx] == 4); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7257 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 7258 | set_scnode_clear_ctx(set); |
| 7259 | /* just for the debug message below */ |
| 7260 | set = NULL; |
| 7261 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7262 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7263 | rc = xpath_node(NULL, 0, set, options); |
| 7264 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7265 | assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4)); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7266 | rc = xpath_text(NULL, 0, set, options); |
| 7267 | } |
| 7268 | LY_CHECK_RET(rc); |
| 7269 | } |
| 7270 | } |
| 7271 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7272 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7273 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7274 | |
| 7275 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7276 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7277 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7278 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7279 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7280 | |
| 7281 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7282 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7283 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7284 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7285 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7286 | |
| 7287 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7288 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 7289 | rc = eval_predicate(exp, tok_idx, set, options, 1); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7290 | LY_CHECK_RET(rc); |
| 7291 | } |
| 7292 | |
| 7293 | return LY_SUCCESS; |
| 7294 | } |
| 7295 | |
| 7296 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7297 | * @brief Evaluate RelativeLocationPath. Logs directly on error. |
| 7298 | * |
| 7299 | * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step |
| 7300 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7301 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7302 | * |
| 7303 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7304 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7305 | * @param[in] all_desc Whether to search all the descendants or children only. |
| 7306 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7307 | * @param[in] options XPath options. |
| 7308 | * @return LY_ERR (YL_EINCOMPLETE on unresolved when) |
| 7309 | */ |
| 7310 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 7311 | eval_relative_location_path(const struct lyxp_expr *exp, uint16_t *tok_idx, ly_bool all_desc, struct lyxp_set *set, |
| 7312 | uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7313 | { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 7314 | ly_bool attr_axis; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7315 | LY_ERR rc; |
| 7316 | |
| 7317 | goto step; |
| 7318 | do { |
| 7319 | /* evaluate '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7320 | if (exp->tok_len[*tok_idx] == 1) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7321 | all_desc = 0; |
| 7322 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7323 | assert(exp->tok_len[*tok_idx] == 2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7324 | all_desc = 1; |
| 7325 | } |
| 7326 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7327 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7328 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7329 | |
| 7330 | step: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7331 | /* evaluate abbreviated axis '@'? if any */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7332 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7333 | attr_axis = 1; |
| 7334 | |
| 7335 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7336 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7337 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7338 | } else { |
| 7339 | attr_axis = 0; |
| 7340 | } |
| 7341 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7342 | /* Step */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7343 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7344 | case LYXP_TOKEN_DOT: |
| 7345 | /* evaluate '.' */ |
| 7346 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 7347 | rc = moveto_scnode_self(set, all_desc, options); |
| 7348 | } else { |
| 7349 | rc = moveto_self(set, all_desc, options); |
| 7350 | } |
| 7351 | LY_CHECK_RET(rc); |
| 7352 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7353 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7354 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7355 | break; |
| 7356 | |
| 7357 | case LYXP_TOKEN_DDOT: |
| 7358 | /* evaluate '..' */ |
| 7359 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 7360 | rc = moveto_scnode_parent(set, all_desc, options); |
| 7361 | } else { |
| 7362 | rc = moveto_parent(set, all_desc, options); |
| 7363 | } |
| 7364 | LY_CHECK_RET(rc); |
| 7365 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7366 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7367 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7368 | break; |
| 7369 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7370 | case LYXP_TOKEN_NAMETEST: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7371 | /* evaluate NameTest Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7372 | 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] | 7373 | LY_CHECK_RET(rc); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7374 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7375 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7376 | case LYXP_TOKEN_NODETYPE: |
| 7377 | /* evaluate NodeType Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7378 | 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] | 7379 | LY_CHECK_RET(rc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7380 | break; |
| 7381 | |
| 7382 | default: |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 7383 | LOGINT_RET(set ? set->ctx : NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7384 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7385 | } 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] | 7386 | |
| 7387 | return LY_SUCCESS; |
| 7388 | } |
| 7389 | |
| 7390 | /** |
| 7391 | * @brief Evaluate AbsoluteLocationPath. Logs directly on error. |
| 7392 | * |
| 7393 | * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath |
| 7394 | * |
| 7395 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7396 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7397 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7398 | * @param[in] options XPath options. |
| 7399 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7400 | */ |
| 7401 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 7402 | 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] | 7403 | { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 7404 | ly_bool all_desc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7405 | |
| 7406 | if (set) { |
| 7407 | /* no matter what tokens follow, we need to be at the root */ |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 7408 | LY_CHECK_RET(moveto_root(set, options)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7409 | } |
| 7410 | |
| 7411 | /* '/' RelativeLocationPath? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7412 | if (exp->tok_len[*tok_idx] == 1) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7413 | /* evaluate '/' - deferred */ |
| 7414 | all_desc = 0; |
| 7415 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7416 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7417 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7418 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7419 | if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7420 | return LY_SUCCESS; |
| 7421 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7422 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7423 | case LYXP_TOKEN_DOT: |
| 7424 | case LYXP_TOKEN_DDOT: |
| 7425 | case LYXP_TOKEN_AT: |
| 7426 | case LYXP_TOKEN_NAMETEST: |
| 7427 | case LYXP_TOKEN_NODETYPE: |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 7428 | 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] | 7429 | break; |
| 7430 | default: |
| 7431 | break; |
| 7432 | } |
| 7433 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7434 | } else { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 7435 | /* '//' RelativeLocationPath */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7436 | /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */ |
| 7437 | all_desc = 1; |
| 7438 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7439 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7440 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7441 | |
Michal Vasko | b0099a9 | 2020-08-31 14:55:23 +0200 | [diff] [blame] | 7442 | 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] | 7443 | } |
| 7444 | |
| 7445 | return LY_SUCCESS; |
| 7446 | } |
| 7447 | |
| 7448 | /** |
| 7449 | * @brief Evaluate FunctionCall. Logs directly on error. |
| 7450 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7451 | * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7452 | * |
| 7453 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7454 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7455 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7456 | * @param[in] options XPath options. |
| 7457 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7458 | */ |
| 7459 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 7460 | 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] | 7461 | { |
| 7462 | LY_ERR rc; |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7463 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7464 | 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] | 7465 | uint16_t arg_count = 0, i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7466 | struct lyxp_set **args = NULL, **args_aux; |
| 7467 | |
| 7468 | if (set) { |
| 7469 | /* FunctionName */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7470 | switch (exp->tok_len[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7471 | case 3: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7472 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7473 | xpath_func = &xpath_not; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7474 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7475 | xpath_func = &xpath_sum; |
| 7476 | } |
| 7477 | break; |
| 7478 | case 4: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7479 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7480 | xpath_func = &xpath_lang; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7481 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7482 | xpath_func = &xpath_last; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7483 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7484 | xpath_func = &xpath_name; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7485 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7486 | xpath_func = &xpath_true; |
| 7487 | } |
| 7488 | break; |
| 7489 | case 5: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7490 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7491 | xpath_func = &xpath_count; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7492 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7493 | xpath_func = &xpath_false; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7494 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7495 | xpath_func = &xpath_floor; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7496 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7497 | xpath_func = &xpath_round; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7498 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7499 | xpath_func = &xpath_deref; |
| 7500 | } |
| 7501 | break; |
| 7502 | case 6: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7503 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7504 | xpath_func = &xpath_concat; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7505 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7506 | xpath_func = &xpath_number; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7507 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7508 | xpath_func = &xpath_string; |
| 7509 | } |
| 7510 | break; |
| 7511 | case 7: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7512 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7513 | xpath_func = &xpath_boolean; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7514 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7515 | xpath_func = &xpath_ceiling; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7516 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7517 | xpath_func = &xpath_current; |
| 7518 | } |
| 7519 | break; |
| 7520 | case 8: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7521 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7522 | xpath_func = &xpath_contains; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7523 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7524 | xpath_func = &xpath_position; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7525 | } 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] | 7526 | xpath_func = &xpath_re_match; |
| 7527 | } |
| 7528 | break; |
| 7529 | case 9: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7530 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7531 | xpath_func = &xpath_substring; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7532 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7533 | xpath_func = &xpath_translate; |
| 7534 | } |
| 7535 | break; |
| 7536 | case 10: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7537 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7538 | xpath_func = &xpath_local_name; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7539 | } 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] | 7540 | xpath_func = &xpath_enum_value; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7541 | } 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] | 7542 | xpath_func = &xpath_bit_is_set; |
| 7543 | } |
| 7544 | break; |
| 7545 | case 11: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7546 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7547 | xpath_func = &xpath_starts_with; |
| 7548 | } |
| 7549 | break; |
| 7550 | case 12: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7551 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7552 | xpath_func = &xpath_derived_from; |
| 7553 | } |
| 7554 | break; |
| 7555 | case 13: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7556 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7557 | xpath_func = &xpath_namespace_uri; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7558 | } 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] | 7559 | xpath_func = &xpath_string_length; |
| 7560 | } |
| 7561 | break; |
| 7562 | case 15: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7563 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7564 | xpath_func = &xpath_normalize_space; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7565 | } 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] | 7566 | xpath_func = &xpath_substring_after; |
| 7567 | } |
| 7568 | break; |
| 7569 | case 16: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7570 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7571 | xpath_func = &xpath_substring_before; |
| 7572 | } |
| 7573 | break; |
| 7574 | case 20: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7575 | 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] | 7576 | xpath_func = &xpath_derived_from_or_self; |
| 7577 | } |
| 7578 | break; |
| 7579 | } |
| 7580 | |
| 7581 | if (!xpath_func) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 7582 | LOGVAL(set->ctx, LY_VCODE_XP_INFUNC, exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7583 | return LY_EVALID; |
| 7584 | } |
| 7585 | } |
| 7586 | |
| 7587 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7588 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7589 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7590 | |
| 7591 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7592 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7593 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7594 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7595 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7596 | |
| 7597 | /* ( Expr ( ',' Expr )* )? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7598 | if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7599 | if (set) { |
| 7600 | args = malloc(sizeof *args); |
| 7601 | LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup); |
| 7602 | arg_count = 1; |
| 7603 | args[0] = set_copy(set); |
| 7604 | if (!args[0]) { |
| 7605 | rc = LY_EMEM; |
| 7606 | goto cleanup; |
| 7607 | } |
| 7608 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7609 | rc = eval_expr_select(exp, tok_idx, 0, args[0], options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7610 | LY_CHECK_GOTO(rc, cleanup); |
| 7611 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7612 | rc = eval_expr_select(exp, tok_idx, 0, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7613 | LY_CHECK_GOTO(rc, cleanup); |
| 7614 | } |
| 7615 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7616 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7617 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7618 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7619 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7620 | |
| 7621 | if (set) { |
| 7622 | ++arg_count; |
| 7623 | args_aux = realloc(args, arg_count * sizeof *args); |
| 7624 | LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup); |
| 7625 | args = args_aux; |
| 7626 | args[arg_count - 1] = set_copy(set); |
| 7627 | if (!args[arg_count - 1]) { |
| 7628 | rc = LY_EMEM; |
| 7629 | goto cleanup; |
| 7630 | } |
| 7631 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7632 | 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] | 7633 | LY_CHECK_GOTO(rc, cleanup); |
| 7634 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7635 | rc = eval_expr_select(exp, tok_idx, 0, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7636 | LY_CHECK_GOTO(rc, cleanup); |
| 7637 | } |
| 7638 | } |
| 7639 | |
| 7640 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7641 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7642 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7643 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7644 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7645 | |
| 7646 | if (set) { |
| 7647 | /* evaluate function */ |
| 7648 | rc = xpath_func(args, arg_count, set, options); |
| 7649 | |
| 7650 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7651 | /* merge all nodes from arg evaluations */ |
| 7652 | for (i = 0; i < arg_count; ++i) { |
| 7653 | set_scnode_clear_ctx(args[i]); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7654 | lyxp_set_scnode_merge(set, args[i]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7655 | } |
| 7656 | } |
| 7657 | } else { |
| 7658 | rc = LY_SUCCESS; |
| 7659 | } |
| 7660 | |
| 7661 | cleanup: |
| 7662 | for (i = 0; i < arg_count; ++i) { |
| 7663 | lyxp_set_free(args[i]); |
| 7664 | } |
| 7665 | free(args); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7666 | return rc; |
| 7667 | } |
| 7668 | |
| 7669 | /** |
| 7670 | * @brief Evaluate Number. Logs directly on error. |
| 7671 | * |
| 7672 | * @param[in] ctx Context for errors. |
| 7673 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7674 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7675 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7676 | * @return LY_ERR |
| 7677 | */ |
| 7678 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 7679 | 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] | 7680 | { |
| 7681 | long double num; |
| 7682 | char *endptr; |
| 7683 | |
| 7684 | if (set) { |
| 7685 | errno = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7686 | num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7687 | if (errno) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 7688 | LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]); |
| 7689 | LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double (%s).", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7690 | 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] | 7691 | return LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7692 | } else if (endptr - &exp->expr[exp->tok_pos[*tok_idx]] != exp->tok_len[*tok_idx]) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 7693 | LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]); |
| 7694 | LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double.", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7695 | exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7696 | return LY_EVALID; |
| 7697 | } |
| 7698 | |
| 7699 | set_fill_number(set, num); |
| 7700 | } |
| 7701 | |
| 7702 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7703 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7704 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7705 | return LY_SUCCESS; |
| 7706 | } |
| 7707 | |
| 7708 | /** |
| 7709 | * @brief Evaluate PathExpr. Logs directly on error. |
| 7710 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7711 | * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate* |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7712 | * | PrimaryExpr Predicate* '/' RelativeLocationPath |
| 7713 | * | PrimaryExpr Predicate* '//' RelativeLocationPath |
| 7714 | * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7715 | * [10] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7716 | * |
| 7717 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7718 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7719 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7720 | * @param[in] options XPath options. |
| 7721 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7722 | */ |
| 7723 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 7724 | 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] | 7725 | { |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 7726 | ly_bool all_desc, parent_pos_pred; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7727 | LY_ERR rc; |
| 7728 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7729 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7730 | case LYXP_TOKEN_PAR1: |
| 7731 | /* '(' Expr ')' */ |
| 7732 | |
| 7733 | /* '(' */ |
| 7734 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7735 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7736 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7737 | |
| 7738 | /* Expr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7739 | rc = eval_expr_select(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7740 | LY_CHECK_RET(rc); |
| 7741 | |
| 7742 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7743 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7744 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7745 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7746 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7747 | |
| 7748 | parent_pos_pred = 0; |
| 7749 | goto predicate; |
| 7750 | |
| 7751 | case LYXP_TOKEN_DOT: |
| 7752 | case LYXP_TOKEN_DDOT: |
| 7753 | case LYXP_TOKEN_AT: |
| 7754 | case LYXP_TOKEN_NAMETEST: |
| 7755 | case LYXP_TOKEN_NODETYPE: |
| 7756 | /* RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7757 | rc = eval_relative_location_path(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7758 | LY_CHECK_RET(rc); |
| 7759 | break; |
| 7760 | |
| 7761 | case LYXP_TOKEN_FUNCNAME: |
| 7762 | /* FunctionCall */ |
| 7763 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7764 | rc = eval_function_call(exp, tok_idx, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7765 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7766 | rc = eval_function_call(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7767 | } |
| 7768 | LY_CHECK_RET(rc); |
| 7769 | |
| 7770 | parent_pos_pred = 1; |
| 7771 | goto predicate; |
| 7772 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 7773 | case LYXP_TOKEN_OPER_PATH: |
| 7774 | case LYXP_TOKEN_OPER_RPATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7775 | /* AbsoluteLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7776 | rc = eval_absolute_location_path(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7777 | LY_CHECK_RET(rc); |
| 7778 | break; |
| 7779 | |
| 7780 | case LYXP_TOKEN_LITERAL: |
| 7781 | /* Literal */ |
| 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 | eval_literal(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 | eval_literal(exp, tok_idx, set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7789 | } |
| 7790 | |
| 7791 | parent_pos_pred = 1; |
| 7792 | goto predicate; |
| 7793 | |
| 7794 | case LYXP_TOKEN_NUMBER: |
| 7795 | /* Number */ |
| 7796 | if (!set || (options & LYXP_SCNODE_ALL)) { |
| 7797 | if (set) { |
| 7798 | set_scnode_clear_ctx(set); |
| 7799 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7800 | rc = eval_number(NULL, exp, tok_idx, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7801 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7802 | rc = eval_number(set->ctx, exp, tok_idx, set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7803 | } |
| 7804 | LY_CHECK_RET(rc); |
| 7805 | |
| 7806 | parent_pos_pred = 1; |
| 7807 | goto predicate; |
| 7808 | |
| 7809 | default: |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 7810 | LOGVAL(set->ctx, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7811 | return LY_EVALID; |
| 7812 | } |
| 7813 | |
| 7814 | return LY_SUCCESS; |
| 7815 | |
| 7816 | predicate: |
| 7817 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7818 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 7819 | rc = eval_predicate(exp, tok_idx, set, options, parent_pos_pred); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7820 | LY_CHECK_RET(rc); |
| 7821 | } |
| 7822 | |
| 7823 | /* ('/' or '//') RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7824 | 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] | 7825 | |
| 7826 | /* evaluate '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7827 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7828 | all_desc = 0; |
| 7829 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7830 | all_desc = 1; |
| 7831 | } |
| 7832 | |
| 7833 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7834 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7835 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7836 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7837 | rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7838 | LY_CHECK_RET(rc); |
| 7839 | } |
| 7840 | |
| 7841 | return LY_SUCCESS; |
| 7842 | } |
| 7843 | |
| 7844 | /** |
| 7845 | * @brief Evaluate UnionExpr. Logs directly on error. |
| 7846 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7847 | * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7848 | * |
| 7849 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7850 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7851 | * @param[in] repeat How many times this expression is repeated. |
| 7852 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7853 | * @param[in] options XPath options. |
| 7854 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7855 | */ |
| 7856 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 7857 | 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] | 7858 | { |
| 7859 | LY_ERR rc = LY_SUCCESS; |
| 7860 | struct lyxp_set orig_set, set2; |
| 7861 | uint16_t i; |
| 7862 | |
| 7863 | assert(repeat); |
| 7864 | |
| 7865 | set_init(&orig_set, set); |
| 7866 | set_init(&set2, set); |
| 7867 | |
| 7868 | set_fill_set(&orig_set, set); |
| 7869 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7870 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7871 | LY_CHECK_GOTO(rc, cleanup); |
| 7872 | |
| 7873 | /* ('|' PathExpr)* */ |
| 7874 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7875 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7876 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7877 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7878 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7879 | |
| 7880 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7881 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7882 | LY_CHECK_GOTO(rc, cleanup); |
| 7883 | continue; |
| 7884 | } |
| 7885 | |
| 7886 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7887 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7888 | LY_CHECK_GOTO(rc, cleanup); |
| 7889 | |
| 7890 | /* eval */ |
| 7891 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7892 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7893 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7894 | rc = moveto_union(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7895 | LY_CHECK_GOTO(rc, cleanup); |
| 7896 | } |
| 7897 | } |
| 7898 | |
| 7899 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7900 | lyxp_set_free_content(&orig_set); |
| 7901 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7902 | return rc; |
| 7903 | } |
| 7904 | |
| 7905 | /** |
| 7906 | * @brief Evaluate UnaryExpr. Logs directly on error. |
| 7907 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7908 | * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7909 | * |
| 7910 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7911 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7912 | * @param[in] repeat How many times this expression is repeated. |
| 7913 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7914 | * @param[in] options XPath options. |
| 7915 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7916 | */ |
| 7917 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 7918 | 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] | 7919 | { |
| 7920 | LY_ERR rc; |
| 7921 | uint16_t this_op, i; |
| 7922 | |
| 7923 | assert(repeat); |
| 7924 | |
| 7925 | /* ('-')+ */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7926 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7927 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7928 | 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] | 7929 | |
| 7930 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7931 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7932 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7933 | } |
| 7934 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7935 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7936 | LY_CHECK_RET(rc); |
| 7937 | |
| 7938 | if (set && (repeat % 2)) { |
| 7939 | if (options & LYXP_SCNODE_ALL) { |
| 7940 | warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]); |
| 7941 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7942 | 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] | 7943 | LY_CHECK_RET(rc); |
| 7944 | } |
| 7945 | } |
| 7946 | |
| 7947 | return LY_SUCCESS; |
| 7948 | } |
| 7949 | |
| 7950 | /** |
| 7951 | * @brief Evaluate MultiplicativeExpr. Logs directly on error. |
| 7952 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7953 | * [18] MultiplicativeExpr ::= UnaryExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7954 | * | MultiplicativeExpr '*' UnaryExpr |
| 7955 | * | MultiplicativeExpr 'div' UnaryExpr |
| 7956 | * | MultiplicativeExpr 'mod' UnaryExpr |
| 7957 | * |
| 7958 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7959 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7960 | * @param[in] repeat How many times this expression is repeated. |
| 7961 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7962 | * @param[in] options XPath options. |
| 7963 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7964 | */ |
| 7965 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 7966 | eval_multiplicative_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, |
| 7967 | uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7968 | { |
| 7969 | LY_ERR rc; |
| 7970 | uint16_t this_op; |
| 7971 | struct lyxp_set orig_set, set2; |
| 7972 | uint16_t i; |
| 7973 | |
| 7974 | assert(repeat); |
| 7975 | |
| 7976 | set_init(&orig_set, set); |
| 7977 | set_init(&set2, set); |
| 7978 | |
| 7979 | set_fill_set(&orig_set, set); |
| 7980 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7981 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7982 | LY_CHECK_GOTO(rc, cleanup); |
| 7983 | |
| 7984 | /* ('*' / 'div' / 'mod' UnaryExpr)* */ |
| 7985 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7986 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7987 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7988 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7989 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 7990 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7991 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7992 | |
| 7993 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7994 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7995 | LY_CHECK_GOTO(rc, cleanup); |
| 7996 | continue; |
| 7997 | } |
| 7998 | |
| 7999 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8000 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8001 | LY_CHECK_GOTO(rc, cleanup); |
| 8002 | |
| 8003 | /* eval */ |
| 8004 | if (options & LYXP_SCNODE_ALL) { |
| 8005 | 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] | 8006 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8007 | set_scnode_clear_ctx(set); |
| 8008 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8009 | 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] | 8010 | LY_CHECK_GOTO(rc, cleanup); |
| 8011 | } |
| 8012 | } |
| 8013 | |
| 8014 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8015 | lyxp_set_free_content(&orig_set); |
| 8016 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8017 | return rc; |
| 8018 | } |
| 8019 | |
| 8020 | /** |
| 8021 | * @brief Evaluate AdditiveExpr. Logs directly on error. |
| 8022 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8023 | * [17] AdditiveExpr ::= MultiplicativeExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8024 | * | AdditiveExpr '+' MultiplicativeExpr |
| 8025 | * | AdditiveExpr '-' MultiplicativeExpr |
| 8026 | * |
| 8027 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8028 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8029 | * @param[in] repeat How many times this expression is repeated. |
| 8030 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8031 | * @param[in] options XPath options. |
| 8032 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8033 | */ |
| 8034 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 8035 | 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] | 8036 | { |
| 8037 | LY_ERR rc; |
| 8038 | uint16_t this_op; |
| 8039 | struct lyxp_set orig_set, set2; |
| 8040 | uint16_t i; |
| 8041 | |
| 8042 | assert(repeat); |
| 8043 | |
| 8044 | set_init(&orig_set, set); |
| 8045 | set_init(&set2, set); |
| 8046 | |
| 8047 | set_fill_set(&orig_set, set); |
| 8048 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8049 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8050 | LY_CHECK_GOTO(rc, cleanup); |
| 8051 | |
| 8052 | /* ('+' / '-' MultiplicativeExpr)* */ |
| 8053 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8054 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8055 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8056 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8057 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 8058 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8059 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8060 | |
| 8061 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8062 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8063 | LY_CHECK_GOTO(rc, cleanup); |
| 8064 | continue; |
| 8065 | } |
| 8066 | |
| 8067 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8068 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8069 | LY_CHECK_GOTO(rc, cleanup); |
| 8070 | |
| 8071 | /* eval */ |
| 8072 | if (options & LYXP_SCNODE_ALL) { |
| 8073 | 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] | 8074 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8075 | set_scnode_clear_ctx(set); |
| 8076 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8077 | 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] | 8078 | LY_CHECK_GOTO(rc, cleanup); |
| 8079 | } |
| 8080 | } |
| 8081 | |
| 8082 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8083 | lyxp_set_free_content(&orig_set); |
| 8084 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8085 | return rc; |
| 8086 | } |
| 8087 | |
| 8088 | /** |
| 8089 | * @brief Evaluate RelationalExpr. Logs directly on error. |
| 8090 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8091 | * [16] RelationalExpr ::= AdditiveExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8092 | * | RelationalExpr '<' AdditiveExpr |
| 8093 | * | RelationalExpr '>' AdditiveExpr |
| 8094 | * | RelationalExpr '<=' AdditiveExpr |
| 8095 | * | RelationalExpr '>=' AdditiveExpr |
| 8096 | * |
| 8097 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8098 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8099 | * @param[in] repeat How many times this expression is repeated. |
| 8100 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8101 | * @param[in] options XPath options. |
| 8102 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8103 | */ |
| 8104 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 8105 | 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] | 8106 | { |
| 8107 | LY_ERR rc; |
| 8108 | uint16_t this_op; |
| 8109 | struct lyxp_set orig_set, set2; |
| 8110 | uint16_t i; |
| 8111 | |
| 8112 | assert(repeat); |
| 8113 | |
| 8114 | set_init(&orig_set, set); |
| 8115 | set_init(&set2, set); |
| 8116 | |
| 8117 | set_fill_set(&orig_set, set); |
| 8118 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8119 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8120 | LY_CHECK_GOTO(rc, cleanup); |
| 8121 | |
| 8122 | /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */ |
| 8123 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8124 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8125 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8126 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8127 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 8128 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8129 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8130 | |
| 8131 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8132 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8133 | LY_CHECK_GOTO(rc, cleanup); |
| 8134 | continue; |
| 8135 | } |
| 8136 | |
| 8137 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8138 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8139 | LY_CHECK_GOTO(rc, cleanup); |
| 8140 | |
| 8141 | /* eval */ |
| 8142 | if (options & LYXP_SCNODE_ALL) { |
| 8143 | 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] | 8144 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8145 | set_scnode_clear_ctx(set); |
| 8146 | } else { |
| 8147 | rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options); |
| 8148 | LY_CHECK_GOTO(rc, cleanup); |
| 8149 | } |
| 8150 | } |
| 8151 | |
| 8152 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8153 | lyxp_set_free_content(&orig_set); |
| 8154 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8155 | return rc; |
| 8156 | } |
| 8157 | |
| 8158 | /** |
| 8159 | * @brief Evaluate EqualityExpr. Logs directly on error. |
| 8160 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8161 | * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8162 | * | EqualityExpr '!=' RelationalExpr |
| 8163 | * |
| 8164 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8165 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8166 | * @param[in] repeat How many times this expression is repeated. |
| 8167 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8168 | * @param[in] options XPath options. |
| 8169 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8170 | */ |
| 8171 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 8172 | 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] | 8173 | { |
| 8174 | LY_ERR rc; |
| 8175 | uint16_t this_op; |
| 8176 | struct lyxp_set orig_set, set2; |
| 8177 | uint16_t i; |
| 8178 | |
| 8179 | assert(repeat); |
| 8180 | |
| 8181 | set_init(&orig_set, set); |
| 8182 | set_init(&set2, set); |
| 8183 | |
| 8184 | set_fill_set(&orig_set, set); |
| 8185 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8186 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8187 | LY_CHECK_GOTO(rc, cleanup); |
| 8188 | |
| 8189 | /* ('=' / '!=' RelationalExpr)* */ |
| 8190 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8191 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8192 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8193 | 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] | 8194 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 8195 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8196 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8197 | |
| 8198 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8199 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8200 | LY_CHECK_GOTO(rc, cleanup); |
| 8201 | continue; |
| 8202 | } |
| 8203 | |
| 8204 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8205 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8206 | LY_CHECK_GOTO(rc, cleanup); |
| 8207 | |
| 8208 | /* eval */ |
| 8209 | if (options & LYXP_SCNODE_ALL) { |
| 8210 | 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] | 8211 | warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1); |
| 8212 | 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] | 8213 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8214 | set_scnode_clear_ctx(set); |
| 8215 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8216 | rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options); |
| 8217 | LY_CHECK_GOTO(rc, cleanup); |
| 8218 | } |
| 8219 | } |
| 8220 | |
| 8221 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8222 | lyxp_set_free_content(&orig_set); |
| 8223 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8224 | return rc; |
| 8225 | } |
| 8226 | |
| 8227 | /** |
| 8228 | * @brief Evaluate AndExpr. Logs directly on error. |
| 8229 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8230 | * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8231 | * |
| 8232 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8233 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8234 | * @param[in] repeat How many times this expression is repeated. |
| 8235 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8236 | * @param[in] options XPath options. |
| 8237 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8238 | */ |
| 8239 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 8240 | 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] | 8241 | { |
| 8242 | LY_ERR rc; |
| 8243 | struct lyxp_set orig_set, set2; |
| 8244 | uint16_t i; |
| 8245 | |
| 8246 | assert(repeat); |
| 8247 | |
| 8248 | set_init(&orig_set, set); |
| 8249 | set_init(&set2, set); |
| 8250 | |
| 8251 | set_fill_set(&orig_set, set); |
| 8252 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8253 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8254 | LY_CHECK_GOTO(rc, cleanup); |
| 8255 | |
| 8256 | /* cast to boolean, we know that will be the final result */ |
| 8257 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 8258 | set_scnode_clear_ctx(set); |
| 8259 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8260 | lyxp_set_cast(set, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8261 | } |
| 8262 | |
| 8263 | /* ('and' EqualityExpr)* */ |
| 8264 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8265 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG); |
| 8266 | 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] | 8267 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8268 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8269 | |
| 8270 | /* lazy evaluation */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8271 | if (!set || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) { |
| 8272 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8273 | LY_CHECK_GOTO(rc, cleanup); |
| 8274 | continue; |
| 8275 | } |
| 8276 | |
| 8277 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8278 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8279 | LY_CHECK_GOTO(rc, cleanup); |
| 8280 | |
| 8281 | /* eval - just get boolean value actually */ |
| 8282 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 8283 | set_scnode_clear_ctx(&set2); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 8284 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8285 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8286 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8287 | set_fill_set(set, &set2); |
| 8288 | } |
| 8289 | } |
| 8290 | |
| 8291 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8292 | lyxp_set_free_content(&orig_set); |
| 8293 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8294 | return rc; |
| 8295 | } |
| 8296 | |
| 8297 | /** |
| 8298 | * @brief Evaluate OrExpr. Logs directly on error. |
| 8299 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8300 | * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8301 | * |
| 8302 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8303 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8304 | * @param[in] repeat How many times this expression is repeated. |
| 8305 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8306 | * @param[in] options XPath options. |
| 8307 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8308 | */ |
| 8309 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 8310 | 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] | 8311 | { |
| 8312 | LY_ERR rc; |
| 8313 | struct lyxp_set orig_set, set2; |
| 8314 | uint16_t i; |
| 8315 | |
| 8316 | assert(repeat); |
| 8317 | |
| 8318 | set_init(&orig_set, set); |
| 8319 | set_init(&set2, set); |
| 8320 | |
| 8321 | set_fill_set(&orig_set, set); |
| 8322 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8323 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8324 | LY_CHECK_GOTO(rc, cleanup); |
| 8325 | |
| 8326 | /* cast to boolean, we know that will be the final result */ |
| 8327 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 8328 | set_scnode_clear_ctx(set); |
| 8329 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8330 | lyxp_set_cast(set, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8331 | } |
| 8332 | |
| 8333 | /* ('or' AndExpr)* */ |
| 8334 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8335 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG); |
| 8336 | 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] | 8337 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8338 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8339 | |
| 8340 | /* lazy evaluation */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8341 | if (!set || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) { |
| 8342 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8343 | LY_CHECK_GOTO(rc, cleanup); |
| 8344 | continue; |
| 8345 | } |
| 8346 | |
| 8347 | set_fill_set(&set2, &orig_set); |
| 8348 | /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one), |
| 8349 | * but it does not matter */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8350 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8351 | LY_CHECK_GOTO(rc, cleanup); |
| 8352 | |
| 8353 | /* eval - just get boolean value actually */ |
| 8354 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 8355 | set_scnode_clear_ctx(&set2); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 8356 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8357 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8358 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8359 | set_fill_set(set, &set2); |
| 8360 | } |
| 8361 | } |
| 8362 | |
| 8363 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8364 | lyxp_set_free_content(&orig_set); |
| 8365 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8366 | return rc; |
| 8367 | } |
| 8368 | |
| 8369 | /** |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8370 | * @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] | 8371 | * |
| 8372 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8373 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8374 | * @param[in] etype Expression type to evaluate. |
| 8375 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8376 | * @param[in] options XPath options. |
| 8377 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8378 | */ |
| 8379 | static LY_ERR |
Michal Vasko | 40308e7 | 2020-10-20 16:38:40 +0200 | [diff] [blame] | 8380 | eval_expr_select(const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype, struct lyxp_set *set, |
| 8381 | uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8382 | { |
| 8383 | uint16_t i, count; |
| 8384 | enum lyxp_expr_type next_etype; |
| 8385 | LY_ERR rc; |
| 8386 | |
| 8387 | /* process operator repeats */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8388 | if (!exp->repeat[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8389 | next_etype = LYXP_EXPR_NONE; |
| 8390 | } else { |
| 8391 | /* find etype repeat */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 8392 | for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i) {} |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8393 | |
| 8394 | /* select one-priority lower because etype expression called us */ |
| 8395 | if (i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8396 | next_etype = exp->repeat[*tok_idx][i - 1]; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8397 | /* count repeats for that expression */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 8398 | 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] | 8399 | } else { |
| 8400 | next_etype = LYXP_EXPR_NONE; |
| 8401 | } |
| 8402 | } |
| 8403 | |
| 8404 | /* decide what expression are we parsing based on the repeat */ |
| 8405 | switch (next_etype) { |
| 8406 | case LYXP_EXPR_OR: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8407 | rc = eval_or_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8408 | break; |
| 8409 | case LYXP_EXPR_AND: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8410 | rc = eval_and_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8411 | break; |
| 8412 | case LYXP_EXPR_EQUALITY: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8413 | rc = eval_equality_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8414 | break; |
| 8415 | case LYXP_EXPR_RELATIONAL: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8416 | rc = eval_relational_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8417 | break; |
| 8418 | case LYXP_EXPR_ADDITIVE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8419 | rc = eval_additive_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8420 | break; |
| 8421 | case LYXP_EXPR_MULTIPLICATIVE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8422 | rc = eval_multiplicative_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8423 | break; |
| 8424 | case LYXP_EXPR_UNARY: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8425 | rc = eval_unary_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8426 | break; |
| 8427 | case LYXP_EXPR_UNION: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8428 | rc = eval_union_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8429 | break; |
| 8430 | case LYXP_EXPR_NONE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8431 | rc = eval_path_expr(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8432 | break; |
| 8433 | default: |
| 8434 | LOGINT_RET(set->ctx); |
| 8435 | } |
| 8436 | |
| 8437 | return rc; |
| 8438 | } |
| 8439 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8440 | /** |
| 8441 | * @brief Get root type. |
| 8442 | * |
| 8443 | * @param[in] ctx_node Context node. |
| 8444 | * @param[in] ctx_scnode Schema context node. |
| 8445 | * @param[in] options XPath options. |
| 8446 | * @return Root type. |
| 8447 | */ |
| 8448 | static enum lyxp_node_type |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 8449 | 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] | 8450 | { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 8451 | const struct lysc_node *op; |
| 8452 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8453 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8454 | /* schema */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 8455 | 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] | 8456 | |
| 8457 | if (op || (options & LYXP_SCNODE)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8458 | /* general root that can access everything */ |
| 8459 | return LYXP_NODE_ROOT; |
| 8460 | } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) { |
| 8461 | /* root context node can access only config data (because we said so, it is unspecified) */ |
| 8462 | return LYXP_NODE_ROOT_CONFIG; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8463 | } |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 8464 | return LYXP_NODE_ROOT; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8465 | } |
| 8466 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8467 | /* data */ |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 8468 | op = ctx_node ? ctx_node->schema : NULL; |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 8469 | 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] | 8470 | |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8471 | if (op || !(options & LYXP_SCHEMA)) { |
| 8472 | /* general root that can access everything */ |
| 8473 | return LYXP_NODE_ROOT; |
Christian Hopps | b6ecaea | 2021-02-06 09:45:38 -0500 | [diff] [blame] | 8474 | } else if (!ctx_node || !ctx_node->schema || (ctx_node->schema->flags & LYS_CONFIG_W)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8475 | /* root context node can access only config data (because we said so, it is unspecified) */ |
| 8476 | return LYXP_NODE_ROOT_CONFIG; |
| 8477 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8478 | return LYXP_NODE_ROOT; |
| 8479 | } |
| 8480 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8481 | LY_ERR |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 8482 | lyxp_eval(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod, |
| 8483 | LY_PREFIX_FORMAT format, void *prefix_data, const struct lyd_node *ctx_node, const struct lyd_node *tree, |
| 8484 | struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8485 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8486 | uint16_t tok_idx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8487 | LY_ERR rc; |
| 8488 | |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 8489 | LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL); |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8490 | if (!cur_mod && ((format == LY_PREF_SCHEMA) || (format == LY_PREF_SCHEMA_RESOLVED))) { |
| 8491 | LOGARG(NULL, "Current module must be set if schema format is used."); |
| 8492 | return LY_EINVAL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8493 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8494 | |
Michal Vasko | d3bb12f | 2020-12-04 14:33:09 +0100 | [diff] [blame] | 8495 | if (tree) { |
| 8496 | /* adjust the pointer to be the first top-level sibling */ |
| 8497 | while (tree->parent) { |
| 8498 | tree = lyd_parent(tree); |
| 8499 | } |
| 8500 | tree = lyd_first_sibling(tree); |
| 8501 | } |
| 8502 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8503 | /* prepare set for evaluation */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8504 | memset(set, 0, sizeof *set); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8505 | set->type = LYXP_SET_NODE_SET; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8506 | set->root_type = lyxp_get_root_type(ctx_node, NULL, options); |
| 8507 | set_insert_node(set, (struct lyd_node *)ctx_node, 0, ctx_node ? LYXP_NODE_ELEM : set->root_type, 0); |
| 8508 | |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 8509 | set->ctx = (struct ly_ctx *)ctx; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8510 | set->cur_node = ctx_node; |
| 8511 | for (set->context_op = ctx_node ? ctx_node->schema : NULL; |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 8512 | set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); |
| 8513 | set->context_op = set->context_op->parent) {} |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 8514 | set->tree = tree; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8515 | set->cur_mod = cur_mod; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8516 | set->format = format; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8517 | set->prefix_data = prefix_data; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8518 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 8519 | LOG_LOCSET(NULL, set->cur_node, NULL, NULL); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 8520 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8521 | /* evaluate */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8522 | rc = eval_expr_select(exp, &tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8523 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8524 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8525 | } |
| 8526 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 8527 | LOG_LOCBACK(0, 1, 0, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8528 | return rc; |
| 8529 | } |
| 8530 | |
| 8531 | #if 0 |
| 8532 | |
| 8533 | /* full xml printing of set elements, not used currently */ |
| 8534 | |
| 8535 | void |
| 8536 | lyxp_set_print_xml(FILE *f, struct lyxp_set *set) |
| 8537 | { |
| 8538 | uint32_t i; |
| 8539 | char *str_num; |
| 8540 | struct lyout out; |
| 8541 | |
| 8542 | memset(&out, 0, sizeof out); |
| 8543 | |
| 8544 | out.type = LYOUT_STREAM; |
| 8545 | out.method.f = f; |
| 8546 | |
| 8547 | switch (set->type) { |
| 8548 | case LYXP_SET_EMPTY: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8549 | ly_print_(&out, "Empty XPath set\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8550 | break; |
| 8551 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8552 | ly_print_(&out, "Boolean XPath set:\n"); |
| 8553 | ly_print_(&out, "%s\n\n", set->value.bool ? "true" : "false"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8554 | break; |
| 8555 | case LYXP_SET_STRING: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8556 | ly_print_(&out, "String XPath set:\n"); |
| 8557 | ly_print_(&out, "\"%s\"\n\n", set->value.str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8558 | break; |
| 8559 | case LYXP_SET_NUMBER: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8560 | ly_print_(&out, "Number XPath set:\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8561 | |
| 8562 | if (isnan(set->value.num)) { |
| 8563 | str_num = strdup("NaN"); |
| 8564 | } else if ((set->value.num == 0) || (set->value.num == -0.0f)) { |
| 8565 | str_num = strdup("0"); |
| 8566 | } else if (isinf(set->value.num) && !signbit(set->value.num)) { |
| 8567 | str_num = strdup("Infinity"); |
| 8568 | } else if (isinf(set->value.num) && signbit(set->value.num)) { |
| 8569 | str_num = strdup("-Infinity"); |
| 8570 | } else if ((long long)set->value.num == set->value.num) { |
| 8571 | if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) { |
| 8572 | str_num = NULL; |
| 8573 | } |
| 8574 | } else { |
| 8575 | if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) { |
| 8576 | str_num = NULL; |
| 8577 | } |
| 8578 | } |
| 8579 | if (!str_num) { |
| 8580 | LOGMEM; |
| 8581 | return; |
| 8582 | } |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8583 | ly_print_(&out, "%s\n\n", str_num); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8584 | free(str_num); |
| 8585 | break; |
| 8586 | case LYXP_SET_NODE_SET: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8587 | ly_print_(&out, "Node XPath set:\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8588 | |
| 8589 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8590 | ly_print_(&out, "%d. ", i + 1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8591 | switch (set->node_type[i]) { |
| 8592 | case LYXP_NODE_ROOT_ALL: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8593 | ly_print_(&out, "ROOT all\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8594 | break; |
| 8595 | case LYXP_NODE_ROOT_CONFIG: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8596 | ly_print_(&out, "ROOT config\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8597 | break; |
| 8598 | case LYXP_NODE_ROOT_STATE: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8599 | ly_print_(&out, "ROOT state\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8600 | break; |
| 8601 | case LYXP_NODE_ROOT_NOTIF: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8602 | 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] | 8603 | break; |
| 8604 | case LYXP_NODE_ROOT_RPC: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8605 | 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] | 8606 | break; |
| 8607 | case LYXP_NODE_ROOT_OUTPUT: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8608 | 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] | 8609 | break; |
| 8610 | case LYXP_NODE_ELEM: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8611 | ly_print_(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8612 | xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT); |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8613 | ly_print_(&out, "\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8614 | break; |
| 8615 | case LYXP_NODE_TEXT: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8616 | 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] | 8617 | break; |
| 8618 | case LYXP_NODE_ATTR: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8619 | 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] | 8620 | break; |
| 8621 | } |
| 8622 | } |
| 8623 | break; |
| 8624 | } |
| 8625 | } |
| 8626 | |
| 8627 | #endif |
| 8628 | |
| 8629 | LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8630 | lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8631 | { |
| 8632 | long double num; |
| 8633 | char *str; |
| 8634 | LY_ERR rc; |
| 8635 | |
| 8636 | if (!set || (set->type == target)) { |
| 8637 | return LY_SUCCESS; |
| 8638 | } |
| 8639 | |
| 8640 | /* it's not possible to convert anything into a node set */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8641 | assert(target != LYXP_SET_NODE_SET); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8642 | |
| 8643 | if (set->type == LYXP_SET_SCNODE_SET) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8644 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8645 | return LY_EINVAL; |
| 8646 | } |
| 8647 | |
| 8648 | /* to STRING */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8649 | 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] | 8650 | switch (set->type) { |
| 8651 | case LYXP_SET_NUMBER: |
| 8652 | if (isnan(set->val.num)) { |
| 8653 | set->val.str = strdup("NaN"); |
| 8654 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 8655 | } else if ((set->val.num == 0) || (set->val.num == -0.0f)) { |
| 8656 | set->val.str = strdup("0"); |
| 8657 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 8658 | } else if (isinf(set->val.num) && !signbit(set->val.num)) { |
| 8659 | set->val.str = strdup("Infinity"); |
| 8660 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 8661 | } else if (isinf(set->val.num) && signbit(set->val.num)) { |
| 8662 | set->val.str = strdup("-Infinity"); |
| 8663 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 8664 | } else if ((long long)set->val.num == set->val.num) { |
| 8665 | if (asprintf(&str, "%lld", (long long)set->val.num) == -1) { |
| 8666 | LOGMEM_RET(set->ctx); |
| 8667 | } |
| 8668 | set->val.str = str; |
| 8669 | } else { |
| 8670 | if (asprintf(&str, "%03.1Lf", set->val.num) == -1) { |
| 8671 | LOGMEM_RET(set->ctx); |
| 8672 | } |
| 8673 | set->val.str = str; |
| 8674 | } |
| 8675 | break; |
| 8676 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8677 | if (set->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8678 | set->val.str = strdup("true"); |
| 8679 | } else { |
| 8680 | set->val.str = strdup("false"); |
| 8681 | } |
| 8682 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM); |
| 8683 | break; |
| 8684 | case LYXP_SET_NODE_SET: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8685 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8686 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8687 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8688 | rc = cast_node_set_to_string(set, &str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8689 | LY_CHECK_RET(rc); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8690 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8691 | set->val.str = str; |
| 8692 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8693 | default: |
| 8694 | LOGINT_RET(set->ctx); |
| 8695 | } |
| 8696 | set->type = LYXP_SET_STRING; |
| 8697 | } |
| 8698 | |
| 8699 | /* to NUMBER */ |
| 8700 | if (target == LYXP_SET_NUMBER) { |
| 8701 | switch (set->type) { |
| 8702 | case LYXP_SET_STRING: |
| 8703 | num = cast_string_to_number(set->val.str); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8704 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8705 | set->val.num = num; |
| 8706 | break; |
| 8707 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8708 | if (set->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8709 | set->val.num = 1; |
| 8710 | } else { |
| 8711 | set->val.num = 0; |
| 8712 | } |
| 8713 | break; |
| 8714 | default: |
| 8715 | LOGINT_RET(set->ctx); |
| 8716 | } |
| 8717 | set->type = LYXP_SET_NUMBER; |
| 8718 | } |
| 8719 | |
| 8720 | /* to BOOLEAN */ |
| 8721 | if (target == LYXP_SET_BOOLEAN) { |
| 8722 | switch (set->type) { |
| 8723 | case LYXP_SET_NUMBER: |
| 8724 | 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] | 8725 | set->val.bln = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8726 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8727 | set->val.bln = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8728 | } |
| 8729 | break; |
| 8730 | case LYXP_SET_STRING: |
| 8731 | if (set->val.str[0]) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8732 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8733 | set->val.bln = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8734 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8735 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8736 | set->val.bln = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8737 | } |
| 8738 | break; |
| 8739 | case LYXP_SET_NODE_SET: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8740 | if (set->used) { |
| 8741 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8742 | set->val.bln = 1; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8743 | } else { |
| 8744 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8745 | set->val.bln = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8746 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8747 | break; |
| 8748 | default: |
| 8749 | LOGINT_RET(set->ctx); |
| 8750 | } |
| 8751 | set->type = LYXP_SET_BOOLEAN; |
| 8752 | } |
| 8753 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8754 | return LY_SUCCESS; |
| 8755 | } |
| 8756 | |
| 8757 | LY_ERR |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 8758 | lyxp_atomize(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod, |
| 8759 | LY_PREFIX_FORMAT format, void *prefix_data, const struct lysc_node *ctx_scnode, struct lyxp_set *set, |
| 8760 | uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8761 | { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 8762 | LY_ERR ret; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8763 | uint16_t tok_idx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8764 | |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 8765 | LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL); |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8766 | if (!cur_mod && ((format == LY_PREF_SCHEMA) || (format == LY_PREF_SCHEMA_RESOLVED))) { |
| 8767 | LOGARG(NULL, "Current module must be set if schema format is used."); |
| 8768 | return LY_EINVAL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8769 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8770 | |
| 8771 | /* prepare set for evaluation */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8772 | memset(set, 0, sizeof *set); |
| 8773 | set->type = LYXP_SET_SCNODE_SET; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8774 | set->root_type = lyxp_get_root_type(NULL, ctx_scnode, options); |
| 8775 | 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] | 8776 | set->val.scnodes[0].in_ctx = LYXP_SET_SCNODE_START; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8777 | |
Michal Vasko | 400e967 | 2021-01-11 13:39:17 +0100 | [diff] [blame] | 8778 | set->ctx = (struct ly_ctx *)ctx; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8779 | set->cur_scnode = ctx_scnode; |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 8780 | for (set->context_op = ctx_scnode; |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 8781 | set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); |
| 8782 | set->context_op = set->context_op->parent) {} |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8783 | set->cur_mod = cur_mod; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8784 | set->format = format; |
Michal Vasko | 5d24f6c | 2020-10-13 13:49:06 +0200 | [diff] [blame] | 8785 | set->prefix_data = prefix_data; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8786 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 8787 | LOG_LOCSET(set->cur_scnode, NULL, NULL, NULL); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 8788 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8789 | /* evaluate */ |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 8790 | ret = eval_expr_select(exp, &tok_idx, 0, set, options); |
| 8791 | |
Radek Krejci | ddace2c | 2021-01-08 11:30:56 +0100 | [diff] [blame] | 8792 | LOG_LOCBACK(1, 0, 0, 0); |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 8793 | return ret; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8794 | } |
Michal Vasko | d43d71a | 2020-08-07 14:54:58 +0200 | [diff] [blame] | 8795 | |
| 8796 | API const char * |
| 8797 | lyxp_get_expr(const struct lyxp_expr *path) |
| 8798 | { |
| 8799 | if (!path) { |
| 8800 | return NULL; |
| 8801 | } |
| 8802 | |
| 8803 | return path->expr; |
| 8804 | } |