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 |
| 15 | |
| 16 | /* needed by libmath functions isfinite(), isinf(), isnan(), signbit(), ... */ |
| 17 | #define _ISOC99_SOURCE |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 18 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 19 | #include "xpath.h" |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 20 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 21 | #include <assert.h> |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 22 | #include <ctype.h> |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 23 | #include <errno.h> |
| 24 | #include <limits.h> |
| 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 | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 36 | #include "parser_data.h" |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 37 | #include "path.h" |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 38 | #include "plugins_types.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 39 | #include "printer.h" |
| 40 | #include "printer_data.h" |
| 41 | #include "tree.h" |
| 42 | #include "tree_data_internal.h" |
| 43 | #include "tree_schema_internal.h" |
| 44 | #include "xml.h" |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 45 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 46 | static LY_ERR reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx); |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 47 | static LY_ERR eval_expr_select(struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype, struct lyxp_set *set, uint32_t options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 48 | |
| 49 | /** |
| 50 | * @brief Print the type of an XPath \p set. |
| 51 | * |
| 52 | * @param[in] set Set to use. |
| 53 | * @return Set type string. |
| 54 | */ |
| 55 | static const char * |
| 56 | print_set_type(struct lyxp_set *set) |
| 57 | { |
| 58 | switch (set->type) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 59 | case LYXP_SET_NODE_SET: |
| 60 | return "node set"; |
| 61 | case LYXP_SET_SCNODE_SET: |
| 62 | return "schema node set"; |
| 63 | case LYXP_SET_BOOLEAN: |
| 64 | return "boolean"; |
| 65 | case LYXP_SET_NUMBER: |
| 66 | return "number"; |
| 67 | case LYXP_SET_STRING: |
| 68 | return "string"; |
| 69 | } |
| 70 | |
| 71 | return NULL; |
| 72 | } |
| 73 | |
Michal Vasko | 24cddf8 | 2020-06-01 08:17:01 +0200 | [diff] [blame] | 74 | const char * |
| 75 | lyxp_print_token(enum lyxp_token tok) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 76 | { |
| 77 | switch (tok) { |
| 78 | case LYXP_TOKEN_PAR1: |
| 79 | return "("; |
| 80 | case LYXP_TOKEN_PAR2: |
| 81 | return ")"; |
| 82 | case LYXP_TOKEN_BRACK1: |
| 83 | return "["; |
| 84 | case LYXP_TOKEN_BRACK2: |
| 85 | return "]"; |
| 86 | case LYXP_TOKEN_DOT: |
| 87 | return "."; |
| 88 | case LYXP_TOKEN_DDOT: |
| 89 | return ".."; |
| 90 | case LYXP_TOKEN_AT: |
| 91 | return "@"; |
| 92 | case LYXP_TOKEN_COMMA: |
| 93 | return ","; |
| 94 | case LYXP_TOKEN_NAMETEST: |
| 95 | return "NameTest"; |
| 96 | case LYXP_TOKEN_NODETYPE: |
| 97 | return "NodeType"; |
| 98 | case LYXP_TOKEN_FUNCNAME: |
| 99 | return "FunctionName"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 100 | case LYXP_TOKEN_OPER_LOG: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 101 | return "Operator(Logic)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 102 | case LYXP_TOKEN_OPER_EQUAL: |
| 103 | return "Operator(Equal)"; |
| 104 | case LYXP_TOKEN_OPER_NEQUAL: |
| 105 | return "Operator(Non-equal)"; |
| 106 | case LYXP_TOKEN_OPER_COMP: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 107 | return "Operator(Comparison)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 108 | case LYXP_TOKEN_OPER_MATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 109 | return "Operator(Math)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 110 | case LYXP_TOKEN_OPER_UNI: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 111 | return "Operator(Union)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 112 | case LYXP_TOKEN_OPER_PATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 113 | return "Operator(Path)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 114 | case LYXP_TOKEN_OPER_RPATH: |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 115 | return "Operator(Recursive Path)"; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 116 | case LYXP_TOKEN_LITERAL: |
| 117 | return "Literal"; |
| 118 | case LYXP_TOKEN_NUMBER: |
| 119 | return "Number"; |
| 120 | default: |
| 121 | LOGINT(NULL); |
| 122 | return ""; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * @brief Print the whole expression \p exp to debug output. |
| 128 | * |
| 129 | * @param[in] exp Expression to use. |
| 130 | */ |
| 131 | static void |
| 132 | print_expr_struct_debug(struct lyxp_expr *exp) |
| 133 | { |
| 134 | uint16_t i, j; |
| 135 | char tmp[128]; |
| 136 | |
| 137 | if (!exp || (ly_log_level < LY_LLDBG)) { |
| 138 | return; |
| 139 | } |
| 140 | |
| 141 | LOGDBG(LY_LDGXPATH, "expression \"%s\":", exp->expr); |
| 142 | for (i = 0; i < exp->used; ++i) { |
Michal Vasko | 24cddf8 | 2020-06-01 08:17:01 +0200 | [diff] [blame] | 143 | sprintf(tmp, "\ttoken %s, in expression \"%.*s\"", lyxp_print_token(exp->tokens[i]), exp->tok_len[i], |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 144 | &exp->expr[exp->tok_pos[i]]); |
| 145 | if (exp->repeat[i]) { |
| 146 | sprintf(tmp + strlen(tmp), " (repeat %d", exp->repeat[i][0]); |
| 147 | for (j = 1; exp->repeat[i][j]; ++j) { |
| 148 | sprintf(tmp + strlen(tmp), ", %d", exp->repeat[i][j]); |
| 149 | } |
| 150 | strcat(tmp, ")"); |
| 151 | } |
| 152 | LOGDBG(LY_LDGXPATH, tmp); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | #ifndef NDEBUG |
| 157 | |
| 158 | /** |
| 159 | * @brief Print XPath set content to debug output. |
| 160 | * |
| 161 | * @param[in] set Set to print. |
| 162 | */ |
| 163 | static void |
| 164 | print_set_debug(struct lyxp_set *set) |
| 165 | { |
| 166 | uint32_t i; |
| 167 | char *str; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 168 | struct lyxp_set_node *item; |
| 169 | struct lyxp_set_scnode *sitem; |
| 170 | |
| 171 | if (ly_log_level < LY_LLDBG) { |
| 172 | return; |
| 173 | } |
| 174 | |
| 175 | switch (set->type) { |
| 176 | case LYXP_SET_NODE_SET: |
| 177 | LOGDBG(LY_LDGXPATH, "set NODE SET:"); |
| 178 | for (i = 0; i < set->used; ++i) { |
| 179 | item = &set->val.nodes[i]; |
| 180 | |
| 181 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 182 | case LYXP_NODE_NONE: |
| 183 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): NONE", i + 1, item->pos); |
| 184 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 185 | case LYXP_NODE_ROOT: |
| 186 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT", i + 1, item->pos); |
| 187 | break; |
| 188 | case LYXP_NODE_ROOT_CONFIG: |
| 189 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT CONFIG", i + 1, item->pos); |
| 190 | break; |
| 191 | case LYXP_NODE_ELEM: |
| 192 | if ((item->node->schema->nodetype == LYS_LIST) |
| 193 | && (((struct lyd_node_inner *)item->node)->child->schema->nodetype == LYS_LEAF)) { |
| 194 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (1st child val: %s)", i + 1, item->pos, |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 195 | item->node->schema->name, LYD_CANON_VALUE(lyd_node_children(item->node, 0))); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 196 | } else if (((struct lyd_node_inner *)item->node)->schema->nodetype == LYS_LEAFLIST) { |
| 197 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (val: %s)", i + 1, item->pos, |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 198 | item->node->schema->name, LYD_CANON_VALUE(item->node)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 199 | } else { |
| 200 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s", i + 1, item->pos, item->node->schema->name); |
| 201 | } |
| 202 | break; |
| 203 | case LYXP_NODE_TEXT: |
| 204 | if (item->node->schema->nodetype & LYS_ANYDATA) { |
| 205 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT <%s>", i + 1, item->pos, |
| 206 | item->node->schema->nodetype == LYS_ANYXML ? "anyxml" : "anydata"); |
| 207 | } else { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 208 | 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] | 209 | } |
| 210 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 211 | case LYXP_NODE_META: |
| 212 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): META %s = %s", i + 1, item->pos, set->val.meta[i].meta->name, |
| 213 | set->val.meta[i].meta->value); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 214 | break; |
| 215 | } |
| 216 | } |
| 217 | break; |
| 218 | |
| 219 | case LYXP_SET_SCNODE_SET: |
| 220 | LOGDBG(LY_LDGXPATH, "set SCNODE SET:"); |
| 221 | for (i = 0; i < set->used; ++i) { |
| 222 | sitem = &set->val.scnodes[i]; |
| 223 | |
| 224 | switch (sitem->type) { |
| 225 | case LYXP_NODE_ROOT: |
| 226 | LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT", i + 1, sitem->in_ctx); |
| 227 | break; |
| 228 | case LYXP_NODE_ROOT_CONFIG: |
| 229 | LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT CONFIG", i + 1, sitem->in_ctx); |
| 230 | break; |
| 231 | case LYXP_NODE_ELEM: |
| 232 | LOGDBG(LY_LDGXPATH, "\t%d (%u): ELEM %s", i + 1, sitem->in_ctx, sitem->scnode->name); |
| 233 | break; |
| 234 | default: |
| 235 | LOGINT(NULL); |
| 236 | break; |
| 237 | } |
| 238 | } |
| 239 | break; |
| 240 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 241 | case LYXP_SET_BOOLEAN: |
| 242 | LOGDBG(LY_LDGXPATH, "set BOOLEAN"); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 243 | LOGDBG(LY_LDGXPATH, "\t%s", (set->val.bln ? "true" : "false")); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 244 | break; |
| 245 | |
| 246 | case LYXP_SET_STRING: |
| 247 | LOGDBG(LY_LDGXPATH, "set STRING"); |
| 248 | LOGDBG(LY_LDGXPATH, "\t%s", set->val.str); |
| 249 | break; |
| 250 | |
| 251 | case LYXP_SET_NUMBER: |
| 252 | LOGDBG(LY_LDGXPATH, "set NUMBER"); |
| 253 | |
| 254 | if (isnan(set->val.num)) { |
| 255 | str = strdup("NaN"); |
| 256 | } else if ((set->val.num == 0) || (set->val.num == -0.0f)) { |
| 257 | str = strdup("0"); |
| 258 | } else if (isinf(set->val.num) && !signbit(set->val.num)) { |
| 259 | str = strdup("Infinity"); |
| 260 | } else if (isinf(set->val.num) && signbit(set->val.num)) { |
| 261 | str = strdup("-Infinity"); |
| 262 | } else if ((long long)set->val.num == set->val.num) { |
| 263 | if (asprintf(&str, "%lld", (long long)set->val.num) == -1) { |
| 264 | str = NULL; |
| 265 | } |
| 266 | } else { |
| 267 | if (asprintf(&str, "%03.1Lf", set->val.num) == -1) { |
| 268 | str = NULL; |
| 269 | } |
| 270 | } |
| 271 | LY_CHECK_ERR_RET(!str, LOGMEM(NULL), ); |
| 272 | |
| 273 | LOGDBG(LY_LDGXPATH, "\t%s", str); |
| 274 | free(str); |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | #endif |
| 279 | |
| 280 | /** |
| 281 | * @brief Realloc the string \p str. |
| 282 | * |
| 283 | * @param[in] ctx libyang context for logging. |
| 284 | * @param[in] needed How much free space is required. |
| 285 | * @param[in,out] str Pointer to the string to use. |
| 286 | * @param[in,out] used Used bytes in \p str. |
| 287 | * @param[in,out] size Allocated bytes in \p str. |
| 288 | * @return LY_ERR |
| 289 | */ |
| 290 | static LY_ERR |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 291 | 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] | 292 | { |
| 293 | if (*size - *used < needed) { |
| 294 | do { |
| 295 | if ((UINT16_MAX - *size) < LYXP_STRING_CAST_SIZE_STEP) { |
| 296 | LOGERR(ctx, LY_EINVAL, "XPath string length limit (%u) reached.", UINT16_MAX); |
| 297 | return LY_EINVAL; |
| 298 | } |
| 299 | *size += LYXP_STRING_CAST_SIZE_STEP; |
| 300 | } while (*size - *used < needed); |
| 301 | *str = ly_realloc(*str, *size * sizeof(char)); |
| 302 | LY_CHECK_ERR_RET(!(*str), LOGMEM(ctx), LY_EMEM); |
| 303 | } |
| 304 | |
| 305 | return LY_SUCCESS; |
| 306 | } |
| 307 | |
| 308 | /** |
| 309 | * @brief Cast nodes recursively to one string @p str. |
| 310 | * |
| 311 | * @param[in] node Node to cast. |
| 312 | * @param[in] fake_cont Whether to put the data into a "fake" container. |
| 313 | * @param[in] root_type Type of the XPath root. |
| 314 | * @param[in] indent Current indent. |
| 315 | * @param[in,out] str Resulting string. |
| 316 | * @param[in,out] used Used bytes in @p str. |
| 317 | * @param[in,out] size Allocated bytes in @p str. |
| 318 | * @return LY_ERR |
| 319 | */ |
| 320 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 321 | cast_string_recursive(const struct lyd_node *node, uint8_t 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] | 322 | uint16_t *used, uint16_t *size) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 323 | { |
Radek Krejci | 7f769d7 | 2020-07-11 23:13:56 +0200 | [diff] [blame] | 324 | char *buf, *line, *ptr = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 325 | const char *value_str; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 326 | const struct lyd_node *child; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 327 | struct lyd_node *tree; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 328 | struct lyd_node_any *any; |
| 329 | LY_ERR rc; |
| 330 | |
| 331 | if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) { |
| 332 | return LY_SUCCESS; |
| 333 | } |
| 334 | |
| 335 | if (fake_cont) { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 336 | rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 337 | LY_CHECK_RET(rc); |
| 338 | strcpy(*str + (*used - 1), "\n"); |
| 339 | ++(*used); |
| 340 | |
| 341 | ++indent; |
| 342 | } |
| 343 | |
| 344 | switch (node->schema->nodetype) { |
| 345 | case LYS_CONTAINER: |
| 346 | case LYS_LIST: |
| 347 | case LYS_RPC: |
| 348 | case LYS_NOTIF: |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 349 | rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 350 | LY_CHECK_RET(rc); |
| 351 | strcpy(*str + (*used - 1), "\n"); |
| 352 | ++(*used); |
| 353 | |
Michal Vasko | 5bfd4be | 2020-06-23 13:26:19 +0200 | [diff] [blame] | 354 | for (child = lyd_node_children(node, 0); child; child = child->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 355 | rc = cast_string_recursive(child, 0, root_type, indent + 1, str, used, size); |
| 356 | LY_CHECK_RET(rc); |
| 357 | } |
| 358 | |
| 359 | break; |
| 360 | |
| 361 | case LYS_LEAF: |
| 362 | case LYS_LEAFLIST: |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 363 | value_str = LYD_CANON_VALUE(node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 364 | |
| 365 | /* print indent */ |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 366 | 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] | 367 | memset(*str + (*used - 1), ' ', indent * 2); |
| 368 | *used += indent * 2; |
| 369 | |
| 370 | /* print value */ |
| 371 | if (*used == 1) { |
| 372 | sprintf(*str + (*used - 1), "%s", value_str); |
| 373 | *used += strlen(value_str); |
| 374 | } else { |
| 375 | sprintf(*str + (*used - 1), "%s\n", value_str); |
| 376 | *used += strlen(value_str) + 1; |
| 377 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 378 | |
| 379 | break; |
| 380 | |
| 381 | case LYS_ANYXML: |
| 382 | case LYS_ANYDATA: |
| 383 | any = (struct lyd_node_any *)node; |
| 384 | if (!(void *)any->value.tree) { |
| 385 | /* no content */ |
| 386 | buf = strdup(""); |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 387 | LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_CTX(node)), LY_EMEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 388 | } else { |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 389 | struct ly_out *out; |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 390 | |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 391 | if (any->value_type == LYD_ANYDATA_LYB) { |
| 392 | /* try to parse it into a data tree */ |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 393 | 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] | 394 | /* successfully parsed */ |
| 395 | free(any->value.mem); |
| 396 | any->value.tree = tree; |
| 397 | any->value_type = LYD_ANYDATA_DATATREE; |
| 398 | } |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 399 | /* 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] | 400 | } |
| 401 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 402 | switch (any->value_type) { |
| 403 | case LYD_ANYDATA_STRING: |
| 404 | case LYD_ANYDATA_XML: |
| 405 | case LYD_ANYDATA_JSON: |
| 406 | buf = strdup(any->value.json); |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 407 | LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_CTX(node)), LY_EMEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 408 | break; |
| 409 | case LYD_ANYDATA_DATATREE: |
Radek Krejci | 84ce7b1 | 2020-06-11 17:28:25 +0200 | [diff] [blame] | 410 | LY_CHECK_RET(ly_out_new_memory(&buf, 0, &out)); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 411 | rc = lyd_print_all(out, any->value.tree, LYD_XML, 0); |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 412 | ly_out_free(out, NULL, 0); |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 413 | LY_CHECK_RET(rc < 0, -rc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 414 | break; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 415 | case LYD_ANYDATA_LYB: |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 416 | LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot convert LYB anydata into string."); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 417 | return LY_EINVAL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 418 | } |
| 419 | } |
| 420 | |
| 421 | line = strtok_r(buf, "\n", &ptr); |
| 422 | do { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 423 | 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] | 424 | if (rc != LY_SUCCESS) { |
| 425 | free(buf); |
| 426 | return rc; |
| 427 | } |
| 428 | memset(*str + (*used - 1), ' ', indent * 2); |
| 429 | *used += indent * 2; |
| 430 | |
| 431 | strcpy(*str + (*used - 1), line); |
| 432 | *used += strlen(line); |
| 433 | |
| 434 | strcpy(*str + (*used - 1), "\n"); |
| 435 | *used += 1; |
| 436 | } while ((line = strtok_r(NULL, "\n", &ptr))); |
| 437 | |
| 438 | free(buf); |
| 439 | break; |
| 440 | |
| 441 | default: |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 442 | LOGINT_RET(LYD_CTX(node)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | if (fake_cont) { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 446 | rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 447 | LY_CHECK_RET(rc); |
| 448 | strcpy(*str + (*used - 1), "\n"); |
| 449 | ++(*used); |
| 450 | |
| 451 | --indent; |
| 452 | } |
| 453 | |
| 454 | return LY_SUCCESS; |
| 455 | } |
| 456 | |
| 457 | /** |
| 458 | * @brief Cast an element into a string. |
| 459 | * |
| 460 | * @param[in] node Node to cast. |
| 461 | * @param[in] fake_cont Whether to put the data into a "fake" container. |
| 462 | * @param[in] root_type Type of the XPath root. |
| 463 | * @param[out] str Element cast to dynamically-allocated string. |
| 464 | * @return LY_ERR |
| 465 | */ |
| 466 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 467 | cast_string_elem(struct lyd_node *node, uint8_t fake_cont, enum lyxp_node_type root_type, char **str) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 468 | { |
| 469 | uint16_t used, size; |
| 470 | LY_ERR rc; |
| 471 | |
| 472 | *str = malloc(LYXP_STRING_CAST_SIZE_START * sizeof(char)); |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 473 | LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_CTX(node)), LY_EMEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 474 | (*str)[0] = '\0'; |
| 475 | used = 1; |
| 476 | size = LYXP_STRING_CAST_SIZE_START; |
| 477 | |
| 478 | rc = cast_string_recursive(node, fake_cont, root_type, 0, str, &used, &size); |
| 479 | if (rc != LY_SUCCESS) { |
| 480 | free(*str); |
| 481 | return rc; |
| 482 | } |
| 483 | |
| 484 | if (size > used) { |
| 485 | *str = ly_realloc(*str, used * sizeof(char)); |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 486 | LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_CTX(node)), LY_EMEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 487 | } |
| 488 | return LY_SUCCESS; |
| 489 | } |
| 490 | |
| 491 | /** |
| 492 | * @brief Cast a LYXP_SET_NODE_SET set into a string. |
| 493 | * Context position aware. |
| 494 | * |
| 495 | * @param[in] set Set to cast. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 496 | * @param[out] str Cast dynamically-allocated string. |
| 497 | * @return LY_ERR |
| 498 | */ |
| 499 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 500 | cast_node_set_to_string(struct lyxp_set *set, char **str) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 501 | { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 502 | switch (set->val.nodes[0].type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 503 | case LYXP_NODE_NONE: |
| 504 | /* invalid */ |
| 505 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 506 | case LYXP_NODE_ROOT: |
| 507 | case LYXP_NODE_ROOT_CONFIG: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 508 | 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] | 509 | case LYXP_NODE_ELEM: |
| 510 | case LYXP_NODE_TEXT: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 511 | 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] | 512 | case LYXP_NODE_META: |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 513 | *str = strdup(set->val.meta[0].meta->value.canonical); |
| 514 | if (!*str) { |
| 515 | LOGMEM_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 516 | } |
| 517 | return LY_SUCCESS; |
| 518 | } |
| 519 | |
| 520 | LOGINT_RET(set->ctx); |
| 521 | } |
| 522 | |
| 523 | /** |
| 524 | * @brief Cast a string into an XPath number. |
| 525 | * |
| 526 | * @param[in] str String to use. |
| 527 | * @return Cast number. |
| 528 | */ |
| 529 | static long double |
| 530 | cast_string_to_number(const char *str) |
| 531 | { |
| 532 | long double num; |
| 533 | char *ptr; |
| 534 | |
| 535 | errno = 0; |
| 536 | num = strtold(str, &ptr); |
| 537 | if (errno || *ptr) { |
| 538 | num = NAN; |
| 539 | } |
| 540 | return num; |
| 541 | } |
| 542 | |
| 543 | /** |
| 544 | * @brief Callback for checking value equality. |
| 545 | * |
| 546 | * @param[in] val1_p First value. |
| 547 | * @param[in] val2_p Second value. |
| 548 | * @param[in] mod Whether hash table is being modified. |
| 549 | * @param[in] cb_data Callback data. |
| 550 | * @return 0 if not equal, non-zero if equal. |
| 551 | */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 552 | static uint8_t |
| 553 | set_values_equal_cb(void *val1_p, void *val2_p, uint8_t UNUSED(mod), void *UNUSED(cb_data)) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 554 | { |
| 555 | struct lyxp_set_hash_node *val1, *val2; |
| 556 | |
| 557 | val1 = (struct lyxp_set_hash_node *)val1_p; |
| 558 | val2 = (struct lyxp_set_hash_node *)val2_p; |
| 559 | |
| 560 | if ((val1->node == val2->node) && (val1->type == val2->type)) { |
| 561 | return 1; |
| 562 | } |
| 563 | |
| 564 | return 0; |
| 565 | } |
| 566 | |
| 567 | /** |
| 568 | * @brief Insert node and its hash into set. |
| 569 | * |
| 570 | * @param[in] set et to insert to. |
| 571 | * @param[in] node Node with hash. |
| 572 | * @param[in] type Node type. |
| 573 | */ |
| 574 | static void |
| 575 | set_insert_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type) |
| 576 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 577 | LY_ERR r; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 578 | uint32_t i, hash; |
| 579 | struct lyxp_set_hash_node hnode; |
| 580 | |
| 581 | if (!set->ht && (set->used >= LYD_HT_MIN_ITEMS)) { |
| 582 | /* create hash table and add all the nodes */ |
| 583 | set->ht = lyht_new(1, sizeof(struct lyxp_set_hash_node), set_values_equal_cb, NULL, 1); |
| 584 | for (i = 0; i < set->used; ++i) { |
| 585 | hnode.node = set->val.nodes[i].node; |
| 586 | hnode.type = set->val.nodes[i].type; |
| 587 | |
| 588 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 589 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 590 | hash = dict_hash_multi(hash, NULL, 0); |
| 591 | |
| 592 | r = lyht_insert(set->ht, &hnode, hash, NULL); |
| 593 | assert(!r); |
| 594 | (void)r; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 595 | |
Michal Vasko | 9d6befd | 2019-12-11 14:56:56 +0100 | [diff] [blame] | 596 | if (hnode.node == node) { |
| 597 | /* it was just added, do not add it twice */ |
| 598 | node = NULL; |
| 599 | } |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | if (set->ht && node) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 604 | /* add the new node into hash table */ |
| 605 | hnode.node = node; |
| 606 | hnode.type = type; |
| 607 | |
| 608 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 609 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 610 | hash = dict_hash_multi(hash, NULL, 0); |
| 611 | |
| 612 | r = lyht_insert(set->ht, &hnode, hash, NULL); |
| 613 | assert(!r); |
| 614 | (void)r; |
| 615 | } |
| 616 | } |
| 617 | |
| 618 | /** |
| 619 | * @brief Remove node and its hash from set. |
| 620 | * |
| 621 | * @param[in] set Set to remove from. |
| 622 | * @param[in] node Node to remove. |
| 623 | * @param[in] type Node type. |
| 624 | */ |
| 625 | static void |
| 626 | set_remove_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type) |
| 627 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 628 | LY_ERR r; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 629 | struct lyxp_set_hash_node hnode; |
| 630 | uint32_t hash; |
| 631 | |
| 632 | if (set->ht) { |
| 633 | hnode.node = node; |
| 634 | hnode.type = type; |
| 635 | |
| 636 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 637 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 638 | hash = dict_hash_multi(hash, NULL, 0); |
| 639 | |
| 640 | r = lyht_remove(set->ht, &hnode, hash); |
| 641 | assert(!r); |
| 642 | (void)r; |
| 643 | |
| 644 | if (!set->ht->used) { |
| 645 | lyht_free(set->ht); |
| 646 | set->ht = NULL; |
| 647 | } |
| 648 | } |
| 649 | } |
| 650 | |
| 651 | /** |
| 652 | * @brief Check whether node is in set based on its hash. |
| 653 | * |
| 654 | * @param[in] set Set to search in. |
| 655 | * @param[in] node Node to search for. |
| 656 | * @param[in] type Node type. |
| 657 | * @param[in] skip_idx Index in @p set to skip. |
| 658 | * @return LY_ERR |
| 659 | */ |
| 660 | static LY_ERR |
| 661 | set_dup_node_hash_check(const struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type, int skip_idx) |
| 662 | { |
| 663 | struct lyxp_set_hash_node hnode, *match_p; |
| 664 | uint32_t hash; |
| 665 | |
| 666 | hnode.node = node; |
| 667 | hnode.type = type; |
| 668 | |
| 669 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 670 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 671 | hash = dict_hash_multi(hash, NULL, 0); |
| 672 | |
| 673 | if (!lyht_find(set->ht, &hnode, hash, (void **)&match_p)) { |
| 674 | if ((skip_idx > -1) && (set->val.nodes[skip_idx].node == match_p->node) && (set->val.nodes[skip_idx].type == match_p->type)) { |
| 675 | /* we found it on the index that should be skipped, find another */ |
| 676 | hnode = *match_p; |
| 677 | if (lyht_find_next(set->ht, &hnode, hash, (void **)&match_p)) { |
| 678 | /* none other found */ |
| 679 | return LY_SUCCESS; |
| 680 | } |
| 681 | } |
| 682 | |
| 683 | return LY_EEXIST; |
| 684 | } |
| 685 | |
| 686 | /* not found */ |
| 687 | return LY_SUCCESS; |
| 688 | } |
| 689 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 690 | void |
| 691 | lyxp_set_free_content(struct lyxp_set *set) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 692 | { |
| 693 | if (!set) { |
| 694 | return; |
| 695 | } |
| 696 | |
| 697 | if (set->type == LYXP_SET_NODE_SET) { |
| 698 | free(set->val.nodes); |
| 699 | lyht_free(set->ht); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 700 | } else if (set->type == LYXP_SET_SCNODE_SET) { |
| 701 | free(set->val.scnodes); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 702 | lyht_free(set->ht); |
| 703 | } else { |
| 704 | if (set->type == LYXP_SET_STRING) { |
| 705 | free(set->val.str); |
| 706 | } |
| 707 | set->type = LYXP_SET_NODE_SET; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 708 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 709 | |
| 710 | set->val.nodes = NULL; |
| 711 | set->used = 0; |
| 712 | set->size = 0; |
| 713 | set->ht = NULL; |
| 714 | set->ctx_pos = 0; |
| 715 | set->ctx_pos = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 716 | } |
| 717 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 718 | /** |
| 719 | * @brief Free dynamically-allocated set. |
| 720 | * |
| 721 | * @param[in] set Set to free. |
| 722 | */ |
| 723 | static void |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 724 | lyxp_set_free(struct lyxp_set *set) |
| 725 | { |
| 726 | if (!set) { |
| 727 | return; |
| 728 | } |
| 729 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 730 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 731 | free(set); |
| 732 | } |
| 733 | |
| 734 | /** |
| 735 | * @brief Initialize set context. |
| 736 | * |
| 737 | * @param[in] new Set to initialize. |
| 738 | * @param[in] set Arbitrary initialized set. |
| 739 | */ |
| 740 | static void |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 741 | set_init(struct lyxp_set *new, const struct lyxp_set *set) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 742 | { |
| 743 | memset(new, 0, sizeof *new); |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 744 | if (set) { |
| 745 | new->ctx = set->ctx; |
| 746 | new->ctx_node = set->ctx_node; |
Michal Vasko | 588112f | 2019-12-10 14:51:53 +0100 | [diff] [blame] | 747 | new->root_type = set->root_type; |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 748 | new->context_op = set->context_op; |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 749 | new->local_mod = set->local_mod; |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 750 | new->tree = set->tree; |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 751 | new->format = set->format; |
| 752 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 753 | } |
| 754 | |
| 755 | /** |
| 756 | * @brief Create a deep copy of a set. |
| 757 | * |
| 758 | * @param[in] set Set to copy. |
| 759 | * @return Copy of @p set. |
| 760 | */ |
| 761 | static struct lyxp_set * |
| 762 | set_copy(struct lyxp_set *set) |
| 763 | { |
| 764 | struct lyxp_set *ret; |
| 765 | uint16_t i; |
| 766 | |
| 767 | if (!set) { |
| 768 | return NULL; |
| 769 | } |
| 770 | |
| 771 | ret = malloc(sizeof *ret); |
| 772 | LY_CHECK_ERR_RET(!ret, LOGMEM(set->ctx), NULL); |
| 773 | set_init(ret, set); |
| 774 | |
| 775 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 776 | ret->type = set->type; |
| 777 | |
| 778 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | ba71654 | 2019-12-16 10:01:58 +0100 | [diff] [blame] | 779 | if ((set->val.scnodes[i].in_ctx == 1) || (set->val.scnodes[i].in_ctx == -2)) { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 780 | int idx = lyxp_set_scnode_insert_node(ret, set->val.scnodes[i].scnode, set->val.scnodes[i].type); |
Michal Vasko | 3f27c52 | 2020-01-06 08:37:49 +0100 | [diff] [blame] | 781 | /* coverity seems to think scnodes can be NULL */ |
| 782 | if ((idx == -1) || !ret->val.scnodes) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 783 | lyxp_set_free(ret); |
| 784 | return NULL; |
| 785 | } |
Michal Vasko | ba71654 | 2019-12-16 10:01:58 +0100 | [diff] [blame] | 786 | ret->val.scnodes[idx].in_ctx = set->val.scnodes[i].in_ctx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 787 | } |
| 788 | } |
| 789 | } else if (set->type == LYXP_SET_NODE_SET) { |
| 790 | ret->type = set->type; |
| 791 | ret->val.nodes = malloc(set->used * sizeof *ret->val.nodes); |
| 792 | LY_CHECK_ERR_RET(!ret->val.nodes, LOGMEM(set->ctx); free(ret), NULL); |
| 793 | memcpy(ret->val.nodes, set->val.nodes, set->used * sizeof *ret->val.nodes); |
| 794 | |
| 795 | ret->used = ret->size = set->used; |
| 796 | ret->ctx_pos = set->ctx_pos; |
| 797 | ret->ctx_size = set->ctx_size; |
| 798 | ret->ht = lyht_dup(set->ht); |
| 799 | } else { |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 800 | memcpy(ret, set, sizeof *ret); |
| 801 | if (set->type == LYXP_SET_STRING) { |
| 802 | ret->val.str = strdup(set->val.str); |
| 803 | LY_CHECK_ERR_RET(!ret->val.str, LOGMEM(set->ctx); free(ret), NULL); |
| 804 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 805 | } |
| 806 | |
| 807 | return ret; |
| 808 | } |
| 809 | |
| 810 | /** |
| 811 | * @brief Fill XPath set with a string. Any current data are disposed of. |
| 812 | * |
| 813 | * @param[in] set Set to fill. |
| 814 | * @param[in] string String to fill into \p set. |
| 815 | * @param[in] str_len Length of \p string. 0 is a valid value! |
| 816 | */ |
| 817 | static void |
| 818 | set_fill_string(struct lyxp_set *set, const char *string, uint16_t str_len) |
| 819 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 820 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 821 | |
| 822 | set->type = LYXP_SET_STRING; |
| 823 | if ((str_len == 0) && (string[0] != '\0')) { |
| 824 | string = ""; |
| 825 | } |
| 826 | set->val.str = strndup(string, str_len); |
| 827 | } |
| 828 | |
| 829 | /** |
| 830 | * @brief Fill XPath set with a number. Any current data are disposed of. |
| 831 | * |
| 832 | * @param[in] set Set to fill. |
| 833 | * @param[in] number Number to fill into \p set. |
| 834 | */ |
| 835 | static void |
| 836 | set_fill_number(struct lyxp_set *set, long double number) |
| 837 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 838 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 839 | |
| 840 | set->type = LYXP_SET_NUMBER; |
| 841 | set->val.num = number; |
| 842 | } |
| 843 | |
| 844 | /** |
| 845 | * @brief Fill XPath set with a boolean. Any current data are disposed of. |
| 846 | * |
| 847 | * @param[in] set Set to fill. |
| 848 | * @param[in] boolean Boolean to fill into \p set. |
| 849 | */ |
| 850 | static void |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 851 | set_fill_boolean(struct lyxp_set *set, uint8_t boolean) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 852 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 853 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 854 | |
| 855 | set->type = LYXP_SET_BOOLEAN; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 856 | set->val.bln = boolean; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 857 | } |
| 858 | |
| 859 | /** |
| 860 | * @brief Fill XPath set with the value from another set (deep assign). |
| 861 | * Any current data are disposed of. |
| 862 | * |
| 863 | * @param[in] trg Set to fill. |
| 864 | * @param[in] src Source set to copy into \p trg. |
| 865 | */ |
| 866 | static void |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 867 | set_fill_set(struct lyxp_set *trg, const struct lyxp_set *src) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 868 | { |
| 869 | if (!trg || !src) { |
| 870 | return; |
| 871 | } |
| 872 | |
| 873 | if (trg->type == LYXP_SET_NODE_SET) { |
| 874 | free(trg->val.nodes); |
| 875 | } else if (trg->type == LYXP_SET_STRING) { |
| 876 | free(trg->val.str); |
| 877 | } |
| 878 | set_init(trg, src); |
| 879 | |
| 880 | if (src->type == LYXP_SET_SCNODE_SET) { |
| 881 | trg->type = LYXP_SET_SCNODE_SET; |
| 882 | trg->used = src->used; |
| 883 | trg->size = src->used; |
| 884 | |
| 885 | trg->val.scnodes = ly_realloc(trg->val.scnodes, trg->size * sizeof *trg->val.scnodes); |
| 886 | LY_CHECK_ERR_RET(!trg->val.scnodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), ); |
| 887 | memcpy(trg->val.scnodes, src->val.scnodes, src->used * sizeof *src->val.scnodes); |
| 888 | } else if (src->type == LYXP_SET_BOOLEAN) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 889 | set_fill_boolean(trg, src->val.bln); |
Michal Vasko | 44f3d2c | 2020-08-24 09:49:38 +0200 | [diff] [blame] | 890 | } else if (src->type == LYXP_SET_NUMBER) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 891 | set_fill_number(trg, src->val.num); |
| 892 | } else if (src->type == LYXP_SET_STRING) { |
| 893 | set_fill_string(trg, src->val.str, strlen(src->val.str)); |
| 894 | } else { |
| 895 | if (trg->type == LYXP_SET_NODE_SET) { |
| 896 | free(trg->val.nodes); |
| 897 | } else if (trg->type == LYXP_SET_STRING) { |
| 898 | free(trg->val.str); |
| 899 | } |
| 900 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 901 | assert(src->type == LYXP_SET_NODE_SET); |
| 902 | |
| 903 | trg->type = LYXP_SET_NODE_SET; |
| 904 | trg->used = src->used; |
| 905 | trg->size = src->used; |
| 906 | trg->ctx_pos = src->ctx_pos; |
| 907 | trg->ctx_size = src->ctx_size; |
| 908 | |
| 909 | trg->val.nodes = malloc(trg->used * sizeof *trg->val.nodes); |
| 910 | LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), ); |
| 911 | memcpy(trg->val.nodes, src->val.nodes, src->used * sizeof *src->val.nodes); |
| 912 | if (src->ht) { |
| 913 | trg->ht = lyht_dup(src->ht); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 914 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 915 | trg->ht = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 916 | } |
| 917 | } |
| 918 | } |
| 919 | |
| 920 | /** |
| 921 | * @brief Clear context of all schema nodes. |
| 922 | * |
| 923 | * @param[in] set Set to clear. |
| 924 | */ |
| 925 | static void |
| 926 | set_scnode_clear_ctx(struct lyxp_set *set) |
| 927 | { |
| 928 | uint32_t i; |
| 929 | |
| 930 | for (i = 0; i < set->used; ++i) { |
| 931 | if (set->val.scnodes[i].in_ctx == 1) { |
| 932 | set->val.scnodes[i].in_ctx = 0; |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 933 | } else if (set->val.scnodes[i].in_ctx == -2) { |
| 934 | set->val.scnodes[i].in_ctx = -1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 935 | } |
| 936 | } |
| 937 | } |
| 938 | |
| 939 | /** |
| 940 | * @brief Remove a node from a set. Removing last node changes |
| 941 | * set into LYXP_SET_EMPTY. Context position aware. |
| 942 | * |
| 943 | * @param[in] set Set to use. |
| 944 | * @param[in] idx Index from @p set of the node to be removed. |
| 945 | */ |
| 946 | static void |
| 947 | set_remove_node(struct lyxp_set *set, uint32_t idx) |
| 948 | { |
| 949 | assert(set && (set->type == LYXP_SET_NODE_SET)); |
| 950 | assert(idx < set->used); |
| 951 | |
| 952 | set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
| 953 | |
| 954 | --set->used; |
| 955 | if (set->used) { |
| 956 | memmove(&set->val.nodes[idx], &set->val.nodes[idx + 1], |
| 957 | (set->used - idx) * sizeof *set->val.nodes); |
| 958 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 959 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 960 | } |
| 961 | } |
| 962 | |
| 963 | /** |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 964 | * @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] | 965 | * |
| 966 | * @param[in] set Set to use. |
| 967 | * @param[in] idx Index from @p set of the node to be removed. |
| 968 | */ |
| 969 | static void |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 970 | set_remove_node_none(struct lyxp_set *set, uint32_t idx) |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 971 | { |
| 972 | assert(set && (set->type == LYXP_SET_NODE_SET)); |
| 973 | assert(idx < set->used); |
| 974 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 975 | if (set->val.nodes[idx].type == LYXP_NODE_ELEM) { |
| 976 | set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
| 977 | } |
| 978 | set->val.nodes[idx].type = LYXP_NODE_NONE; |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 979 | } |
| 980 | |
| 981 | /** |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 982 | * @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] | 983 | * set into LYXP_SET_EMPTY. Context position aware. |
| 984 | * |
| 985 | * @param[in] set Set to consolidate. |
| 986 | */ |
| 987 | static void |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 988 | set_remove_nodes_none(struct lyxp_set *set) |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 989 | { |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 990 | uint16_t i, orig_used, end = 0; |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 991 | int32_t start; |
| 992 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 993 | assert(set); |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 994 | |
| 995 | orig_used = set->used; |
| 996 | set->used = 0; |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 997 | for (i = 0; i < orig_used; ) { |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 998 | start = -1; |
| 999 | do { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1000 | if ((set->val.nodes[i].type != LYXP_NODE_NONE) && (start == -1)) { |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1001 | start = i; |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1002 | } else if ((start > -1) && (set->val.nodes[i].type == LYXP_NODE_NONE)) { |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1003 | end = i; |
| 1004 | ++i; |
| 1005 | break; |
| 1006 | } |
| 1007 | |
| 1008 | ++i; |
| 1009 | if (i == orig_used) { |
| 1010 | end = i; |
| 1011 | } |
| 1012 | } while (i < orig_used); |
| 1013 | |
| 1014 | if (start > -1) { |
| 1015 | /* move the whole chunk of valid nodes together */ |
| 1016 | if (set->used != (unsigned)start) { |
| 1017 | memmove(&set->val.nodes[set->used], &set->val.nodes[start], (end - start) * sizeof *set->val.nodes); |
| 1018 | } |
| 1019 | set->used += end - start; |
| 1020 | } |
| 1021 | } |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1022 | } |
| 1023 | |
| 1024 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1025 | * @brief Check for duplicates in a node set. |
| 1026 | * |
| 1027 | * @param[in] set Set to check. |
| 1028 | * @param[in] node Node to look for in @p set. |
| 1029 | * @param[in] node_type Type of @p node. |
| 1030 | * @param[in] skip_idx Index from @p set to skip. |
| 1031 | * @return LY_ERR |
| 1032 | */ |
| 1033 | static LY_ERR |
| 1034 | set_dup_node_check(const struct lyxp_set *set, const struct lyd_node *node, enum lyxp_node_type node_type, int skip_idx) |
| 1035 | { |
| 1036 | uint32_t i; |
| 1037 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1038 | if (set->ht && node) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1039 | return set_dup_node_hash_check(set, (struct lyd_node *)node, node_type, skip_idx); |
| 1040 | } |
| 1041 | |
| 1042 | for (i = 0; i < set->used; ++i) { |
| 1043 | if ((skip_idx > -1) && (i == (unsigned)skip_idx)) { |
| 1044 | continue; |
| 1045 | } |
| 1046 | |
| 1047 | if ((set->val.nodes[i].node == node) && (set->val.nodes[i].type == node_type)) { |
| 1048 | return LY_EEXIST; |
| 1049 | } |
| 1050 | } |
| 1051 | |
| 1052 | return LY_SUCCESS; |
| 1053 | } |
| 1054 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 1055 | int |
| 1056 | lyxp_set_scnode_dup_node_check(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, int skip_idx) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1057 | { |
| 1058 | uint32_t i; |
| 1059 | |
| 1060 | for (i = 0; i < set->used; ++i) { |
| 1061 | if ((skip_idx > -1) && (i == (unsigned)skip_idx)) { |
| 1062 | continue; |
| 1063 | } |
| 1064 | |
| 1065 | if ((set->val.scnodes[i].scnode == node) && (set->val.scnodes[i].type == node_type)) { |
| 1066 | return i; |
| 1067 | } |
| 1068 | } |
| 1069 | |
| 1070 | return -1; |
| 1071 | } |
| 1072 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 1073 | void |
| 1074 | lyxp_set_scnode_merge(struct lyxp_set *set1, struct lyxp_set *set2) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1075 | { |
| 1076 | uint32_t orig_used, i, j; |
| 1077 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1078 | 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] | 1079 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1080 | if (!set2->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1081 | return; |
| 1082 | } |
| 1083 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1084 | if (!set1->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1085 | memcpy(set1, set2, sizeof *set1); |
| 1086 | return; |
| 1087 | } |
| 1088 | |
| 1089 | if (set1->used + set2->used > set1->size) { |
| 1090 | set1->size = set1->used + set2->used; |
| 1091 | set1->val.scnodes = ly_realloc(set1->val.scnodes, set1->size * sizeof *set1->val.scnodes); |
| 1092 | LY_CHECK_ERR_RET(!set1->val.scnodes, LOGMEM(set1->ctx), ); |
| 1093 | } |
| 1094 | |
| 1095 | orig_used = set1->used; |
| 1096 | |
| 1097 | for (i = 0; i < set2->used; ++i) { |
| 1098 | for (j = 0; j < orig_used; ++j) { |
| 1099 | /* detect duplicities */ |
| 1100 | if (set1->val.scnodes[j].scnode == set2->val.scnodes[i].scnode) { |
| 1101 | break; |
| 1102 | } |
| 1103 | } |
| 1104 | |
| 1105 | if (j == orig_used) { |
| 1106 | memcpy(&set1->val.scnodes[set1->used], &set2->val.scnodes[i], sizeof *set2->val.scnodes); |
| 1107 | ++set1->used; |
| 1108 | } |
| 1109 | } |
| 1110 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1111 | lyxp_set_free_content(set2); |
| 1112 | set2->type = LYXP_SET_SCNODE_SET; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1113 | } |
| 1114 | |
| 1115 | /** |
| 1116 | * @brief Insert a node into a set. Context position aware. |
| 1117 | * |
| 1118 | * @param[in] set Set to use. |
| 1119 | * @param[in] node Node to insert to @p set. |
| 1120 | * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting. |
| 1121 | * @param[in] node_type Node type of @p node. |
| 1122 | * @param[in] idx Index in @p set to insert into. |
| 1123 | */ |
| 1124 | static void |
| 1125 | set_insert_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx) |
| 1126 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1127 | assert(set && (set->type == LYXP_SET_NODE_SET)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1128 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1129 | if (!set->size) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1130 | /* first item */ |
| 1131 | if (idx) { |
| 1132 | /* no real harm done, but it is a bug */ |
| 1133 | LOGINT(set->ctx); |
| 1134 | idx = 0; |
| 1135 | } |
| 1136 | set->val.nodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.nodes); |
| 1137 | LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), ); |
| 1138 | set->type = LYXP_SET_NODE_SET; |
| 1139 | set->used = 0; |
| 1140 | set->size = LYXP_SET_SIZE_START; |
| 1141 | set->ctx_pos = 1; |
| 1142 | set->ctx_size = 1; |
| 1143 | set->ht = NULL; |
| 1144 | } else { |
| 1145 | /* not an empty set */ |
| 1146 | if (set->used == set->size) { |
| 1147 | |
| 1148 | /* set is full */ |
| 1149 | set->val.nodes = ly_realloc(set->val.nodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.nodes); |
| 1150 | LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), ); |
| 1151 | set->size += LYXP_SET_SIZE_STEP; |
| 1152 | } |
| 1153 | |
| 1154 | if (idx > set->used) { |
| 1155 | LOGINT(set->ctx); |
| 1156 | idx = set->used; |
| 1157 | } |
| 1158 | |
| 1159 | /* make space for the new node */ |
| 1160 | if (idx < set->used) { |
| 1161 | memmove(&set->val.nodes[idx + 1], &set->val.nodes[idx], (set->used - idx) * sizeof *set->val.nodes); |
| 1162 | } |
| 1163 | } |
| 1164 | |
| 1165 | /* finally assign the value */ |
| 1166 | set->val.nodes[idx].node = (struct lyd_node *)node; |
| 1167 | set->val.nodes[idx].type = node_type; |
| 1168 | set->val.nodes[idx].pos = pos; |
| 1169 | ++set->used; |
| 1170 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1171 | if (set->val.nodes[idx].type == LYXP_NODE_ELEM) { |
| 1172 | set_insert_node_hash(set, (struct lyd_node *)node, node_type); |
| 1173 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1174 | } |
| 1175 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 1176 | int |
| 1177 | lyxp_set_scnode_insert_node(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1178 | { |
| 1179 | int ret; |
| 1180 | |
| 1181 | assert(set->type == LYXP_SET_SCNODE_SET); |
| 1182 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 1183 | ret = lyxp_set_scnode_dup_node_check(set, node, node_type, -1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1184 | if (ret > -1) { |
| 1185 | set->val.scnodes[ret].in_ctx = 1; |
| 1186 | } else { |
| 1187 | if (set->used == set->size) { |
| 1188 | set->val.scnodes = ly_realloc(set->val.scnodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.scnodes); |
| 1189 | LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), -1); |
| 1190 | set->size += LYXP_SET_SIZE_STEP; |
| 1191 | } |
| 1192 | |
| 1193 | ret = set->used; |
| 1194 | set->val.scnodes[ret].scnode = (struct lysc_node *)node; |
| 1195 | set->val.scnodes[ret].type = node_type; |
| 1196 | set->val.scnodes[ret].in_ctx = 1; |
| 1197 | ++set->used; |
| 1198 | } |
| 1199 | |
| 1200 | return ret; |
| 1201 | } |
| 1202 | |
| 1203 | /** |
| 1204 | * @brief Replace a node in a set with another. Context position aware. |
| 1205 | * |
| 1206 | * @param[in] set Set to use. |
| 1207 | * @param[in] node Node to insert to @p set. |
| 1208 | * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting. |
| 1209 | * @param[in] node_type Node type of @p node. |
| 1210 | * @param[in] idx Index in @p set of the node to replace. |
| 1211 | */ |
| 1212 | static void |
| 1213 | set_replace_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx) |
| 1214 | { |
| 1215 | assert(set && (idx < set->used)); |
| 1216 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1217 | if (set->val.nodes[idx].type == LYXP_NODE_ELEM) { |
| 1218 | set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
| 1219 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1220 | set->val.nodes[idx].node = (struct lyd_node *)node; |
| 1221 | set->val.nodes[idx].type = node_type; |
| 1222 | set->val.nodes[idx].pos = pos; |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1223 | if (set->val.nodes[idx].type == LYXP_NODE_ELEM) { |
| 1224 | set_insert_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
| 1225 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1226 | } |
| 1227 | |
| 1228 | /** |
| 1229 | * @brief Set all nodes with ctx 1 to a new unique context value. |
| 1230 | * |
| 1231 | * @param[in] set Set to modify. |
| 1232 | * @return New context value. |
| 1233 | */ |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 1234 | static int32_t |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1235 | set_scnode_new_in_ctx(struct lyxp_set *set) |
| 1236 | { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 1237 | uint32_t i; |
| 1238 | int32_t ret_ctx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1239 | |
| 1240 | assert(set->type == LYXP_SET_SCNODE_SET); |
| 1241 | |
| 1242 | ret_ctx = 3; |
| 1243 | retry: |
| 1244 | for (i = 0; i < set->used; ++i) { |
| 1245 | if (set->val.scnodes[i].in_ctx >= ret_ctx) { |
| 1246 | ret_ctx = set->val.scnodes[i].in_ctx + 1; |
| 1247 | goto retry; |
| 1248 | } |
| 1249 | } |
| 1250 | for (i = 0; i < set->used; ++i) { |
| 1251 | if (set->val.scnodes[i].in_ctx == 1) { |
| 1252 | set->val.scnodes[i].in_ctx = ret_ctx; |
| 1253 | } |
| 1254 | } |
| 1255 | |
| 1256 | return ret_ctx; |
| 1257 | } |
| 1258 | |
| 1259 | /** |
| 1260 | * @brief Get unique @p node position in the data. |
| 1261 | * |
| 1262 | * @param[in] node Node to find. |
| 1263 | * @param[in] node_type Node type of @p node. |
| 1264 | * @param[in] root Root node. |
| 1265 | * @param[in] root_type Type of the XPath @p root node. |
| 1266 | * @param[in] prev Node that we think is before @p node in DFS from @p root. Can optionally |
| 1267 | * be used to increase efficiency and start the DFS from this node. |
| 1268 | * @param[in] prev_pos Node @p prev position. Optional, but must be set if @p prev is set. |
| 1269 | * @return Node position. |
| 1270 | */ |
| 1271 | static uint32_t |
| 1272 | 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] | 1273 | 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] | 1274 | { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1275 | const struct lyd_node *elem = NULL, *top_sibling; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1276 | uint32_t pos = 1; |
| 1277 | |
| 1278 | assert(prev && prev_pos && !root->prev->next); |
| 1279 | |
| 1280 | if ((node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ROOT_CONFIG)) { |
| 1281 | return 0; |
| 1282 | } |
| 1283 | |
| 1284 | if (*prev) { |
| 1285 | /* start from the previous element instead from the root */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1286 | pos = *prev_pos; |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 1287 | for (top_sibling = *prev; top_sibling->parent; top_sibling = (struct lyd_node *)top_sibling->parent) {} |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1288 | goto dfs_search; |
| 1289 | } |
| 1290 | |
| 1291 | for (top_sibling = root; top_sibling; top_sibling = top_sibling->next) { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1292 | LYD_TREE_DFS_BEGIN(top_sibling, elem) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1293 | dfs_search: |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1294 | if (*prev && !elem) { |
| 1295 | /* resume previous DFS */ |
| 1296 | elem = LYD_TREE_DFS_next = (struct lyd_node *)*prev; |
| 1297 | LYD_TREE_DFS_continue = 0; |
| 1298 | } |
| 1299 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1300 | 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] | 1301 | /* skip */ |
| 1302 | LYD_TREE_DFS_continue = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1303 | } else { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1304 | if (elem == node) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1305 | break; |
| 1306 | } |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1307 | ++pos; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1308 | } |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1309 | |
| 1310 | LYD_TREE_DFS_END(top_sibling, elem); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1311 | } |
| 1312 | |
| 1313 | /* node found */ |
| 1314 | if (elem) { |
| 1315 | break; |
| 1316 | } |
| 1317 | } |
| 1318 | |
| 1319 | if (!elem) { |
| 1320 | if (!(*prev)) { |
| 1321 | /* we went from root and failed to find it, cannot be */ |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1322 | LOGINT(LYD_CTX(node)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1323 | return 0; |
| 1324 | } else { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1325 | /* start the search again from the beginning */ |
| 1326 | *prev = root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1327 | |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1328 | top_sibling = root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1329 | pos = 1; |
| 1330 | goto dfs_search; |
| 1331 | } |
| 1332 | } |
| 1333 | |
| 1334 | /* remember the last found node for next time */ |
| 1335 | *prev = node; |
| 1336 | *prev_pos = pos; |
| 1337 | |
| 1338 | return pos; |
| 1339 | } |
| 1340 | |
| 1341 | /** |
| 1342 | * @brief Assign (fill) missing node positions. |
| 1343 | * |
| 1344 | * @param[in] set Set to fill positions in. |
| 1345 | * @param[in] root Context root node. |
| 1346 | * @param[in] root_type Context root type. |
| 1347 | * @return LY_ERR |
| 1348 | */ |
| 1349 | static LY_ERR |
| 1350 | set_assign_pos(struct lyxp_set *set, const struct lyd_node *root, enum lyxp_node_type root_type) |
| 1351 | { |
| 1352 | const struct lyd_node *prev = NULL, *tmp_node; |
| 1353 | uint32_t i, tmp_pos = 0; |
| 1354 | |
| 1355 | for (i = 0; i < set->used; ++i) { |
| 1356 | if (!set->val.nodes[i].pos) { |
| 1357 | tmp_node = NULL; |
| 1358 | switch (set->val.nodes[i].type) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1359 | case LYXP_NODE_META: |
| 1360 | tmp_node = set->val.meta[i].meta->parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1361 | if (!tmp_node) { |
| 1362 | LOGINT_RET(root->schema->module->ctx); |
| 1363 | } |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1364 | /* fallthrough */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1365 | case LYXP_NODE_ELEM: |
| 1366 | case LYXP_NODE_TEXT: |
| 1367 | if (!tmp_node) { |
| 1368 | tmp_node = set->val.nodes[i].node; |
| 1369 | } |
| 1370 | set->val.nodes[i].pos = get_node_pos(tmp_node, set->val.nodes[i].type, root, root_type, &prev, &tmp_pos); |
| 1371 | break; |
| 1372 | default: |
| 1373 | /* all roots have position 0 */ |
| 1374 | break; |
| 1375 | } |
| 1376 | } |
| 1377 | } |
| 1378 | |
| 1379 | return LY_SUCCESS; |
| 1380 | } |
| 1381 | |
| 1382 | /** |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1383 | * @brief Get unique @p meta position in the parent metadata. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1384 | * |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1385 | * @param[in] meta Metadata to use. |
| 1386 | * @return Metadata position. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1387 | */ |
| 1388 | static uint16_t |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1389 | get_meta_pos(struct lyd_meta *meta) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1390 | { |
| 1391 | uint16_t pos = 0; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1392 | struct lyd_meta *meta2; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1393 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1394 | for (meta2 = meta->parent->meta; meta2 && (meta2 != meta); meta2 = meta2->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1395 | ++pos; |
| 1396 | } |
| 1397 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1398 | assert(meta2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1399 | return pos; |
| 1400 | } |
| 1401 | |
| 1402 | /** |
| 1403 | * @brief Compare 2 nodes in respect to XPath document order. |
| 1404 | * |
| 1405 | * @param[in] item1 1st node. |
| 1406 | * @param[in] item2 2nd node. |
| 1407 | * @return If 1st > 2nd returns 1, 1st == 2nd returns 0, and 1st < 2nd returns -1. |
| 1408 | */ |
| 1409 | static int |
| 1410 | set_sort_compare(struct lyxp_set_node *item1, struct lyxp_set_node *item2) |
| 1411 | { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1412 | uint32_t meta_pos1 = 0, meta_pos2 = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1413 | |
| 1414 | if (item1->pos < item2->pos) { |
| 1415 | return -1; |
| 1416 | } |
| 1417 | |
| 1418 | if (item1->pos > item2->pos) { |
| 1419 | return 1; |
| 1420 | } |
| 1421 | |
| 1422 | /* node positions are equal, the fun case */ |
| 1423 | |
| 1424 | /* 1st ELEM - == - 2nd TEXT, 1st TEXT - == - 2nd ELEM */ |
| 1425 | /* special case since text nodes are actually saved as their parents */ |
| 1426 | if ((item1->node == item2->node) && (item1->type != item2->type)) { |
| 1427 | if (item1->type == LYXP_NODE_ELEM) { |
| 1428 | assert(item2->type == LYXP_NODE_TEXT); |
| 1429 | return -1; |
| 1430 | } else { |
| 1431 | assert((item1->type == LYXP_NODE_TEXT) && (item2->type == LYXP_NODE_ELEM)); |
| 1432 | return 1; |
| 1433 | } |
| 1434 | } |
| 1435 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1436 | /* we need meta positions now */ |
| 1437 | if (item1->type == LYXP_NODE_META) { |
| 1438 | meta_pos1 = get_meta_pos((struct lyd_meta *)item1->node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1439 | } |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1440 | if (item2->type == LYXP_NODE_META) { |
| 1441 | meta_pos2 = get_meta_pos((struct lyd_meta *)item2->node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1442 | } |
| 1443 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1444 | /* 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] | 1445 | /* check for duplicates */ |
| 1446 | if (item1->node == item2->node) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1447 | 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] | 1448 | return 0; |
| 1449 | } |
| 1450 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1451 | /* 1st ELEM - 2nd TEXT, 1st ELEM - any pos - 2nd META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1452 | /* elem is always first, 2nd node is after it */ |
| 1453 | if (item1->type == LYXP_NODE_ELEM) { |
| 1454 | assert(item2->type != LYXP_NODE_ELEM); |
| 1455 | return -1; |
| 1456 | } |
| 1457 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1458 | /* 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] | 1459 | /* 2nd is before 1st */ |
| 1460 | if (((item1->type == LYXP_NODE_TEXT) |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1461 | && ((item2->type == LYXP_NODE_ELEM) || (item2->type == LYXP_NODE_META))) |
| 1462 | || ((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_ELEM)) |
| 1463 | || (((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_META)) |
| 1464 | && (meta_pos1 > meta_pos2))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1465 | return 1; |
| 1466 | } |
| 1467 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1468 | /* 1st META - any pos - 2nd TEXT, 1st META <pos< - 2nd META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1469 | /* 2nd is after 1st */ |
| 1470 | return -1; |
| 1471 | } |
| 1472 | |
| 1473 | /** |
| 1474 | * @brief Set cast for comparisons. |
| 1475 | * |
| 1476 | * @param[in] trg Target set to cast source into. |
| 1477 | * @param[in] src Source set. |
| 1478 | * @param[in] type Target set type. |
| 1479 | * @param[in] src_idx Source set node index. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1480 | * @return LY_ERR |
| 1481 | */ |
| 1482 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1483 | 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] | 1484 | { |
| 1485 | assert(src->type == LYXP_SET_NODE_SET); |
| 1486 | |
| 1487 | set_init(trg, src); |
| 1488 | |
| 1489 | /* insert node into target set */ |
| 1490 | set_insert_node(trg, src->val.nodes[src_idx].node, src->val.nodes[src_idx].pos, src->val.nodes[src_idx].type, 0); |
| 1491 | |
| 1492 | /* cast target set appropriately */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1493 | return lyxp_set_cast(trg, type); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1494 | } |
| 1495 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1496 | /** |
| 1497 | * @brief Set content canonization for comparisons. |
| 1498 | * |
| 1499 | * @param[in] trg Target set to put the canononized source into. |
| 1500 | * @param[in] src Source set. |
| 1501 | * @param[in] xp_node Source XPath node/meta to use for canonization. |
| 1502 | * @return LY_ERR |
| 1503 | */ |
| 1504 | static LY_ERR |
| 1505 | set_comp_canonize(struct lyxp_set *trg, const struct lyxp_set *src, const struct lyxp_set_node *xp_node) |
| 1506 | { |
| 1507 | struct lysc_type *type = NULL; |
| 1508 | struct lyd_value val; |
| 1509 | struct ly_err_item *err = NULL; |
| 1510 | char *str, *ptr; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 1511 | uint8_t dynamic; |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1512 | LY_ERR rc; |
| 1513 | |
| 1514 | /* is there anything to canonize even? */ |
| 1515 | if ((src->type == LYXP_SET_NUMBER) || (src->type == LYXP_SET_STRING)) { |
| 1516 | /* do we have a type to use for canonization? */ |
| 1517 | if ((xp_node->type == LYXP_NODE_ELEM) && (xp_node->node->schema->nodetype & LYD_NODE_TERM)) { |
| 1518 | type = ((struct lyd_node_term *)xp_node->node)->value.realtype; |
| 1519 | } else if (xp_node->type == LYXP_NODE_META) { |
| 1520 | type = ((struct lyd_meta *)xp_node->node)->value.realtype; |
| 1521 | } |
| 1522 | } |
| 1523 | if (!type) { |
| 1524 | goto fill; |
| 1525 | } |
| 1526 | |
| 1527 | if (src->type == LYXP_SET_NUMBER) { |
| 1528 | /* canonize number */ |
| 1529 | if (asprintf(&str, "%Lf", src->val.num) == -1) { |
| 1530 | LOGMEM(src->ctx); |
| 1531 | return LY_EMEM; |
| 1532 | } |
| 1533 | } else { |
| 1534 | /* canonize string */ |
| 1535 | str = strdup(src->val.str); |
| 1536 | } |
| 1537 | |
| 1538 | /* ignore errors, the value may not satisfy schema constraints */ |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 1539 | rc = type->plugin->store(src->ctx, type, str, strlen(str), LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_DYNAMIC, |
| 1540 | LY_PREF_JSON, NULL, NULL, NULL, &val, &err); |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1541 | ly_err_free(err); |
| 1542 | if (rc) { |
| 1543 | /* invalid value */ |
| 1544 | free(str); |
| 1545 | goto fill; |
| 1546 | } |
| 1547 | |
| 1548 | /* storing successful, now print the canonical value */ |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 1549 | str = (char *)type->plugin->print(&val, LY_PREF_JSON, NULL, &dynamic); |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1550 | |
| 1551 | /* use the canonized value */ |
| 1552 | set_init(trg, src); |
| 1553 | trg->type = src->type; |
| 1554 | if (src->type == LYXP_SET_NUMBER) { |
| 1555 | trg->val.num = strtold(str, &ptr); |
| 1556 | if (dynamic) { |
| 1557 | free(str); |
| 1558 | } |
| 1559 | LY_CHECK_ERR_RET(ptr[0], LOGINT(src->ctx), LY_EINT); |
| 1560 | } else { |
| 1561 | trg->val.str = (dynamic ? str : strdup(str)); |
| 1562 | } |
| 1563 | type->plugin->free(src->ctx, &val); |
| 1564 | return LY_SUCCESS; |
| 1565 | |
| 1566 | fill: |
| 1567 | /* no canonization needed/possible */ |
| 1568 | set_fill_set(trg, src); |
| 1569 | return LY_SUCCESS; |
| 1570 | } |
| 1571 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1572 | #ifndef NDEBUG |
| 1573 | |
| 1574 | /** |
| 1575 | * @brief Bubble sort @p set into XPath document order. |
| 1576 | * Context position aware. Unused in the 'Release' build target. |
| 1577 | * |
| 1578 | * @param[in] set Set to sort. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1579 | * @return How many times the whole set was traversed - 1 (if set was sorted, returns 0). |
| 1580 | */ |
| 1581 | static int |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1582 | set_sort(struct lyxp_set *set) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1583 | { |
| 1584 | uint32_t i, j; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 1585 | int ret = 0, cmp; |
| 1586 | uint8_t inverted, change; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1587 | const struct lyd_node *root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1588 | struct lyxp_set_node item; |
| 1589 | struct lyxp_set_hash_node hnode; |
| 1590 | uint64_t hash; |
| 1591 | |
Michal Vasko | 3cf8fbf | 2020-05-27 15:21:21 +0200 | [diff] [blame] | 1592 | if ((set->type != LYXP_SET_NODE_SET) || (set->used < 2)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1593 | return 0; |
| 1594 | } |
| 1595 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1596 | /* find first top-level node to be used as anchor for positions */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 1597 | for (root = set->ctx_node; root->parent; root = (const struct lyd_node *)root->parent) {} |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 1598 | for ( ; root->prev->next; root = root->prev) {} |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1599 | |
| 1600 | /* fill positions */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1601 | if (set_assign_pos(set, root, set->root_type)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1602 | return -1; |
| 1603 | } |
| 1604 | |
| 1605 | LOGDBG(LY_LDGXPATH, "SORT BEGIN"); |
| 1606 | print_set_debug(set); |
| 1607 | |
| 1608 | for (i = 0; i < set->used; ++i) { |
| 1609 | inverted = 0; |
| 1610 | change = 0; |
| 1611 | |
| 1612 | for (j = 1; j < set->used - i; ++j) { |
| 1613 | /* compare node positions */ |
| 1614 | if (inverted) { |
| 1615 | cmp = set_sort_compare(&set->val.nodes[j], &set->val.nodes[j - 1]); |
| 1616 | } else { |
| 1617 | cmp = set_sort_compare(&set->val.nodes[j - 1], &set->val.nodes[j]); |
| 1618 | } |
| 1619 | |
| 1620 | /* swap if needed */ |
| 1621 | if ((inverted && (cmp < 0)) || (!inverted && (cmp > 0))) { |
| 1622 | change = 1; |
| 1623 | |
| 1624 | item = set->val.nodes[j - 1]; |
| 1625 | set->val.nodes[j - 1] = set->val.nodes[j]; |
| 1626 | set->val.nodes[j] = item; |
| 1627 | } else { |
| 1628 | /* whether node_pos1 should be smaller than node_pos2 or the other way around */ |
| 1629 | inverted = !inverted; |
| 1630 | } |
| 1631 | } |
| 1632 | |
| 1633 | ++ret; |
| 1634 | |
| 1635 | if (!change) { |
| 1636 | break; |
| 1637 | } |
| 1638 | } |
| 1639 | |
| 1640 | LOGDBG(LY_LDGXPATH, "SORT END %d", ret); |
| 1641 | print_set_debug(set); |
| 1642 | |
| 1643 | /* check node hashes */ |
| 1644 | if (set->used >= LYD_HT_MIN_ITEMS) { |
| 1645 | assert(set->ht); |
| 1646 | for (i = 0; i < set->used; ++i) { |
| 1647 | hnode.node = set->val.nodes[i].node; |
| 1648 | hnode.type = set->val.nodes[i].type; |
| 1649 | |
| 1650 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 1651 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 1652 | hash = dict_hash_multi(hash, NULL, 0); |
| 1653 | |
| 1654 | assert(!lyht_find(set->ht, &hnode, hash, NULL)); |
| 1655 | } |
| 1656 | } |
| 1657 | |
| 1658 | return ret - 1; |
| 1659 | } |
| 1660 | |
| 1661 | /** |
| 1662 | * @brief Remove duplicate entries in a sorted node set. |
| 1663 | * |
| 1664 | * @param[in] set Sorted set to check. |
| 1665 | * @return LY_ERR (LY_EEXIST if some duplicates are found) |
| 1666 | */ |
| 1667 | static LY_ERR |
| 1668 | set_sorted_dup_node_clean(struct lyxp_set *set) |
| 1669 | { |
| 1670 | uint32_t i = 0; |
| 1671 | LY_ERR ret = LY_SUCCESS; |
| 1672 | |
| 1673 | if (set->used > 1) { |
| 1674 | while (i < set->used - 1) { |
| 1675 | if ((set->val.nodes[i].node == set->val.nodes[i + 1].node) |
| 1676 | && (set->val.nodes[i].type == set->val.nodes[i + 1].type)) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1677 | set_remove_node_none(set, i + 1); |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1678 | ret = LY_EEXIST; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1679 | } |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1680 | ++i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1681 | } |
| 1682 | } |
| 1683 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1684 | set_remove_nodes_none(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1685 | return ret; |
| 1686 | } |
| 1687 | |
| 1688 | #endif |
| 1689 | |
| 1690 | /** |
| 1691 | * @brief Merge 2 sorted sets into one. |
| 1692 | * |
| 1693 | * @param[in,out] trg Set to merge into. Duplicates are removed. |
| 1694 | * @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] | 1695 | * @return LY_ERR |
| 1696 | */ |
| 1697 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1698 | set_sorted_merge(struct lyxp_set *trg, struct lyxp_set *src) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1699 | { |
| 1700 | uint32_t i, j, k, count, dup_count; |
| 1701 | int cmp; |
| 1702 | const struct lyd_node *root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1703 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1704 | 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] | 1705 | return LY_EINVAL; |
| 1706 | } |
| 1707 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1708 | if (!src->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1709 | return LY_SUCCESS; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1710 | } else if (!trg->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1711 | set_fill_set(trg, src); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1712 | lyxp_set_free_content(src); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1713 | return LY_SUCCESS; |
| 1714 | } |
| 1715 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1716 | /* find first top-level node to be used as anchor for positions */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 1717 | for (root = trg->ctx_node; root->parent; root = (const struct lyd_node *)root->parent) {} |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 1718 | for ( ; root->prev->next; root = root->prev) {} |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1719 | |
| 1720 | /* fill positions */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1721 | 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] | 1722 | return LY_EINT; |
| 1723 | } |
| 1724 | |
| 1725 | #ifndef NDEBUG |
| 1726 | LOGDBG(LY_LDGXPATH, "MERGE target"); |
| 1727 | print_set_debug(trg); |
| 1728 | LOGDBG(LY_LDGXPATH, "MERGE source"); |
| 1729 | print_set_debug(src); |
| 1730 | #endif |
| 1731 | |
| 1732 | /* make memory for the merge (duplicates are not detected yet, so space |
| 1733 | * will likely be wasted on them, too bad) */ |
| 1734 | if (trg->size - trg->used < src->used) { |
| 1735 | trg->size = trg->used + src->used; |
| 1736 | |
| 1737 | trg->val.nodes = ly_realloc(trg->val.nodes, trg->size * sizeof *trg->val.nodes); |
| 1738 | LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx), LY_EMEM); |
| 1739 | } |
| 1740 | |
| 1741 | i = 0; |
| 1742 | j = 0; |
| 1743 | count = 0; |
| 1744 | dup_count = 0; |
| 1745 | do { |
| 1746 | cmp = set_sort_compare(&src->val.nodes[i], &trg->val.nodes[j]); |
| 1747 | if (!cmp) { |
| 1748 | if (!count) { |
| 1749 | /* duplicate, just skip it */ |
| 1750 | ++i; |
| 1751 | ++j; |
| 1752 | } else { |
| 1753 | /* we are copying something already, so let's copy the duplicate too, |
| 1754 | * we are hoping that afterwards there are some more nodes to |
| 1755 | * copy and this way we can copy them all together */ |
| 1756 | ++count; |
| 1757 | ++dup_count; |
| 1758 | ++i; |
| 1759 | ++j; |
| 1760 | } |
| 1761 | } else if (cmp < 0) { |
| 1762 | /* inserting src node into trg, just remember it for now */ |
| 1763 | ++count; |
| 1764 | ++i; |
| 1765 | |
| 1766 | /* insert the hash now */ |
| 1767 | set_insert_node_hash(trg, src->val.nodes[i - 1].node, src->val.nodes[i - 1].type); |
| 1768 | } else if (count) { |
| 1769 | copy_nodes: |
| 1770 | /* time to actually copy the nodes, we have found the largest block of nodes */ |
| 1771 | memmove(&trg->val.nodes[j + (count - dup_count)], |
| 1772 | &trg->val.nodes[j], |
| 1773 | (trg->used - j) * sizeof *trg->val.nodes); |
| 1774 | memcpy(&trg->val.nodes[j - dup_count], &src->val.nodes[i - count], count * sizeof *src->val.nodes); |
| 1775 | |
| 1776 | trg->used += count - dup_count; |
| 1777 | /* do not change i, except the copying above, we are basically doing exactly what is in the else branch below */ |
| 1778 | j += count - dup_count; |
| 1779 | |
| 1780 | count = 0; |
| 1781 | dup_count = 0; |
| 1782 | } else { |
| 1783 | ++j; |
| 1784 | } |
| 1785 | } while ((i < src->used) && (j < trg->used)); |
| 1786 | |
| 1787 | if ((i < src->used) || count) { |
| 1788 | /* insert all the hashes first */ |
| 1789 | for (k = i; k < src->used; ++k) { |
| 1790 | set_insert_node_hash(trg, src->val.nodes[k].node, src->val.nodes[k].type); |
| 1791 | } |
| 1792 | |
| 1793 | /* loop ended, but we need to copy something at trg end */ |
| 1794 | count += src->used - i; |
| 1795 | i = src->used; |
| 1796 | goto copy_nodes; |
| 1797 | } |
| 1798 | |
| 1799 | /* we are inserting hashes before the actual node insert, which causes |
| 1800 | * situations when there were initially not enough items for a hash table, |
| 1801 | * but even after some were inserted, hash table was not created (during |
| 1802 | * insertion the number of items is not updated yet) */ |
| 1803 | if (!trg->ht && (trg->used >= LYD_HT_MIN_ITEMS)) { |
| 1804 | set_insert_node_hash(trg, NULL, 0); |
| 1805 | } |
| 1806 | |
| 1807 | #ifndef NDEBUG |
| 1808 | LOGDBG(LY_LDGXPATH, "MERGE result"); |
| 1809 | print_set_debug(trg); |
| 1810 | #endif |
| 1811 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1812 | lyxp_set_free_content(src); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1813 | return LY_SUCCESS; |
| 1814 | } |
| 1815 | |
| 1816 | /* |
| 1817 | * (re)parse functions |
| 1818 | * |
| 1819 | * Parse functions parse the expression into |
| 1820 | * tokens (syntactic analysis). |
| 1821 | * |
| 1822 | * Reparse functions perform semantic analysis |
| 1823 | * (do not save the result, just a check) of |
| 1824 | * the expression and fill repeat indices. |
| 1825 | */ |
| 1826 | |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1827 | LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1828 | 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] | 1829 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1830 | if (exp->used == tok_idx) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1831 | if (ctx) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1832 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF); |
| 1833 | } |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1834 | return LY_EINCOMPLETE; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1835 | } |
| 1836 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1837 | if (want_tok && (exp->tokens[tok_idx] != want_tok)) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1838 | if (ctx) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1839 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1840 | 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] | 1841 | } |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1842 | return LY_ENOT; |
| 1843 | } |
| 1844 | |
| 1845 | return LY_SUCCESS; |
| 1846 | } |
| 1847 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1848 | LY_ERR |
| 1849 | lyxp_next_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok) |
| 1850 | { |
| 1851 | LY_CHECK_RET(lyxp_check_token(ctx, exp, *tok_idx, want_tok)); |
| 1852 | |
| 1853 | /* skip the token */ |
| 1854 | ++(*tok_idx); |
| 1855 | |
| 1856 | return LY_SUCCESS; |
| 1857 | } |
| 1858 | |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1859 | /* just like lyxp_check_token() but tests for 2 tokens */ |
| 1860 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1861 | exp_check_token2(const struct ly_ctx *ctx, 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] | 1862 | enum lyxp_token want_tok2) |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1863 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1864 | if (exp->used == tok_idx) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1865 | if (ctx) { |
| 1866 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF); |
| 1867 | } |
| 1868 | return LY_EINCOMPLETE; |
| 1869 | } |
| 1870 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1871 | 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] | 1872 | if (ctx) { |
| 1873 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1874 | lyxp_print_token(exp->tokens[tok_idx]), &exp->expr[exp->tok_pos[tok_idx]]); |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1875 | } |
| 1876 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1877 | } |
| 1878 | |
| 1879 | return LY_SUCCESS; |
| 1880 | } |
| 1881 | |
| 1882 | /** |
| 1883 | * @brief Stack operation push on the repeat array. |
| 1884 | * |
| 1885 | * @param[in] exp Expression to use. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1886 | * @param[in] tok_idx Position in the expresion \p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1887 | * @param[in] repeat_op_idx Index from \p exp of the operator token. This value is pushed. |
| 1888 | */ |
| 1889 | static void |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1890 | 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] | 1891 | { |
| 1892 | uint16_t i; |
| 1893 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1894 | if (exp->repeat[tok_idx]) { |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 1895 | for (i = 0; exp->repeat[tok_idx][i]; ++i) {} |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1896 | exp->repeat[tok_idx] = realloc(exp->repeat[tok_idx], (i + 2) * sizeof *exp->repeat[tok_idx]); |
| 1897 | LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), ); |
| 1898 | exp->repeat[tok_idx][i] = repeat_op_idx; |
| 1899 | exp->repeat[tok_idx][i + 1] = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1900 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1901 | exp->repeat[tok_idx] = calloc(2, sizeof *exp->repeat[tok_idx]); |
| 1902 | LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), ); |
| 1903 | exp->repeat[tok_idx][0] = repeat_op_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1904 | } |
| 1905 | } |
| 1906 | |
| 1907 | /** |
| 1908 | * @brief Reparse Predicate. Logs directly on error. |
| 1909 | * |
| 1910 | * [7] Predicate ::= '[' Expr ']' |
| 1911 | * |
| 1912 | * @param[in] ctx Context for logging. |
| 1913 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1914 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1915 | * @return LY_ERR |
| 1916 | */ |
| 1917 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1918 | 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] | 1919 | { |
| 1920 | LY_ERR rc; |
| 1921 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1922 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1923 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1924 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1925 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1926 | rc = reparse_or_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1927 | LY_CHECK_RET(rc); |
| 1928 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1929 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1930 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1931 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1932 | |
| 1933 | return LY_SUCCESS; |
| 1934 | } |
| 1935 | |
| 1936 | /** |
| 1937 | * @brief Reparse RelativeLocationPath. Logs directly on error. |
| 1938 | * |
| 1939 | * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step |
| 1940 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 1941 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 1942 | * |
| 1943 | * @param[in] ctx Context for logging. |
| 1944 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1945 | * @param[in] tok_idx Position in the expression \p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1946 | * @return LY_ERR (LY_EINCOMPLETE on forward reference) |
| 1947 | */ |
| 1948 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1949 | 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] | 1950 | { |
| 1951 | LY_ERR rc; |
| 1952 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1953 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1954 | LY_CHECK_RET(rc); |
| 1955 | |
| 1956 | goto step; |
| 1957 | do { |
| 1958 | /* '/' or '//' */ |
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 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1961 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1962 | LY_CHECK_RET(rc); |
| 1963 | step: |
| 1964 | /* Step */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1965 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1966 | case LYXP_TOKEN_DOT: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1967 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1968 | break; |
| 1969 | |
| 1970 | case LYXP_TOKEN_DDOT: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1971 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1972 | break; |
| 1973 | |
| 1974 | case LYXP_TOKEN_AT: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1975 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1976 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1977 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1978 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1979 | if ((exp->tokens[*tok_idx] != LYXP_TOKEN_NAMETEST) && (exp->tokens[*tok_idx] != LYXP_TOKEN_NODETYPE)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1980 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1981 | 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] | 1982 | return LY_EVALID; |
| 1983 | } |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1984 | /* fall through */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1985 | case LYXP_TOKEN_NAMETEST: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1986 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1987 | goto reparse_predicate; |
| 1988 | break; |
| 1989 | |
| 1990 | case LYXP_TOKEN_NODETYPE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1991 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1992 | |
| 1993 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1994 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1995 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1996 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1997 | |
| 1998 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1999 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2000 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2001 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2002 | |
| 2003 | reparse_predicate: |
| 2004 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2005 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 2006 | rc = reparse_predicate(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2007 | LY_CHECK_RET(rc); |
| 2008 | } |
| 2009 | break; |
| 2010 | default: |
| 2011 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2012 | 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] | 2013 | return LY_EVALID; |
| 2014 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2015 | } 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] | 2016 | |
| 2017 | return LY_SUCCESS; |
| 2018 | } |
| 2019 | |
| 2020 | /** |
| 2021 | * @brief Reparse AbsoluteLocationPath. Logs directly on error. |
| 2022 | * |
| 2023 | * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath |
| 2024 | * |
| 2025 | * @param[in] ctx Context for logging. |
| 2026 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2027 | * @param[in] tok_idx Position in the expression \p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2028 | * @return LY_ERR |
| 2029 | */ |
| 2030 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2031 | 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] | 2032 | { |
| 2033 | LY_ERR rc; |
| 2034 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2035 | 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] | 2036 | |
| 2037 | /* '/' RelativeLocationPath? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2038 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2039 | /* '/' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2040 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2041 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2042 | if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2043 | return LY_SUCCESS; |
| 2044 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2045 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2046 | case LYXP_TOKEN_DOT: |
| 2047 | case LYXP_TOKEN_DDOT: |
| 2048 | case LYXP_TOKEN_AT: |
| 2049 | case LYXP_TOKEN_NAMETEST: |
| 2050 | case LYXP_TOKEN_NODETYPE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2051 | rc = reparse_relative_location_path(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2052 | LY_CHECK_RET(rc); |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 2053 | /* fall through */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2054 | default: |
| 2055 | break; |
| 2056 | } |
| 2057 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2058 | } else { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 2059 | /* '//' RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2060 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2061 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2062 | rc = reparse_relative_location_path(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2063 | LY_CHECK_RET(rc); |
| 2064 | } |
| 2065 | |
| 2066 | return LY_SUCCESS; |
| 2067 | } |
| 2068 | |
| 2069 | /** |
| 2070 | * @brief Reparse FunctionCall. Logs directly on error. |
| 2071 | * |
| 2072 | * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')' |
| 2073 | * |
| 2074 | * @param[in] ctx Context for logging. |
| 2075 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2076 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2077 | * @return LY_ERR |
| 2078 | */ |
| 2079 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2080 | 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] | 2081 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2082 | int8_t min_arg_count = -1; |
| 2083 | uint32_t arg_count, max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2084 | uint16_t func_tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2085 | LY_ERR rc; |
| 2086 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2087 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2088 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2089 | func_tok_idx = *tok_idx; |
| 2090 | switch (exp->tok_len[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2091 | case 3: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2092 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2093 | min_arg_count = 1; |
| 2094 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2095 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2096 | min_arg_count = 1; |
| 2097 | max_arg_count = 1; |
| 2098 | } |
| 2099 | break; |
| 2100 | case 4: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2101 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2102 | min_arg_count = 1; |
| 2103 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2104 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2105 | min_arg_count = 0; |
| 2106 | max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2107 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2108 | min_arg_count = 0; |
| 2109 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2110 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2111 | min_arg_count = 0; |
| 2112 | max_arg_count = 0; |
| 2113 | } |
| 2114 | break; |
| 2115 | case 5: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2116 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2117 | min_arg_count = 1; |
| 2118 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2119 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2120 | min_arg_count = 0; |
| 2121 | max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2122 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2123 | min_arg_count = 1; |
| 2124 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2125 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2126 | min_arg_count = 1; |
| 2127 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2128 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2129 | min_arg_count = 1; |
| 2130 | max_arg_count = 1; |
| 2131 | } |
| 2132 | break; |
| 2133 | case 6: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2134 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2135 | min_arg_count = 2; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2136 | max_arg_count = UINT32_MAX; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2137 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2138 | min_arg_count = 0; |
| 2139 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2140 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2141 | min_arg_count = 0; |
| 2142 | max_arg_count = 1; |
| 2143 | } |
| 2144 | break; |
| 2145 | case 7: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2146 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2147 | min_arg_count = 1; |
| 2148 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2149 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2150 | min_arg_count = 1; |
| 2151 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2152 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2153 | min_arg_count = 0; |
| 2154 | max_arg_count = 0; |
| 2155 | } |
| 2156 | break; |
| 2157 | case 8: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2158 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2159 | min_arg_count = 2; |
| 2160 | max_arg_count = 2; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2161 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2162 | min_arg_count = 0; |
| 2163 | max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2164 | } 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] | 2165 | min_arg_count = 2; |
| 2166 | max_arg_count = 2; |
| 2167 | } |
| 2168 | break; |
| 2169 | case 9: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2170 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2171 | min_arg_count = 2; |
| 2172 | max_arg_count = 3; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2173 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2174 | min_arg_count = 3; |
| 2175 | max_arg_count = 3; |
| 2176 | } |
| 2177 | break; |
| 2178 | case 10: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2179 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2180 | min_arg_count = 0; |
| 2181 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2182 | } 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] | 2183 | min_arg_count = 1; |
| 2184 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2185 | } 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] | 2186 | min_arg_count = 2; |
| 2187 | max_arg_count = 2; |
| 2188 | } |
| 2189 | break; |
| 2190 | case 11: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2191 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2192 | min_arg_count = 2; |
| 2193 | max_arg_count = 2; |
| 2194 | } |
| 2195 | break; |
| 2196 | case 12: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2197 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2198 | min_arg_count = 2; |
| 2199 | max_arg_count = 2; |
| 2200 | } |
| 2201 | break; |
| 2202 | case 13: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2203 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2204 | min_arg_count = 0; |
| 2205 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2206 | } 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] | 2207 | min_arg_count = 0; |
| 2208 | max_arg_count = 1; |
| 2209 | } |
| 2210 | break; |
| 2211 | case 15: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2212 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2213 | min_arg_count = 0; |
| 2214 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2215 | } 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] | 2216 | min_arg_count = 2; |
| 2217 | max_arg_count = 2; |
| 2218 | } |
| 2219 | break; |
| 2220 | case 16: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2221 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2222 | min_arg_count = 2; |
| 2223 | max_arg_count = 2; |
| 2224 | } |
| 2225 | break; |
| 2226 | case 20: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2227 | 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] | 2228 | min_arg_count = 2; |
| 2229 | max_arg_count = 2; |
| 2230 | } |
| 2231 | break; |
| 2232 | } |
| 2233 | if (min_arg_count == -1) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2234 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INFUNC, exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2235 | return LY_EINVAL; |
| 2236 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2237 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2238 | |
| 2239 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2240 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2241 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2242 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2243 | |
| 2244 | /* ( Expr ( ',' Expr )* )? */ |
| 2245 | arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2246 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2247 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2248 | if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2249 | ++arg_count; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2250 | rc = reparse_or_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2251 | LY_CHECK_RET(rc); |
| 2252 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2253 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) { |
| 2254 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2255 | |
| 2256 | ++arg_count; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2257 | rc = reparse_or_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2258 | LY_CHECK_RET(rc); |
| 2259 | } |
| 2260 | |
| 2261 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2262 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2263 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2264 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2265 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2266 | if ((arg_count < (uint8_t)min_arg_count) || (arg_count > max_arg_count)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2267 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INARGCOUNT, arg_count, exp->tok_len[func_tok_idx], |
| 2268 | &exp->expr[exp->tok_pos[func_tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2269 | return LY_EVALID; |
| 2270 | } |
| 2271 | |
| 2272 | return LY_SUCCESS; |
| 2273 | } |
| 2274 | |
| 2275 | /** |
| 2276 | * @brief Reparse PathExpr. Logs directly on error. |
| 2277 | * |
| 2278 | * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate* |
| 2279 | * | PrimaryExpr Predicate* '/' RelativeLocationPath |
| 2280 | * | PrimaryExpr Predicate* '//' RelativeLocationPath |
| 2281 | * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath |
| 2282 | * [8] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall |
| 2283 | * |
| 2284 | * @param[in] ctx Context for logging. |
| 2285 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2286 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2287 | * @return LY_ERR |
| 2288 | */ |
| 2289 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2290 | 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] | 2291 | { |
| 2292 | LY_ERR rc; |
| 2293 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2294 | if (lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 2295 | return LY_EVALID; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2296 | } |
| 2297 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2298 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2299 | case LYXP_TOKEN_PAR1: |
| 2300 | /* '(' Expr ')' Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2301 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2302 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2303 | rc = reparse_or_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2304 | LY_CHECK_RET(rc); |
| 2305 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2306 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2307 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2308 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2309 | goto predicate; |
| 2310 | break; |
| 2311 | case LYXP_TOKEN_DOT: |
| 2312 | case LYXP_TOKEN_DDOT: |
| 2313 | case LYXP_TOKEN_AT: |
| 2314 | case LYXP_TOKEN_NAMETEST: |
| 2315 | case LYXP_TOKEN_NODETYPE: |
| 2316 | /* RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2317 | rc = reparse_relative_location_path(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2318 | LY_CHECK_RET(rc); |
| 2319 | break; |
| 2320 | case LYXP_TOKEN_FUNCNAME: |
| 2321 | /* FunctionCall */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2322 | rc = reparse_function_call(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2323 | LY_CHECK_RET(rc); |
| 2324 | goto predicate; |
| 2325 | break; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2326 | case LYXP_TOKEN_OPER_PATH: |
| 2327 | case LYXP_TOKEN_OPER_RPATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2328 | /* AbsoluteLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2329 | rc = reparse_absolute_location_path(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2330 | LY_CHECK_RET(rc); |
| 2331 | break; |
| 2332 | case LYXP_TOKEN_LITERAL: |
| 2333 | /* Literal */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2334 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2335 | goto predicate; |
| 2336 | break; |
| 2337 | case LYXP_TOKEN_NUMBER: |
| 2338 | /* Number */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2339 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2340 | goto predicate; |
| 2341 | break; |
| 2342 | default: |
| 2343 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2344 | 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] | 2345 | return LY_EVALID; |
| 2346 | } |
| 2347 | |
| 2348 | return LY_SUCCESS; |
| 2349 | |
| 2350 | predicate: |
| 2351 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2352 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 2353 | rc = reparse_predicate(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2354 | LY_CHECK_RET(rc); |
| 2355 | } |
| 2356 | |
| 2357 | /* ('/' or '//') RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2358 | 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] | 2359 | |
| 2360 | /* '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2361 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2362 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2363 | rc = reparse_relative_location_path(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2364 | LY_CHECK_RET(rc); |
| 2365 | } |
| 2366 | |
| 2367 | return LY_SUCCESS; |
| 2368 | } |
| 2369 | |
| 2370 | /** |
| 2371 | * @brief Reparse UnaryExpr. Logs directly on error. |
| 2372 | * |
| 2373 | * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr |
| 2374 | * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr |
| 2375 | * |
| 2376 | * @param[in] ctx Context for logging. |
| 2377 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2378 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2379 | * @return LY_ERR |
| 2380 | */ |
| 2381 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2382 | 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] | 2383 | { |
| 2384 | uint16_t prev_exp; |
| 2385 | LY_ERR rc; |
| 2386 | |
| 2387 | /* ('-')* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2388 | prev_exp = *tok_idx; |
| 2389 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) |
| 2390 | && (exp->expr[exp->tok_pos[*tok_idx]] == '-')) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2391 | exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2392 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2393 | } |
| 2394 | |
| 2395 | /* PathExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2396 | prev_exp = *tok_idx; |
| 2397 | rc = reparse_path_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2398 | LY_CHECK_RET(rc); |
| 2399 | |
| 2400 | /* ('|' PathExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2401 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_UNI)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2402 | exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2403 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2404 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2405 | rc = reparse_path_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2406 | LY_CHECK_RET(rc); |
| 2407 | } |
| 2408 | |
| 2409 | return LY_SUCCESS; |
| 2410 | } |
| 2411 | |
| 2412 | /** |
| 2413 | * @brief Reparse AdditiveExpr. Logs directly on error. |
| 2414 | * |
| 2415 | * [15] AdditiveExpr ::= MultiplicativeExpr |
| 2416 | * | AdditiveExpr '+' MultiplicativeExpr |
| 2417 | * | AdditiveExpr '-' MultiplicativeExpr |
| 2418 | * [16] MultiplicativeExpr ::= UnaryExpr |
| 2419 | * | MultiplicativeExpr '*' UnaryExpr |
| 2420 | * | MultiplicativeExpr 'div' UnaryExpr |
| 2421 | * | MultiplicativeExpr 'mod' UnaryExpr |
| 2422 | * |
| 2423 | * @param[in] ctx Context for logging. |
| 2424 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2425 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2426 | * @return LY_ERR |
| 2427 | */ |
| 2428 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2429 | 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] | 2430 | { |
| 2431 | uint16_t prev_add_exp, prev_mul_exp; |
| 2432 | LY_ERR rc; |
| 2433 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2434 | prev_add_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2435 | goto reparse_multiplicative_expr; |
| 2436 | |
| 2437 | /* ('+' / '-' MultiplicativeExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2438 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) |
| 2439 | && ((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] | 2440 | exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2441 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2442 | |
| 2443 | reparse_multiplicative_expr: |
| 2444 | /* UnaryExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2445 | prev_mul_exp = *tok_idx; |
| 2446 | rc = reparse_unary_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2447 | LY_CHECK_RET(rc); |
| 2448 | |
| 2449 | /* ('*' / 'div' / 'mod' UnaryExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2450 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) |
| 2451 | && ((exp->expr[exp->tok_pos[*tok_idx]] == '*') || (exp->tok_len[*tok_idx] == 3))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2452 | exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2453 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2454 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2455 | rc = reparse_unary_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2456 | LY_CHECK_RET(rc); |
| 2457 | } |
| 2458 | } |
| 2459 | |
| 2460 | return LY_SUCCESS; |
| 2461 | } |
| 2462 | |
| 2463 | /** |
| 2464 | * @brief Reparse EqualityExpr. Logs directly on error. |
| 2465 | * |
| 2466 | * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr |
| 2467 | * | EqualityExpr '!=' RelationalExpr |
| 2468 | * [14] RelationalExpr ::= AdditiveExpr |
| 2469 | * | RelationalExpr '<' AdditiveExpr |
| 2470 | * | RelationalExpr '>' AdditiveExpr |
| 2471 | * | RelationalExpr '<=' AdditiveExpr |
| 2472 | * | RelationalExpr '>=' AdditiveExpr |
| 2473 | * |
| 2474 | * @param[in] ctx Context for logging. |
| 2475 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2476 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2477 | * @return LY_ERR |
| 2478 | */ |
| 2479 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2480 | 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] | 2481 | { |
| 2482 | uint16_t prev_eq_exp, prev_rel_exp; |
| 2483 | LY_ERR rc; |
| 2484 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2485 | prev_eq_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2486 | goto reparse_additive_expr; |
| 2487 | |
| 2488 | /* ('=' / '!=' RelationalExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2489 | 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] | 2490 | exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2491 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2492 | |
| 2493 | reparse_additive_expr: |
| 2494 | /* AdditiveExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2495 | prev_rel_exp = *tok_idx; |
| 2496 | rc = reparse_additive_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2497 | LY_CHECK_RET(rc); |
| 2498 | |
| 2499 | /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2500 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_COMP)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2501 | exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2502 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2503 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2504 | rc = reparse_additive_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2505 | LY_CHECK_RET(rc); |
| 2506 | } |
| 2507 | } |
| 2508 | |
| 2509 | return LY_SUCCESS; |
| 2510 | } |
| 2511 | |
| 2512 | /** |
| 2513 | * @brief Reparse OrExpr. Logs directly on error. |
| 2514 | * |
| 2515 | * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr |
| 2516 | * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr |
| 2517 | * |
| 2518 | * @param[in] ctx Context for logging. |
| 2519 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2520 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2521 | * @return LY_ERR |
| 2522 | */ |
| 2523 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2524 | 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] | 2525 | { |
| 2526 | uint16_t prev_or_exp, prev_and_exp; |
| 2527 | LY_ERR rc; |
| 2528 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2529 | prev_or_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2530 | goto reparse_equality_expr; |
| 2531 | |
| 2532 | /* ('or' AndExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2533 | 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] | 2534 | exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2535 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2536 | |
| 2537 | reparse_equality_expr: |
| 2538 | /* EqualityExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2539 | prev_and_exp = *tok_idx; |
| 2540 | rc = reparse_equality_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2541 | LY_CHECK_RET(rc); |
| 2542 | |
| 2543 | /* ('and' EqualityExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2544 | 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] | 2545 | exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2546 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2547 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2548 | rc = reparse_equality_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2549 | LY_CHECK_RET(rc); |
| 2550 | } |
| 2551 | } |
| 2552 | |
| 2553 | return LY_SUCCESS; |
| 2554 | } |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2555 | |
| 2556 | /** |
| 2557 | * @brief Parse NCName. |
| 2558 | * |
| 2559 | * @param[in] ncname Name to parse. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2560 | * @return Length of @p ncname valid bytes. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2561 | */ |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2562 | static long int |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2563 | parse_ncname(const char *ncname) |
| 2564 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2565 | uint32_t uc; |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2566 | size_t size; |
| 2567 | long int len = 0; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2568 | |
| 2569 | LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0); |
| 2570 | if (!is_xmlqnamestartchar(uc) || (uc == ':')) { |
| 2571 | return len; |
| 2572 | } |
| 2573 | |
| 2574 | do { |
| 2575 | len += size; |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 2576 | if (!*ncname) { |
| 2577 | break; |
| 2578 | } |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2579 | LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2580 | } while (is_xmlqnamechar(uc) && (uc != ':')); |
| 2581 | |
| 2582 | return len; |
| 2583 | } |
| 2584 | |
| 2585 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2586 | * @brief Add @p token into the expression @p exp. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2587 | * |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2588 | * @param[in] ctx Context for logging. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2589 | * @param[in] exp Expression to use. |
| 2590 | * @param[in] token Token to add. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2591 | * @param[in] tok_pos Token position in the XPath expression. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2592 | * @param[in] tok_len Token length in the XPath expression. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2593 | * @return LY_ERR |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2594 | */ |
| 2595 | static LY_ERR |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 2596 | 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] | 2597 | { |
| 2598 | uint32_t prev; |
| 2599 | |
| 2600 | if (exp->used == exp->size) { |
| 2601 | prev = exp->size; |
| 2602 | exp->size += LYXP_EXPR_SIZE_STEP; |
| 2603 | if (prev > exp->size) { |
| 2604 | LOGINT(ctx); |
| 2605 | return LY_EINT; |
| 2606 | } |
| 2607 | |
| 2608 | exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens); |
| 2609 | LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM); |
| 2610 | exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos); |
| 2611 | LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM); |
| 2612 | exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len); |
| 2613 | LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM); |
| 2614 | } |
| 2615 | |
| 2616 | exp->tokens[exp->used] = token; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2617 | exp->tok_pos[exp->used] = tok_pos; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2618 | exp->tok_len[exp->used] = tok_len; |
| 2619 | ++exp->used; |
| 2620 | return LY_SUCCESS; |
| 2621 | } |
| 2622 | |
| 2623 | void |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 2624 | lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr) |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2625 | { |
| 2626 | uint16_t i; |
| 2627 | |
| 2628 | if (!expr) { |
| 2629 | return; |
| 2630 | } |
| 2631 | |
| 2632 | lydict_remove(ctx, expr->expr); |
| 2633 | free(expr->tokens); |
| 2634 | free(expr->tok_pos); |
| 2635 | free(expr->tok_len); |
| 2636 | if (expr->repeat) { |
| 2637 | for (i = 0; i < expr->used; ++i) { |
| 2638 | free(expr->repeat[i]); |
| 2639 | } |
| 2640 | } |
| 2641 | free(expr->repeat); |
| 2642 | free(expr); |
| 2643 | } |
| 2644 | |
| 2645 | struct lyxp_expr * |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2646 | lyxp_expr_parse(const struct ly_ctx *ctx, const char *expr, size_t expr_len, uint8_t reparse) |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2647 | { |
| 2648 | struct lyxp_expr *ret; |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2649 | size_t parsed = 0, tok_len; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2650 | enum lyxp_token tok_type; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2651 | uint8_t prev_function_check = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2652 | uint16_t tok_idx = 0; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2653 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2654 | if (!expr[0]) { |
| 2655 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF); |
| 2656 | return NULL; |
| 2657 | } |
| 2658 | |
| 2659 | if (!expr_len) { |
| 2660 | expr_len = strlen(expr); |
| 2661 | } |
| 2662 | if (expr_len > UINT16_MAX) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2663 | LOGERR(ctx, LY_EINVAL, "XPath expression cannot be longer than %ud characters.", UINT16_MAX); |
| 2664 | return NULL; |
| 2665 | } |
| 2666 | |
| 2667 | /* init lyxp_expr structure */ |
| 2668 | ret = calloc(1, sizeof *ret); |
| 2669 | LY_CHECK_ERR_GOTO(!ret, LOGMEM(ctx), error); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2670 | ret->expr = lydict_insert(ctx, expr, expr_len); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2671 | LY_CHECK_ERR_GOTO(!ret->expr, LOGMEM(ctx), error); |
| 2672 | ret->used = 0; |
| 2673 | ret->size = LYXP_EXPR_SIZE_START; |
| 2674 | ret->tokens = malloc(ret->size * sizeof *ret->tokens); |
| 2675 | LY_CHECK_ERR_GOTO(!ret->tokens, LOGMEM(ctx), error); |
| 2676 | |
| 2677 | ret->tok_pos = malloc(ret->size * sizeof *ret->tok_pos); |
| 2678 | LY_CHECK_ERR_GOTO(!ret->tok_pos, LOGMEM(ctx), error); |
| 2679 | |
| 2680 | ret->tok_len = malloc(ret->size * sizeof *ret->tok_len); |
| 2681 | LY_CHECK_ERR_GOTO(!ret->tok_len, LOGMEM(ctx), error); |
| 2682 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2683 | /* make expr 0-terminated */ |
| 2684 | expr = ret->expr; |
| 2685 | |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2686 | while (is_xmlws(expr[parsed])) { |
| 2687 | ++parsed; |
| 2688 | } |
| 2689 | |
| 2690 | do { |
| 2691 | if (expr[parsed] == '(') { |
| 2692 | |
| 2693 | /* '(' */ |
| 2694 | tok_len = 1; |
| 2695 | tok_type = LYXP_TOKEN_PAR1; |
| 2696 | |
| 2697 | if (prev_function_check && ret->used && (ret->tokens[ret->used - 1] == LYXP_TOKEN_NAMETEST)) { |
| 2698 | /* it is a NodeType/FunctionName after all */ |
| 2699 | if (((ret->tok_len[ret->used - 1] == 4) |
| 2700 | && (!strncmp(&expr[ret->tok_pos[ret->used - 1]], "node", 4) |
| 2701 | || !strncmp(&expr[ret->tok_pos[ret->used - 1]], "text", 4))) || |
| 2702 | ((ret->tok_len[ret->used - 1] == 7) |
| 2703 | && !strncmp(&expr[ret->tok_pos[ret->used - 1]], "comment", 7))) { |
| 2704 | ret->tokens[ret->used - 1] = LYXP_TOKEN_NODETYPE; |
| 2705 | } else { |
| 2706 | ret->tokens[ret->used - 1] = LYXP_TOKEN_FUNCNAME; |
| 2707 | } |
| 2708 | prev_function_check = 0; |
| 2709 | } |
| 2710 | |
| 2711 | } else if (expr[parsed] == ')') { |
| 2712 | |
| 2713 | /* ')' */ |
| 2714 | tok_len = 1; |
| 2715 | tok_type = LYXP_TOKEN_PAR2; |
| 2716 | |
| 2717 | } else if (expr[parsed] == '[') { |
| 2718 | |
| 2719 | /* '[' */ |
| 2720 | tok_len = 1; |
| 2721 | tok_type = LYXP_TOKEN_BRACK1; |
| 2722 | |
| 2723 | } else if (expr[parsed] == ']') { |
| 2724 | |
| 2725 | /* ']' */ |
| 2726 | tok_len = 1; |
| 2727 | tok_type = LYXP_TOKEN_BRACK2; |
| 2728 | |
| 2729 | } else if (!strncmp(&expr[parsed], "..", 2)) { |
| 2730 | |
| 2731 | /* '..' */ |
| 2732 | tok_len = 2; |
| 2733 | tok_type = LYXP_TOKEN_DDOT; |
| 2734 | |
| 2735 | } else if ((expr[parsed] == '.') && (!isdigit(expr[parsed + 1]))) { |
| 2736 | |
| 2737 | /* '.' */ |
| 2738 | tok_len = 1; |
| 2739 | tok_type = LYXP_TOKEN_DOT; |
| 2740 | |
| 2741 | } else if (expr[parsed] == '@') { |
| 2742 | |
| 2743 | /* '@' */ |
| 2744 | tok_len = 1; |
| 2745 | tok_type = LYXP_TOKEN_AT; |
| 2746 | |
| 2747 | } else if (expr[parsed] == ',') { |
| 2748 | |
| 2749 | /* ',' */ |
| 2750 | tok_len = 1; |
| 2751 | tok_type = LYXP_TOKEN_COMMA; |
| 2752 | |
| 2753 | } else if (expr[parsed] == '\'') { |
| 2754 | |
| 2755 | /* Literal with ' */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 2756 | for (tok_len = 1; (expr[parsed + tok_len] != '\0') && (expr[parsed + tok_len] != '\''); ++tok_len) {} |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2757 | LY_CHECK_ERR_GOTO(expr[parsed + tok_len] == '\0', |
| 2758 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOE, expr[parsed], &expr[parsed]), error); |
| 2759 | ++tok_len; |
| 2760 | tok_type = LYXP_TOKEN_LITERAL; |
| 2761 | |
| 2762 | } else if (expr[parsed] == '\"') { |
| 2763 | |
| 2764 | /* Literal with " */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 2765 | for (tok_len = 1; (expr[parsed + tok_len] != '\0') && (expr[parsed + tok_len] != '\"'); ++tok_len) {} |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2766 | LY_CHECK_ERR_GOTO(expr[parsed + tok_len] == '\0', |
| 2767 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOE, expr[parsed], &expr[parsed]), error); |
| 2768 | ++tok_len; |
| 2769 | tok_type = LYXP_TOKEN_LITERAL; |
| 2770 | |
| 2771 | } else if ((expr[parsed] == '.') || (isdigit(expr[parsed]))) { |
| 2772 | |
| 2773 | /* Number */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 2774 | for (tok_len = 0; isdigit(expr[parsed + tok_len]); ++tok_len) {} |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2775 | if (expr[parsed + tok_len] == '.') { |
| 2776 | ++tok_len; |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 2777 | for ( ; isdigit(expr[parsed + tok_len]); ++tok_len) {} |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2778 | } |
| 2779 | tok_type = LYXP_TOKEN_NUMBER; |
| 2780 | |
| 2781 | } else if (expr[parsed] == '/') { |
| 2782 | |
| 2783 | /* Operator '/', '//' */ |
| 2784 | if (!strncmp(&expr[parsed], "//", 2)) { |
| 2785 | tok_len = 2; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2786 | tok_type = LYXP_TOKEN_OPER_RPATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2787 | } else { |
| 2788 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2789 | tok_type = LYXP_TOKEN_OPER_PATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2790 | } |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2791 | |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 2792 | } else if (!strncmp(&expr[parsed], "!=", 2)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2793 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2794 | /* Operator '!=' */ |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2795 | tok_len = 2; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2796 | tok_type = LYXP_TOKEN_OPER_NEQUAL; |
| 2797 | |
| 2798 | } else if (!strncmp(&expr[parsed], "<=", 2) || !strncmp(&expr[parsed], ">=", 2)) { |
| 2799 | |
| 2800 | /* Operator '<=', '>=' */ |
| 2801 | tok_len = 2; |
| 2802 | tok_type = LYXP_TOKEN_OPER_COMP; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2803 | |
| 2804 | } else if (expr[parsed] == '|') { |
| 2805 | |
| 2806 | /* Operator '|' */ |
| 2807 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2808 | tok_type = LYXP_TOKEN_OPER_UNI; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2809 | |
| 2810 | } else if ((expr[parsed] == '+') || (expr[parsed] == '-')) { |
| 2811 | |
| 2812 | /* Operator '+', '-' */ |
| 2813 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2814 | tok_type = LYXP_TOKEN_OPER_MATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2815 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2816 | } else if (expr[parsed] == '=') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2817 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2818 | /* Operator '=' */ |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2819 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2820 | tok_type = LYXP_TOKEN_OPER_EQUAL; |
| 2821 | |
| 2822 | } else if ((expr[parsed] == '<') || (expr[parsed] == '>')) { |
| 2823 | |
| 2824 | /* Operator '<', '>' */ |
| 2825 | tok_len = 1; |
| 2826 | tok_type = LYXP_TOKEN_OPER_COMP; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2827 | |
| 2828 | } else if (ret->used && (ret->tokens[ret->used - 1] != LYXP_TOKEN_AT) |
| 2829 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_PAR1) |
| 2830 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_BRACK1) |
| 2831 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_COMMA) |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2832 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_LOG) |
| 2833 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_EQUAL) |
| 2834 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_NEQUAL) |
| 2835 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_COMP) |
| 2836 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_MATH) |
| 2837 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_UNI) |
| 2838 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_PATH) |
| 2839 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_RPATH)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2840 | |
| 2841 | /* Operator '*', 'or', 'and', 'mod', or 'div' */ |
| 2842 | if (expr[parsed] == '*') { |
| 2843 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2844 | tok_type = LYXP_TOKEN_OPER_MATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2845 | |
| 2846 | } else if (!strncmp(&expr[parsed], "or", 2)) { |
| 2847 | tok_len = 2; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2848 | tok_type = LYXP_TOKEN_OPER_LOG; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2849 | |
| 2850 | } else if (!strncmp(&expr[parsed], "and", 3)) { |
| 2851 | tok_len = 3; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2852 | tok_type = LYXP_TOKEN_OPER_LOG; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2853 | |
| 2854 | } else if (!strncmp(&expr[parsed], "mod", 3) || !strncmp(&expr[parsed], "div", 3)) { |
| 2855 | tok_len = 3; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2856 | tok_type = LYXP_TOKEN_OPER_MATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2857 | |
| 2858 | } else if (prev_function_check) { |
Michal Vasko | 5307857 | 2019-05-24 08:50:15 +0200 | [diff] [blame] | 2859 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Invalid character 0x%x ('%c'), perhaps \"%.*s\" is supposed to be a function call.", |
| 2860 | expr[parsed], expr[parsed], ret->tok_len[ret->used - 1], &ret->expr[ret->tok_pos[ret->used - 1]]); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2861 | goto error; |
| 2862 | } else { |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2863 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INEXPR, parsed + 1, expr); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2864 | goto error; |
| 2865 | } |
| 2866 | } else if (expr[parsed] == '*') { |
| 2867 | |
| 2868 | /* NameTest '*' */ |
| 2869 | tok_len = 1; |
| 2870 | tok_type = LYXP_TOKEN_NAMETEST; |
| 2871 | |
| 2872 | } else { |
| 2873 | |
| 2874 | /* NameTest (NCName ':' '*' | QName) or NodeType/FunctionName */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2875 | long int ncname_len = parse_ncname(&expr[parsed]); |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2876 | LY_CHECK_ERR_GOTO(ncname_len < 0, LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INEXPR, parsed - ncname_len + 1, expr), error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2877 | tok_len = ncname_len; |
| 2878 | |
| 2879 | if (expr[parsed + tok_len] == ':') { |
| 2880 | ++tok_len; |
| 2881 | if (expr[parsed + tok_len] == '*') { |
| 2882 | ++tok_len; |
| 2883 | } else { |
| 2884 | ncname_len = parse_ncname(&expr[parsed + tok_len]); |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2885 | LY_CHECK_ERR_GOTO(ncname_len < 0, LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INEXPR, parsed - ncname_len + 1, expr), error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2886 | tok_len += ncname_len; |
| 2887 | } |
| 2888 | /* remove old flag to prevent ambiguities */ |
| 2889 | prev_function_check = 0; |
| 2890 | tok_type = LYXP_TOKEN_NAMETEST; |
| 2891 | } else { |
| 2892 | /* there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */ |
| 2893 | prev_function_check = 1; |
| 2894 | tok_type = LYXP_TOKEN_NAMETEST; |
| 2895 | } |
| 2896 | } |
| 2897 | |
| 2898 | /* store the token, move on to the next one */ |
| 2899 | LY_CHECK_GOTO(exp_add_token(ctx, ret, tok_type, parsed, tok_len), error); |
| 2900 | parsed += tok_len; |
| 2901 | while (is_xmlws(expr[parsed])) { |
| 2902 | ++parsed; |
| 2903 | } |
| 2904 | |
| 2905 | } while (expr[parsed]); |
| 2906 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2907 | if (reparse) { |
| 2908 | /* prealloc repeat */ |
| 2909 | ret->repeat = calloc(ret->size, sizeof *ret->repeat); |
| 2910 | LY_CHECK_ERR_GOTO(!ret->repeat, LOGMEM(ctx), error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2911 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2912 | /* fill repeat */ |
| 2913 | LY_CHECK_GOTO(reparse_or_expr(ctx, ret, &tok_idx), error); |
| 2914 | if (ret->used > tok_idx) { |
| 2915 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Unparsed characters \"%s\" left at the end of an XPath expression.", |
| 2916 | &ret->expr[ret->tok_pos[tok_idx]]); |
| 2917 | goto error; |
| 2918 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2919 | } |
| 2920 | |
| 2921 | print_expr_struct_debug(ret); |
| 2922 | |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2923 | return ret; |
| 2924 | |
| 2925 | error: |
| 2926 | lyxp_expr_free(ctx, ret); |
| 2927 | return NULL; |
| 2928 | } |
| 2929 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2930 | struct lyxp_expr * |
| 2931 | lyxp_expr_dup(const struct ly_ctx *ctx, const struct lyxp_expr *exp) |
| 2932 | { |
| 2933 | struct lyxp_expr *dup; |
| 2934 | uint32_t i, j; |
| 2935 | |
| 2936 | dup = calloc(1, sizeof *dup); |
| 2937 | LY_CHECK_ERR_GOTO(!dup, LOGMEM(ctx), error); |
| 2938 | |
| 2939 | dup->tokens = malloc(exp->used * sizeof *dup->tokens); |
| 2940 | LY_CHECK_ERR_GOTO(!dup->tokens, LOGMEM(ctx), error); |
| 2941 | memcpy(dup->tokens, exp->tokens, exp->used * sizeof *dup->tokens); |
| 2942 | |
| 2943 | dup->tok_pos = malloc(exp->used * sizeof *dup->tok_pos); |
| 2944 | LY_CHECK_ERR_GOTO(!dup->tok_pos, LOGMEM(ctx), error); |
| 2945 | memcpy(dup->tok_pos, exp->tok_pos, exp->used * sizeof *dup->tok_pos); |
| 2946 | |
| 2947 | dup->tok_len = malloc(exp->used * sizeof *dup->tok_len); |
| 2948 | LY_CHECK_ERR_GOTO(!dup->tok_len, LOGMEM(ctx), error); |
| 2949 | memcpy(dup->tok_len, exp->tok_len, exp->used * sizeof *dup->tok_len); |
| 2950 | |
| 2951 | dup->repeat = malloc(exp->used * sizeof *dup->repeat); |
| 2952 | LY_CHECK_ERR_GOTO(!dup->repeat, LOGMEM(ctx), error); |
| 2953 | for (i = 0; i < exp->used; ++i) { |
| 2954 | if (!exp->repeat[i]) { |
| 2955 | dup->repeat[i] = NULL; |
| 2956 | } else { |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 2957 | for (j = 0; exp->repeat[i][j]; ++j) {} |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2958 | /* the ending 0 as well */ |
| 2959 | ++j; |
| 2960 | |
Michal Vasko | 99c7164 | 2020-07-03 13:33:36 +0200 | [diff] [blame] | 2961 | dup->repeat[i] = malloc(j * sizeof **dup->repeat); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2962 | LY_CHECK_ERR_GOTO(!dup->repeat[i], LOGMEM(ctx), error); |
| 2963 | memcpy(dup->repeat[i], exp->repeat[i], j * sizeof **dup->repeat); |
| 2964 | dup->repeat[i][j - 1] = 0; |
| 2965 | } |
| 2966 | } |
| 2967 | |
| 2968 | dup->used = exp->used; |
| 2969 | dup->size = exp->used; |
| 2970 | dup->expr = lydict_insert(ctx, exp->expr, 0); |
| 2971 | |
| 2972 | return dup; |
| 2973 | |
| 2974 | error: |
| 2975 | lyxp_expr_free(ctx, dup); |
| 2976 | return NULL; |
| 2977 | } |
| 2978 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2979 | /* |
| 2980 | * warn functions |
| 2981 | * |
| 2982 | * Warn functions check specific reasonable conditions for schema XPath |
| 2983 | * and print a warning if they are not satisfied. |
| 2984 | */ |
| 2985 | |
| 2986 | /** |
| 2987 | * @brief Get the last-added schema node that is currently in the context. |
| 2988 | * |
| 2989 | * @param[in] set Set to search in. |
| 2990 | * @return Last-added schema context node, NULL if no node is in context. |
| 2991 | */ |
| 2992 | static struct lysc_node * |
| 2993 | warn_get_scnode_in_ctx(struct lyxp_set *set) |
| 2994 | { |
| 2995 | uint32_t i; |
| 2996 | |
| 2997 | if (!set || (set->type != LYXP_SET_SCNODE_SET)) { |
| 2998 | return NULL; |
| 2999 | } |
| 3000 | |
| 3001 | i = set->used; |
| 3002 | do { |
| 3003 | --i; |
| 3004 | if (set->val.scnodes[i].in_ctx == 1) { |
| 3005 | /* if there are more, simply return the first found (last added) */ |
| 3006 | return set->val.scnodes[i].scnode; |
| 3007 | } |
| 3008 | } while (i); |
| 3009 | |
| 3010 | return NULL; |
| 3011 | } |
| 3012 | |
| 3013 | /** |
| 3014 | * @brief Test whether a type is numeric - integer type or decimal64. |
| 3015 | * |
| 3016 | * @param[in] type Type to test. |
| 3017 | * @return 1 if numeric, 0 otherwise. |
| 3018 | */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3019 | static uint8_t |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3020 | warn_is_numeric_type(struct lysc_type *type) |
| 3021 | { |
| 3022 | struct lysc_type_union *uni; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3023 | uint8_t ret; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3024 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3025 | |
| 3026 | switch (type->basetype) { |
| 3027 | case LY_TYPE_DEC64: |
| 3028 | case LY_TYPE_INT8: |
| 3029 | case LY_TYPE_UINT8: |
| 3030 | case LY_TYPE_INT16: |
| 3031 | case LY_TYPE_UINT16: |
| 3032 | case LY_TYPE_INT32: |
| 3033 | case LY_TYPE_UINT32: |
| 3034 | case LY_TYPE_INT64: |
| 3035 | case LY_TYPE_UINT64: |
| 3036 | return 1; |
| 3037 | case LY_TYPE_UNION: |
| 3038 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3039 | LY_ARRAY_FOR(uni->types, u) { |
| 3040 | ret = warn_is_numeric_type(uni->types[u]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3041 | if (ret) { |
| 3042 | /* found a suitable type */ |
| 3043 | return 1; |
| 3044 | } |
| 3045 | } |
| 3046 | /* did not find any suitable type */ |
| 3047 | return 0; |
| 3048 | case LY_TYPE_LEAFREF: |
| 3049 | return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype); |
| 3050 | default: |
| 3051 | return 0; |
| 3052 | } |
| 3053 | } |
| 3054 | |
| 3055 | /** |
| 3056 | * @brief Test whether a type is string-like - no integers, decimal64 or binary. |
| 3057 | * |
| 3058 | * @param[in] type Type to test. |
| 3059 | * @return 1 if string, 0 otherwise. |
| 3060 | */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3061 | static uint8_t |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3062 | warn_is_string_type(struct lysc_type *type) |
| 3063 | { |
| 3064 | struct lysc_type_union *uni; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3065 | uint8_t 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_BITS: |
| 3070 | case LY_TYPE_ENUM: |
| 3071 | case LY_TYPE_IDENT: |
| 3072 | case LY_TYPE_INST: |
| 3073 | case LY_TYPE_STRING: |
| 3074 | return 1; |
| 3075 | case LY_TYPE_UNION: |
| 3076 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3077 | LY_ARRAY_FOR(uni->types, u) { |
| 3078 | ret = warn_is_string_type(uni->types[u]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3079 | if (ret) { |
| 3080 | /* found a suitable type */ |
| 3081 | return 1; |
| 3082 | } |
| 3083 | } |
| 3084 | /* did not find any suitable type */ |
| 3085 | return 0; |
| 3086 | case LY_TYPE_LEAFREF: |
| 3087 | return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype); |
| 3088 | default: |
| 3089 | return 0; |
| 3090 | } |
| 3091 | } |
| 3092 | |
| 3093 | /** |
| 3094 | * @brief Test whether a type is one specific type. |
| 3095 | * |
| 3096 | * @param[in] type Type to test. |
| 3097 | * @param[in] base Expected type. |
| 3098 | * @return 1 if it is, 0 otherwise. |
| 3099 | */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3100 | static uint8_t |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3101 | warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base) |
| 3102 | { |
| 3103 | struct lysc_type_union *uni; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3104 | uint8_t ret; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3105 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3106 | |
| 3107 | if (type->basetype == base) { |
| 3108 | return 1; |
| 3109 | } else if (type->basetype == LY_TYPE_UNION) { |
| 3110 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3111 | LY_ARRAY_FOR(uni->types, u) { |
| 3112 | ret = warn_is_specific_type(uni->types[u], base); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3113 | if (ret) { |
| 3114 | /* found a suitable type */ |
| 3115 | return 1; |
| 3116 | } |
| 3117 | } |
| 3118 | /* did not find any suitable type */ |
| 3119 | return 0; |
| 3120 | } else if (type->basetype == LY_TYPE_LEAFREF) { |
| 3121 | return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base); |
| 3122 | } |
| 3123 | |
| 3124 | return 0; |
| 3125 | } |
| 3126 | |
| 3127 | /** |
| 3128 | * @brief Get next type of a (union) type. |
| 3129 | * |
| 3130 | * @param[in] type Base type. |
| 3131 | * @param[in] prev_type Previously returned type. |
| 3132 | * @return Next type or NULL. |
| 3133 | */ |
| 3134 | static struct lysc_type * |
| 3135 | warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type) |
| 3136 | { |
| 3137 | struct lysc_type_union *uni; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3138 | uint8_t found = 0; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3139 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3140 | |
| 3141 | switch (type->basetype) { |
| 3142 | case LY_TYPE_UNION: |
| 3143 | uni = (struct lysc_type_union *)type; |
| 3144 | if (!prev_type) { |
| 3145 | return uni->types[0]; |
| 3146 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3147 | LY_ARRAY_FOR(uni->types, u) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3148 | if (found) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3149 | return uni->types[u]; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3150 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3151 | if (prev_type == uni->types[u]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3152 | found = 1; |
| 3153 | } |
| 3154 | } |
| 3155 | return NULL; |
| 3156 | default: |
| 3157 | if (prev_type) { |
| 3158 | assert(type == prev_type); |
| 3159 | return NULL; |
| 3160 | } else { |
| 3161 | return type; |
| 3162 | } |
| 3163 | } |
| 3164 | } |
| 3165 | |
| 3166 | /** |
| 3167 | * @brief Test whether 2 types have a common type. |
| 3168 | * |
| 3169 | * @param[in] type1 First type. |
| 3170 | * @param[in] type2 Second type. |
| 3171 | * @return 1 if they do, 0 otherwise. |
| 3172 | */ |
| 3173 | static int |
| 3174 | warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2) |
| 3175 | { |
| 3176 | struct lysc_type *t1, *rt1, *t2, *rt2; |
| 3177 | |
| 3178 | t1 = NULL; |
| 3179 | while ((t1 = warn_is_equal_type_next_type(type1, t1))) { |
| 3180 | if (t1->basetype == LY_TYPE_LEAFREF) { |
| 3181 | rt1 = ((struct lysc_type_leafref *)t1)->realtype; |
| 3182 | } else { |
| 3183 | rt1 = t1; |
| 3184 | } |
| 3185 | |
| 3186 | t2 = NULL; |
| 3187 | while ((t2 = warn_is_equal_type_next_type(type2, t2))) { |
| 3188 | if (t2->basetype == LY_TYPE_LEAFREF) { |
| 3189 | rt2 = ((struct lysc_type_leafref *)t2)->realtype; |
| 3190 | } else { |
| 3191 | rt2 = t2; |
| 3192 | } |
| 3193 | |
| 3194 | if (rt2->basetype == rt1->basetype) { |
| 3195 | /* match found */ |
| 3196 | return 1; |
| 3197 | } |
| 3198 | } |
| 3199 | } |
| 3200 | |
| 3201 | return 0; |
| 3202 | } |
| 3203 | |
| 3204 | /** |
| 3205 | * @brief Check both operands of comparison operators. |
| 3206 | * |
| 3207 | * @param[in] ctx Context for errors. |
| 3208 | * @param[in] set1 First operand set. |
| 3209 | * @param[in] set2 Second operand set. |
| 3210 | * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!='). |
| 3211 | * @param[in] expr Start of the expression to print with the warning. |
| 3212 | * @param[in] tok_pos Token position. |
| 3213 | */ |
| 3214 | static void |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3215 | warn_operands(struct ly_ctx *ctx, struct lyxp_set *set1, struct lyxp_set *set2, uint8_t numbers_only, const char *expr, uint16_t tok_pos) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3216 | { |
| 3217 | struct lysc_node_leaf *node1, *node2; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3218 | uint8_t leaves = 1, warning = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3219 | |
| 3220 | node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1); |
| 3221 | node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2); |
| 3222 | |
| 3223 | if (!node1 && !node2) { |
| 3224 | /* no node-sets involved, nothing to do */ |
| 3225 | return; |
| 3226 | } |
| 3227 | |
| 3228 | if (node1) { |
| 3229 | if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3230 | LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name); |
| 3231 | warning = 1; |
| 3232 | leaves = 0; |
| 3233 | } else if (numbers_only && !warn_is_numeric_type(node1->type)) { |
| 3234 | LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name); |
| 3235 | warning = 1; |
| 3236 | } |
| 3237 | } |
| 3238 | |
| 3239 | if (node2) { |
| 3240 | if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3241 | LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name); |
| 3242 | warning = 1; |
| 3243 | leaves = 0; |
| 3244 | } else if (numbers_only && !warn_is_numeric_type(node2->type)) { |
| 3245 | LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name); |
| 3246 | warning = 1; |
| 3247 | } |
| 3248 | } |
| 3249 | |
| 3250 | if (node1 && node2 && leaves && !numbers_only) { |
| 3251 | if ((warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type)) |
| 3252 | || (!warn_is_numeric_type(node1->type) && warn_is_numeric_type(node2->type)) |
| 3253 | || (!warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type) |
| 3254 | && !warn_is_equal_type(node1->type, node2->type))) { |
| 3255 | LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name); |
| 3256 | warning = 1; |
| 3257 | } |
| 3258 | } |
| 3259 | |
| 3260 | if (warning) { |
| 3261 | LOGWRN(ctx, "Previous warning generated by XPath subexpression[%u] \"%.20s\".", tok_pos, expr + tok_pos); |
| 3262 | } |
| 3263 | } |
| 3264 | |
| 3265 | /** |
| 3266 | * @brief Check that a value is valid for a leaf. If not applicable, does nothing. |
| 3267 | * |
| 3268 | * @param[in] exp Parsed XPath expression. |
| 3269 | * @param[in] set Set with the leaf/leaf-list. |
| 3270 | * @param[in] val_exp Index of the value (literal/number) in @p exp. |
| 3271 | * @param[in] equal_exp Index of the start of the equality expression in @p exp. |
| 3272 | * @param[in] last_equal_exp Index of the end of the equality expression in @p exp. |
| 3273 | */ |
| 3274 | static void |
| 3275 | warn_equality_value(struct lyxp_expr *exp, struct lyxp_set *set, uint16_t val_exp, uint16_t equal_exp, uint16_t last_equal_exp) |
| 3276 | { |
| 3277 | struct lysc_node *scnode; |
| 3278 | struct lysc_type *type; |
| 3279 | char *value; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3280 | struct lyd_value storage; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3281 | LY_ERR rc; |
| 3282 | struct ly_err_item *err = NULL; |
| 3283 | |
| 3284 | if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST)) |
| 3285 | && ((exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) || (exp->tokens[val_exp] == LYXP_TOKEN_NUMBER))) { |
| 3286 | /* check that the node can have the specified value */ |
| 3287 | if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) { |
| 3288 | value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2); |
| 3289 | } else { |
| 3290 | value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]); |
| 3291 | } |
| 3292 | if (!value) { |
| 3293 | LOGMEM(set->ctx); |
| 3294 | return; |
| 3295 | } |
| 3296 | |
| 3297 | if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) { |
| 3298 | LOGWRN(set->ctx, "Identityref \"%s\" comparison with identity \"%s\" without prefix, consider adding" |
| 3299 | " a prefix or best using \"derived-from(-or-self)()\" functions.", scnode->name, value); |
| 3300 | 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] | 3301 | (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp], |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3302 | exp->expr + exp->tok_pos[equal_exp]); |
| 3303 | } |
| 3304 | |
| 3305 | type = ((struct lysc_node_leaf *)scnode)->type; |
| 3306 | if (type->basetype != LY_TYPE_IDENT) { |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 3307 | rc = type->plugin->store(set->ctx, type, value, strlen(value), LY_TYPE_OPTS_SCHEMA, LY_PREF_SCHEMA, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 3308 | (void *)set->local_mod, NULL, NULL, &storage, &err); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3309 | |
| 3310 | if (err) { |
| 3311 | LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg); |
| 3312 | ly_err_free(err); |
| 3313 | } else if (rc != LY_SUCCESS) { |
| 3314 | LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value); |
| 3315 | } |
| 3316 | if (rc != LY_SUCCESS) { |
| 3317 | 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] | 3318 | (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp], |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3319 | exp->expr + exp->tok_pos[equal_exp]); |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3320 | } else { |
| 3321 | type->plugin->free(set->ctx, &storage); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3322 | } |
| 3323 | } |
| 3324 | free(value); |
| 3325 | } |
| 3326 | } |
| 3327 | |
| 3328 | /* |
| 3329 | * XPath functions |
| 3330 | */ |
| 3331 | |
| 3332 | /** |
| 3333 | * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN |
| 3334 | * depending on whether the first node bit value from the second argument is set. |
| 3335 | * |
| 3336 | * @param[in] args Array of arguments. |
| 3337 | * @param[in] arg_count Count of elements in @p args. |
| 3338 | * @param[in,out] set Context and result set at the same time. |
| 3339 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3340 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3341 | */ |
| 3342 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3343 | 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] | 3344 | { |
| 3345 | struct lyd_node_term *leaf; |
| 3346 | struct lysc_node_leaf *sleaf; |
| 3347 | struct lysc_type_bits *bits; |
| 3348 | LY_ERR rc = LY_SUCCESS; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3349 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3350 | |
| 3351 | if (options & LYXP_SCNODE_ALL) { |
| 3352 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3353 | 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] | 3354 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3355 | 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] | 3356 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) { |
| 3357 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"bits\".", __func__, sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3358 | } |
| 3359 | |
| 3360 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 3361 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3362 | 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] | 3363 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3364 | 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] | 3365 | } |
| 3366 | } |
| 3367 | set_scnode_clear_ctx(set); |
| 3368 | return rc; |
| 3369 | } |
| 3370 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3371 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3372 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "bit-is-set(node-set, string)"); |
| 3373 | return LY_EVALID; |
| 3374 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3375 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3376 | LY_CHECK_RET(rc); |
| 3377 | |
| 3378 | set_fill_boolean(set, 0); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3379 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3380 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
| 3381 | if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) |
| 3382 | && (((struct lysc_node_leaf *)leaf->schema)->type->basetype == LY_TYPE_BITS)) { |
| 3383 | bits = (struct lysc_type_bits *)((struct lysc_node_leaf *)leaf->schema)->type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3384 | LY_ARRAY_FOR(bits->bits, u) { |
| 3385 | if (!strcmp(bits->bits[u].name, args[1]->val.str)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3386 | set_fill_boolean(set, 1); |
| 3387 | break; |
| 3388 | } |
| 3389 | } |
| 3390 | } |
| 3391 | } |
| 3392 | |
| 3393 | return LY_SUCCESS; |
| 3394 | } |
| 3395 | |
| 3396 | /** |
| 3397 | * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN |
| 3398 | * with the argument converted to boolean. |
| 3399 | * |
| 3400 | * @param[in] args Array of arguments. |
| 3401 | * @param[in] arg_count Count of elements in @p args. |
| 3402 | * @param[in,out] set Context and result set at the same time. |
| 3403 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3404 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3405 | */ |
| 3406 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3407 | 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] | 3408 | { |
| 3409 | LY_ERR rc; |
| 3410 | |
| 3411 | if (options & LYXP_SCNODE_ALL) { |
| 3412 | set_scnode_clear_ctx(set); |
| 3413 | return LY_SUCCESS; |
| 3414 | } |
| 3415 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3416 | rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3417 | LY_CHECK_RET(rc); |
| 3418 | set_fill_set(set, args[0]); |
| 3419 | |
| 3420 | return LY_SUCCESS; |
| 3421 | } |
| 3422 | |
| 3423 | /** |
| 3424 | * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER |
| 3425 | * with the first argument rounded up to the nearest integer. |
| 3426 | * |
| 3427 | * @param[in] args Array of arguments. |
| 3428 | * @param[in] arg_count Count of elements in @p args. |
| 3429 | * @param[in,out] set Context and result set at the same time. |
| 3430 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3431 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3432 | */ |
| 3433 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3434 | 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] | 3435 | { |
| 3436 | struct lysc_node_leaf *sleaf; |
| 3437 | LY_ERR rc = LY_SUCCESS; |
| 3438 | |
| 3439 | if (options & LYXP_SCNODE_ALL) { |
| 3440 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3441 | 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] | 3442 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3443 | 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] | 3444 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) { |
| 3445 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3446 | } |
| 3447 | set_scnode_clear_ctx(set); |
| 3448 | return rc; |
| 3449 | } |
| 3450 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3451 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3452 | LY_CHECK_RET(rc); |
| 3453 | if ((long long)args[0]->val.num != args[0]->val.num) { |
| 3454 | set_fill_number(set, ((long long)args[0]->val.num) + 1); |
| 3455 | } else { |
| 3456 | set_fill_number(set, args[0]->val.num); |
| 3457 | } |
| 3458 | |
| 3459 | return LY_SUCCESS; |
| 3460 | } |
| 3461 | |
| 3462 | /** |
| 3463 | * @brief Execute the XPath concat(string, string, string*) function. |
| 3464 | * Returns LYXP_SET_STRING with the concatenation of all the arguments. |
| 3465 | * |
| 3466 | * @param[in] args Array of arguments. |
| 3467 | * @param[in] arg_count Count of elements in @p args. |
| 3468 | * @param[in,out] set Context and result set at the same time. |
| 3469 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3470 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3471 | */ |
| 3472 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3473 | 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] | 3474 | { |
| 3475 | uint16_t i; |
| 3476 | char *str = NULL; |
| 3477 | size_t used = 1; |
| 3478 | LY_ERR rc = LY_SUCCESS; |
| 3479 | struct lysc_node_leaf *sleaf; |
| 3480 | |
| 3481 | if (options & LYXP_SCNODE_ALL) { |
| 3482 | for (i = 0; i < arg_count; ++i) { |
| 3483 | if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) { |
| 3484 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3485 | LOGWRN(set->ctx, "Argument #%u of %s is a %s node \"%s\".", |
| 3486 | i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3487 | } else if (!warn_is_string_type(sleaf->type)) { |
Radek Krejci | 70124c8 | 2020-08-14 22:17:03 +0200 | [diff] [blame] | 3488 | 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] | 3489 | } |
| 3490 | } |
| 3491 | } |
| 3492 | set_scnode_clear_ctx(set); |
| 3493 | return rc; |
| 3494 | } |
| 3495 | |
| 3496 | for (i = 0; i < arg_count; ++i) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3497 | rc = lyxp_set_cast(args[i], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3498 | if (rc != LY_SUCCESS) { |
| 3499 | free(str); |
| 3500 | return rc; |
| 3501 | } |
| 3502 | |
| 3503 | str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char)); |
| 3504 | LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM); |
| 3505 | strcpy(str + used - 1, args[i]->val.str); |
| 3506 | used += strlen(args[i]->val.str); |
| 3507 | } |
| 3508 | |
| 3509 | /* free, kind of */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3510 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3511 | set->type = LYXP_SET_STRING; |
| 3512 | set->val.str = str; |
| 3513 | |
| 3514 | return LY_SUCCESS; |
| 3515 | } |
| 3516 | |
| 3517 | /** |
| 3518 | * @brief Execute the XPath contains(string, string) function. |
| 3519 | * Returns LYXP_SET_BOOLEAN whether the second argument can |
| 3520 | * be found in the first or not. |
| 3521 | * |
| 3522 | * @param[in] args Array of arguments. |
| 3523 | * @param[in] arg_count Count of elements in @p args. |
| 3524 | * @param[in,out] set Context and result set at the same time. |
| 3525 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3526 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3527 | */ |
| 3528 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3529 | 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] | 3530 | { |
| 3531 | struct lysc_node_leaf *sleaf; |
| 3532 | LY_ERR rc = LY_SUCCESS; |
| 3533 | |
| 3534 | if (options & LYXP_SCNODE_ALL) { |
| 3535 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3536 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3537 | 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] | 3538 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3539 | 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] | 3540 | } |
| 3541 | } |
| 3542 | |
| 3543 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 3544 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3545 | 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] | 3546 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3547 | 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] | 3548 | } |
| 3549 | } |
| 3550 | set_scnode_clear_ctx(set); |
| 3551 | return rc; |
| 3552 | } |
| 3553 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3554 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3555 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3556 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3557 | LY_CHECK_RET(rc); |
| 3558 | |
| 3559 | if (strstr(args[0]->val.str, args[1]->val.str)) { |
| 3560 | set_fill_boolean(set, 1); |
| 3561 | } else { |
| 3562 | set_fill_boolean(set, 0); |
| 3563 | } |
| 3564 | |
| 3565 | return LY_SUCCESS; |
| 3566 | } |
| 3567 | |
| 3568 | /** |
| 3569 | * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER |
| 3570 | * with the size of the node-set from the argument. |
| 3571 | * |
| 3572 | * @param[in] args Array of arguments. |
| 3573 | * @param[in] arg_count Count of elements in @p args. |
| 3574 | * @param[in,out] set Context and result set at the same time. |
| 3575 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3576 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3577 | */ |
| 3578 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3579 | 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] | 3580 | { |
| 3581 | struct lysc_node *scnode = NULL; |
| 3582 | LY_ERR rc = LY_SUCCESS; |
| 3583 | |
| 3584 | if (options & LYXP_SCNODE_ALL) { |
| 3585 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(scnode = warn_get_scnode_in_ctx(args[0]))) { |
| 3586 | 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] | 3587 | } |
| 3588 | set_scnode_clear_ctx(set); |
| 3589 | return rc; |
| 3590 | } |
| 3591 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3592 | if (args[0]->type != LYXP_SET_NODE_SET) { |
| 3593 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "count(node-set)"); |
| 3594 | return LY_EVALID; |
| 3595 | } |
| 3596 | |
| 3597 | set_fill_number(set, args[0]->used); |
| 3598 | return LY_SUCCESS; |
| 3599 | } |
| 3600 | |
| 3601 | /** |
| 3602 | * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET |
| 3603 | * with the context with the intial node. |
| 3604 | * |
| 3605 | * @param[in] args Array of arguments. |
| 3606 | * @param[in] arg_count Count of elements in @p args. |
| 3607 | * @param[in,out] set Context and result set at the same time. |
| 3608 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3609 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3610 | */ |
| 3611 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3612 | 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] | 3613 | { |
| 3614 | if (arg_count || args) { |
| 3615 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGCOUNT, arg_count, "current()"); |
| 3616 | return LY_EVALID; |
| 3617 | } |
| 3618 | |
| 3619 | if (options & LYXP_SCNODE_ALL) { |
| 3620 | set_scnode_clear_ctx(set); |
| 3621 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 3622 | lyxp_set_scnode_insert_node(set, set->ctx_scnode, LYXP_NODE_ELEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3623 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3624 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3625 | |
| 3626 | /* position is filled later */ |
| 3627 | set_insert_node(set, set->ctx_node, 0, LYXP_NODE_ELEM, 0); |
| 3628 | } |
| 3629 | |
| 3630 | return LY_SUCCESS; |
| 3631 | } |
| 3632 | |
| 3633 | /** |
| 3634 | * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either |
| 3635 | * leafref or instance-identifier target node(s). |
| 3636 | * |
| 3637 | * @param[in] args Array of arguments. |
| 3638 | * @param[in] arg_count Count of elements in @p args. |
| 3639 | * @param[in,out] set Context and result set at the same time. |
| 3640 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3641 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3642 | */ |
| 3643 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3644 | 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] | 3645 | { |
| 3646 | struct lyd_node_term *leaf; |
Michal Vasko | 42e497c | 2020-01-06 08:38:25 +0100 | [diff] [blame] | 3647 | struct lysc_node_leaf *sleaf = NULL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3648 | struct lysc_type_leafref *lref; |
Michal Vasko | ae9e4cb | 2019-09-25 08:43:05 +0200 | [diff] [blame] | 3649 | const struct lysc_node *target; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3650 | struct ly_path *p; |
| 3651 | struct lyd_node *node; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3652 | char *errmsg = NULL; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 3653 | uint8_t oper; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3654 | LY_ERR rc = LY_SUCCESS; |
| 3655 | |
| 3656 | if (options & LYXP_SCNODE_ALL) { |
| 3657 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3658 | 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] | 3659 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3660 | 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] | 3661 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) && !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) { |
| 3662 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"leafref\" nor \"instance-identifier\".", |
| 3663 | __func__, sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3664 | } |
| 3665 | set_scnode_clear_ctx(set); |
Michal Vasko | 42e497c | 2020-01-06 08:38:25 +0100 | [diff] [blame] | 3666 | if (sleaf && (sleaf->type->basetype == LY_TYPE_LEAFREF)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3667 | lref = (struct lysc_type_leafref *)sleaf->type; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 3668 | oper = lysc_is_output((struct lysc_node *)sleaf) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3669 | |
| 3670 | /* it was already evaluated on schema, it must succeed */ |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 3671 | rc = ly_path_compile(set->ctx, sleaf->module, (struct lysc_node *)sleaf, lref->path, LY_PATH_LREF_TRUE, |
| 3672 | oper, LY_PATH_TARGET_MANY, set->format, lref->path_context, &p); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3673 | assert(!rc); |
| 3674 | |
| 3675 | /* get the target node */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3676 | target = p[LY_ARRAY_COUNT(p) - 1].node; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3677 | ly_path_free(set->ctx, p); |
| 3678 | |
| 3679 | lyxp_set_scnode_insert_node(set, target, LYXP_NODE_ELEM); |
Michal Vasko | ae9e4cb | 2019-09-25 08:43:05 +0200 | [diff] [blame] | 3680 | } |
| 3681 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3682 | return rc; |
| 3683 | } |
| 3684 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3685 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3686 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "deref(node-set)"); |
| 3687 | return LY_EVALID; |
| 3688 | } |
| 3689 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3690 | lyxp_set_free_content(set); |
| 3691 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3692 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
| 3693 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 3694 | if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 3695 | if (sleaf->type->basetype == LY_TYPE_LEAFREF) { |
| 3696 | /* find leafref target */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3697 | if (ly_type_find_leafref((struct lysc_type_leafref *)sleaf->type, (struct lyd_node *)leaf, |
| 3698 | &leaf->value, set->tree, &node, &errmsg)) { |
| 3699 | LOGERR(set->ctx, LY_EVALID, errmsg); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3700 | free(errmsg); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3701 | return LY_EVALID; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3702 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3703 | } else { |
| 3704 | assert(sleaf->type->basetype == LY_TYPE_INST); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3705 | if (ly_path_eval(leaf->value.target, set->tree, &node)) { |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3706 | LOGERR(set->ctx, LY_EVALID, "Invalid instance-identifier \"%s\" value - required instance not found.", |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 3707 | LYD_CANON_VALUE(leaf)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3708 | return LY_EVALID; |
| 3709 | } |
| 3710 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3711 | |
| 3712 | /* insert it */ |
| 3713 | set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3714 | } |
| 3715 | } |
| 3716 | |
| 3717 | return LY_SUCCESS; |
| 3718 | } |
| 3719 | |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3720 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3721 | xpath_derived_(struct lyxp_set **args, struct lyxp_set *set, uint32_t options, uint8_t self_match, const char *func) |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3722 | { |
| 3723 | uint16_t i; |
| 3724 | LY_ARRAY_COUNT_TYPE u; |
| 3725 | struct lyd_node_term *leaf; |
| 3726 | struct lysc_node_leaf *sleaf; |
| 3727 | struct lyd_meta *meta; |
| 3728 | struct lyd_value data = {0}, *val; |
| 3729 | struct ly_err_item *err = NULL; |
| 3730 | LY_ERR rc = LY_SUCCESS; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3731 | uint8_t found; |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3732 | |
| 3733 | if (options & LYXP_SCNODE_ALL) { |
| 3734 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3735 | LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", func); |
| 3736 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3737 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype), |
| 3738 | sleaf->name); |
| 3739 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) { |
| 3740 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", func, sleaf->name); |
| 3741 | } |
| 3742 | |
| 3743 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 3744 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3745 | LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype), |
| 3746 | sleaf->name); |
| 3747 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3748 | LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", func, sleaf->name); |
| 3749 | } |
| 3750 | } |
| 3751 | set_scnode_clear_ctx(set); |
| 3752 | return rc; |
| 3753 | } |
| 3754 | |
| 3755 | if (args[0]->type != LYXP_SET_NODE_SET) { |
| 3756 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), |
| 3757 | "derived-from(-or-self)(node-set, string)"); |
| 3758 | return LY_EVALID; |
| 3759 | } |
| 3760 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
| 3761 | LY_CHECK_RET(rc); |
| 3762 | |
| 3763 | set_fill_boolean(set, 0); |
| 3764 | found = 0; |
| 3765 | for (i = 0; i < args[0]->used; ++i) { |
| 3766 | if ((args[0]->val.nodes[i].type != LYXP_NODE_ELEM) && (args[0]->val.nodes[i].type != LYXP_NODE_META)) { |
| 3767 | continue; |
| 3768 | } |
| 3769 | |
| 3770 | if (args[0]->val.nodes[i].type == LYXP_NODE_ELEM) { |
| 3771 | leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node; |
| 3772 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 3773 | val = &leaf->value; |
| 3774 | if (!(sleaf->nodetype & LYD_NODE_TERM) || (leaf->value.realtype->basetype != LY_TYPE_IDENT)) { |
| 3775 | /* uninteresting */ |
| 3776 | continue; |
| 3777 | } |
| 3778 | |
| 3779 | /* store args[1] as ident */ |
| 3780 | rc = val->realtype->plugin->store(set->ctx, val->realtype, args[1]->val.str, strlen(args[1]->val.str), |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3781 | 0, set->format, (void *)set->local_mod, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 3782 | (struct lyd_node *)leaf, set->tree, &data, &err); |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3783 | } else { |
| 3784 | meta = args[0]->val.meta[i].meta; |
| 3785 | val = &meta->value; |
| 3786 | if (val->realtype->basetype != LY_TYPE_IDENT) { |
| 3787 | /* uninteresting */ |
| 3788 | continue; |
| 3789 | } |
| 3790 | |
| 3791 | /* store args[1] as ident */ |
| 3792 | rc = val->realtype->plugin->store(set->ctx, val->realtype, args[1]->val.str, strlen(args[1]->val.str), |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3793 | 0, set->format, (void *)meta->annotation->module, |
| 3794 | meta->parent, set->tree, &data, &err); |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3795 | } |
| 3796 | |
| 3797 | if (err) { |
| 3798 | ly_err_print(err); |
| 3799 | ly_err_free(err); |
| 3800 | } |
| 3801 | LY_CHECK_RET(rc); |
| 3802 | |
| 3803 | /* finally check the identity itself */ |
| 3804 | if (self_match && (data.ident == val->ident)) { |
| 3805 | set_fill_boolean(set, 1); |
| 3806 | found = 1; |
| 3807 | } |
| 3808 | if (!found) { |
| 3809 | LY_ARRAY_FOR(data.ident->derived, u) { |
| 3810 | if (data.ident->derived[u] == val->ident) { |
| 3811 | set_fill_boolean(set, 1); |
| 3812 | found = 1; |
| 3813 | break; |
| 3814 | } |
| 3815 | } |
| 3816 | } |
| 3817 | |
| 3818 | /* free temporary value */ |
| 3819 | val->realtype->plugin->free(set->ctx, &data); |
| 3820 | if (found) { |
| 3821 | break; |
| 3822 | } |
| 3823 | } |
| 3824 | |
| 3825 | return LY_SUCCESS; |
| 3826 | } |
| 3827 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3828 | /** |
| 3829 | * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending |
| 3830 | * on whether the first argument nodes contain a node of an identity derived from the second |
| 3831 | * argument identity. |
| 3832 | * |
| 3833 | * @param[in] args Array of arguments. |
| 3834 | * @param[in] arg_count Count of elements in @p args. |
| 3835 | * @param[in,out] set Context and result set at the same time. |
| 3836 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3837 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3838 | */ |
| 3839 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3840 | 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] | 3841 | { |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3842 | return xpath_derived_(args, set, options, 0, __func__); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3843 | } |
| 3844 | |
| 3845 | /** |
| 3846 | * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending |
| 3847 | * on whether the first argument nodes contain a node of an identity that either is or is derived from |
| 3848 | * the second argument identity. |
| 3849 | * |
| 3850 | * @param[in] args Array of arguments. |
| 3851 | * @param[in] arg_count Count of elements in @p args. |
| 3852 | * @param[in,out] set Context and result set at the same time. |
| 3853 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3854 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3855 | */ |
| 3856 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3857 | 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] | 3858 | { |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3859 | return xpath_derived_(args, set, options, 1, __func__); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3860 | } |
| 3861 | |
| 3862 | /** |
| 3863 | * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER |
| 3864 | * with the integer value of the first node's enum value, otherwise NaN. |
| 3865 | * |
| 3866 | * @param[in] args Array of arguments. |
| 3867 | * @param[in] arg_count Count of elements in @p args. |
| 3868 | * @param[in,out] set Context and result set at the same time. |
| 3869 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3870 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3871 | */ |
| 3872 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3873 | 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] | 3874 | { |
| 3875 | struct lyd_node_term *leaf; |
| 3876 | struct lysc_node_leaf *sleaf; |
| 3877 | LY_ERR rc = LY_SUCCESS; |
| 3878 | |
| 3879 | if (options & LYXP_SCNODE_ALL) { |
| 3880 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3881 | 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] | 3882 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3883 | 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] | 3884 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) { |
| 3885 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"enumeration\".", __func__, sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3886 | } |
| 3887 | set_scnode_clear_ctx(set); |
| 3888 | return rc; |
| 3889 | } |
| 3890 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3891 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3892 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "enum-value(node-set)"); |
| 3893 | return LY_EVALID; |
| 3894 | } |
| 3895 | |
| 3896 | set_fill_number(set, NAN); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3897 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3898 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
| 3899 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 3900 | if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) { |
| 3901 | set_fill_number(set, leaf->value.enum_item->value); |
| 3902 | } |
| 3903 | } |
| 3904 | |
| 3905 | return LY_SUCCESS; |
| 3906 | } |
| 3907 | |
| 3908 | /** |
| 3909 | * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN |
| 3910 | * with false value. |
| 3911 | * |
| 3912 | * @param[in] args Array of arguments. |
| 3913 | * @param[in] arg_count Count of elements in @p args. |
| 3914 | * @param[in,out] set Context and result set at the same time. |
| 3915 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3916 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3917 | */ |
| 3918 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3919 | 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] | 3920 | { |
| 3921 | if (options & LYXP_SCNODE_ALL) { |
| 3922 | set_scnode_clear_ctx(set); |
| 3923 | return LY_SUCCESS; |
| 3924 | } |
| 3925 | |
| 3926 | set_fill_boolean(set, 0); |
| 3927 | return LY_SUCCESS; |
| 3928 | } |
| 3929 | |
| 3930 | /** |
| 3931 | * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER |
| 3932 | * with the first argument floored (truncated). |
| 3933 | * |
| 3934 | * @param[in] args Array of arguments. |
| 3935 | * @param[in] arg_count Count of elements in @p args. |
| 3936 | * @param[in,out] set Context and result set at the same time. |
| 3937 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3938 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3939 | */ |
| 3940 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3941 | xpath_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] | 3942 | { |
| 3943 | LY_ERR rc; |
| 3944 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3945 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3946 | LY_CHECK_RET(rc); |
| 3947 | if (isfinite(args[0]->val.num)) { |
| 3948 | set_fill_number(set, (long long)args[0]->val.num); |
| 3949 | } |
| 3950 | |
| 3951 | return LY_SUCCESS; |
| 3952 | } |
| 3953 | |
| 3954 | /** |
| 3955 | * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN |
| 3956 | * whether the language of the text matches the one from the argument. |
| 3957 | * |
| 3958 | * @param[in] args Array of arguments. |
| 3959 | * @param[in] arg_count Count of elements in @p args. |
| 3960 | * @param[in,out] set Context and result set at the same time. |
| 3961 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3962 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3963 | */ |
| 3964 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3965 | 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] | 3966 | { |
| 3967 | const struct lyd_node *node; |
| 3968 | struct lysc_node_leaf *sleaf; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 3969 | struct lyd_meta *meta = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3970 | const char *val; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3971 | LY_ERR rc = LY_SUCCESS; |
| 3972 | |
| 3973 | if (options & LYXP_SCNODE_ALL) { |
| 3974 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3975 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3976 | 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] | 3977 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3978 | 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] | 3979 | } |
| 3980 | } |
| 3981 | set_scnode_clear_ctx(set); |
| 3982 | return rc; |
| 3983 | } |
| 3984 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3985 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3986 | LY_CHECK_RET(rc); |
| 3987 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3988 | if (set->type != LYXP_SET_NODE_SET) { |
| 3989 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "lang(string)"); |
| 3990 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3991 | } else if (!set->used) { |
| 3992 | set_fill_boolean(set, 0); |
| 3993 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3994 | } |
| 3995 | |
| 3996 | switch (set->val.nodes[0].type) { |
| 3997 | case LYXP_NODE_ELEM: |
| 3998 | case LYXP_NODE_TEXT: |
| 3999 | node = set->val.nodes[0].node; |
| 4000 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4001 | case LYXP_NODE_META: |
| 4002 | node = set->val.meta[0].meta->parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4003 | break; |
| 4004 | default: |
| 4005 | /* nothing to do with roots */ |
| 4006 | set_fill_boolean(set, 0); |
| 4007 | return LY_SUCCESS; |
| 4008 | } |
| 4009 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4010 | /* find lang metadata */ |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 4011 | for ( ; node; node = (struct lyd_node *)node->parent) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4012 | for (meta = node->meta; meta; meta = meta->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4013 | /* annotations */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4014 | 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] | 4015 | break; |
| 4016 | } |
| 4017 | } |
| 4018 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4019 | if (meta) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4020 | break; |
| 4021 | } |
| 4022 | } |
| 4023 | |
| 4024 | /* compare languages */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4025 | if (!meta) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4026 | set_fill_boolean(set, 0); |
| 4027 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4028 | uint64_t i; |
| 4029 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 4030 | val = meta->value.canonical; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4031 | for (i = 0; args[0]->val.str[i]; ++i) { |
| 4032 | if (tolower(args[0]->val.str[i]) != tolower(val[i])) { |
| 4033 | set_fill_boolean(set, 0); |
| 4034 | break; |
| 4035 | } |
| 4036 | } |
| 4037 | if (!args[0]->val.str[i]) { |
| 4038 | if (!val[i] || (val[i] == '-')) { |
| 4039 | set_fill_boolean(set, 1); |
| 4040 | } else { |
| 4041 | set_fill_boolean(set, 0); |
| 4042 | } |
| 4043 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4044 | } |
| 4045 | |
| 4046 | return LY_SUCCESS; |
| 4047 | } |
| 4048 | |
| 4049 | /** |
| 4050 | * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER |
| 4051 | * with the context size. |
| 4052 | * |
| 4053 | * @param[in] args Array of arguments. |
| 4054 | * @param[in] arg_count Count of elements in @p args. |
| 4055 | * @param[in,out] set Context and result set at the same time. |
| 4056 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4057 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4058 | */ |
| 4059 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4060 | 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] | 4061 | { |
| 4062 | if (options & LYXP_SCNODE_ALL) { |
| 4063 | set_scnode_clear_ctx(set); |
| 4064 | return LY_SUCCESS; |
| 4065 | } |
| 4066 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4067 | if (set->type != LYXP_SET_NODE_SET) { |
| 4068 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "last()"); |
| 4069 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4070 | } else if (!set->used) { |
| 4071 | set_fill_number(set, 0); |
| 4072 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4073 | } |
| 4074 | |
| 4075 | set_fill_number(set, set->ctx_size); |
| 4076 | return LY_SUCCESS; |
| 4077 | } |
| 4078 | |
| 4079 | /** |
| 4080 | * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING |
| 4081 | * with the node name without namespace from the argument or the context. |
| 4082 | * |
| 4083 | * @param[in] args Array of arguments. |
| 4084 | * @param[in] arg_count Count of elements in @p args. |
| 4085 | * @param[in,out] set Context and result set at the same time. |
| 4086 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4087 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4088 | */ |
| 4089 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4090 | 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] | 4091 | { |
| 4092 | struct lyxp_set_node *item; |
| 4093 | /* suppress unused variable warning */ |
| 4094 | (void)options; |
| 4095 | |
| 4096 | if (options & LYXP_SCNODE_ALL) { |
| 4097 | set_scnode_clear_ctx(set); |
| 4098 | return LY_SUCCESS; |
| 4099 | } |
| 4100 | |
| 4101 | if (arg_count) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4102 | if (args[0]->type != LYXP_SET_NODE_SET) { |
| 4103 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "local-name(node-set?)"); |
| 4104 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4105 | } else if (!args[0]->used) { |
| 4106 | set_fill_string(set, "", 0); |
| 4107 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4108 | } |
| 4109 | |
| 4110 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4111 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4112 | |
| 4113 | item = &args[0]->val.nodes[0]; |
| 4114 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4115 | if (set->type != LYXP_SET_NODE_SET) { |
| 4116 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "local-name(node-set?)"); |
| 4117 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4118 | } else if (!set->used) { |
| 4119 | set_fill_string(set, "", 0); |
| 4120 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4121 | } |
| 4122 | |
| 4123 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4124 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4125 | |
| 4126 | item = &set->val.nodes[0]; |
| 4127 | } |
| 4128 | |
| 4129 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4130 | case LYXP_NODE_NONE: |
| 4131 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4132 | case LYXP_NODE_ROOT: |
| 4133 | case LYXP_NODE_ROOT_CONFIG: |
| 4134 | case LYXP_NODE_TEXT: |
| 4135 | set_fill_string(set, "", 0); |
| 4136 | break; |
| 4137 | case LYXP_NODE_ELEM: |
| 4138 | set_fill_string(set, item->node->schema->name, strlen(item->node->schema->name)); |
| 4139 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4140 | case LYXP_NODE_META: |
| 4141 | 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] | 4142 | break; |
| 4143 | } |
| 4144 | |
| 4145 | return LY_SUCCESS; |
| 4146 | } |
| 4147 | |
| 4148 | /** |
| 4149 | * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING |
| 4150 | * with the node name fully qualified (with namespace) from the argument or the context. |
| 4151 | * |
| 4152 | * @param[in] args Array of arguments. |
| 4153 | * @param[in] arg_count Count of elements in @p args. |
| 4154 | * @param[in,out] set Context and result set at the same time. |
| 4155 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4156 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4157 | */ |
| 4158 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4159 | 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] | 4160 | { |
| 4161 | struct lyxp_set_node *item; |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 4162 | struct lys_module *mod = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4163 | char *str; |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 4164 | const char *name = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4165 | |
| 4166 | if (options & LYXP_SCNODE_ALL) { |
| 4167 | set_scnode_clear_ctx(set); |
| 4168 | return LY_SUCCESS; |
| 4169 | } |
| 4170 | |
| 4171 | if (arg_count) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4172 | if (args[0]->type != LYXP_SET_NODE_SET) { |
| 4173 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "name(node-set?)"); |
| 4174 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4175 | } else if (!args[0]->used) { |
| 4176 | set_fill_string(set, "", 0); |
| 4177 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4178 | } |
| 4179 | |
| 4180 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4181 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4182 | |
| 4183 | item = &args[0]->val.nodes[0]; |
| 4184 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4185 | if (set->type != LYXP_SET_NODE_SET) { |
| 4186 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "name(node-set?)"); |
| 4187 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4188 | } else if (!set->used) { |
| 4189 | set_fill_string(set, "", 0); |
| 4190 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4191 | } |
| 4192 | |
| 4193 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4194 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4195 | |
| 4196 | item = &set->val.nodes[0]; |
| 4197 | } |
| 4198 | |
| 4199 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4200 | case LYXP_NODE_NONE: |
| 4201 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4202 | case LYXP_NODE_ROOT: |
| 4203 | case LYXP_NODE_ROOT_CONFIG: |
| 4204 | case LYXP_NODE_TEXT: |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 4205 | /* keep NULL */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4206 | break; |
| 4207 | case LYXP_NODE_ELEM: |
| 4208 | mod = item->node->schema->module; |
| 4209 | name = item->node->schema->name; |
| 4210 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4211 | case LYXP_NODE_META: |
| 4212 | mod = ((struct lyd_meta *)item->node)->annotation->module; |
| 4213 | name = ((struct lyd_meta *)item->node)->name; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4214 | break; |
| 4215 | } |
| 4216 | |
| 4217 | if (mod && name) { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4218 | int rc = -1; |
| 4219 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4220 | switch (set->format) { |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 4221 | case LY_PREF_SCHEMA: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4222 | rc = asprintf(&str, "%s:%s", lys_prefix_find_module(set->local_mod, mod), name); |
| 4223 | break; |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 4224 | case LY_PREF_JSON: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4225 | rc = asprintf(&str, "%s:%s", mod->name, name); |
| 4226 | break; |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 4227 | case LY_PREF_XML: |
Michal Vasko | 9409ef6 | 2019-09-12 11:47:17 +0200 | [diff] [blame] | 4228 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4229 | } |
| 4230 | LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM); |
| 4231 | set_fill_string(set, str, strlen(str)); |
| 4232 | free(str); |
| 4233 | } else { |
| 4234 | set_fill_string(set, "", 0); |
| 4235 | } |
| 4236 | |
| 4237 | return LY_SUCCESS; |
| 4238 | } |
| 4239 | |
| 4240 | /** |
| 4241 | * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING |
| 4242 | * with the namespace of the node from the argument or the context. |
| 4243 | * |
| 4244 | * @param[in] args Array of arguments. |
| 4245 | * @param[in] arg_count Count of elements in @p args. |
| 4246 | * @param[in,out] set Context and result set at the same time. |
| 4247 | * @param[in] options XPath options. |
| 4248 | * @return LY_ERR (LY_EINVAL for wrong arguments on schema) |
| 4249 | */ |
| 4250 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4251 | 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] | 4252 | { |
| 4253 | struct lyxp_set_node *item; |
| 4254 | struct lys_module *mod; |
| 4255 | /* suppress unused variable warning */ |
| 4256 | (void)options; |
| 4257 | |
| 4258 | if (options & LYXP_SCNODE_ALL) { |
| 4259 | set_scnode_clear_ctx(set); |
| 4260 | return LY_SUCCESS; |
| 4261 | } |
| 4262 | |
| 4263 | if (arg_count) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4264 | if (args[0]->type != LYXP_SET_NODE_SET) { |
| 4265 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), |
| 4266 | "namespace-uri(node-set?)"); |
| 4267 | return LY_EVALID; |
| 4268 | } else if (!args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4269 | set_fill_string(set, "", 0); |
| 4270 | return LY_SUCCESS; |
| 4271 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4272 | |
| 4273 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4274 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4275 | |
| 4276 | item = &args[0]->val.nodes[0]; |
| 4277 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4278 | if (set->type != LYXP_SET_NODE_SET) { |
| 4279 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "namespace-uri(node-set?)"); |
| 4280 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4281 | } else if (!set->used) { |
| 4282 | set_fill_string(set, "", 0); |
| 4283 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4284 | } |
| 4285 | |
| 4286 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4287 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4288 | |
| 4289 | item = &set->val.nodes[0]; |
| 4290 | } |
| 4291 | |
| 4292 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4293 | case LYXP_NODE_NONE: |
| 4294 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4295 | case LYXP_NODE_ROOT: |
| 4296 | case LYXP_NODE_ROOT_CONFIG: |
| 4297 | case LYXP_NODE_TEXT: |
| 4298 | set_fill_string(set, "", 0); |
| 4299 | break; |
| 4300 | case LYXP_NODE_ELEM: |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4301 | case LYXP_NODE_META: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4302 | if (item->type == LYXP_NODE_ELEM) { |
| 4303 | mod = item->node->schema->module; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4304 | } else { /* LYXP_NODE_META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4305 | /* annotations */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4306 | mod = ((struct lyd_meta *)item->node)->annotation->module; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4307 | } |
| 4308 | |
| 4309 | set_fill_string(set, mod->ns, strlen(mod->ns)); |
| 4310 | break; |
| 4311 | } |
| 4312 | |
| 4313 | return LY_SUCCESS; |
| 4314 | } |
| 4315 | |
| 4316 | /** |
| 4317 | * @brief Execute the XPath node() function (node type). Returns LYXP_SET_NODE_SET |
| 4318 | * with only nodes from the context. In practice it either leaves the context |
| 4319 | * as it is or returns an empty node set. |
| 4320 | * |
| 4321 | * @param[in] args Array of arguments. |
| 4322 | * @param[in] arg_count Count of elements in @p args. |
| 4323 | * @param[in,out] set Context and result set at the same time. |
| 4324 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4325 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4326 | */ |
| 4327 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4328 | 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] | 4329 | { |
| 4330 | if (options & LYXP_SCNODE_ALL) { |
| 4331 | set_scnode_clear_ctx(set); |
| 4332 | return LY_SUCCESS; |
| 4333 | } |
| 4334 | |
| 4335 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4336 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4337 | } |
| 4338 | return LY_SUCCESS; |
| 4339 | } |
| 4340 | |
| 4341 | /** |
| 4342 | * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING |
| 4343 | * with normalized value (no leading, trailing, double white spaces) of the node |
| 4344 | * from the argument or the context. |
| 4345 | * |
| 4346 | * @param[in] args Array of arguments. |
| 4347 | * @param[in] arg_count Count of elements in @p args. |
| 4348 | * @param[in,out] set Context and result set at the same time. |
| 4349 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4350 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4351 | */ |
| 4352 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4353 | 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] | 4354 | { |
| 4355 | uint16_t i, new_used; |
| 4356 | char *new; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4357 | uint8_t have_spaces = 0, space_before = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4358 | struct lysc_node_leaf *sleaf; |
| 4359 | LY_ERR rc = LY_SUCCESS; |
| 4360 | |
| 4361 | if (options & LYXP_SCNODE_ALL) { |
| 4362 | if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4363 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4364 | 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] | 4365 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4366 | 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] | 4367 | } |
| 4368 | } |
| 4369 | set_scnode_clear_ctx(set); |
| 4370 | return rc; |
| 4371 | } |
| 4372 | |
| 4373 | if (arg_count) { |
| 4374 | set_fill_set(set, args[0]); |
| 4375 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4376 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4377 | LY_CHECK_RET(rc); |
| 4378 | |
| 4379 | /* is there any normalization necessary? */ |
| 4380 | for (i = 0; set->val.str[i]; ++i) { |
| 4381 | if (is_xmlws(set->val.str[i])) { |
| 4382 | if ((i == 0) || space_before || (!set->val.str[i + 1])) { |
| 4383 | have_spaces = 1; |
| 4384 | break; |
| 4385 | } |
| 4386 | space_before = 1; |
| 4387 | } else { |
| 4388 | space_before = 0; |
| 4389 | } |
| 4390 | } |
| 4391 | |
| 4392 | /* yep, there is */ |
| 4393 | if (have_spaces) { |
| 4394 | /* it's enough, at least one character will go, makes space for ending '\0' */ |
| 4395 | new = malloc(strlen(set->val.str) * sizeof(char)); |
| 4396 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 4397 | new_used = 0; |
| 4398 | |
| 4399 | space_before = 0; |
| 4400 | for (i = 0; set->val.str[i]; ++i) { |
| 4401 | if (is_xmlws(set->val.str[i])) { |
| 4402 | if ((i == 0) || space_before) { |
| 4403 | space_before = 1; |
| 4404 | continue; |
| 4405 | } else { |
| 4406 | space_before = 1; |
| 4407 | } |
| 4408 | } else { |
| 4409 | space_before = 0; |
| 4410 | } |
| 4411 | |
| 4412 | new[new_used] = (space_before ? ' ' : set->val.str[i]); |
| 4413 | ++new_used; |
| 4414 | } |
| 4415 | |
| 4416 | /* at worst there is one trailing space now */ |
| 4417 | if (new_used && is_xmlws(new[new_used - 1])) { |
| 4418 | --new_used; |
| 4419 | } |
| 4420 | |
| 4421 | new = ly_realloc(new, (new_used + 1) * sizeof(char)); |
| 4422 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 4423 | new[new_used] = '\0'; |
| 4424 | |
| 4425 | free(set->val.str); |
| 4426 | set->val.str = new; |
| 4427 | } |
| 4428 | |
| 4429 | return LY_SUCCESS; |
| 4430 | } |
| 4431 | |
| 4432 | /** |
| 4433 | * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN |
| 4434 | * with the argument converted to boolean and logically inverted. |
| 4435 | * |
| 4436 | * @param[in] args Array of arguments. |
| 4437 | * @param[in] arg_count Count of elements in @p args. |
| 4438 | * @param[in,out] set Context and result set at the same time. |
| 4439 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4440 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4441 | */ |
| 4442 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4443 | 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] | 4444 | { |
| 4445 | if (options & LYXP_SCNODE_ALL) { |
| 4446 | set_scnode_clear_ctx(set); |
| 4447 | return LY_SUCCESS; |
| 4448 | } |
| 4449 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4450 | lyxp_set_cast(args[0], LYXP_SET_BOOLEAN); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4451 | if (args[0]->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4452 | set_fill_boolean(set, 0); |
| 4453 | } else { |
| 4454 | set_fill_boolean(set, 1); |
| 4455 | } |
| 4456 | |
| 4457 | return LY_SUCCESS; |
| 4458 | } |
| 4459 | |
| 4460 | /** |
| 4461 | * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER |
| 4462 | * with the number representation of either the argument or the context. |
| 4463 | * |
| 4464 | * @param[in] args Array of arguments. |
| 4465 | * @param[in] arg_count Count of elements in @p args. |
| 4466 | * @param[in,out] set Context and result set at the same time. |
| 4467 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4468 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4469 | */ |
| 4470 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4471 | 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] | 4472 | { |
| 4473 | LY_ERR rc; |
| 4474 | |
| 4475 | if (options & LYXP_SCNODE_ALL) { |
| 4476 | set_scnode_clear_ctx(set); |
| 4477 | return LY_SUCCESS; |
| 4478 | } |
| 4479 | |
| 4480 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4481 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4482 | LY_CHECK_RET(rc); |
| 4483 | set_fill_set(set, args[0]); |
| 4484 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4485 | rc = lyxp_set_cast(set, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4486 | LY_CHECK_RET(rc); |
| 4487 | } |
| 4488 | |
| 4489 | return LY_SUCCESS; |
| 4490 | } |
| 4491 | |
| 4492 | /** |
| 4493 | * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER |
| 4494 | * with the context position. |
| 4495 | * |
| 4496 | * @param[in] args Array of arguments. |
| 4497 | * @param[in] arg_count Count of elements in @p args. |
| 4498 | * @param[in,out] set Context and result set at the same time. |
| 4499 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4500 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4501 | */ |
| 4502 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4503 | 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] | 4504 | { |
| 4505 | if (options & LYXP_SCNODE_ALL) { |
| 4506 | set_scnode_clear_ctx(set); |
| 4507 | return LY_SUCCESS; |
| 4508 | } |
| 4509 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4510 | if (set->type != LYXP_SET_NODE_SET) { |
| 4511 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "position()"); |
| 4512 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4513 | } else if (!set->used) { |
| 4514 | set_fill_number(set, 0); |
| 4515 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4516 | } |
| 4517 | |
| 4518 | set_fill_number(set, set->ctx_pos); |
| 4519 | |
| 4520 | /* UNUSED in 'Release' build type */ |
| 4521 | (void)options; |
| 4522 | return LY_SUCCESS; |
| 4523 | } |
| 4524 | |
| 4525 | /** |
| 4526 | * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN |
| 4527 | * depending on whether the second argument regex matches the first argument string. For details refer to |
| 4528 | * YANG 1.1 RFC section 10.2.1. |
| 4529 | * |
| 4530 | * @param[in] args Array of arguments. |
| 4531 | * @param[in] arg_count Count of elements in @p args. |
| 4532 | * @param[in,out] set Context and result set at the same time. |
| 4533 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4534 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4535 | */ |
| 4536 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4537 | 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] | 4538 | { |
| 4539 | struct lysc_pattern **patterns = NULL, **pattern; |
| 4540 | struct lysc_node_leaf *sleaf; |
| 4541 | char *path; |
| 4542 | LY_ERR rc = LY_SUCCESS; |
| 4543 | struct ly_err_item *err; |
| 4544 | |
| 4545 | if (options & LYXP_SCNODE_ALL) { |
| 4546 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4547 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4548 | 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] | 4549 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4550 | 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] | 4551 | } |
| 4552 | } |
| 4553 | |
| 4554 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4555 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4556 | 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] | 4557 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4558 | 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] | 4559 | } |
| 4560 | } |
| 4561 | set_scnode_clear_ctx(set); |
| 4562 | return rc; |
| 4563 | } |
| 4564 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4565 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4566 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4567 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4568 | LY_CHECK_RET(rc); |
| 4569 | |
| 4570 | LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM); |
| 4571 | *pattern = malloc(sizeof **pattern); |
| 4572 | path = lyd_path(set->ctx_node, LYD_PATH_LOG, NULL, 0); |
| 4573 | rc = lys_compile_type_pattern_check(set->ctx, path, args[1]->val.str, &(*pattern)->code); |
| 4574 | free(path); |
| 4575 | if (rc != LY_SUCCESS) { |
| 4576 | LY_ARRAY_FREE(patterns); |
| 4577 | return rc; |
| 4578 | } |
| 4579 | |
| 4580 | rc = ly_type_validate_patterns(patterns, args[0]->val.str, strlen(args[0]->val.str), &err); |
| 4581 | pcre2_code_free((*pattern)->code); |
| 4582 | free(*pattern); |
| 4583 | LY_ARRAY_FREE(patterns); |
| 4584 | if (rc && (rc != LY_EVALID)) { |
| 4585 | ly_err_print(err); |
| 4586 | ly_err_free(err); |
| 4587 | return rc; |
| 4588 | } |
| 4589 | |
| 4590 | if (rc == LY_EVALID) { |
| 4591 | ly_err_free(err); |
| 4592 | set_fill_boolean(set, 0); |
| 4593 | } else { |
| 4594 | set_fill_boolean(set, 1); |
| 4595 | } |
| 4596 | |
| 4597 | return LY_SUCCESS; |
| 4598 | } |
| 4599 | |
| 4600 | /** |
| 4601 | * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER |
| 4602 | * with the rounded first argument. For details refer to |
| 4603 | * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round. |
| 4604 | * |
| 4605 | * @param[in] args Array of arguments. |
| 4606 | * @param[in] arg_count Count of elements in @p args. |
| 4607 | * @param[in,out] set Context and result set at the same time. |
| 4608 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4609 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4610 | */ |
| 4611 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4612 | 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] | 4613 | { |
| 4614 | struct lysc_node_leaf *sleaf; |
| 4615 | LY_ERR rc = LY_SUCCESS; |
| 4616 | |
| 4617 | if (options & LYXP_SCNODE_ALL) { |
| 4618 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4619 | 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] | 4620 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4621 | 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] | 4622 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) { |
| 4623 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4624 | } |
| 4625 | set_scnode_clear_ctx(set); |
| 4626 | return rc; |
| 4627 | } |
| 4628 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4629 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4630 | LY_CHECK_RET(rc); |
| 4631 | |
| 4632 | /* cover only the cases where floor can't be used */ |
| 4633 | if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) { |
| 4634 | set_fill_number(set, -0.0f); |
| 4635 | } else { |
| 4636 | args[0]->val.num += 0.5; |
| 4637 | rc = xpath_floor(args, 1, args[0], options); |
| 4638 | LY_CHECK_RET(rc); |
| 4639 | set_fill_number(set, args[0]->val.num); |
| 4640 | } |
| 4641 | |
| 4642 | return LY_SUCCESS; |
| 4643 | } |
| 4644 | |
| 4645 | /** |
| 4646 | * @brief Execute the XPath starts-with(string, string) function. |
| 4647 | * Returns LYXP_SET_BOOLEAN whether the second argument is |
| 4648 | * the prefix of the first or not. |
| 4649 | * |
| 4650 | * @param[in] args Array of arguments. |
| 4651 | * @param[in] arg_count Count of elements in @p args. |
| 4652 | * @param[in,out] set Context and result set at the same time. |
| 4653 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4654 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4655 | */ |
| 4656 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4657 | 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] | 4658 | { |
| 4659 | struct lysc_node_leaf *sleaf; |
| 4660 | LY_ERR rc = LY_SUCCESS; |
| 4661 | |
| 4662 | if (options & LYXP_SCNODE_ALL) { |
| 4663 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4664 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4665 | 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] | 4666 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4667 | 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] | 4668 | } |
| 4669 | } |
| 4670 | |
| 4671 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4672 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4673 | 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] | 4674 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4675 | 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] | 4676 | } |
| 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_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4683 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4684 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4685 | LY_CHECK_RET(rc); |
| 4686 | |
| 4687 | if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) { |
| 4688 | set_fill_boolean(set, 0); |
| 4689 | } else { |
| 4690 | set_fill_boolean(set, 1); |
| 4691 | } |
| 4692 | |
| 4693 | return LY_SUCCESS; |
| 4694 | } |
| 4695 | |
| 4696 | /** |
| 4697 | * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING |
| 4698 | * with the string representation of either the argument or the context. |
| 4699 | * |
| 4700 | * @param[in] args Array of arguments. |
| 4701 | * @param[in] arg_count Count of elements in @p args. |
| 4702 | * @param[in,out] set Context and result set at the same time. |
| 4703 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4704 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4705 | */ |
| 4706 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4707 | 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] | 4708 | { |
| 4709 | LY_ERR rc; |
| 4710 | |
| 4711 | if (options & LYXP_SCNODE_ALL) { |
| 4712 | set_scnode_clear_ctx(set); |
| 4713 | return LY_SUCCESS; |
| 4714 | } |
| 4715 | |
| 4716 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4717 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4718 | LY_CHECK_RET(rc); |
| 4719 | set_fill_set(set, args[0]); |
| 4720 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4721 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4722 | LY_CHECK_RET(rc); |
| 4723 | } |
| 4724 | |
| 4725 | return LY_SUCCESS; |
| 4726 | } |
| 4727 | |
| 4728 | /** |
| 4729 | * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER |
| 4730 | * with the length of the string in either the argument or the context. |
| 4731 | * |
| 4732 | * @param[in] args Array of arguments. |
| 4733 | * @param[in] arg_count Count of elements in @p args. |
| 4734 | * @param[in,out] set Context and result set at the same time. |
| 4735 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4736 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4737 | */ |
| 4738 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4739 | 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] | 4740 | { |
| 4741 | struct lysc_node_leaf *sleaf; |
| 4742 | LY_ERR rc = LY_SUCCESS; |
| 4743 | |
| 4744 | if (options & LYXP_SCNODE_ALL) { |
| 4745 | if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4746 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4747 | 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] | 4748 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4749 | 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] | 4750 | } |
| 4751 | } |
| 4752 | if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) { |
| 4753 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4754 | 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] | 4755 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4756 | 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] | 4757 | } |
| 4758 | } |
| 4759 | set_scnode_clear_ctx(set); |
| 4760 | return rc; |
| 4761 | } |
| 4762 | |
| 4763 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4764 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4765 | LY_CHECK_RET(rc); |
| 4766 | set_fill_number(set, strlen(args[0]->val.str)); |
| 4767 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4768 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4769 | LY_CHECK_RET(rc); |
| 4770 | set_fill_number(set, strlen(set->val.str)); |
| 4771 | } |
| 4772 | |
| 4773 | return LY_SUCCESS; |
| 4774 | } |
| 4775 | |
| 4776 | /** |
| 4777 | * @brief Execute the XPath substring(string, number, number?) function. |
| 4778 | * Returns LYXP_SET_STRING substring of the first argument starting |
| 4779 | * on the second argument index ending on the third argument index, |
| 4780 | * indexed from 1. For exact definition refer to |
| 4781 | * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring. |
| 4782 | * |
| 4783 | * @param[in] args Array of arguments. |
| 4784 | * @param[in] arg_count Count of elements in @p args. |
| 4785 | * @param[in,out] set Context and result set at the same time. |
| 4786 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4787 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4788 | */ |
| 4789 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4790 | 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] | 4791 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4792 | int32_t start, len; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4793 | uint16_t str_start, str_len, pos; |
| 4794 | struct lysc_node_leaf *sleaf; |
| 4795 | LY_ERR rc = LY_SUCCESS; |
| 4796 | |
| 4797 | if (options & LYXP_SCNODE_ALL) { |
| 4798 | if ((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 | |
| 4806 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4807 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4808 | 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] | 4809 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 4810 | 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] | 4811 | } |
| 4812 | } |
| 4813 | |
| 4814 | if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET) |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 4815 | && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4816 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4817 | 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] | 4818 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 4819 | 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] | 4820 | } |
| 4821 | } |
| 4822 | set_scnode_clear_ctx(set); |
| 4823 | return rc; |
| 4824 | } |
| 4825 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4826 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4827 | LY_CHECK_RET(rc); |
| 4828 | |
| 4829 | /* start */ |
| 4830 | if (xpath_round(&args[1], 1, args[1], options)) { |
| 4831 | return -1; |
| 4832 | } |
| 4833 | if (isfinite(args[1]->val.num)) { |
| 4834 | start = args[1]->val.num - 1; |
| 4835 | } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4836 | start = INT32_MIN; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4837 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4838 | start = INT32_MAX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4839 | } |
| 4840 | |
| 4841 | /* len */ |
| 4842 | if (arg_count == 3) { |
| 4843 | rc = xpath_round(&args[2], 1, args[2], options); |
| 4844 | LY_CHECK_RET(rc); |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4845 | if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4846 | len = 0; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4847 | } else if (isfinite(args[2]->val.num)) { |
| 4848 | len = args[2]->val.num; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4849 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4850 | len = INT32_MAX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4851 | } |
| 4852 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4853 | len = INT32_MAX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4854 | } |
| 4855 | |
| 4856 | /* find matching character positions */ |
| 4857 | str_start = 0; |
| 4858 | str_len = 0; |
| 4859 | for (pos = 0; args[0]->val.str[pos]; ++pos) { |
| 4860 | if (pos < start) { |
| 4861 | ++str_start; |
| 4862 | } else if (pos < start + len) { |
| 4863 | ++str_len; |
| 4864 | } else { |
| 4865 | break; |
| 4866 | } |
| 4867 | } |
| 4868 | |
| 4869 | set_fill_string(set, args[0]->val.str + str_start, str_len); |
| 4870 | return LY_SUCCESS; |
| 4871 | } |
| 4872 | |
| 4873 | /** |
| 4874 | * @brief Execute the XPath substring-after(string, string) function. |
| 4875 | * Returns LYXP_SET_STRING with the string succeeding the occurance |
| 4876 | * of the second argument in the first or an empty string. |
| 4877 | * |
| 4878 | * @param[in] args Array of arguments. |
| 4879 | * @param[in] arg_count Count of elements in @p args. |
| 4880 | * @param[in,out] set Context and result set at the same time. |
| 4881 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4882 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4883 | */ |
| 4884 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4885 | 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] | 4886 | { |
| 4887 | char *ptr; |
| 4888 | struct lysc_node_leaf *sleaf; |
| 4889 | LY_ERR rc = LY_SUCCESS; |
| 4890 | |
| 4891 | if (options & LYXP_SCNODE_ALL) { |
| 4892 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4893 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4894 | 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] | 4895 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4896 | 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] | 4897 | } |
| 4898 | } |
| 4899 | |
| 4900 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4901 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4902 | 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] | 4903 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4904 | 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] | 4905 | } |
| 4906 | } |
| 4907 | set_scnode_clear_ctx(set); |
| 4908 | return rc; |
| 4909 | } |
| 4910 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4911 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4912 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4913 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4914 | LY_CHECK_RET(rc); |
| 4915 | |
| 4916 | ptr = strstr(args[0]->val.str, args[1]->val.str); |
| 4917 | if (ptr) { |
| 4918 | set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str))); |
| 4919 | } else { |
| 4920 | set_fill_string(set, "", 0); |
| 4921 | } |
| 4922 | |
| 4923 | return LY_SUCCESS; |
| 4924 | } |
| 4925 | |
| 4926 | /** |
| 4927 | * @brief Execute the XPath substring-before(string, string) function. |
| 4928 | * Returns LYXP_SET_STRING with the string preceding 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_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] | 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, args[0]->val.str, ptr - args[0]->val.str); |
| 4972 | } else { |
| 4973 | set_fill_string(set, "", 0); |
| 4974 | } |
| 4975 | |
| 4976 | return LY_SUCCESS; |
| 4977 | } |
| 4978 | |
| 4979 | /** |
| 4980 | * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER |
| 4981 | * with the sum of all the nodes in the context. |
| 4982 | * |
| 4983 | * @param[in] args Array of arguments. |
| 4984 | * @param[in] arg_count Count of elements in @p args. |
| 4985 | * @param[in,out] set Context and result set at the same time. |
| 4986 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4987 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4988 | */ |
| 4989 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4990 | xpath_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] | 4991 | { |
| 4992 | long double num; |
| 4993 | char *str; |
| 4994 | uint16_t i; |
| 4995 | struct lyxp_set set_item; |
| 4996 | struct lysc_node_leaf *sleaf; |
| 4997 | LY_ERR rc = LY_SUCCESS; |
| 4998 | |
| 4999 | if (options & LYXP_SCNODE_ALL) { |
| 5000 | if (args[0]->type == LYXP_SET_SCNODE_SET) { |
| 5001 | for (i = 0; i < args[0]->used; ++i) { |
| 5002 | if (args[0]->val.scnodes[i].in_ctx == 1) { |
| 5003 | sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode; |
| 5004 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5005 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, |
| 5006 | lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5007 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 5008 | 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] | 5009 | } |
| 5010 | } |
| 5011 | } |
| 5012 | } |
| 5013 | set_scnode_clear_ctx(set); |
| 5014 | return rc; |
| 5015 | } |
| 5016 | |
| 5017 | set_fill_number(set, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5018 | |
| 5019 | if (args[0]->type != LYXP_SET_NODE_SET) { |
| 5020 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "sum(node-set)"); |
| 5021 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5022 | } else if (!args[0]->used) { |
| 5023 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5024 | } |
| 5025 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5026 | set_init(&set_item, set); |
| 5027 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5028 | set_item.type = LYXP_SET_NODE_SET; |
| 5029 | set_item.val.nodes = malloc(sizeof *set_item.val.nodes); |
| 5030 | LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM); |
| 5031 | |
| 5032 | set_item.used = 1; |
| 5033 | set_item.size = 1; |
| 5034 | |
| 5035 | for (i = 0; i < args[0]->used; ++i) { |
| 5036 | set_item.val.nodes[0] = args[0]->val.nodes[i]; |
| 5037 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5038 | rc = cast_node_set_to_string(&set_item, &str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5039 | LY_CHECK_RET(rc); |
| 5040 | num = cast_string_to_number(str); |
| 5041 | free(str); |
| 5042 | set->val.num += num; |
| 5043 | } |
| 5044 | |
| 5045 | free(set_item.val.nodes); |
| 5046 | |
| 5047 | return LY_SUCCESS; |
| 5048 | } |
| 5049 | |
| 5050 | /** |
| 5051 | * @brief Execute the XPath text() function (node type). Returns LYXP_SET_NODE_SET |
| 5052 | * with the text content of the nodes in the context. |
| 5053 | * |
| 5054 | * @param[in] args Array of arguments. |
| 5055 | * @param[in] arg_count Count of elements in @p args. |
| 5056 | * @param[in,out] set Context and result set at the same time. |
| 5057 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5058 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5059 | */ |
| 5060 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5061 | 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] | 5062 | { |
| 5063 | uint32_t i; |
| 5064 | |
| 5065 | if (options & LYXP_SCNODE_ALL) { |
| 5066 | set_scnode_clear_ctx(set); |
| 5067 | return LY_SUCCESS; |
| 5068 | } |
| 5069 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5070 | if (set->type != LYXP_SET_NODE_SET) { |
| 5071 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "text()"); |
| 5072 | return LY_EVALID; |
| 5073 | } |
| 5074 | |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 5075 | for (i = 0; i < set->used; ) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5076 | switch (set->val.nodes[i].type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 5077 | case LYXP_NODE_NONE: |
| 5078 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5079 | case LYXP_NODE_ELEM: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5080 | if (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 5081 | set->val.nodes[i].type = LYXP_NODE_TEXT; |
| 5082 | ++i; |
| 5083 | break; |
| 5084 | } |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 5085 | /* fall through */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5086 | case LYXP_NODE_ROOT: |
| 5087 | case LYXP_NODE_ROOT_CONFIG: |
| 5088 | case LYXP_NODE_TEXT: |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5089 | case LYXP_NODE_META: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5090 | set_remove_node(set, i); |
| 5091 | break; |
| 5092 | } |
| 5093 | } |
| 5094 | |
| 5095 | return LY_SUCCESS; |
| 5096 | } |
| 5097 | |
| 5098 | /** |
| 5099 | * @brief Execute the XPath translate(string, string, string) function. |
| 5100 | * Returns LYXP_SET_STRING with the first argument with the characters |
| 5101 | * from the second argument replaced by those on the corresponding |
| 5102 | * positions in the third argument. |
| 5103 | * |
| 5104 | * @param[in] args Array of arguments. |
| 5105 | * @param[in] arg_count Count of elements in @p args. |
| 5106 | * @param[in,out] set Context and result set at the same time. |
| 5107 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5108 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5109 | */ |
| 5110 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5111 | 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] | 5112 | { |
| 5113 | uint16_t i, j, new_used; |
| 5114 | char *new; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5115 | uint8_t have_removed; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5116 | struct lysc_node_leaf *sleaf; |
| 5117 | LY_ERR rc = LY_SUCCESS; |
| 5118 | |
| 5119 | if (options & LYXP_SCNODE_ALL) { |
| 5120 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 5121 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5122 | 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] | 5123 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5124 | 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] | 5125 | } |
| 5126 | } |
| 5127 | |
| 5128 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 5129 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5130 | 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] | 5131 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5132 | 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] | 5133 | } |
| 5134 | } |
| 5135 | |
| 5136 | if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) { |
| 5137 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5138 | 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] | 5139 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5140 | 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] | 5141 | } |
| 5142 | } |
| 5143 | set_scnode_clear_ctx(set); |
| 5144 | return rc; |
| 5145 | } |
| 5146 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5147 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5148 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5149 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5150 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5151 | rc = lyxp_set_cast(args[2], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5152 | LY_CHECK_RET(rc); |
| 5153 | |
| 5154 | new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char)); |
| 5155 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 5156 | new_used = 0; |
| 5157 | |
| 5158 | have_removed = 0; |
| 5159 | for (i = 0; args[0]->val.str[i]; ++i) { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5160 | uint8_t found = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5161 | |
| 5162 | for (j = 0; args[1]->val.str[j]; ++j) { |
| 5163 | if (args[0]->val.str[i] == args[1]->val.str[j]) { |
| 5164 | /* removing this char */ |
| 5165 | if (j >= strlen(args[2]->val.str)) { |
| 5166 | have_removed = 1; |
| 5167 | found = 1; |
| 5168 | break; |
| 5169 | } |
| 5170 | /* replacing this char */ |
| 5171 | new[new_used] = args[2]->val.str[j]; |
| 5172 | ++new_used; |
| 5173 | found = 1; |
| 5174 | break; |
| 5175 | } |
| 5176 | } |
| 5177 | |
| 5178 | /* copying this char */ |
| 5179 | if (!found) { |
| 5180 | new[new_used] = args[0]->val.str[i]; |
| 5181 | ++new_used; |
| 5182 | } |
| 5183 | } |
| 5184 | |
| 5185 | if (have_removed) { |
| 5186 | new = ly_realloc(new, (new_used + 1) * sizeof(char)); |
| 5187 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 5188 | } |
| 5189 | new[new_used] = '\0'; |
| 5190 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5191 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5192 | set->type = LYXP_SET_STRING; |
| 5193 | set->val.str = new; |
| 5194 | |
| 5195 | return LY_SUCCESS; |
| 5196 | } |
| 5197 | |
| 5198 | /** |
| 5199 | * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN |
| 5200 | * with true value. |
| 5201 | * |
| 5202 | * @param[in] args Array of arguments. |
| 5203 | * @param[in] arg_count Count of elements in @p args. |
| 5204 | * @param[in,out] set Context and result set at the same time. |
| 5205 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5206 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5207 | */ |
| 5208 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5209 | 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] | 5210 | { |
| 5211 | if (options & LYXP_SCNODE_ALL) { |
| 5212 | set_scnode_clear_ctx(set); |
| 5213 | return LY_SUCCESS; |
| 5214 | } |
| 5215 | |
| 5216 | set_fill_boolean(set, 1); |
| 5217 | return LY_SUCCESS; |
| 5218 | } |
| 5219 | |
| 5220 | /* |
| 5221 | * moveto functions |
| 5222 | * |
| 5223 | * They and only they actually change the context (set). |
| 5224 | */ |
| 5225 | |
| 5226 | /** |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5227 | * @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] | 5228 | * |
Michal Vasko | 2104e9f | 2020-03-06 08:23:25 +0100 | [diff] [blame] | 5229 | * XPath @p set is expected to be a (sc)node set! |
| 5230 | * |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5231 | * @param[in,out] qname Qualified node name. If includes prefix, it is skipped. |
| 5232 | * @param[in,out] qname_len Length of @p qname, is updated accordingly. |
| 5233 | * @param[in] set Set with XPath context. |
| 5234 | * @param[out] moveto_mod Expected module of a matching node. |
| 5235 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5236 | */ |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5237 | static LY_ERR |
| 5238 | moveto_resolve_model(const char **qname, uint16_t *qname_len, struct lyxp_set *set, const struct lys_module **moveto_mod) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5239 | { |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 5240 | const struct lys_module *mod = NULL; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5241 | const char *ptr; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5242 | size_t pref_len; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5243 | |
Michal Vasko | 2104e9f | 2020-03-06 08:23:25 +0100 | [diff] [blame] | 5244 | assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET)); |
| 5245 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5246 | if ((ptr = ly_strnchr(*qname, ':', *qname_len))) { |
| 5247 | /* specific module */ |
| 5248 | pref_len = ptr - *qname; |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 5249 | mod = ly_resolve_prefix(set->ctx, *qname, pref_len, set->format, (void *)set->local_mod); |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5250 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5251 | /* check for errors and non-implemented modules, as they are not valid */ |
Juraj Vijtiuk | d75faa6 | 2019-11-26 14:10:10 +0100 | [diff] [blame] | 5252 | if (!mod || !mod->implemented) { |
Michal Vasko | 2104e9f | 2020-03-06 08:23:25 +0100 | [diff] [blame] | 5253 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 5254 | LOGVAL(set->ctx, LY_VLOG_LYSC, set->ctx_scnode, LY_VCODE_XP_INMOD, pref_len, *qname); |
| 5255 | } else { |
| 5256 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INMOD, pref_len, *qname); |
| 5257 | } |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5258 | return LY_EVALID; |
| 5259 | } |
Juraj Vijtiuk | d75faa6 | 2019-11-26 14:10:10 +0100 | [diff] [blame] | 5260 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5261 | *qname += pref_len + 1; |
| 5262 | *qname_len -= pref_len + 1; |
| 5263 | } else if (((*qname)[0] == '*') && (*qname_len == 1)) { |
| 5264 | /* all modules - special case */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5265 | mod = NULL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5266 | } else if (set->type == LYXP_SET_SCNODE_SET) { |
| 5267 | /* current node module */ |
| 5268 | mod = set->ctx_scnode->module; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5269 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5270 | /* current node module */ |
| 5271 | mod = set->ctx_node->schema->module; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5272 | } |
| 5273 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5274 | *moveto_mod = mod; |
| 5275 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5276 | } |
| 5277 | |
| 5278 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5279 | * @brief Move context @p set to the root. Handles absolute path. |
| 5280 | * Result is LYXP_SET_NODE_SET. |
| 5281 | * |
| 5282 | * @param[in,out] set Set to use. |
| 5283 | * @param[in] options Xpath options. |
| 5284 | */ |
| 5285 | static void |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5286 | moveto_root(struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5287 | { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5288 | if (!set) { |
| 5289 | return; |
| 5290 | } |
| 5291 | |
| 5292 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5293 | set_scnode_clear_ctx(set); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 5294 | lyxp_set_scnode_insert_node(set, NULL, set->root_type); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5295 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5296 | set->type = LYXP_SET_NODE_SET; |
| 5297 | set->used = 0; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5298 | set_insert_node(set, NULL, 0, set->root_type, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5299 | } |
| 5300 | } |
| 5301 | |
| 5302 | /** |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 5303 | * @brief Check whether a node has some unresolved "when". |
| 5304 | * |
| 5305 | * @param[in] node Node to check. |
| 5306 | * @return LY_ERR value (LY_EINCOMPLETE if there are some unresolved "when") |
| 5307 | */ |
| 5308 | static LY_ERR |
| 5309 | moveto_when_check(const struct lyd_node *node) |
| 5310 | { |
| 5311 | const struct lysc_node *schema; |
| 5312 | |
| 5313 | if (!node) { |
| 5314 | return LY_SUCCESS; |
| 5315 | } |
| 5316 | |
| 5317 | schema = node->schema; |
| 5318 | do { |
| 5319 | if (schema->when && !(node->flags & LYD_WHEN_TRUE)) { |
| 5320 | return LY_EINCOMPLETE; |
| 5321 | } |
| 5322 | schema = schema->parent; |
| 5323 | } while (schema && (schema->nodetype & (LYS_CASE | LYS_CHOICE))); |
| 5324 | |
| 5325 | return LY_SUCCESS; |
| 5326 | } |
| 5327 | |
| 5328 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5329 | * @brief Check @p node as a part of NameTest processing. |
| 5330 | * |
| 5331 | * @param[in] node Node to check. |
| 5332 | * @param[in] root_type XPath root node type. |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5333 | * @param[in] context_op XPath operation parent. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5334 | * @param[in] node_name Node name in the dictionary to move to, NULL for any node. |
| 5335 | * @param[in] moveto_mod Expected module of the node, NULL for any. |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5336 | * @return LY_ERR (LY_ENOT if node does not match, LY_EINCOMPLETE on unresolved when, |
| 5337 | * LY_EINVAL if netither node nor any children match) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5338 | */ |
| 5339 | static LY_ERR |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5340 | moveto_node_check(const struct lyd_node *node, enum lyxp_node_type root_type, const struct lysc_node *context_op, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 5341 | const char *node_name, const struct lys_module *moveto_mod) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5342 | { |
| 5343 | /* module check */ |
| 5344 | if (moveto_mod && (node->schema->module != moveto_mod)) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5345 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5346 | } |
| 5347 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5348 | /* context check */ |
| 5349 | if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5350 | return LY_EINVAL; |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5351 | } else if (context_op && (node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node->schema != context_op)) { |
| 5352 | return LY_EINVAL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5353 | } |
| 5354 | |
| 5355 | /* name check */ |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5356 | if (node_name && strcmp(node_name, "*") && (node->schema->name != node_name)) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5357 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5358 | } |
| 5359 | |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 5360 | /* when check */ |
| 5361 | if (moveto_when_check(node)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5362 | return LY_EINCOMPLETE; |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 5363 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5364 | |
| 5365 | /* match */ |
| 5366 | return LY_SUCCESS; |
| 5367 | } |
| 5368 | |
| 5369 | /** |
| 5370 | * @brief Check @p node as a part of schema NameTest processing. |
| 5371 | * |
| 5372 | * @param[in] node Schema node to check. |
| 5373 | * @param[in] root_type XPath root node type. |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5374 | * @param[in] context_op XPath operation parent. |
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 nodes. |
| 5376 | * @param[in] moveto_mod Expected module of the node, NULL for any. |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5377 | * @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] | 5378 | */ |
| 5379 | static LY_ERR |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5380 | moveto_scnode_check(const struct lysc_node *node, enum lyxp_node_type root_type, const struct lysc_node *context_op, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 5381 | const char *node_name, const struct lys_module *moveto_mod) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5382 | { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5383 | /* module check */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5384 | if (moveto_mod && (node->module != moveto_mod)) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5385 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5386 | } |
| 5387 | |
| 5388 | /* context check */ |
| 5389 | if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) { |
| 5390 | return LY_EINVAL; |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5391 | } else if (context_op && (node->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node != context_op)) { |
| 5392 | return LY_EINVAL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5393 | } |
| 5394 | |
| 5395 | /* name check */ |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5396 | if (node_name && strcmp(node_name, "*") && (node->name != node_name)) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5397 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5398 | } |
| 5399 | |
| 5400 | /* match */ |
| 5401 | return LY_SUCCESS; |
| 5402 | } |
| 5403 | |
| 5404 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5405 | * @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] | 5406 | * |
| 5407 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5408 | * @param[in] mod Matching node module, NULL for any. |
| 5409 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5410 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 5411 | */ |
| 5412 | static LY_ERR |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5413 | moveto_node(struct lyxp_set *set, const struct lys_module *mod, const char *ncname) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5414 | { |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 5415 | uint32_t i; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5416 | const struct lyd_node *siblings, *sub; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5417 | LY_ERR rc; |
| 5418 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5419 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5420 | return LY_SUCCESS; |
| 5421 | } |
| 5422 | |
| 5423 | if (set->type != LYXP_SET_NODE_SET) { |
| 5424 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 5425 | return LY_EVALID; |
| 5426 | } |
| 5427 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5428 | for (i = 0; i < set->used; ) { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5429 | uint8_t replaced = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5430 | |
| 5431 | 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] | 5432 | assert(!set->val.nodes[i].node); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5433 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5434 | /* search in all the trees */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5435 | siblings = set->tree; |
Michal Vasko | 5bfd4be | 2020-06-23 13:26:19 +0200 | [diff] [blame] | 5436 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5437 | /* search in children */ |
Michal Vasko | 5bfd4be | 2020-06-23 13:26:19 +0200 | [diff] [blame] | 5438 | siblings = lyd_node_children(set->val.nodes[i].node, 0); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5439 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5440 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5441 | for (sub = siblings; sub; sub = sub->next) { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5442 | rc = moveto_node_check(sub, set->root_type, set->context_op, ncname, mod); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5443 | if (rc == LY_SUCCESS) { |
| 5444 | if (!replaced) { |
| 5445 | set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i); |
| 5446 | replaced = 1; |
| 5447 | } else { |
| 5448 | set_insert_node(set, sub, 0, LYXP_NODE_ELEM, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5449 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5450 | ++i; |
| 5451 | } else if (rc == LY_EINCOMPLETE) { |
| 5452 | return rc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5453 | } |
| 5454 | } |
| 5455 | |
| 5456 | if (!replaced) { |
| 5457 | /* no match */ |
| 5458 | set_remove_node(set, i); |
| 5459 | } |
| 5460 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5461 | |
| 5462 | return LY_SUCCESS; |
| 5463 | } |
| 5464 | |
| 5465 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5466 | * @brief Move context @p set to a node using hashes. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY). |
| 5467 | * Context position aware. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5468 | * |
| 5469 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5470 | * @param[in] scnode Matching node schema. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5471 | * @param[in] predicates If @p scnode is ::LYS_LIST or ::LYS_LEAFLIST, the predicates specifying a single instance. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5472 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 5473 | */ |
| 5474 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5475 | moveto_node_hash(struct lyxp_set *set, const struct lysc_node *scnode, const struct ly_path_predicate *predicates) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5476 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5477 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5478 | uint32_t i; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5479 | const struct lyd_node *siblings; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5480 | struct lyd_node *sub, *inst = NULL; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5481 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5482 | assert(scnode && (!(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) || predicates)); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5483 | |
| 5484 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5485 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5486 | } |
| 5487 | |
| 5488 | if (set->type != LYXP_SET_NODE_SET) { |
| 5489 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5490 | ret = LY_EVALID; |
| 5491 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5492 | } |
| 5493 | |
| 5494 | /* context check for all the nodes since we have the schema node */ |
| 5495 | if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) { |
| 5496 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5497 | goto cleanup; |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 5498 | } else if (set->context_op && (scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5499 | && (scnode != set->context_op)) { |
| 5500 | lyxp_set_free_content(set); |
| 5501 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5502 | } |
| 5503 | |
| 5504 | /* create specific data instance if needed */ |
| 5505 | if (scnode->nodetype == LYS_LIST) { |
| 5506 | LY_CHECK_GOTO(ret = lyd_create_list(scnode, predicates, &inst), cleanup); |
| 5507 | } else if (scnode->nodetype == LYS_LEAFLIST) { |
| 5508 | 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] | 5509 | } |
| 5510 | |
| 5511 | for (i = 0; i < set->used; ) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5512 | siblings = NULL; |
| 5513 | |
| 5514 | if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) { |
| 5515 | assert(!set->val.nodes[i].node); |
| 5516 | |
| 5517 | /* search in all the trees */ |
| 5518 | siblings = set->tree; |
| 5519 | } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
| 5520 | /* search in children */ |
Michal Vasko | 5bfd4be | 2020-06-23 13:26:19 +0200 | [diff] [blame] | 5521 | siblings = lyd_node_children(set->val.nodes[i].node, 0); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5522 | } |
| 5523 | |
| 5524 | /* find the node using hashes */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5525 | if (inst) { |
| 5526 | lyd_find_sibling_first(siblings, inst, &sub); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5527 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5528 | lyd_find_sibling_val(siblings, scnode, NULL, 0, &sub); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5529 | } |
| 5530 | |
| 5531 | /* when check */ |
| 5532 | if (sub && moveto_when_check(sub)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5533 | ret = LY_EINCOMPLETE; |
| 5534 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5535 | } |
| 5536 | |
| 5537 | if (sub) { |
| 5538 | /* pos filled later */ |
Michal Vasko | cb1b7c0 | 2020-07-03 13:38:12 +0200 | [diff] [blame] | 5539 | set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5540 | ++i; |
Michal Vasko | cb1b7c0 | 2020-07-03 13:38:12 +0200 | [diff] [blame] | 5541 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5542 | /* no match */ |
| 5543 | set_remove_node(set, i); |
| 5544 | } |
| 5545 | } |
| 5546 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5547 | cleanup: |
| 5548 | lyd_free_tree(inst); |
| 5549 | return ret; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5550 | } |
| 5551 | |
| 5552 | /** |
| 5553 | * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY). |
| 5554 | * |
| 5555 | * @param[in,out] set Set to use. |
| 5556 | * @param[in] mod Matching node module, NULL for any. |
| 5557 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5558 | * @param[in] options XPath options. |
| 5559 | * @return LY_ERR |
| 5560 | */ |
| 5561 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5562 | moveto_scnode(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] | 5563 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5564 | int idx; |
| 5565 | uint8_t temp_ctx = 0; |
| 5566 | uint32_t getnext_opts; |
| 5567 | uint32_t orig_used, i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5568 | uint32_t mod_idx; |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 5569 | const struct lysc_node *iter, *start_parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5570 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5571 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5572 | return LY_SUCCESS; |
| 5573 | } |
| 5574 | |
| 5575 | if (set->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 5576 | LOGVAL(set->ctx, LY_VLOG_LYSC, set->ctx_scnode, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5577 | return LY_EVALID; |
| 5578 | } |
| 5579 | |
Michal Vasko | cafad9d | 2019-11-07 15:20:03 +0100 | [diff] [blame] | 5580 | /* getnext opts */ |
| 5581 | getnext_opts = LYS_GETNEXT_NOSTATECHECK; |
| 5582 | if (options & LYXP_SCNODE_OUTPUT) { |
| 5583 | getnext_opts |= LYS_GETNEXT_OUTPUT; |
| 5584 | } |
| 5585 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5586 | orig_used = set->used; |
| 5587 | for (i = 0; i < orig_used; ++i) { |
| 5588 | if (set->val.scnodes[i].in_ctx != 1) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5589 | if (set->val.scnodes[i].in_ctx != -2) { |
| 5590 | continue; |
| 5591 | } |
| 5592 | |
| 5593 | /* remember context node */ |
| 5594 | set->val.scnodes[i].in_ctx = -1; |
Michal Vasko | ec4df48 | 2019-12-16 10:02:18 +0100 | [diff] [blame] | 5595 | } else { |
| 5596 | set->val.scnodes[i].in_ctx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5597 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5598 | |
| 5599 | start_parent = set->val.scnodes[i].scnode; |
| 5600 | |
| 5601 | if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5602 | /* it can actually be in any module, it's all <running>, but we know it's mod (if set), |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5603 | * so use it directly (root node itself is useless in this case) */ |
| 5604 | mod_idx = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5605 | while (mod || (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] | 5606 | iter = NULL; |
Michal Vasko | 509de4d | 2019-12-10 14:51:30 +0100 | [diff] [blame] | 5607 | /* module may not be implemented */ |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 5608 | while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5609 | if (!moveto_scnode_check(iter, set->root_type, set->context_op, ncname, mod)) { |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 5610 | idx = lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM); |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5611 | LY_CHECK_RET(idx < 0, LY_EMEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5612 | /* we need to prevent these nodes from being considered in this moveto */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5613 | if (((uint32_t)idx < orig_used) && ((uint32_t)idx > i)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5614 | set->val.scnodes[idx].in_ctx = 2; |
| 5615 | temp_ctx = 1; |
| 5616 | } |
| 5617 | } |
| 5618 | } |
| 5619 | |
| 5620 | if (!mod_idx) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5621 | /* mod was specified, we are not going through the whole context */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5622 | break; |
| 5623 | } |
| 5624 | /* next iteration */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5625 | mod = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5626 | } |
| 5627 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 5628 | } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) { |
| 5629 | iter = NULL; |
| 5630 | while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5631 | if (!moveto_scnode_check(iter, set->root_type, set->context_op, ncname, (mod ? mod : set->local_mod))) { |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 5632 | idx = lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM); |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5633 | LY_CHECK_RET(idx < 0, LY_EMEM); |
| 5634 | if (((uint32_t)idx < orig_used) && ((uint32_t)idx > i)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5635 | set->val.scnodes[idx].in_ctx = 2; |
| 5636 | temp_ctx = 1; |
| 5637 | } |
| 5638 | } |
| 5639 | } |
| 5640 | } |
| 5641 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5642 | |
| 5643 | /* correct temporary in_ctx values */ |
| 5644 | if (temp_ctx) { |
| 5645 | for (i = 0; i < orig_used; ++i) { |
| 5646 | if (set->val.scnodes[i].in_ctx == 2) { |
| 5647 | set->val.scnodes[i].in_ctx = 1; |
| 5648 | } |
| 5649 | } |
| 5650 | } |
| 5651 | |
| 5652 | return LY_SUCCESS; |
| 5653 | } |
| 5654 | |
| 5655 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5656 | * @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] | 5657 | * Context position aware. |
| 5658 | * |
| 5659 | * @param[in] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5660 | * @param[in] mod Matching node module, NULL for any. |
| 5661 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5662 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 5663 | */ |
| 5664 | static LY_ERR |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5665 | moveto_node_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5666 | { |
| 5667 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5668 | const struct lyd_node *next, *elem, *start; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5669 | struct lyxp_set ret_set; |
| 5670 | LY_ERR rc; |
| 5671 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5672 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5673 | return LY_SUCCESS; |
| 5674 | } |
| 5675 | |
| 5676 | if (set->type != LYXP_SET_NODE_SET) { |
| 5677 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 5678 | return LY_EVALID; |
| 5679 | } |
| 5680 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5681 | /* replace the original nodes (and throws away all text and meta nodes, root is replaced by a child) */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5682 | rc = moveto_node(set, NULL, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5683 | LY_CHECK_RET(rc); |
| 5684 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5685 | /* 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] | 5686 | set_init(&ret_set, set); |
| 5687 | for (i = 0; i < set->used; ++i) { |
| 5688 | |
| 5689 | /* TREE DFS */ |
| 5690 | start = set->val.nodes[i].node; |
| 5691 | for (elem = next = start; elem; elem = next) { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5692 | rc = moveto_node_check(elem, set->root_type, set->context_op, ncname, mod); |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5693 | if (!rc) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5694 | /* add matching node into result set */ |
| 5695 | set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used); |
| 5696 | if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) { |
| 5697 | /* the node is a duplicate, we'll process it later in the set */ |
| 5698 | goto skip_children; |
| 5699 | } |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5700 | } else if (rc == LY_EINCOMPLETE) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5701 | return rc; |
| 5702 | } else if (rc == LY_EINVAL) { |
| 5703 | goto skip_children; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5704 | } |
| 5705 | |
| 5706 | /* TREE DFS NEXT ELEM */ |
| 5707 | /* select element for the next run - children first */ |
Michal Vasko | 5bfd4be | 2020-06-23 13:26:19 +0200 | [diff] [blame] | 5708 | next = lyd_node_children(elem, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5709 | if (!next) { |
| 5710 | skip_children: |
| 5711 | /* no children, so try siblings, but only if it's not the start, |
| 5712 | * that is considered to be the root and it's siblings are not traversed */ |
| 5713 | if (elem != start) { |
| 5714 | next = elem->next; |
| 5715 | } else { |
| 5716 | break; |
| 5717 | } |
| 5718 | } |
| 5719 | while (!next) { |
| 5720 | /* no siblings, go back through the parents */ |
| 5721 | if ((struct lyd_node *)elem->parent == start) { |
| 5722 | /* we are done, no next element to process */ |
| 5723 | break; |
| 5724 | } |
| 5725 | /* parent is already processed, go to its sibling */ |
| 5726 | elem = (struct lyd_node *)elem->parent; |
| 5727 | next = elem->next; |
| 5728 | } |
| 5729 | } |
| 5730 | } |
| 5731 | |
| 5732 | /* make the temporary set the current one */ |
| 5733 | ret_set.ctx_pos = set->ctx_pos; |
| 5734 | ret_set.ctx_size = set->ctx_size; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5735 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5736 | memcpy(set, &ret_set, sizeof *set); |
| 5737 | |
| 5738 | return LY_SUCCESS; |
| 5739 | } |
| 5740 | |
| 5741 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5742 | * @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] | 5743 | * |
| 5744 | * @param[in] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5745 | * @param[in] mod Matching node module, NULL for any. |
| 5746 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5747 | * @param[in] options XPath options. |
| 5748 | * @return LY_ERR |
| 5749 | */ |
| 5750 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5751 | moveto_scnode_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] | 5752 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5753 | uint32_t i, orig_used; |
| 5754 | int idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5755 | const struct lysc_node *next, *elem, *start; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5756 | LY_ERR rc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5757 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5758 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5759 | return LY_SUCCESS; |
| 5760 | } |
| 5761 | |
| 5762 | if (set->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 5763 | LOGVAL(set->ctx, LY_VLOG_LYSC, set->ctx_scnode, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5764 | return LY_EVALID; |
| 5765 | } |
| 5766 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5767 | orig_used = set->used; |
| 5768 | for (i = 0; i < orig_used; ++i) { |
| 5769 | if (set->val.scnodes[i].in_ctx != 1) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5770 | if (set->val.scnodes[i].in_ctx != -2) { |
| 5771 | continue; |
| 5772 | } |
| 5773 | |
| 5774 | /* remember context node */ |
| 5775 | set->val.scnodes[i].in_ctx = -1; |
Michal Vasko | ec4df48 | 2019-12-16 10:02:18 +0100 | [diff] [blame] | 5776 | } else { |
| 5777 | set->val.scnodes[i].in_ctx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5778 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5779 | |
| 5780 | /* TREE DFS */ |
| 5781 | start = set->val.scnodes[i].scnode; |
| 5782 | for (elem = next = start; elem; elem = next) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5783 | if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) { |
| 5784 | /* schema-only nodes, skip root */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5785 | goto next_iter; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5786 | } |
| 5787 | |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5788 | rc = moveto_scnode_check(elem, set->root_type, set->context_op, ncname, mod); |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5789 | if (!rc) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 5790 | if ((idx = lyxp_set_scnode_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) > -1) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5791 | set->val.scnodes[idx].in_ctx = 1; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5792 | if ((uint32_t)idx > i) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5793 | /* we will process it later in the set */ |
| 5794 | goto skip_children; |
| 5795 | } |
| 5796 | } else { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 5797 | lyxp_set_scnode_insert_node(set, elem, LYXP_NODE_ELEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5798 | } |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5799 | } else if (rc == LY_EINVAL) { |
| 5800 | goto skip_children; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5801 | } |
| 5802 | |
| 5803 | next_iter: |
| 5804 | /* TREE DFS NEXT ELEM */ |
| 5805 | /* select element for the next run - children first */ |
| 5806 | next = lysc_node_children(elem, options & LYXP_SCNODE_OUTPUT ? LYS_CONFIG_R : LYS_CONFIG_W); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5807 | if (!next) { |
| 5808 | skip_children: |
| 5809 | /* no children, so try siblings, but only if it's not the start, |
| 5810 | * that is considered to be the root and it's siblings are not traversed */ |
| 5811 | if (elem != start) { |
| 5812 | next = elem->next; |
| 5813 | } else { |
| 5814 | break; |
| 5815 | } |
| 5816 | } |
| 5817 | while (!next) { |
| 5818 | /* no siblings, go back through the parents */ |
| 5819 | if (elem->parent == start) { |
| 5820 | /* we are done, no next element to process */ |
| 5821 | break; |
| 5822 | } |
| 5823 | /* parent is already processed, go to its sibling */ |
| 5824 | elem = elem->parent; |
| 5825 | next = elem->next; |
| 5826 | } |
| 5827 | } |
| 5828 | } |
| 5829 | |
| 5830 | return LY_SUCCESS; |
| 5831 | } |
| 5832 | |
| 5833 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5834 | * @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] | 5835 | * Indirectly context position aware. |
| 5836 | * |
| 5837 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5838 | * @param[in] mod Matching metadata module, NULL for any. |
| 5839 | * @param[in] ncname Matching metadata name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5840 | * @return LY_ERR |
| 5841 | */ |
| 5842 | static LY_ERR |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5843 | 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] | 5844 | { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5845 | struct lyd_meta *sub; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5846 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5847 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5848 | return LY_SUCCESS; |
| 5849 | } |
| 5850 | |
| 5851 | if (set->type != LYXP_SET_NODE_SET) { |
| 5852 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 5853 | return LY_EVALID; |
| 5854 | } |
| 5855 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5856 | for (uint32_t i = 0; i < set->used; ) { |
| 5857 | uint8_t replaced = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5858 | |
| 5859 | /* only attributes of an elem (not dummy) can be in the result, skip all the rest; |
| 5860 | * our attributes are always qualified */ |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5861 | if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5862 | for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5863 | |
| 5864 | /* check "namespace" */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5865 | if (mod && (sub->annotation->module != mod)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5866 | continue; |
| 5867 | } |
| 5868 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5869 | if (!ncname || (sub->name == ncname)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5870 | /* match */ |
| 5871 | if (!replaced) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5872 | set->val.meta[i].meta = sub; |
| 5873 | set->val.meta[i].type = LYXP_NODE_META; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5874 | /* pos does not change */ |
| 5875 | replaced = 1; |
| 5876 | } else { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5877 | 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] | 5878 | } |
| 5879 | ++i; |
| 5880 | } |
| 5881 | } |
| 5882 | } |
| 5883 | |
| 5884 | if (!replaced) { |
| 5885 | /* no match */ |
| 5886 | set_remove_node(set, i); |
| 5887 | } |
| 5888 | } |
| 5889 | |
| 5890 | return LY_SUCCESS; |
| 5891 | } |
| 5892 | |
| 5893 | /** |
| 5894 | * @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] | 5895 | * Result is LYXP_SET_NODE_SET. Context position aware. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5896 | * |
| 5897 | * @param[in,out] set1 Set to use for the result. |
| 5898 | * @param[in] set2 Set that is copied to @p set1. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5899 | * @return LY_ERR |
| 5900 | */ |
| 5901 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5902 | moveto_union(struct lyxp_set *set1, struct lyxp_set *set2) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5903 | { |
| 5904 | LY_ERR rc; |
| 5905 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5906 | if ((set1->type != LYXP_SET_NODE_SET) || (set2->type != LYXP_SET_NODE_SET)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5907 | LOGVAL(set1->ctx, LY_VLOG_LYD, set1->ctx_node, LY_VCODE_XP_INOP_2, "union", print_set_type(set1), print_set_type(set2)); |
| 5908 | return LY_EVALID; |
| 5909 | } |
| 5910 | |
| 5911 | /* set2 is empty or both set1 and set2 */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5912 | if (!set2->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5913 | return LY_SUCCESS; |
| 5914 | } |
| 5915 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5916 | if (!set1->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5917 | memcpy(set1, set2, sizeof *set1); |
| 5918 | /* dynamic memory belongs to set1 now, do not free */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5919 | memset(set2, 0, sizeof *set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5920 | return LY_SUCCESS; |
| 5921 | } |
| 5922 | |
| 5923 | /* we assume sets are sorted */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5924 | assert(!set_sort(set1) && !set_sort(set2)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5925 | |
| 5926 | /* sort, remove duplicates */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5927 | rc = set_sorted_merge(set1, set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5928 | LY_CHECK_RET(rc); |
| 5929 | |
| 5930 | /* final set must be sorted */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5931 | assert(!set_sort(set1)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5932 | |
| 5933 | return LY_SUCCESS; |
| 5934 | } |
| 5935 | |
| 5936 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5937 | * @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] | 5938 | * Context position aware. |
| 5939 | * |
| 5940 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5941 | * @param[in] mod Matching metadata module, NULL for any. |
| 5942 | * @param[in] ncname Matching metadata name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5943 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 5944 | */ |
| 5945 | static int |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5946 | moveto_attr_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5947 | { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5948 | struct lyd_meta *sub; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5949 | struct lyxp_set *set_all_desc = NULL; |
| 5950 | LY_ERR rc; |
| 5951 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5952 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5953 | return LY_SUCCESS; |
| 5954 | } |
| 5955 | |
| 5956 | if (set->type != LYXP_SET_NODE_SET) { |
| 5957 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 5958 | return LY_EVALID; |
| 5959 | } |
| 5960 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5961 | /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory, |
| 5962 | * but it likely won't be used much, so it's a waste of time */ |
| 5963 | /* copy the context */ |
| 5964 | set_all_desc = set_copy(set); |
| 5965 | /* get all descendant nodes (the original context nodes are removed) */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5966 | rc = moveto_node_alldesc(set_all_desc, NULL, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5967 | if (rc != LY_SUCCESS) { |
| 5968 | lyxp_set_free(set_all_desc); |
| 5969 | return rc; |
| 5970 | } |
| 5971 | /* prepend the original context nodes */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5972 | rc = moveto_union(set, set_all_desc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5973 | if (rc != LY_SUCCESS) { |
| 5974 | lyxp_set_free(set_all_desc); |
| 5975 | return rc; |
| 5976 | } |
| 5977 | lyxp_set_free(set_all_desc); |
| 5978 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5979 | for (uint32_t i = 0; i < set->used; ) { |
| 5980 | uint8_t replaced = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5981 | |
| 5982 | /* only attributes of an elem can be in the result, skip all the rest, |
| 5983 | * we have all attributes qualified in lyd tree */ |
| 5984 | if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5985 | for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5986 | /* check "namespace" */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5987 | if (mod && (sub->annotation->module != mod)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5988 | continue; |
| 5989 | } |
| 5990 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5991 | if (!ncname || (sub->name == ncname)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5992 | /* match */ |
| 5993 | if (!replaced) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5994 | set->val.meta[i].meta = sub; |
| 5995 | set->val.meta[i].type = LYXP_NODE_META; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5996 | /* pos does not change */ |
| 5997 | replaced = 1; |
| 5998 | } else { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5999 | 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] | 6000 | } |
| 6001 | ++i; |
| 6002 | } |
| 6003 | } |
| 6004 | } |
| 6005 | |
| 6006 | if (!replaced) { |
| 6007 | /* no match */ |
| 6008 | set_remove_node(set, i); |
| 6009 | } |
| 6010 | } |
| 6011 | |
| 6012 | return LY_SUCCESS; |
| 6013 | } |
| 6014 | |
| 6015 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6016 | * @brief Move context @p set to self and al chilren, recursively. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET. |
| 6017 | * Context position aware. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6018 | * |
| 6019 | * @param[in] parent Current parent. |
| 6020 | * @param[in] parent_pos Position of @p parent. |
| 6021 | * @param[in] parent_type Node type of @p parent. |
| 6022 | * @param[in,out] to_set Set to use. |
| 6023 | * @param[in] dup_check_set Set for checking duplicities. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6024 | * @param[in] options XPath options. |
| 6025 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6026 | */ |
| 6027 | static LY_ERR |
| 6028 | 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] | 6029 | 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] | 6030 | { |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6031 | const struct lyd_node *iter, *first; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6032 | LY_ERR rc; |
| 6033 | |
| 6034 | switch (parent_type) { |
| 6035 | case LYXP_NODE_ROOT: |
| 6036 | case LYXP_NODE_ROOT_CONFIG: |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6037 | assert(!parent); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6038 | |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6039 | /* add all top-level nodes as elements */ |
| 6040 | first = to_set->tree; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6041 | break; |
| 6042 | case LYXP_NODE_ELEM: |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6043 | /* add just the text node of this term element node */ |
| 6044 | if (parent->schema->nodetype & (LYD_NODE_TERM | LYD_NODE_ANY)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6045 | if (!set_dup_node_check(dup_check_set, parent, LYXP_NODE_TEXT, -1)) { |
| 6046 | set_insert_node(to_set, parent, parent_pos, LYXP_NODE_TEXT, to_set->used); |
| 6047 | } |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6048 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6049 | } |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6050 | |
| 6051 | /* add all the children of this node */ |
Michal Vasko | 5bfd4be | 2020-06-23 13:26:19 +0200 | [diff] [blame] | 6052 | first = lyd_node_children(parent, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6053 | break; |
| 6054 | default: |
| 6055 | LOGINT_RET(parent->schema->module->ctx); |
| 6056 | } |
| 6057 | |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6058 | /* add all top-level nodes as elements */ |
| 6059 | LY_LIST_FOR(first, iter) { |
| 6060 | /* context check */ |
| 6061 | if ((parent_type == LYXP_NODE_ROOT_CONFIG) && (iter->schema->flags & LYS_CONFIG_R)) { |
| 6062 | continue; |
| 6063 | } |
| 6064 | |
| 6065 | /* when check */ |
| 6066 | if (moveto_when_check(iter)) { |
| 6067 | return LY_EINCOMPLETE; |
| 6068 | } |
| 6069 | |
| 6070 | if (!set_dup_node_check(dup_check_set, iter, LYXP_NODE_ELEM, -1)) { |
| 6071 | set_insert_node(to_set, iter, 0, LYXP_NODE_ELEM, to_set->used); |
| 6072 | |
| 6073 | /* also add all the children of this node, recursively */ |
| 6074 | rc = moveto_self_add_children_r(iter, 0, LYXP_NODE_ELEM, to_set, dup_check_set, options); |
| 6075 | LY_CHECK_RET(rc); |
| 6076 | } |
| 6077 | } |
| 6078 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6079 | return LY_SUCCESS; |
| 6080 | } |
| 6081 | |
| 6082 | /** |
| 6083 | * @brief Move context @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET |
| 6084 | * (or LYXP_SET_EMPTY). Context position aware. |
| 6085 | * |
| 6086 | * @param[in,out] set Set to use. |
| 6087 | * @param[in] all_desc Whether to go to all descendants ('//') or not ('/'). |
| 6088 | * @param[in] options XPath options. |
| 6089 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6090 | */ |
| 6091 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6092 | moveto_self(struct lyxp_set *set, uint8_t all_desc, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6093 | { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6094 | struct lyxp_set ret_set; |
| 6095 | LY_ERR rc; |
| 6096 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6097 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6098 | return LY_SUCCESS; |
| 6099 | } |
| 6100 | |
| 6101 | if (set->type != LYXP_SET_NODE_SET) { |
| 6102 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 6103 | return LY_EVALID; |
| 6104 | } |
| 6105 | |
| 6106 | /* nothing to do */ |
| 6107 | if (!all_desc) { |
| 6108 | return LY_SUCCESS; |
| 6109 | } |
| 6110 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6111 | /* add all the children, they get added recursively */ |
| 6112 | set_init(&ret_set, set); |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6113 | for (uint32_t i = 0; i < set->used; ++i) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6114 | /* copy the current node to tmp */ |
| 6115 | set_insert_node(&ret_set, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, ret_set.used); |
| 6116 | |
| 6117 | /* do not touch attributes and text nodes */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6118 | 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] | 6119 | continue; |
| 6120 | } |
| 6121 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6122 | /* add all the children */ |
| 6123 | 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 | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6124 | set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6125 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6126 | lyxp_set_free_content(&ret_set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6127 | return rc; |
| 6128 | } |
| 6129 | } |
| 6130 | |
| 6131 | /* use the temporary set as the current one */ |
| 6132 | ret_set.ctx_pos = set->ctx_pos; |
| 6133 | ret_set.ctx_size = set->ctx_size; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6134 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6135 | memcpy(set, &ret_set, sizeof *set); |
| 6136 | |
| 6137 | return LY_SUCCESS; |
| 6138 | } |
| 6139 | |
| 6140 | /** |
| 6141 | * @brief Move context schema @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_SCNODE_SET |
| 6142 | * (or LYXP_SET_EMPTY). |
| 6143 | * |
| 6144 | * @param[in,out] set Set to use. |
| 6145 | * @param[in] all_desc Whether to go to all descendants ('//') or not ('/'). |
| 6146 | * @param[in] options XPath options. |
| 6147 | * @return LY_ERR |
| 6148 | */ |
| 6149 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6150 | moveto_scnode_self(struct lyxp_set *set, uint8_t all_desc, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6151 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6152 | uint32_t getnext_opts; |
| 6153 | uint32_t mod_idx; |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6154 | const struct lysc_node *iter, *start_parent; |
| 6155 | const struct lys_module *mod; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6156 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6157 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6158 | return LY_SUCCESS; |
| 6159 | } |
| 6160 | |
| 6161 | if (set->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 6162 | LOGVAL(set->ctx, LY_VLOG_LYSC, set->ctx_scnode, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6163 | return LY_EVALID; |
| 6164 | } |
| 6165 | |
| 6166 | /* nothing to do */ |
| 6167 | if (!all_desc) { |
| 6168 | return LY_SUCCESS; |
| 6169 | } |
| 6170 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6171 | /* getnext opts */ |
| 6172 | getnext_opts = LYS_GETNEXT_NOSTATECHECK; |
| 6173 | if (options & LYXP_SCNODE_OUTPUT) { |
| 6174 | getnext_opts |= LYS_GETNEXT_OUTPUT; |
| 6175 | } |
| 6176 | |
| 6177 | /* 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] | 6178 | for (uint32_t i = 0; i < set->used; ++i) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6179 | if (set->val.scnodes[i].in_ctx != 1) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6180 | if (set->val.scnodes[i].in_ctx != -2) { |
| 6181 | continue; |
| 6182 | } |
| 6183 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6184 | /* remember context node */ |
| 6185 | set->val.scnodes[i].in_ctx = -1; |
| 6186 | } else { |
| 6187 | set->val.scnodes[i].in_ctx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6188 | } |
| 6189 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6190 | start_parent = set->val.scnodes[i].scnode; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6191 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6192 | if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) { |
| 6193 | /* it can actually be in any module, it's all <running> */ |
| 6194 | mod_idx = 0; |
| 6195 | while ((mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) { |
| 6196 | iter = NULL; |
| 6197 | /* module may not be implemented */ |
| 6198 | while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) { |
| 6199 | /* context check */ |
| 6200 | if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) { |
| 6201 | continue; |
| 6202 | } |
| 6203 | |
| 6204 | lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM); |
| 6205 | /* throw away the insert index, we want to consider that node again, recursively */ |
| 6206 | } |
| 6207 | } |
| 6208 | |
| 6209 | } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) { |
| 6210 | iter = NULL; |
| 6211 | while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6212 | /* context check */ |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6213 | 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] | 6214 | continue; |
| 6215 | } |
| 6216 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6217 | lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6218 | } |
| 6219 | } |
| 6220 | } |
| 6221 | |
| 6222 | return LY_SUCCESS; |
| 6223 | } |
| 6224 | |
| 6225 | /** |
| 6226 | * @brief Move context @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_NODE_SET |
| 6227 | * (or LYXP_SET_EMPTY). Context position aware. |
| 6228 | * |
| 6229 | * @param[in] set Set to use. |
| 6230 | * @param[in] all_desc Whether to go to all descendants ('//') or not ('/'). |
| 6231 | * @param[in] options XPath options. |
| 6232 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6233 | */ |
| 6234 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6235 | moveto_parent(struct lyxp_set *set, uint8_t all_desc, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6236 | { |
| 6237 | LY_ERR rc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6238 | struct lyd_node *node, *new_node; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6239 | enum lyxp_node_type new_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6240 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6241 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6242 | return LY_SUCCESS; |
| 6243 | } |
| 6244 | |
| 6245 | if (set->type != LYXP_SET_NODE_SET) { |
| 6246 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 6247 | return LY_EVALID; |
| 6248 | } |
| 6249 | |
| 6250 | if (all_desc) { |
| 6251 | /* <path>//.. == <path>//./.. */ |
| 6252 | rc = moveto_self(set, 1, options); |
| 6253 | LY_CHECK_RET(rc); |
| 6254 | } |
| 6255 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6256 | for (uint32_t i = 0; i < set->used; ++i) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6257 | node = set->val.nodes[i].node; |
| 6258 | |
| 6259 | if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
| 6260 | new_node = (struct lyd_node *)node->parent; |
| 6261 | } else if (set->val.nodes[i].type == LYXP_NODE_TEXT) { |
| 6262 | new_node = node; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6263 | } else if (set->val.nodes[i].type == LYXP_NODE_META) { |
| 6264 | new_node = set->val.meta[i].meta->parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6265 | if (!new_node) { |
| 6266 | LOGINT_RET(set->ctx); |
| 6267 | } |
| 6268 | } else { |
| 6269 | /* root does not have a parent */ |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6270 | set_remove_node_none(set, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6271 | continue; |
| 6272 | } |
| 6273 | |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 6274 | /* when check */ |
| 6275 | if (moveto_when_check(new_node)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6276 | return LY_EINCOMPLETE; |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 6277 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6278 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6279 | if (!new_node) { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 6280 | /* node already there can also be the root */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6281 | new_type = set->root_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6282 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6283 | } else { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 6284 | /* 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] | 6285 | new_type = LYXP_NODE_ELEM; |
| 6286 | } |
| 6287 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6288 | if (set_dup_node_check(set, new_node, new_type, -1)) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6289 | set_remove_node_none(set, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6290 | } else { |
| 6291 | set_replace_node(set, new_node, 0, new_type, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6292 | } |
| 6293 | } |
| 6294 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6295 | set_remove_nodes_none(set); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6296 | assert(!set_sort(set) && !set_sorted_dup_node_clean(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6297 | |
| 6298 | return LY_SUCCESS; |
| 6299 | } |
| 6300 | |
| 6301 | /** |
| 6302 | * @brief Move context schema @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_SCNODE_SET |
| 6303 | * (or LYXP_SET_EMPTY). |
| 6304 | * |
| 6305 | * @param[in] set Set to use. |
| 6306 | * @param[in] all_desc Whether to go to all descendants ('//') or not ('/'). |
| 6307 | * @param[in] options XPath options. |
| 6308 | * @return LY_ERR |
| 6309 | */ |
| 6310 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6311 | moveto_scnode_parent(struct lyxp_set *set, uint8_t all_desc, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6312 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6313 | int idx; |
| 6314 | uint32_t i, orig_used; |
| 6315 | uint8_t temp_ctx = 0; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6316 | const struct lysc_node *node, *new_node; |
| 6317 | enum lyxp_node_type new_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6318 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6319 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6320 | return LY_SUCCESS; |
| 6321 | } |
| 6322 | |
| 6323 | if (set->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 6324 | LOGVAL(set->ctx, LY_VLOG_LYSC, set->ctx_scnode, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6325 | return LY_EVALID; |
| 6326 | } |
| 6327 | |
| 6328 | if (all_desc) { |
| 6329 | /* <path>//.. == <path>//./.. */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6330 | LY_CHECK_RET(moveto_scnode_self(set, 1, options)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6331 | } |
| 6332 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6333 | orig_used = set->used; |
| 6334 | for (i = 0; i < orig_used; ++i) { |
| 6335 | if (set->val.scnodes[i].in_ctx != 1) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6336 | if (set->val.scnodes[i].in_ctx != -2) { |
| 6337 | continue; |
| 6338 | } |
| 6339 | |
| 6340 | /* remember context node */ |
| 6341 | set->val.scnodes[i].in_ctx = -1; |
Michal Vasko | ec4df48 | 2019-12-16 10:02:18 +0100 | [diff] [blame] | 6342 | } else { |
| 6343 | set->val.scnodes[i].in_ctx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6344 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6345 | |
| 6346 | node = set->val.scnodes[i].scnode; |
| 6347 | |
| 6348 | if (set->val.scnodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6349 | new_node = lysc_data_parent(node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6350 | } else { |
| 6351 | /* root does not have a parent */ |
| 6352 | continue; |
| 6353 | } |
| 6354 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6355 | if (!new_node) { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 6356 | /* node has no parent */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6357 | new_type = set->root_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6358 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6359 | } else { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 6360 | /* 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] | 6361 | new_type = LYXP_NODE_ELEM; |
| 6362 | } |
| 6363 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6364 | idx = lyxp_set_scnode_insert_node(set, new_node, new_type); |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6365 | LY_CHECK_RET(idx < 0, LY_EMEM); |
| 6366 | if (((uint32_t)idx < orig_used) && ((uint32_t)idx > i)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6367 | set->val.scnodes[idx].in_ctx = 2; |
| 6368 | temp_ctx = 1; |
| 6369 | } |
| 6370 | } |
| 6371 | |
| 6372 | if (temp_ctx) { |
| 6373 | for (i = 0; i < orig_used; ++i) { |
| 6374 | if (set->val.scnodes[i].in_ctx == 2) { |
| 6375 | set->val.scnodes[i].in_ctx = 1; |
| 6376 | } |
| 6377 | } |
| 6378 | } |
| 6379 | |
| 6380 | return LY_SUCCESS; |
| 6381 | } |
| 6382 | |
| 6383 | /** |
| 6384 | * @brief Move context @p set to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'. |
| 6385 | * Result is LYXP_SET_BOOLEAN. Indirectly context position aware. |
| 6386 | * |
| 6387 | * @param[in,out] set1 Set to use for the result. |
| 6388 | * @param[in] set2 Set acting as the second operand for @p op. |
| 6389 | * @param[in] op Comparison operator to process. |
| 6390 | * @param[in] options XPath options. |
| 6391 | * @return LY_ERR |
| 6392 | */ |
| 6393 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6394 | 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] | 6395 | { |
| 6396 | /* |
| 6397 | * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING |
| 6398 | * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING) |
| 6399 | * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 6400 | * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 6401 | * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING |
| 6402 | * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6403 | * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 6404 | * |
| 6405 | * '=' or '!=' |
| 6406 | * BOOLEAN + BOOLEAN |
| 6407 | * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 6408 | * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 6409 | * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 6410 | * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 6411 | * NUMBER + NUMBER |
| 6412 | * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6413 | * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 6414 | * STRING + STRING |
| 6415 | * |
| 6416 | * '<=', '<', '>=', '>' |
| 6417 | * NUMBER + NUMBER |
| 6418 | * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 6419 | * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 6420 | * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 6421 | * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6422 | * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 6423 | * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 6424 | * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6425 | * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6426 | */ |
| 6427 | struct lyxp_set iter1, iter2; |
| 6428 | int result; |
| 6429 | int64_t i; |
| 6430 | LY_ERR rc; |
| 6431 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6432 | memset(&iter1, 0, sizeof iter1); |
| 6433 | memset(&iter2, 0, sizeof iter2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6434 | |
| 6435 | /* iterative evaluation with node-sets */ |
| 6436 | if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) { |
| 6437 | if (set1->type == LYXP_SET_NODE_SET) { |
| 6438 | for (i = 0; i < set1->used; ++i) { |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6439 | /* cast set1 */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6440 | switch (set2->type) { |
| 6441 | case LYXP_SET_NUMBER: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6442 | rc = set_comp_cast(&iter1, set1, LYXP_SET_NUMBER, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6443 | break; |
| 6444 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6445 | rc = set_comp_cast(&iter1, set1, LYXP_SET_BOOLEAN, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6446 | break; |
| 6447 | default: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6448 | rc = set_comp_cast(&iter1, set1, LYXP_SET_STRING, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6449 | break; |
| 6450 | } |
| 6451 | LY_CHECK_RET(rc); |
| 6452 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6453 | /* canonize set2 */ |
| 6454 | LY_CHECK_ERR_RET(rc = set_comp_canonize(&iter2, set2, &set1->val.nodes[i]), lyxp_set_free_content(&iter1), rc); |
| 6455 | |
| 6456 | /* compare recursively */ |
| 6457 | rc = moveto_op_comp(&iter1, &iter2, op, options); |
| 6458 | lyxp_set_free_content(&iter2); |
| 6459 | LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6460 | |
| 6461 | /* lazy evaluation until true */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6462 | if (iter1.val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6463 | set_fill_boolean(set1, 1); |
| 6464 | return LY_SUCCESS; |
| 6465 | } |
| 6466 | } |
| 6467 | } else { |
| 6468 | for (i = 0; i < set2->used; ++i) { |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6469 | /* set set2 */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6470 | switch (set1->type) { |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6471 | case LYXP_SET_NUMBER: |
| 6472 | rc = set_comp_cast(&iter2, set2, LYXP_SET_NUMBER, i); |
| 6473 | break; |
| 6474 | case LYXP_SET_BOOLEAN: |
| 6475 | rc = set_comp_cast(&iter2, set2, LYXP_SET_BOOLEAN, i); |
| 6476 | break; |
| 6477 | default: |
| 6478 | rc = set_comp_cast(&iter2, set2, LYXP_SET_STRING, i); |
| 6479 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6480 | } |
| 6481 | LY_CHECK_RET(rc); |
| 6482 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6483 | /* canonize set1 */ |
| 6484 | 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] | 6485 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6486 | /* compare recursively */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6487 | rc = moveto_op_comp(&iter1, &iter2, op, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6488 | lyxp_set_free_content(&iter2); |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6489 | LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6490 | |
| 6491 | /* lazy evaluation until true */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6492 | if (iter1.val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6493 | set_fill_boolean(set1, 1); |
| 6494 | return LY_SUCCESS; |
| 6495 | } |
| 6496 | } |
| 6497 | } |
| 6498 | |
| 6499 | /* false for all nodes */ |
| 6500 | set_fill_boolean(set1, 0); |
| 6501 | return LY_SUCCESS; |
| 6502 | } |
| 6503 | |
| 6504 | /* first convert properly */ |
| 6505 | if ((op[0] == '=') || (op[0] == '!')) { |
| 6506 | if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6507 | lyxp_set_cast(set1, LYXP_SET_BOOLEAN); |
| 6508 | lyxp_set_cast(set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6509 | } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6510 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6511 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6512 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6513 | LY_CHECK_RET(rc); |
| 6514 | } /* else we have 2 strings */ |
| 6515 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6516 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6517 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6518 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6519 | LY_CHECK_RET(rc); |
| 6520 | } |
| 6521 | |
| 6522 | assert(set1->type == set2->type); |
| 6523 | |
| 6524 | /* compute result */ |
| 6525 | if (op[0] == '=') { |
| 6526 | if (set1->type == LYXP_SET_BOOLEAN) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6527 | result = (set1->val.bln == set2->val.bln); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6528 | } else if (set1->type == LYXP_SET_NUMBER) { |
| 6529 | result = (set1->val.num == set2->val.num); |
| 6530 | } else { |
| 6531 | assert(set1->type == LYXP_SET_STRING); |
Michal Vasko | ac6c72f | 2019-11-14 16:09:34 +0100 | [diff] [blame] | 6532 | result = !strcmp(set1->val.str, set2->val.str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6533 | } |
| 6534 | } else if (op[0] == '!') { |
| 6535 | if (set1->type == LYXP_SET_BOOLEAN) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6536 | result = (set1->val.bln != set2->val.bln); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6537 | } else if (set1->type == LYXP_SET_NUMBER) { |
| 6538 | result = (set1->val.num != set2->val.num); |
| 6539 | } else { |
| 6540 | assert(set1->type == LYXP_SET_STRING); |
Michal Vasko | ac6c72f | 2019-11-14 16:09:34 +0100 | [diff] [blame] | 6541 | result = !strcmp(set1->val.str, set2->val.str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6542 | } |
| 6543 | } else { |
| 6544 | assert(set1->type == LYXP_SET_NUMBER); |
| 6545 | if (op[0] == '<') { |
| 6546 | if (op[1] == '=') { |
| 6547 | result = (set1->val.num <= set2->val.num); |
| 6548 | } else { |
| 6549 | result = (set1->val.num < set2->val.num); |
| 6550 | } |
| 6551 | } else { |
| 6552 | if (op[1] == '=') { |
| 6553 | result = (set1->val.num >= set2->val.num); |
| 6554 | } else { |
| 6555 | result = (set1->val.num > set2->val.num); |
| 6556 | } |
| 6557 | } |
| 6558 | } |
| 6559 | |
| 6560 | /* assign result */ |
| 6561 | if (result) { |
| 6562 | set_fill_boolean(set1, 1); |
| 6563 | } else { |
| 6564 | set_fill_boolean(set1, 0); |
| 6565 | } |
| 6566 | |
| 6567 | return LY_SUCCESS; |
| 6568 | } |
| 6569 | |
| 6570 | /** |
| 6571 | * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div', |
| 6572 | * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware. |
| 6573 | * |
| 6574 | * @param[in,out] set1 Set to use for the result. |
| 6575 | * @param[in] set2 Set acting as the second operand for @p op. |
| 6576 | * @param[in] op Operator to process. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6577 | * @return LY_ERR |
| 6578 | */ |
| 6579 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6580 | 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] | 6581 | { |
| 6582 | LY_ERR rc; |
| 6583 | |
| 6584 | /* unary '-' */ |
| 6585 | if (!set2 && (op[0] == '-')) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6586 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6587 | LY_CHECK_RET(rc); |
| 6588 | set1->val.num *= -1; |
| 6589 | lyxp_set_free(set2); |
| 6590 | return LY_SUCCESS; |
| 6591 | } |
| 6592 | |
| 6593 | assert(set1 && set2); |
| 6594 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6595 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6596 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6597 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6598 | LY_CHECK_RET(rc); |
| 6599 | |
| 6600 | switch (op[0]) { |
| 6601 | /* '+' */ |
| 6602 | case '+': |
| 6603 | set1->val.num += set2->val.num; |
| 6604 | break; |
| 6605 | |
| 6606 | /* '-' */ |
| 6607 | case '-': |
| 6608 | set1->val.num -= set2->val.num; |
| 6609 | break; |
| 6610 | |
| 6611 | /* '*' */ |
| 6612 | case '*': |
| 6613 | set1->val.num *= set2->val.num; |
| 6614 | break; |
| 6615 | |
| 6616 | /* 'div' */ |
| 6617 | case 'd': |
| 6618 | set1->val.num /= set2->val.num; |
| 6619 | break; |
| 6620 | |
| 6621 | /* 'mod' */ |
| 6622 | case 'm': |
| 6623 | set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num); |
| 6624 | break; |
| 6625 | |
| 6626 | default: |
| 6627 | LOGINT_RET(set1->ctx); |
| 6628 | } |
| 6629 | |
| 6630 | return LY_SUCCESS; |
| 6631 | } |
| 6632 | |
| 6633 | /* |
| 6634 | * eval functions |
| 6635 | * |
| 6636 | * They execute a parsed XPath expression on some data subtree. |
| 6637 | */ |
| 6638 | |
| 6639 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6640 | * @brief Evaluate Predicate. Logs directly on error. |
| 6641 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6642 | * [9] Predicate ::= '[' Expr ']' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6643 | * |
| 6644 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6645 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6646 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 6647 | * @param[in] options XPath options. |
| 6648 | * @param[in] parent_pos_pred Whether parent predicate was a positional one. |
| 6649 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6650 | */ |
| 6651 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6652 | eval_predicate(struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, uint32_t options, uint8_t parent_pos_pred) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6653 | { |
| 6654 | LY_ERR rc; |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 6655 | uint16_t i, orig_exp; |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6656 | uint32_t orig_pos, orig_size; |
| 6657 | int32_t pred_in_ctx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6658 | struct lyxp_set set2; |
| 6659 | struct lyd_node *orig_parent; |
| 6660 | |
| 6661 | /* '[' */ |
| 6662 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6663 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 6664 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6665 | |
| 6666 | if (!set) { |
| 6667 | only_parse: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6668 | rc = eval_expr_select(exp, tok_idx, 0, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6669 | LY_CHECK_RET(rc); |
| 6670 | } else if (set->type == LYXP_SET_NODE_SET) { |
| 6671 | /* 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] | 6672 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6673 | |
| 6674 | /* empty set, nothing to evaluate */ |
| 6675 | if (!set->used) { |
| 6676 | goto only_parse; |
| 6677 | } |
| 6678 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6679 | orig_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6680 | orig_pos = 0; |
| 6681 | orig_size = set->used; |
| 6682 | orig_parent = NULL; |
Michal Vasko | 39dbf35 | 2020-05-21 10:08:59 +0200 | [diff] [blame] | 6683 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6684 | set_init(&set2, set); |
| 6685 | set_insert_node(&set2, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, 0); |
| 6686 | /* remember the node context position for position() and context size for last(), |
| 6687 | * predicates should always be evaluated with respect to the child axis (since we do |
| 6688 | * not support explicit axes) so we assign positions based on their parents */ |
| 6689 | if (parent_pos_pred && ((struct lyd_node *)set->val.nodes[i].node->parent != orig_parent)) { |
| 6690 | orig_parent = (struct lyd_node *)set->val.nodes[i].node->parent; |
| 6691 | orig_pos = 1; |
| 6692 | } else { |
| 6693 | ++orig_pos; |
| 6694 | } |
| 6695 | |
| 6696 | set2.ctx_pos = orig_pos; |
| 6697 | set2.ctx_size = orig_size; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6698 | *tok_idx = orig_exp; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6699 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6700 | rc = eval_expr_select(exp, tok_idx, 0, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6701 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6702 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6703 | return rc; |
| 6704 | } |
| 6705 | |
| 6706 | /* number is a position */ |
| 6707 | if (set2.type == LYXP_SET_NUMBER) { |
| 6708 | if ((long long)set2.val.num == orig_pos) { |
| 6709 | set2.val.num = 1; |
| 6710 | } else { |
| 6711 | set2.val.num = 0; |
| 6712 | } |
| 6713 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6714 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6715 | |
| 6716 | /* predicate satisfied or not? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6717 | if (!set2.val.bln) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6718 | set_remove_node_none(set, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6719 | } |
| 6720 | } |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6721 | set_remove_nodes_none(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6722 | |
| 6723 | } else if (set->type == LYXP_SET_SCNODE_SET) { |
| 6724 | for (i = 0; i < set->used; ++i) { |
| 6725 | if (set->val.scnodes[i].in_ctx == 1) { |
| 6726 | /* there is a currently-valid node */ |
| 6727 | break; |
| 6728 | } |
| 6729 | } |
| 6730 | /* empty set, nothing to evaluate */ |
| 6731 | if (i == set->used) { |
| 6732 | goto only_parse; |
| 6733 | } |
| 6734 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6735 | orig_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6736 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6737 | /* set special in_ctx to all the valid snodes */ |
| 6738 | pred_in_ctx = set_scnode_new_in_ctx(set); |
| 6739 | |
| 6740 | /* use the valid snodes one-by-one */ |
| 6741 | for (i = 0; i < set->used; ++i) { |
| 6742 | if (set->val.scnodes[i].in_ctx != pred_in_ctx) { |
| 6743 | continue; |
| 6744 | } |
| 6745 | set->val.scnodes[i].in_ctx = 1; |
| 6746 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6747 | *tok_idx = orig_exp; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6748 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6749 | rc = eval_expr_select(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6750 | LY_CHECK_RET(rc); |
| 6751 | |
| 6752 | set->val.scnodes[i].in_ctx = pred_in_ctx; |
| 6753 | } |
| 6754 | |
| 6755 | /* restore the state as it was before the predicate */ |
| 6756 | for (i = 0; i < set->used; ++i) { |
| 6757 | if (set->val.scnodes[i].in_ctx == 1) { |
| 6758 | set->val.scnodes[i].in_ctx = 0; |
| 6759 | } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) { |
| 6760 | set->val.scnodes[i].in_ctx = 1; |
| 6761 | } |
| 6762 | } |
| 6763 | |
| 6764 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6765 | set2.type = LYXP_SET_NODE_SET; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6766 | set_fill_set(&set2, set); |
| 6767 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6768 | rc = eval_expr_select(exp, tok_idx, 0, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6769 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6770 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6771 | return rc; |
| 6772 | } |
| 6773 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6774 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6775 | if (!set2.val.bln) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6776 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6777 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6778 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6779 | } |
| 6780 | |
| 6781 | /* ']' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6782 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6783 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6784 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 6785 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6786 | |
| 6787 | return LY_SUCCESS; |
| 6788 | } |
| 6789 | |
| 6790 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6791 | * @brief Evaluate Literal. Logs directly on error. |
| 6792 | * |
| 6793 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6794 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6795 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 6796 | */ |
| 6797 | static void |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6798 | eval_literal(struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6799 | { |
| 6800 | if (set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6801 | if (exp->tok_len[*tok_idx] == 2) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6802 | set_fill_string(set, "", 0); |
| 6803 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6804 | 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] | 6805 | } |
| 6806 | } |
| 6807 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6808 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 6809 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6810 | } |
| 6811 | |
| 6812 | /** |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6813 | * @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] | 6814 | * |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6815 | * @param[in] exp Full parsed XPath expression. |
| 6816 | * @param[in,out] tok_idx Index in @p exp at the beginning of the predicate, is updated on success. |
| 6817 | * @param[in] scnode Found schema node as context for the predicate. |
| 6818 | * @param[in] format Format of any prefixes in key names/values. |
| 6819 | * @param[out] predicates Parsed predicates. |
| 6820 | * @param[out] pred_type Type of @p predicates. |
| 6821 | * @return LY_SUCCESS on success, |
| 6822 | * @return LY_ERR on any error. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6823 | */ |
| 6824 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6825 | eval_name_test_try_compile_predicates(struct lyxp_expr *exp, uint16_t *tok_idx, const struct lysc_node *scnode, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 6826 | LY_PREFIX_FORMAT format, struct ly_path_predicate **predicates, |
| 6827 | enum ly_path_pred_type *pred_type) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6828 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6829 | LY_ERR ret = LY_SUCCESS; |
| 6830 | uint16_t key_count, e_idx, pred_idx = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6831 | const struct lysc_node *key; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6832 | size_t pred_len; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6833 | uint32_t prev_lo; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6834 | struct lyxp_expr *exp2 = NULL; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6835 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6836 | assert(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6837 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6838 | if (scnode->nodetype == LYS_LIST) { |
| 6839 | /* get key count */ |
| 6840 | if (scnode->flags & LYS_KEYLESS) { |
| 6841 | return LY_EINVAL; |
| 6842 | } |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 6843 | for (key_count = 0, key = lysc_node_children(scnode, 0); key && (key->flags & LYS_KEY); key = key->next, ++key_count) {} |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6844 | assert(key_count); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6845 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6846 | /* learn where the predicates end */ |
| 6847 | e_idx = *tok_idx; |
| 6848 | while (key_count) { |
| 6849 | /* '[' */ |
| 6850 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) { |
| 6851 | return LY_EINVAL; |
| 6852 | } |
| 6853 | ++e_idx; |
| 6854 | |
| 6855 | /* ']' */ |
| 6856 | while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) { |
| 6857 | ++e_idx; |
| 6858 | } |
| 6859 | ++e_idx; |
| 6860 | |
| 6861 | /* another presumably key predicate parsed */ |
| 6862 | --key_count; |
| 6863 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6864 | } else { |
| 6865 | /* learn just where this single predicate ends */ |
| 6866 | e_idx = *tok_idx; |
| 6867 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6868 | /* '[' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6869 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) { |
| 6870 | return LY_EINVAL; |
| 6871 | } |
| 6872 | ++e_idx; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6873 | |
| 6874 | /* ']' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6875 | while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) { |
| 6876 | ++e_idx; |
| 6877 | } |
| 6878 | ++e_idx; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6879 | } |
| 6880 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6881 | /* get the joined length of all the keys predicates/of the single leaf-list predicate */ |
| 6882 | pred_len = (exp->tok_pos[e_idx - 1] + exp->tok_len[e_idx - 1]) - exp->tok_pos[*tok_idx]; |
| 6883 | |
| 6884 | /* turn logging off */ |
| 6885 | prev_lo = ly_log_options(0); |
| 6886 | |
| 6887 | /* parse the predicate(s) */ |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 6888 | LY_CHECK_GOTO(ret = ly_path_parse_predicate(scnode->module->ctx, scnode, exp->expr + exp->tok_pos[*tok_idx], pred_len, |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6889 | LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp2), cleanup); |
| 6890 | |
| 6891 | /* compile */ |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 6892 | ret = ly_path_compile_predicate(scnode->module->ctx, scnode, scnode->module, scnode, exp2, &pred_idx, |
| 6893 | format, scnode->module, predicates, pred_type); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6894 | LY_CHECK_GOTO(ret, cleanup); |
| 6895 | |
| 6896 | /* success, the predicate must include all the needed information for hash-based search */ |
| 6897 | *tok_idx = e_idx; |
| 6898 | |
| 6899 | cleanup: |
| 6900 | ly_log_options(prev_lo); |
| 6901 | lyxp_expr_free(scnode->module->ctx, exp2); |
| 6902 | return ret; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6903 | } |
| 6904 | |
| 6905 | /** |
| 6906 | * @brief Evaluate NameTest and any following Predicates. Logs directly on error. |
| 6907 | * |
| 6908 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 6909 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 6910 | * [7] NameTest ::= '*' | NCName ':' '*' | QName |
| 6911 | * |
| 6912 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6913 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6914 | * @param[in] attr_axis Whether to search attributes or standard nodes. |
| 6915 | * @param[in] all_desc Whether to search all the descendants or children only. |
| 6916 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 6917 | * @param[in] options XPath options. |
| 6918 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6919 | */ |
| 6920 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6921 | eval_name_test_with_predicate(struct lyxp_expr *exp, uint16_t *tok_idx, uint8_t attr_axis, uint8_t all_desc, struct lyxp_set *set, |
| 6922 | uint32_t options) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6923 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6924 | char *path; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6925 | const char *ncname, *ncname_dict = NULL; |
| 6926 | uint16_t ncname_len; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6927 | const struct lys_module *moveto_mod; |
| 6928 | const struct lysc_node *scnode = NULL, *tmp; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6929 | struct ly_path_predicate *predicates = NULL; |
| 6930 | enum ly_path_pred_type pred_type = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6931 | LY_ERR rc = LY_SUCCESS; |
| 6932 | |
| 6933 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6934 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 6935 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6936 | |
| 6937 | if (!set) { |
| 6938 | goto moveto; |
| 6939 | } |
| 6940 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6941 | ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]]; |
| 6942 | ncname_len = exp->tok_len[*tok_idx - 1]; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6943 | |
| 6944 | /* parse (and skip) module name */ |
| 6945 | rc = moveto_resolve_model(&ncname, &ncname_len, set, &moveto_mod); |
| 6946 | LY_CHECK_GOTO(rc, cleanup); |
| 6947 | |
| 6948 | if (moveto_mod && !attr_axis && !all_desc && (set->type == LYXP_SET_NODE_SET)) { |
| 6949 | /* find the matching schema node in some parent in the context */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6950 | for (uint32_t i = 0; i < set->used; ++i) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6951 | if (set->val.nodes[i].type == set->root_type) { |
| 6952 | tmp = lys_find_child(NULL, moveto_mod, ncname, ncname_len, 0, 0); |
| 6953 | } else if ((set->val.nodes[i].type == LYXP_NODE_ELEM) |
| 6954 | && (!scnode || (lysc_data_parent(scnode) != set->val.nodes[i].node->schema))) { |
| 6955 | /* do not repeat the same search */ |
| 6956 | tmp = lys_find_child(set->val.nodes[i].node->schema, moveto_mod, ncname, ncname_len, 0, 0); |
Radek Krejci | b124784 | 2020-07-02 16:22:38 +0200 | [diff] [blame] | 6957 | } else { |
| 6958 | tmp = NULL; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6959 | } |
| 6960 | |
| 6961 | /* additional context check */ |
| 6962 | if (tmp && (set->root_type == LYXP_NODE_ROOT_CONFIG) && (tmp->flags & LYS_CONFIG_R)) { |
| 6963 | tmp = NULL; |
| 6964 | } |
| 6965 | |
| 6966 | if (tmp) { |
| 6967 | if (scnode) { |
| 6968 | /* we found a schema node with the same name but at different level, give up, too complicated */ |
| 6969 | scnode = NULL; |
| 6970 | break; |
| 6971 | } else { |
| 6972 | /* remember the found schema node and continue to make sure it can be used */ |
| 6973 | scnode = tmp; |
| 6974 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6975 | } |
| 6976 | } |
| 6977 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6978 | if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) { |
| 6979 | /* try to create the predicates */ |
| 6980 | if (eval_name_test_try_compile_predicates(exp, tok_idx, scnode, set->format, &predicates, &pred_type)) { |
| 6981 | /* hashes cannot be used */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6982 | scnode = NULL; |
| 6983 | } |
| 6984 | } |
| 6985 | } |
| 6986 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6987 | if (!scnode && moveto_mod) { |
| 6988 | /* we are not able to match based on a schema node and not all the modules match, |
| 6989 | * use dictionary for efficient comparison */ |
| 6990 | ncname_dict = lydict_insert(set->ctx, ncname, ncname_len); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6991 | } |
| 6992 | |
| 6993 | moveto: |
| 6994 | /* move to the attribute(s), data node(s), or schema node(s) */ |
| 6995 | if (attr_axis) { |
| 6996 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 6997 | set_scnode_clear_ctx(set); |
| 6998 | } else { |
| 6999 | if (all_desc) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7000 | rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7001 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7002 | rc = moveto_attr(set, moveto_mod, ncname_dict); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7003 | } |
| 7004 | LY_CHECK_GOTO(rc, cleanup); |
| 7005 | } |
| 7006 | } else { |
| 7007 | if (set && (options & LYXP_SCNODE_ALL)) { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7008 | int64_t i; |
| 7009 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7010 | if (all_desc) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7011 | rc = moveto_scnode_alldesc(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7012 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7013 | rc = moveto_scnode(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7014 | } |
| 7015 | LY_CHECK_GOTO(rc, cleanup); |
| 7016 | |
| 7017 | for (i = set->used - 1; i > -1; --i) { |
| 7018 | if (set->val.scnodes[i].in_ctx > 0) { |
| 7019 | break; |
| 7020 | } |
| 7021 | } |
| 7022 | if (i == -1) { |
| 7023 | path = lysc_path(set->ctx_scnode, LYSC_PATH_LOG, NULL, 0); |
| 7024 | LOGWRN(set->ctx, "Schema node \"%.*s\" not found (%.*s) with context node \"%s\".", |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 7025 | ncname_len, ncname, ncname - exp->expr, exp->expr, path); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7026 | free(path); |
| 7027 | } |
| 7028 | } else { |
| 7029 | if (all_desc) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7030 | rc = moveto_node_alldesc(set, moveto_mod, ncname_dict); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7031 | } else { |
| 7032 | if (scnode) { |
| 7033 | /* we can find the nodes using hashes */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7034 | rc = moveto_node_hash(set, scnode, predicates); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7035 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7036 | rc = moveto_node(set, moveto_mod, ncname_dict); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7037 | } |
| 7038 | } |
| 7039 | LY_CHECK_GOTO(rc, cleanup); |
| 7040 | } |
| 7041 | } |
| 7042 | |
| 7043 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7044 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 7045 | rc = eval_predicate(exp, tok_idx, set, options, 1); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7046 | LY_CHECK_RET(rc); |
| 7047 | } |
| 7048 | |
| 7049 | cleanup: |
Michal Vasko | db51a8d | 2020-05-27 15:22:29 +0200 | [diff] [blame] | 7050 | if (set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7051 | lydict_remove(set->ctx, ncname_dict); |
| 7052 | ly_path_predicates_free(set->ctx, pred_type, scnode, predicates); |
Michal Vasko | db51a8d | 2020-05-27 15:22:29 +0200 | [diff] [blame] | 7053 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7054 | return rc; |
| 7055 | } |
| 7056 | |
| 7057 | /** |
| 7058 | * @brief Evaluate NodeType and any following Predicates. Logs directly on error. |
| 7059 | * |
| 7060 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 7061 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 7062 | * [8] NodeType ::= 'text' | 'node' |
| 7063 | * |
| 7064 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7065 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7066 | * @param[in] attr_axis Whether to search attributes or standard nodes. |
| 7067 | * @param[in] all_desc Whether to search all the descendants or children only. |
| 7068 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7069 | * @param[in] options XPath options. |
| 7070 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7071 | */ |
| 7072 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7073 | eval_node_type_with_predicate(struct lyxp_expr *exp, uint16_t *tok_idx, uint8_t attr_axis, uint8_t all_desc, |
| 7074 | struct lyxp_set *set, uint32_t options) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7075 | { |
| 7076 | LY_ERR rc; |
| 7077 | |
| 7078 | /* TODO */ |
| 7079 | (void)attr_axis; |
| 7080 | (void)all_desc; |
| 7081 | |
| 7082 | if (set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7083 | assert(exp->tok_len[*tok_idx] == 4); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7084 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 7085 | set_scnode_clear_ctx(set); |
| 7086 | /* just for the debug message below */ |
| 7087 | set = NULL; |
| 7088 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7089 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7090 | rc = xpath_node(NULL, 0, set, options); |
| 7091 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7092 | assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4)); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7093 | rc = xpath_text(NULL, 0, set, options); |
| 7094 | } |
| 7095 | LY_CHECK_RET(rc); |
| 7096 | } |
| 7097 | } |
| 7098 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7099 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7100 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7101 | |
| 7102 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7103 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7104 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7105 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7106 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7107 | |
| 7108 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7109 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7110 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7111 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7112 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7113 | |
| 7114 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7115 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 7116 | rc = eval_predicate(exp, tok_idx, set, options, 1); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7117 | LY_CHECK_RET(rc); |
| 7118 | } |
| 7119 | |
| 7120 | return LY_SUCCESS; |
| 7121 | } |
| 7122 | |
| 7123 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7124 | * @brief Evaluate RelativeLocationPath. Logs directly on error. |
| 7125 | * |
| 7126 | * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step |
| 7127 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7128 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7129 | * |
| 7130 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7131 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7132 | * @param[in] all_desc Whether to search all the descendants or children only. |
| 7133 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7134 | * @param[in] options XPath options. |
| 7135 | * @return LY_ERR (YL_EINCOMPLETE on unresolved when) |
| 7136 | */ |
| 7137 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7138 | eval_relative_location_path(struct lyxp_expr *exp, uint16_t *tok_idx, uint8_t all_desc, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7139 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7140 | uint8_t attr_axis; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7141 | LY_ERR rc; |
| 7142 | |
| 7143 | goto step; |
| 7144 | do { |
| 7145 | /* evaluate '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7146 | if (exp->tok_len[*tok_idx] == 1) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7147 | all_desc = 0; |
| 7148 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7149 | assert(exp->tok_len[*tok_idx] == 2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7150 | all_desc = 1; |
| 7151 | } |
| 7152 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7153 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7154 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7155 | |
| 7156 | step: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7157 | /* evaluate abbreviated axis '@'? if any */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7158 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7159 | attr_axis = 1; |
| 7160 | |
| 7161 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7162 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7163 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7164 | } else { |
| 7165 | attr_axis = 0; |
| 7166 | } |
| 7167 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7168 | /* Step */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7169 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7170 | case LYXP_TOKEN_DOT: |
| 7171 | /* evaluate '.' */ |
| 7172 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 7173 | rc = moveto_scnode_self(set, all_desc, options); |
| 7174 | } else { |
| 7175 | rc = moveto_self(set, all_desc, options); |
| 7176 | } |
| 7177 | LY_CHECK_RET(rc); |
| 7178 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7179 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7180 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7181 | break; |
| 7182 | |
| 7183 | case LYXP_TOKEN_DDOT: |
| 7184 | /* evaluate '..' */ |
| 7185 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 7186 | rc = moveto_scnode_parent(set, all_desc, options); |
| 7187 | } else { |
| 7188 | rc = moveto_parent(set, all_desc, options); |
| 7189 | } |
| 7190 | LY_CHECK_RET(rc); |
| 7191 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7192 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7193 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7194 | break; |
| 7195 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7196 | case LYXP_TOKEN_NAMETEST: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7197 | /* evaluate NameTest Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7198 | 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] | 7199 | LY_CHECK_RET(rc); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7200 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7201 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7202 | case LYXP_TOKEN_NODETYPE: |
| 7203 | /* evaluate NodeType Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7204 | 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] | 7205 | LY_CHECK_RET(rc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7206 | break; |
| 7207 | |
| 7208 | default: |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 7209 | LOGINT_RET(set ? set->ctx : NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7210 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7211 | } 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] | 7212 | |
| 7213 | return LY_SUCCESS; |
| 7214 | } |
| 7215 | |
| 7216 | /** |
| 7217 | * @brief Evaluate AbsoluteLocationPath. Logs directly on error. |
| 7218 | * |
| 7219 | * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath |
| 7220 | * |
| 7221 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7222 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7223 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7224 | * @param[in] options XPath options. |
| 7225 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7226 | */ |
| 7227 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7228 | eval_absolute_location_path(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] | 7229 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7230 | uint8_t all_desc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7231 | LY_ERR rc; |
| 7232 | |
| 7233 | if (set) { |
| 7234 | /* no matter what tokens follow, we need to be at the root */ |
| 7235 | moveto_root(set, options); |
| 7236 | } |
| 7237 | |
| 7238 | /* '/' RelativeLocationPath? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7239 | if (exp->tok_len[*tok_idx] == 1) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7240 | /* evaluate '/' - deferred */ |
| 7241 | all_desc = 0; |
| 7242 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7243 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7244 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7245 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7246 | if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7247 | return LY_SUCCESS; |
| 7248 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7249 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7250 | case LYXP_TOKEN_DOT: |
| 7251 | case LYXP_TOKEN_DDOT: |
| 7252 | case LYXP_TOKEN_AT: |
| 7253 | case LYXP_TOKEN_NAMETEST: |
| 7254 | case LYXP_TOKEN_NODETYPE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7255 | rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7256 | LY_CHECK_RET(rc); |
| 7257 | break; |
| 7258 | default: |
| 7259 | break; |
| 7260 | } |
| 7261 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7262 | } else { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 7263 | /* '//' RelativeLocationPath */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7264 | /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */ |
| 7265 | all_desc = 1; |
| 7266 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7267 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7268 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7269 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7270 | rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7271 | LY_CHECK_RET(rc); |
| 7272 | } |
| 7273 | |
| 7274 | return LY_SUCCESS; |
| 7275 | } |
| 7276 | |
| 7277 | /** |
| 7278 | * @brief Evaluate FunctionCall. Logs directly on error. |
| 7279 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7280 | * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7281 | * |
| 7282 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7283 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7284 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7285 | * @param[in] options XPath options. |
| 7286 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7287 | */ |
| 7288 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7289 | eval_function_call(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] | 7290 | { |
| 7291 | LY_ERR rc; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7292 | 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] | 7293 | uint16_t arg_count = 0, i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7294 | struct lyxp_set **args = NULL, **args_aux; |
| 7295 | |
| 7296 | if (set) { |
| 7297 | /* FunctionName */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7298 | switch (exp->tok_len[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7299 | case 3: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7300 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7301 | xpath_func = &xpath_not; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7302 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7303 | xpath_func = &xpath_sum; |
| 7304 | } |
| 7305 | break; |
| 7306 | case 4: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7307 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7308 | xpath_func = &xpath_lang; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7309 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7310 | xpath_func = &xpath_last; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7311 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7312 | xpath_func = &xpath_name; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7313 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7314 | xpath_func = &xpath_true; |
| 7315 | } |
| 7316 | break; |
| 7317 | case 5: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7318 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7319 | xpath_func = &xpath_count; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7320 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7321 | xpath_func = &xpath_false; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7322 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7323 | xpath_func = &xpath_floor; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7324 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7325 | xpath_func = &xpath_round; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7326 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7327 | xpath_func = &xpath_deref; |
| 7328 | } |
| 7329 | break; |
| 7330 | case 6: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7331 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7332 | xpath_func = &xpath_concat; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7333 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7334 | xpath_func = &xpath_number; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7335 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7336 | xpath_func = &xpath_string; |
| 7337 | } |
| 7338 | break; |
| 7339 | case 7: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7340 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7341 | xpath_func = &xpath_boolean; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7342 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7343 | xpath_func = &xpath_ceiling; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7344 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7345 | xpath_func = &xpath_current; |
| 7346 | } |
| 7347 | break; |
| 7348 | case 8: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7349 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7350 | xpath_func = &xpath_contains; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7351 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7352 | xpath_func = &xpath_position; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7353 | } 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] | 7354 | xpath_func = &xpath_re_match; |
| 7355 | } |
| 7356 | break; |
| 7357 | case 9: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7358 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7359 | xpath_func = &xpath_substring; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7360 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7361 | xpath_func = &xpath_translate; |
| 7362 | } |
| 7363 | break; |
| 7364 | case 10: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7365 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7366 | xpath_func = &xpath_local_name; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7367 | } 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] | 7368 | xpath_func = &xpath_enum_value; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7369 | } 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] | 7370 | xpath_func = &xpath_bit_is_set; |
| 7371 | } |
| 7372 | break; |
| 7373 | case 11: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7374 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7375 | xpath_func = &xpath_starts_with; |
| 7376 | } |
| 7377 | break; |
| 7378 | case 12: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7379 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7380 | xpath_func = &xpath_derived_from; |
| 7381 | } |
| 7382 | break; |
| 7383 | case 13: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7384 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7385 | xpath_func = &xpath_namespace_uri; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7386 | } 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] | 7387 | xpath_func = &xpath_string_length; |
| 7388 | } |
| 7389 | break; |
| 7390 | case 15: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7391 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7392 | xpath_func = &xpath_normalize_space; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7393 | } 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] | 7394 | xpath_func = &xpath_substring_after; |
| 7395 | } |
| 7396 | break; |
| 7397 | case 16: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7398 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7399 | xpath_func = &xpath_substring_before; |
| 7400 | } |
| 7401 | break; |
| 7402 | case 20: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7403 | 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] | 7404 | xpath_func = &xpath_derived_from_or_self; |
| 7405 | } |
| 7406 | break; |
| 7407 | } |
| 7408 | |
| 7409 | if (!xpath_func) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7410 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INFUNC, exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7411 | return LY_EVALID; |
| 7412 | } |
| 7413 | } |
| 7414 | |
| 7415 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7416 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7417 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7418 | |
| 7419 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7420 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7421 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7422 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7423 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7424 | |
| 7425 | /* ( Expr ( ',' Expr )* )? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7426 | if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7427 | if (set) { |
| 7428 | args = malloc(sizeof *args); |
| 7429 | LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup); |
| 7430 | arg_count = 1; |
| 7431 | args[0] = set_copy(set); |
| 7432 | if (!args[0]) { |
| 7433 | rc = LY_EMEM; |
| 7434 | goto cleanup; |
| 7435 | } |
| 7436 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7437 | rc = eval_expr_select(exp, tok_idx, 0, args[0], options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7438 | LY_CHECK_GOTO(rc, cleanup); |
| 7439 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7440 | rc = eval_expr_select(exp, tok_idx, 0, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7441 | LY_CHECK_GOTO(rc, cleanup); |
| 7442 | } |
| 7443 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7444 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7445 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7446 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7447 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7448 | |
| 7449 | if (set) { |
| 7450 | ++arg_count; |
| 7451 | args_aux = realloc(args, arg_count * sizeof *args); |
| 7452 | LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup); |
| 7453 | args = args_aux; |
| 7454 | args[arg_count - 1] = set_copy(set); |
| 7455 | if (!args[arg_count - 1]) { |
| 7456 | rc = LY_EMEM; |
| 7457 | goto cleanup; |
| 7458 | } |
| 7459 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7460 | 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] | 7461 | LY_CHECK_GOTO(rc, cleanup); |
| 7462 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7463 | rc = eval_expr_select(exp, tok_idx, 0, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7464 | LY_CHECK_GOTO(rc, cleanup); |
| 7465 | } |
| 7466 | } |
| 7467 | |
| 7468 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7469 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7470 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7471 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7472 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7473 | |
| 7474 | if (set) { |
| 7475 | /* evaluate function */ |
| 7476 | rc = xpath_func(args, arg_count, set, options); |
| 7477 | |
| 7478 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7479 | /* merge all nodes from arg evaluations */ |
| 7480 | for (i = 0; i < arg_count; ++i) { |
| 7481 | set_scnode_clear_ctx(args[i]); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7482 | lyxp_set_scnode_merge(set, args[i]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7483 | } |
| 7484 | } |
| 7485 | } else { |
| 7486 | rc = LY_SUCCESS; |
| 7487 | } |
| 7488 | |
| 7489 | cleanup: |
| 7490 | for (i = 0; i < arg_count; ++i) { |
| 7491 | lyxp_set_free(args[i]); |
| 7492 | } |
| 7493 | free(args); |
| 7494 | |
| 7495 | return rc; |
| 7496 | } |
| 7497 | |
| 7498 | /** |
| 7499 | * @brief Evaluate Number. Logs directly on error. |
| 7500 | * |
| 7501 | * @param[in] ctx Context for errors. |
| 7502 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7503 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7504 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7505 | * @return LY_ERR |
| 7506 | */ |
| 7507 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7508 | eval_number(struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7509 | { |
| 7510 | long double num; |
| 7511 | char *endptr; |
| 7512 | |
| 7513 | if (set) { |
| 7514 | errno = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7515 | num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7516 | if (errno) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7517 | LOGVAL(ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7518 | LOGVAL(ctx, LY_VLOG_LYD, set->ctx_node, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double (%s).", |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7519 | 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] | 7520 | return LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7521 | } else if (endptr - &exp->expr[exp->tok_pos[*tok_idx]] != exp->tok_len[*tok_idx]) { |
| 7522 | LOGVAL(ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7523 | LOGVAL(ctx, LY_VLOG_LYD, set->ctx_node, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double.", |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7524 | exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7525 | return LY_EVALID; |
| 7526 | } |
| 7527 | |
| 7528 | set_fill_number(set, num); |
| 7529 | } |
| 7530 | |
| 7531 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7532 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7533 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7534 | return LY_SUCCESS; |
| 7535 | } |
| 7536 | |
| 7537 | /** |
| 7538 | * @brief Evaluate PathExpr. Logs directly on error. |
| 7539 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7540 | * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate* |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7541 | * | PrimaryExpr Predicate* '/' RelativeLocationPath |
| 7542 | * | PrimaryExpr Predicate* '//' RelativeLocationPath |
| 7543 | * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7544 | * [10] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7545 | * |
| 7546 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7547 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7548 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7549 | * @param[in] options XPath options. |
| 7550 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7551 | */ |
| 7552 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7553 | eval_path_expr(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] | 7554 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7555 | uint8_t all_desc, parent_pos_pred; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7556 | LY_ERR rc; |
| 7557 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7558 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7559 | case LYXP_TOKEN_PAR1: |
| 7560 | /* '(' Expr ')' */ |
| 7561 | |
| 7562 | /* '(' */ |
| 7563 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7564 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7565 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7566 | |
| 7567 | /* Expr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7568 | rc = eval_expr_select(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7569 | LY_CHECK_RET(rc); |
| 7570 | |
| 7571 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7572 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7573 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7574 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7575 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7576 | |
| 7577 | parent_pos_pred = 0; |
| 7578 | goto predicate; |
| 7579 | |
| 7580 | case LYXP_TOKEN_DOT: |
| 7581 | case LYXP_TOKEN_DDOT: |
| 7582 | case LYXP_TOKEN_AT: |
| 7583 | case LYXP_TOKEN_NAMETEST: |
| 7584 | case LYXP_TOKEN_NODETYPE: |
| 7585 | /* RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7586 | rc = eval_relative_location_path(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7587 | LY_CHECK_RET(rc); |
| 7588 | break; |
| 7589 | |
| 7590 | case LYXP_TOKEN_FUNCNAME: |
| 7591 | /* FunctionCall */ |
| 7592 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7593 | rc = eval_function_call(exp, tok_idx, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7594 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7595 | rc = eval_function_call(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7596 | } |
| 7597 | LY_CHECK_RET(rc); |
| 7598 | |
| 7599 | parent_pos_pred = 1; |
| 7600 | goto predicate; |
| 7601 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 7602 | case LYXP_TOKEN_OPER_PATH: |
| 7603 | case LYXP_TOKEN_OPER_RPATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7604 | /* AbsoluteLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7605 | rc = eval_absolute_location_path(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7606 | LY_CHECK_RET(rc); |
| 7607 | break; |
| 7608 | |
| 7609 | case LYXP_TOKEN_LITERAL: |
| 7610 | /* Literal */ |
| 7611 | if (!set || (options & LYXP_SCNODE_ALL)) { |
| 7612 | if (set) { |
| 7613 | set_scnode_clear_ctx(set); |
| 7614 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7615 | eval_literal(exp, tok_idx, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7616 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7617 | eval_literal(exp, tok_idx, set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7618 | } |
| 7619 | |
| 7620 | parent_pos_pred = 1; |
| 7621 | goto predicate; |
| 7622 | |
| 7623 | case LYXP_TOKEN_NUMBER: |
| 7624 | /* Number */ |
| 7625 | if (!set || (options & LYXP_SCNODE_ALL)) { |
| 7626 | if (set) { |
| 7627 | set_scnode_clear_ctx(set); |
| 7628 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7629 | rc = eval_number(NULL, exp, tok_idx, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7630 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7631 | rc = eval_number(set->ctx, exp, tok_idx, set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7632 | } |
| 7633 | LY_CHECK_RET(rc); |
| 7634 | |
| 7635 | parent_pos_pred = 1; |
| 7636 | goto predicate; |
| 7637 | |
| 7638 | default: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7639 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[*tok_idx]), |
| 7640 | &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7641 | return LY_EVALID; |
| 7642 | } |
| 7643 | |
| 7644 | return LY_SUCCESS; |
| 7645 | |
| 7646 | predicate: |
| 7647 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7648 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 7649 | rc = eval_predicate(exp, tok_idx, set, options, parent_pos_pred); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7650 | LY_CHECK_RET(rc); |
| 7651 | } |
| 7652 | |
| 7653 | /* ('/' or '//') RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7654 | 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] | 7655 | |
| 7656 | /* evaluate '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7657 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7658 | all_desc = 0; |
| 7659 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7660 | all_desc = 1; |
| 7661 | } |
| 7662 | |
| 7663 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7664 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7665 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7666 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7667 | rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7668 | LY_CHECK_RET(rc); |
| 7669 | } |
| 7670 | |
| 7671 | return LY_SUCCESS; |
| 7672 | } |
| 7673 | |
| 7674 | /** |
| 7675 | * @brief Evaluate UnionExpr. Logs directly on error. |
| 7676 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7677 | * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7678 | * |
| 7679 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7680 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7681 | * @param[in] repeat How many times this expression is repeated. |
| 7682 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7683 | * @param[in] options XPath options. |
| 7684 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7685 | */ |
| 7686 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7687 | eval_union_expr(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] | 7688 | { |
| 7689 | LY_ERR rc = LY_SUCCESS; |
| 7690 | struct lyxp_set orig_set, set2; |
| 7691 | uint16_t i; |
| 7692 | |
| 7693 | assert(repeat); |
| 7694 | |
| 7695 | set_init(&orig_set, set); |
| 7696 | set_init(&set2, set); |
| 7697 | |
| 7698 | set_fill_set(&orig_set, set); |
| 7699 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7700 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7701 | LY_CHECK_GOTO(rc, cleanup); |
| 7702 | |
| 7703 | /* ('|' PathExpr)* */ |
| 7704 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7705 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7706 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7707 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7708 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7709 | |
| 7710 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7711 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7712 | LY_CHECK_GOTO(rc, cleanup); |
| 7713 | continue; |
| 7714 | } |
| 7715 | |
| 7716 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7717 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7718 | LY_CHECK_GOTO(rc, cleanup); |
| 7719 | |
| 7720 | /* eval */ |
| 7721 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7722 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7723 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7724 | rc = moveto_union(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7725 | LY_CHECK_GOTO(rc, cleanup); |
| 7726 | } |
| 7727 | } |
| 7728 | |
| 7729 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7730 | lyxp_set_free_content(&orig_set); |
| 7731 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7732 | return rc; |
| 7733 | } |
| 7734 | |
| 7735 | /** |
| 7736 | * @brief Evaluate UnaryExpr. Logs directly on error. |
| 7737 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7738 | * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7739 | * |
| 7740 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7741 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7742 | * @param[in] repeat How many times this expression is repeated. |
| 7743 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7744 | * @param[in] options XPath options. |
| 7745 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7746 | */ |
| 7747 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7748 | eval_unary_expr(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] | 7749 | { |
| 7750 | LY_ERR rc; |
| 7751 | uint16_t this_op, i; |
| 7752 | |
| 7753 | assert(repeat); |
| 7754 | |
| 7755 | /* ('-')+ */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7756 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7757 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7758 | 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] | 7759 | |
| 7760 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7761 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7762 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7763 | } |
| 7764 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7765 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7766 | LY_CHECK_RET(rc); |
| 7767 | |
| 7768 | if (set && (repeat % 2)) { |
| 7769 | if (options & LYXP_SCNODE_ALL) { |
| 7770 | warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]); |
| 7771 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7772 | 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] | 7773 | LY_CHECK_RET(rc); |
| 7774 | } |
| 7775 | } |
| 7776 | |
| 7777 | return LY_SUCCESS; |
| 7778 | } |
| 7779 | |
| 7780 | /** |
| 7781 | * @brief Evaluate MultiplicativeExpr. Logs directly on error. |
| 7782 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7783 | * [18] MultiplicativeExpr ::= UnaryExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7784 | * | MultiplicativeExpr '*' UnaryExpr |
| 7785 | * | MultiplicativeExpr 'div' UnaryExpr |
| 7786 | * | MultiplicativeExpr 'mod' UnaryExpr |
| 7787 | * |
| 7788 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7789 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7790 | * @param[in] repeat How many times this expression is repeated. |
| 7791 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7792 | * @param[in] options XPath options. |
| 7793 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7794 | */ |
| 7795 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7796 | eval_multiplicative_expr(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] | 7797 | { |
| 7798 | LY_ERR rc; |
| 7799 | uint16_t this_op; |
| 7800 | struct lyxp_set orig_set, set2; |
| 7801 | uint16_t i; |
| 7802 | |
| 7803 | assert(repeat); |
| 7804 | |
| 7805 | set_init(&orig_set, set); |
| 7806 | set_init(&set2, set); |
| 7807 | |
| 7808 | set_fill_set(&orig_set, set); |
| 7809 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7810 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7811 | LY_CHECK_GOTO(rc, cleanup); |
| 7812 | |
| 7813 | /* ('*' / 'div' / 'mod' UnaryExpr)* */ |
| 7814 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7815 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7816 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7817 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7818 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7819 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7820 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7821 | |
| 7822 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7823 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7824 | LY_CHECK_GOTO(rc, cleanup); |
| 7825 | continue; |
| 7826 | } |
| 7827 | |
| 7828 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7829 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7830 | LY_CHECK_GOTO(rc, cleanup); |
| 7831 | |
| 7832 | /* eval */ |
| 7833 | if (options & LYXP_SCNODE_ALL) { |
| 7834 | 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] | 7835 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7836 | set_scnode_clear_ctx(set); |
| 7837 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7838 | 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] | 7839 | LY_CHECK_GOTO(rc, cleanup); |
| 7840 | } |
| 7841 | } |
| 7842 | |
| 7843 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7844 | lyxp_set_free_content(&orig_set); |
| 7845 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7846 | return rc; |
| 7847 | } |
| 7848 | |
| 7849 | /** |
| 7850 | * @brief Evaluate AdditiveExpr. Logs directly on error. |
| 7851 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7852 | * [17] AdditiveExpr ::= MultiplicativeExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7853 | * | AdditiveExpr '+' MultiplicativeExpr |
| 7854 | * | AdditiveExpr '-' MultiplicativeExpr |
| 7855 | * |
| 7856 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7857 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7858 | * @param[in] repeat How many times this expression is repeated. |
| 7859 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7860 | * @param[in] options XPath options. |
| 7861 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7862 | */ |
| 7863 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7864 | eval_additive_expr(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] | 7865 | { |
| 7866 | LY_ERR rc; |
| 7867 | uint16_t this_op; |
| 7868 | struct lyxp_set orig_set, set2; |
| 7869 | uint16_t i; |
| 7870 | |
| 7871 | assert(repeat); |
| 7872 | |
| 7873 | set_init(&orig_set, set); |
| 7874 | set_init(&set2, set); |
| 7875 | |
| 7876 | set_fill_set(&orig_set, set); |
| 7877 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7878 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7879 | LY_CHECK_GOTO(rc, cleanup); |
| 7880 | |
| 7881 | /* ('+' / '-' MultiplicativeExpr)* */ |
| 7882 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7883 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7884 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7885 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7886 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7887 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7888 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7889 | |
| 7890 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7891 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7892 | LY_CHECK_GOTO(rc, cleanup); |
| 7893 | continue; |
| 7894 | } |
| 7895 | |
| 7896 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7897 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7898 | LY_CHECK_GOTO(rc, cleanup); |
| 7899 | |
| 7900 | /* eval */ |
| 7901 | if (options & LYXP_SCNODE_ALL) { |
| 7902 | 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] | 7903 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7904 | set_scnode_clear_ctx(set); |
| 7905 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7906 | 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] | 7907 | LY_CHECK_GOTO(rc, cleanup); |
| 7908 | } |
| 7909 | } |
| 7910 | |
| 7911 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7912 | lyxp_set_free_content(&orig_set); |
| 7913 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7914 | return rc; |
| 7915 | } |
| 7916 | |
| 7917 | /** |
| 7918 | * @brief Evaluate RelationalExpr. Logs directly on error. |
| 7919 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7920 | * [16] RelationalExpr ::= AdditiveExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7921 | * | RelationalExpr '<' AdditiveExpr |
| 7922 | * | RelationalExpr '>' AdditiveExpr |
| 7923 | * | RelationalExpr '<=' AdditiveExpr |
| 7924 | * | RelationalExpr '>=' AdditiveExpr |
| 7925 | * |
| 7926 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7927 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7928 | * @param[in] repeat How many times this expression is repeated. |
| 7929 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7930 | * @param[in] options XPath options. |
| 7931 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7932 | */ |
| 7933 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7934 | eval_relational_expr(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] | 7935 | { |
| 7936 | LY_ERR rc; |
| 7937 | uint16_t this_op; |
| 7938 | struct lyxp_set orig_set, set2; |
| 7939 | uint16_t i; |
| 7940 | |
| 7941 | assert(repeat); |
| 7942 | |
| 7943 | set_init(&orig_set, set); |
| 7944 | set_init(&set2, set); |
| 7945 | |
| 7946 | set_fill_set(&orig_set, set); |
| 7947 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7948 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7949 | LY_CHECK_GOTO(rc, cleanup); |
| 7950 | |
| 7951 | /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */ |
| 7952 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7953 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7954 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7955 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7956 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7957 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7958 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7959 | |
| 7960 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7961 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7962 | LY_CHECK_GOTO(rc, cleanup); |
| 7963 | continue; |
| 7964 | } |
| 7965 | |
| 7966 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7967 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7968 | LY_CHECK_GOTO(rc, cleanup); |
| 7969 | |
| 7970 | /* eval */ |
| 7971 | if (options & LYXP_SCNODE_ALL) { |
| 7972 | 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] | 7973 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7974 | set_scnode_clear_ctx(set); |
| 7975 | } else { |
| 7976 | rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options); |
| 7977 | LY_CHECK_GOTO(rc, cleanup); |
| 7978 | } |
| 7979 | } |
| 7980 | |
| 7981 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7982 | lyxp_set_free_content(&orig_set); |
| 7983 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7984 | return rc; |
| 7985 | } |
| 7986 | |
| 7987 | /** |
| 7988 | * @brief Evaluate EqualityExpr. Logs directly on error. |
| 7989 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7990 | * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7991 | * | EqualityExpr '!=' RelationalExpr |
| 7992 | * |
| 7993 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7994 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7995 | * @param[in] repeat How many times this expression is repeated. |
| 7996 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7997 | * @param[in] options XPath options. |
| 7998 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7999 | */ |
| 8000 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 8001 | eval_equality_expr(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] | 8002 | { |
| 8003 | LY_ERR rc; |
| 8004 | uint16_t this_op; |
| 8005 | struct lyxp_set orig_set, set2; |
| 8006 | uint16_t i; |
| 8007 | |
| 8008 | assert(repeat); |
| 8009 | |
| 8010 | set_init(&orig_set, set); |
| 8011 | set_init(&set2, set); |
| 8012 | |
| 8013 | set_fill_set(&orig_set, set); |
| 8014 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8015 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8016 | LY_CHECK_GOTO(rc, cleanup); |
| 8017 | |
| 8018 | /* ('=' / '!=' RelationalExpr)* */ |
| 8019 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8020 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8021 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8022 | 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] | 8023 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8024 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 8025 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8026 | |
| 8027 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8028 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8029 | LY_CHECK_GOTO(rc, cleanup); |
| 8030 | continue; |
| 8031 | } |
| 8032 | |
| 8033 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8034 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8035 | LY_CHECK_GOTO(rc, cleanup); |
| 8036 | |
| 8037 | /* eval */ |
| 8038 | if (options & LYXP_SCNODE_ALL) { |
| 8039 | 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] | 8040 | warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1); |
| 8041 | 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] | 8042 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8043 | set_scnode_clear_ctx(set); |
| 8044 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8045 | rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options); |
| 8046 | LY_CHECK_GOTO(rc, cleanup); |
| 8047 | } |
| 8048 | } |
| 8049 | |
| 8050 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8051 | lyxp_set_free_content(&orig_set); |
| 8052 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8053 | return rc; |
| 8054 | } |
| 8055 | |
| 8056 | /** |
| 8057 | * @brief Evaluate AndExpr. Logs directly on error. |
| 8058 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8059 | * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8060 | * |
| 8061 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8062 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8063 | * @param[in] repeat How many times this expression is repeated. |
| 8064 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8065 | * @param[in] options XPath options. |
| 8066 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8067 | */ |
| 8068 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 8069 | eval_and_expr(struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8070 | { |
| 8071 | LY_ERR rc; |
| 8072 | struct lyxp_set orig_set, set2; |
| 8073 | uint16_t i; |
| 8074 | |
| 8075 | assert(repeat); |
| 8076 | |
| 8077 | set_init(&orig_set, set); |
| 8078 | set_init(&set2, set); |
| 8079 | |
| 8080 | set_fill_set(&orig_set, set); |
| 8081 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8082 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8083 | LY_CHECK_GOTO(rc, cleanup); |
| 8084 | |
| 8085 | /* cast to boolean, we know that will be the final result */ |
| 8086 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 8087 | set_scnode_clear_ctx(set); |
| 8088 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8089 | lyxp_set_cast(set, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8090 | } |
| 8091 | |
| 8092 | /* ('and' EqualityExpr)* */ |
| 8093 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8094 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG); |
| 8095 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || !set->val.bln ? "skipped" : "parsed"), |
| 8096 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 8097 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8098 | |
| 8099 | /* lazy evaluation */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8100 | if (!set || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) { |
| 8101 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8102 | LY_CHECK_GOTO(rc, cleanup); |
| 8103 | continue; |
| 8104 | } |
| 8105 | |
| 8106 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8107 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8108 | LY_CHECK_GOTO(rc, cleanup); |
| 8109 | |
| 8110 | /* eval - just get boolean value actually */ |
| 8111 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 8112 | set_scnode_clear_ctx(&set2); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 8113 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8114 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8115 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8116 | set_fill_set(set, &set2); |
| 8117 | } |
| 8118 | } |
| 8119 | |
| 8120 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8121 | lyxp_set_free_content(&orig_set); |
| 8122 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8123 | return rc; |
| 8124 | } |
| 8125 | |
| 8126 | /** |
| 8127 | * @brief Evaluate OrExpr. Logs directly on error. |
| 8128 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8129 | * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8130 | * |
| 8131 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8132 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8133 | * @param[in] repeat How many times this expression is repeated. |
| 8134 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8135 | * @param[in] options XPath options. |
| 8136 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8137 | */ |
| 8138 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 8139 | eval_or_expr(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] | 8140 | { |
| 8141 | LY_ERR rc; |
| 8142 | struct lyxp_set orig_set, set2; |
| 8143 | uint16_t i; |
| 8144 | |
| 8145 | assert(repeat); |
| 8146 | |
| 8147 | set_init(&orig_set, set); |
| 8148 | set_init(&set2, set); |
| 8149 | |
| 8150 | set_fill_set(&orig_set, set); |
| 8151 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8152 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8153 | LY_CHECK_GOTO(rc, cleanup); |
| 8154 | |
| 8155 | /* cast to boolean, we know that will be the final result */ |
| 8156 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 8157 | set_scnode_clear_ctx(set); |
| 8158 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8159 | lyxp_set_cast(set, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8160 | } |
| 8161 | |
| 8162 | /* ('or' AndExpr)* */ |
| 8163 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8164 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG); |
| 8165 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || set->val.bln ? "skipped" : "parsed"), |
| 8166 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 8167 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8168 | |
| 8169 | /* lazy evaluation */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8170 | if (!set || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) { |
| 8171 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8172 | LY_CHECK_GOTO(rc, cleanup); |
| 8173 | continue; |
| 8174 | } |
| 8175 | |
| 8176 | set_fill_set(&set2, &orig_set); |
| 8177 | /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one), |
| 8178 | * but it does not matter */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8179 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8180 | LY_CHECK_GOTO(rc, cleanup); |
| 8181 | |
| 8182 | /* eval - just get boolean value actually */ |
| 8183 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 8184 | set_scnode_clear_ctx(&set2); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 8185 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8186 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8187 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8188 | set_fill_set(set, &set2); |
| 8189 | } |
| 8190 | } |
| 8191 | |
| 8192 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8193 | lyxp_set_free_content(&orig_set); |
| 8194 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8195 | return rc; |
| 8196 | } |
| 8197 | |
| 8198 | /** |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8199 | * @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] | 8200 | * |
| 8201 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8202 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8203 | * @param[in] etype Expression type to evaluate. |
| 8204 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8205 | * @param[in] options XPath options. |
| 8206 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8207 | */ |
| 8208 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 8209 | eval_expr_select(struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8210 | { |
| 8211 | uint16_t i, count; |
| 8212 | enum lyxp_expr_type next_etype; |
| 8213 | LY_ERR rc; |
| 8214 | |
| 8215 | /* process operator repeats */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8216 | if (!exp->repeat[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8217 | next_etype = LYXP_EXPR_NONE; |
| 8218 | } else { |
| 8219 | /* find etype repeat */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 8220 | for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i) {} |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8221 | |
| 8222 | /* select one-priority lower because etype expression called us */ |
| 8223 | if (i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8224 | next_etype = exp->repeat[*tok_idx][i - 1]; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8225 | /* count repeats for that expression */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 8226 | 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] | 8227 | } else { |
| 8228 | next_etype = LYXP_EXPR_NONE; |
| 8229 | } |
| 8230 | } |
| 8231 | |
| 8232 | /* decide what expression are we parsing based on the repeat */ |
| 8233 | switch (next_etype) { |
| 8234 | case LYXP_EXPR_OR: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8235 | rc = eval_or_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8236 | break; |
| 8237 | case LYXP_EXPR_AND: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8238 | rc = eval_and_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8239 | break; |
| 8240 | case LYXP_EXPR_EQUALITY: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8241 | rc = eval_equality_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8242 | break; |
| 8243 | case LYXP_EXPR_RELATIONAL: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8244 | rc = eval_relational_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8245 | break; |
| 8246 | case LYXP_EXPR_ADDITIVE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8247 | rc = eval_additive_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8248 | break; |
| 8249 | case LYXP_EXPR_MULTIPLICATIVE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8250 | rc = eval_multiplicative_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8251 | break; |
| 8252 | case LYXP_EXPR_UNARY: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8253 | rc = eval_unary_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8254 | break; |
| 8255 | case LYXP_EXPR_UNION: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8256 | rc = eval_union_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8257 | break; |
| 8258 | case LYXP_EXPR_NONE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8259 | rc = eval_path_expr(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8260 | break; |
| 8261 | default: |
| 8262 | LOGINT_RET(set->ctx); |
| 8263 | } |
| 8264 | |
| 8265 | return rc; |
| 8266 | } |
| 8267 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8268 | /** |
| 8269 | * @brief Get root type. |
| 8270 | * |
| 8271 | * @param[in] ctx_node Context node. |
| 8272 | * @param[in] ctx_scnode Schema context node. |
| 8273 | * @param[in] options XPath options. |
| 8274 | * @return Root type. |
| 8275 | */ |
| 8276 | static enum lyxp_node_type |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 8277 | 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] | 8278 | { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 8279 | const struct lysc_node *op; |
| 8280 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8281 | if (options & LYXP_SCNODE_ALL) { |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 8282 | 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] | 8283 | |
| 8284 | if (op || (options & LYXP_SCNODE)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8285 | /* general root that can access everything */ |
| 8286 | return LYXP_NODE_ROOT; |
| 8287 | } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) { |
| 8288 | /* root context node can access only config data (because we said so, it is unspecified) */ |
| 8289 | return LYXP_NODE_ROOT_CONFIG; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8290 | } |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 8291 | return LYXP_NODE_ROOT; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8292 | } |
| 8293 | |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 8294 | op = ctx_node ? ctx_node->schema : NULL; |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 8295 | 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] | 8296 | |
| 8297 | if (!ctx_node || (!op && (ctx_node->schema->flags & LYS_CONFIG_W))) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8298 | /* root context node can access only config data (because we said so, it is unspecified) */ |
| 8299 | return LYXP_NODE_ROOT_CONFIG; |
| 8300 | } |
| 8301 | |
| 8302 | return LYXP_NODE_ROOT; |
| 8303 | } |
| 8304 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8305 | LY_ERR |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 8306 | lyxp_eval(struct lyxp_expr *exp, LY_PREFIX_FORMAT format, const struct lys_module *local_mod, const struct lyd_node *ctx_node, |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 8307 | enum lyxp_node_type ctx_node_type, const struct lyd_node *tree, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8308 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8309 | uint16_t tok_idx = 0; |
| 8310 | const struct lyd_node *real_ctx_node; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8311 | LY_ERR rc; |
| 8312 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8313 | LY_CHECK_ARG_RET(NULL, exp, local_mod, ctx_node, set, LY_EINVAL); |
| 8314 | |
| 8315 | if ((ctx_node_type == LYXP_NODE_ROOT) || (ctx_node_type == LYXP_NODE_ROOT_CONFIG)) { |
| 8316 | /* we always need some context node because it is used for resolving unqualified names */ |
| 8317 | real_ctx_node = NULL; |
| 8318 | } else { |
| 8319 | real_ctx_node = ctx_node; |
| 8320 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8321 | |
| 8322 | /* prepare set for evaluation */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8323 | tok_idx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8324 | memset(set, 0, sizeof *set); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8325 | set->type = LYXP_SET_NODE_SET; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8326 | set_insert_node(set, (struct lyd_node *)real_ctx_node, 0, ctx_node_type, 0); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 8327 | set->ctx = local_mod->ctx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8328 | set->ctx_node = ctx_node; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8329 | set->root_type = lyxp_get_root_type(real_ctx_node, NULL, options); |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 8330 | for (set->context_op = ctx_node->schema; |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 8331 | set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); |
| 8332 | set->context_op = set->context_op->parent) {} |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8333 | set->local_mod = local_mod; |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 8334 | set->tree = tree; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8335 | set->format = format; |
| 8336 | |
| 8337 | /* evaluate */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8338 | rc = eval_expr_select(exp, &tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8339 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8340 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8341 | } |
| 8342 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8343 | return rc; |
| 8344 | } |
| 8345 | |
| 8346 | #if 0 |
| 8347 | |
| 8348 | /* full xml printing of set elements, not used currently */ |
| 8349 | |
| 8350 | void |
| 8351 | lyxp_set_print_xml(FILE *f, struct lyxp_set *set) |
| 8352 | { |
| 8353 | uint32_t i; |
| 8354 | char *str_num; |
| 8355 | struct lyout out; |
| 8356 | |
| 8357 | memset(&out, 0, sizeof out); |
| 8358 | |
| 8359 | out.type = LYOUT_STREAM; |
| 8360 | out.method.f = f; |
| 8361 | |
| 8362 | switch (set->type) { |
| 8363 | case LYXP_SET_EMPTY: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8364 | ly_print_(&out, "Empty XPath set\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8365 | break; |
| 8366 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8367 | ly_print_(&out, "Boolean XPath set:\n"); |
| 8368 | ly_print_(&out, "%s\n\n", set->value.bool ? "true" : "false"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8369 | break; |
| 8370 | case LYXP_SET_STRING: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8371 | ly_print_(&out, "String XPath set:\n"); |
| 8372 | ly_print_(&out, "\"%s\"\n\n", set->value.str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8373 | break; |
| 8374 | case LYXP_SET_NUMBER: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8375 | ly_print_(&out, "Number XPath set:\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8376 | |
| 8377 | if (isnan(set->value.num)) { |
| 8378 | str_num = strdup("NaN"); |
| 8379 | } else if ((set->value.num == 0) || (set->value.num == -0.0f)) { |
| 8380 | str_num = strdup("0"); |
| 8381 | } else if (isinf(set->value.num) && !signbit(set->value.num)) { |
| 8382 | str_num = strdup("Infinity"); |
| 8383 | } else if (isinf(set->value.num) && signbit(set->value.num)) { |
| 8384 | str_num = strdup("-Infinity"); |
| 8385 | } else if ((long long)set->value.num == set->value.num) { |
| 8386 | if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) { |
| 8387 | str_num = NULL; |
| 8388 | } |
| 8389 | } else { |
| 8390 | if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) { |
| 8391 | str_num = NULL; |
| 8392 | } |
| 8393 | } |
| 8394 | if (!str_num) { |
| 8395 | LOGMEM; |
| 8396 | return; |
| 8397 | } |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8398 | ly_print_(&out, "%s\n\n", str_num); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8399 | free(str_num); |
| 8400 | break; |
| 8401 | case LYXP_SET_NODE_SET: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8402 | ly_print_(&out, "Node XPath set:\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8403 | |
| 8404 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8405 | ly_print_(&out, "%d. ", i + 1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8406 | switch (set->node_type[i]) { |
| 8407 | case LYXP_NODE_ROOT_ALL: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8408 | ly_print_(&out, "ROOT all\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8409 | break; |
| 8410 | case LYXP_NODE_ROOT_CONFIG: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8411 | ly_print_(&out, "ROOT config\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8412 | break; |
| 8413 | case LYXP_NODE_ROOT_STATE: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8414 | ly_print_(&out, "ROOT state\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8415 | break; |
| 8416 | case LYXP_NODE_ROOT_NOTIF: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8417 | 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] | 8418 | break; |
| 8419 | case LYXP_NODE_ROOT_RPC: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8420 | 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] | 8421 | break; |
| 8422 | case LYXP_NODE_ROOT_OUTPUT: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8423 | 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] | 8424 | break; |
| 8425 | case LYXP_NODE_ELEM: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8426 | ly_print_(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8427 | xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT); |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8428 | ly_print_(&out, "\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8429 | break; |
| 8430 | case LYXP_NODE_TEXT: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8431 | 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] | 8432 | break; |
| 8433 | case LYXP_NODE_ATTR: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8434 | 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] | 8435 | break; |
| 8436 | } |
| 8437 | } |
| 8438 | break; |
| 8439 | } |
| 8440 | } |
| 8441 | |
| 8442 | #endif |
| 8443 | |
| 8444 | LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8445 | lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8446 | { |
| 8447 | long double num; |
| 8448 | char *str; |
| 8449 | LY_ERR rc; |
| 8450 | |
| 8451 | if (!set || (set->type == target)) { |
| 8452 | return LY_SUCCESS; |
| 8453 | } |
| 8454 | |
| 8455 | /* it's not possible to convert anything into a node set */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8456 | assert(target != LYXP_SET_NODE_SET); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8457 | |
| 8458 | if (set->type == LYXP_SET_SCNODE_SET) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8459 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8460 | return LY_EINVAL; |
| 8461 | } |
| 8462 | |
| 8463 | /* to STRING */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8464 | 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] | 8465 | switch (set->type) { |
| 8466 | case LYXP_SET_NUMBER: |
| 8467 | if (isnan(set->val.num)) { |
| 8468 | set->val.str = strdup("NaN"); |
| 8469 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 8470 | } else if ((set->val.num == 0) || (set->val.num == -0.0f)) { |
| 8471 | set->val.str = strdup("0"); |
| 8472 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 8473 | } else if (isinf(set->val.num) && !signbit(set->val.num)) { |
| 8474 | set->val.str = strdup("Infinity"); |
| 8475 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 8476 | } else if (isinf(set->val.num) && signbit(set->val.num)) { |
| 8477 | set->val.str = strdup("-Infinity"); |
| 8478 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 8479 | } else if ((long long)set->val.num == set->val.num) { |
| 8480 | if (asprintf(&str, "%lld", (long long)set->val.num) == -1) { |
| 8481 | LOGMEM_RET(set->ctx); |
| 8482 | } |
| 8483 | set->val.str = str; |
| 8484 | } else { |
| 8485 | if (asprintf(&str, "%03.1Lf", set->val.num) == -1) { |
| 8486 | LOGMEM_RET(set->ctx); |
| 8487 | } |
| 8488 | set->val.str = str; |
| 8489 | } |
| 8490 | break; |
| 8491 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8492 | if (set->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8493 | set->val.str = strdup("true"); |
| 8494 | } else { |
| 8495 | set->val.str = strdup("false"); |
| 8496 | } |
| 8497 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM); |
| 8498 | break; |
| 8499 | case LYXP_SET_NODE_SET: |
| 8500 | assert(set->used); |
| 8501 | |
| 8502 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8503 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8504 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8505 | rc = cast_node_set_to_string(set, &str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8506 | LY_CHECK_RET(rc); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8507 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8508 | set->val.str = str; |
| 8509 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8510 | default: |
| 8511 | LOGINT_RET(set->ctx); |
| 8512 | } |
| 8513 | set->type = LYXP_SET_STRING; |
| 8514 | } |
| 8515 | |
| 8516 | /* to NUMBER */ |
| 8517 | if (target == LYXP_SET_NUMBER) { |
| 8518 | switch (set->type) { |
| 8519 | case LYXP_SET_STRING: |
| 8520 | num = cast_string_to_number(set->val.str); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8521 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8522 | set->val.num = num; |
| 8523 | break; |
| 8524 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8525 | if (set->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8526 | set->val.num = 1; |
| 8527 | } else { |
| 8528 | set->val.num = 0; |
| 8529 | } |
| 8530 | break; |
| 8531 | default: |
| 8532 | LOGINT_RET(set->ctx); |
| 8533 | } |
| 8534 | set->type = LYXP_SET_NUMBER; |
| 8535 | } |
| 8536 | |
| 8537 | /* to BOOLEAN */ |
| 8538 | if (target == LYXP_SET_BOOLEAN) { |
| 8539 | switch (set->type) { |
| 8540 | case LYXP_SET_NUMBER: |
| 8541 | 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] | 8542 | set->val.bln = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8543 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8544 | set->val.bln = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8545 | } |
| 8546 | break; |
| 8547 | case LYXP_SET_STRING: |
| 8548 | if (set->val.str[0]) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8549 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8550 | set->val.bln = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8551 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8552 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8553 | set->val.bln = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8554 | } |
| 8555 | break; |
| 8556 | case LYXP_SET_NODE_SET: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8557 | if (set->used) { |
| 8558 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8559 | set->val.bln = 1; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8560 | } else { |
| 8561 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8562 | set->val.bln = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8563 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8564 | break; |
| 8565 | default: |
| 8566 | LOGINT_RET(set->ctx); |
| 8567 | } |
| 8568 | set->type = LYXP_SET_BOOLEAN; |
| 8569 | } |
| 8570 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8571 | return LY_SUCCESS; |
| 8572 | } |
| 8573 | |
| 8574 | LY_ERR |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 8575 | lyxp_atomize(struct lyxp_expr *exp, LY_PREFIX_FORMAT format, const struct lys_module *local_mod, |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 8576 | const struct lysc_node *ctx_scnode, enum lyxp_node_type ctx_scnode_type, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8577 | { |
| 8578 | struct ly_ctx *ctx; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8579 | const struct lysc_node *real_ctx_scnode; |
| 8580 | uint16_t tok_idx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8581 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8582 | LY_CHECK_ARG_RET(NULL, exp, local_mod, ctx_scnode, set, LY_EINVAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8583 | |
| 8584 | ctx = local_mod->ctx; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8585 | if ((ctx_scnode_type == LYXP_NODE_ROOT) || (ctx_scnode_type == LYXP_NODE_ROOT_CONFIG)) { |
| 8586 | /* we always need some context node because it is used for resolving unqualified names */ |
| 8587 | real_ctx_scnode = NULL; |
| 8588 | } else { |
| 8589 | real_ctx_scnode = ctx_scnode; |
| 8590 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8591 | |
| 8592 | /* prepare set for evaluation */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8593 | tok_idx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8594 | memset(set, 0, sizeof *set); |
| 8595 | set->type = LYXP_SET_SCNODE_SET; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8596 | lyxp_set_scnode_insert_node(set, real_ctx_scnode, ctx_scnode_type); |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 8597 | set->val.scnodes[0].in_ctx = -2; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8598 | set->ctx = ctx; |
| 8599 | set->ctx_scnode = ctx_scnode; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8600 | set->root_type = lyxp_get_root_type(NULL, real_ctx_scnode, options); |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 8601 | for (set->context_op = ctx_scnode; |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 8602 | set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); |
| 8603 | set->context_op = set->context_op->parent) {} |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8604 | set->local_mod = local_mod; |
| 8605 | set->format = format; |
| 8606 | |
| 8607 | /* evaluate */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8608 | return eval_expr_select(exp, &tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8609 | } |
Michal Vasko | d43d71a | 2020-08-07 14:54:58 +0200 | [diff] [blame] | 8610 | |
| 8611 | API const char * |
| 8612 | lyxp_get_expr(const struct lyxp_expr *path) |
| 8613 | { |
| 8614 | if (!path) { |
| 8615 | return NULL; |
| 8616 | } |
| 8617 | |
| 8618 | return path->expr; |
| 8619 | } |