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); |
| 47 | static LY_ERR eval_expr_select(struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype, struct lyxp_set *set, int 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; |
| 168 | int dynamic; |
| 169 | struct lyxp_set_node *item; |
| 170 | struct lyxp_set_scnode *sitem; |
| 171 | |
| 172 | if (ly_log_level < LY_LLDBG) { |
| 173 | return; |
| 174 | } |
| 175 | |
| 176 | switch (set->type) { |
| 177 | case LYXP_SET_NODE_SET: |
| 178 | LOGDBG(LY_LDGXPATH, "set NODE SET:"); |
| 179 | for (i = 0; i < set->used; ++i) { |
| 180 | item = &set->val.nodes[i]; |
| 181 | |
| 182 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 183 | case LYXP_NODE_NONE: |
| 184 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): NONE", i + 1, item->pos); |
| 185 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 186 | case LYXP_NODE_ROOT: |
| 187 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT", i + 1, item->pos); |
| 188 | break; |
| 189 | case LYXP_NODE_ROOT_CONFIG: |
| 190 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT CONFIG", i + 1, item->pos); |
| 191 | break; |
| 192 | case LYXP_NODE_ELEM: |
| 193 | if ((item->node->schema->nodetype == LYS_LIST) |
| 194 | && (((struct lyd_node_inner *)item->node)->child->schema->nodetype == LYS_LEAF)) { |
| 195 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (1st child val: %s)", i + 1, item->pos, |
| 196 | item->node->schema->name, |
Michal Vasko | 5bfd4be | 2020-06-23 13:26:19 +0200 | [diff] [blame] | 197 | (str = (char *)lyd_value2str((struct lyd_node_term *)lyd_node_children(item->node, 0), &dynamic))); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 198 | if (dynamic) { |
| 199 | free(str); |
| 200 | } |
| 201 | } else if (((struct lyd_node_inner *)item->node)->schema->nodetype == LYS_LEAFLIST) { |
| 202 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (val: %s)", i + 1, item->pos, |
| 203 | item->node->schema->name, |
| 204 | (str = (char *)lyd_value2str((struct lyd_node_term *)item->node, &dynamic))); |
| 205 | if (dynamic) { |
| 206 | free(str); |
| 207 | } |
| 208 | } else { |
| 209 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s", i + 1, item->pos, item->node->schema->name); |
| 210 | } |
| 211 | break; |
| 212 | case LYXP_NODE_TEXT: |
| 213 | if (item->node->schema->nodetype & LYS_ANYDATA) { |
| 214 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT <%s>", i + 1, item->pos, |
| 215 | item->node->schema->nodetype == LYS_ANYXML ? "anyxml" : "anydata"); |
| 216 | } else { |
| 217 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT %s", i + 1, item->pos, |
| 218 | (str = (char *)lyd_value2str((struct lyd_node_term *)item->node, &dynamic))); |
| 219 | if (dynamic) { |
| 220 | free(str); |
| 221 | } |
| 222 | } |
| 223 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 224 | case LYXP_NODE_META: |
| 225 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): META %s = %s", i + 1, item->pos, set->val.meta[i].meta->name, |
| 226 | set->val.meta[i].meta->value); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 227 | break; |
| 228 | } |
| 229 | } |
| 230 | break; |
| 231 | |
| 232 | case LYXP_SET_SCNODE_SET: |
| 233 | LOGDBG(LY_LDGXPATH, "set SCNODE SET:"); |
| 234 | for (i = 0; i < set->used; ++i) { |
| 235 | sitem = &set->val.scnodes[i]; |
| 236 | |
| 237 | switch (sitem->type) { |
| 238 | case LYXP_NODE_ROOT: |
| 239 | LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT", i + 1, sitem->in_ctx); |
| 240 | break; |
| 241 | case LYXP_NODE_ROOT_CONFIG: |
| 242 | LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT CONFIG", i + 1, sitem->in_ctx); |
| 243 | break; |
| 244 | case LYXP_NODE_ELEM: |
| 245 | LOGDBG(LY_LDGXPATH, "\t%d (%u): ELEM %s", i + 1, sitem->in_ctx, sitem->scnode->name); |
| 246 | break; |
| 247 | default: |
| 248 | LOGINT(NULL); |
| 249 | break; |
| 250 | } |
| 251 | } |
| 252 | break; |
| 253 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 254 | case LYXP_SET_BOOLEAN: |
| 255 | LOGDBG(LY_LDGXPATH, "set BOOLEAN"); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 256 | LOGDBG(LY_LDGXPATH, "\t%s", (set->val.bln ? "true" : "false")); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 257 | break; |
| 258 | |
| 259 | case LYXP_SET_STRING: |
| 260 | LOGDBG(LY_LDGXPATH, "set STRING"); |
| 261 | LOGDBG(LY_LDGXPATH, "\t%s", set->val.str); |
| 262 | break; |
| 263 | |
| 264 | case LYXP_SET_NUMBER: |
| 265 | LOGDBG(LY_LDGXPATH, "set NUMBER"); |
| 266 | |
| 267 | if (isnan(set->val.num)) { |
| 268 | str = strdup("NaN"); |
| 269 | } else if ((set->val.num == 0) || (set->val.num == -0.0f)) { |
| 270 | str = strdup("0"); |
| 271 | } else if (isinf(set->val.num) && !signbit(set->val.num)) { |
| 272 | str = strdup("Infinity"); |
| 273 | } else if (isinf(set->val.num) && signbit(set->val.num)) { |
| 274 | str = strdup("-Infinity"); |
| 275 | } else if ((long long)set->val.num == set->val.num) { |
| 276 | if (asprintf(&str, "%lld", (long long)set->val.num) == -1) { |
| 277 | str = NULL; |
| 278 | } |
| 279 | } else { |
| 280 | if (asprintf(&str, "%03.1Lf", set->val.num) == -1) { |
| 281 | str = NULL; |
| 282 | } |
| 283 | } |
| 284 | LY_CHECK_ERR_RET(!str, LOGMEM(NULL), ); |
| 285 | |
| 286 | LOGDBG(LY_LDGXPATH, "\t%s", str); |
| 287 | free(str); |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | #endif |
| 292 | |
| 293 | /** |
| 294 | * @brief Realloc the string \p str. |
| 295 | * |
| 296 | * @param[in] ctx libyang context for logging. |
| 297 | * @param[in] needed How much free space is required. |
| 298 | * @param[in,out] str Pointer to the string to use. |
| 299 | * @param[in,out] used Used bytes in \p str. |
| 300 | * @param[in,out] size Allocated bytes in \p str. |
| 301 | * @return LY_ERR |
| 302 | */ |
| 303 | static LY_ERR |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 304 | 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] | 305 | { |
| 306 | if (*size - *used < needed) { |
| 307 | do { |
| 308 | if ((UINT16_MAX - *size) < LYXP_STRING_CAST_SIZE_STEP) { |
| 309 | LOGERR(ctx, LY_EINVAL, "XPath string length limit (%u) reached.", UINT16_MAX); |
| 310 | return LY_EINVAL; |
| 311 | } |
| 312 | *size += LYXP_STRING_CAST_SIZE_STEP; |
| 313 | } while (*size - *used < needed); |
| 314 | *str = ly_realloc(*str, *size * sizeof(char)); |
| 315 | LY_CHECK_ERR_RET(!(*str), LOGMEM(ctx), LY_EMEM); |
| 316 | } |
| 317 | |
| 318 | return LY_SUCCESS; |
| 319 | } |
| 320 | |
| 321 | /** |
| 322 | * @brief Cast nodes recursively to one string @p str. |
| 323 | * |
| 324 | * @param[in] node Node to cast. |
| 325 | * @param[in] fake_cont Whether to put the data into a "fake" container. |
| 326 | * @param[in] root_type Type of the XPath root. |
| 327 | * @param[in] indent Current indent. |
| 328 | * @param[in,out] str Resulting string. |
| 329 | * @param[in,out] used Used bytes in @p str. |
| 330 | * @param[in,out] size Allocated bytes in @p str. |
| 331 | * @return LY_ERR |
| 332 | */ |
| 333 | static LY_ERR |
| 334 | cast_string_recursive(const struct lyd_node *node, int fake_cont, enum lyxp_node_type root_type, uint16_t indent, char **str, |
| 335 | uint16_t *used, uint16_t *size) |
| 336 | { |
Radek Krejci | 7f769d7 | 2020-07-11 23:13:56 +0200 | [diff] [blame] | 337 | char *buf, *line, *ptr = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 338 | const char *value_str; |
| 339 | int dynamic; |
| 340 | const struct lyd_node *child; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 341 | struct lyd_node *tree; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 342 | struct lyd_node_any *any; |
| 343 | LY_ERR rc; |
| 344 | |
| 345 | if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) { |
| 346 | return LY_SUCCESS; |
| 347 | } |
| 348 | |
| 349 | if (fake_cont) { |
| 350 | rc = cast_string_realloc(LYD_NODE_CTX(node), 1, str, used, size); |
| 351 | LY_CHECK_RET(rc); |
| 352 | strcpy(*str + (*used - 1), "\n"); |
| 353 | ++(*used); |
| 354 | |
| 355 | ++indent; |
| 356 | } |
| 357 | |
| 358 | switch (node->schema->nodetype) { |
| 359 | case LYS_CONTAINER: |
| 360 | case LYS_LIST: |
| 361 | case LYS_RPC: |
| 362 | case LYS_NOTIF: |
| 363 | rc = cast_string_realloc(LYD_NODE_CTX(node), 1, str, used, size); |
| 364 | LY_CHECK_RET(rc); |
| 365 | strcpy(*str + (*used - 1), "\n"); |
| 366 | ++(*used); |
| 367 | |
Michal Vasko | 5bfd4be | 2020-06-23 13:26:19 +0200 | [diff] [blame] | 368 | for (child = lyd_node_children(node, 0); child; child = child->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 369 | rc = cast_string_recursive(child, 0, root_type, indent + 1, str, used, size); |
| 370 | LY_CHECK_RET(rc); |
| 371 | } |
| 372 | |
| 373 | break; |
| 374 | |
| 375 | case LYS_LEAF: |
| 376 | case LYS_LEAFLIST: |
| 377 | value_str = lyd_value2str(((struct lyd_node_term *)node), &dynamic); |
| 378 | |
| 379 | /* print indent */ |
| 380 | rc = cast_string_realloc(LYD_NODE_CTX(node), indent * 2 + strlen(value_str) + 1, str, used, size); |
| 381 | if (rc != LY_SUCCESS) { |
| 382 | if (dynamic) { |
| 383 | free((char *)value_str); |
| 384 | } |
| 385 | return rc; |
| 386 | } |
| 387 | memset(*str + (*used - 1), ' ', indent * 2); |
| 388 | *used += indent * 2; |
| 389 | |
| 390 | /* print value */ |
| 391 | if (*used == 1) { |
| 392 | sprintf(*str + (*used - 1), "%s", value_str); |
| 393 | *used += strlen(value_str); |
| 394 | } else { |
| 395 | sprintf(*str + (*used - 1), "%s\n", value_str); |
| 396 | *used += strlen(value_str) + 1; |
| 397 | } |
| 398 | if (dynamic) { |
| 399 | free((char *)value_str); |
| 400 | } |
| 401 | |
| 402 | break; |
| 403 | |
| 404 | case LYS_ANYXML: |
| 405 | case LYS_ANYDATA: |
| 406 | any = (struct lyd_node_any *)node; |
| 407 | if (!(void *)any->value.tree) { |
| 408 | /* no content */ |
| 409 | buf = strdup(""); |
| 410 | LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_NODE_CTX(node)), LY_EMEM); |
| 411 | } else { |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 412 | struct ly_out *out; |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 413 | |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 414 | if (any->value_type == LYD_ANYDATA_LYB) { |
| 415 | /* try to parse it into a data tree */ |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 416 | if (lyd_parse_data_mem((struct ly_ctx *)LYD_NODE_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] | 417 | /* successfully parsed */ |
| 418 | free(any->value.mem); |
| 419 | any->value.tree = tree; |
| 420 | any->value_type = LYD_ANYDATA_DATATREE; |
| 421 | } |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 422 | /* 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] | 423 | } |
| 424 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 425 | switch (any->value_type) { |
| 426 | case LYD_ANYDATA_STRING: |
| 427 | case LYD_ANYDATA_XML: |
| 428 | case LYD_ANYDATA_JSON: |
| 429 | buf = strdup(any->value.json); |
| 430 | LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_NODE_CTX(node)), LY_EMEM); |
| 431 | break; |
| 432 | case LYD_ANYDATA_DATATREE: |
Radek Krejci | 84ce7b1 | 2020-06-11 17:28:25 +0200 | [diff] [blame] | 433 | LY_CHECK_RET(ly_out_new_memory(&buf, 0, &out)); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 434 | rc = lyd_print_all(out, any->value.tree, LYD_XML, 0); |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 435 | ly_out_free(out, NULL, 0); |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 436 | LY_CHECK_RET(rc < 0, -rc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 437 | break; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 438 | case LYD_ANYDATA_LYB: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 439 | LOGERR(LYD_NODE_CTX(node), LY_EINVAL, "Cannot convert LYB anydata into string."); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 440 | return LY_EINVAL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 441 | } |
| 442 | } |
| 443 | |
| 444 | line = strtok_r(buf, "\n", &ptr); |
| 445 | do { |
| 446 | rc = cast_string_realloc(LYD_NODE_CTX(node), indent * 2 + strlen(line) + 1, str, used, size); |
| 447 | if (rc != LY_SUCCESS) { |
| 448 | free(buf); |
| 449 | return rc; |
| 450 | } |
| 451 | memset(*str + (*used - 1), ' ', indent * 2); |
| 452 | *used += indent * 2; |
| 453 | |
| 454 | strcpy(*str + (*used - 1), line); |
| 455 | *used += strlen(line); |
| 456 | |
| 457 | strcpy(*str + (*used - 1), "\n"); |
| 458 | *used += 1; |
| 459 | } while ((line = strtok_r(NULL, "\n", &ptr))); |
| 460 | |
| 461 | free(buf); |
| 462 | break; |
| 463 | |
| 464 | default: |
| 465 | LOGINT_RET(LYD_NODE_CTX(node)); |
| 466 | } |
| 467 | |
| 468 | if (fake_cont) { |
| 469 | rc = cast_string_realloc(LYD_NODE_CTX(node), 1, str, used, size); |
| 470 | LY_CHECK_RET(rc); |
| 471 | strcpy(*str + (*used - 1), "\n"); |
| 472 | ++(*used); |
| 473 | |
| 474 | --indent; |
| 475 | } |
| 476 | |
| 477 | return LY_SUCCESS; |
| 478 | } |
| 479 | |
| 480 | /** |
| 481 | * @brief Cast an element into a string. |
| 482 | * |
| 483 | * @param[in] node Node to cast. |
| 484 | * @param[in] fake_cont Whether to put the data into a "fake" container. |
| 485 | * @param[in] root_type Type of the XPath root. |
| 486 | * @param[out] str Element cast to dynamically-allocated string. |
| 487 | * @return LY_ERR |
| 488 | */ |
| 489 | static LY_ERR |
| 490 | cast_string_elem(struct lyd_node *node, int fake_cont, enum lyxp_node_type root_type, char **str) |
| 491 | { |
| 492 | uint16_t used, size; |
| 493 | LY_ERR rc; |
| 494 | |
| 495 | *str = malloc(LYXP_STRING_CAST_SIZE_START * sizeof(char)); |
| 496 | LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_NODE_CTX(node)), LY_EMEM); |
| 497 | (*str)[0] = '\0'; |
| 498 | used = 1; |
| 499 | size = LYXP_STRING_CAST_SIZE_START; |
| 500 | |
| 501 | rc = cast_string_recursive(node, fake_cont, root_type, 0, str, &used, &size); |
| 502 | if (rc != LY_SUCCESS) { |
| 503 | free(*str); |
| 504 | return rc; |
| 505 | } |
| 506 | |
| 507 | if (size > used) { |
| 508 | *str = ly_realloc(*str, used * sizeof(char)); |
| 509 | LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_NODE_CTX(node)), LY_EMEM); |
| 510 | } |
| 511 | return LY_SUCCESS; |
| 512 | } |
| 513 | |
| 514 | /** |
| 515 | * @brief Cast a LYXP_SET_NODE_SET set into a string. |
| 516 | * Context position aware. |
| 517 | * |
| 518 | * @param[in] set Set to cast. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 519 | * @param[out] str Cast dynamically-allocated string. |
| 520 | * @return LY_ERR |
| 521 | */ |
| 522 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 523 | cast_node_set_to_string(struct lyxp_set *set, char **str) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 524 | { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 525 | int dynamic; |
| 526 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 527 | switch (set->val.nodes[0].type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 528 | case LYXP_NODE_NONE: |
| 529 | /* invalid */ |
| 530 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 531 | case LYXP_NODE_ROOT: |
| 532 | case LYXP_NODE_ROOT_CONFIG: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 533 | 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] | 534 | case LYXP_NODE_ELEM: |
| 535 | case LYXP_NODE_TEXT: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 536 | 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] | 537 | case LYXP_NODE_META: |
| 538 | *str = (char *)lyd_meta2str(set->val.meta[0].meta, &dynamic); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 539 | if (!dynamic) { |
| 540 | *str = strdup(*str); |
| 541 | if (!*str) { |
| 542 | LOGMEM_RET(set->ctx); |
| 543 | } |
| 544 | } |
| 545 | return LY_SUCCESS; |
| 546 | } |
| 547 | |
| 548 | LOGINT_RET(set->ctx); |
| 549 | } |
| 550 | |
| 551 | /** |
| 552 | * @brief Cast a string into an XPath number. |
| 553 | * |
| 554 | * @param[in] str String to use. |
| 555 | * @return Cast number. |
| 556 | */ |
| 557 | static long double |
| 558 | cast_string_to_number(const char *str) |
| 559 | { |
| 560 | long double num; |
| 561 | char *ptr; |
| 562 | |
| 563 | errno = 0; |
| 564 | num = strtold(str, &ptr); |
| 565 | if (errno || *ptr) { |
| 566 | num = NAN; |
| 567 | } |
| 568 | return num; |
| 569 | } |
| 570 | |
| 571 | /** |
| 572 | * @brief Callback for checking value equality. |
| 573 | * |
| 574 | * @param[in] val1_p First value. |
| 575 | * @param[in] val2_p Second value. |
| 576 | * @param[in] mod Whether hash table is being modified. |
| 577 | * @param[in] cb_data Callback data. |
| 578 | * @return 0 if not equal, non-zero if equal. |
| 579 | */ |
| 580 | static int |
| 581 | set_values_equal_cb(void *val1_p, void *val2_p, int UNUSED(mod), void *UNUSED(cb_data)) |
| 582 | { |
| 583 | struct lyxp_set_hash_node *val1, *val2; |
| 584 | |
| 585 | val1 = (struct lyxp_set_hash_node *)val1_p; |
| 586 | val2 = (struct lyxp_set_hash_node *)val2_p; |
| 587 | |
| 588 | if ((val1->node == val2->node) && (val1->type == val2->type)) { |
| 589 | return 1; |
| 590 | } |
| 591 | |
| 592 | return 0; |
| 593 | } |
| 594 | |
| 595 | /** |
| 596 | * @brief Insert node and its hash into set. |
| 597 | * |
| 598 | * @param[in] set et to insert to. |
| 599 | * @param[in] node Node with hash. |
| 600 | * @param[in] type Node type. |
| 601 | */ |
| 602 | static void |
| 603 | set_insert_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type) |
| 604 | { |
| 605 | int r; |
| 606 | uint32_t i, hash; |
| 607 | struct lyxp_set_hash_node hnode; |
| 608 | |
| 609 | if (!set->ht && (set->used >= LYD_HT_MIN_ITEMS)) { |
| 610 | /* create hash table and add all the nodes */ |
| 611 | set->ht = lyht_new(1, sizeof(struct lyxp_set_hash_node), set_values_equal_cb, NULL, 1); |
| 612 | for (i = 0; i < set->used; ++i) { |
| 613 | hnode.node = set->val.nodes[i].node; |
| 614 | hnode.type = set->val.nodes[i].type; |
| 615 | |
| 616 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 617 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 618 | hash = dict_hash_multi(hash, NULL, 0); |
| 619 | |
| 620 | r = lyht_insert(set->ht, &hnode, hash, NULL); |
| 621 | assert(!r); |
| 622 | (void)r; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 623 | |
Michal Vasko | 9d6befd | 2019-12-11 14:56:56 +0100 | [diff] [blame] | 624 | if (hnode.node == node) { |
| 625 | /* it was just added, do not add it twice */ |
| 626 | node = NULL; |
| 627 | } |
| 628 | } |
| 629 | } |
| 630 | |
| 631 | if (set->ht && node) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 632 | /* add the new node into hash table */ |
| 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_insert(set->ht, &hnode, hash, NULL); |
| 641 | assert(!r); |
| 642 | (void)r; |
| 643 | } |
| 644 | } |
| 645 | |
| 646 | /** |
| 647 | * @brief Remove node and its hash from set. |
| 648 | * |
| 649 | * @param[in] set Set to remove from. |
| 650 | * @param[in] node Node to remove. |
| 651 | * @param[in] type Node type. |
| 652 | */ |
| 653 | static void |
| 654 | set_remove_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type) |
| 655 | { |
| 656 | int r; |
| 657 | struct lyxp_set_hash_node hnode; |
| 658 | uint32_t hash; |
| 659 | |
| 660 | if (set->ht) { |
| 661 | hnode.node = node; |
| 662 | hnode.type = type; |
| 663 | |
| 664 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 665 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 666 | hash = dict_hash_multi(hash, NULL, 0); |
| 667 | |
| 668 | r = lyht_remove(set->ht, &hnode, hash); |
| 669 | assert(!r); |
| 670 | (void)r; |
| 671 | |
| 672 | if (!set->ht->used) { |
| 673 | lyht_free(set->ht); |
| 674 | set->ht = NULL; |
| 675 | } |
| 676 | } |
| 677 | } |
| 678 | |
| 679 | /** |
| 680 | * @brief Check whether node is in set based on its hash. |
| 681 | * |
| 682 | * @param[in] set Set to search in. |
| 683 | * @param[in] node Node to search for. |
| 684 | * @param[in] type Node type. |
| 685 | * @param[in] skip_idx Index in @p set to skip. |
| 686 | * @return LY_ERR |
| 687 | */ |
| 688 | static LY_ERR |
| 689 | set_dup_node_hash_check(const struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type, int skip_idx) |
| 690 | { |
| 691 | struct lyxp_set_hash_node hnode, *match_p; |
| 692 | uint32_t hash; |
| 693 | |
| 694 | hnode.node = node; |
| 695 | hnode.type = type; |
| 696 | |
| 697 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 698 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 699 | hash = dict_hash_multi(hash, NULL, 0); |
| 700 | |
| 701 | if (!lyht_find(set->ht, &hnode, hash, (void **)&match_p)) { |
| 702 | if ((skip_idx > -1) && (set->val.nodes[skip_idx].node == match_p->node) && (set->val.nodes[skip_idx].type == match_p->type)) { |
| 703 | /* we found it on the index that should be skipped, find another */ |
| 704 | hnode = *match_p; |
| 705 | if (lyht_find_next(set->ht, &hnode, hash, (void **)&match_p)) { |
| 706 | /* none other found */ |
| 707 | return LY_SUCCESS; |
| 708 | } |
| 709 | } |
| 710 | |
| 711 | return LY_EEXIST; |
| 712 | } |
| 713 | |
| 714 | /* not found */ |
| 715 | return LY_SUCCESS; |
| 716 | } |
| 717 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 718 | void |
| 719 | lyxp_set_free_content(struct lyxp_set *set) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 720 | { |
| 721 | if (!set) { |
| 722 | return; |
| 723 | } |
| 724 | |
| 725 | if (set->type == LYXP_SET_NODE_SET) { |
| 726 | free(set->val.nodes); |
| 727 | lyht_free(set->ht); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 728 | } else if (set->type == LYXP_SET_SCNODE_SET) { |
| 729 | free(set->val.scnodes); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 730 | lyht_free(set->ht); |
| 731 | } else { |
| 732 | if (set->type == LYXP_SET_STRING) { |
| 733 | free(set->val.str); |
| 734 | } |
| 735 | set->type = LYXP_SET_NODE_SET; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 736 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 737 | |
| 738 | set->val.nodes = NULL; |
| 739 | set->used = 0; |
| 740 | set->size = 0; |
| 741 | set->ht = NULL; |
| 742 | set->ctx_pos = 0; |
| 743 | set->ctx_pos = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 744 | } |
| 745 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 746 | /** |
| 747 | * @brief Free dynamically-allocated set. |
| 748 | * |
| 749 | * @param[in] set Set to free. |
| 750 | */ |
| 751 | static void |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 752 | lyxp_set_free(struct lyxp_set *set) |
| 753 | { |
| 754 | if (!set) { |
| 755 | return; |
| 756 | } |
| 757 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 758 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 759 | free(set); |
| 760 | } |
| 761 | |
| 762 | /** |
| 763 | * @brief Initialize set context. |
| 764 | * |
| 765 | * @param[in] new Set to initialize. |
| 766 | * @param[in] set Arbitrary initialized set. |
| 767 | */ |
| 768 | static void |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 769 | set_init(struct lyxp_set *new, const struct lyxp_set *set) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 770 | { |
| 771 | memset(new, 0, sizeof *new); |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 772 | if (set) { |
| 773 | new->ctx = set->ctx; |
| 774 | new->ctx_node = set->ctx_node; |
Michal Vasko | 588112f | 2019-12-10 14:51:53 +0100 | [diff] [blame] | 775 | new->root_type = set->root_type; |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 776 | new->context_op = set->context_op; |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 777 | new->local_mod = set->local_mod; |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 778 | new->tree = set->tree; |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 779 | new->format = set->format; |
| 780 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | /** |
| 784 | * @brief Create a deep copy of a set. |
| 785 | * |
| 786 | * @param[in] set Set to copy. |
| 787 | * @return Copy of @p set. |
| 788 | */ |
| 789 | static struct lyxp_set * |
| 790 | set_copy(struct lyxp_set *set) |
| 791 | { |
| 792 | struct lyxp_set *ret; |
| 793 | uint16_t i; |
Michal Vasko | ba71654 | 2019-12-16 10:01:58 +0100 | [diff] [blame] | 794 | int idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 795 | |
| 796 | if (!set) { |
| 797 | return NULL; |
| 798 | } |
| 799 | |
| 800 | ret = malloc(sizeof *ret); |
| 801 | LY_CHECK_ERR_RET(!ret, LOGMEM(set->ctx), NULL); |
| 802 | set_init(ret, set); |
| 803 | |
| 804 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 805 | ret->type = set->type; |
| 806 | |
| 807 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | ba71654 | 2019-12-16 10:01:58 +0100 | [diff] [blame] | 808 | if ((set->val.scnodes[i].in_ctx == 1) || (set->val.scnodes[i].in_ctx == -2)) { |
| 809 | 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] | 810 | /* coverity seems to think scnodes can be NULL */ |
| 811 | if ((idx == -1) || !ret->val.scnodes) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 812 | lyxp_set_free(ret); |
| 813 | return NULL; |
| 814 | } |
Michal Vasko | ba71654 | 2019-12-16 10:01:58 +0100 | [diff] [blame] | 815 | ret->val.scnodes[idx].in_ctx = set->val.scnodes[i].in_ctx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 816 | } |
| 817 | } |
| 818 | } else if (set->type == LYXP_SET_NODE_SET) { |
| 819 | ret->type = set->type; |
| 820 | ret->val.nodes = malloc(set->used * sizeof *ret->val.nodes); |
| 821 | LY_CHECK_ERR_RET(!ret->val.nodes, LOGMEM(set->ctx); free(ret), NULL); |
| 822 | memcpy(ret->val.nodes, set->val.nodes, set->used * sizeof *ret->val.nodes); |
| 823 | |
| 824 | ret->used = ret->size = set->used; |
| 825 | ret->ctx_pos = set->ctx_pos; |
| 826 | ret->ctx_size = set->ctx_size; |
| 827 | ret->ht = lyht_dup(set->ht); |
| 828 | } else { |
| 829 | memcpy(ret, set, sizeof *ret); |
| 830 | if (set->type == LYXP_SET_STRING) { |
| 831 | ret->val.str = strdup(set->val.str); |
| 832 | LY_CHECK_ERR_RET(!ret->val.str, LOGMEM(set->ctx); free(ret), NULL); |
| 833 | } |
| 834 | } |
| 835 | |
| 836 | return ret; |
| 837 | } |
| 838 | |
| 839 | /** |
| 840 | * @brief Fill XPath set with a string. Any current data are disposed of. |
| 841 | * |
| 842 | * @param[in] set Set to fill. |
| 843 | * @param[in] string String to fill into \p set. |
| 844 | * @param[in] str_len Length of \p string. 0 is a valid value! |
| 845 | */ |
| 846 | static void |
| 847 | set_fill_string(struct lyxp_set *set, const char *string, uint16_t str_len) |
| 848 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 849 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 850 | |
| 851 | set->type = LYXP_SET_STRING; |
| 852 | if ((str_len == 0) && (string[0] != '\0')) { |
| 853 | string = ""; |
| 854 | } |
| 855 | set->val.str = strndup(string, str_len); |
| 856 | } |
| 857 | |
| 858 | /** |
| 859 | * @brief Fill XPath set with a number. Any current data are disposed of. |
| 860 | * |
| 861 | * @param[in] set Set to fill. |
| 862 | * @param[in] number Number to fill into \p set. |
| 863 | */ |
| 864 | static void |
| 865 | set_fill_number(struct lyxp_set *set, long double number) |
| 866 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 867 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 868 | |
| 869 | set->type = LYXP_SET_NUMBER; |
| 870 | set->val.num = number; |
| 871 | } |
| 872 | |
| 873 | /** |
| 874 | * @brief Fill XPath set with a boolean. Any current data are disposed of. |
| 875 | * |
| 876 | * @param[in] set Set to fill. |
| 877 | * @param[in] boolean Boolean to fill into \p set. |
| 878 | */ |
| 879 | static void |
| 880 | set_fill_boolean(struct lyxp_set *set, int boolean) |
| 881 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 882 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 883 | |
| 884 | set->type = LYXP_SET_BOOLEAN; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 885 | set->val.bln = boolean; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 886 | } |
| 887 | |
| 888 | /** |
| 889 | * @brief Fill XPath set with the value from another set (deep assign). |
| 890 | * Any current data are disposed of. |
| 891 | * |
| 892 | * @param[in] trg Set to fill. |
| 893 | * @param[in] src Source set to copy into \p trg. |
| 894 | */ |
| 895 | static void |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 896 | set_fill_set(struct lyxp_set *trg, const struct lyxp_set *src) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 897 | { |
| 898 | if (!trg || !src) { |
| 899 | return; |
| 900 | } |
| 901 | |
| 902 | if (trg->type == LYXP_SET_NODE_SET) { |
| 903 | free(trg->val.nodes); |
| 904 | } else if (trg->type == LYXP_SET_STRING) { |
| 905 | free(trg->val.str); |
| 906 | } |
| 907 | set_init(trg, src); |
| 908 | |
| 909 | if (src->type == LYXP_SET_SCNODE_SET) { |
| 910 | trg->type = LYXP_SET_SCNODE_SET; |
| 911 | trg->used = src->used; |
| 912 | trg->size = src->used; |
| 913 | |
| 914 | trg->val.scnodes = ly_realloc(trg->val.scnodes, trg->size * sizeof *trg->val.scnodes); |
| 915 | LY_CHECK_ERR_RET(!trg->val.scnodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), ); |
| 916 | memcpy(trg->val.scnodes, src->val.scnodes, src->used * sizeof *src->val.scnodes); |
| 917 | } else if (src->type == LYXP_SET_BOOLEAN) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 918 | set_fill_boolean(trg, src->val.bln); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 919 | } else if (src->type == LYXP_SET_NUMBER) { |
| 920 | set_fill_number(trg, src->val.num); |
| 921 | } else if (src->type == LYXP_SET_STRING) { |
| 922 | set_fill_string(trg, src->val.str, strlen(src->val.str)); |
| 923 | } else { |
| 924 | if (trg->type == LYXP_SET_NODE_SET) { |
| 925 | free(trg->val.nodes); |
| 926 | } else if (trg->type == LYXP_SET_STRING) { |
| 927 | free(trg->val.str); |
| 928 | } |
| 929 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 930 | assert(src->type == LYXP_SET_NODE_SET); |
| 931 | |
| 932 | trg->type = LYXP_SET_NODE_SET; |
| 933 | trg->used = src->used; |
| 934 | trg->size = src->used; |
| 935 | trg->ctx_pos = src->ctx_pos; |
| 936 | trg->ctx_size = src->ctx_size; |
| 937 | |
| 938 | trg->val.nodes = malloc(trg->used * sizeof *trg->val.nodes); |
| 939 | LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), ); |
| 940 | memcpy(trg->val.nodes, src->val.nodes, src->used * sizeof *src->val.nodes); |
| 941 | if (src->ht) { |
| 942 | trg->ht = lyht_dup(src->ht); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 943 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 944 | trg->ht = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 945 | } |
| 946 | } |
| 947 | } |
| 948 | |
| 949 | /** |
| 950 | * @brief Clear context of all schema nodes. |
| 951 | * |
| 952 | * @param[in] set Set to clear. |
| 953 | */ |
| 954 | static void |
| 955 | set_scnode_clear_ctx(struct lyxp_set *set) |
| 956 | { |
| 957 | uint32_t i; |
| 958 | |
| 959 | for (i = 0; i < set->used; ++i) { |
| 960 | if (set->val.scnodes[i].in_ctx == 1) { |
| 961 | set->val.scnodes[i].in_ctx = 0; |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 962 | } else if (set->val.scnodes[i].in_ctx == -2) { |
| 963 | set->val.scnodes[i].in_ctx = -1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 964 | } |
| 965 | } |
| 966 | } |
| 967 | |
| 968 | /** |
| 969 | * @brief Remove a node from a set. Removing last node changes |
| 970 | * set into LYXP_SET_EMPTY. Context position aware. |
| 971 | * |
| 972 | * @param[in] set Set to use. |
| 973 | * @param[in] idx Index from @p set of the node to be removed. |
| 974 | */ |
| 975 | static void |
| 976 | set_remove_node(struct lyxp_set *set, uint32_t idx) |
| 977 | { |
| 978 | assert(set && (set->type == LYXP_SET_NODE_SET)); |
| 979 | assert(idx < set->used); |
| 980 | |
| 981 | set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
| 982 | |
| 983 | --set->used; |
| 984 | if (set->used) { |
| 985 | memmove(&set->val.nodes[idx], &set->val.nodes[idx + 1], |
| 986 | (set->used - idx) * sizeof *set->val.nodes); |
| 987 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 988 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 989 | } |
| 990 | } |
| 991 | |
| 992 | /** |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 993 | * @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] | 994 | * |
| 995 | * @param[in] set Set to use. |
| 996 | * @param[in] idx Index from @p set of the node to be removed. |
| 997 | */ |
| 998 | static void |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 999 | set_remove_node_none(struct lyxp_set *set, uint32_t idx) |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1000 | { |
| 1001 | assert(set && (set->type == LYXP_SET_NODE_SET)); |
| 1002 | assert(idx < set->used); |
| 1003 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1004 | if (set->val.nodes[idx].type == LYXP_NODE_ELEM) { |
| 1005 | set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
| 1006 | } |
| 1007 | set->val.nodes[idx].type = LYXP_NODE_NONE; |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1008 | } |
| 1009 | |
| 1010 | /** |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1011 | * @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] | 1012 | * set into LYXP_SET_EMPTY. Context position aware. |
| 1013 | * |
| 1014 | * @param[in] set Set to consolidate. |
| 1015 | */ |
| 1016 | static void |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1017 | set_remove_nodes_none(struct lyxp_set *set) |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1018 | { |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 1019 | uint16_t i, orig_used, end = 0; |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1020 | int32_t start; |
| 1021 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1022 | assert(set); |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1023 | |
| 1024 | orig_used = set->used; |
| 1025 | set->used = 0; |
| 1026 | for (i = 0; i < orig_used;) { |
| 1027 | start = -1; |
| 1028 | do { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1029 | if ((set->val.nodes[i].type != LYXP_NODE_NONE) && (start == -1)) { |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1030 | start = i; |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1031 | } else if ((start > -1) && (set->val.nodes[i].type == LYXP_NODE_NONE)) { |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1032 | end = i; |
| 1033 | ++i; |
| 1034 | break; |
| 1035 | } |
| 1036 | |
| 1037 | ++i; |
| 1038 | if (i == orig_used) { |
| 1039 | end = i; |
| 1040 | } |
| 1041 | } while (i < orig_used); |
| 1042 | |
| 1043 | if (start > -1) { |
| 1044 | /* move the whole chunk of valid nodes together */ |
| 1045 | if (set->used != (unsigned)start) { |
| 1046 | memmove(&set->val.nodes[set->used], &set->val.nodes[start], (end - start) * sizeof *set->val.nodes); |
| 1047 | } |
| 1048 | set->used += end - start; |
| 1049 | } |
| 1050 | } |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1051 | } |
| 1052 | |
| 1053 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1054 | * @brief Check for duplicates in a node set. |
| 1055 | * |
| 1056 | * @param[in] set Set to check. |
| 1057 | * @param[in] node Node to look for in @p set. |
| 1058 | * @param[in] node_type Type of @p node. |
| 1059 | * @param[in] skip_idx Index from @p set to skip. |
| 1060 | * @return LY_ERR |
| 1061 | */ |
| 1062 | static LY_ERR |
| 1063 | set_dup_node_check(const struct lyxp_set *set, const struct lyd_node *node, enum lyxp_node_type node_type, int skip_idx) |
| 1064 | { |
| 1065 | uint32_t i; |
| 1066 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1067 | if (set->ht && node) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1068 | return set_dup_node_hash_check(set, (struct lyd_node *)node, node_type, skip_idx); |
| 1069 | } |
| 1070 | |
| 1071 | for (i = 0; i < set->used; ++i) { |
| 1072 | if ((skip_idx > -1) && (i == (unsigned)skip_idx)) { |
| 1073 | continue; |
| 1074 | } |
| 1075 | |
| 1076 | if ((set->val.nodes[i].node == node) && (set->val.nodes[i].type == node_type)) { |
| 1077 | return LY_EEXIST; |
| 1078 | } |
| 1079 | } |
| 1080 | |
| 1081 | return LY_SUCCESS; |
| 1082 | } |
| 1083 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 1084 | int |
| 1085 | 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] | 1086 | { |
| 1087 | uint32_t i; |
| 1088 | |
| 1089 | for (i = 0; i < set->used; ++i) { |
| 1090 | if ((skip_idx > -1) && (i == (unsigned)skip_idx)) { |
| 1091 | continue; |
| 1092 | } |
| 1093 | |
| 1094 | if ((set->val.scnodes[i].scnode == node) && (set->val.scnodes[i].type == node_type)) { |
| 1095 | return i; |
| 1096 | } |
| 1097 | } |
| 1098 | |
| 1099 | return -1; |
| 1100 | } |
| 1101 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 1102 | void |
| 1103 | lyxp_set_scnode_merge(struct lyxp_set *set1, struct lyxp_set *set2) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1104 | { |
| 1105 | uint32_t orig_used, i, j; |
| 1106 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1107 | 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] | 1108 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1109 | if (!set2->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1110 | return; |
| 1111 | } |
| 1112 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1113 | if (!set1->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1114 | memcpy(set1, set2, sizeof *set1); |
| 1115 | return; |
| 1116 | } |
| 1117 | |
| 1118 | if (set1->used + set2->used > set1->size) { |
| 1119 | set1->size = set1->used + set2->used; |
| 1120 | set1->val.scnodes = ly_realloc(set1->val.scnodes, set1->size * sizeof *set1->val.scnodes); |
| 1121 | LY_CHECK_ERR_RET(!set1->val.scnodes, LOGMEM(set1->ctx), ); |
| 1122 | } |
| 1123 | |
| 1124 | orig_used = set1->used; |
| 1125 | |
| 1126 | for (i = 0; i < set2->used; ++i) { |
| 1127 | for (j = 0; j < orig_used; ++j) { |
| 1128 | /* detect duplicities */ |
| 1129 | if (set1->val.scnodes[j].scnode == set2->val.scnodes[i].scnode) { |
| 1130 | break; |
| 1131 | } |
| 1132 | } |
| 1133 | |
| 1134 | if (j == orig_used) { |
| 1135 | memcpy(&set1->val.scnodes[set1->used], &set2->val.scnodes[i], sizeof *set2->val.scnodes); |
| 1136 | ++set1->used; |
| 1137 | } |
| 1138 | } |
| 1139 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1140 | lyxp_set_free_content(set2); |
| 1141 | set2->type = LYXP_SET_SCNODE_SET; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1142 | } |
| 1143 | |
| 1144 | /** |
| 1145 | * @brief Insert a node into a set. Context position aware. |
| 1146 | * |
| 1147 | * @param[in] set Set to use. |
| 1148 | * @param[in] node Node to insert to @p set. |
| 1149 | * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting. |
| 1150 | * @param[in] node_type Node type of @p node. |
| 1151 | * @param[in] idx Index in @p set to insert into. |
| 1152 | */ |
| 1153 | static void |
| 1154 | set_insert_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx) |
| 1155 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1156 | assert(set && (set->type == LYXP_SET_NODE_SET)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1157 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1158 | if (!set->size) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1159 | /* first item */ |
| 1160 | if (idx) { |
| 1161 | /* no real harm done, but it is a bug */ |
| 1162 | LOGINT(set->ctx); |
| 1163 | idx = 0; |
| 1164 | } |
| 1165 | set->val.nodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.nodes); |
| 1166 | LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), ); |
| 1167 | set->type = LYXP_SET_NODE_SET; |
| 1168 | set->used = 0; |
| 1169 | set->size = LYXP_SET_SIZE_START; |
| 1170 | set->ctx_pos = 1; |
| 1171 | set->ctx_size = 1; |
| 1172 | set->ht = NULL; |
| 1173 | } else { |
| 1174 | /* not an empty set */ |
| 1175 | if (set->used == set->size) { |
| 1176 | |
| 1177 | /* set is full */ |
| 1178 | set->val.nodes = ly_realloc(set->val.nodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.nodes); |
| 1179 | LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), ); |
| 1180 | set->size += LYXP_SET_SIZE_STEP; |
| 1181 | } |
| 1182 | |
| 1183 | if (idx > set->used) { |
| 1184 | LOGINT(set->ctx); |
| 1185 | idx = set->used; |
| 1186 | } |
| 1187 | |
| 1188 | /* make space for the new node */ |
| 1189 | if (idx < set->used) { |
| 1190 | memmove(&set->val.nodes[idx + 1], &set->val.nodes[idx], (set->used - idx) * sizeof *set->val.nodes); |
| 1191 | } |
| 1192 | } |
| 1193 | |
| 1194 | /* finally assign the value */ |
| 1195 | set->val.nodes[idx].node = (struct lyd_node *)node; |
| 1196 | set->val.nodes[idx].type = node_type; |
| 1197 | set->val.nodes[idx].pos = pos; |
| 1198 | ++set->used; |
| 1199 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1200 | if (set->val.nodes[idx].type == LYXP_NODE_ELEM) { |
| 1201 | set_insert_node_hash(set, (struct lyd_node *)node, node_type); |
| 1202 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1203 | } |
| 1204 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 1205 | int |
| 1206 | 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] | 1207 | { |
| 1208 | int ret; |
| 1209 | |
| 1210 | assert(set->type == LYXP_SET_SCNODE_SET); |
| 1211 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 1212 | ret = lyxp_set_scnode_dup_node_check(set, node, node_type, -1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1213 | if (ret > -1) { |
| 1214 | set->val.scnodes[ret].in_ctx = 1; |
| 1215 | } else { |
| 1216 | if (set->used == set->size) { |
| 1217 | set->val.scnodes = ly_realloc(set->val.scnodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.scnodes); |
| 1218 | LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), -1); |
| 1219 | set->size += LYXP_SET_SIZE_STEP; |
| 1220 | } |
| 1221 | |
| 1222 | ret = set->used; |
| 1223 | set->val.scnodes[ret].scnode = (struct lysc_node *)node; |
| 1224 | set->val.scnodes[ret].type = node_type; |
| 1225 | set->val.scnodes[ret].in_ctx = 1; |
| 1226 | ++set->used; |
| 1227 | } |
| 1228 | |
| 1229 | return ret; |
| 1230 | } |
| 1231 | |
| 1232 | /** |
| 1233 | * @brief Replace a node in a set with another. Context position aware. |
| 1234 | * |
| 1235 | * @param[in] set Set to use. |
| 1236 | * @param[in] node Node to insert to @p set. |
| 1237 | * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting. |
| 1238 | * @param[in] node_type Node type of @p node. |
| 1239 | * @param[in] idx Index in @p set of the node to replace. |
| 1240 | */ |
| 1241 | static void |
| 1242 | set_replace_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx) |
| 1243 | { |
| 1244 | assert(set && (idx < set->used)); |
| 1245 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1246 | if (set->val.nodes[idx].type == LYXP_NODE_ELEM) { |
| 1247 | set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
| 1248 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1249 | set->val.nodes[idx].node = (struct lyd_node *)node; |
| 1250 | set->val.nodes[idx].type = node_type; |
| 1251 | set->val.nodes[idx].pos = pos; |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1252 | if (set->val.nodes[idx].type == LYXP_NODE_ELEM) { |
| 1253 | set_insert_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
| 1254 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1255 | } |
| 1256 | |
| 1257 | /** |
| 1258 | * @brief Set all nodes with ctx 1 to a new unique context value. |
| 1259 | * |
| 1260 | * @param[in] set Set to modify. |
| 1261 | * @return New context value. |
| 1262 | */ |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 1263 | static int32_t |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1264 | set_scnode_new_in_ctx(struct lyxp_set *set) |
| 1265 | { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 1266 | uint32_t i; |
| 1267 | int32_t ret_ctx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1268 | |
| 1269 | assert(set->type == LYXP_SET_SCNODE_SET); |
| 1270 | |
| 1271 | ret_ctx = 3; |
| 1272 | retry: |
| 1273 | for (i = 0; i < set->used; ++i) { |
| 1274 | if (set->val.scnodes[i].in_ctx >= ret_ctx) { |
| 1275 | ret_ctx = set->val.scnodes[i].in_ctx + 1; |
| 1276 | goto retry; |
| 1277 | } |
| 1278 | } |
| 1279 | for (i = 0; i < set->used; ++i) { |
| 1280 | if (set->val.scnodes[i].in_ctx == 1) { |
| 1281 | set->val.scnodes[i].in_ctx = ret_ctx; |
| 1282 | } |
| 1283 | } |
| 1284 | |
| 1285 | return ret_ctx; |
| 1286 | } |
| 1287 | |
| 1288 | /** |
| 1289 | * @brief Get unique @p node position in the data. |
| 1290 | * |
| 1291 | * @param[in] node Node to find. |
| 1292 | * @param[in] node_type Node type of @p node. |
| 1293 | * @param[in] root Root node. |
| 1294 | * @param[in] root_type Type of the XPath @p root node. |
| 1295 | * @param[in] prev Node that we think is before @p node in DFS from @p root. Can optionally |
| 1296 | * be used to increase efficiency and start the DFS from this node. |
| 1297 | * @param[in] prev_pos Node @p prev position. Optional, but must be set if @p prev is set. |
| 1298 | * @return Node position. |
| 1299 | */ |
| 1300 | static uint32_t |
| 1301 | get_node_pos(const struct lyd_node *node, enum lyxp_node_type node_type, const struct lyd_node *root, |
| 1302 | enum lyxp_node_type root_type, const struct lyd_node **prev, uint32_t *prev_pos) |
| 1303 | { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1304 | const struct lyd_node *elem = NULL, *top_sibling; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1305 | uint32_t pos = 1; |
| 1306 | |
| 1307 | assert(prev && prev_pos && !root->prev->next); |
| 1308 | |
| 1309 | if ((node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ROOT_CONFIG)) { |
| 1310 | return 0; |
| 1311 | } |
| 1312 | |
| 1313 | if (*prev) { |
| 1314 | /* start from the previous element instead from the root */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1315 | pos = *prev_pos; |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1316 | 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] | 1317 | goto dfs_search; |
| 1318 | } |
| 1319 | |
| 1320 | for (top_sibling = root; top_sibling; top_sibling = top_sibling->next) { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1321 | LYD_TREE_DFS_BEGIN(top_sibling, elem) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1322 | dfs_search: |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1323 | if (*prev && !elem) { |
| 1324 | /* resume previous DFS */ |
| 1325 | elem = LYD_TREE_DFS_next = (struct lyd_node *)*prev; |
| 1326 | LYD_TREE_DFS_continue = 0; |
| 1327 | } |
| 1328 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1329 | 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] | 1330 | /* skip */ |
| 1331 | LYD_TREE_DFS_continue = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1332 | } else { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1333 | if (elem == node) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1334 | break; |
| 1335 | } |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1336 | ++pos; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1337 | } |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1338 | |
| 1339 | LYD_TREE_DFS_END(top_sibling, elem); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1340 | } |
| 1341 | |
| 1342 | /* node found */ |
| 1343 | if (elem) { |
| 1344 | break; |
| 1345 | } |
| 1346 | } |
| 1347 | |
| 1348 | if (!elem) { |
| 1349 | if (!(*prev)) { |
| 1350 | /* we went from root and failed to find it, cannot be */ |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1351 | LOGINT(LYD_NODE_CTX(node)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1352 | return 0; |
| 1353 | } else { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1354 | /* start the search again from the beginning */ |
| 1355 | *prev = root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1356 | |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1357 | top_sibling = root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1358 | pos = 1; |
| 1359 | goto dfs_search; |
| 1360 | } |
| 1361 | } |
| 1362 | |
| 1363 | /* remember the last found node for next time */ |
| 1364 | *prev = node; |
| 1365 | *prev_pos = pos; |
| 1366 | |
| 1367 | return pos; |
| 1368 | } |
| 1369 | |
| 1370 | /** |
| 1371 | * @brief Assign (fill) missing node positions. |
| 1372 | * |
| 1373 | * @param[in] set Set to fill positions in. |
| 1374 | * @param[in] root Context root node. |
| 1375 | * @param[in] root_type Context root type. |
| 1376 | * @return LY_ERR |
| 1377 | */ |
| 1378 | static LY_ERR |
| 1379 | set_assign_pos(struct lyxp_set *set, const struct lyd_node *root, enum lyxp_node_type root_type) |
| 1380 | { |
| 1381 | const struct lyd_node *prev = NULL, *tmp_node; |
| 1382 | uint32_t i, tmp_pos = 0; |
| 1383 | |
| 1384 | for (i = 0; i < set->used; ++i) { |
| 1385 | if (!set->val.nodes[i].pos) { |
| 1386 | tmp_node = NULL; |
| 1387 | switch (set->val.nodes[i].type) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1388 | case LYXP_NODE_META: |
| 1389 | tmp_node = set->val.meta[i].meta->parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1390 | if (!tmp_node) { |
| 1391 | LOGINT_RET(root->schema->module->ctx); |
| 1392 | } |
| 1393 | /* fallthrough */ |
| 1394 | case LYXP_NODE_ELEM: |
| 1395 | case LYXP_NODE_TEXT: |
| 1396 | if (!tmp_node) { |
| 1397 | tmp_node = set->val.nodes[i].node; |
| 1398 | } |
| 1399 | set->val.nodes[i].pos = get_node_pos(tmp_node, set->val.nodes[i].type, root, root_type, &prev, &tmp_pos); |
| 1400 | break; |
| 1401 | default: |
| 1402 | /* all roots have position 0 */ |
| 1403 | break; |
| 1404 | } |
| 1405 | } |
| 1406 | } |
| 1407 | |
| 1408 | return LY_SUCCESS; |
| 1409 | } |
| 1410 | |
| 1411 | /** |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1412 | * @brief Get unique @p meta position in the parent metadata. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1413 | * |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1414 | * @param[in] meta Metadata to use. |
| 1415 | * @return Metadata position. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1416 | */ |
| 1417 | static uint16_t |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1418 | get_meta_pos(struct lyd_meta *meta) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1419 | { |
| 1420 | uint16_t pos = 0; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1421 | struct lyd_meta *meta2; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1422 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1423 | for (meta2 = meta->parent->meta; meta2 && (meta2 != meta); meta2 = meta2->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1424 | ++pos; |
| 1425 | } |
| 1426 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1427 | assert(meta2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1428 | return pos; |
| 1429 | } |
| 1430 | |
| 1431 | /** |
| 1432 | * @brief Compare 2 nodes in respect to XPath document order. |
| 1433 | * |
| 1434 | * @param[in] item1 1st node. |
| 1435 | * @param[in] item2 2nd node. |
| 1436 | * @return If 1st > 2nd returns 1, 1st == 2nd returns 0, and 1st < 2nd returns -1. |
| 1437 | */ |
| 1438 | static int |
| 1439 | set_sort_compare(struct lyxp_set_node *item1, struct lyxp_set_node *item2) |
| 1440 | { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1441 | uint32_t meta_pos1 = 0, meta_pos2 = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1442 | |
| 1443 | if (item1->pos < item2->pos) { |
| 1444 | return -1; |
| 1445 | } |
| 1446 | |
| 1447 | if (item1->pos > item2->pos) { |
| 1448 | return 1; |
| 1449 | } |
| 1450 | |
| 1451 | /* node positions are equal, the fun case */ |
| 1452 | |
| 1453 | /* 1st ELEM - == - 2nd TEXT, 1st TEXT - == - 2nd ELEM */ |
| 1454 | /* special case since text nodes are actually saved as their parents */ |
| 1455 | if ((item1->node == item2->node) && (item1->type != item2->type)) { |
| 1456 | if (item1->type == LYXP_NODE_ELEM) { |
| 1457 | assert(item2->type == LYXP_NODE_TEXT); |
| 1458 | return -1; |
| 1459 | } else { |
| 1460 | assert((item1->type == LYXP_NODE_TEXT) && (item2->type == LYXP_NODE_ELEM)); |
| 1461 | return 1; |
| 1462 | } |
| 1463 | } |
| 1464 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1465 | /* we need meta positions now */ |
| 1466 | if (item1->type == LYXP_NODE_META) { |
| 1467 | meta_pos1 = get_meta_pos((struct lyd_meta *)item1->node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1468 | } |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1469 | if (item2->type == LYXP_NODE_META) { |
| 1470 | meta_pos2 = get_meta_pos((struct lyd_meta *)item2->node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1471 | } |
| 1472 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1473 | /* 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] | 1474 | /* check for duplicates */ |
| 1475 | if (item1->node == item2->node) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1476 | 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] | 1477 | return 0; |
| 1478 | } |
| 1479 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1480 | /* 1st ELEM - 2nd TEXT, 1st ELEM - any pos - 2nd META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1481 | /* elem is always first, 2nd node is after it */ |
| 1482 | if (item1->type == LYXP_NODE_ELEM) { |
| 1483 | assert(item2->type != LYXP_NODE_ELEM); |
| 1484 | return -1; |
| 1485 | } |
| 1486 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1487 | /* 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] | 1488 | /* 2nd is before 1st */ |
| 1489 | if (((item1->type == LYXP_NODE_TEXT) |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1490 | && ((item2->type == LYXP_NODE_ELEM) || (item2->type == LYXP_NODE_META))) |
| 1491 | || ((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_ELEM)) |
| 1492 | || (((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_META)) |
| 1493 | && (meta_pos1 > meta_pos2))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1494 | return 1; |
| 1495 | } |
| 1496 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1497 | /* 1st META - any pos - 2nd TEXT, 1st META <pos< - 2nd META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1498 | /* 2nd is after 1st */ |
| 1499 | return -1; |
| 1500 | } |
| 1501 | |
| 1502 | /** |
| 1503 | * @brief Set cast for comparisons. |
| 1504 | * |
| 1505 | * @param[in] trg Target set to cast source into. |
| 1506 | * @param[in] src Source set. |
| 1507 | * @param[in] type Target set type. |
| 1508 | * @param[in] src_idx Source set node index. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1509 | * @return LY_ERR |
| 1510 | */ |
| 1511 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1512 | 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] | 1513 | { |
| 1514 | assert(src->type == LYXP_SET_NODE_SET); |
| 1515 | |
| 1516 | set_init(trg, src); |
| 1517 | |
| 1518 | /* insert node into target set */ |
| 1519 | set_insert_node(trg, src->val.nodes[src_idx].node, src->val.nodes[src_idx].pos, src->val.nodes[src_idx].type, 0); |
| 1520 | |
| 1521 | /* cast target set appropriately */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1522 | return lyxp_set_cast(trg, type); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1523 | } |
| 1524 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1525 | /** |
| 1526 | * @brief Set content canonization for comparisons. |
| 1527 | * |
| 1528 | * @param[in] trg Target set to put the canononized source into. |
| 1529 | * @param[in] src Source set. |
| 1530 | * @param[in] xp_node Source XPath node/meta to use for canonization. |
| 1531 | * @return LY_ERR |
| 1532 | */ |
| 1533 | static LY_ERR |
| 1534 | set_comp_canonize(struct lyxp_set *trg, const struct lyxp_set *src, const struct lyxp_set_node *xp_node) |
| 1535 | { |
| 1536 | struct lysc_type *type = NULL; |
| 1537 | struct lyd_value val; |
| 1538 | struct ly_err_item *err = NULL; |
| 1539 | char *str, *ptr; |
| 1540 | int dynamic; |
| 1541 | LY_ERR rc; |
| 1542 | |
| 1543 | /* is there anything to canonize even? */ |
| 1544 | if ((src->type == LYXP_SET_NUMBER) || (src->type == LYXP_SET_STRING)) { |
| 1545 | /* do we have a type to use for canonization? */ |
| 1546 | if ((xp_node->type == LYXP_NODE_ELEM) && (xp_node->node->schema->nodetype & LYD_NODE_TERM)) { |
| 1547 | type = ((struct lyd_node_term *)xp_node->node)->value.realtype; |
| 1548 | } else if (xp_node->type == LYXP_NODE_META) { |
| 1549 | type = ((struct lyd_meta *)xp_node->node)->value.realtype; |
| 1550 | } |
| 1551 | } |
| 1552 | if (!type) { |
| 1553 | goto fill; |
| 1554 | } |
| 1555 | |
| 1556 | if (src->type == LYXP_SET_NUMBER) { |
| 1557 | /* canonize number */ |
| 1558 | if (asprintf(&str, "%Lf", src->val.num) == -1) { |
| 1559 | LOGMEM(src->ctx); |
| 1560 | return LY_EMEM; |
| 1561 | } |
| 1562 | } else { |
| 1563 | /* canonize string */ |
| 1564 | str = strdup(src->val.str); |
| 1565 | } |
| 1566 | |
| 1567 | /* ignore errors, the value may not satisfy schema constraints */ |
| 1568 | rc = type->plugin->store(src->ctx, type, str, strlen(str), |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 1569 | LY_TYPE_OPTS_STORE | LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_DYNAMIC, LY_PREF_JSON, |
| 1570 | NULL, NULL, NULL, &val, NULL, &err); |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1571 | ly_err_free(err); |
| 1572 | if (rc) { |
| 1573 | /* invalid value */ |
| 1574 | free(str); |
| 1575 | goto fill; |
| 1576 | } |
| 1577 | |
| 1578 | /* storing successful, now print the canonical value */ |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 1579 | str = (char *)type->plugin->print(&val, LY_PREF_JSON, NULL, &dynamic); |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1580 | |
| 1581 | /* use the canonized value */ |
| 1582 | set_init(trg, src); |
| 1583 | trg->type = src->type; |
| 1584 | if (src->type == LYXP_SET_NUMBER) { |
| 1585 | trg->val.num = strtold(str, &ptr); |
| 1586 | if (dynamic) { |
| 1587 | free(str); |
| 1588 | } |
| 1589 | LY_CHECK_ERR_RET(ptr[0], LOGINT(src->ctx), LY_EINT); |
| 1590 | } else { |
| 1591 | trg->val.str = (dynamic ? str : strdup(str)); |
| 1592 | } |
| 1593 | type->plugin->free(src->ctx, &val); |
| 1594 | return LY_SUCCESS; |
| 1595 | |
| 1596 | fill: |
| 1597 | /* no canonization needed/possible */ |
| 1598 | set_fill_set(trg, src); |
| 1599 | return LY_SUCCESS; |
| 1600 | } |
| 1601 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1602 | #ifndef NDEBUG |
| 1603 | |
| 1604 | /** |
| 1605 | * @brief Bubble sort @p set into XPath document order. |
| 1606 | * Context position aware. Unused in the 'Release' build target. |
| 1607 | * |
| 1608 | * @param[in] set Set to sort. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1609 | * @return How many times the whole set was traversed - 1 (if set was sorted, returns 0). |
| 1610 | */ |
| 1611 | static int |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1612 | set_sort(struct lyxp_set *set) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1613 | { |
| 1614 | uint32_t i, j; |
| 1615 | int ret = 0, cmp, inverted, change; |
| 1616 | const struct lyd_node *root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1617 | struct lyxp_set_node item; |
| 1618 | struct lyxp_set_hash_node hnode; |
| 1619 | uint64_t hash; |
| 1620 | |
Michal Vasko | 3cf8fbf | 2020-05-27 15:21:21 +0200 | [diff] [blame] | 1621 | if ((set->type != LYXP_SET_NODE_SET) || (set->used < 2)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1622 | return 0; |
| 1623 | } |
| 1624 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1625 | /* find first top-level node to be used as anchor for positions */ |
| 1626 | for (root = set->ctx_node; root->parent; root = (const struct lyd_node *)root->parent); |
| 1627 | for (; root->prev->next; root = root->prev); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1628 | |
| 1629 | /* fill positions */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1630 | if (set_assign_pos(set, root, set->root_type)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1631 | return -1; |
| 1632 | } |
| 1633 | |
| 1634 | LOGDBG(LY_LDGXPATH, "SORT BEGIN"); |
| 1635 | print_set_debug(set); |
| 1636 | |
| 1637 | for (i = 0; i < set->used; ++i) { |
| 1638 | inverted = 0; |
| 1639 | change = 0; |
| 1640 | |
| 1641 | for (j = 1; j < set->used - i; ++j) { |
| 1642 | /* compare node positions */ |
| 1643 | if (inverted) { |
| 1644 | cmp = set_sort_compare(&set->val.nodes[j], &set->val.nodes[j - 1]); |
| 1645 | } else { |
| 1646 | cmp = set_sort_compare(&set->val.nodes[j - 1], &set->val.nodes[j]); |
| 1647 | } |
| 1648 | |
| 1649 | /* swap if needed */ |
| 1650 | if ((inverted && (cmp < 0)) || (!inverted && (cmp > 0))) { |
| 1651 | change = 1; |
| 1652 | |
| 1653 | item = set->val.nodes[j - 1]; |
| 1654 | set->val.nodes[j - 1] = set->val.nodes[j]; |
| 1655 | set->val.nodes[j] = item; |
| 1656 | } else { |
| 1657 | /* whether node_pos1 should be smaller than node_pos2 or the other way around */ |
| 1658 | inverted = !inverted; |
| 1659 | } |
| 1660 | } |
| 1661 | |
| 1662 | ++ret; |
| 1663 | |
| 1664 | if (!change) { |
| 1665 | break; |
| 1666 | } |
| 1667 | } |
| 1668 | |
| 1669 | LOGDBG(LY_LDGXPATH, "SORT END %d", ret); |
| 1670 | print_set_debug(set); |
| 1671 | |
| 1672 | /* check node hashes */ |
| 1673 | if (set->used >= LYD_HT_MIN_ITEMS) { |
| 1674 | assert(set->ht); |
| 1675 | for (i = 0; i < set->used; ++i) { |
| 1676 | hnode.node = set->val.nodes[i].node; |
| 1677 | hnode.type = set->val.nodes[i].type; |
| 1678 | |
| 1679 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 1680 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 1681 | hash = dict_hash_multi(hash, NULL, 0); |
| 1682 | |
| 1683 | assert(!lyht_find(set->ht, &hnode, hash, NULL)); |
| 1684 | } |
| 1685 | } |
| 1686 | |
| 1687 | return ret - 1; |
| 1688 | } |
| 1689 | |
| 1690 | /** |
| 1691 | * @brief Remove duplicate entries in a sorted node set. |
| 1692 | * |
| 1693 | * @param[in] set Sorted set to check. |
| 1694 | * @return LY_ERR (LY_EEXIST if some duplicates are found) |
| 1695 | */ |
| 1696 | static LY_ERR |
| 1697 | set_sorted_dup_node_clean(struct lyxp_set *set) |
| 1698 | { |
| 1699 | uint32_t i = 0; |
| 1700 | LY_ERR ret = LY_SUCCESS; |
| 1701 | |
| 1702 | if (set->used > 1) { |
| 1703 | while (i < set->used - 1) { |
| 1704 | if ((set->val.nodes[i].node == set->val.nodes[i + 1].node) |
| 1705 | && (set->val.nodes[i].type == set->val.nodes[i + 1].type)) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1706 | set_remove_node_none(set, i + 1); |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1707 | ret = LY_EEXIST; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1708 | } |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1709 | ++i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1710 | } |
| 1711 | } |
| 1712 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1713 | set_remove_nodes_none(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1714 | return ret; |
| 1715 | } |
| 1716 | |
| 1717 | #endif |
| 1718 | |
| 1719 | /** |
| 1720 | * @brief Merge 2 sorted sets into one. |
| 1721 | * |
| 1722 | * @param[in,out] trg Set to merge into. Duplicates are removed. |
| 1723 | * @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] | 1724 | * @return LY_ERR |
| 1725 | */ |
| 1726 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1727 | set_sorted_merge(struct lyxp_set *trg, struct lyxp_set *src) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1728 | { |
| 1729 | uint32_t i, j, k, count, dup_count; |
| 1730 | int cmp; |
| 1731 | const struct lyd_node *root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1732 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1733 | 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] | 1734 | return LY_EINVAL; |
| 1735 | } |
| 1736 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1737 | if (!src->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1738 | return LY_SUCCESS; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1739 | } else if (!trg->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1740 | set_fill_set(trg, src); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1741 | lyxp_set_free_content(src); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1742 | return LY_SUCCESS; |
| 1743 | } |
| 1744 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1745 | /* find first top-level node to be used as anchor for positions */ |
| 1746 | for (root = trg->ctx_node; root->parent; root = (const struct lyd_node *)root->parent); |
| 1747 | for (; root->prev->next; root = root->prev); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1748 | |
| 1749 | /* fill positions */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1750 | 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] | 1751 | return LY_EINT; |
| 1752 | } |
| 1753 | |
| 1754 | #ifndef NDEBUG |
| 1755 | LOGDBG(LY_LDGXPATH, "MERGE target"); |
| 1756 | print_set_debug(trg); |
| 1757 | LOGDBG(LY_LDGXPATH, "MERGE source"); |
| 1758 | print_set_debug(src); |
| 1759 | #endif |
| 1760 | |
| 1761 | /* make memory for the merge (duplicates are not detected yet, so space |
| 1762 | * will likely be wasted on them, too bad) */ |
| 1763 | if (trg->size - trg->used < src->used) { |
| 1764 | trg->size = trg->used + src->used; |
| 1765 | |
| 1766 | trg->val.nodes = ly_realloc(trg->val.nodes, trg->size * sizeof *trg->val.nodes); |
| 1767 | LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx), LY_EMEM); |
| 1768 | } |
| 1769 | |
| 1770 | i = 0; |
| 1771 | j = 0; |
| 1772 | count = 0; |
| 1773 | dup_count = 0; |
| 1774 | do { |
| 1775 | cmp = set_sort_compare(&src->val.nodes[i], &trg->val.nodes[j]); |
| 1776 | if (!cmp) { |
| 1777 | if (!count) { |
| 1778 | /* duplicate, just skip it */ |
| 1779 | ++i; |
| 1780 | ++j; |
| 1781 | } else { |
| 1782 | /* we are copying something already, so let's copy the duplicate too, |
| 1783 | * we are hoping that afterwards there are some more nodes to |
| 1784 | * copy and this way we can copy them all together */ |
| 1785 | ++count; |
| 1786 | ++dup_count; |
| 1787 | ++i; |
| 1788 | ++j; |
| 1789 | } |
| 1790 | } else if (cmp < 0) { |
| 1791 | /* inserting src node into trg, just remember it for now */ |
| 1792 | ++count; |
| 1793 | ++i; |
| 1794 | |
| 1795 | /* insert the hash now */ |
| 1796 | set_insert_node_hash(trg, src->val.nodes[i - 1].node, src->val.nodes[i - 1].type); |
| 1797 | } else if (count) { |
| 1798 | copy_nodes: |
| 1799 | /* time to actually copy the nodes, we have found the largest block of nodes */ |
| 1800 | memmove(&trg->val.nodes[j + (count - dup_count)], |
| 1801 | &trg->val.nodes[j], |
| 1802 | (trg->used - j) * sizeof *trg->val.nodes); |
| 1803 | memcpy(&trg->val.nodes[j - dup_count], &src->val.nodes[i - count], count * sizeof *src->val.nodes); |
| 1804 | |
| 1805 | trg->used += count - dup_count; |
| 1806 | /* do not change i, except the copying above, we are basically doing exactly what is in the else branch below */ |
| 1807 | j += count - dup_count; |
| 1808 | |
| 1809 | count = 0; |
| 1810 | dup_count = 0; |
| 1811 | } else { |
| 1812 | ++j; |
| 1813 | } |
| 1814 | } while ((i < src->used) && (j < trg->used)); |
| 1815 | |
| 1816 | if ((i < src->used) || count) { |
| 1817 | /* insert all the hashes first */ |
| 1818 | for (k = i; k < src->used; ++k) { |
| 1819 | set_insert_node_hash(trg, src->val.nodes[k].node, src->val.nodes[k].type); |
| 1820 | } |
| 1821 | |
| 1822 | /* loop ended, but we need to copy something at trg end */ |
| 1823 | count += src->used - i; |
| 1824 | i = src->used; |
| 1825 | goto copy_nodes; |
| 1826 | } |
| 1827 | |
| 1828 | /* we are inserting hashes before the actual node insert, which causes |
| 1829 | * situations when there were initially not enough items for a hash table, |
| 1830 | * but even after some were inserted, hash table was not created (during |
| 1831 | * insertion the number of items is not updated yet) */ |
| 1832 | if (!trg->ht && (trg->used >= LYD_HT_MIN_ITEMS)) { |
| 1833 | set_insert_node_hash(trg, NULL, 0); |
| 1834 | } |
| 1835 | |
| 1836 | #ifndef NDEBUG |
| 1837 | LOGDBG(LY_LDGXPATH, "MERGE result"); |
| 1838 | print_set_debug(trg); |
| 1839 | #endif |
| 1840 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1841 | lyxp_set_free_content(src); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1842 | return LY_SUCCESS; |
| 1843 | } |
| 1844 | |
| 1845 | /* |
| 1846 | * (re)parse functions |
| 1847 | * |
| 1848 | * Parse functions parse the expression into |
| 1849 | * tokens (syntactic analysis). |
| 1850 | * |
| 1851 | * Reparse functions perform semantic analysis |
| 1852 | * (do not save the result, just a check) of |
| 1853 | * the expression and fill repeat indices. |
| 1854 | */ |
| 1855 | |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1856 | LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1857 | 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] | 1858 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1859 | if (exp->used == tok_idx) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1860 | if (ctx) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1861 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF); |
| 1862 | } |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1863 | return LY_EINCOMPLETE; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1864 | } |
| 1865 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1866 | if (want_tok && (exp->tokens[tok_idx] != want_tok)) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1867 | if (ctx) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1868 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1869 | 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] | 1870 | } |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1871 | return LY_ENOT; |
| 1872 | } |
| 1873 | |
| 1874 | return LY_SUCCESS; |
| 1875 | } |
| 1876 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1877 | LY_ERR |
| 1878 | lyxp_next_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok) |
| 1879 | { |
| 1880 | LY_CHECK_RET(lyxp_check_token(ctx, exp, *tok_idx, want_tok)); |
| 1881 | |
| 1882 | /* skip the token */ |
| 1883 | ++(*tok_idx); |
| 1884 | |
| 1885 | return LY_SUCCESS; |
| 1886 | } |
| 1887 | |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1888 | /* just like lyxp_check_token() but tests for 2 tokens */ |
| 1889 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1890 | exp_check_token2(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t tok_idx, enum lyxp_token want_tok1, |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1891 | enum lyxp_token want_tok2) |
| 1892 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1893 | if (exp->used == tok_idx) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1894 | if (ctx) { |
| 1895 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF); |
| 1896 | } |
| 1897 | return LY_EINCOMPLETE; |
| 1898 | } |
| 1899 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1900 | 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] | 1901 | if (ctx) { |
| 1902 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1903 | 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] | 1904 | } |
| 1905 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1906 | } |
| 1907 | |
| 1908 | return LY_SUCCESS; |
| 1909 | } |
| 1910 | |
| 1911 | /** |
| 1912 | * @brief Stack operation push on the repeat array. |
| 1913 | * |
| 1914 | * @param[in] exp Expression to use. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1915 | * @param[in] tok_idx Position in the expresion \p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1916 | * @param[in] repeat_op_idx Index from \p exp of the operator token. This value is pushed. |
| 1917 | */ |
| 1918 | static void |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1919 | 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] | 1920 | { |
| 1921 | uint16_t i; |
| 1922 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1923 | if (exp->repeat[tok_idx]) { |
| 1924 | for (i = 0; exp->repeat[tok_idx][i]; ++i); |
| 1925 | exp->repeat[tok_idx] = realloc(exp->repeat[tok_idx], (i + 2) * sizeof *exp->repeat[tok_idx]); |
| 1926 | LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), ); |
| 1927 | exp->repeat[tok_idx][i] = repeat_op_idx; |
| 1928 | exp->repeat[tok_idx][i + 1] = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1929 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1930 | exp->repeat[tok_idx] = calloc(2, sizeof *exp->repeat[tok_idx]); |
| 1931 | LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), ); |
| 1932 | exp->repeat[tok_idx][0] = repeat_op_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1933 | } |
| 1934 | } |
| 1935 | |
| 1936 | /** |
| 1937 | * @brief Reparse Predicate. Logs directly on error. |
| 1938 | * |
| 1939 | * [7] Predicate ::= '[' Expr ']' |
| 1940 | * |
| 1941 | * @param[in] ctx Context for logging. |
| 1942 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1943 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1944 | * @return LY_ERR |
| 1945 | */ |
| 1946 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1947 | 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] | 1948 | { |
| 1949 | LY_ERR rc; |
| 1950 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1951 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1952 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1953 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1954 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1955 | rc = reparse_or_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1956 | LY_CHECK_RET(rc); |
| 1957 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1958 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1959 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1960 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1961 | |
| 1962 | return LY_SUCCESS; |
| 1963 | } |
| 1964 | |
| 1965 | /** |
| 1966 | * @brief Reparse RelativeLocationPath. Logs directly on error. |
| 1967 | * |
| 1968 | * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step |
| 1969 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 1970 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 1971 | * |
| 1972 | * @param[in] ctx Context for logging. |
| 1973 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1974 | * @param[in] tok_idx Position in the expression \p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1975 | * @return LY_ERR (LY_EINCOMPLETE on forward reference) |
| 1976 | */ |
| 1977 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1978 | 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] | 1979 | { |
| 1980 | LY_ERR rc; |
| 1981 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1982 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1983 | LY_CHECK_RET(rc); |
| 1984 | |
| 1985 | goto step; |
| 1986 | do { |
| 1987 | /* '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1988 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1989 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1990 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1991 | LY_CHECK_RET(rc); |
| 1992 | step: |
| 1993 | /* Step */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1994 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1995 | case LYXP_TOKEN_DOT: |
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 | break; |
| 1998 | |
| 1999 | case LYXP_TOKEN_DDOT: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2000 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2001 | break; |
| 2002 | |
| 2003 | case LYXP_TOKEN_AT: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2004 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2005 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2006 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2007 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2008 | 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] | 2009 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2010 | 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] | 2011 | return LY_EVALID; |
| 2012 | } |
| 2013 | /* fall through */ |
| 2014 | case LYXP_TOKEN_NAMETEST: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2015 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2016 | goto reparse_predicate; |
| 2017 | break; |
| 2018 | |
| 2019 | case LYXP_TOKEN_NODETYPE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2020 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2021 | |
| 2022 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2023 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2024 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2025 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2026 | |
| 2027 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2028 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2029 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2030 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2031 | |
| 2032 | reparse_predicate: |
| 2033 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2034 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 2035 | rc = reparse_predicate(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2036 | LY_CHECK_RET(rc); |
| 2037 | } |
| 2038 | break; |
| 2039 | default: |
| 2040 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2041 | 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] | 2042 | return LY_EVALID; |
| 2043 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2044 | } 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] | 2045 | |
| 2046 | return LY_SUCCESS; |
| 2047 | } |
| 2048 | |
| 2049 | /** |
| 2050 | * @brief Reparse AbsoluteLocationPath. Logs directly on error. |
| 2051 | * |
| 2052 | * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath |
| 2053 | * |
| 2054 | * @param[in] ctx Context for logging. |
| 2055 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2056 | * @param[in] tok_idx Position in the expression \p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2057 | * @return LY_ERR |
| 2058 | */ |
| 2059 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2060 | 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] | 2061 | { |
| 2062 | LY_ERR rc; |
| 2063 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2064 | 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] | 2065 | |
| 2066 | /* '/' RelativeLocationPath? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2067 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2068 | /* '/' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2069 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2070 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2071 | if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2072 | return LY_SUCCESS; |
| 2073 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2074 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2075 | case LYXP_TOKEN_DOT: |
| 2076 | case LYXP_TOKEN_DDOT: |
| 2077 | case LYXP_TOKEN_AT: |
| 2078 | case LYXP_TOKEN_NAMETEST: |
| 2079 | case LYXP_TOKEN_NODETYPE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2080 | rc = reparse_relative_location_path(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2081 | LY_CHECK_RET(rc); |
| 2082 | /* fall through */ |
| 2083 | default: |
| 2084 | break; |
| 2085 | } |
| 2086 | |
| 2087 | /* '//' RelativeLocationPath */ |
| 2088 | } else { |
| 2089 | /* '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2090 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2091 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2092 | rc = reparse_relative_location_path(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2093 | LY_CHECK_RET(rc); |
| 2094 | } |
| 2095 | |
| 2096 | return LY_SUCCESS; |
| 2097 | } |
| 2098 | |
| 2099 | /** |
| 2100 | * @brief Reparse FunctionCall. Logs directly on error. |
| 2101 | * |
| 2102 | * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')' |
| 2103 | * |
| 2104 | * @param[in] ctx Context for logging. |
| 2105 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2106 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2107 | * @return LY_ERR |
| 2108 | */ |
| 2109 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2110 | 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] | 2111 | { |
| 2112 | int min_arg_count = -1, max_arg_count, arg_count; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2113 | uint16_t func_tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2114 | LY_ERR rc; |
| 2115 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2116 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2117 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2118 | func_tok_idx = *tok_idx; |
| 2119 | switch (exp->tok_len[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2120 | case 3: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2121 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2122 | min_arg_count = 1; |
| 2123 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2124 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2125 | min_arg_count = 1; |
| 2126 | max_arg_count = 1; |
| 2127 | } |
| 2128 | break; |
| 2129 | case 4: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2130 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2131 | min_arg_count = 1; |
| 2132 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2133 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2134 | min_arg_count = 0; |
| 2135 | max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2136 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2137 | min_arg_count = 0; |
| 2138 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2139 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2140 | min_arg_count = 0; |
| 2141 | max_arg_count = 0; |
| 2142 | } |
| 2143 | break; |
| 2144 | case 5: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2145 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2146 | min_arg_count = 1; |
| 2147 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2148 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2149 | min_arg_count = 0; |
| 2150 | max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2151 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2152 | min_arg_count = 1; |
| 2153 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2154 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2155 | min_arg_count = 1; |
| 2156 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2157 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2158 | min_arg_count = 1; |
| 2159 | max_arg_count = 1; |
| 2160 | } |
| 2161 | break; |
| 2162 | case 6: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2163 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2164 | min_arg_count = 2; |
Michal Vasko | be2e356 | 2019-10-15 15:35:35 +0200 | [diff] [blame] | 2165 | max_arg_count = INT_MAX; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2166 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2167 | min_arg_count = 0; |
| 2168 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2169 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2170 | min_arg_count = 0; |
| 2171 | max_arg_count = 1; |
| 2172 | } |
| 2173 | break; |
| 2174 | case 7: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2175 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2176 | min_arg_count = 1; |
| 2177 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2178 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2179 | min_arg_count = 1; |
| 2180 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2181 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2182 | min_arg_count = 0; |
| 2183 | max_arg_count = 0; |
| 2184 | } |
| 2185 | break; |
| 2186 | case 8: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2187 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2188 | min_arg_count = 2; |
| 2189 | max_arg_count = 2; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2190 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2191 | min_arg_count = 0; |
| 2192 | max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2193 | } 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] | 2194 | min_arg_count = 2; |
| 2195 | max_arg_count = 2; |
| 2196 | } |
| 2197 | break; |
| 2198 | case 9: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2199 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2200 | min_arg_count = 2; |
| 2201 | max_arg_count = 3; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2202 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2203 | min_arg_count = 3; |
| 2204 | max_arg_count = 3; |
| 2205 | } |
| 2206 | break; |
| 2207 | case 10: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2208 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2209 | min_arg_count = 0; |
| 2210 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2211 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2212 | min_arg_count = 1; |
| 2213 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2214 | } 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] | 2215 | min_arg_count = 2; |
| 2216 | max_arg_count = 2; |
| 2217 | } |
| 2218 | break; |
| 2219 | case 11: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2220 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2221 | min_arg_count = 2; |
| 2222 | max_arg_count = 2; |
| 2223 | } |
| 2224 | break; |
| 2225 | case 12: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2226 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2227 | min_arg_count = 2; |
| 2228 | max_arg_count = 2; |
| 2229 | } |
| 2230 | break; |
| 2231 | case 13: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2232 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2233 | min_arg_count = 0; |
| 2234 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2235 | } 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] | 2236 | min_arg_count = 0; |
| 2237 | max_arg_count = 1; |
| 2238 | } |
| 2239 | break; |
| 2240 | case 15: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2241 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2242 | min_arg_count = 0; |
| 2243 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2244 | } 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] | 2245 | min_arg_count = 2; |
| 2246 | max_arg_count = 2; |
| 2247 | } |
| 2248 | break; |
| 2249 | case 16: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2250 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2251 | min_arg_count = 2; |
| 2252 | max_arg_count = 2; |
| 2253 | } |
| 2254 | break; |
| 2255 | case 20: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2256 | 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] | 2257 | min_arg_count = 2; |
| 2258 | max_arg_count = 2; |
| 2259 | } |
| 2260 | break; |
| 2261 | } |
| 2262 | if (min_arg_count == -1) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2263 | 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] | 2264 | return LY_EINVAL; |
| 2265 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2266 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2267 | |
| 2268 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2269 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2270 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2271 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2272 | |
| 2273 | /* ( Expr ( ',' Expr )* )? */ |
| 2274 | arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2275 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2276 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2277 | if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2278 | ++arg_count; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2279 | rc = reparse_or_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2280 | LY_CHECK_RET(rc); |
| 2281 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2282 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) { |
| 2283 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2284 | |
| 2285 | ++arg_count; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2286 | rc = reparse_or_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2287 | LY_CHECK_RET(rc); |
| 2288 | } |
| 2289 | |
| 2290 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2291 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2292 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2293 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2294 | |
| 2295 | if ((arg_count < min_arg_count) || (arg_count > max_arg_count)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2296 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INARGCOUNT, arg_count, exp->tok_len[func_tok_idx], |
| 2297 | &exp->expr[exp->tok_pos[func_tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2298 | return LY_EVALID; |
| 2299 | } |
| 2300 | |
| 2301 | return LY_SUCCESS; |
| 2302 | } |
| 2303 | |
| 2304 | /** |
| 2305 | * @brief Reparse PathExpr. Logs directly on error. |
| 2306 | * |
| 2307 | * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate* |
| 2308 | * | PrimaryExpr Predicate* '/' RelativeLocationPath |
| 2309 | * | PrimaryExpr Predicate* '//' RelativeLocationPath |
| 2310 | * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath |
| 2311 | * [8] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall |
| 2312 | * |
| 2313 | * @param[in] ctx Context for logging. |
| 2314 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2315 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2316 | * @return LY_ERR |
| 2317 | */ |
| 2318 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2319 | 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] | 2320 | { |
| 2321 | LY_ERR rc; |
| 2322 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2323 | if (lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 2324 | return LY_EVALID; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2325 | } |
| 2326 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2327 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2328 | case LYXP_TOKEN_PAR1: |
| 2329 | /* '(' Expr ')' Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2330 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2331 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2332 | rc = reparse_or_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2333 | LY_CHECK_RET(rc); |
| 2334 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2335 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2336 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2337 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2338 | goto predicate; |
| 2339 | break; |
| 2340 | case LYXP_TOKEN_DOT: |
| 2341 | case LYXP_TOKEN_DDOT: |
| 2342 | case LYXP_TOKEN_AT: |
| 2343 | case LYXP_TOKEN_NAMETEST: |
| 2344 | case LYXP_TOKEN_NODETYPE: |
| 2345 | /* RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2346 | rc = reparse_relative_location_path(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2347 | LY_CHECK_RET(rc); |
| 2348 | break; |
| 2349 | case LYXP_TOKEN_FUNCNAME: |
| 2350 | /* FunctionCall */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2351 | rc = reparse_function_call(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2352 | LY_CHECK_RET(rc); |
| 2353 | goto predicate; |
| 2354 | break; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2355 | case LYXP_TOKEN_OPER_PATH: |
| 2356 | case LYXP_TOKEN_OPER_RPATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2357 | /* AbsoluteLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2358 | rc = reparse_absolute_location_path(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2359 | LY_CHECK_RET(rc); |
| 2360 | break; |
| 2361 | case LYXP_TOKEN_LITERAL: |
| 2362 | /* Literal */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2363 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2364 | goto predicate; |
| 2365 | break; |
| 2366 | case LYXP_TOKEN_NUMBER: |
| 2367 | /* Number */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2368 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2369 | goto predicate; |
| 2370 | break; |
| 2371 | default: |
| 2372 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2373 | 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] | 2374 | return LY_EVALID; |
| 2375 | } |
| 2376 | |
| 2377 | return LY_SUCCESS; |
| 2378 | |
| 2379 | predicate: |
| 2380 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2381 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 2382 | rc = reparse_predicate(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2383 | LY_CHECK_RET(rc); |
| 2384 | } |
| 2385 | |
| 2386 | /* ('/' or '//') RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2387 | 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] | 2388 | |
| 2389 | /* '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2390 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2391 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2392 | rc = reparse_relative_location_path(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2393 | LY_CHECK_RET(rc); |
| 2394 | } |
| 2395 | |
| 2396 | return LY_SUCCESS; |
| 2397 | } |
| 2398 | |
| 2399 | /** |
| 2400 | * @brief Reparse UnaryExpr. Logs directly on error. |
| 2401 | * |
| 2402 | * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr |
| 2403 | * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr |
| 2404 | * |
| 2405 | * @param[in] ctx Context for logging. |
| 2406 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2407 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2408 | * @return LY_ERR |
| 2409 | */ |
| 2410 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2411 | 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] | 2412 | { |
| 2413 | uint16_t prev_exp; |
| 2414 | LY_ERR rc; |
| 2415 | |
| 2416 | /* ('-')* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2417 | prev_exp = *tok_idx; |
| 2418 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) |
| 2419 | && (exp->expr[exp->tok_pos[*tok_idx]] == '-')) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2420 | exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2421 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2422 | } |
| 2423 | |
| 2424 | /* PathExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2425 | prev_exp = *tok_idx; |
| 2426 | rc = reparse_path_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2427 | LY_CHECK_RET(rc); |
| 2428 | |
| 2429 | /* ('|' PathExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2430 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_UNI)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2431 | exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2432 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2433 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2434 | rc = reparse_path_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2435 | LY_CHECK_RET(rc); |
| 2436 | } |
| 2437 | |
| 2438 | return LY_SUCCESS; |
| 2439 | } |
| 2440 | |
| 2441 | /** |
| 2442 | * @brief Reparse AdditiveExpr. Logs directly on error. |
| 2443 | * |
| 2444 | * [15] AdditiveExpr ::= MultiplicativeExpr |
| 2445 | * | AdditiveExpr '+' MultiplicativeExpr |
| 2446 | * | AdditiveExpr '-' MultiplicativeExpr |
| 2447 | * [16] MultiplicativeExpr ::= UnaryExpr |
| 2448 | * | MultiplicativeExpr '*' UnaryExpr |
| 2449 | * | MultiplicativeExpr 'div' UnaryExpr |
| 2450 | * | MultiplicativeExpr 'mod' UnaryExpr |
| 2451 | * |
| 2452 | * @param[in] ctx Context for logging. |
| 2453 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2454 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2455 | * @return LY_ERR |
| 2456 | */ |
| 2457 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2458 | 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] | 2459 | { |
| 2460 | uint16_t prev_add_exp, prev_mul_exp; |
| 2461 | LY_ERR rc; |
| 2462 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2463 | prev_add_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2464 | goto reparse_multiplicative_expr; |
| 2465 | |
| 2466 | /* ('+' / '-' MultiplicativeExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2467 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) |
| 2468 | && ((exp->expr[exp->tok_pos[*tok_idx]] == '+') || (exp->expr[exp->tok_pos[*tok_idx]] == '-'))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2469 | exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2470 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2471 | |
| 2472 | reparse_multiplicative_expr: |
| 2473 | /* UnaryExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2474 | prev_mul_exp = *tok_idx; |
| 2475 | rc = reparse_unary_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2476 | LY_CHECK_RET(rc); |
| 2477 | |
| 2478 | /* ('*' / 'div' / 'mod' UnaryExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2479 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) |
| 2480 | && ((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] | 2481 | exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2482 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2483 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2484 | rc = reparse_unary_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2485 | LY_CHECK_RET(rc); |
| 2486 | } |
| 2487 | } |
| 2488 | |
| 2489 | return LY_SUCCESS; |
| 2490 | } |
| 2491 | |
| 2492 | /** |
| 2493 | * @brief Reparse EqualityExpr. Logs directly on error. |
| 2494 | * |
| 2495 | * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr |
| 2496 | * | EqualityExpr '!=' RelationalExpr |
| 2497 | * [14] RelationalExpr ::= AdditiveExpr |
| 2498 | * | RelationalExpr '<' AdditiveExpr |
| 2499 | * | RelationalExpr '>' AdditiveExpr |
| 2500 | * | RelationalExpr '<=' AdditiveExpr |
| 2501 | * | RelationalExpr '>=' AdditiveExpr |
| 2502 | * |
| 2503 | * @param[in] ctx Context for logging. |
| 2504 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2505 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2506 | * @return LY_ERR |
| 2507 | */ |
| 2508 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2509 | 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] | 2510 | { |
| 2511 | uint16_t prev_eq_exp, prev_rel_exp; |
| 2512 | LY_ERR rc; |
| 2513 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2514 | prev_eq_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2515 | goto reparse_additive_expr; |
| 2516 | |
| 2517 | /* ('=' / '!=' RelationalExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2518 | 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] | 2519 | exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2520 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2521 | |
| 2522 | reparse_additive_expr: |
| 2523 | /* AdditiveExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2524 | prev_rel_exp = *tok_idx; |
| 2525 | rc = reparse_additive_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2526 | LY_CHECK_RET(rc); |
| 2527 | |
| 2528 | /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2529 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_COMP)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2530 | exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2531 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2532 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2533 | rc = reparse_additive_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2534 | LY_CHECK_RET(rc); |
| 2535 | } |
| 2536 | } |
| 2537 | |
| 2538 | return LY_SUCCESS; |
| 2539 | } |
| 2540 | |
| 2541 | /** |
| 2542 | * @brief Reparse OrExpr. Logs directly on error. |
| 2543 | * |
| 2544 | * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr |
| 2545 | * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr |
| 2546 | * |
| 2547 | * @param[in] ctx Context for logging. |
| 2548 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2549 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2550 | * @return LY_ERR |
| 2551 | */ |
| 2552 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2553 | 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] | 2554 | { |
| 2555 | uint16_t prev_or_exp, prev_and_exp; |
| 2556 | LY_ERR rc; |
| 2557 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2558 | prev_or_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2559 | goto reparse_equality_expr; |
| 2560 | |
| 2561 | /* ('or' AndExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2562 | 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] | 2563 | exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2564 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2565 | |
| 2566 | reparse_equality_expr: |
| 2567 | /* EqualityExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2568 | prev_and_exp = *tok_idx; |
| 2569 | rc = reparse_equality_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2570 | LY_CHECK_RET(rc); |
| 2571 | |
| 2572 | /* ('and' EqualityExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2573 | 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] | 2574 | exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2575 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2576 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2577 | rc = reparse_equality_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2578 | LY_CHECK_RET(rc); |
| 2579 | } |
| 2580 | } |
| 2581 | |
| 2582 | return LY_SUCCESS; |
| 2583 | } |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2584 | |
| 2585 | /** |
| 2586 | * @brief Parse NCName. |
| 2587 | * |
| 2588 | * @param[in] ncname Name to parse. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2589 | * @return Length of @p ncname valid bytes. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2590 | */ |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2591 | static long int |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2592 | parse_ncname(const char *ncname) |
| 2593 | { |
| 2594 | unsigned int uc; |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2595 | size_t size; |
| 2596 | long int len = 0; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2597 | |
| 2598 | LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0); |
| 2599 | if (!is_xmlqnamestartchar(uc) || (uc == ':')) { |
| 2600 | return len; |
| 2601 | } |
| 2602 | |
| 2603 | do { |
| 2604 | len += size; |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 2605 | if (!*ncname) { |
| 2606 | break; |
| 2607 | } |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2608 | LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2609 | } while (is_xmlqnamechar(uc) && (uc != ':')); |
| 2610 | |
| 2611 | return len; |
| 2612 | } |
| 2613 | |
| 2614 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2615 | * @brief Add @p token into the expression @p exp. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2616 | * |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2617 | * @param[in] ctx Context for logging. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2618 | * @param[in] exp Expression to use. |
| 2619 | * @param[in] token Token to add. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2620 | * @param[in] tok_pos Token position in the XPath expression. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2621 | * @param[in] tok_len Token length in the XPath expression. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2622 | * @return LY_ERR |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2623 | */ |
| 2624 | static LY_ERR |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 2625 | 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] | 2626 | { |
| 2627 | uint32_t prev; |
| 2628 | |
| 2629 | if (exp->used == exp->size) { |
| 2630 | prev = exp->size; |
| 2631 | exp->size += LYXP_EXPR_SIZE_STEP; |
| 2632 | if (prev > exp->size) { |
| 2633 | LOGINT(ctx); |
| 2634 | return LY_EINT; |
| 2635 | } |
| 2636 | |
| 2637 | exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens); |
| 2638 | LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM); |
| 2639 | exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos); |
| 2640 | LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM); |
| 2641 | exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len); |
| 2642 | LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM); |
| 2643 | } |
| 2644 | |
| 2645 | exp->tokens[exp->used] = token; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2646 | exp->tok_pos[exp->used] = tok_pos; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2647 | exp->tok_len[exp->used] = tok_len; |
| 2648 | ++exp->used; |
| 2649 | return LY_SUCCESS; |
| 2650 | } |
| 2651 | |
| 2652 | void |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 2653 | lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr) |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2654 | { |
| 2655 | uint16_t i; |
| 2656 | |
| 2657 | if (!expr) { |
| 2658 | return; |
| 2659 | } |
| 2660 | |
| 2661 | lydict_remove(ctx, expr->expr); |
| 2662 | free(expr->tokens); |
| 2663 | free(expr->tok_pos); |
| 2664 | free(expr->tok_len); |
| 2665 | if (expr->repeat) { |
| 2666 | for (i = 0; i < expr->used; ++i) { |
| 2667 | free(expr->repeat[i]); |
| 2668 | } |
| 2669 | } |
| 2670 | free(expr->repeat); |
| 2671 | free(expr); |
| 2672 | } |
| 2673 | |
| 2674 | struct lyxp_expr * |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2675 | lyxp_expr_parse(const struct ly_ctx *ctx, const char *expr, size_t expr_len, int reparse) |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2676 | { |
| 2677 | struct lyxp_expr *ret; |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2678 | size_t parsed = 0, tok_len; |
| 2679 | long int ncname_len; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2680 | enum lyxp_token tok_type; |
| 2681 | int prev_function_check = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2682 | uint16_t tok_idx = 0; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2683 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2684 | if (!expr[0]) { |
| 2685 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF); |
| 2686 | return NULL; |
| 2687 | } |
| 2688 | |
| 2689 | if (!expr_len) { |
| 2690 | expr_len = strlen(expr); |
| 2691 | } |
| 2692 | if (expr_len > UINT16_MAX) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2693 | LOGERR(ctx, LY_EINVAL, "XPath expression cannot be longer than %ud characters.", UINT16_MAX); |
| 2694 | return NULL; |
| 2695 | } |
| 2696 | |
| 2697 | /* init lyxp_expr structure */ |
| 2698 | ret = calloc(1, sizeof *ret); |
| 2699 | LY_CHECK_ERR_GOTO(!ret, LOGMEM(ctx), error); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2700 | ret->expr = lydict_insert(ctx, expr, expr_len); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2701 | LY_CHECK_ERR_GOTO(!ret->expr, LOGMEM(ctx), error); |
| 2702 | ret->used = 0; |
| 2703 | ret->size = LYXP_EXPR_SIZE_START; |
| 2704 | ret->tokens = malloc(ret->size * sizeof *ret->tokens); |
| 2705 | LY_CHECK_ERR_GOTO(!ret->tokens, LOGMEM(ctx), error); |
| 2706 | |
| 2707 | ret->tok_pos = malloc(ret->size * sizeof *ret->tok_pos); |
| 2708 | LY_CHECK_ERR_GOTO(!ret->tok_pos, LOGMEM(ctx), error); |
| 2709 | |
| 2710 | ret->tok_len = malloc(ret->size * sizeof *ret->tok_len); |
| 2711 | LY_CHECK_ERR_GOTO(!ret->tok_len, LOGMEM(ctx), error); |
| 2712 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2713 | /* make expr 0-terminated */ |
| 2714 | expr = ret->expr; |
| 2715 | |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2716 | while (is_xmlws(expr[parsed])) { |
| 2717 | ++parsed; |
| 2718 | } |
| 2719 | |
| 2720 | do { |
| 2721 | if (expr[parsed] == '(') { |
| 2722 | |
| 2723 | /* '(' */ |
| 2724 | tok_len = 1; |
| 2725 | tok_type = LYXP_TOKEN_PAR1; |
| 2726 | |
| 2727 | if (prev_function_check && ret->used && (ret->tokens[ret->used - 1] == LYXP_TOKEN_NAMETEST)) { |
| 2728 | /* it is a NodeType/FunctionName after all */ |
| 2729 | if (((ret->tok_len[ret->used - 1] == 4) |
| 2730 | && (!strncmp(&expr[ret->tok_pos[ret->used - 1]], "node", 4) |
| 2731 | || !strncmp(&expr[ret->tok_pos[ret->used - 1]], "text", 4))) || |
| 2732 | ((ret->tok_len[ret->used - 1] == 7) |
| 2733 | && !strncmp(&expr[ret->tok_pos[ret->used - 1]], "comment", 7))) { |
| 2734 | ret->tokens[ret->used - 1] = LYXP_TOKEN_NODETYPE; |
| 2735 | } else { |
| 2736 | ret->tokens[ret->used - 1] = LYXP_TOKEN_FUNCNAME; |
| 2737 | } |
| 2738 | prev_function_check = 0; |
| 2739 | } |
| 2740 | |
| 2741 | } else if (expr[parsed] == ')') { |
| 2742 | |
| 2743 | /* ')' */ |
| 2744 | tok_len = 1; |
| 2745 | tok_type = LYXP_TOKEN_PAR2; |
| 2746 | |
| 2747 | } else if (expr[parsed] == '[') { |
| 2748 | |
| 2749 | /* '[' */ |
| 2750 | tok_len = 1; |
| 2751 | tok_type = LYXP_TOKEN_BRACK1; |
| 2752 | |
| 2753 | } else if (expr[parsed] == ']') { |
| 2754 | |
| 2755 | /* ']' */ |
| 2756 | tok_len = 1; |
| 2757 | tok_type = LYXP_TOKEN_BRACK2; |
| 2758 | |
| 2759 | } else if (!strncmp(&expr[parsed], "..", 2)) { |
| 2760 | |
| 2761 | /* '..' */ |
| 2762 | tok_len = 2; |
| 2763 | tok_type = LYXP_TOKEN_DDOT; |
| 2764 | |
| 2765 | } else if ((expr[parsed] == '.') && (!isdigit(expr[parsed + 1]))) { |
| 2766 | |
| 2767 | /* '.' */ |
| 2768 | tok_len = 1; |
| 2769 | tok_type = LYXP_TOKEN_DOT; |
| 2770 | |
| 2771 | } else if (expr[parsed] == '@') { |
| 2772 | |
| 2773 | /* '@' */ |
| 2774 | tok_len = 1; |
| 2775 | tok_type = LYXP_TOKEN_AT; |
| 2776 | |
| 2777 | } else if (expr[parsed] == ',') { |
| 2778 | |
| 2779 | /* ',' */ |
| 2780 | tok_len = 1; |
| 2781 | tok_type = LYXP_TOKEN_COMMA; |
| 2782 | |
| 2783 | } else if (expr[parsed] == '\'') { |
| 2784 | |
| 2785 | /* Literal with ' */ |
| 2786 | for (tok_len = 1; (expr[parsed + tok_len] != '\0') && (expr[parsed + tok_len] != '\''); ++tok_len); |
| 2787 | LY_CHECK_ERR_GOTO(expr[parsed + tok_len] == '\0', |
| 2788 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOE, expr[parsed], &expr[parsed]), error); |
| 2789 | ++tok_len; |
| 2790 | tok_type = LYXP_TOKEN_LITERAL; |
| 2791 | |
| 2792 | } else if (expr[parsed] == '\"') { |
| 2793 | |
| 2794 | /* Literal with " */ |
| 2795 | for (tok_len = 1; (expr[parsed + tok_len] != '\0') && (expr[parsed + tok_len] != '\"'); ++tok_len); |
| 2796 | LY_CHECK_ERR_GOTO(expr[parsed + tok_len] == '\0', |
| 2797 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOE, expr[parsed], &expr[parsed]), error); |
| 2798 | ++tok_len; |
| 2799 | tok_type = LYXP_TOKEN_LITERAL; |
| 2800 | |
| 2801 | } else if ((expr[parsed] == '.') || (isdigit(expr[parsed]))) { |
| 2802 | |
| 2803 | /* Number */ |
| 2804 | for (tok_len = 0; isdigit(expr[parsed + tok_len]); ++tok_len); |
| 2805 | if (expr[parsed + tok_len] == '.') { |
| 2806 | ++tok_len; |
| 2807 | for (; isdigit(expr[parsed + tok_len]); ++tok_len); |
| 2808 | } |
| 2809 | tok_type = LYXP_TOKEN_NUMBER; |
| 2810 | |
| 2811 | } else if (expr[parsed] == '/') { |
| 2812 | |
| 2813 | /* Operator '/', '//' */ |
| 2814 | if (!strncmp(&expr[parsed], "//", 2)) { |
| 2815 | tok_len = 2; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2816 | tok_type = LYXP_TOKEN_OPER_RPATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2817 | } else { |
| 2818 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2819 | tok_type = LYXP_TOKEN_OPER_PATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2820 | } |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2821 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2822 | } else if (!strncmp(&expr[parsed], "!=", 2)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2823 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2824 | /* Operator '!=' */ |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2825 | tok_len = 2; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2826 | tok_type = LYXP_TOKEN_OPER_NEQUAL; |
| 2827 | |
| 2828 | } else if (!strncmp(&expr[parsed], "<=", 2) || !strncmp(&expr[parsed], ">=", 2)) { |
| 2829 | |
| 2830 | /* Operator '<=', '>=' */ |
| 2831 | tok_len = 2; |
| 2832 | tok_type = LYXP_TOKEN_OPER_COMP; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2833 | |
| 2834 | } else if (expr[parsed] == '|') { |
| 2835 | |
| 2836 | /* Operator '|' */ |
| 2837 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2838 | tok_type = LYXP_TOKEN_OPER_UNI; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2839 | |
| 2840 | } else if ((expr[parsed] == '+') || (expr[parsed] == '-')) { |
| 2841 | |
| 2842 | /* Operator '+', '-' */ |
| 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 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2846 | } else if (expr[parsed] == '=') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2847 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2848 | /* Operator '=' */ |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2849 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2850 | tok_type = LYXP_TOKEN_OPER_EQUAL; |
| 2851 | |
| 2852 | } else if ((expr[parsed] == '<') || (expr[parsed] == '>')) { |
| 2853 | |
| 2854 | /* Operator '<', '>' */ |
| 2855 | tok_len = 1; |
| 2856 | tok_type = LYXP_TOKEN_OPER_COMP; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2857 | |
| 2858 | } else if (ret->used && (ret->tokens[ret->used - 1] != LYXP_TOKEN_AT) |
| 2859 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_PAR1) |
| 2860 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_BRACK1) |
| 2861 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_COMMA) |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2862 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_LOG) |
| 2863 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_EQUAL) |
| 2864 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_NEQUAL) |
| 2865 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_COMP) |
| 2866 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_MATH) |
| 2867 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_UNI) |
| 2868 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_PATH) |
| 2869 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_RPATH)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2870 | |
| 2871 | /* Operator '*', 'or', 'and', 'mod', or 'div' */ |
| 2872 | if (expr[parsed] == '*') { |
| 2873 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2874 | tok_type = LYXP_TOKEN_OPER_MATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2875 | |
| 2876 | } else if (!strncmp(&expr[parsed], "or", 2)) { |
| 2877 | tok_len = 2; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2878 | tok_type = LYXP_TOKEN_OPER_LOG; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2879 | |
| 2880 | } else if (!strncmp(&expr[parsed], "and", 3)) { |
| 2881 | tok_len = 3; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2882 | tok_type = LYXP_TOKEN_OPER_LOG; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2883 | |
| 2884 | } else if (!strncmp(&expr[parsed], "mod", 3) || !strncmp(&expr[parsed], "div", 3)) { |
| 2885 | tok_len = 3; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2886 | tok_type = LYXP_TOKEN_OPER_MATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2887 | |
| 2888 | } else if (prev_function_check) { |
Michal Vasko | 5307857 | 2019-05-24 08:50:15 +0200 | [diff] [blame] | 2889 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Invalid character 0x%x ('%c'), perhaps \"%.*s\" is supposed to be a function call.", |
| 2890 | 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] | 2891 | goto error; |
| 2892 | } else { |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2893 | 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] | 2894 | goto error; |
| 2895 | } |
| 2896 | } else if (expr[parsed] == '*') { |
| 2897 | |
| 2898 | /* NameTest '*' */ |
| 2899 | tok_len = 1; |
| 2900 | tok_type = LYXP_TOKEN_NAMETEST; |
| 2901 | |
| 2902 | } else { |
| 2903 | |
| 2904 | /* NameTest (NCName ':' '*' | QName) or NodeType/FunctionName */ |
| 2905 | ncname_len = parse_ncname(&expr[parsed]); |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2906 | 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] | 2907 | tok_len = ncname_len; |
| 2908 | |
| 2909 | if (expr[parsed + tok_len] == ':') { |
| 2910 | ++tok_len; |
| 2911 | if (expr[parsed + tok_len] == '*') { |
| 2912 | ++tok_len; |
| 2913 | } else { |
| 2914 | ncname_len = parse_ncname(&expr[parsed + tok_len]); |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2915 | 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] | 2916 | tok_len += ncname_len; |
| 2917 | } |
| 2918 | /* remove old flag to prevent ambiguities */ |
| 2919 | prev_function_check = 0; |
| 2920 | tok_type = LYXP_TOKEN_NAMETEST; |
| 2921 | } else { |
| 2922 | /* there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */ |
| 2923 | prev_function_check = 1; |
| 2924 | tok_type = LYXP_TOKEN_NAMETEST; |
| 2925 | } |
| 2926 | } |
| 2927 | |
| 2928 | /* store the token, move on to the next one */ |
| 2929 | LY_CHECK_GOTO(exp_add_token(ctx, ret, tok_type, parsed, tok_len), error); |
| 2930 | parsed += tok_len; |
| 2931 | while (is_xmlws(expr[parsed])) { |
| 2932 | ++parsed; |
| 2933 | } |
| 2934 | |
| 2935 | } while (expr[parsed]); |
| 2936 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2937 | if (reparse) { |
| 2938 | /* prealloc repeat */ |
| 2939 | ret->repeat = calloc(ret->size, sizeof *ret->repeat); |
| 2940 | LY_CHECK_ERR_GOTO(!ret->repeat, LOGMEM(ctx), error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2941 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2942 | /* fill repeat */ |
| 2943 | LY_CHECK_GOTO(reparse_or_expr(ctx, ret, &tok_idx), error); |
| 2944 | if (ret->used > tok_idx) { |
| 2945 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Unparsed characters \"%s\" left at the end of an XPath expression.", |
| 2946 | &ret->expr[ret->tok_pos[tok_idx]]); |
| 2947 | goto error; |
| 2948 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2949 | } |
| 2950 | |
| 2951 | print_expr_struct_debug(ret); |
| 2952 | |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2953 | return ret; |
| 2954 | |
| 2955 | error: |
| 2956 | lyxp_expr_free(ctx, ret); |
| 2957 | return NULL; |
| 2958 | } |
| 2959 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2960 | struct lyxp_expr * |
| 2961 | lyxp_expr_dup(const struct ly_ctx *ctx, const struct lyxp_expr *exp) |
| 2962 | { |
| 2963 | struct lyxp_expr *dup; |
| 2964 | uint32_t i, j; |
| 2965 | |
| 2966 | dup = calloc(1, sizeof *dup); |
| 2967 | LY_CHECK_ERR_GOTO(!dup, LOGMEM(ctx), error); |
| 2968 | |
| 2969 | dup->tokens = malloc(exp->used * sizeof *dup->tokens); |
| 2970 | LY_CHECK_ERR_GOTO(!dup->tokens, LOGMEM(ctx), error); |
| 2971 | memcpy(dup->tokens, exp->tokens, exp->used * sizeof *dup->tokens); |
| 2972 | |
| 2973 | dup->tok_pos = malloc(exp->used * sizeof *dup->tok_pos); |
| 2974 | LY_CHECK_ERR_GOTO(!dup->tok_pos, LOGMEM(ctx), error); |
| 2975 | memcpy(dup->tok_pos, exp->tok_pos, exp->used * sizeof *dup->tok_pos); |
| 2976 | |
| 2977 | dup->tok_len = malloc(exp->used * sizeof *dup->tok_len); |
| 2978 | LY_CHECK_ERR_GOTO(!dup->tok_len, LOGMEM(ctx), error); |
| 2979 | memcpy(dup->tok_len, exp->tok_len, exp->used * sizeof *dup->tok_len); |
| 2980 | |
| 2981 | dup->repeat = malloc(exp->used * sizeof *dup->repeat); |
| 2982 | LY_CHECK_ERR_GOTO(!dup->repeat, LOGMEM(ctx), error); |
| 2983 | for (i = 0; i < exp->used; ++i) { |
| 2984 | if (!exp->repeat[i]) { |
| 2985 | dup->repeat[i] = NULL; |
| 2986 | } else { |
| 2987 | for (j = 0; exp->repeat[i][j]; ++j); |
| 2988 | /* the ending 0 as well */ |
| 2989 | ++j; |
| 2990 | |
Michal Vasko | 99c7164 | 2020-07-03 13:33:36 +0200 | [diff] [blame] | 2991 | dup->repeat[i] = malloc(j * sizeof **dup->repeat); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2992 | LY_CHECK_ERR_GOTO(!dup->repeat[i], LOGMEM(ctx), error); |
| 2993 | memcpy(dup->repeat[i], exp->repeat[i], j * sizeof **dup->repeat); |
| 2994 | dup->repeat[i][j - 1] = 0; |
| 2995 | } |
| 2996 | } |
| 2997 | |
| 2998 | dup->used = exp->used; |
| 2999 | dup->size = exp->used; |
| 3000 | dup->expr = lydict_insert(ctx, exp->expr, 0); |
| 3001 | |
| 3002 | return dup; |
| 3003 | |
| 3004 | error: |
| 3005 | lyxp_expr_free(ctx, dup); |
| 3006 | return NULL; |
| 3007 | } |
| 3008 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3009 | /* |
| 3010 | * warn functions |
| 3011 | * |
| 3012 | * Warn functions check specific reasonable conditions for schema XPath |
| 3013 | * and print a warning if they are not satisfied. |
| 3014 | */ |
| 3015 | |
| 3016 | /** |
| 3017 | * @brief Get the last-added schema node that is currently in the context. |
| 3018 | * |
| 3019 | * @param[in] set Set to search in. |
| 3020 | * @return Last-added schema context node, NULL if no node is in context. |
| 3021 | */ |
| 3022 | static struct lysc_node * |
| 3023 | warn_get_scnode_in_ctx(struct lyxp_set *set) |
| 3024 | { |
| 3025 | uint32_t i; |
| 3026 | |
| 3027 | if (!set || (set->type != LYXP_SET_SCNODE_SET)) { |
| 3028 | return NULL; |
| 3029 | } |
| 3030 | |
| 3031 | i = set->used; |
| 3032 | do { |
| 3033 | --i; |
| 3034 | if (set->val.scnodes[i].in_ctx == 1) { |
| 3035 | /* if there are more, simply return the first found (last added) */ |
| 3036 | return set->val.scnodes[i].scnode; |
| 3037 | } |
| 3038 | } while (i); |
| 3039 | |
| 3040 | return NULL; |
| 3041 | } |
| 3042 | |
| 3043 | /** |
| 3044 | * @brief Test whether a type is numeric - integer type or decimal64. |
| 3045 | * |
| 3046 | * @param[in] type Type to test. |
| 3047 | * @return 1 if numeric, 0 otherwise. |
| 3048 | */ |
| 3049 | static int |
| 3050 | warn_is_numeric_type(struct lysc_type *type) |
| 3051 | { |
| 3052 | struct lysc_type_union *uni; |
| 3053 | int ret; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3054 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3055 | |
| 3056 | switch (type->basetype) { |
| 3057 | case LY_TYPE_DEC64: |
| 3058 | case LY_TYPE_INT8: |
| 3059 | case LY_TYPE_UINT8: |
| 3060 | case LY_TYPE_INT16: |
| 3061 | case LY_TYPE_UINT16: |
| 3062 | case LY_TYPE_INT32: |
| 3063 | case LY_TYPE_UINT32: |
| 3064 | case LY_TYPE_INT64: |
| 3065 | case LY_TYPE_UINT64: |
| 3066 | return 1; |
| 3067 | case LY_TYPE_UNION: |
| 3068 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3069 | LY_ARRAY_FOR(uni->types, u) { |
| 3070 | ret = warn_is_numeric_type(uni->types[u]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3071 | if (ret) { |
| 3072 | /* found a suitable type */ |
| 3073 | return 1; |
| 3074 | } |
| 3075 | } |
| 3076 | /* did not find any suitable type */ |
| 3077 | return 0; |
| 3078 | case LY_TYPE_LEAFREF: |
| 3079 | return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype); |
| 3080 | default: |
| 3081 | return 0; |
| 3082 | } |
| 3083 | } |
| 3084 | |
| 3085 | /** |
| 3086 | * @brief Test whether a type is string-like - no integers, decimal64 or binary. |
| 3087 | * |
| 3088 | * @param[in] type Type to test. |
| 3089 | * @return 1 if string, 0 otherwise. |
| 3090 | */ |
| 3091 | static int |
| 3092 | warn_is_string_type(struct lysc_type *type) |
| 3093 | { |
| 3094 | struct lysc_type_union *uni; |
| 3095 | int ret; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3096 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3097 | |
| 3098 | switch (type->basetype) { |
| 3099 | case LY_TYPE_BITS: |
| 3100 | case LY_TYPE_ENUM: |
| 3101 | case LY_TYPE_IDENT: |
| 3102 | case LY_TYPE_INST: |
| 3103 | case LY_TYPE_STRING: |
| 3104 | return 1; |
| 3105 | case LY_TYPE_UNION: |
| 3106 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3107 | LY_ARRAY_FOR(uni->types, u) { |
| 3108 | ret = warn_is_string_type(uni->types[u]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3109 | if (ret) { |
| 3110 | /* found a suitable type */ |
| 3111 | return 1; |
| 3112 | } |
| 3113 | } |
| 3114 | /* did not find any suitable type */ |
| 3115 | return 0; |
| 3116 | case LY_TYPE_LEAFREF: |
| 3117 | return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype); |
| 3118 | default: |
| 3119 | return 0; |
| 3120 | } |
| 3121 | } |
| 3122 | |
| 3123 | /** |
| 3124 | * @brief Test whether a type is one specific type. |
| 3125 | * |
| 3126 | * @param[in] type Type to test. |
| 3127 | * @param[in] base Expected type. |
| 3128 | * @return 1 if it is, 0 otherwise. |
| 3129 | */ |
| 3130 | static int |
| 3131 | warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base) |
| 3132 | { |
| 3133 | struct lysc_type_union *uni; |
| 3134 | int ret; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3135 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3136 | |
| 3137 | if (type->basetype == base) { |
| 3138 | return 1; |
| 3139 | } else if (type->basetype == LY_TYPE_UNION) { |
| 3140 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3141 | LY_ARRAY_FOR(uni->types, u) { |
| 3142 | ret = warn_is_specific_type(uni->types[u], base); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3143 | if (ret) { |
| 3144 | /* found a suitable type */ |
| 3145 | return 1; |
| 3146 | } |
| 3147 | } |
| 3148 | /* did not find any suitable type */ |
| 3149 | return 0; |
| 3150 | } else if (type->basetype == LY_TYPE_LEAFREF) { |
| 3151 | return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base); |
| 3152 | } |
| 3153 | |
| 3154 | return 0; |
| 3155 | } |
| 3156 | |
| 3157 | /** |
| 3158 | * @brief Get next type of a (union) type. |
| 3159 | * |
| 3160 | * @param[in] type Base type. |
| 3161 | * @param[in] prev_type Previously returned type. |
| 3162 | * @return Next type or NULL. |
| 3163 | */ |
| 3164 | static struct lysc_type * |
| 3165 | warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type) |
| 3166 | { |
| 3167 | struct lysc_type_union *uni; |
| 3168 | int found = 0; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3169 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3170 | |
| 3171 | switch (type->basetype) { |
| 3172 | case LY_TYPE_UNION: |
| 3173 | uni = (struct lysc_type_union *)type; |
| 3174 | if (!prev_type) { |
| 3175 | return uni->types[0]; |
| 3176 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3177 | LY_ARRAY_FOR(uni->types, u) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3178 | if (found) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3179 | return uni->types[u]; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3180 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3181 | if (prev_type == uni->types[u]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3182 | found = 1; |
| 3183 | } |
| 3184 | } |
| 3185 | return NULL; |
| 3186 | default: |
| 3187 | if (prev_type) { |
| 3188 | assert(type == prev_type); |
| 3189 | return NULL; |
| 3190 | } else { |
| 3191 | return type; |
| 3192 | } |
| 3193 | } |
| 3194 | } |
| 3195 | |
| 3196 | /** |
| 3197 | * @brief Test whether 2 types have a common type. |
| 3198 | * |
| 3199 | * @param[in] type1 First type. |
| 3200 | * @param[in] type2 Second type. |
| 3201 | * @return 1 if they do, 0 otherwise. |
| 3202 | */ |
| 3203 | static int |
| 3204 | warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2) |
| 3205 | { |
| 3206 | struct lysc_type *t1, *rt1, *t2, *rt2; |
| 3207 | |
| 3208 | t1 = NULL; |
| 3209 | while ((t1 = warn_is_equal_type_next_type(type1, t1))) { |
| 3210 | if (t1->basetype == LY_TYPE_LEAFREF) { |
| 3211 | rt1 = ((struct lysc_type_leafref *)t1)->realtype; |
| 3212 | } else { |
| 3213 | rt1 = t1; |
| 3214 | } |
| 3215 | |
| 3216 | t2 = NULL; |
| 3217 | while ((t2 = warn_is_equal_type_next_type(type2, t2))) { |
| 3218 | if (t2->basetype == LY_TYPE_LEAFREF) { |
| 3219 | rt2 = ((struct lysc_type_leafref *)t2)->realtype; |
| 3220 | } else { |
| 3221 | rt2 = t2; |
| 3222 | } |
| 3223 | |
| 3224 | if (rt2->basetype == rt1->basetype) { |
| 3225 | /* match found */ |
| 3226 | return 1; |
| 3227 | } |
| 3228 | } |
| 3229 | } |
| 3230 | |
| 3231 | return 0; |
| 3232 | } |
| 3233 | |
| 3234 | /** |
| 3235 | * @brief Check both operands of comparison operators. |
| 3236 | * |
| 3237 | * @param[in] ctx Context for errors. |
| 3238 | * @param[in] set1 First operand set. |
| 3239 | * @param[in] set2 Second operand set. |
| 3240 | * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!='). |
| 3241 | * @param[in] expr Start of the expression to print with the warning. |
| 3242 | * @param[in] tok_pos Token position. |
| 3243 | */ |
| 3244 | static void |
| 3245 | warn_operands(struct ly_ctx *ctx, struct lyxp_set *set1, struct lyxp_set *set2, int numbers_only, const char *expr, uint16_t tok_pos) |
| 3246 | { |
| 3247 | struct lysc_node_leaf *node1, *node2; |
| 3248 | int leaves = 1, warning = 0; |
| 3249 | |
| 3250 | node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1); |
| 3251 | node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2); |
| 3252 | |
| 3253 | if (!node1 && !node2) { |
| 3254 | /* no node-sets involved, nothing to do */ |
| 3255 | return; |
| 3256 | } |
| 3257 | |
| 3258 | if (node1) { |
| 3259 | if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3260 | LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name); |
| 3261 | warning = 1; |
| 3262 | leaves = 0; |
| 3263 | } else if (numbers_only && !warn_is_numeric_type(node1->type)) { |
| 3264 | LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name); |
| 3265 | warning = 1; |
| 3266 | } |
| 3267 | } |
| 3268 | |
| 3269 | if (node2) { |
| 3270 | if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3271 | LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name); |
| 3272 | warning = 1; |
| 3273 | leaves = 0; |
| 3274 | } else if (numbers_only && !warn_is_numeric_type(node2->type)) { |
| 3275 | LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name); |
| 3276 | warning = 1; |
| 3277 | } |
| 3278 | } |
| 3279 | |
| 3280 | if (node1 && node2 && leaves && !numbers_only) { |
| 3281 | if ((warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type)) |
| 3282 | || (!warn_is_numeric_type(node1->type) && warn_is_numeric_type(node2->type)) |
| 3283 | || (!warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type) |
| 3284 | && !warn_is_equal_type(node1->type, node2->type))) { |
| 3285 | LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name); |
| 3286 | warning = 1; |
| 3287 | } |
| 3288 | } |
| 3289 | |
| 3290 | if (warning) { |
| 3291 | LOGWRN(ctx, "Previous warning generated by XPath subexpression[%u] \"%.20s\".", tok_pos, expr + tok_pos); |
| 3292 | } |
| 3293 | } |
| 3294 | |
| 3295 | /** |
| 3296 | * @brief Check that a value is valid for a leaf. If not applicable, does nothing. |
| 3297 | * |
| 3298 | * @param[in] exp Parsed XPath expression. |
| 3299 | * @param[in] set Set with the leaf/leaf-list. |
| 3300 | * @param[in] val_exp Index of the value (literal/number) in @p exp. |
| 3301 | * @param[in] equal_exp Index of the start of the equality expression in @p exp. |
| 3302 | * @param[in] last_equal_exp Index of the end of the equality expression in @p exp. |
| 3303 | */ |
| 3304 | static void |
| 3305 | warn_equality_value(struct lyxp_expr *exp, struct lyxp_set *set, uint16_t val_exp, uint16_t equal_exp, uint16_t last_equal_exp) |
| 3306 | { |
| 3307 | struct lysc_node *scnode; |
| 3308 | struct lysc_type *type; |
| 3309 | char *value; |
| 3310 | LY_ERR rc; |
| 3311 | struct ly_err_item *err = NULL; |
| 3312 | |
| 3313 | if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST)) |
| 3314 | && ((exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) || (exp->tokens[val_exp] == LYXP_TOKEN_NUMBER))) { |
| 3315 | /* check that the node can have the specified value */ |
| 3316 | if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) { |
| 3317 | value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2); |
| 3318 | } else { |
| 3319 | value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]); |
| 3320 | } |
| 3321 | if (!value) { |
| 3322 | LOGMEM(set->ctx); |
| 3323 | return; |
| 3324 | } |
| 3325 | |
| 3326 | if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) { |
| 3327 | LOGWRN(set->ctx, "Identityref \"%s\" comparison with identity \"%s\" without prefix, consider adding" |
| 3328 | " a prefix or best using \"derived-from(-or-self)()\" functions.", scnode->name, value); |
| 3329 | LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp], |
| 3330 | (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp], |
| 3331 | exp->expr + exp->tok_pos[equal_exp]); |
| 3332 | } |
| 3333 | |
| 3334 | type = ((struct lysc_node_leaf *)scnode)->type; |
| 3335 | if (type->basetype != LY_TYPE_IDENT) { |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 3336 | rc = type->plugin->store(set->ctx, type, value, strlen(value), LY_TYPE_OPTS_SCHEMA, LY_PREF_SCHEMA, |
| 3337 | (void *)set->local_mod, NULL, NULL, NULL, NULL, &err); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3338 | |
| 3339 | if (err) { |
| 3340 | LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg); |
| 3341 | ly_err_free(err); |
| 3342 | } else if (rc != LY_SUCCESS) { |
| 3343 | LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value); |
| 3344 | } |
| 3345 | if (rc != LY_SUCCESS) { |
| 3346 | LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp], |
| 3347 | (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp], |
| 3348 | exp->expr + exp->tok_pos[equal_exp]); |
| 3349 | } |
| 3350 | } |
| 3351 | free(value); |
| 3352 | } |
| 3353 | } |
| 3354 | |
| 3355 | /* |
| 3356 | * XPath functions |
| 3357 | */ |
| 3358 | |
| 3359 | /** |
| 3360 | * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN |
| 3361 | * depending on whether the first node bit value from the second argument is set. |
| 3362 | * |
| 3363 | * @param[in] args Array of arguments. |
| 3364 | * @param[in] arg_count Count of elements in @p args. |
| 3365 | * @param[in,out] set Context and result set at the same time. |
| 3366 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3367 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3368 | */ |
| 3369 | static LY_ERR |
| 3370 | xpath_bit_is_set(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3371 | { |
| 3372 | struct lyd_node_term *leaf; |
| 3373 | struct lysc_node_leaf *sleaf; |
| 3374 | struct lysc_type_bits *bits; |
| 3375 | LY_ERR rc = LY_SUCCESS; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3376 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3377 | |
| 3378 | if (options & LYXP_SCNODE_ALL) { |
| 3379 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3380 | 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] | 3381 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3382 | 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] | 3383 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) { |
| 3384 | 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] | 3385 | } |
| 3386 | |
| 3387 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 3388 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3389 | 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] | 3390 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3391 | 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] | 3392 | } |
| 3393 | } |
| 3394 | set_scnode_clear_ctx(set); |
| 3395 | return rc; |
| 3396 | } |
| 3397 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3398 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3399 | 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)"); |
| 3400 | return LY_EVALID; |
| 3401 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3402 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3403 | LY_CHECK_RET(rc); |
| 3404 | |
| 3405 | set_fill_boolean(set, 0); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3406 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3407 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
| 3408 | if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) |
| 3409 | && (((struct lysc_node_leaf *)leaf->schema)->type->basetype == LY_TYPE_BITS)) { |
| 3410 | bits = (struct lysc_type_bits *)((struct lysc_node_leaf *)leaf->schema)->type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3411 | LY_ARRAY_FOR(bits->bits, u) { |
| 3412 | if (!strcmp(bits->bits[u].name, args[1]->val.str)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3413 | set_fill_boolean(set, 1); |
| 3414 | break; |
| 3415 | } |
| 3416 | } |
| 3417 | } |
| 3418 | } |
| 3419 | |
| 3420 | return LY_SUCCESS; |
| 3421 | } |
| 3422 | |
| 3423 | /** |
| 3424 | * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN |
| 3425 | * with the argument converted to boolean. |
| 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 |
| 3434 | xpath_boolean(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3435 | { |
| 3436 | LY_ERR rc; |
| 3437 | |
| 3438 | if (options & LYXP_SCNODE_ALL) { |
| 3439 | set_scnode_clear_ctx(set); |
| 3440 | return LY_SUCCESS; |
| 3441 | } |
| 3442 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3443 | rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3444 | LY_CHECK_RET(rc); |
| 3445 | set_fill_set(set, args[0]); |
| 3446 | |
| 3447 | return LY_SUCCESS; |
| 3448 | } |
| 3449 | |
| 3450 | /** |
| 3451 | * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER |
| 3452 | * with the first argument rounded up to the nearest integer. |
| 3453 | * |
| 3454 | * @param[in] args Array of arguments. |
| 3455 | * @param[in] arg_count Count of elements in @p args. |
| 3456 | * @param[in,out] set Context and result set at the same time. |
| 3457 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3458 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3459 | */ |
| 3460 | static LY_ERR |
| 3461 | xpath_ceiling(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3462 | { |
| 3463 | struct lysc_node_leaf *sleaf; |
| 3464 | LY_ERR rc = LY_SUCCESS; |
| 3465 | |
| 3466 | if (options & LYXP_SCNODE_ALL) { |
| 3467 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3468 | 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] | 3469 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3470 | 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] | 3471 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) { |
| 3472 | 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] | 3473 | } |
| 3474 | set_scnode_clear_ctx(set); |
| 3475 | return rc; |
| 3476 | } |
| 3477 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3478 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3479 | LY_CHECK_RET(rc); |
| 3480 | if ((long long)args[0]->val.num != args[0]->val.num) { |
| 3481 | set_fill_number(set, ((long long)args[0]->val.num) + 1); |
| 3482 | } else { |
| 3483 | set_fill_number(set, args[0]->val.num); |
| 3484 | } |
| 3485 | |
| 3486 | return LY_SUCCESS; |
| 3487 | } |
| 3488 | |
| 3489 | /** |
| 3490 | * @brief Execute the XPath concat(string, string, string*) function. |
| 3491 | * Returns LYXP_SET_STRING with the concatenation of all the arguments. |
| 3492 | * |
| 3493 | * @param[in] args Array of arguments. |
| 3494 | * @param[in] arg_count Count of elements in @p args. |
| 3495 | * @param[in,out] set Context and result set at the same time. |
| 3496 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3497 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3498 | */ |
| 3499 | static LY_ERR |
| 3500 | xpath_concat(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options) |
| 3501 | { |
| 3502 | uint16_t i; |
| 3503 | char *str = NULL; |
| 3504 | size_t used = 1; |
| 3505 | LY_ERR rc = LY_SUCCESS; |
| 3506 | struct lysc_node_leaf *sleaf; |
| 3507 | |
| 3508 | if (options & LYXP_SCNODE_ALL) { |
| 3509 | for (i = 0; i < arg_count; ++i) { |
| 3510 | if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) { |
| 3511 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3512 | LOGWRN(set->ctx, "Argument #%u of %s is a %s node \"%s\".", |
| 3513 | i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3514 | } else if (!warn_is_string_type(sleaf->type)) { |
Radek Krejci | 70124c8 | 2020-08-14 22:17:03 +0200 | [diff] [blame] | 3515 | 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] | 3516 | } |
| 3517 | } |
| 3518 | } |
| 3519 | set_scnode_clear_ctx(set); |
| 3520 | return rc; |
| 3521 | } |
| 3522 | |
| 3523 | for (i = 0; i < arg_count; ++i) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3524 | rc = lyxp_set_cast(args[i], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3525 | if (rc != LY_SUCCESS) { |
| 3526 | free(str); |
| 3527 | return rc; |
| 3528 | } |
| 3529 | |
| 3530 | str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char)); |
| 3531 | LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM); |
| 3532 | strcpy(str + used - 1, args[i]->val.str); |
| 3533 | used += strlen(args[i]->val.str); |
| 3534 | } |
| 3535 | |
| 3536 | /* free, kind of */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3537 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3538 | set->type = LYXP_SET_STRING; |
| 3539 | set->val.str = str; |
| 3540 | |
| 3541 | return LY_SUCCESS; |
| 3542 | } |
| 3543 | |
| 3544 | /** |
| 3545 | * @brief Execute the XPath contains(string, string) function. |
| 3546 | * Returns LYXP_SET_BOOLEAN whether the second argument can |
| 3547 | * be found in the first or not. |
| 3548 | * |
| 3549 | * @param[in] args Array of arguments. |
| 3550 | * @param[in] arg_count Count of elements in @p args. |
| 3551 | * @param[in,out] set Context and result set at the same time. |
| 3552 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3553 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3554 | */ |
| 3555 | static LY_ERR |
| 3556 | xpath_contains(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3557 | { |
| 3558 | struct lysc_node_leaf *sleaf; |
| 3559 | LY_ERR rc = LY_SUCCESS; |
| 3560 | |
| 3561 | if (options & LYXP_SCNODE_ALL) { |
| 3562 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3563 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3564 | 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] | 3565 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3566 | 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] | 3567 | } |
| 3568 | } |
| 3569 | |
| 3570 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 3571 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3572 | 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] | 3573 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3574 | 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] | 3575 | } |
| 3576 | } |
| 3577 | set_scnode_clear_ctx(set); |
| 3578 | return rc; |
| 3579 | } |
| 3580 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3581 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3582 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3583 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3584 | LY_CHECK_RET(rc); |
| 3585 | |
| 3586 | if (strstr(args[0]->val.str, args[1]->val.str)) { |
| 3587 | set_fill_boolean(set, 1); |
| 3588 | } else { |
| 3589 | set_fill_boolean(set, 0); |
| 3590 | } |
| 3591 | |
| 3592 | return LY_SUCCESS; |
| 3593 | } |
| 3594 | |
| 3595 | /** |
| 3596 | * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER |
| 3597 | * with the size of the node-set from the argument. |
| 3598 | * |
| 3599 | * @param[in] args Array of arguments. |
| 3600 | * @param[in] arg_count Count of elements in @p args. |
| 3601 | * @param[in,out] set Context and result set at the same time. |
| 3602 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3603 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3604 | */ |
| 3605 | static LY_ERR |
| 3606 | xpath_count(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3607 | { |
| 3608 | struct lysc_node *scnode = NULL; |
| 3609 | LY_ERR rc = LY_SUCCESS; |
| 3610 | |
| 3611 | if (options & LYXP_SCNODE_ALL) { |
| 3612 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(scnode = warn_get_scnode_in_ctx(args[0]))) { |
| 3613 | 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] | 3614 | } |
| 3615 | set_scnode_clear_ctx(set); |
| 3616 | return rc; |
| 3617 | } |
| 3618 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3619 | if (args[0]->type != LYXP_SET_NODE_SET) { |
| 3620 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "count(node-set)"); |
| 3621 | return LY_EVALID; |
| 3622 | } |
| 3623 | |
| 3624 | set_fill_number(set, args[0]->used); |
| 3625 | return LY_SUCCESS; |
| 3626 | } |
| 3627 | |
| 3628 | /** |
| 3629 | * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET |
| 3630 | * with the context with the intial node. |
| 3631 | * |
| 3632 | * @param[in] args Array of arguments. |
| 3633 | * @param[in] arg_count Count of elements in @p args. |
| 3634 | * @param[in,out] set Context and result set at the same time. |
| 3635 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3636 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3637 | */ |
| 3638 | static LY_ERR |
| 3639 | xpath_current(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options) |
| 3640 | { |
| 3641 | if (arg_count || args) { |
| 3642 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGCOUNT, arg_count, "current()"); |
| 3643 | return LY_EVALID; |
| 3644 | } |
| 3645 | |
| 3646 | if (options & LYXP_SCNODE_ALL) { |
| 3647 | set_scnode_clear_ctx(set); |
| 3648 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 3649 | lyxp_set_scnode_insert_node(set, set->ctx_scnode, LYXP_NODE_ELEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3650 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3651 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3652 | |
| 3653 | /* position is filled later */ |
| 3654 | set_insert_node(set, set->ctx_node, 0, LYXP_NODE_ELEM, 0); |
| 3655 | } |
| 3656 | |
| 3657 | return LY_SUCCESS; |
| 3658 | } |
| 3659 | |
| 3660 | /** |
| 3661 | * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either |
| 3662 | * leafref or instance-identifier target node(s). |
| 3663 | * |
| 3664 | * @param[in] args Array of arguments. |
| 3665 | * @param[in] arg_count Count of elements in @p args. |
| 3666 | * @param[in,out] set Context and result set at the same time. |
| 3667 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3668 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3669 | */ |
| 3670 | static LY_ERR |
| 3671 | xpath_deref(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3672 | { |
| 3673 | struct lyd_node_term *leaf; |
Michal Vasko | 42e497c | 2020-01-06 08:38:25 +0100 | [diff] [blame] | 3674 | struct lysc_node_leaf *sleaf = NULL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3675 | struct lysc_type_leafref *lref; |
Michal Vasko | ae9e4cb | 2019-09-25 08:43:05 +0200 | [diff] [blame] | 3676 | const struct lysc_node *target; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3677 | struct ly_path *p; |
| 3678 | struct lyd_node *node; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3679 | char *errmsg = NULL; |
| 3680 | const char *val; |
| 3681 | int dynamic; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 3682 | uint8_t oper; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3683 | LY_ERR rc = LY_SUCCESS; |
| 3684 | |
| 3685 | if (options & LYXP_SCNODE_ALL) { |
| 3686 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3687 | 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] | 3688 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3689 | 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] | 3690 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) && !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) { |
| 3691 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"leafref\" nor \"instance-identifier\".", |
| 3692 | __func__, sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3693 | } |
| 3694 | set_scnode_clear_ctx(set); |
Michal Vasko | 42e497c | 2020-01-06 08:38:25 +0100 | [diff] [blame] | 3695 | if (sleaf && (sleaf->type->basetype == LY_TYPE_LEAFREF)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3696 | lref = (struct lysc_type_leafref *)sleaf->type; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 3697 | 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] | 3698 | |
| 3699 | /* it was already evaluated on schema, it must succeed */ |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 3700 | rc = ly_path_compile(set->ctx, sleaf->module, (struct lysc_node *)sleaf, lref->path, LY_PATH_LREF_TRUE, |
| 3701 | oper, LY_PATH_TARGET_MANY, set->format, lref->path_context, &p); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3702 | assert(!rc); |
| 3703 | |
| 3704 | /* get the target node */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3705 | target = p[LY_ARRAY_COUNT(p) - 1].node; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3706 | ly_path_free(set->ctx, p); |
| 3707 | |
| 3708 | lyxp_set_scnode_insert_node(set, target, LYXP_NODE_ELEM); |
Michal Vasko | ae9e4cb | 2019-09-25 08:43:05 +0200 | [diff] [blame] | 3709 | } |
| 3710 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3711 | return rc; |
| 3712 | } |
| 3713 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3714 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3715 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "deref(node-set)"); |
| 3716 | return LY_EVALID; |
| 3717 | } |
| 3718 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3719 | lyxp_set_free_content(set); |
| 3720 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3721 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
| 3722 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 3723 | if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 3724 | if (sleaf->type->basetype == LY_TYPE_LEAFREF) { |
| 3725 | /* find leafref target */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3726 | if (ly_type_find_leafref((struct lysc_type_leafref *)sleaf->type, (struct lyd_node *)leaf, |
| 3727 | &leaf->value, set->tree, &node, &errmsg)) { |
| 3728 | LOGERR(set->ctx, LY_EVALID, errmsg); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3729 | free(errmsg); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3730 | return LY_EVALID; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3731 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3732 | } else { |
| 3733 | assert(sleaf->type->basetype == LY_TYPE_INST); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3734 | if (ly_path_eval(leaf->value.target, set->tree, &node)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3735 | val = lyd_value2str(leaf, &dynamic); |
| 3736 | LOGERR(set->ctx, LY_EVALID, "Invalid instance-identifier \"%s\" value - required instance not found.", val); |
| 3737 | if (dynamic) { |
| 3738 | free((char *)val); |
| 3739 | } |
| 3740 | return LY_EVALID; |
| 3741 | } |
| 3742 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3743 | |
| 3744 | /* insert it */ |
| 3745 | set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3746 | } |
| 3747 | } |
| 3748 | |
| 3749 | return LY_SUCCESS; |
| 3750 | } |
| 3751 | |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3752 | static LY_ERR |
| 3753 | xpath_derived_(struct lyxp_set **args, struct lyxp_set *set, int options, int self_match, const char *func) |
| 3754 | { |
| 3755 | uint16_t i; |
| 3756 | LY_ARRAY_COUNT_TYPE u; |
| 3757 | struct lyd_node_term *leaf; |
| 3758 | struct lysc_node_leaf *sleaf; |
| 3759 | struct lyd_meta *meta; |
| 3760 | struct lyd_value data = {0}, *val; |
| 3761 | struct ly_err_item *err = NULL; |
| 3762 | LY_ERR rc = LY_SUCCESS; |
| 3763 | int found; |
| 3764 | |
| 3765 | if (options & LYXP_SCNODE_ALL) { |
| 3766 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3767 | LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", func); |
| 3768 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3769 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype), |
| 3770 | sleaf->name); |
| 3771 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) { |
| 3772 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", func, sleaf->name); |
| 3773 | } |
| 3774 | |
| 3775 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 3776 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3777 | LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype), |
| 3778 | sleaf->name); |
| 3779 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3780 | LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", func, sleaf->name); |
| 3781 | } |
| 3782 | } |
| 3783 | set_scnode_clear_ctx(set); |
| 3784 | return rc; |
| 3785 | } |
| 3786 | |
| 3787 | if (args[0]->type != LYXP_SET_NODE_SET) { |
| 3788 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), |
| 3789 | "derived-from(-or-self)(node-set, string)"); |
| 3790 | return LY_EVALID; |
| 3791 | } |
| 3792 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
| 3793 | LY_CHECK_RET(rc); |
| 3794 | |
| 3795 | set_fill_boolean(set, 0); |
| 3796 | found = 0; |
| 3797 | for (i = 0; i < args[0]->used; ++i) { |
| 3798 | if ((args[0]->val.nodes[i].type != LYXP_NODE_ELEM) && (args[0]->val.nodes[i].type != LYXP_NODE_META)) { |
| 3799 | continue; |
| 3800 | } |
| 3801 | |
| 3802 | if (args[0]->val.nodes[i].type == LYXP_NODE_ELEM) { |
| 3803 | leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node; |
| 3804 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 3805 | val = &leaf->value; |
| 3806 | if (!(sleaf->nodetype & LYD_NODE_TERM) || (leaf->value.realtype->basetype != LY_TYPE_IDENT)) { |
| 3807 | /* uninteresting */ |
| 3808 | continue; |
| 3809 | } |
| 3810 | |
| 3811 | /* store args[1] as ident */ |
| 3812 | rc = val->realtype->plugin->store(set->ctx, val->realtype, args[1]->val.str, strlen(args[1]->val.str), |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 3813 | LY_TYPE_OPTS_STORE, set->format, (void *)set->local_mod, |
| 3814 | (struct lyd_node *)leaf, set->tree, &data, NULL, &err); |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3815 | } else { |
| 3816 | meta = args[0]->val.meta[i].meta; |
| 3817 | val = &meta->value; |
| 3818 | if (val->realtype->basetype != LY_TYPE_IDENT) { |
| 3819 | /* uninteresting */ |
| 3820 | continue; |
| 3821 | } |
| 3822 | |
| 3823 | /* store args[1] as ident */ |
| 3824 | rc = val->realtype->plugin->store(set->ctx, val->realtype, args[1]->val.str, strlen(args[1]->val.str), |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 3825 | LY_TYPE_OPTS_STORE, set->format, (void *)meta->annotation->module, |
| 3826 | meta->parent, set->tree, &data, NULL, &err); |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3827 | } |
| 3828 | |
| 3829 | if (err) { |
| 3830 | ly_err_print(err); |
| 3831 | ly_err_free(err); |
| 3832 | } |
| 3833 | LY_CHECK_RET(rc); |
| 3834 | |
| 3835 | /* finally check the identity itself */ |
| 3836 | if (self_match && (data.ident == val->ident)) { |
| 3837 | set_fill_boolean(set, 1); |
| 3838 | found = 1; |
| 3839 | } |
| 3840 | if (!found) { |
| 3841 | LY_ARRAY_FOR(data.ident->derived, u) { |
| 3842 | if (data.ident->derived[u] == val->ident) { |
| 3843 | set_fill_boolean(set, 1); |
| 3844 | found = 1; |
| 3845 | break; |
| 3846 | } |
| 3847 | } |
| 3848 | } |
| 3849 | |
| 3850 | /* free temporary value */ |
| 3851 | val->realtype->plugin->free(set->ctx, &data); |
| 3852 | if (found) { |
| 3853 | break; |
| 3854 | } |
| 3855 | } |
| 3856 | |
| 3857 | return LY_SUCCESS; |
| 3858 | } |
| 3859 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3860 | /** |
| 3861 | * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending |
| 3862 | * on whether the first argument nodes contain a node of an identity derived from the second |
| 3863 | * argument identity. |
| 3864 | * |
| 3865 | * @param[in] args Array of arguments. |
| 3866 | * @param[in] arg_count Count of elements in @p args. |
| 3867 | * @param[in,out] set Context and result set at the same time. |
| 3868 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3869 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3870 | */ |
| 3871 | static LY_ERR |
| 3872 | xpath_derived_from(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3873 | { |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3874 | return xpath_derived_(args, set, options, 0, __func__); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3875 | } |
| 3876 | |
| 3877 | /** |
| 3878 | * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending |
| 3879 | * on whether the first argument nodes contain a node of an identity that either is or is derived from |
| 3880 | * the second argument identity. |
| 3881 | * |
| 3882 | * @param[in] args Array of arguments. |
| 3883 | * @param[in] arg_count Count of elements in @p args. |
| 3884 | * @param[in,out] set Context and result set at the same time. |
| 3885 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3886 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3887 | */ |
| 3888 | static LY_ERR |
| 3889 | xpath_derived_from_or_self(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3890 | { |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3891 | return xpath_derived_(args, set, options, 1, __func__); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3892 | } |
| 3893 | |
| 3894 | /** |
| 3895 | * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER |
| 3896 | * with the integer value of the first node's enum value, otherwise NaN. |
| 3897 | * |
| 3898 | * @param[in] args Array of arguments. |
| 3899 | * @param[in] arg_count Count of elements in @p args. |
| 3900 | * @param[in,out] set Context and result set at the same time. |
| 3901 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3902 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3903 | */ |
| 3904 | static LY_ERR |
| 3905 | xpath_enum_value(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3906 | { |
| 3907 | struct lyd_node_term *leaf; |
| 3908 | struct lysc_node_leaf *sleaf; |
| 3909 | LY_ERR rc = LY_SUCCESS; |
| 3910 | |
| 3911 | if (options & LYXP_SCNODE_ALL) { |
| 3912 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3913 | 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] | 3914 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3915 | 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] | 3916 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) { |
| 3917 | 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] | 3918 | } |
| 3919 | set_scnode_clear_ctx(set); |
| 3920 | return rc; |
| 3921 | } |
| 3922 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3923 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3924 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "enum-value(node-set)"); |
| 3925 | return LY_EVALID; |
| 3926 | } |
| 3927 | |
| 3928 | set_fill_number(set, NAN); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3929 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3930 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
| 3931 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 3932 | if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) { |
| 3933 | set_fill_number(set, leaf->value.enum_item->value); |
| 3934 | } |
| 3935 | } |
| 3936 | |
| 3937 | return LY_SUCCESS; |
| 3938 | } |
| 3939 | |
| 3940 | /** |
| 3941 | * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN |
| 3942 | * with false value. |
| 3943 | * |
| 3944 | * @param[in] args Array of arguments. |
| 3945 | * @param[in] arg_count Count of elements in @p args. |
| 3946 | * @param[in,out] set Context and result set at the same time. |
| 3947 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3948 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3949 | */ |
| 3950 | static LY_ERR |
| 3951 | xpath_false(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3952 | { |
| 3953 | if (options & LYXP_SCNODE_ALL) { |
| 3954 | set_scnode_clear_ctx(set); |
| 3955 | return LY_SUCCESS; |
| 3956 | } |
| 3957 | |
| 3958 | set_fill_boolean(set, 0); |
| 3959 | return LY_SUCCESS; |
| 3960 | } |
| 3961 | |
| 3962 | /** |
| 3963 | * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER |
| 3964 | * with the first argument floored (truncated). |
| 3965 | * |
| 3966 | * @param[in] args Array of arguments. |
| 3967 | * @param[in] arg_count Count of elements in @p args. |
| 3968 | * @param[in,out] set Context and result set at the same time. |
| 3969 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3970 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3971 | */ |
| 3972 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3973 | xpath_floor(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int UNUSED(options)) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3974 | { |
| 3975 | LY_ERR rc; |
| 3976 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3977 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3978 | LY_CHECK_RET(rc); |
| 3979 | if (isfinite(args[0]->val.num)) { |
| 3980 | set_fill_number(set, (long long)args[0]->val.num); |
| 3981 | } |
| 3982 | |
| 3983 | return LY_SUCCESS; |
| 3984 | } |
| 3985 | |
| 3986 | /** |
| 3987 | * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN |
| 3988 | * whether the language of the text matches the one from the argument. |
| 3989 | * |
| 3990 | * @param[in] args Array of arguments. |
| 3991 | * @param[in] arg_count Count of elements in @p args. |
| 3992 | * @param[in,out] set Context and result set at the same time. |
| 3993 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3994 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3995 | */ |
| 3996 | static LY_ERR |
| 3997 | xpath_lang(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3998 | { |
| 3999 | const struct lyd_node *node; |
| 4000 | struct lysc_node_leaf *sleaf; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4001 | struct lyd_meta *meta = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4002 | const char *val; |
| 4003 | int i, dynamic; |
| 4004 | LY_ERR rc = LY_SUCCESS; |
| 4005 | |
| 4006 | if (options & LYXP_SCNODE_ALL) { |
| 4007 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4008 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4009 | 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] | 4010 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4011 | 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] | 4012 | } |
| 4013 | } |
| 4014 | set_scnode_clear_ctx(set); |
| 4015 | return rc; |
| 4016 | } |
| 4017 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4018 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4019 | LY_CHECK_RET(rc); |
| 4020 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4021 | if (set->type != LYXP_SET_NODE_SET) { |
| 4022 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "lang(string)"); |
| 4023 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4024 | } else if (!set->used) { |
| 4025 | set_fill_boolean(set, 0); |
| 4026 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4027 | } |
| 4028 | |
| 4029 | switch (set->val.nodes[0].type) { |
| 4030 | case LYXP_NODE_ELEM: |
| 4031 | case LYXP_NODE_TEXT: |
| 4032 | node = set->val.nodes[0].node; |
| 4033 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4034 | case LYXP_NODE_META: |
| 4035 | node = set->val.meta[0].meta->parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4036 | break; |
| 4037 | default: |
| 4038 | /* nothing to do with roots */ |
| 4039 | set_fill_boolean(set, 0); |
| 4040 | return LY_SUCCESS; |
| 4041 | } |
| 4042 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4043 | /* find lang metadata */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4044 | for (; node; node = (struct lyd_node *)node->parent) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4045 | for (meta = node->meta; meta; meta = meta->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4046 | /* annotations */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4047 | 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] | 4048 | break; |
| 4049 | } |
| 4050 | } |
| 4051 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4052 | if (meta) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4053 | break; |
| 4054 | } |
| 4055 | } |
| 4056 | |
| 4057 | /* compare languages */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4058 | if (!meta) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4059 | set_fill_boolean(set, 0); |
| 4060 | } else { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4061 | val = lyd_meta2str(meta, &dynamic); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4062 | for (i = 0; args[0]->val.str[i]; ++i) { |
| 4063 | if (tolower(args[0]->val.str[i]) != tolower(val[i])) { |
| 4064 | set_fill_boolean(set, 0); |
| 4065 | break; |
| 4066 | } |
| 4067 | } |
| 4068 | if (!args[0]->val.str[i]) { |
| 4069 | if (!val[i] || (val[i] == '-')) { |
| 4070 | set_fill_boolean(set, 1); |
| 4071 | } else { |
| 4072 | set_fill_boolean(set, 0); |
| 4073 | } |
| 4074 | } |
| 4075 | if (dynamic) { |
| 4076 | free((char *)val); |
| 4077 | } |
| 4078 | } |
| 4079 | |
| 4080 | return LY_SUCCESS; |
| 4081 | } |
| 4082 | |
| 4083 | /** |
| 4084 | * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER |
| 4085 | * with the context size. |
| 4086 | * |
| 4087 | * @param[in] args Array of arguments. |
| 4088 | * @param[in] arg_count Count of elements in @p args. |
| 4089 | * @param[in,out] set Context and result set at the same time. |
| 4090 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4091 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4092 | */ |
| 4093 | static LY_ERR |
| 4094 | xpath_last(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 4095 | { |
| 4096 | if (options & LYXP_SCNODE_ALL) { |
| 4097 | set_scnode_clear_ctx(set); |
| 4098 | return LY_SUCCESS; |
| 4099 | } |
| 4100 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4101 | if (set->type != LYXP_SET_NODE_SET) { |
| 4102 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "last()"); |
| 4103 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4104 | } else if (!set->used) { |
| 4105 | set_fill_number(set, 0); |
| 4106 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4107 | } |
| 4108 | |
| 4109 | set_fill_number(set, set->ctx_size); |
| 4110 | return LY_SUCCESS; |
| 4111 | } |
| 4112 | |
| 4113 | /** |
| 4114 | * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING |
| 4115 | * with the node name without namespace from the argument or the context. |
| 4116 | * |
| 4117 | * @param[in] args Array of arguments. |
| 4118 | * @param[in] arg_count Count of elements in @p args. |
| 4119 | * @param[in,out] set Context and result set at the same time. |
| 4120 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4121 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4122 | */ |
| 4123 | static LY_ERR |
| 4124 | xpath_local_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options) |
| 4125 | { |
| 4126 | struct lyxp_set_node *item; |
| 4127 | /* suppress unused variable warning */ |
| 4128 | (void)options; |
| 4129 | |
| 4130 | if (options & LYXP_SCNODE_ALL) { |
| 4131 | set_scnode_clear_ctx(set); |
| 4132 | return LY_SUCCESS; |
| 4133 | } |
| 4134 | |
| 4135 | if (arg_count) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4136 | if (args[0]->type != LYXP_SET_NODE_SET) { |
| 4137 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "local-name(node-set?)"); |
| 4138 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4139 | } else if (!args[0]->used) { |
| 4140 | set_fill_string(set, "", 0); |
| 4141 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4142 | } |
| 4143 | |
| 4144 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4145 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4146 | |
| 4147 | item = &args[0]->val.nodes[0]; |
| 4148 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4149 | if (set->type != LYXP_SET_NODE_SET) { |
| 4150 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "local-name(node-set?)"); |
| 4151 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4152 | } else if (!set->used) { |
| 4153 | set_fill_string(set, "", 0); |
| 4154 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4155 | } |
| 4156 | |
| 4157 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4158 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4159 | |
| 4160 | item = &set->val.nodes[0]; |
| 4161 | } |
| 4162 | |
| 4163 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4164 | case LYXP_NODE_NONE: |
| 4165 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4166 | case LYXP_NODE_ROOT: |
| 4167 | case LYXP_NODE_ROOT_CONFIG: |
| 4168 | case LYXP_NODE_TEXT: |
| 4169 | set_fill_string(set, "", 0); |
| 4170 | break; |
| 4171 | case LYXP_NODE_ELEM: |
| 4172 | set_fill_string(set, item->node->schema->name, strlen(item->node->schema->name)); |
| 4173 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4174 | case LYXP_NODE_META: |
| 4175 | 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] | 4176 | break; |
| 4177 | } |
| 4178 | |
| 4179 | return LY_SUCCESS; |
| 4180 | } |
| 4181 | |
| 4182 | /** |
| 4183 | * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING |
| 4184 | * with the node name fully qualified (with namespace) from the argument or the context. |
| 4185 | * |
| 4186 | * @param[in] args Array of arguments. |
| 4187 | * @param[in] arg_count Count of elements in @p args. |
| 4188 | * @param[in,out] set Context and result set at the same time. |
| 4189 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4190 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4191 | */ |
| 4192 | static LY_ERR |
| 4193 | xpath_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options) |
| 4194 | { |
| 4195 | struct lyxp_set_node *item; |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 4196 | struct lys_module *mod = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4197 | char *str; |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 4198 | const char *name = NULL; |
| 4199 | int rc = -1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4200 | |
| 4201 | if (options & LYXP_SCNODE_ALL) { |
| 4202 | set_scnode_clear_ctx(set); |
| 4203 | return LY_SUCCESS; |
| 4204 | } |
| 4205 | |
| 4206 | if (arg_count) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4207 | if (args[0]->type != LYXP_SET_NODE_SET) { |
| 4208 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "name(node-set?)"); |
| 4209 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4210 | } else if (!args[0]->used) { |
| 4211 | set_fill_string(set, "", 0); |
| 4212 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4213 | } |
| 4214 | |
| 4215 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4216 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4217 | |
| 4218 | item = &args[0]->val.nodes[0]; |
| 4219 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4220 | if (set->type != LYXP_SET_NODE_SET) { |
| 4221 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "name(node-set?)"); |
| 4222 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4223 | } else if (!set->used) { |
| 4224 | set_fill_string(set, "", 0); |
| 4225 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4226 | } |
| 4227 | |
| 4228 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4229 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4230 | |
| 4231 | item = &set->val.nodes[0]; |
| 4232 | } |
| 4233 | |
| 4234 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4235 | case LYXP_NODE_NONE: |
| 4236 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4237 | case LYXP_NODE_ROOT: |
| 4238 | case LYXP_NODE_ROOT_CONFIG: |
| 4239 | case LYXP_NODE_TEXT: |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 4240 | /* keep NULL */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4241 | break; |
| 4242 | case LYXP_NODE_ELEM: |
| 4243 | mod = item->node->schema->module; |
| 4244 | name = item->node->schema->name; |
| 4245 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4246 | case LYXP_NODE_META: |
| 4247 | mod = ((struct lyd_meta *)item->node)->annotation->module; |
| 4248 | name = ((struct lyd_meta *)item->node)->name; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4249 | break; |
| 4250 | } |
| 4251 | |
| 4252 | if (mod && name) { |
| 4253 | switch (set->format) { |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 4254 | case LY_PREF_SCHEMA: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4255 | rc = asprintf(&str, "%s:%s", lys_prefix_find_module(set->local_mod, mod), name); |
| 4256 | break; |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 4257 | case LY_PREF_JSON: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4258 | rc = asprintf(&str, "%s:%s", mod->name, name); |
| 4259 | break; |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 4260 | case LY_PREF_XML: |
Michal Vasko | 9409ef6 | 2019-09-12 11:47:17 +0200 | [diff] [blame] | 4261 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4262 | } |
| 4263 | LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM); |
| 4264 | set_fill_string(set, str, strlen(str)); |
| 4265 | free(str); |
| 4266 | } else { |
| 4267 | set_fill_string(set, "", 0); |
| 4268 | } |
| 4269 | |
| 4270 | return LY_SUCCESS; |
| 4271 | } |
| 4272 | |
| 4273 | /** |
| 4274 | * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING |
| 4275 | * with the namespace of the node from the argument or the context. |
| 4276 | * |
| 4277 | * @param[in] args Array of arguments. |
| 4278 | * @param[in] arg_count Count of elements in @p args. |
| 4279 | * @param[in,out] set Context and result set at the same time. |
| 4280 | * @param[in] options XPath options. |
| 4281 | * @return LY_ERR (LY_EINVAL for wrong arguments on schema) |
| 4282 | */ |
| 4283 | static LY_ERR |
| 4284 | xpath_namespace_uri(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options) |
| 4285 | { |
| 4286 | struct lyxp_set_node *item; |
| 4287 | struct lys_module *mod; |
| 4288 | /* suppress unused variable warning */ |
| 4289 | (void)options; |
| 4290 | |
| 4291 | if (options & LYXP_SCNODE_ALL) { |
| 4292 | set_scnode_clear_ctx(set); |
| 4293 | return LY_SUCCESS; |
| 4294 | } |
| 4295 | |
| 4296 | if (arg_count) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4297 | if (args[0]->type != LYXP_SET_NODE_SET) { |
| 4298 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), |
| 4299 | "namespace-uri(node-set?)"); |
| 4300 | return LY_EVALID; |
| 4301 | } else if (!args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4302 | set_fill_string(set, "", 0); |
| 4303 | return LY_SUCCESS; |
| 4304 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4305 | |
| 4306 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4307 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4308 | |
| 4309 | item = &args[0]->val.nodes[0]; |
| 4310 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4311 | if (set->type != LYXP_SET_NODE_SET) { |
| 4312 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "namespace-uri(node-set?)"); |
| 4313 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4314 | } else if (!set->used) { |
| 4315 | set_fill_string(set, "", 0); |
| 4316 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4317 | } |
| 4318 | |
| 4319 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4320 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4321 | |
| 4322 | item = &set->val.nodes[0]; |
| 4323 | } |
| 4324 | |
| 4325 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4326 | case LYXP_NODE_NONE: |
| 4327 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4328 | case LYXP_NODE_ROOT: |
| 4329 | case LYXP_NODE_ROOT_CONFIG: |
| 4330 | case LYXP_NODE_TEXT: |
| 4331 | set_fill_string(set, "", 0); |
| 4332 | break; |
| 4333 | case LYXP_NODE_ELEM: |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4334 | case LYXP_NODE_META: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4335 | if (item->type == LYXP_NODE_ELEM) { |
| 4336 | mod = item->node->schema->module; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4337 | } else { /* LYXP_NODE_META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4338 | /* annotations */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4339 | mod = ((struct lyd_meta *)item->node)->annotation->module; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4340 | } |
| 4341 | |
| 4342 | set_fill_string(set, mod->ns, strlen(mod->ns)); |
| 4343 | break; |
| 4344 | } |
| 4345 | |
| 4346 | return LY_SUCCESS; |
| 4347 | } |
| 4348 | |
| 4349 | /** |
| 4350 | * @brief Execute the XPath node() function (node type). Returns LYXP_SET_NODE_SET |
| 4351 | * with only nodes from the context. In practice it either leaves the context |
| 4352 | * as it is or returns an empty node set. |
| 4353 | * |
| 4354 | * @param[in] args Array of arguments. |
| 4355 | * @param[in] arg_count Count of elements in @p args. |
| 4356 | * @param[in,out] set Context and result set at the same time. |
| 4357 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4358 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4359 | */ |
| 4360 | static LY_ERR |
| 4361 | xpath_node(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 4362 | { |
| 4363 | if (options & LYXP_SCNODE_ALL) { |
| 4364 | set_scnode_clear_ctx(set); |
| 4365 | return LY_SUCCESS; |
| 4366 | } |
| 4367 | |
| 4368 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4369 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4370 | } |
| 4371 | return LY_SUCCESS; |
| 4372 | } |
| 4373 | |
| 4374 | /** |
| 4375 | * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING |
| 4376 | * with normalized value (no leading, trailing, double white spaces) of the node |
| 4377 | * from the argument or the context. |
| 4378 | * |
| 4379 | * @param[in] args Array of arguments. |
| 4380 | * @param[in] arg_count Count of elements in @p args. |
| 4381 | * @param[in,out] set Context and result set at the same time. |
| 4382 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4383 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4384 | */ |
| 4385 | static LY_ERR |
| 4386 | xpath_normalize_space(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options) |
| 4387 | { |
| 4388 | uint16_t i, new_used; |
| 4389 | char *new; |
| 4390 | int have_spaces = 0, space_before = 0; |
| 4391 | struct lysc_node_leaf *sleaf; |
| 4392 | LY_ERR rc = LY_SUCCESS; |
| 4393 | |
| 4394 | if (options & LYXP_SCNODE_ALL) { |
| 4395 | if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4396 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4397 | 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] | 4398 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4399 | 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] | 4400 | } |
| 4401 | } |
| 4402 | set_scnode_clear_ctx(set); |
| 4403 | return rc; |
| 4404 | } |
| 4405 | |
| 4406 | if (arg_count) { |
| 4407 | set_fill_set(set, args[0]); |
| 4408 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4409 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4410 | LY_CHECK_RET(rc); |
| 4411 | |
| 4412 | /* is there any normalization necessary? */ |
| 4413 | for (i = 0; set->val.str[i]; ++i) { |
| 4414 | if (is_xmlws(set->val.str[i])) { |
| 4415 | if ((i == 0) || space_before || (!set->val.str[i + 1])) { |
| 4416 | have_spaces = 1; |
| 4417 | break; |
| 4418 | } |
| 4419 | space_before = 1; |
| 4420 | } else { |
| 4421 | space_before = 0; |
| 4422 | } |
| 4423 | } |
| 4424 | |
| 4425 | /* yep, there is */ |
| 4426 | if (have_spaces) { |
| 4427 | /* it's enough, at least one character will go, makes space for ending '\0' */ |
| 4428 | new = malloc(strlen(set->val.str) * sizeof(char)); |
| 4429 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 4430 | new_used = 0; |
| 4431 | |
| 4432 | space_before = 0; |
| 4433 | for (i = 0; set->val.str[i]; ++i) { |
| 4434 | if (is_xmlws(set->val.str[i])) { |
| 4435 | if ((i == 0) || space_before) { |
| 4436 | space_before = 1; |
| 4437 | continue; |
| 4438 | } else { |
| 4439 | space_before = 1; |
| 4440 | } |
| 4441 | } else { |
| 4442 | space_before = 0; |
| 4443 | } |
| 4444 | |
| 4445 | new[new_used] = (space_before ? ' ' : set->val.str[i]); |
| 4446 | ++new_used; |
| 4447 | } |
| 4448 | |
| 4449 | /* at worst there is one trailing space now */ |
| 4450 | if (new_used && is_xmlws(new[new_used - 1])) { |
| 4451 | --new_used; |
| 4452 | } |
| 4453 | |
| 4454 | new = ly_realloc(new, (new_used + 1) * sizeof(char)); |
| 4455 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 4456 | new[new_used] = '\0'; |
| 4457 | |
| 4458 | free(set->val.str); |
| 4459 | set->val.str = new; |
| 4460 | } |
| 4461 | |
| 4462 | return LY_SUCCESS; |
| 4463 | } |
| 4464 | |
| 4465 | /** |
| 4466 | * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN |
| 4467 | * with the argument converted to boolean and logically inverted. |
| 4468 | * |
| 4469 | * @param[in] args Array of arguments. |
| 4470 | * @param[in] arg_count Count of elements in @p args. |
| 4471 | * @param[in,out] set Context and result set at the same time. |
| 4472 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4473 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4474 | */ |
| 4475 | static LY_ERR |
| 4476 | xpath_not(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 4477 | { |
| 4478 | if (options & LYXP_SCNODE_ALL) { |
| 4479 | set_scnode_clear_ctx(set); |
| 4480 | return LY_SUCCESS; |
| 4481 | } |
| 4482 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4483 | lyxp_set_cast(args[0], LYXP_SET_BOOLEAN); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4484 | if (args[0]->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4485 | set_fill_boolean(set, 0); |
| 4486 | } else { |
| 4487 | set_fill_boolean(set, 1); |
| 4488 | } |
| 4489 | |
| 4490 | return LY_SUCCESS; |
| 4491 | } |
| 4492 | |
| 4493 | /** |
| 4494 | * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER |
| 4495 | * with the number representation of either the argument or the context. |
| 4496 | * |
| 4497 | * @param[in] args Array of arguments. |
| 4498 | * @param[in] arg_count Count of elements in @p args. |
| 4499 | * @param[in,out] set Context and result set at the same time. |
| 4500 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4501 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4502 | */ |
| 4503 | static LY_ERR |
| 4504 | xpath_number(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options) |
| 4505 | { |
| 4506 | LY_ERR rc; |
| 4507 | |
| 4508 | if (options & LYXP_SCNODE_ALL) { |
| 4509 | set_scnode_clear_ctx(set); |
| 4510 | return LY_SUCCESS; |
| 4511 | } |
| 4512 | |
| 4513 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4514 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4515 | LY_CHECK_RET(rc); |
| 4516 | set_fill_set(set, args[0]); |
| 4517 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4518 | rc = lyxp_set_cast(set, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4519 | LY_CHECK_RET(rc); |
| 4520 | } |
| 4521 | |
| 4522 | return LY_SUCCESS; |
| 4523 | } |
| 4524 | |
| 4525 | /** |
| 4526 | * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER |
| 4527 | * with the context position. |
| 4528 | * |
| 4529 | * @param[in] args Array of arguments. |
| 4530 | * @param[in] arg_count Count of elements in @p args. |
| 4531 | * @param[in,out] set Context and result set at the same time. |
| 4532 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4533 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4534 | */ |
| 4535 | static LY_ERR |
| 4536 | xpath_position(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 4537 | { |
| 4538 | if (options & LYXP_SCNODE_ALL) { |
| 4539 | set_scnode_clear_ctx(set); |
| 4540 | return LY_SUCCESS; |
| 4541 | } |
| 4542 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4543 | if (set->type != LYXP_SET_NODE_SET) { |
| 4544 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "position()"); |
| 4545 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4546 | } else if (!set->used) { |
| 4547 | set_fill_number(set, 0); |
| 4548 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4549 | } |
| 4550 | |
| 4551 | set_fill_number(set, set->ctx_pos); |
| 4552 | |
| 4553 | /* UNUSED in 'Release' build type */ |
| 4554 | (void)options; |
| 4555 | return LY_SUCCESS; |
| 4556 | } |
| 4557 | |
| 4558 | /** |
| 4559 | * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN |
| 4560 | * depending on whether the second argument regex matches the first argument string. For details refer to |
| 4561 | * YANG 1.1 RFC section 10.2.1. |
| 4562 | * |
| 4563 | * @param[in] args Array of arguments. |
| 4564 | * @param[in] arg_count Count of elements in @p args. |
| 4565 | * @param[in,out] set Context and result set at the same time. |
| 4566 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4567 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4568 | */ |
| 4569 | static LY_ERR |
| 4570 | xpath_re_match(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 4571 | { |
| 4572 | struct lysc_pattern **patterns = NULL, **pattern; |
| 4573 | struct lysc_node_leaf *sleaf; |
| 4574 | char *path; |
| 4575 | LY_ERR rc = LY_SUCCESS; |
| 4576 | struct ly_err_item *err; |
| 4577 | |
| 4578 | if (options & LYXP_SCNODE_ALL) { |
| 4579 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4580 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4581 | 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] | 4582 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4583 | 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] | 4584 | } |
| 4585 | } |
| 4586 | |
| 4587 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4588 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4589 | 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] | 4590 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4591 | 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] | 4592 | } |
| 4593 | } |
| 4594 | set_scnode_clear_ctx(set); |
| 4595 | return rc; |
| 4596 | } |
| 4597 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4598 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4599 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4600 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4601 | LY_CHECK_RET(rc); |
| 4602 | |
| 4603 | LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM); |
| 4604 | *pattern = malloc(sizeof **pattern); |
| 4605 | path = lyd_path(set->ctx_node, LYD_PATH_LOG, NULL, 0); |
| 4606 | rc = lys_compile_type_pattern_check(set->ctx, path, args[1]->val.str, &(*pattern)->code); |
| 4607 | free(path); |
| 4608 | if (rc != LY_SUCCESS) { |
| 4609 | LY_ARRAY_FREE(patterns); |
| 4610 | return rc; |
| 4611 | } |
| 4612 | |
| 4613 | rc = ly_type_validate_patterns(patterns, args[0]->val.str, strlen(args[0]->val.str), &err); |
| 4614 | pcre2_code_free((*pattern)->code); |
| 4615 | free(*pattern); |
| 4616 | LY_ARRAY_FREE(patterns); |
| 4617 | if (rc && (rc != LY_EVALID)) { |
| 4618 | ly_err_print(err); |
| 4619 | ly_err_free(err); |
| 4620 | return rc; |
| 4621 | } |
| 4622 | |
| 4623 | if (rc == LY_EVALID) { |
| 4624 | ly_err_free(err); |
| 4625 | set_fill_boolean(set, 0); |
| 4626 | } else { |
| 4627 | set_fill_boolean(set, 1); |
| 4628 | } |
| 4629 | |
| 4630 | return LY_SUCCESS; |
| 4631 | } |
| 4632 | |
| 4633 | /** |
| 4634 | * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER |
| 4635 | * with the rounded first argument. For details refer to |
| 4636 | * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round. |
| 4637 | * |
| 4638 | * @param[in] args Array of arguments. |
| 4639 | * @param[in] arg_count Count of elements in @p args. |
| 4640 | * @param[in,out] set Context and result set at the same time. |
| 4641 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4642 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4643 | */ |
| 4644 | static LY_ERR |
| 4645 | xpath_round(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 4646 | { |
| 4647 | struct lysc_node_leaf *sleaf; |
| 4648 | LY_ERR rc = LY_SUCCESS; |
| 4649 | |
| 4650 | if (options & LYXP_SCNODE_ALL) { |
| 4651 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4652 | 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] | 4653 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4654 | 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] | 4655 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) { |
| 4656 | 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] | 4657 | } |
| 4658 | set_scnode_clear_ctx(set); |
| 4659 | return rc; |
| 4660 | } |
| 4661 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4662 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4663 | LY_CHECK_RET(rc); |
| 4664 | |
| 4665 | /* cover only the cases where floor can't be used */ |
| 4666 | if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) { |
| 4667 | set_fill_number(set, -0.0f); |
| 4668 | } else { |
| 4669 | args[0]->val.num += 0.5; |
| 4670 | rc = xpath_floor(args, 1, args[0], options); |
| 4671 | LY_CHECK_RET(rc); |
| 4672 | set_fill_number(set, args[0]->val.num); |
| 4673 | } |
| 4674 | |
| 4675 | return LY_SUCCESS; |
| 4676 | } |
| 4677 | |
| 4678 | /** |
| 4679 | * @brief Execute the XPath starts-with(string, string) function. |
| 4680 | * Returns LYXP_SET_BOOLEAN whether the second argument is |
| 4681 | * the prefix of the first or not. |
| 4682 | * |
| 4683 | * @param[in] args Array of arguments. |
| 4684 | * @param[in] arg_count Count of elements in @p args. |
| 4685 | * @param[in,out] set Context and result set at the same time. |
| 4686 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4687 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4688 | */ |
| 4689 | static LY_ERR |
| 4690 | xpath_starts_with(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 4691 | { |
| 4692 | struct lysc_node_leaf *sleaf; |
| 4693 | LY_ERR rc = LY_SUCCESS; |
| 4694 | |
| 4695 | if (options & LYXP_SCNODE_ALL) { |
| 4696 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4697 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4698 | 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] | 4699 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4700 | 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] | 4701 | } |
| 4702 | } |
| 4703 | |
| 4704 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4705 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4706 | 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] | 4707 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4708 | 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] | 4709 | } |
| 4710 | } |
| 4711 | set_scnode_clear_ctx(set); |
| 4712 | return rc; |
| 4713 | } |
| 4714 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4715 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4716 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4717 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4718 | LY_CHECK_RET(rc); |
| 4719 | |
| 4720 | if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) { |
| 4721 | set_fill_boolean(set, 0); |
| 4722 | } else { |
| 4723 | set_fill_boolean(set, 1); |
| 4724 | } |
| 4725 | |
| 4726 | return LY_SUCCESS; |
| 4727 | } |
| 4728 | |
| 4729 | /** |
| 4730 | * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING |
| 4731 | * with the string representation of either the argument or the context. |
| 4732 | * |
| 4733 | * @param[in] args Array of arguments. |
| 4734 | * @param[in] arg_count Count of elements in @p args. |
| 4735 | * @param[in,out] set Context and result set at the same time. |
| 4736 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4737 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4738 | */ |
| 4739 | static LY_ERR |
| 4740 | xpath_string(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options) |
| 4741 | { |
| 4742 | LY_ERR rc; |
| 4743 | |
| 4744 | if (options & LYXP_SCNODE_ALL) { |
| 4745 | set_scnode_clear_ctx(set); |
| 4746 | return LY_SUCCESS; |
| 4747 | } |
| 4748 | |
| 4749 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4750 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4751 | LY_CHECK_RET(rc); |
| 4752 | set_fill_set(set, args[0]); |
| 4753 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4754 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4755 | LY_CHECK_RET(rc); |
| 4756 | } |
| 4757 | |
| 4758 | return LY_SUCCESS; |
| 4759 | } |
| 4760 | |
| 4761 | /** |
| 4762 | * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER |
| 4763 | * with the length of the string in either the argument or the context. |
| 4764 | * |
| 4765 | * @param[in] args Array of arguments. |
| 4766 | * @param[in] arg_count Count of elements in @p args. |
| 4767 | * @param[in,out] set Context and result set at the same time. |
| 4768 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4769 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4770 | */ |
| 4771 | static LY_ERR |
| 4772 | xpath_string_length(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options) |
| 4773 | { |
| 4774 | struct lysc_node_leaf *sleaf; |
| 4775 | LY_ERR rc = LY_SUCCESS; |
| 4776 | |
| 4777 | if (options & LYXP_SCNODE_ALL) { |
| 4778 | if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4779 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4780 | 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] | 4781 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4782 | 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] | 4783 | } |
| 4784 | } |
| 4785 | if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) { |
| 4786 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4787 | 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] | 4788 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4789 | 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] | 4790 | } |
| 4791 | } |
| 4792 | set_scnode_clear_ctx(set); |
| 4793 | return rc; |
| 4794 | } |
| 4795 | |
| 4796 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4797 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4798 | LY_CHECK_RET(rc); |
| 4799 | set_fill_number(set, strlen(args[0]->val.str)); |
| 4800 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4801 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4802 | LY_CHECK_RET(rc); |
| 4803 | set_fill_number(set, strlen(set->val.str)); |
| 4804 | } |
| 4805 | |
| 4806 | return LY_SUCCESS; |
| 4807 | } |
| 4808 | |
| 4809 | /** |
| 4810 | * @brief Execute the XPath substring(string, number, number?) function. |
| 4811 | * Returns LYXP_SET_STRING substring of the first argument starting |
| 4812 | * on the second argument index ending on the third argument index, |
| 4813 | * indexed from 1. For exact definition refer to |
| 4814 | * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring. |
| 4815 | * |
| 4816 | * @param[in] args Array of arguments. |
| 4817 | * @param[in] arg_count Count of elements in @p args. |
| 4818 | * @param[in,out] set Context and result set at the same time. |
| 4819 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4820 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4821 | */ |
| 4822 | static LY_ERR |
| 4823 | xpath_substring(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options) |
| 4824 | { |
| 4825 | int start, len; |
| 4826 | uint16_t str_start, str_len, pos; |
| 4827 | struct lysc_node_leaf *sleaf; |
| 4828 | LY_ERR rc = LY_SUCCESS; |
| 4829 | |
| 4830 | if (options & LYXP_SCNODE_ALL) { |
| 4831 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4832 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4833 | 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] | 4834 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4835 | 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] | 4836 | } |
| 4837 | } |
| 4838 | |
| 4839 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4840 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4841 | 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] | 4842 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 4843 | 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] | 4844 | } |
| 4845 | } |
| 4846 | |
| 4847 | if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET) |
| 4848 | && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) { |
| 4849 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4850 | 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] | 4851 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 4852 | 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] | 4853 | } |
| 4854 | } |
| 4855 | set_scnode_clear_ctx(set); |
| 4856 | return rc; |
| 4857 | } |
| 4858 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4859 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4860 | LY_CHECK_RET(rc); |
| 4861 | |
| 4862 | /* start */ |
| 4863 | if (xpath_round(&args[1], 1, args[1], options)) { |
| 4864 | return -1; |
| 4865 | } |
| 4866 | if (isfinite(args[1]->val.num)) { |
| 4867 | start = args[1]->val.num - 1; |
| 4868 | } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) { |
| 4869 | start = INT_MIN; |
| 4870 | } else { |
| 4871 | start = INT_MAX; |
| 4872 | } |
| 4873 | |
| 4874 | /* len */ |
| 4875 | if (arg_count == 3) { |
| 4876 | rc = xpath_round(&args[2], 1, args[2], options); |
| 4877 | LY_CHECK_RET(rc); |
| 4878 | if (isfinite(args[2]->val.num)) { |
| 4879 | len = args[2]->val.num; |
| 4880 | } else if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) { |
| 4881 | len = 0; |
| 4882 | } else { |
| 4883 | len = INT_MAX; |
| 4884 | } |
| 4885 | } else { |
| 4886 | len = INT_MAX; |
| 4887 | } |
| 4888 | |
| 4889 | /* find matching character positions */ |
| 4890 | str_start = 0; |
| 4891 | str_len = 0; |
| 4892 | for (pos = 0; args[0]->val.str[pos]; ++pos) { |
| 4893 | if (pos < start) { |
| 4894 | ++str_start; |
| 4895 | } else if (pos < start + len) { |
| 4896 | ++str_len; |
| 4897 | } else { |
| 4898 | break; |
| 4899 | } |
| 4900 | } |
| 4901 | |
| 4902 | set_fill_string(set, args[0]->val.str + str_start, str_len); |
| 4903 | return LY_SUCCESS; |
| 4904 | } |
| 4905 | |
| 4906 | /** |
| 4907 | * @brief Execute the XPath substring-after(string, string) function. |
| 4908 | * Returns LYXP_SET_STRING with the string succeeding the occurance |
| 4909 | * of the second argument in the first or an empty string. |
| 4910 | * |
| 4911 | * @param[in] args Array of arguments. |
| 4912 | * @param[in] arg_count Count of elements in @p args. |
| 4913 | * @param[in,out] set Context and result set at the same time. |
| 4914 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4915 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4916 | */ |
| 4917 | static LY_ERR |
| 4918 | xpath_substring_after(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 4919 | { |
| 4920 | char *ptr; |
| 4921 | struct lysc_node_leaf *sleaf; |
| 4922 | LY_ERR rc = LY_SUCCESS; |
| 4923 | |
| 4924 | if (options & LYXP_SCNODE_ALL) { |
| 4925 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4926 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4927 | 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] | 4928 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4929 | 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] | 4930 | } |
| 4931 | } |
| 4932 | |
| 4933 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4934 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4935 | 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] | 4936 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4937 | 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] | 4938 | } |
| 4939 | } |
| 4940 | set_scnode_clear_ctx(set); |
| 4941 | return rc; |
| 4942 | } |
| 4943 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4944 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4945 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4946 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4947 | LY_CHECK_RET(rc); |
| 4948 | |
| 4949 | ptr = strstr(args[0]->val.str, args[1]->val.str); |
| 4950 | if (ptr) { |
| 4951 | set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str))); |
| 4952 | } else { |
| 4953 | set_fill_string(set, "", 0); |
| 4954 | } |
| 4955 | |
| 4956 | return LY_SUCCESS; |
| 4957 | } |
| 4958 | |
| 4959 | /** |
| 4960 | * @brief Execute the XPath substring-before(string, string) function. |
| 4961 | * Returns LYXP_SET_STRING with the string preceding the occurance |
| 4962 | * of the second argument in the first or an empty string. |
| 4963 | * |
| 4964 | * @param[in] args Array of arguments. |
| 4965 | * @param[in] arg_count Count of elements in @p args. |
| 4966 | * @param[in,out] set Context and result set at the same time. |
| 4967 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4968 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4969 | */ |
| 4970 | static LY_ERR |
| 4971 | xpath_substring_before(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 4972 | { |
| 4973 | char *ptr; |
| 4974 | struct lysc_node_leaf *sleaf; |
| 4975 | LY_ERR rc = LY_SUCCESS; |
| 4976 | |
| 4977 | if (options & LYXP_SCNODE_ALL) { |
| 4978 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4979 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4980 | 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] | 4981 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4982 | 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] | 4983 | } |
| 4984 | } |
| 4985 | |
| 4986 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4987 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4988 | 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] | 4989 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4990 | 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] | 4991 | } |
| 4992 | } |
| 4993 | set_scnode_clear_ctx(set); |
| 4994 | return rc; |
| 4995 | } |
| 4996 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4997 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4998 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4999 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5000 | LY_CHECK_RET(rc); |
| 5001 | |
| 5002 | ptr = strstr(args[0]->val.str, args[1]->val.str); |
| 5003 | if (ptr) { |
| 5004 | set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str); |
| 5005 | } else { |
| 5006 | set_fill_string(set, "", 0); |
| 5007 | } |
| 5008 | |
| 5009 | return LY_SUCCESS; |
| 5010 | } |
| 5011 | |
| 5012 | /** |
| 5013 | * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER |
| 5014 | * with the sum of all the nodes in the context. |
| 5015 | * |
| 5016 | * @param[in] args Array of arguments. |
| 5017 | * @param[in] arg_count Count of elements in @p args. |
| 5018 | * @param[in,out] set Context and result set at the same time. |
| 5019 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5020 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5021 | */ |
| 5022 | static LY_ERR |
| 5023 | xpath_sum(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 5024 | { |
| 5025 | long double num; |
| 5026 | char *str; |
| 5027 | uint16_t i; |
| 5028 | struct lyxp_set set_item; |
| 5029 | struct lysc_node_leaf *sleaf; |
| 5030 | LY_ERR rc = LY_SUCCESS; |
| 5031 | |
| 5032 | if (options & LYXP_SCNODE_ALL) { |
| 5033 | if (args[0]->type == LYXP_SET_SCNODE_SET) { |
| 5034 | for (i = 0; i < args[0]->used; ++i) { |
| 5035 | if (args[0]->val.scnodes[i].in_ctx == 1) { |
| 5036 | sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode; |
| 5037 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5038 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, |
| 5039 | lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5040 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 5041 | 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] | 5042 | } |
| 5043 | } |
| 5044 | } |
| 5045 | } |
| 5046 | set_scnode_clear_ctx(set); |
| 5047 | return rc; |
| 5048 | } |
| 5049 | |
| 5050 | set_fill_number(set, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5051 | |
| 5052 | if (args[0]->type != LYXP_SET_NODE_SET) { |
| 5053 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "sum(node-set)"); |
| 5054 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5055 | } else if (!args[0]->used) { |
| 5056 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5057 | } |
| 5058 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5059 | set_init(&set_item, set); |
| 5060 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5061 | set_item.type = LYXP_SET_NODE_SET; |
| 5062 | set_item.val.nodes = malloc(sizeof *set_item.val.nodes); |
| 5063 | LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM); |
| 5064 | |
| 5065 | set_item.used = 1; |
| 5066 | set_item.size = 1; |
| 5067 | |
| 5068 | for (i = 0; i < args[0]->used; ++i) { |
| 5069 | set_item.val.nodes[0] = args[0]->val.nodes[i]; |
| 5070 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5071 | rc = cast_node_set_to_string(&set_item, &str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5072 | LY_CHECK_RET(rc); |
| 5073 | num = cast_string_to_number(str); |
| 5074 | free(str); |
| 5075 | set->val.num += num; |
| 5076 | } |
| 5077 | |
| 5078 | free(set_item.val.nodes); |
| 5079 | |
| 5080 | return LY_SUCCESS; |
| 5081 | } |
| 5082 | |
| 5083 | /** |
| 5084 | * @brief Execute the XPath text() function (node type). Returns LYXP_SET_NODE_SET |
| 5085 | * with the text content of the nodes in the context. |
| 5086 | * |
| 5087 | * @param[in] args Array of arguments. |
| 5088 | * @param[in] arg_count Count of elements in @p args. |
| 5089 | * @param[in,out] set Context and result set at the same time. |
| 5090 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5091 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5092 | */ |
| 5093 | static LY_ERR |
| 5094 | xpath_text(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 5095 | { |
| 5096 | uint32_t i; |
| 5097 | |
| 5098 | if (options & LYXP_SCNODE_ALL) { |
| 5099 | set_scnode_clear_ctx(set); |
| 5100 | return LY_SUCCESS; |
| 5101 | } |
| 5102 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5103 | if (set->type != LYXP_SET_NODE_SET) { |
| 5104 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "text()"); |
| 5105 | return LY_EVALID; |
| 5106 | } |
| 5107 | |
| 5108 | for (i = 0; i < set->used;) { |
| 5109 | switch (set->val.nodes[i].type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 5110 | case LYXP_NODE_NONE: |
| 5111 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5112 | case LYXP_NODE_ELEM: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5113 | if (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 5114 | set->val.nodes[i].type = LYXP_NODE_TEXT; |
| 5115 | ++i; |
| 5116 | break; |
| 5117 | } |
| 5118 | /* fall through */ |
| 5119 | case LYXP_NODE_ROOT: |
| 5120 | case LYXP_NODE_ROOT_CONFIG: |
| 5121 | case LYXP_NODE_TEXT: |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5122 | case LYXP_NODE_META: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5123 | set_remove_node(set, i); |
| 5124 | break; |
| 5125 | } |
| 5126 | } |
| 5127 | |
| 5128 | return LY_SUCCESS; |
| 5129 | } |
| 5130 | |
| 5131 | /** |
| 5132 | * @brief Execute the XPath translate(string, string, string) function. |
| 5133 | * Returns LYXP_SET_STRING with the first argument with the characters |
| 5134 | * from the second argument replaced by those on the corresponding |
| 5135 | * positions in the third argument. |
| 5136 | * |
| 5137 | * @param[in] args Array of arguments. |
| 5138 | * @param[in] arg_count Count of elements in @p args. |
| 5139 | * @param[in,out] set Context and result set at the same time. |
| 5140 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5141 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5142 | */ |
| 5143 | static LY_ERR |
| 5144 | xpath_translate(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 5145 | { |
| 5146 | uint16_t i, j, new_used; |
| 5147 | char *new; |
| 5148 | int found, have_removed; |
| 5149 | struct lysc_node_leaf *sleaf; |
| 5150 | LY_ERR rc = LY_SUCCESS; |
| 5151 | |
| 5152 | if (options & LYXP_SCNODE_ALL) { |
| 5153 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 5154 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5155 | 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] | 5156 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5157 | 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] | 5158 | } |
| 5159 | } |
| 5160 | |
| 5161 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 5162 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5163 | 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] | 5164 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5165 | 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] | 5166 | } |
| 5167 | } |
| 5168 | |
| 5169 | if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) { |
| 5170 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5171 | 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] | 5172 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5173 | 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] | 5174 | } |
| 5175 | } |
| 5176 | set_scnode_clear_ctx(set); |
| 5177 | return rc; |
| 5178 | } |
| 5179 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5180 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5181 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5182 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5183 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5184 | rc = lyxp_set_cast(args[2], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5185 | LY_CHECK_RET(rc); |
| 5186 | |
| 5187 | new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char)); |
| 5188 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 5189 | new_used = 0; |
| 5190 | |
| 5191 | have_removed = 0; |
| 5192 | for (i = 0; args[0]->val.str[i]; ++i) { |
| 5193 | found = 0; |
| 5194 | |
| 5195 | for (j = 0; args[1]->val.str[j]; ++j) { |
| 5196 | if (args[0]->val.str[i] == args[1]->val.str[j]) { |
| 5197 | /* removing this char */ |
| 5198 | if (j >= strlen(args[2]->val.str)) { |
| 5199 | have_removed = 1; |
| 5200 | found = 1; |
| 5201 | break; |
| 5202 | } |
| 5203 | /* replacing this char */ |
| 5204 | new[new_used] = args[2]->val.str[j]; |
| 5205 | ++new_used; |
| 5206 | found = 1; |
| 5207 | break; |
| 5208 | } |
| 5209 | } |
| 5210 | |
| 5211 | /* copying this char */ |
| 5212 | if (!found) { |
| 5213 | new[new_used] = args[0]->val.str[i]; |
| 5214 | ++new_used; |
| 5215 | } |
| 5216 | } |
| 5217 | |
| 5218 | if (have_removed) { |
| 5219 | new = ly_realloc(new, (new_used + 1) * sizeof(char)); |
| 5220 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 5221 | } |
| 5222 | new[new_used] = '\0'; |
| 5223 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5224 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5225 | set->type = LYXP_SET_STRING; |
| 5226 | set->val.str = new; |
| 5227 | |
| 5228 | return LY_SUCCESS; |
| 5229 | } |
| 5230 | |
| 5231 | /** |
| 5232 | * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN |
| 5233 | * with true value. |
| 5234 | * |
| 5235 | * @param[in] args Array of arguments. |
| 5236 | * @param[in] arg_count Count of elements in @p args. |
| 5237 | * @param[in,out] set Context and result set at the same time. |
| 5238 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5239 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5240 | */ |
| 5241 | static LY_ERR |
| 5242 | xpath_true(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 5243 | { |
| 5244 | if (options & LYXP_SCNODE_ALL) { |
| 5245 | set_scnode_clear_ctx(set); |
| 5246 | return LY_SUCCESS; |
| 5247 | } |
| 5248 | |
| 5249 | set_fill_boolean(set, 1); |
| 5250 | return LY_SUCCESS; |
| 5251 | } |
| 5252 | |
| 5253 | /* |
| 5254 | * moveto functions |
| 5255 | * |
| 5256 | * They and only they actually change the context (set). |
| 5257 | */ |
| 5258 | |
| 5259 | /** |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5260 | * @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] | 5261 | * |
Michal Vasko | 2104e9f | 2020-03-06 08:23:25 +0100 | [diff] [blame] | 5262 | * XPath @p set is expected to be a (sc)node set! |
| 5263 | * |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5264 | * @param[in,out] qname Qualified node name. If includes prefix, it is skipped. |
| 5265 | * @param[in,out] qname_len Length of @p qname, is updated accordingly. |
| 5266 | * @param[in] set Set with XPath context. |
| 5267 | * @param[out] moveto_mod Expected module of a matching node. |
| 5268 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5269 | */ |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5270 | static LY_ERR |
| 5271 | 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] | 5272 | { |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 5273 | const struct lys_module *mod = NULL; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5274 | const char *ptr; |
| 5275 | int pref_len; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5276 | |
Michal Vasko | 2104e9f | 2020-03-06 08:23:25 +0100 | [diff] [blame] | 5277 | assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET)); |
| 5278 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5279 | if ((ptr = ly_strnchr(*qname, ':', *qname_len))) { |
| 5280 | /* specific module */ |
| 5281 | pref_len = ptr - *qname; |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 5282 | 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] | 5283 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5284 | /* check for errors and non-implemented modules, as they are not valid */ |
Juraj Vijtiuk | d75faa6 | 2019-11-26 14:10:10 +0100 | [diff] [blame] | 5285 | if (!mod || !mod->implemented) { |
Michal Vasko | 2104e9f | 2020-03-06 08:23:25 +0100 | [diff] [blame] | 5286 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 5287 | LOGVAL(set->ctx, LY_VLOG_LYSC, set->ctx_scnode, LY_VCODE_XP_INMOD, pref_len, *qname); |
| 5288 | } else { |
| 5289 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INMOD, pref_len, *qname); |
| 5290 | } |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5291 | return LY_EVALID; |
| 5292 | } |
Juraj Vijtiuk | d75faa6 | 2019-11-26 14:10:10 +0100 | [diff] [blame] | 5293 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5294 | *qname += pref_len + 1; |
| 5295 | *qname_len -= pref_len + 1; |
| 5296 | } else if (((*qname)[0] == '*') && (*qname_len == 1)) { |
| 5297 | /* all modules - special case */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5298 | mod = NULL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5299 | } else if (set->type == LYXP_SET_SCNODE_SET) { |
| 5300 | /* current node module */ |
| 5301 | mod = set->ctx_scnode->module; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5302 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5303 | /* current node module */ |
| 5304 | mod = set->ctx_node->schema->module; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5305 | } |
| 5306 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5307 | *moveto_mod = mod; |
| 5308 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5309 | } |
| 5310 | |
| 5311 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5312 | * @brief Move context @p set to the root. Handles absolute path. |
| 5313 | * Result is LYXP_SET_NODE_SET. |
| 5314 | * |
| 5315 | * @param[in,out] set Set to use. |
| 5316 | * @param[in] options Xpath options. |
| 5317 | */ |
| 5318 | static void |
| 5319 | moveto_root(struct lyxp_set *set, int options) |
| 5320 | { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5321 | if (!set) { |
| 5322 | return; |
| 5323 | } |
| 5324 | |
| 5325 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5326 | set_scnode_clear_ctx(set); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 5327 | lyxp_set_scnode_insert_node(set, NULL, set->root_type); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5328 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5329 | set->type = LYXP_SET_NODE_SET; |
| 5330 | set->used = 0; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5331 | set_insert_node(set, NULL, 0, set->root_type, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5332 | } |
| 5333 | } |
| 5334 | |
| 5335 | /** |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 5336 | * @brief Check whether a node has some unresolved "when". |
| 5337 | * |
| 5338 | * @param[in] node Node to check. |
| 5339 | * @return LY_ERR value (LY_EINCOMPLETE if there are some unresolved "when") |
| 5340 | */ |
| 5341 | static LY_ERR |
| 5342 | moveto_when_check(const struct lyd_node *node) |
| 5343 | { |
| 5344 | const struct lysc_node *schema; |
| 5345 | |
| 5346 | if (!node) { |
| 5347 | return LY_SUCCESS; |
| 5348 | } |
| 5349 | |
| 5350 | schema = node->schema; |
| 5351 | do { |
| 5352 | if (schema->when && !(node->flags & LYD_WHEN_TRUE)) { |
| 5353 | return LY_EINCOMPLETE; |
| 5354 | } |
| 5355 | schema = schema->parent; |
| 5356 | } while (schema && (schema->nodetype & (LYS_CASE | LYS_CHOICE))); |
| 5357 | |
| 5358 | return LY_SUCCESS; |
| 5359 | } |
| 5360 | |
| 5361 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5362 | * @brief Check @p node as a part of NameTest processing. |
| 5363 | * |
| 5364 | * @param[in] node Node to check. |
| 5365 | * @param[in] root_type XPath root node type. |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5366 | * @param[in] context_op XPath operation parent. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5367 | * @param[in] node_name Node name in the dictionary to move to, NULL for any node. |
| 5368 | * @param[in] moveto_mod Expected module of the node, NULL for any. |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5369 | * @return LY_ERR (LY_ENOT if node does not match, LY_EINCOMPLETE on unresolved when, |
| 5370 | * LY_EINVAL if netither node nor any children match) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5371 | */ |
| 5372 | static LY_ERR |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5373 | moveto_node_check(const struct lyd_node *node, enum lyxp_node_type root_type, const struct lysc_node *context_op, |
| 5374 | const char *node_name, const struct lys_module *moveto_mod) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5375 | { |
| 5376 | /* module check */ |
| 5377 | if (moveto_mod && (node->schema->module != moveto_mod)) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5378 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5379 | } |
| 5380 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5381 | /* context check */ |
| 5382 | 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] | 5383 | return LY_EINVAL; |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5384 | } else if (context_op && (node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node->schema != context_op)) { |
| 5385 | return LY_EINVAL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5386 | } |
| 5387 | |
| 5388 | /* name check */ |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5389 | if (node_name && strcmp(node_name, "*") && (node->schema->name != node_name)) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5390 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5391 | } |
| 5392 | |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 5393 | /* when check */ |
| 5394 | if (moveto_when_check(node)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5395 | return LY_EINCOMPLETE; |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 5396 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5397 | |
| 5398 | /* match */ |
| 5399 | return LY_SUCCESS; |
| 5400 | } |
| 5401 | |
| 5402 | /** |
| 5403 | * @brief Check @p node as a part of schema NameTest processing. |
| 5404 | * |
| 5405 | * @param[in] node Schema node to check. |
| 5406 | * @param[in] root_type XPath root node type. |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5407 | * @param[in] context_op XPath operation parent. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5408 | * @param[in] node_name Node name in the dictionary to move to, NULL for any nodes. |
| 5409 | * @param[in] moveto_mod Expected module of the node, NULL for any. |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5410 | * @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] | 5411 | */ |
| 5412 | static LY_ERR |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5413 | moveto_scnode_check(const struct lysc_node *node, enum lyxp_node_type root_type, const struct lysc_node *context_op, |
| 5414 | const char *node_name, const struct lys_module *moveto_mod) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5415 | { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5416 | /* module check */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5417 | if (moveto_mod && (node->module != moveto_mod)) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5418 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5419 | } |
| 5420 | |
| 5421 | /* context check */ |
| 5422 | if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) { |
| 5423 | return LY_EINVAL; |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5424 | } else if (context_op && (node->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node != context_op)) { |
| 5425 | return LY_EINVAL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5426 | } |
| 5427 | |
| 5428 | /* name check */ |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5429 | if (node_name && strcmp(node_name, "*") && (node->name != node_name)) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5430 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5431 | } |
| 5432 | |
| 5433 | /* match */ |
| 5434 | return LY_SUCCESS; |
| 5435 | } |
| 5436 | |
| 5437 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5438 | * @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] | 5439 | * |
| 5440 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5441 | * @param[in] mod Matching node module, NULL for any. |
| 5442 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5443 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 5444 | */ |
| 5445 | static LY_ERR |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5446 | 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] | 5447 | { |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 5448 | uint32_t i; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5449 | int replaced; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5450 | const struct lyd_node *siblings, *sub; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5451 | LY_ERR rc; |
| 5452 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5453 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5454 | return LY_SUCCESS; |
| 5455 | } |
| 5456 | |
| 5457 | if (set->type != LYXP_SET_NODE_SET) { |
| 5458 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 5459 | return LY_EVALID; |
| 5460 | } |
| 5461 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5462 | for (i = 0; i < set->used; ) { |
| 5463 | replaced = 0; |
| 5464 | |
| 5465 | 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] | 5466 | assert(!set->val.nodes[i].node); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5467 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5468 | /* search in all the trees */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5469 | siblings = set->tree; |
Michal Vasko | 5bfd4be | 2020-06-23 13:26:19 +0200 | [diff] [blame] | 5470 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5471 | /* search in children */ |
Michal Vasko | 5bfd4be | 2020-06-23 13:26:19 +0200 | [diff] [blame] | 5472 | siblings = lyd_node_children(set->val.nodes[i].node, 0); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5473 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5474 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5475 | for (sub = siblings; sub; sub = sub->next) { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5476 | 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] | 5477 | if (rc == LY_SUCCESS) { |
| 5478 | if (!replaced) { |
| 5479 | set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i); |
| 5480 | replaced = 1; |
| 5481 | } else { |
| 5482 | set_insert_node(set, sub, 0, LYXP_NODE_ELEM, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5483 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5484 | ++i; |
| 5485 | } else if (rc == LY_EINCOMPLETE) { |
| 5486 | return rc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5487 | } |
| 5488 | } |
| 5489 | |
| 5490 | if (!replaced) { |
| 5491 | /* no match */ |
| 5492 | set_remove_node(set, i); |
| 5493 | } |
| 5494 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5495 | |
| 5496 | return LY_SUCCESS; |
| 5497 | } |
| 5498 | |
| 5499 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5500 | * @brief Move context @p set to a node using hashes. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY). |
| 5501 | * Context position aware. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5502 | * |
| 5503 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5504 | * @param[in] scnode Matching node schema. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5505 | * @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] | 5506 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 5507 | */ |
| 5508 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5509 | 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] | 5510 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5511 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5512 | uint32_t i; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5513 | const struct lyd_node *siblings; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5514 | struct lyd_node *sub, *inst = NULL; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5515 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5516 | assert(scnode && (!(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) || predicates)); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5517 | |
| 5518 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5519 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5520 | } |
| 5521 | |
| 5522 | if (set->type != LYXP_SET_NODE_SET) { |
| 5523 | 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] | 5524 | ret = LY_EVALID; |
| 5525 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5526 | } |
| 5527 | |
| 5528 | /* context check for all the nodes since we have the schema node */ |
| 5529 | if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) { |
| 5530 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5531 | goto cleanup; |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5532 | } else if (set->context_op && (scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) |
| 5533 | && (scnode != set->context_op)) { |
| 5534 | lyxp_set_free_content(set); |
| 5535 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5536 | } |
| 5537 | |
| 5538 | /* create specific data instance if needed */ |
| 5539 | if (scnode->nodetype == LYS_LIST) { |
| 5540 | LY_CHECK_GOTO(ret = lyd_create_list(scnode, predicates, &inst), cleanup); |
| 5541 | } else if (scnode->nodetype == LYS_LEAFLIST) { |
| 5542 | 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] | 5543 | } |
| 5544 | |
| 5545 | for (i = 0; i < set->used; ) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5546 | siblings = NULL; |
| 5547 | |
| 5548 | if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) { |
| 5549 | assert(!set->val.nodes[i].node); |
| 5550 | |
| 5551 | /* search in all the trees */ |
| 5552 | siblings = set->tree; |
| 5553 | } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
| 5554 | /* search in children */ |
Michal Vasko | 5bfd4be | 2020-06-23 13:26:19 +0200 | [diff] [blame] | 5555 | siblings = lyd_node_children(set->val.nodes[i].node, 0); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5556 | } |
| 5557 | |
| 5558 | /* find the node using hashes */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5559 | if (inst) { |
| 5560 | lyd_find_sibling_first(siblings, inst, &sub); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5561 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5562 | lyd_find_sibling_val(siblings, scnode, NULL, 0, &sub); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5563 | } |
| 5564 | |
| 5565 | /* when check */ |
| 5566 | if (sub && moveto_when_check(sub)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5567 | ret = LY_EINCOMPLETE; |
| 5568 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5569 | } |
| 5570 | |
| 5571 | if (sub) { |
| 5572 | /* pos filled later */ |
Michal Vasko | cb1b7c0 | 2020-07-03 13:38:12 +0200 | [diff] [blame] | 5573 | set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5574 | ++i; |
Michal Vasko | cb1b7c0 | 2020-07-03 13:38:12 +0200 | [diff] [blame] | 5575 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5576 | /* no match */ |
| 5577 | set_remove_node(set, i); |
| 5578 | } |
| 5579 | } |
| 5580 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5581 | cleanup: |
| 5582 | lyd_free_tree(inst); |
| 5583 | return ret; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5584 | } |
| 5585 | |
| 5586 | /** |
| 5587 | * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY). |
| 5588 | * |
| 5589 | * @param[in,out] set Set to use. |
| 5590 | * @param[in] mod Matching node module, NULL for any. |
| 5591 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5592 | * @param[in] options XPath options. |
| 5593 | * @return LY_ERR |
| 5594 | */ |
| 5595 | static LY_ERR |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5596 | moveto_scnode(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, int options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5597 | { |
Michal Vasko | cafad9d | 2019-11-07 15:20:03 +0100 | [diff] [blame] | 5598 | int i, orig_used, idx, temp_ctx = 0, getnext_opts; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5599 | uint32_t mod_idx; |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 5600 | const struct lysc_node *iter, *start_parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5601 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5602 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5603 | return LY_SUCCESS; |
| 5604 | } |
| 5605 | |
| 5606 | if (set->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 5607 | 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] | 5608 | return LY_EVALID; |
| 5609 | } |
| 5610 | |
Michal Vasko | cafad9d | 2019-11-07 15:20:03 +0100 | [diff] [blame] | 5611 | /* getnext opts */ |
| 5612 | getnext_opts = LYS_GETNEXT_NOSTATECHECK; |
| 5613 | if (options & LYXP_SCNODE_OUTPUT) { |
| 5614 | getnext_opts |= LYS_GETNEXT_OUTPUT; |
| 5615 | } |
| 5616 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5617 | orig_used = set->used; |
| 5618 | for (i = 0; i < orig_used; ++i) { |
| 5619 | if (set->val.scnodes[i].in_ctx != 1) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5620 | if (set->val.scnodes[i].in_ctx != -2) { |
| 5621 | continue; |
| 5622 | } |
| 5623 | |
| 5624 | /* remember context node */ |
| 5625 | set->val.scnodes[i].in_ctx = -1; |
Michal Vasko | ec4df48 | 2019-12-16 10:02:18 +0100 | [diff] [blame] | 5626 | } else { |
| 5627 | set->val.scnodes[i].in_ctx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5628 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5629 | |
| 5630 | start_parent = set->val.scnodes[i].scnode; |
| 5631 | |
| 5632 | 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] | 5633 | /* 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] | 5634 | * so use it directly (root node itself is useless in this case) */ |
| 5635 | mod_idx = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5636 | 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] | 5637 | iter = NULL; |
Michal Vasko | 509de4d | 2019-12-10 14:51:30 +0100 | [diff] [blame] | 5638 | /* module may not be implemented */ |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 5639 | while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5640 | 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] | 5641 | idx = lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5642 | /* we need to prevent these nodes from being considered in this moveto */ |
| 5643 | if ((idx < orig_used) && (idx > i)) { |
| 5644 | set->val.scnodes[idx].in_ctx = 2; |
| 5645 | temp_ctx = 1; |
| 5646 | } |
| 5647 | } |
| 5648 | } |
| 5649 | |
| 5650 | if (!mod_idx) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5651 | /* mod was specified, we are not going through the whole context */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5652 | break; |
| 5653 | } |
| 5654 | /* next iteration */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5655 | mod = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5656 | } |
| 5657 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 5658 | } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) { |
| 5659 | iter = NULL; |
| 5660 | while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5661 | 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] | 5662 | idx = lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5663 | if ((idx < orig_used) && (idx > i)) { |
| 5664 | set->val.scnodes[idx].in_ctx = 2; |
| 5665 | temp_ctx = 1; |
| 5666 | } |
| 5667 | } |
| 5668 | } |
| 5669 | } |
| 5670 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5671 | |
| 5672 | /* correct temporary in_ctx values */ |
| 5673 | if (temp_ctx) { |
| 5674 | for (i = 0; i < orig_used; ++i) { |
| 5675 | if (set->val.scnodes[i].in_ctx == 2) { |
| 5676 | set->val.scnodes[i].in_ctx = 1; |
| 5677 | } |
| 5678 | } |
| 5679 | } |
| 5680 | |
| 5681 | return LY_SUCCESS; |
| 5682 | } |
| 5683 | |
| 5684 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5685 | * @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] | 5686 | * Context position aware. |
| 5687 | * |
| 5688 | * @param[in] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5689 | * @param[in] mod Matching node module, NULL for any. |
| 5690 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5691 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 5692 | */ |
| 5693 | static LY_ERR |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5694 | 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] | 5695 | { |
| 5696 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5697 | const struct lyd_node *next, *elem, *start; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5698 | struct lyxp_set ret_set; |
| 5699 | LY_ERR rc; |
| 5700 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5701 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5702 | return LY_SUCCESS; |
| 5703 | } |
| 5704 | |
| 5705 | if (set->type != LYXP_SET_NODE_SET) { |
| 5706 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 5707 | return LY_EVALID; |
| 5708 | } |
| 5709 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5710 | /* 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] | 5711 | rc = moveto_node(set, NULL, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5712 | LY_CHECK_RET(rc); |
| 5713 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5714 | /* 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] | 5715 | set_init(&ret_set, set); |
| 5716 | for (i = 0; i < set->used; ++i) { |
| 5717 | |
| 5718 | /* TREE DFS */ |
| 5719 | start = set->val.nodes[i].node; |
| 5720 | for (elem = next = start; elem; elem = next) { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5721 | 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] | 5722 | if (!rc) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5723 | /* add matching node into result set */ |
| 5724 | set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used); |
| 5725 | if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) { |
| 5726 | /* the node is a duplicate, we'll process it later in the set */ |
| 5727 | goto skip_children; |
| 5728 | } |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5729 | } else if (rc == LY_EINCOMPLETE) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5730 | return rc; |
| 5731 | } else if (rc == LY_EINVAL) { |
| 5732 | goto skip_children; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5733 | } |
| 5734 | |
| 5735 | /* TREE DFS NEXT ELEM */ |
| 5736 | /* select element for the next run - children first */ |
Michal Vasko | 5bfd4be | 2020-06-23 13:26:19 +0200 | [diff] [blame] | 5737 | next = lyd_node_children(elem, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5738 | if (!next) { |
| 5739 | skip_children: |
| 5740 | /* no children, so try siblings, but only if it's not the start, |
| 5741 | * that is considered to be the root and it's siblings are not traversed */ |
| 5742 | if (elem != start) { |
| 5743 | next = elem->next; |
| 5744 | } else { |
| 5745 | break; |
| 5746 | } |
| 5747 | } |
| 5748 | while (!next) { |
| 5749 | /* no siblings, go back through the parents */ |
| 5750 | if ((struct lyd_node *)elem->parent == start) { |
| 5751 | /* we are done, no next element to process */ |
| 5752 | break; |
| 5753 | } |
| 5754 | /* parent is already processed, go to its sibling */ |
| 5755 | elem = (struct lyd_node *)elem->parent; |
| 5756 | next = elem->next; |
| 5757 | } |
| 5758 | } |
| 5759 | } |
| 5760 | |
| 5761 | /* make the temporary set the current one */ |
| 5762 | ret_set.ctx_pos = set->ctx_pos; |
| 5763 | ret_set.ctx_size = set->ctx_size; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5764 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5765 | memcpy(set, &ret_set, sizeof *set); |
| 5766 | |
| 5767 | return LY_SUCCESS; |
| 5768 | } |
| 5769 | |
| 5770 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5771 | * @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] | 5772 | * |
| 5773 | * @param[in] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5774 | * @param[in] mod Matching node module, NULL for any. |
| 5775 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5776 | * @param[in] options XPath options. |
| 5777 | * @return LY_ERR |
| 5778 | */ |
| 5779 | static LY_ERR |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5780 | moveto_scnode_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, int options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5781 | { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5782 | int i, orig_used, idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5783 | const struct lysc_node *next, *elem, *start; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5784 | LY_ERR rc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5785 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5786 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5787 | return LY_SUCCESS; |
| 5788 | } |
| 5789 | |
| 5790 | if (set->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 5791 | 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] | 5792 | return LY_EVALID; |
| 5793 | } |
| 5794 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5795 | orig_used = set->used; |
| 5796 | for (i = 0; i < orig_used; ++i) { |
| 5797 | if (set->val.scnodes[i].in_ctx != 1) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5798 | if (set->val.scnodes[i].in_ctx != -2) { |
| 5799 | continue; |
| 5800 | } |
| 5801 | |
| 5802 | /* remember context node */ |
| 5803 | set->val.scnodes[i].in_ctx = -1; |
Michal Vasko | ec4df48 | 2019-12-16 10:02:18 +0100 | [diff] [blame] | 5804 | } else { |
| 5805 | set->val.scnodes[i].in_ctx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5806 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5807 | |
| 5808 | /* TREE DFS */ |
| 5809 | start = set->val.scnodes[i].scnode; |
| 5810 | for (elem = next = start; elem; elem = next) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5811 | if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) { |
| 5812 | /* schema-only nodes, skip root */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5813 | goto next_iter; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5814 | } |
| 5815 | |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5816 | 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] | 5817 | if (!rc) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 5818 | 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] | 5819 | set->val.scnodes[idx].in_ctx = 1; |
| 5820 | if (idx > i) { |
| 5821 | /* we will process it later in the set */ |
| 5822 | goto skip_children; |
| 5823 | } |
| 5824 | } else { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 5825 | lyxp_set_scnode_insert_node(set, elem, LYXP_NODE_ELEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5826 | } |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5827 | } else if (rc == LY_EINVAL) { |
| 5828 | goto skip_children; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5829 | } |
| 5830 | |
| 5831 | next_iter: |
| 5832 | /* TREE DFS NEXT ELEM */ |
| 5833 | /* select element for the next run - children first */ |
| 5834 | 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] | 5835 | if (!next) { |
| 5836 | skip_children: |
| 5837 | /* no children, so try siblings, but only if it's not the start, |
| 5838 | * that is considered to be the root and it's siblings are not traversed */ |
| 5839 | if (elem != start) { |
| 5840 | next = elem->next; |
| 5841 | } else { |
| 5842 | break; |
| 5843 | } |
| 5844 | } |
| 5845 | while (!next) { |
| 5846 | /* no siblings, go back through the parents */ |
| 5847 | if (elem->parent == start) { |
| 5848 | /* we are done, no next element to process */ |
| 5849 | break; |
| 5850 | } |
| 5851 | /* parent is already processed, go to its sibling */ |
| 5852 | elem = elem->parent; |
| 5853 | next = elem->next; |
| 5854 | } |
| 5855 | } |
| 5856 | } |
| 5857 | |
| 5858 | return LY_SUCCESS; |
| 5859 | } |
| 5860 | |
| 5861 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5862 | * @brief Move context @p set to an attribute. Result is LYXP_SET_NODE_SET. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5863 | * Indirectly context position aware. |
| 5864 | * |
| 5865 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5866 | * @param[in] mod Matching metadata module, NULL for any. |
| 5867 | * @param[in] ncname Matching metadata name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5868 | * @return LY_ERR |
| 5869 | */ |
| 5870 | static LY_ERR |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5871 | 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] | 5872 | { |
| 5873 | uint32_t i; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5874 | int replaced; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5875 | struct lyd_meta *sub; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5876 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5877 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5878 | return LY_SUCCESS; |
| 5879 | } |
| 5880 | |
| 5881 | if (set->type != LYXP_SET_NODE_SET) { |
| 5882 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 5883 | return LY_EVALID; |
| 5884 | } |
| 5885 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5886 | for (i = 0; i < set->used; ) { |
| 5887 | replaced = 0; |
| 5888 | |
| 5889 | /* only attributes of an elem (not dummy) can be in the result, skip all the rest; |
| 5890 | * our attributes are always qualified */ |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5891 | if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5892 | for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5893 | |
| 5894 | /* check "namespace" */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5895 | if (mod && (sub->annotation->module != mod)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5896 | continue; |
| 5897 | } |
| 5898 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5899 | if (!ncname || (sub->name == ncname)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5900 | /* match */ |
| 5901 | if (!replaced) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5902 | set->val.meta[i].meta = sub; |
| 5903 | set->val.meta[i].type = LYXP_NODE_META; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5904 | /* pos does not change */ |
| 5905 | replaced = 1; |
| 5906 | } else { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5907 | 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] | 5908 | } |
| 5909 | ++i; |
| 5910 | } |
| 5911 | } |
| 5912 | } |
| 5913 | |
| 5914 | if (!replaced) { |
| 5915 | /* no match */ |
| 5916 | set_remove_node(set, i); |
| 5917 | } |
| 5918 | } |
| 5919 | |
| 5920 | return LY_SUCCESS; |
| 5921 | } |
| 5922 | |
| 5923 | /** |
| 5924 | * @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] | 5925 | * Result is LYXP_SET_NODE_SET. Context position aware. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5926 | * |
| 5927 | * @param[in,out] set1 Set to use for the result. |
| 5928 | * @param[in] set2 Set that is copied to @p set1. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5929 | * @return LY_ERR |
| 5930 | */ |
| 5931 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5932 | moveto_union(struct lyxp_set *set1, struct lyxp_set *set2) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5933 | { |
| 5934 | LY_ERR rc; |
| 5935 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5936 | 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] | 5937 | LOGVAL(set1->ctx, LY_VLOG_LYD, set1->ctx_node, LY_VCODE_XP_INOP_2, "union", print_set_type(set1), print_set_type(set2)); |
| 5938 | return LY_EVALID; |
| 5939 | } |
| 5940 | |
| 5941 | /* set2 is empty or both set1 and set2 */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5942 | if (!set2->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5943 | return LY_SUCCESS; |
| 5944 | } |
| 5945 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5946 | if (!set1->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5947 | memcpy(set1, set2, sizeof *set1); |
| 5948 | /* dynamic memory belongs to set1 now, do not free */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5949 | memset(set2, 0, sizeof *set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5950 | return LY_SUCCESS; |
| 5951 | } |
| 5952 | |
| 5953 | /* we assume sets are sorted */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5954 | assert(!set_sort(set1) && !set_sort(set2)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5955 | |
| 5956 | /* sort, remove duplicates */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5957 | rc = set_sorted_merge(set1, set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5958 | LY_CHECK_RET(rc); |
| 5959 | |
| 5960 | /* final set must be sorted */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5961 | assert(!set_sort(set1)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5962 | |
| 5963 | return LY_SUCCESS; |
| 5964 | } |
| 5965 | |
| 5966 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5967 | * @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] | 5968 | * Context position aware. |
| 5969 | * |
| 5970 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5971 | * @param[in] mod Matching metadata module, NULL for any. |
| 5972 | * @param[in] ncname Matching metadata name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5973 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 5974 | */ |
| 5975 | static int |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5976 | 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] | 5977 | { |
| 5978 | uint32_t i; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5979 | int replaced; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5980 | struct lyd_meta *sub; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5981 | struct lyxp_set *set_all_desc = NULL; |
| 5982 | LY_ERR rc; |
| 5983 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5984 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5985 | return LY_SUCCESS; |
| 5986 | } |
| 5987 | |
| 5988 | if (set->type != LYXP_SET_NODE_SET) { |
| 5989 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 5990 | return LY_EVALID; |
| 5991 | } |
| 5992 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5993 | /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory, |
| 5994 | * but it likely won't be used much, so it's a waste of time */ |
| 5995 | /* copy the context */ |
| 5996 | set_all_desc = set_copy(set); |
| 5997 | /* get all descendant nodes (the original context nodes are removed) */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5998 | rc = moveto_node_alldesc(set_all_desc, NULL, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5999 | if (rc != LY_SUCCESS) { |
| 6000 | lyxp_set_free(set_all_desc); |
| 6001 | return rc; |
| 6002 | } |
| 6003 | /* prepend the original context nodes */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6004 | rc = moveto_union(set, set_all_desc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6005 | if (rc != LY_SUCCESS) { |
| 6006 | lyxp_set_free(set_all_desc); |
| 6007 | return rc; |
| 6008 | } |
| 6009 | lyxp_set_free(set_all_desc); |
| 6010 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6011 | for (i = 0; i < set->used; ) { |
| 6012 | replaced = 0; |
| 6013 | |
| 6014 | /* only attributes of an elem can be in the result, skip all the rest, |
| 6015 | * we have all attributes qualified in lyd tree */ |
| 6016 | if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6017 | for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6018 | /* check "namespace" */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6019 | if (mod && (sub->annotation->module != mod)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6020 | continue; |
| 6021 | } |
| 6022 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6023 | if (!ncname || (sub->name == ncname)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6024 | /* match */ |
| 6025 | if (!replaced) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6026 | set->val.meta[i].meta = sub; |
| 6027 | set->val.meta[i].type = LYXP_NODE_META; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6028 | /* pos does not change */ |
| 6029 | replaced = 1; |
| 6030 | } else { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6031 | 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] | 6032 | } |
| 6033 | ++i; |
| 6034 | } |
| 6035 | } |
| 6036 | } |
| 6037 | |
| 6038 | if (!replaced) { |
| 6039 | /* no match */ |
| 6040 | set_remove_node(set, i); |
| 6041 | } |
| 6042 | } |
| 6043 | |
| 6044 | return LY_SUCCESS; |
| 6045 | } |
| 6046 | |
| 6047 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6048 | * @brief Move context @p set to self and al chilren, recursively. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET. |
| 6049 | * Context position aware. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6050 | * |
| 6051 | * @param[in] parent Current parent. |
| 6052 | * @param[in] parent_pos Position of @p parent. |
| 6053 | * @param[in] parent_type Node type of @p parent. |
| 6054 | * @param[in,out] to_set Set to use. |
| 6055 | * @param[in] dup_check_set Set for checking duplicities. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6056 | * @param[in] options XPath options. |
| 6057 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6058 | */ |
| 6059 | static LY_ERR |
| 6060 | moveto_self_add_children_r(const struct lyd_node *parent, uint32_t parent_pos, enum lyxp_node_type parent_type, |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6061 | struct lyxp_set *to_set, const struct lyxp_set *dup_check_set, int options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6062 | { |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6063 | const struct lyd_node *iter, *first; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6064 | LY_ERR rc; |
| 6065 | |
| 6066 | switch (parent_type) { |
| 6067 | case LYXP_NODE_ROOT: |
| 6068 | case LYXP_NODE_ROOT_CONFIG: |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6069 | assert(!parent); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6070 | |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6071 | /* add all top-level nodes as elements */ |
| 6072 | first = to_set->tree; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6073 | break; |
| 6074 | case LYXP_NODE_ELEM: |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6075 | /* add just the text node of this term element node */ |
| 6076 | if (parent->schema->nodetype & (LYD_NODE_TERM | LYD_NODE_ANY)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6077 | if (!set_dup_node_check(dup_check_set, parent, LYXP_NODE_TEXT, -1)) { |
| 6078 | set_insert_node(to_set, parent, parent_pos, LYXP_NODE_TEXT, to_set->used); |
| 6079 | } |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6080 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6081 | } |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6082 | |
| 6083 | /* add all the children of this node */ |
Michal Vasko | 5bfd4be | 2020-06-23 13:26:19 +0200 | [diff] [blame] | 6084 | first = lyd_node_children(parent, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6085 | break; |
| 6086 | default: |
| 6087 | LOGINT_RET(parent->schema->module->ctx); |
| 6088 | } |
| 6089 | |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6090 | /* add all top-level nodes as elements */ |
| 6091 | LY_LIST_FOR(first, iter) { |
| 6092 | /* context check */ |
| 6093 | if ((parent_type == LYXP_NODE_ROOT_CONFIG) && (iter->schema->flags & LYS_CONFIG_R)) { |
| 6094 | continue; |
| 6095 | } |
| 6096 | |
| 6097 | /* when check */ |
| 6098 | if (moveto_when_check(iter)) { |
| 6099 | return LY_EINCOMPLETE; |
| 6100 | } |
| 6101 | |
| 6102 | if (!set_dup_node_check(dup_check_set, iter, LYXP_NODE_ELEM, -1)) { |
| 6103 | set_insert_node(to_set, iter, 0, LYXP_NODE_ELEM, to_set->used); |
| 6104 | |
| 6105 | /* also add all the children of this node, recursively */ |
| 6106 | rc = moveto_self_add_children_r(iter, 0, LYXP_NODE_ELEM, to_set, dup_check_set, options); |
| 6107 | LY_CHECK_RET(rc); |
| 6108 | } |
| 6109 | } |
| 6110 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6111 | return LY_SUCCESS; |
| 6112 | } |
| 6113 | |
| 6114 | /** |
| 6115 | * @brief Move context @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET |
| 6116 | * (or LYXP_SET_EMPTY). Context position aware. |
| 6117 | * |
| 6118 | * @param[in,out] set Set to use. |
| 6119 | * @param[in] all_desc Whether to go to all descendants ('//') or not ('/'). |
| 6120 | * @param[in] options XPath options. |
| 6121 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6122 | */ |
| 6123 | static LY_ERR |
| 6124 | moveto_self(struct lyxp_set *set, int all_desc, int options) |
| 6125 | { |
| 6126 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6127 | struct lyxp_set ret_set; |
| 6128 | LY_ERR rc; |
| 6129 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6130 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6131 | return LY_SUCCESS; |
| 6132 | } |
| 6133 | |
| 6134 | if (set->type != LYXP_SET_NODE_SET) { |
| 6135 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 6136 | return LY_EVALID; |
| 6137 | } |
| 6138 | |
| 6139 | /* nothing to do */ |
| 6140 | if (!all_desc) { |
| 6141 | return LY_SUCCESS; |
| 6142 | } |
| 6143 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6144 | /* add all the children, they get added recursively */ |
| 6145 | set_init(&ret_set, set); |
| 6146 | for (i = 0; i < set->used; ++i) { |
| 6147 | /* copy the current node to tmp */ |
| 6148 | set_insert_node(&ret_set, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, ret_set.used); |
| 6149 | |
| 6150 | /* do not touch attributes and text nodes */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6151 | 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] | 6152 | continue; |
| 6153 | } |
| 6154 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6155 | /* add all the children */ |
| 6156 | 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] | 6157 | set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6158 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6159 | lyxp_set_free_content(&ret_set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6160 | return rc; |
| 6161 | } |
| 6162 | } |
| 6163 | |
| 6164 | /* use the temporary set as the current one */ |
| 6165 | ret_set.ctx_pos = set->ctx_pos; |
| 6166 | ret_set.ctx_size = set->ctx_size; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6167 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6168 | memcpy(set, &ret_set, sizeof *set); |
| 6169 | |
| 6170 | return LY_SUCCESS; |
| 6171 | } |
| 6172 | |
| 6173 | /** |
| 6174 | * @brief Move context schema @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_SCNODE_SET |
| 6175 | * (or LYXP_SET_EMPTY). |
| 6176 | * |
| 6177 | * @param[in,out] set Set to use. |
| 6178 | * @param[in] all_desc Whether to go to all descendants ('//') or not ('/'). |
| 6179 | * @param[in] options XPath options. |
| 6180 | * @return LY_ERR |
| 6181 | */ |
| 6182 | static LY_ERR |
| 6183 | moveto_scnode_self(struct lyxp_set *set, int all_desc, int options) |
| 6184 | { |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6185 | int getnext_opts; |
| 6186 | uint32_t i, mod_idx; |
| 6187 | const struct lysc_node *iter, *start_parent; |
| 6188 | const struct lys_module *mod; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6189 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6190 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6191 | return LY_SUCCESS; |
| 6192 | } |
| 6193 | |
| 6194 | if (set->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 6195 | 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] | 6196 | return LY_EVALID; |
| 6197 | } |
| 6198 | |
| 6199 | /* nothing to do */ |
| 6200 | if (!all_desc) { |
| 6201 | return LY_SUCCESS; |
| 6202 | } |
| 6203 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6204 | /* getnext opts */ |
| 6205 | getnext_opts = LYS_GETNEXT_NOSTATECHECK; |
| 6206 | if (options & LYXP_SCNODE_OUTPUT) { |
| 6207 | getnext_opts |= LYS_GETNEXT_OUTPUT; |
| 6208 | } |
| 6209 | |
| 6210 | /* add all the children, recursively as they are being added into the same set */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6211 | for (i = 0; i < set->used; ++i) { |
| 6212 | if (set->val.scnodes[i].in_ctx != 1) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6213 | if (set->val.scnodes[i].in_ctx != -2) { |
| 6214 | continue; |
| 6215 | } |
| 6216 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6217 | /* remember context node */ |
| 6218 | set->val.scnodes[i].in_ctx = -1; |
| 6219 | } else { |
| 6220 | set->val.scnodes[i].in_ctx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6221 | } |
| 6222 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6223 | start_parent = set->val.scnodes[i].scnode; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6224 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6225 | if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) { |
| 6226 | /* it can actually be in any module, it's all <running> */ |
| 6227 | mod_idx = 0; |
| 6228 | while ((mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) { |
| 6229 | iter = NULL; |
| 6230 | /* module may not be implemented */ |
| 6231 | while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) { |
| 6232 | /* context check */ |
| 6233 | if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) { |
| 6234 | continue; |
| 6235 | } |
| 6236 | |
| 6237 | lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM); |
| 6238 | /* throw away the insert index, we want to consider that node again, recursively */ |
| 6239 | } |
| 6240 | } |
| 6241 | |
| 6242 | } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) { |
| 6243 | iter = NULL; |
| 6244 | while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6245 | /* context check */ |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6246 | 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] | 6247 | continue; |
| 6248 | } |
| 6249 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6250 | lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6251 | } |
| 6252 | } |
| 6253 | } |
| 6254 | |
| 6255 | return LY_SUCCESS; |
| 6256 | } |
| 6257 | |
| 6258 | /** |
| 6259 | * @brief Move context @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_NODE_SET |
| 6260 | * (or LYXP_SET_EMPTY). Context position aware. |
| 6261 | * |
| 6262 | * @param[in] set Set to use. |
| 6263 | * @param[in] all_desc Whether to go to all descendants ('//') or not ('/'). |
| 6264 | * @param[in] options XPath options. |
| 6265 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6266 | */ |
| 6267 | static LY_ERR |
| 6268 | moveto_parent(struct lyxp_set *set, int all_desc, int options) |
| 6269 | { |
| 6270 | LY_ERR rc; |
| 6271 | uint32_t i; |
| 6272 | struct lyd_node *node, *new_node; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6273 | enum lyxp_node_type new_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6274 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6275 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6276 | return LY_SUCCESS; |
| 6277 | } |
| 6278 | |
| 6279 | if (set->type != LYXP_SET_NODE_SET) { |
| 6280 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 6281 | return LY_EVALID; |
| 6282 | } |
| 6283 | |
| 6284 | if (all_desc) { |
| 6285 | /* <path>//.. == <path>//./.. */ |
| 6286 | rc = moveto_self(set, 1, options); |
| 6287 | LY_CHECK_RET(rc); |
| 6288 | } |
| 6289 | |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 6290 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6291 | node = set->val.nodes[i].node; |
| 6292 | |
| 6293 | if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
| 6294 | new_node = (struct lyd_node *)node->parent; |
| 6295 | } else if (set->val.nodes[i].type == LYXP_NODE_TEXT) { |
| 6296 | new_node = node; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6297 | } else if (set->val.nodes[i].type == LYXP_NODE_META) { |
| 6298 | new_node = set->val.meta[i].meta->parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6299 | if (!new_node) { |
| 6300 | LOGINT_RET(set->ctx); |
| 6301 | } |
| 6302 | } else { |
| 6303 | /* root does not have a parent */ |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6304 | set_remove_node_none(set, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6305 | continue; |
| 6306 | } |
| 6307 | |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 6308 | /* when check */ |
| 6309 | if (moveto_when_check(new_node)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6310 | return LY_EINCOMPLETE; |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 6311 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6312 | |
| 6313 | /* node already there can also be the root */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6314 | if (!new_node) { |
| 6315 | new_type = set->root_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6316 | |
| 6317 | /* node has a standard parent (it can equal the root, it's not the root yet since they are fake) */ |
| 6318 | } else { |
| 6319 | new_type = LYXP_NODE_ELEM; |
| 6320 | } |
| 6321 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6322 | if (set_dup_node_check(set, new_node, new_type, -1)) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6323 | set_remove_node_none(set, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6324 | } else { |
| 6325 | set_replace_node(set, new_node, 0, new_type, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6326 | } |
| 6327 | } |
| 6328 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6329 | set_remove_nodes_none(set); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6330 | assert(!set_sort(set) && !set_sorted_dup_node_clean(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6331 | |
| 6332 | return LY_SUCCESS; |
| 6333 | } |
| 6334 | |
| 6335 | /** |
| 6336 | * @brief Move context schema @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_SCNODE_SET |
| 6337 | * (or LYXP_SET_EMPTY). |
| 6338 | * |
| 6339 | * @param[in] set Set to use. |
| 6340 | * @param[in] all_desc Whether to go to all descendants ('//') or not ('/'). |
| 6341 | * @param[in] options XPath options. |
| 6342 | * @return LY_ERR |
| 6343 | */ |
| 6344 | static LY_ERR |
| 6345 | moveto_scnode_parent(struct lyxp_set *set, int all_desc, int options) |
| 6346 | { |
| 6347 | int idx, i, orig_used, temp_ctx = 0; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6348 | const struct lysc_node *node, *new_node; |
| 6349 | enum lyxp_node_type new_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6350 | LY_ERR rc; |
| 6351 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6352 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6353 | return LY_SUCCESS; |
| 6354 | } |
| 6355 | |
| 6356 | if (set->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 6357 | 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] | 6358 | return LY_EVALID; |
| 6359 | } |
| 6360 | |
| 6361 | if (all_desc) { |
| 6362 | /* <path>//.. == <path>//./.. */ |
| 6363 | rc = moveto_scnode_self(set, 1, options); |
| 6364 | LY_CHECK_RET(rc); |
| 6365 | } |
| 6366 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6367 | orig_used = set->used; |
| 6368 | for (i = 0; i < orig_used; ++i) { |
| 6369 | if (set->val.scnodes[i].in_ctx != 1) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6370 | if (set->val.scnodes[i].in_ctx != -2) { |
| 6371 | continue; |
| 6372 | } |
| 6373 | |
| 6374 | /* remember context node */ |
| 6375 | set->val.scnodes[i].in_ctx = -1; |
Michal Vasko | ec4df48 | 2019-12-16 10:02:18 +0100 | [diff] [blame] | 6376 | } else { |
| 6377 | set->val.scnodes[i].in_ctx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6378 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6379 | |
| 6380 | node = set->val.scnodes[i].scnode; |
| 6381 | |
| 6382 | if (set->val.scnodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6383 | new_node = lysc_data_parent(node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6384 | } else { |
| 6385 | /* root does not have a parent */ |
| 6386 | continue; |
| 6387 | } |
| 6388 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6389 | /* node has no parent */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6390 | if (!new_node) { |
| 6391 | new_type = set->root_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6392 | |
| 6393 | /* node has a standard parent (it can equal the root, it's not the root yet since they are fake) */ |
| 6394 | } else { |
| 6395 | new_type = LYXP_NODE_ELEM; |
| 6396 | } |
| 6397 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6398 | idx = lyxp_set_scnode_insert_node(set, new_node, new_type); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6399 | if ((idx < orig_used) && (idx > i)) { |
| 6400 | set->val.scnodes[idx].in_ctx = 2; |
| 6401 | temp_ctx = 1; |
| 6402 | } |
| 6403 | } |
| 6404 | |
| 6405 | if (temp_ctx) { |
| 6406 | for (i = 0; i < orig_used; ++i) { |
| 6407 | if (set->val.scnodes[i].in_ctx == 2) { |
| 6408 | set->val.scnodes[i].in_ctx = 1; |
| 6409 | } |
| 6410 | } |
| 6411 | } |
| 6412 | |
| 6413 | return LY_SUCCESS; |
| 6414 | } |
| 6415 | |
| 6416 | /** |
| 6417 | * @brief Move context @p set to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'. |
| 6418 | * Result is LYXP_SET_BOOLEAN. Indirectly context position aware. |
| 6419 | * |
| 6420 | * @param[in,out] set1 Set to use for the result. |
| 6421 | * @param[in] set2 Set acting as the second operand for @p op. |
| 6422 | * @param[in] op Comparison operator to process. |
| 6423 | * @param[in] options XPath options. |
| 6424 | * @return LY_ERR |
| 6425 | */ |
| 6426 | static LY_ERR |
| 6427 | moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op, int options) |
| 6428 | { |
| 6429 | /* |
| 6430 | * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING |
| 6431 | * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING) |
| 6432 | * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 6433 | * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 6434 | * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING |
| 6435 | * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6436 | * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 6437 | * |
| 6438 | * '=' or '!=' |
| 6439 | * BOOLEAN + BOOLEAN |
| 6440 | * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 6441 | * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 6442 | * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 6443 | * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 6444 | * NUMBER + NUMBER |
| 6445 | * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6446 | * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 6447 | * STRING + STRING |
| 6448 | * |
| 6449 | * '<=', '<', '>=', '>' |
| 6450 | * NUMBER + NUMBER |
| 6451 | * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 6452 | * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 6453 | * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 6454 | * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6455 | * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 6456 | * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 6457 | * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6458 | * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6459 | */ |
| 6460 | struct lyxp_set iter1, iter2; |
| 6461 | int result; |
| 6462 | int64_t i; |
| 6463 | LY_ERR rc; |
| 6464 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6465 | memset(&iter1, 0, sizeof iter1); |
| 6466 | memset(&iter2, 0, sizeof iter2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6467 | |
| 6468 | /* iterative evaluation with node-sets */ |
| 6469 | if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) { |
| 6470 | if (set1->type == LYXP_SET_NODE_SET) { |
| 6471 | for (i = 0; i < set1->used; ++i) { |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6472 | /* cast set1 */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6473 | switch (set2->type) { |
| 6474 | case LYXP_SET_NUMBER: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6475 | rc = set_comp_cast(&iter1, set1, LYXP_SET_NUMBER, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6476 | break; |
| 6477 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6478 | rc = set_comp_cast(&iter1, set1, LYXP_SET_BOOLEAN, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6479 | break; |
| 6480 | default: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6481 | rc = set_comp_cast(&iter1, set1, LYXP_SET_STRING, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6482 | break; |
| 6483 | } |
| 6484 | LY_CHECK_RET(rc); |
| 6485 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6486 | /* canonize set2 */ |
| 6487 | LY_CHECK_ERR_RET(rc = set_comp_canonize(&iter2, set2, &set1->val.nodes[i]), lyxp_set_free_content(&iter1), rc); |
| 6488 | |
| 6489 | /* compare recursively */ |
| 6490 | rc = moveto_op_comp(&iter1, &iter2, op, options); |
| 6491 | lyxp_set_free_content(&iter2); |
| 6492 | LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6493 | |
| 6494 | /* lazy evaluation until true */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6495 | if (iter1.val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6496 | set_fill_boolean(set1, 1); |
| 6497 | return LY_SUCCESS; |
| 6498 | } |
| 6499 | } |
| 6500 | } else { |
| 6501 | for (i = 0; i < set2->used; ++i) { |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6502 | /* set set2 */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6503 | switch (set1->type) { |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6504 | case LYXP_SET_NUMBER: |
| 6505 | rc = set_comp_cast(&iter2, set2, LYXP_SET_NUMBER, i); |
| 6506 | break; |
| 6507 | case LYXP_SET_BOOLEAN: |
| 6508 | rc = set_comp_cast(&iter2, set2, LYXP_SET_BOOLEAN, i); |
| 6509 | break; |
| 6510 | default: |
| 6511 | rc = set_comp_cast(&iter2, set2, LYXP_SET_STRING, i); |
| 6512 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6513 | } |
| 6514 | LY_CHECK_RET(rc); |
| 6515 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6516 | /* canonize set1 */ |
| 6517 | 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] | 6518 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6519 | /* compare recursively */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6520 | rc = moveto_op_comp(&iter1, &iter2, op, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6521 | lyxp_set_free_content(&iter2); |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6522 | LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6523 | |
| 6524 | /* lazy evaluation until true */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6525 | if (iter1.val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6526 | set_fill_boolean(set1, 1); |
| 6527 | return LY_SUCCESS; |
| 6528 | } |
| 6529 | } |
| 6530 | } |
| 6531 | |
| 6532 | /* false for all nodes */ |
| 6533 | set_fill_boolean(set1, 0); |
| 6534 | return LY_SUCCESS; |
| 6535 | } |
| 6536 | |
| 6537 | /* first convert properly */ |
| 6538 | if ((op[0] == '=') || (op[0] == '!')) { |
| 6539 | if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6540 | lyxp_set_cast(set1, LYXP_SET_BOOLEAN); |
| 6541 | lyxp_set_cast(set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6542 | } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6543 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6544 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6545 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6546 | LY_CHECK_RET(rc); |
| 6547 | } /* else we have 2 strings */ |
| 6548 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6549 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6550 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6551 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6552 | LY_CHECK_RET(rc); |
| 6553 | } |
| 6554 | |
| 6555 | assert(set1->type == set2->type); |
| 6556 | |
| 6557 | /* compute result */ |
| 6558 | if (op[0] == '=') { |
| 6559 | if (set1->type == LYXP_SET_BOOLEAN) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6560 | result = (set1->val.bln == set2->val.bln); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6561 | } else if (set1->type == LYXP_SET_NUMBER) { |
| 6562 | result = (set1->val.num == set2->val.num); |
| 6563 | } else { |
| 6564 | assert(set1->type == LYXP_SET_STRING); |
Michal Vasko | ac6c72f | 2019-11-14 16:09:34 +0100 | [diff] [blame] | 6565 | result = !strcmp(set1->val.str, set2->val.str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6566 | } |
| 6567 | } else if (op[0] == '!') { |
| 6568 | if (set1->type == LYXP_SET_BOOLEAN) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6569 | result = (set1->val.bln != set2->val.bln); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6570 | } else if (set1->type == LYXP_SET_NUMBER) { |
| 6571 | result = (set1->val.num != set2->val.num); |
| 6572 | } else { |
| 6573 | assert(set1->type == LYXP_SET_STRING); |
Michal Vasko | ac6c72f | 2019-11-14 16:09:34 +0100 | [diff] [blame] | 6574 | result = !strcmp(set1->val.str, set2->val.str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6575 | } |
| 6576 | } else { |
| 6577 | assert(set1->type == LYXP_SET_NUMBER); |
| 6578 | if (op[0] == '<') { |
| 6579 | if (op[1] == '=') { |
| 6580 | result = (set1->val.num <= set2->val.num); |
| 6581 | } else { |
| 6582 | result = (set1->val.num < set2->val.num); |
| 6583 | } |
| 6584 | } else { |
| 6585 | if (op[1] == '=') { |
| 6586 | result = (set1->val.num >= set2->val.num); |
| 6587 | } else { |
| 6588 | result = (set1->val.num > set2->val.num); |
| 6589 | } |
| 6590 | } |
| 6591 | } |
| 6592 | |
| 6593 | /* assign result */ |
| 6594 | if (result) { |
| 6595 | set_fill_boolean(set1, 1); |
| 6596 | } else { |
| 6597 | set_fill_boolean(set1, 0); |
| 6598 | } |
| 6599 | |
| 6600 | return LY_SUCCESS; |
| 6601 | } |
| 6602 | |
| 6603 | /** |
| 6604 | * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div', |
| 6605 | * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware. |
| 6606 | * |
| 6607 | * @param[in,out] set1 Set to use for the result. |
| 6608 | * @param[in] set2 Set acting as the second operand for @p op. |
| 6609 | * @param[in] op Operator to process. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6610 | * @return LY_ERR |
| 6611 | */ |
| 6612 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6613 | 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] | 6614 | { |
| 6615 | LY_ERR rc; |
| 6616 | |
| 6617 | /* unary '-' */ |
| 6618 | if (!set2 && (op[0] == '-')) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6619 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6620 | LY_CHECK_RET(rc); |
| 6621 | set1->val.num *= -1; |
| 6622 | lyxp_set_free(set2); |
| 6623 | return LY_SUCCESS; |
| 6624 | } |
| 6625 | |
| 6626 | assert(set1 && set2); |
| 6627 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6628 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6629 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6630 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6631 | LY_CHECK_RET(rc); |
| 6632 | |
| 6633 | switch (op[0]) { |
| 6634 | /* '+' */ |
| 6635 | case '+': |
| 6636 | set1->val.num += set2->val.num; |
| 6637 | break; |
| 6638 | |
| 6639 | /* '-' */ |
| 6640 | case '-': |
| 6641 | set1->val.num -= set2->val.num; |
| 6642 | break; |
| 6643 | |
| 6644 | /* '*' */ |
| 6645 | case '*': |
| 6646 | set1->val.num *= set2->val.num; |
| 6647 | break; |
| 6648 | |
| 6649 | /* 'div' */ |
| 6650 | case 'd': |
| 6651 | set1->val.num /= set2->val.num; |
| 6652 | break; |
| 6653 | |
| 6654 | /* 'mod' */ |
| 6655 | case 'm': |
| 6656 | set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num); |
| 6657 | break; |
| 6658 | |
| 6659 | default: |
| 6660 | LOGINT_RET(set1->ctx); |
| 6661 | } |
| 6662 | |
| 6663 | return LY_SUCCESS; |
| 6664 | } |
| 6665 | |
| 6666 | /* |
| 6667 | * eval functions |
| 6668 | * |
| 6669 | * They execute a parsed XPath expression on some data subtree. |
| 6670 | */ |
| 6671 | |
| 6672 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6673 | * @brief Evaluate Predicate. Logs directly on error. |
| 6674 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6675 | * [9] Predicate ::= '[' Expr ']' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6676 | * |
| 6677 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6678 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6679 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 6680 | * @param[in] options XPath options. |
| 6681 | * @param[in] parent_pos_pred Whether parent predicate was a positional one. |
| 6682 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6683 | */ |
| 6684 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6685 | eval_predicate(struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, int options, int parent_pos_pred) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6686 | { |
| 6687 | LY_ERR rc; |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 6688 | uint16_t i, orig_exp; |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6689 | uint32_t orig_pos, orig_size; |
| 6690 | int32_t pred_in_ctx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6691 | struct lyxp_set set2; |
| 6692 | struct lyd_node *orig_parent; |
| 6693 | |
| 6694 | /* '[' */ |
| 6695 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6696 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 6697 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6698 | |
| 6699 | if (!set) { |
| 6700 | only_parse: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6701 | rc = eval_expr_select(exp, tok_idx, 0, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6702 | LY_CHECK_RET(rc); |
| 6703 | } else if (set->type == LYXP_SET_NODE_SET) { |
| 6704 | /* 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] | 6705 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6706 | |
| 6707 | /* empty set, nothing to evaluate */ |
| 6708 | if (!set->used) { |
| 6709 | goto only_parse; |
| 6710 | } |
| 6711 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6712 | orig_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6713 | orig_pos = 0; |
| 6714 | orig_size = set->used; |
| 6715 | orig_parent = NULL; |
Michal Vasko | 39dbf35 | 2020-05-21 10:08:59 +0200 | [diff] [blame] | 6716 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6717 | set_init(&set2, set); |
| 6718 | set_insert_node(&set2, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, 0); |
| 6719 | /* remember the node context position for position() and context size for last(), |
| 6720 | * predicates should always be evaluated with respect to the child axis (since we do |
| 6721 | * not support explicit axes) so we assign positions based on their parents */ |
| 6722 | if (parent_pos_pred && ((struct lyd_node *)set->val.nodes[i].node->parent != orig_parent)) { |
| 6723 | orig_parent = (struct lyd_node *)set->val.nodes[i].node->parent; |
| 6724 | orig_pos = 1; |
| 6725 | } else { |
| 6726 | ++orig_pos; |
| 6727 | } |
| 6728 | |
| 6729 | set2.ctx_pos = orig_pos; |
| 6730 | set2.ctx_size = orig_size; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6731 | *tok_idx = orig_exp; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6732 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6733 | rc = eval_expr_select(exp, tok_idx, 0, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6734 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6735 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6736 | return rc; |
| 6737 | } |
| 6738 | |
| 6739 | /* number is a position */ |
| 6740 | if (set2.type == LYXP_SET_NUMBER) { |
| 6741 | if ((long long)set2.val.num == orig_pos) { |
| 6742 | set2.val.num = 1; |
| 6743 | } else { |
| 6744 | set2.val.num = 0; |
| 6745 | } |
| 6746 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6747 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6748 | |
| 6749 | /* predicate satisfied or not? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6750 | if (!set2.val.bln) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6751 | set_remove_node_none(set, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6752 | } |
| 6753 | } |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6754 | set_remove_nodes_none(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6755 | |
| 6756 | } else if (set->type == LYXP_SET_SCNODE_SET) { |
| 6757 | for (i = 0; i < set->used; ++i) { |
| 6758 | if (set->val.scnodes[i].in_ctx == 1) { |
| 6759 | /* there is a currently-valid node */ |
| 6760 | break; |
| 6761 | } |
| 6762 | } |
| 6763 | /* empty set, nothing to evaluate */ |
| 6764 | if (i == set->used) { |
| 6765 | goto only_parse; |
| 6766 | } |
| 6767 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6768 | orig_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6769 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6770 | /* set special in_ctx to all the valid snodes */ |
| 6771 | pred_in_ctx = set_scnode_new_in_ctx(set); |
| 6772 | |
| 6773 | /* use the valid snodes one-by-one */ |
| 6774 | for (i = 0; i < set->used; ++i) { |
| 6775 | if (set->val.scnodes[i].in_ctx != pred_in_ctx) { |
| 6776 | continue; |
| 6777 | } |
| 6778 | set->val.scnodes[i].in_ctx = 1; |
| 6779 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6780 | *tok_idx = orig_exp; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6781 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6782 | rc = eval_expr_select(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6783 | LY_CHECK_RET(rc); |
| 6784 | |
| 6785 | set->val.scnodes[i].in_ctx = pred_in_ctx; |
| 6786 | } |
| 6787 | |
| 6788 | /* restore the state as it was before the predicate */ |
| 6789 | for (i = 0; i < set->used; ++i) { |
| 6790 | if (set->val.scnodes[i].in_ctx == 1) { |
| 6791 | set->val.scnodes[i].in_ctx = 0; |
| 6792 | } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) { |
| 6793 | set->val.scnodes[i].in_ctx = 1; |
| 6794 | } |
| 6795 | } |
| 6796 | |
| 6797 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6798 | set2.type = LYXP_SET_NODE_SET; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6799 | set_fill_set(&set2, set); |
| 6800 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6801 | rc = eval_expr_select(exp, tok_idx, 0, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6802 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6803 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6804 | return rc; |
| 6805 | } |
| 6806 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6807 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6808 | if (!set2.val.bln) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6809 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6810 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6811 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6812 | } |
| 6813 | |
| 6814 | /* ']' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6815 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6816 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6817 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 6818 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6819 | |
| 6820 | return LY_SUCCESS; |
| 6821 | } |
| 6822 | |
| 6823 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6824 | * @brief Evaluate Literal. Logs directly on error. |
| 6825 | * |
| 6826 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6827 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6828 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 6829 | */ |
| 6830 | static void |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6831 | 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] | 6832 | { |
| 6833 | if (set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6834 | if (exp->tok_len[*tok_idx] == 2) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6835 | set_fill_string(set, "", 0); |
| 6836 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6837 | 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] | 6838 | } |
| 6839 | } |
| 6840 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6841 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 6842 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6843 | } |
| 6844 | |
| 6845 | /** |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6846 | * @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] | 6847 | * |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6848 | * @param[in] exp Full parsed XPath expression. |
| 6849 | * @param[in,out] tok_idx Index in @p exp at the beginning of the predicate, is updated on success. |
| 6850 | * @param[in] scnode Found schema node as context for the predicate. |
| 6851 | * @param[in] format Format of any prefixes in key names/values. |
| 6852 | * @param[out] predicates Parsed predicates. |
| 6853 | * @param[out] pred_type Type of @p predicates. |
| 6854 | * @return LY_SUCCESS on success, |
| 6855 | * @return LY_ERR on any error. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6856 | */ |
| 6857 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6858 | eval_name_test_try_compile_predicates(struct lyxp_expr *exp, uint16_t *tok_idx, const struct lysc_node *scnode, |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 6859 | LY_PREFIX_FORMAT format, struct ly_path_predicate **predicates, |
| 6860 | enum ly_path_pred_type *pred_type) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6861 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6862 | LY_ERR ret = LY_SUCCESS; |
| 6863 | uint16_t key_count, e_idx, pred_idx = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6864 | const struct lysc_node *key; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6865 | size_t pred_len; |
| 6866 | int prev_lo; |
| 6867 | struct lyxp_expr *exp2 = NULL; |
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 | assert(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6870 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6871 | if (scnode->nodetype == LYS_LIST) { |
| 6872 | /* get key count */ |
| 6873 | if (scnode->flags & LYS_KEYLESS) { |
| 6874 | return LY_EINVAL; |
| 6875 | } |
| 6876 | for (key_count = 0, key = lysc_node_children(scnode, 0); key && (key->flags & LYS_KEY); key = key->next, ++key_count); |
| 6877 | assert(key_count); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6878 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6879 | /* learn where the predicates end */ |
| 6880 | e_idx = *tok_idx; |
| 6881 | while (key_count) { |
| 6882 | /* '[' */ |
| 6883 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) { |
| 6884 | return LY_EINVAL; |
| 6885 | } |
| 6886 | ++e_idx; |
| 6887 | |
| 6888 | /* ']' */ |
| 6889 | while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) { |
| 6890 | ++e_idx; |
| 6891 | } |
| 6892 | ++e_idx; |
| 6893 | |
| 6894 | /* another presumably key predicate parsed */ |
| 6895 | --key_count; |
| 6896 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6897 | } else { |
| 6898 | /* learn just where this single predicate ends */ |
| 6899 | e_idx = *tok_idx; |
| 6900 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6901 | /* '[' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6902 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) { |
| 6903 | return LY_EINVAL; |
| 6904 | } |
| 6905 | ++e_idx; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6906 | |
| 6907 | /* ']' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6908 | while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) { |
| 6909 | ++e_idx; |
| 6910 | } |
| 6911 | ++e_idx; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6912 | } |
| 6913 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6914 | /* get the joined length of all the keys predicates/of the single leaf-list predicate */ |
| 6915 | pred_len = (exp->tok_pos[e_idx - 1] + exp->tok_len[e_idx - 1]) - exp->tok_pos[*tok_idx]; |
| 6916 | |
| 6917 | /* turn logging off */ |
| 6918 | prev_lo = ly_log_options(0); |
| 6919 | |
| 6920 | /* parse the predicate(s) */ |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 6921 | 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] | 6922 | LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp2), cleanup); |
| 6923 | |
| 6924 | /* compile */ |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 6925 | ret = ly_path_compile_predicate(scnode->module->ctx, scnode, scnode->module, scnode, exp2, &pred_idx, |
| 6926 | format, scnode->module, predicates, pred_type); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6927 | LY_CHECK_GOTO(ret, cleanup); |
| 6928 | |
| 6929 | /* success, the predicate must include all the needed information for hash-based search */ |
| 6930 | *tok_idx = e_idx; |
| 6931 | |
| 6932 | cleanup: |
| 6933 | ly_log_options(prev_lo); |
| 6934 | lyxp_expr_free(scnode->module->ctx, exp2); |
| 6935 | return ret; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6936 | } |
| 6937 | |
| 6938 | /** |
| 6939 | * @brief Evaluate NameTest and any following Predicates. Logs directly on error. |
| 6940 | * |
| 6941 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 6942 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 6943 | * [7] NameTest ::= '*' | NCName ':' '*' | QName |
| 6944 | * |
| 6945 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6946 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6947 | * @param[in] attr_axis Whether to search attributes or standard nodes. |
| 6948 | * @param[in] all_desc Whether to search all the descendants or children only. |
| 6949 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 6950 | * @param[in] options XPath options. |
| 6951 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6952 | */ |
| 6953 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6954 | eval_name_test_with_predicate(struct lyxp_expr *exp, uint16_t *tok_idx, int attr_axis, int all_desc, struct lyxp_set *set, |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6955 | int options) |
| 6956 | { |
| 6957 | int i; |
| 6958 | char *path; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6959 | const char *ncname, *ncname_dict = NULL; |
| 6960 | uint16_t ncname_len; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6961 | const struct lys_module *moveto_mod; |
| 6962 | const struct lysc_node *scnode = NULL, *tmp; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6963 | struct ly_path_predicate *predicates = NULL; |
| 6964 | enum ly_path_pred_type pred_type = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6965 | LY_ERR rc = LY_SUCCESS; |
| 6966 | |
| 6967 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6968 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 6969 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6970 | |
| 6971 | if (!set) { |
| 6972 | goto moveto; |
| 6973 | } |
| 6974 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6975 | ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]]; |
| 6976 | ncname_len = exp->tok_len[*tok_idx - 1]; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6977 | |
| 6978 | /* parse (and skip) module name */ |
| 6979 | rc = moveto_resolve_model(&ncname, &ncname_len, set, &moveto_mod); |
| 6980 | LY_CHECK_GOTO(rc, cleanup); |
| 6981 | |
| 6982 | if (moveto_mod && !attr_axis && !all_desc && (set->type == LYXP_SET_NODE_SET)) { |
| 6983 | /* find the matching schema node in some parent in the context */ |
| 6984 | for (i = 0; i < (signed)set->used; ++i) { |
| 6985 | if (set->val.nodes[i].type == set->root_type) { |
| 6986 | tmp = lys_find_child(NULL, moveto_mod, ncname, ncname_len, 0, 0); |
| 6987 | } else if ((set->val.nodes[i].type == LYXP_NODE_ELEM) |
| 6988 | && (!scnode || (lysc_data_parent(scnode) != set->val.nodes[i].node->schema))) { |
| 6989 | /* do not repeat the same search */ |
| 6990 | 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] | 6991 | } else { |
| 6992 | tmp = NULL; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6993 | } |
| 6994 | |
| 6995 | /* additional context check */ |
| 6996 | if (tmp && (set->root_type == LYXP_NODE_ROOT_CONFIG) && (tmp->flags & LYS_CONFIG_R)) { |
| 6997 | tmp = NULL; |
| 6998 | } |
| 6999 | |
| 7000 | if (tmp) { |
| 7001 | if (scnode) { |
| 7002 | /* we found a schema node with the same name but at different level, give up, too complicated */ |
| 7003 | scnode = NULL; |
| 7004 | break; |
| 7005 | } else { |
| 7006 | /* remember the found schema node and continue to make sure it can be used */ |
| 7007 | scnode = tmp; |
| 7008 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7009 | } |
| 7010 | } |
| 7011 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7012 | if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) { |
| 7013 | /* try to create the predicates */ |
| 7014 | if (eval_name_test_try_compile_predicates(exp, tok_idx, scnode, set->format, &predicates, &pred_type)) { |
| 7015 | /* hashes cannot be used */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7016 | scnode = NULL; |
| 7017 | } |
| 7018 | } |
| 7019 | } |
| 7020 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7021 | if (!scnode && moveto_mod) { |
| 7022 | /* we are not able to match based on a schema node and not all the modules match, |
| 7023 | * use dictionary for efficient comparison */ |
| 7024 | ncname_dict = lydict_insert(set->ctx, ncname, ncname_len); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7025 | } |
| 7026 | |
| 7027 | moveto: |
| 7028 | /* move to the attribute(s), data node(s), or schema node(s) */ |
| 7029 | if (attr_axis) { |
| 7030 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 7031 | set_scnode_clear_ctx(set); |
| 7032 | } else { |
| 7033 | if (all_desc) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7034 | rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict); |
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_attr(set, moveto_mod, ncname_dict); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7037 | } |
| 7038 | LY_CHECK_GOTO(rc, cleanup); |
| 7039 | } |
| 7040 | } else { |
| 7041 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 7042 | if (all_desc) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7043 | rc = moveto_scnode_alldesc(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7044 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7045 | rc = moveto_scnode(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7046 | } |
| 7047 | LY_CHECK_GOTO(rc, cleanup); |
| 7048 | |
| 7049 | for (i = set->used - 1; i > -1; --i) { |
| 7050 | if (set->val.scnodes[i].in_ctx > 0) { |
| 7051 | break; |
| 7052 | } |
| 7053 | } |
| 7054 | if (i == -1) { |
| 7055 | path = lysc_path(set->ctx_scnode, LYSC_PATH_LOG, NULL, 0); |
| 7056 | 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] | 7057 | ncname_len, ncname, ncname - exp->expr, exp->expr, path); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7058 | free(path); |
| 7059 | } |
| 7060 | } else { |
| 7061 | if (all_desc) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7062 | rc = moveto_node_alldesc(set, moveto_mod, ncname_dict); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7063 | } else { |
| 7064 | if (scnode) { |
| 7065 | /* we can find the nodes using hashes */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7066 | rc = moveto_node_hash(set, scnode, predicates); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7067 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7068 | rc = moveto_node(set, moveto_mod, ncname_dict); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7069 | } |
| 7070 | } |
| 7071 | LY_CHECK_GOTO(rc, cleanup); |
| 7072 | } |
| 7073 | } |
| 7074 | |
| 7075 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7076 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 7077 | rc = eval_predicate(exp, tok_idx, set, options, 1); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7078 | LY_CHECK_RET(rc); |
| 7079 | } |
| 7080 | |
| 7081 | cleanup: |
Michal Vasko | db51a8d | 2020-05-27 15:22:29 +0200 | [diff] [blame] | 7082 | if (set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7083 | lydict_remove(set->ctx, ncname_dict); |
| 7084 | ly_path_predicates_free(set->ctx, pred_type, scnode, predicates); |
Michal Vasko | db51a8d | 2020-05-27 15:22:29 +0200 | [diff] [blame] | 7085 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7086 | return rc; |
| 7087 | } |
| 7088 | |
| 7089 | /** |
| 7090 | * @brief Evaluate NodeType and any following Predicates. Logs directly on error. |
| 7091 | * |
| 7092 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 7093 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 7094 | * [8] NodeType ::= 'text' | 'node' |
| 7095 | * |
| 7096 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7097 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7098 | * @param[in] attr_axis Whether to search attributes or standard nodes. |
| 7099 | * @param[in] all_desc Whether to search all the descendants or children only. |
| 7100 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7101 | * @param[in] options XPath options. |
| 7102 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7103 | */ |
| 7104 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7105 | eval_node_type_with_predicate(struct lyxp_expr *exp, uint16_t *tok_idx, int attr_axis, int all_desc, |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7106 | struct lyxp_set *set, int options) |
| 7107 | { |
| 7108 | LY_ERR rc; |
| 7109 | |
| 7110 | /* TODO */ |
| 7111 | (void)attr_axis; |
| 7112 | (void)all_desc; |
| 7113 | |
| 7114 | if (set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7115 | assert(exp->tok_len[*tok_idx] == 4); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7116 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 7117 | set_scnode_clear_ctx(set); |
| 7118 | /* just for the debug message below */ |
| 7119 | set = NULL; |
| 7120 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7121 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7122 | rc = xpath_node(NULL, 0, set, options); |
| 7123 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7124 | assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4)); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7125 | rc = xpath_text(NULL, 0, set, options); |
| 7126 | } |
| 7127 | LY_CHECK_RET(rc); |
| 7128 | } |
| 7129 | } |
| 7130 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7131 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7132 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7133 | |
| 7134 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7135 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7136 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7137 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7138 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7139 | |
| 7140 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7141 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7142 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7143 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7144 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7145 | |
| 7146 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7147 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 7148 | rc = eval_predicate(exp, tok_idx, set, options, 1); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7149 | LY_CHECK_RET(rc); |
| 7150 | } |
| 7151 | |
| 7152 | return LY_SUCCESS; |
| 7153 | } |
| 7154 | |
| 7155 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7156 | * @brief Evaluate RelativeLocationPath. Logs directly on error. |
| 7157 | * |
| 7158 | * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step |
| 7159 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7160 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7161 | * |
| 7162 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7163 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7164 | * @param[in] all_desc Whether to search all the descendants or children only. |
| 7165 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7166 | * @param[in] options XPath options. |
| 7167 | * @return LY_ERR (YL_EINCOMPLETE on unresolved when) |
| 7168 | */ |
| 7169 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7170 | eval_relative_location_path(struct lyxp_expr *exp, uint16_t *tok_idx, int all_desc, struct lyxp_set *set, int options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7171 | { |
| 7172 | int attr_axis; |
| 7173 | LY_ERR rc; |
| 7174 | |
| 7175 | goto step; |
| 7176 | do { |
| 7177 | /* evaluate '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7178 | if (exp->tok_len[*tok_idx] == 1) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7179 | all_desc = 0; |
| 7180 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7181 | assert(exp->tok_len[*tok_idx] == 2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7182 | all_desc = 1; |
| 7183 | } |
| 7184 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7185 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7186 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7187 | |
| 7188 | step: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7189 | /* evaluate abbreviated axis '@'? if any */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7190 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7191 | attr_axis = 1; |
| 7192 | |
| 7193 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7194 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7195 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7196 | } else { |
| 7197 | attr_axis = 0; |
| 7198 | } |
| 7199 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7200 | /* Step */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7201 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7202 | case LYXP_TOKEN_DOT: |
| 7203 | /* evaluate '.' */ |
| 7204 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 7205 | rc = moveto_scnode_self(set, all_desc, options); |
| 7206 | } else { |
| 7207 | rc = moveto_self(set, all_desc, options); |
| 7208 | } |
| 7209 | LY_CHECK_RET(rc); |
| 7210 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7211 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7212 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7213 | break; |
| 7214 | |
| 7215 | case LYXP_TOKEN_DDOT: |
| 7216 | /* evaluate '..' */ |
| 7217 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 7218 | rc = moveto_scnode_parent(set, all_desc, options); |
| 7219 | } else { |
| 7220 | rc = moveto_parent(set, all_desc, options); |
| 7221 | } |
| 7222 | LY_CHECK_RET(rc); |
| 7223 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7224 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7225 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7226 | break; |
| 7227 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7228 | case LYXP_TOKEN_NAMETEST: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7229 | /* evaluate NameTest Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7230 | 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] | 7231 | LY_CHECK_RET(rc); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7232 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7233 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7234 | case LYXP_TOKEN_NODETYPE: |
| 7235 | /* evaluate NodeType Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7236 | 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] | 7237 | LY_CHECK_RET(rc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7238 | break; |
| 7239 | |
| 7240 | default: |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 7241 | LOGINT_RET(set ? set->ctx : NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7242 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7243 | } 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] | 7244 | |
| 7245 | return LY_SUCCESS; |
| 7246 | } |
| 7247 | |
| 7248 | /** |
| 7249 | * @brief Evaluate AbsoluteLocationPath. Logs directly on error. |
| 7250 | * |
| 7251 | * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath |
| 7252 | * |
| 7253 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7254 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7255 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7256 | * @param[in] options XPath options. |
| 7257 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7258 | */ |
| 7259 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7260 | eval_absolute_location_path(struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, int options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7261 | { |
| 7262 | int all_desc; |
| 7263 | LY_ERR rc; |
| 7264 | |
| 7265 | if (set) { |
| 7266 | /* no matter what tokens follow, we need to be at the root */ |
| 7267 | moveto_root(set, options); |
| 7268 | } |
| 7269 | |
| 7270 | /* '/' RelativeLocationPath? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7271 | if (exp->tok_len[*tok_idx] == 1) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7272 | /* evaluate '/' - deferred */ |
| 7273 | all_desc = 0; |
| 7274 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7275 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7276 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7277 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7278 | if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7279 | return LY_SUCCESS; |
| 7280 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7281 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7282 | case LYXP_TOKEN_DOT: |
| 7283 | case LYXP_TOKEN_DDOT: |
| 7284 | case LYXP_TOKEN_AT: |
| 7285 | case LYXP_TOKEN_NAMETEST: |
| 7286 | case LYXP_TOKEN_NODETYPE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7287 | rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7288 | LY_CHECK_RET(rc); |
| 7289 | break; |
| 7290 | default: |
| 7291 | break; |
| 7292 | } |
| 7293 | |
| 7294 | /* '//' RelativeLocationPath */ |
| 7295 | } else { |
| 7296 | /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */ |
| 7297 | all_desc = 1; |
| 7298 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7299 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7300 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7301 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7302 | rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7303 | LY_CHECK_RET(rc); |
| 7304 | } |
| 7305 | |
| 7306 | return LY_SUCCESS; |
| 7307 | } |
| 7308 | |
| 7309 | /** |
| 7310 | * @brief Evaluate FunctionCall. Logs directly on error. |
| 7311 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7312 | * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7313 | * |
| 7314 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7315 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7316 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7317 | * @param[in] options XPath options. |
| 7318 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7319 | */ |
| 7320 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7321 | eval_function_call(struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, int options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7322 | { |
| 7323 | LY_ERR rc; |
| 7324 | LY_ERR (*xpath_func)(struct lyxp_set **, uint16_t, struct lyxp_set *, int) = NULL; |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 7325 | uint16_t arg_count = 0, i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7326 | struct lyxp_set **args = NULL, **args_aux; |
| 7327 | |
| 7328 | if (set) { |
| 7329 | /* FunctionName */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7330 | switch (exp->tok_len[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7331 | case 3: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7332 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7333 | xpath_func = &xpath_not; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7334 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7335 | xpath_func = &xpath_sum; |
| 7336 | } |
| 7337 | break; |
| 7338 | case 4: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7339 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7340 | xpath_func = &xpath_lang; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7341 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7342 | xpath_func = &xpath_last; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7343 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7344 | xpath_func = &xpath_name; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7345 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7346 | xpath_func = &xpath_true; |
| 7347 | } |
| 7348 | break; |
| 7349 | case 5: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7350 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7351 | xpath_func = &xpath_count; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7352 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7353 | xpath_func = &xpath_false; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7354 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7355 | xpath_func = &xpath_floor; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7356 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7357 | xpath_func = &xpath_round; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7358 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7359 | xpath_func = &xpath_deref; |
| 7360 | } |
| 7361 | break; |
| 7362 | case 6: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7363 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7364 | xpath_func = &xpath_concat; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7365 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7366 | xpath_func = &xpath_number; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7367 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7368 | xpath_func = &xpath_string; |
| 7369 | } |
| 7370 | break; |
| 7371 | case 7: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7372 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7373 | xpath_func = &xpath_boolean; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7374 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7375 | xpath_func = &xpath_ceiling; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7376 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7377 | xpath_func = &xpath_current; |
| 7378 | } |
| 7379 | break; |
| 7380 | case 8: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7381 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7382 | xpath_func = &xpath_contains; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7383 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7384 | xpath_func = &xpath_position; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7385 | } 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] | 7386 | xpath_func = &xpath_re_match; |
| 7387 | } |
| 7388 | break; |
| 7389 | case 9: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7390 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7391 | xpath_func = &xpath_substring; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7392 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7393 | xpath_func = &xpath_translate; |
| 7394 | } |
| 7395 | break; |
| 7396 | case 10: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7397 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7398 | xpath_func = &xpath_local_name; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7399 | } 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] | 7400 | xpath_func = &xpath_enum_value; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7401 | } 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] | 7402 | xpath_func = &xpath_bit_is_set; |
| 7403 | } |
| 7404 | break; |
| 7405 | case 11: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7406 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7407 | xpath_func = &xpath_starts_with; |
| 7408 | } |
| 7409 | break; |
| 7410 | case 12: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7411 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7412 | xpath_func = &xpath_derived_from; |
| 7413 | } |
| 7414 | break; |
| 7415 | case 13: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7416 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7417 | xpath_func = &xpath_namespace_uri; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7418 | } 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] | 7419 | xpath_func = &xpath_string_length; |
| 7420 | } |
| 7421 | break; |
| 7422 | case 15: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7423 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7424 | xpath_func = &xpath_normalize_space; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7425 | } 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] | 7426 | xpath_func = &xpath_substring_after; |
| 7427 | } |
| 7428 | break; |
| 7429 | case 16: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7430 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7431 | xpath_func = &xpath_substring_before; |
| 7432 | } |
| 7433 | break; |
| 7434 | case 20: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7435 | 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] | 7436 | xpath_func = &xpath_derived_from_or_self; |
| 7437 | } |
| 7438 | break; |
| 7439 | } |
| 7440 | |
| 7441 | if (!xpath_func) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7442 | 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] | 7443 | return LY_EVALID; |
| 7444 | } |
| 7445 | } |
| 7446 | |
| 7447 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7448 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7449 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7450 | |
| 7451 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7452 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7453 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7454 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7455 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7456 | |
| 7457 | /* ( Expr ( ',' Expr )* )? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7458 | if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7459 | if (set) { |
| 7460 | args = malloc(sizeof *args); |
| 7461 | LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup); |
| 7462 | arg_count = 1; |
| 7463 | args[0] = set_copy(set); |
| 7464 | if (!args[0]) { |
| 7465 | rc = LY_EMEM; |
| 7466 | goto cleanup; |
| 7467 | } |
| 7468 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7469 | rc = eval_expr_select(exp, tok_idx, 0, args[0], options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7470 | LY_CHECK_GOTO(rc, cleanup); |
| 7471 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7472 | rc = eval_expr_select(exp, tok_idx, 0, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7473 | LY_CHECK_GOTO(rc, cleanup); |
| 7474 | } |
| 7475 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7476 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7477 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7478 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7479 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7480 | |
| 7481 | if (set) { |
| 7482 | ++arg_count; |
| 7483 | args_aux = realloc(args, arg_count * sizeof *args); |
| 7484 | LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup); |
| 7485 | args = args_aux; |
| 7486 | args[arg_count - 1] = set_copy(set); |
| 7487 | if (!args[arg_count - 1]) { |
| 7488 | rc = LY_EMEM; |
| 7489 | goto cleanup; |
| 7490 | } |
| 7491 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7492 | 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] | 7493 | LY_CHECK_GOTO(rc, cleanup); |
| 7494 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7495 | rc = eval_expr_select(exp, tok_idx, 0, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7496 | LY_CHECK_GOTO(rc, cleanup); |
| 7497 | } |
| 7498 | } |
| 7499 | |
| 7500 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7501 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7502 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7503 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7504 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7505 | |
| 7506 | if (set) { |
| 7507 | /* evaluate function */ |
| 7508 | rc = xpath_func(args, arg_count, set, options); |
| 7509 | |
| 7510 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7511 | /* merge all nodes from arg evaluations */ |
| 7512 | for (i = 0; i < arg_count; ++i) { |
| 7513 | set_scnode_clear_ctx(args[i]); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7514 | lyxp_set_scnode_merge(set, args[i]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7515 | } |
| 7516 | } |
| 7517 | } else { |
| 7518 | rc = LY_SUCCESS; |
| 7519 | } |
| 7520 | |
| 7521 | cleanup: |
| 7522 | for (i = 0; i < arg_count; ++i) { |
| 7523 | lyxp_set_free(args[i]); |
| 7524 | } |
| 7525 | free(args); |
| 7526 | |
| 7527 | return rc; |
| 7528 | } |
| 7529 | |
| 7530 | /** |
| 7531 | * @brief Evaluate Number. Logs directly on error. |
| 7532 | * |
| 7533 | * @param[in] ctx Context for errors. |
| 7534 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7535 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7536 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7537 | * @return LY_ERR |
| 7538 | */ |
| 7539 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7540 | 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] | 7541 | { |
| 7542 | long double num; |
| 7543 | char *endptr; |
| 7544 | |
| 7545 | if (set) { |
| 7546 | errno = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7547 | num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7548 | if (errno) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7549 | 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] | 7550 | 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] | 7551 | 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] | 7552 | return LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7553 | } else if (endptr - &exp->expr[exp->tok_pos[*tok_idx]] != exp->tok_len[*tok_idx]) { |
| 7554 | 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] | 7555 | 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] | 7556 | exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7557 | return LY_EVALID; |
| 7558 | } |
| 7559 | |
| 7560 | set_fill_number(set, num); |
| 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 | return LY_SUCCESS; |
| 7567 | } |
| 7568 | |
| 7569 | /** |
| 7570 | * @brief Evaluate PathExpr. Logs directly on error. |
| 7571 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7572 | * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate* |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7573 | * | PrimaryExpr Predicate* '/' RelativeLocationPath |
| 7574 | * | PrimaryExpr Predicate* '//' RelativeLocationPath |
| 7575 | * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7576 | * [10] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7577 | * |
| 7578 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7579 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7580 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7581 | * @param[in] options XPath options. |
| 7582 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7583 | */ |
| 7584 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7585 | eval_path_expr(struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, int options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7586 | { |
| 7587 | int all_desc, parent_pos_pred; |
| 7588 | LY_ERR rc; |
| 7589 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7590 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7591 | case LYXP_TOKEN_PAR1: |
| 7592 | /* '(' Expr ')' */ |
| 7593 | |
| 7594 | /* '(' */ |
| 7595 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7596 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7597 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7598 | |
| 7599 | /* Expr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7600 | rc = eval_expr_select(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7601 | LY_CHECK_RET(rc); |
| 7602 | |
| 7603 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7604 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7605 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7606 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7607 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7608 | |
| 7609 | parent_pos_pred = 0; |
| 7610 | goto predicate; |
| 7611 | |
| 7612 | case LYXP_TOKEN_DOT: |
| 7613 | case LYXP_TOKEN_DDOT: |
| 7614 | case LYXP_TOKEN_AT: |
| 7615 | case LYXP_TOKEN_NAMETEST: |
| 7616 | case LYXP_TOKEN_NODETYPE: |
| 7617 | /* RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7618 | rc = eval_relative_location_path(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7619 | LY_CHECK_RET(rc); |
| 7620 | break; |
| 7621 | |
| 7622 | case LYXP_TOKEN_FUNCNAME: |
| 7623 | /* FunctionCall */ |
| 7624 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7625 | rc = eval_function_call(exp, tok_idx, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7626 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7627 | rc = eval_function_call(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7628 | } |
| 7629 | LY_CHECK_RET(rc); |
| 7630 | |
| 7631 | parent_pos_pred = 1; |
| 7632 | goto predicate; |
| 7633 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 7634 | case LYXP_TOKEN_OPER_PATH: |
| 7635 | case LYXP_TOKEN_OPER_RPATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7636 | /* AbsoluteLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7637 | rc = eval_absolute_location_path(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7638 | LY_CHECK_RET(rc); |
| 7639 | break; |
| 7640 | |
| 7641 | case LYXP_TOKEN_LITERAL: |
| 7642 | /* Literal */ |
| 7643 | if (!set || (options & LYXP_SCNODE_ALL)) { |
| 7644 | if (set) { |
| 7645 | set_scnode_clear_ctx(set); |
| 7646 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7647 | eval_literal(exp, tok_idx, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7648 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7649 | eval_literal(exp, tok_idx, set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7650 | } |
| 7651 | |
| 7652 | parent_pos_pred = 1; |
| 7653 | goto predicate; |
| 7654 | |
| 7655 | case LYXP_TOKEN_NUMBER: |
| 7656 | /* Number */ |
| 7657 | if (!set || (options & LYXP_SCNODE_ALL)) { |
| 7658 | if (set) { |
| 7659 | set_scnode_clear_ctx(set); |
| 7660 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7661 | rc = eval_number(NULL, exp, tok_idx, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7662 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7663 | rc = eval_number(set->ctx, exp, tok_idx, set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7664 | } |
| 7665 | LY_CHECK_RET(rc); |
| 7666 | |
| 7667 | parent_pos_pred = 1; |
| 7668 | goto predicate; |
| 7669 | |
| 7670 | default: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7671 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[*tok_idx]), |
| 7672 | &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7673 | return LY_EVALID; |
| 7674 | } |
| 7675 | |
| 7676 | return LY_SUCCESS; |
| 7677 | |
| 7678 | predicate: |
| 7679 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7680 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 7681 | rc = eval_predicate(exp, tok_idx, set, options, parent_pos_pred); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7682 | LY_CHECK_RET(rc); |
| 7683 | } |
| 7684 | |
| 7685 | /* ('/' or '//') RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7686 | 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] | 7687 | |
| 7688 | /* evaluate '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7689 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7690 | all_desc = 0; |
| 7691 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7692 | all_desc = 1; |
| 7693 | } |
| 7694 | |
| 7695 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7696 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7697 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7698 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7699 | rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7700 | LY_CHECK_RET(rc); |
| 7701 | } |
| 7702 | |
| 7703 | return LY_SUCCESS; |
| 7704 | } |
| 7705 | |
| 7706 | /** |
| 7707 | * @brief Evaluate UnionExpr. Logs directly on error. |
| 7708 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7709 | * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7710 | * |
| 7711 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7712 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7713 | * @param[in] repeat How many times this expression is repeated. |
| 7714 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7715 | * @param[in] options XPath options. |
| 7716 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7717 | */ |
| 7718 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7719 | eval_union_expr(struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, int options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7720 | { |
| 7721 | LY_ERR rc = LY_SUCCESS; |
| 7722 | struct lyxp_set orig_set, set2; |
| 7723 | uint16_t i; |
| 7724 | |
| 7725 | assert(repeat); |
| 7726 | |
| 7727 | set_init(&orig_set, set); |
| 7728 | set_init(&set2, set); |
| 7729 | |
| 7730 | set_fill_set(&orig_set, set); |
| 7731 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7732 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7733 | LY_CHECK_GOTO(rc, cleanup); |
| 7734 | |
| 7735 | /* ('|' PathExpr)* */ |
| 7736 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7737 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7738 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7739 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7740 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7741 | |
| 7742 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7743 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7744 | LY_CHECK_GOTO(rc, cleanup); |
| 7745 | continue; |
| 7746 | } |
| 7747 | |
| 7748 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7749 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7750 | LY_CHECK_GOTO(rc, cleanup); |
| 7751 | |
| 7752 | /* eval */ |
| 7753 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7754 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7755 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7756 | rc = moveto_union(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7757 | LY_CHECK_GOTO(rc, cleanup); |
| 7758 | } |
| 7759 | } |
| 7760 | |
| 7761 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7762 | lyxp_set_free_content(&orig_set); |
| 7763 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7764 | return rc; |
| 7765 | } |
| 7766 | |
| 7767 | /** |
| 7768 | * @brief Evaluate UnaryExpr. Logs directly on error. |
| 7769 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7770 | * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7771 | * |
| 7772 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7773 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7774 | * @param[in] repeat How many times this expression is repeated. |
| 7775 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7776 | * @param[in] options XPath options. |
| 7777 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7778 | */ |
| 7779 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7780 | eval_unary_expr(struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, int options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7781 | { |
| 7782 | LY_ERR rc; |
| 7783 | uint16_t this_op, i; |
| 7784 | |
| 7785 | assert(repeat); |
| 7786 | |
| 7787 | /* ('-')+ */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7788 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7789 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7790 | 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] | 7791 | |
| 7792 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7793 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7794 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7795 | } |
| 7796 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7797 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7798 | LY_CHECK_RET(rc); |
| 7799 | |
| 7800 | if (set && (repeat % 2)) { |
| 7801 | if (options & LYXP_SCNODE_ALL) { |
| 7802 | warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]); |
| 7803 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7804 | 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] | 7805 | LY_CHECK_RET(rc); |
| 7806 | } |
| 7807 | } |
| 7808 | |
| 7809 | return LY_SUCCESS; |
| 7810 | } |
| 7811 | |
| 7812 | /** |
| 7813 | * @brief Evaluate MultiplicativeExpr. Logs directly on error. |
| 7814 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7815 | * [18] MultiplicativeExpr ::= UnaryExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7816 | * | MultiplicativeExpr '*' UnaryExpr |
| 7817 | * | MultiplicativeExpr 'div' UnaryExpr |
| 7818 | * | MultiplicativeExpr 'mod' UnaryExpr |
| 7819 | * |
| 7820 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7821 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7822 | * @param[in] repeat How many times this expression is repeated. |
| 7823 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7824 | * @param[in] options XPath options. |
| 7825 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7826 | */ |
| 7827 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7828 | eval_multiplicative_expr(struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, int options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7829 | { |
| 7830 | LY_ERR rc; |
| 7831 | uint16_t this_op; |
| 7832 | struct lyxp_set orig_set, set2; |
| 7833 | uint16_t i; |
| 7834 | |
| 7835 | assert(repeat); |
| 7836 | |
| 7837 | set_init(&orig_set, set); |
| 7838 | set_init(&set2, set); |
| 7839 | |
| 7840 | set_fill_set(&orig_set, set); |
| 7841 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7842 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7843 | LY_CHECK_GOTO(rc, cleanup); |
| 7844 | |
| 7845 | /* ('*' / 'div' / 'mod' UnaryExpr)* */ |
| 7846 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7847 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7848 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7849 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7850 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7851 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7852 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7853 | |
| 7854 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7855 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7856 | LY_CHECK_GOTO(rc, cleanup); |
| 7857 | continue; |
| 7858 | } |
| 7859 | |
| 7860 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7861 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7862 | LY_CHECK_GOTO(rc, cleanup); |
| 7863 | |
| 7864 | /* eval */ |
| 7865 | if (options & LYXP_SCNODE_ALL) { |
| 7866 | 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] | 7867 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7868 | set_scnode_clear_ctx(set); |
| 7869 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7870 | 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] | 7871 | LY_CHECK_GOTO(rc, cleanup); |
| 7872 | } |
| 7873 | } |
| 7874 | |
| 7875 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7876 | lyxp_set_free_content(&orig_set); |
| 7877 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7878 | return rc; |
| 7879 | } |
| 7880 | |
| 7881 | /** |
| 7882 | * @brief Evaluate AdditiveExpr. Logs directly on error. |
| 7883 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7884 | * [17] AdditiveExpr ::= MultiplicativeExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7885 | * | AdditiveExpr '+' MultiplicativeExpr |
| 7886 | * | AdditiveExpr '-' MultiplicativeExpr |
| 7887 | * |
| 7888 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7889 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7890 | * @param[in] repeat How many times this expression is repeated. |
| 7891 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7892 | * @param[in] options XPath options. |
| 7893 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7894 | */ |
| 7895 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7896 | eval_additive_expr(struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, int options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7897 | { |
| 7898 | LY_ERR rc; |
| 7899 | uint16_t this_op; |
| 7900 | struct lyxp_set orig_set, set2; |
| 7901 | uint16_t i; |
| 7902 | |
| 7903 | assert(repeat); |
| 7904 | |
| 7905 | set_init(&orig_set, set); |
| 7906 | set_init(&set2, set); |
| 7907 | |
| 7908 | set_fill_set(&orig_set, set); |
| 7909 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7910 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7911 | LY_CHECK_GOTO(rc, cleanup); |
| 7912 | |
| 7913 | /* ('+' / '-' MultiplicativeExpr)* */ |
| 7914 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7915 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7916 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7917 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7918 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7919 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7920 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7921 | |
| 7922 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7923 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7924 | LY_CHECK_GOTO(rc, cleanup); |
| 7925 | continue; |
| 7926 | } |
| 7927 | |
| 7928 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7929 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7930 | LY_CHECK_GOTO(rc, cleanup); |
| 7931 | |
| 7932 | /* eval */ |
| 7933 | if (options & LYXP_SCNODE_ALL) { |
| 7934 | 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] | 7935 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7936 | set_scnode_clear_ctx(set); |
| 7937 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7938 | 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] | 7939 | LY_CHECK_GOTO(rc, cleanup); |
| 7940 | } |
| 7941 | } |
| 7942 | |
| 7943 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7944 | lyxp_set_free_content(&orig_set); |
| 7945 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7946 | return rc; |
| 7947 | } |
| 7948 | |
| 7949 | /** |
| 7950 | * @brief Evaluate RelationalExpr. Logs directly on error. |
| 7951 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7952 | * [16] RelationalExpr ::= AdditiveExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7953 | * | RelationalExpr '<' AdditiveExpr |
| 7954 | * | RelationalExpr '>' AdditiveExpr |
| 7955 | * | RelationalExpr '<=' AdditiveExpr |
| 7956 | * | RelationalExpr '>=' AdditiveExpr |
| 7957 | * |
| 7958 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7959 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7960 | * @param[in] repeat How many times this expression is repeated. |
| 7961 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7962 | * @param[in] options XPath options. |
| 7963 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7964 | */ |
| 7965 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7966 | eval_relational_expr(struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, int options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7967 | { |
| 7968 | LY_ERR rc; |
| 7969 | uint16_t this_op; |
| 7970 | struct lyxp_set orig_set, set2; |
| 7971 | uint16_t i; |
| 7972 | |
| 7973 | assert(repeat); |
| 7974 | |
| 7975 | set_init(&orig_set, set); |
| 7976 | set_init(&set2, set); |
| 7977 | |
| 7978 | set_fill_set(&orig_set, set); |
| 7979 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7980 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7981 | LY_CHECK_GOTO(rc, cleanup); |
| 7982 | |
| 7983 | /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */ |
| 7984 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7985 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7986 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7987 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7988 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7989 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7990 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7991 | |
| 7992 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7993 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7994 | LY_CHECK_GOTO(rc, cleanup); |
| 7995 | continue; |
| 7996 | } |
| 7997 | |
| 7998 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7999 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8000 | LY_CHECK_GOTO(rc, cleanup); |
| 8001 | |
| 8002 | /* eval */ |
| 8003 | if (options & LYXP_SCNODE_ALL) { |
| 8004 | 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] | 8005 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8006 | set_scnode_clear_ctx(set); |
| 8007 | } else { |
| 8008 | rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options); |
| 8009 | LY_CHECK_GOTO(rc, cleanup); |
| 8010 | } |
| 8011 | } |
| 8012 | |
| 8013 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8014 | lyxp_set_free_content(&orig_set); |
| 8015 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8016 | return rc; |
| 8017 | } |
| 8018 | |
| 8019 | /** |
| 8020 | * @brief Evaluate EqualityExpr. Logs directly on error. |
| 8021 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8022 | * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8023 | * | EqualityExpr '!=' RelationalExpr |
| 8024 | * |
| 8025 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8026 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8027 | * @param[in] repeat How many times this expression is repeated. |
| 8028 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8029 | * @param[in] options XPath options. |
| 8030 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8031 | */ |
| 8032 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8033 | eval_equality_expr(struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, int options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8034 | { |
| 8035 | LY_ERR rc; |
| 8036 | uint16_t this_op; |
| 8037 | struct lyxp_set orig_set, set2; |
| 8038 | uint16_t i; |
| 8039 | |
| 8040 | assert(repeat); |
| 8041 | |
| 8042 | set_init(&orig_set, set); |
| 8043 | set_init(&set2, set); |
| 8044 | |
| 8045 | set_fill_set(&orig_set, set); |
| 8046 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8047 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8048 | LY_CHECK_GOTO(rc, cleanup); |
| 8049 | |
| 8050 | /* ('=' / '!=' RelationalExpr)* */ |
| 8051 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8052 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8053 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8054 | 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] | 8055 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8056 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 8057 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8058 | |
| 8059 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8060 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8061 | LY_CHECK_GOTO(rc, cleanup); |
| 8062 | continue; |
| 8063 | } |
| 8064 | |
| 8065 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8066 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8067 | LY_CHECK_GOTO(rc, cleanup); |
| 8068 | |
| 8069 | /* eval */ |
| 8070 | if (options & LYXP_SCNODE_ALL) { |
| 8071 | 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] | 8072 | warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1); |
| 8073 | 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] | 8074 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8075 | set_scnode_clear_ctx(set); |
| 8076 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8077 | rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options); |
| 8078 | LY_CHECK_GOTO(rc, cleanup); |
| 8079 | } |
| 8080 | } |
| 8081 | |
| 8082 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8083 | lyxp_set_free_content(&orig_set); |
| 8084 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8085 | return rc; |
| 8086 | } |
| 8087 | |
| 8088 | /** |
| 8089 | * @brief Evaluate AndExpr. Logs directly on error. |
| 8090 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8091 | * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8092 | * |
| 8093 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8094 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8095 | * @param[in] repeat How many times this expression is repeated. |
| 8096 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8097 | * @param[in] options XPath options. |
| 8098 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8099 | */ |
| 8100 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8101 | eval_and_expr(struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, int options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8102 | { |
| 8103 | LY_ERR rc; |
| 8104 | struct lyxp_set orig_set, set2; |
| 8105 | uint16_t i; |
| 8106 | |
| 8107 | assert(repeat); |
| 8108 | |
| 8109 | set_init(&orig_set, set); |
| 8110 | set_init(&set2, set); |
| 8111 | |
| 8112 | set_fill_set(&orig_set, set); |
| 8113 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8114 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8115 | LY_CHECK_GOTO(rc, cleanup); |
| 8116 | |
| 8117 | /* cast to boolean, we know that will be the final result */ |
| 8118 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 8119 | set_scnode_clear_ctx(set); |
| 8120 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8121 | lyxp_set_cast(set, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8122 | } |
| 8123 | |
| 8124 | /* ('and' EqualityExpr)* */ |
| 8125 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8126 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG); |
| 8127 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || !set->val.bln ? "skipped" : "parsed"), |
| 8128 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 8129 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8130 | |
| 8131 | /* lazy evaluation */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8132 | if (!set || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) { |
| 8133 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8134 | LY_CHECK_GOTO(rc, cleanup); |
| 8135 | continue; |
| 8136 | } |
| 8137 | |
| 8138 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8139 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8140 | LY_CHECK_GOTO(rc, cleanup); |
| 8141 | |
| 8142 | /* eval - just get boolean value actually */ |
| 8143 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 8144 | set_scnode_clear_ctx(&set2); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 8145 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8146 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8147 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8148 | set_fill_set(set, &set2); |
| 8149 | } |
| 8150 | } |
| 8151 | |
| 8152 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8153 | lyxp_set_free_content(&orig_set); |
| 8154 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8155 | return rc; |
| 8156 | } |
| 8157 | |
| 8158 | /** |
| 8159 | * @brief Evaluate OrExpr. Logs directly on error. |
| 8160 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8161 | * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8162 | * |
| 8163 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8164 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8165 | * @param[in] repeat How many times this expression is repeated. |
| 8166 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8167 | * @param[in] options XPath options. |
| 8168 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8169 | */ |
| 8170 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8171 | eval_or_expr(struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, int options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8172 | { |
| 8173 | LY_ERR rc; |
| 8174 | struct lyxp_set orig_set, set2; |
| 8175 | uint16_t i; |
| 8176 | |
| 8177 | assert(repeat); |
| 8178 | |
| 8179 | set_init(&orig_set, set); |
| 8180 | set_init(&set2, set); |
| 8181 | |
| 8182 | set_fill_set(&orig_set, set); |
| 8183 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8184 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8185 | LY_CHECK_GOTO(rc, cleanup); |
| 8186 | |
| 8187 | /* cast to boolean, we know that will be the final result */ |
| 8188 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 8189 | set_scnode_clear_ctx(set); |
| 8190 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8191 | lyxp_set_cast(set, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8192 | } |
| 8193 | |
| 8194 | /* ('or' AndExpr)* */ |
| 8195 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8196 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG); |
| 8197 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || set->val.bln ? "skipped" : "parsed"), |
| 8198 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 8199 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8200 | |
| 8201 | /* lazy evaluation */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8202 | if (!set || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) { |
| 8203 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8204 | LY_CHECK_GOTO(rc, cleanup); |
| 8205 | continue; |
| 8206 | } |
| 8207 | |
| 8208 | set_fill_set(&set2, &orig_set); |
| 8209 | /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one), |
| 8210 | * but it does not matter */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8211 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8212 | LY_CHECK_GOTO(rc, cleanup); |
| 8213 | |
| 8214 | /* eval - just get boolean value actually */ |
| 8215 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 8216 | set_scnode_clear_ctx(&set2); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 8217 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8218 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8219 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8220 | set_fill_set(set, &set2); |
| 8221 | } |
| 8222 | } |
| 8223 | |
| 8224 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8225 | lyxp_set_free_content(&orig_set); |
| 8226 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8227 | return rc; |
| 8228 | } |
| 8229 | |
| 8230 | /** |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8231 | * @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] | 8232 | * |
| 8233 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8234 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8235 | * @param[in] etype Expression type to evaluate. |
| 8236 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8237 | * @param[in] options XPath options. |
| 8238 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8239 | */ |
| 8240 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8241 | eval_expr_select(struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype, struct lyxp_set *set, int options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8242 | { |
| 8243 | uint16_t i, count; |
| 8244 | enum lyxp_expr_type next_etype; |
| 8245 | LY_ERR rc; |
| 8246 | |
| 8247 | /* process operator repeats */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8248 | if (!exp->repeat[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8249 | next_etype = LYXP_EXPR_NONE; |
| 8250 | } else { |
| 8251 | /* find etype repeat */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8252 | for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8253 | |
| 8254 | /* select one-priority lower because etype expression called us */ |
| 8255 | if (i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8256 | next_etype = exp->repeat[*tok_idx][i - 1]; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8257 | /* count repeats for that expression */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8258 | 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] | 8259 | } else { |
| 8260 | next_etype = LYXP_EXPR_NONE; |
| 8261 | } |
| 8262 | } |
| 8263 | |
| 8264 | /* decide what expression are we parsing based on the repeat */ |
| 8265 | switch (next_etype) { |
| 8266 | case LYXP_EXPR_OR: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8267 | rc = eval_or_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8268 | break; |
| 8269 | case LYXP_EXPR_AND: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8270 | rc = eval_and_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8271 | break; |
| 8272 | case LYXP_EXPR_EQUALITY: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8273 | rc = eval_equality_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8274 | break; |
| 8275 | case LYXP_EXPR_RELATIONAL: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8276 | rc = eval_relational_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8277 | break; |
| 8278 | case LYXP_EXPR_ADDITIVE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8279 | rc = eval_additive_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8280 | break; |
| 8281 | case LYXP_EXPR_MULTIPLICATIVE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8282 | rc = eval_multiplicative_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8283 | break; |
| 8284 | case LYXP_EXPR_UNARY: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8285 | rc = eval_unary_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8286 | break; |
| 8287 | case LYXP_EXPR_UNION: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8288 | rc = eval_union_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8289 | break; |
| 8290 | case LYXP_EXPR_NONE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8291 | rc = eval_path_expr(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8292 | break; |
| 8293 | default: |
| 8294 | LOGINT_RET(set->ctx); |
| 8295 | } |
| 8296 | |
| 8297 | return rc; |
| 8298 | } |
| 8299 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8300 | /** |
| 8301 | * @brief Get root type. |
| 8302 | * |
| 8303 | * @param[in] ctx_node Context node. |
| 8304 | * @param[in] ctx_scnode Schema context node. |
| 8305 | * @param[in] options XPath options. |
| 8306 | * @return Root type. |
| 8307 | */ |
| 8308 | static enum lyxp_node_type |
| 8309 | lyxp_get_root_type(const struct lyd_node *ctx_node, const struct lysc_node *ctx_scnode, int options) |
| 8310 | { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 8311 | const struct lysc_node *op; |
| 8312 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8313 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 8314 | for (op = ctx_scnode; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent); |
| 8315 | |
| 8316 | if (op || (options & LYXP_SCNODE)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8317 | /* general root that can access everything */ |
| 8318 | return LYXP_NODE_ROOT; |
| 8319 | } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) { |
| 8320 | /* root context node can access only config data (because we said so, it is unspecified) */ |
| 8321 | return LYXP_NODE_ROOT_CONFIG; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8322 | } |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 8323 | return LYXP_NODE_ROOT; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8324 | } |
| 8325 | |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 8326 | op = ctx_node ? ctx_node->schema : NULL; |
| 8327 | for (; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent); |
| 8328 | |
| 8329 | if (!ctx_node || (!op && (ctx_node->schema->flags & LYS_CONFIG_W))) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8330 | /* root context node can access only config data (because we said so, it is unspecified) */ |
| 8331 | return LYXP_NODE_ROOT_CONFIG; |
| 8332 | } |
| 8333 | |
| 8334 | return LYXP_NODE_ROOT; |
| 8335 | } |
| 8336 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8337 | LY_ERR |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 8338 | lyxp_eval(struct lyxp_expr *exp, LY_PREFIX_FORMAT format, const struct lys_module *local_mod, const struct lyd_node *ctx_node, |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 8339 | enum lyxp_node_type ctx_node_type, const struct lyd_node *tree, struct lyxp_set *set, int options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8340 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8341 | uint16_t tok_idx = 0; |
| 8342 | const struct lyd_node *real_ctx_node; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8343 | LY_ERR rc; |
| 8344 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8345 | LY_CHECK_ARG_RET(NULL, exp, local_mod, ctx_node, set, LY_EINVAL); |
| 8346 | |
| 8347 | if ((ctx_node_type == LYXP_NODE_ROOT) || (ctx_node_type == LYXP_NODE_ROOT_CONFIG)) { |
| 8348 | /* we always need some context node because it is used for resolving unqualified names */ |
| 8349 | real_ctx_node = NULL; |
| 8350 | } else { |
| 8351 | real_ctx_node = ctx_node; |
| 8352 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8353 | |
| 8354 | /* prepare set for evaluation */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8355 | tok_idx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8356 | memset(set, 0, sizeof *set); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8357 | set->type = LYXP_SET_NODE_SET; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8358 | 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] | 8359 | set->ctx = local_mod->ctx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8360 | set->ctx_node = ctx_node; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8361 | set->root_type = lyxp_get_root_type(real_ctx_node, NULL, options); |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 8362 | for (set->context_op = ctx_node->schema; |
| 8363 | set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); |
| 8364 | set->context_op = set->context_op->parent); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8365 | set->local_mod = local_mod; |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 8366 | set->tree = tree; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8367 | set->format = format; |
| 8368 | |
| 8369 | /* evaluate */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8370 | rc = eval_expr_select(exp, &tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8371 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8372 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8373 | } |
| 8374 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8375 | return rc; |
| 8376 | } |
| 8377 | |
| 8378 | #if 0 |
| 8379 | |
| 8380 | /* full xml printing of set elements, not used currently */ |
| 8381 | |
| 8382 | void |
| 8383 | lyxp_set_print_xml(FILE *f, struct lyxp_set *set) |
| 8384 | { |
| 8385 | uint32_t i; |
| 8386 | char *str_num; |
| 8387 | struct lyout out; |
| 8388 | |
| 8389 | memset(&out, 0, sizeof out); |
| 8390 | |
| 8391 | out.type = LYOUT_STREAM; |
| 8392 | out.method.f = f; |
| 8393 | |
| 8394 | switch (set->type) { |
| 8395 | case LYXP_SET_EMPTY: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8396 | ly_print_(&out, "Empty XPath set\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8397 | break; |
| 8398 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8399 | ly_print_(&out, "Boolean XPath set:\n"); |
| 8400 | ly_print_(&out, "%s\n\n", set->value.bool ? "true" : "false"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8401 | break; |
| 8402 | case LYXP_SET_STRING: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8403 | ly_print_(&out, "String XPath set:\n"); |
| 8404 | ly_print_(&out, "\"%s\"\n\n", set->value.str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8405 | break; |
| 8406 | case LYXP_SET_NUMBER: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8407 | ly_print_(&out, "Number XPath set:\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8408 | |
| 8409 | if (isnan(set->value.num)) { |
| 8410 | str_num = strdup("NaN"); |
| 8411 | } else if ((set->value.num == 0) || (set->value.num == -0.0f)) { |
| 8412 | str_num = strdup("0"); |
| 8413 | } else if (isinf(set->value.num) && !signbit(set->value.num)) { |
| 8414 | str_num = strdup("Infinity"); |
| 8415 | } else if (isinf(set->value.num) && signbit(set->value.num)) { |
| 8416 | str_num = strdup("-Infinity"); |
| 8417 | } else if ((long long)set->value.num == set->value.num) { |
| 8418 | if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) { |
| 8419 | str_num = NULL; |
| 8420 | } |
| 8421 | } else { |
| 8422 | if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) { |
| 8423 | str_num = NULL; |
| 8424 | } |
| 8425 | } |
| 8426 | if (!str_num) { |
| 8427 | LOGMEM; |
| 8428 | return; |
| 8429 | } |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8430 | ly_print_(&out, "%s\n\n", str_num); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8431 | free(str_num); |
| 8432 | break; |
| 8433 | case LYXP_SET_NODE_SET: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8434 | ly_print_(&out, "Node XPath set:\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8435 | |
| 8436 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8437 | ly_print_(&out, "%d. ", i + 1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8438 | switch (set->node_type[i]) { |
| 8439 | case LYXP_NODE_ROOT_ALL: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8440 | ly_print_(&out, "ROOT all\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8441 | break; |
| 8442 | case LYXP_NODE_ROOT_CONFIG: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8443 | ly_print_(&out, "ROOT config\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8444 | break; |
| 8445 | case LYXP_NODE_ROOT_STATE: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8446 | ly_print_(&out, "ROOT state\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8447 | break; |
| 8448 | case LYXP_NODE_ROOT_NOTIF: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8449 | 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] | 8450 | break; |
| 8451 | case LYXP_NODE_ROOT_RPC: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8452 | 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] | 8453 | break; |
| 8454 | case LYXP_NODE_ROOT_OUTPUT: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8455 | 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] | 8456 | break; |
| 8457 | case LYXP_NODE_ELEM: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8458 | ly_print_(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8459 | xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT); |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8460 | ly_print_(&out, "\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8461 | break; |
| 8462 | case LYXP_NODE_TEXT: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8463 | 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] | 8464 | break; |
| 8465 | case LYXP_NODE_ATTR: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8466 | 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] | 8467 | break; |
| 8468 | } |
| 8469 | } |
| 8470 | break; |
| 8471 | } |
| 8472 | } |
| 8473 | |
| 8474 | #endif |
| 8475 | |
| 8476 | LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8477 | lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8478 | { |
| 8479 | long double num; |
| 8480 | char *str; |
| 8481 | LY_ERR rc; |
| 8482 | |
| 8483 | if (!set || (set->type == target)) { |
| 8484 | return LY_SUCCESS; |
| 8485 | } |
| 8486 | |
| 8487 | /* it's not possible to convert anything into a node set */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8488 | assert(target != LYXP_SET_NODE_SET); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8489 | |
| 8490 | if (set->type == LYXP_SET_SCNODE_SET) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8491 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8492 | return LY_EINVAL; |
| 8493 | } |
| 8494 | |
| 8495 | /* to STRING */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8496 | 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] | 8497 | switch (set->type) { |
| 8498 | case LYXP_SET_NUMBER: |
| 8499 | if (isnan(set->val.num)) { |
| 8500 | set->val.str = strdup("NaN"); |
| 8501 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 8502 | } else if ((set->val.num == 0) || (set->val.num == -0.0f)) { |
| 8503 | set->val.str = strdup("0"); |
| 8504 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 8505 | } else if (isinf(set->val.num) && !signbit(set->val.num)) { |
| 8506 | set->val.str = strdup("Infinity"); |
| 8507 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 8508 | } else if (isinf(set->val.num) && signbit(set->val.num)) { |
| 8509 | set->val.str = strdup("-Infinity"); |
| 8510 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 8511 | } else if ((long long)set->val.num == set->val.num) { |
| 8512 | if (asprintf(&str, "%lld", (long long)set->val.num) == -1) { |
| 8513 | LOGMEM_RET(set->ctx); |
| 8514 | } |
| 8515 | set->val.str = str; |
| 8516 | } else { |
| 8517 | if (asprintf(&str, "%03.1Lf", set->val.num) == -1) { |
| 8518 | LOGMEM_RET(set->ctx); |
| 8519 | } |
| 8520 | set->val.str = str; |
| 8521 | } |
| 8522 | break; |
| 8523 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8524 | if (set->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8525 | set->val.str = strdup("true"); |
| 8526 | } else { |
| 8527 | set->val.str = strdup("false"); |
| 8528 | } |
| 8529 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM); |
| 8530 | break; |
| 8531 | case LYXP_SET_NODE_SET: |
| 8532 | assert(set->used); |
| 8533 | |
| 8534 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8535 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8536 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8537 | rc = cast_node_set_to_string(set, &str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8538 | LY_CHECK_RET(rc); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8539 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8540 | set->val.str = str; |
| 8541 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8542 | default: |
| 8543 | LOGINT_RET(set->ctx); |
| 8544 | } |
| 8545 | set->type = LYXP_SET_STRING; |
| 8546 | } |
| 8547 | |
| 8548 | /* to NUMBER */ |
| 8549 | if (target == LYXP_SET_NUMBER) { |
| 8550 | switch (set->type) { |
| 8551 | case LYXP_SET_STRING: |
| 8552 | num = cast_string_to_number(set->val.str); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8553 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8554 | set->val.num = num; |
| 8555 | break; |
| 8556 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8557 | if (set->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8558 | set->val.num = 1; |
| 8559 | } else { |
| 8560 | set->val.num = 0; |
| 8561 | } |
| 8562 | break; |
| 8563 | default: |
| 8564 | LOGINT_RET(set->ctx); |
| 8565 | } |
| 8566 | set->type = LYXP_SET_NUMBER; |
| 8567 | } |
| 8568 | |
| 8569 | /* to BOOLEAN */ |
| 8570 | if (target == LYXP_SET_BOOLEAN) { |
| 8571 | switch (set->type) { |
| 8572 | case LYXP_SET_NUMBER: |
| 8573 | 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] | 8574 | set->val.bln = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8575 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8576 | set->val.bln = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8577 | } |
| 8578 | break; |
| 8579 | case LYXP_SET_STRING: |
| 8580 | if (set->val.str[0]) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8581 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8582 | set->val.bln = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8583 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8584 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8585 | set->val.bln = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8586 | } |
| 8587 | break; |
| 8588 | case LYXP_SET_NODE_SET: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8589 | if (set->used) { |
| 8590 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8591 | set->val.bln = 1; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8592 | } else { |
| 8593 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8594 | set->val.bln = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8595 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8596 | break; |
| 8597 | default: |
| 8598 | LOGINT_RET(set->ctx); |
| 8599 | } |
| 8600 | set->type = LYXP_SET_BOOLEAN; |
| 8601 | } |
| 8602 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8603 | return LY_SUCCESS; |
| 8604 | } |
| 8605 | |
| 8606 | LY_ERR |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 8607 | lyxp_atomize(struct lyxp_expr *exp, LY_PREFIX_FORMAT format, const struct lys_module *local_mod, |
| 8608 | const struct lysc_node *ctx_scnode, enum lyxp_node_type ctx_scnode_type, struct lyxp_set *set, int options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8609 | { |
| 8610 | struct ly_ctx *ctx; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8611 | const struct lysc_node *real_ctx_scnode; |
| 8612 | uint16_t tok_idx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8613 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8614 | 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] | 8615 | |
| 8616 | ctx = local_mod->ctx; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8617 | if ((ctx_scnode_type == LYXP_NODE_ROOT) || (ctx_scnode_type == LYXP_NODE_ROOT_CONFIG)) { |
| 8618 | /* we always need some context node because it is used for resolving unqualified names */ |
| 8619 | real_ctx_scnode = NULL; |
| 8620 | } else { |
| 8621 | real_ctx_scnode = ctx_scnode; |
| 8622 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8623 | |
| 8624 | /* prepare set for evaluation */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8625 | tok_idx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8626 | memset(set, 0, sizeof *set); |
| 8627 | set->type = LYXP_SET_SCNODE_SET; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8628 | lyxp_set_scnode_insert_node(set, real_ctx_scnode, ctx_scnode_type); |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 8629 | set->val.scnodes[0].in_ctx = -2; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8630 | set->ctx = ctx; |
| 8631 | set->ctx_scnode = ctx_scnode; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8632 | set->root_type = lyxp_get_root_type(NULL, real_ctx_scnode, options); |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 8633 | for (set->context_op = ctx_scnode; |
| 8634 | set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); |
| 8635 | set->context_op = set->context_op->parent); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8636 | set->local_mod = local_mod; |
| 8637 | set->format = format; |
| 8638 | |
| 8639 | /* evaluate */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8640 | return eval_expr_select(exp, &tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8641 | } |
Michal Vasko | d43d71a | 2020-08-07 14:54:58 +0200 | [diff] [blame] | 8642 | |
| 8643 | API const char * |
| 8644 | lyxp_get_expr(const struct lyxp_expr *path) |
| 8645 | { |
| 8646 | if (!path) { |
| 8647 | return NULL; |
| 8648 | } |
| 8649 | |
| 8650 | return path->expr; |
| 8651 | } |