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 | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 1304 | const struct lyd_node *next, *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 */ |
| 1315 | elem = next = *prev; |
| 1316 | pos = *prev_pos; |
| 1317 | for (top_sibling = elem; top_sibling->parent; top_sibling = (struct lyd_node *)top_sibling->parent); |
| 1318 | goto dfs_search; |
| 1319 | } |
| 1320 | |
| 1321 | for (top_sibling = root; top_sibling; top_sibling = top_sibling->next) { |
| 1322 | /* TREE DFS */ |
| 1323 | LYD_TREE_DFS_BEGIN(top_sibling, next, elem) { |
| 1324 | dfs_search: |
| 1325 | if ((root_type == LYXP_NODE_ROOT_CONFIG) && (elem->schema->flags & LYS_CONFIG_R)) { |
| 1326 | goto skip_children; |
| 1327 | } |
| 1328 | |
| 1329 | if (elem == node) { |
| 1330 | break; |
| 1331 | } |
| 1332 | ++pos; |
| 1333 | |
| 1334 | /* TREE DFS END */ |
| 1335 | /* select element for the next run - children first, |
| 1336 | * child exception for lyd_node_leaf and lyd_node_leaflist, but not the root */ |
| 1337 | if (elem->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
| 1338 | next = NULL; |
| 1339 | } else { |
Michal Vasko | 5bfd4be | 2020-06-23 13:26:19 +0200 | [diff] [blame] | 1340 | next = lyd_node_children(elem, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1341 | } |
| 1342 | if (!next) { |
| 1343 | skip_children: |
| 1344 | /* no children */ |
| 1345 | if (elem == top_sibling) { |
| 1346 | /* we are done, root has no children */ |
| 1347 | elem = NULL; |
| 1348 | break; |
| 1349 | } |
| 1350 | /* try siblings */ |
| 1351 | next = elem->next; |
| 1352 | } |
| 1353 | while (!next) { |
| 1354 | /* no siblings, go back through parents */ |
| 1355 | if (elem->parent == top_sibling->parent) { |
| 1356 | /* we are done, no next element to process */ |
| 1357 | elem = NULL; |
| 1358 | break; |
| 1359 | } |
| 1360 | /* parent is already processed, go to its sibling */ |
| 1361 | elem = (struct lyd_node *)elem->parent; |
| 1362 | next = elem->next; |
| 1363 | } |
| 1364 | } |
| 1365 | |
| 1366 | /* node found */ |
| 1367 | if (elem) { |
| 1368 | break; |
| 1369 | } |
| 1370 | } |
| 1371 | |
| 1372 | if (!elem) { |
| 1373 | if (!(*prev)) { |
| 1374 | /* we went from root and failed to find it, cannot be */ |
| 1375 | LOGINT(node->schema->module->ctx); |
| 1376 | return 0; |
| 1377 | } else { |
| 1378 | *prev = NULL; |
| 1379 | *prev_pos = 0; |
| 1380 | |
| 1381 | elem = next = top_sibling = root; |
| 1382 | pos = 1; |
| 1383 | goto dfs_search; |
| 1384 | } |
| 1385 | } |
| 1386 | |
| 1387 | /* remember the last found node for next time */ |
| 1388 | *prev = node; |
| 1389 | *prev_pos = pos; |
| 1390 | |
| 1391 | return pos; |
| 1392 | } |
| 1393 | |
| 1394 | /** |
| 1395 | * @brief Assign (fill) missing node positions. |
| 1396 | * |
| 1397 | * @param[in] set Set to fill positions in. |
| 1398 | * @param[in] root Context root node. |
| 1399 | * @param[in] root_type Context root type. |
| 1400 | * @return LY_ERR |
| 1401 | */ |
| 1402 | static LY_ERR |
| 1403 | set_assign_pos(struct lyxp_set *set, const struct lyd_node *root, enum lyxp_node_type root_type) |
| 1404 | { |
| 1405 | const struct lyd_node *prev = NULL, *tmp_node; |
| 1406 | uint32_t i, tmp_pos = 0; |
| 1407 | |
| 1408 | for (i = 0; i < set->used; ++i) { |
| 1409 | if (!set->val.nodes[i].pos) { |
| 1410 | tmp_node = NULL; |
| 1411 | switch (set->val.nodes[i].type) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1412 | case LYXP_NODE_META: |
| 1413 | tmp_node = set->val.meta[i].meta->parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1414 | if (!tmp_node) { |
| 1415 | LOGINT_RET(root->schema->module->ctx); |
| 1416 | } |
| 1417 | /* fallthrough */ |
| 1418 | case LYXP_NODE_ELEM: |
| 1419 | case LYXP_NODE_TEXT: |
| 1420 | if (!tmp_node) { |
| 1421 | tmp_node = set->val.nodes[i].node; |
| 1422 | } |
| 1423 | set->val.nodes[i].pos = get_node_pos(tmp_node, set->val.nodes[i].type, root, root_type, &prev, &tmp_pos); |
| 1424 | break; |
| 1425 | default: |
| 1426 | /* all roots have position 0 */ |
| 1427 | break; |
| 1428 | } |
| 1429 | } |
| 1430 | } |
| 1431 | |
| 1432 | return LY_SUCCESS; |
| 1433 | } |
| 1434 | |
| 1435 | /** |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1436 | * @brief Get unique @p meta position in the parent metadata. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1437 | * |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1438 | * @param[in] meta Metadata to use. |
| 1439 | * @return Metadata position. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1440 | */ |
| 1441 | static uint16_t |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1442 | get_meta_pos(struct lyd_meta *meta) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1443 | { |
| 1444 | uint16_t pos = 0; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1445 | struct lyd_meta *meta2; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1446 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1447 | for (meta2 = meta->parent->meta; meta2 && (meta2 != meta); meta2 = meta2->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1448 | ++pos; |
| 1449 | } |
| 1450 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1451 | assert(meta2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1452 | return pos; |
| 1453 | } |
| 1454 | |
| 1455 | /** |
| 1456 | * @brief Compare 2 nodes in respect to XPath document order. |
| 1457 | * |
| 1458 | * @param[in] item1 1st node. |
| 1459 | * @param[in] item2 2nd node. |
| 1460 | * @return If 1st > 2nd returns 1, 1st == 2nd returns 0, and 1st < 2nd returns -1. |
| 1461 | */ |
| 1462 | static int |
| 1463 | set_sort_compare(struct lyxp_set_node *item1, struct lyxp_set_node *item2) |
| 1464 | { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1465 | uint32_t meta_pos1 = 0, meta_pos2 = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1466 | |
| 1467 | if (item1->pos < item2->pos) { |
| 1468 | return -1; |
| 1469 | } |
| 1470 | |
| 1471 | if (item1->pos > item2->pos) { |
| 1472 | return 1; |
| 1473 | } |
| 1474 | |
| 1475 | /* node positions are equal, the fun case */ |
| 1476 | |
| 1477 | /* 1st ELEM - == - 2nd TEXT, 1st TEXT - == - 2nd ELEM */ |
| 1478 | /* special case since text nodes are actually saved as their parents */ |
| 1479 | if ((item1->node == item2->node) && (item1->type != item2->type)) { |
| 1480 | if (item1->type == LYXP_NODE_ELEM) { |
| 1481 | assert(item2->type == LYXP_NODE_TEXT); |
| 1482 | return -1; |
| 1483 | } else { |
| 1484 | assert((item1->type == LYXP_NODE_TEXT) && (item2->type == LYXP_NODE_ELEM)); |
| 1485 | return 1; |
| 1486 | } |
| 1487 | } |
| 1488 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1489 | /* we need meta positions now */ |
| 1490 | if (item1->type == LYXP_NODE_META) { |
| 1491 | meta_pos1 = get_meta_pos((struct lyd_meta *)item1->node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1492 | } |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1493 | if (item2->type == LYXP_NODE_META) { |
| 1494 | meta_pos2 = get_meta_pos((struct lyd_meta *)item2->node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1495 | } |
| 1496 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1497 | /* 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] | 1498 | /* check for duplicates */ |
| 1499 | if (item1->node == item2->node) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1500 | 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] | 1501 | return 0; |
| 1502 | } |
| 1503 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1504 | /* 1st ELEM - 2nd TEXT, 1st ELEM - any pos - 2nd META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1505 | /* elem is always first, 2nd node is after it */ |
| 1506 | if (item1->type == LYXP_NODE_ELEM) { |
| 1507 | assert(item2->type != LYXP_NODE_ELEM); |
| 1508 | return -1; |
| 1509 | } |
| 1510 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1511 | /* 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] | 1512 | /* 2nd is before 1st */ |
| 1513 | if (((item1->type == LYXP_NODE_TEXT) |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1514 | && ((item2->type == LYXP_NODE_ELEM) || (item2->type == LYXP_NODE_META))) |
| 1515 | || ((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_ELEM)) |
| 1516 | || (((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_META)) |
| 1517 | && (meta_pos1 > meta_pos2))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1518 | return 1; |
| 1519 | } |
| 1520 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1521 | /* 1st META - any pos - 2nd TEXT, 1st META <pos< - 2nd META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1522 | /* 2nd is after 1st */ |
| 1523 | return -1; |
| 1524 | } |
| 1525 | |
| 1526 | /** |
| 1527 | * @brief Set cast for comparisons. |
| 1528 | * |
| 1529 | * @param[in] trg Target set to cast source into. |
| 1530 | * @param[in] src Source set. |
| 1531 | * @param[in] type Target set type. |
| 1532 | * @param[in] src_idx Source set node index. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1533 | * @return LY_ERR |
| 1534 | */ |
| 1535 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1536 | 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] | 1537 | { |
| 1538 | assert(src->type == LYXP_SET_NODE_SET); |
| 1539 | |
| 1540 | set_init(trg, src); |
| 1541 | |
| 1542 | /* insert node into target set */ |
| 1543 | set_insert_node(trg, src->val.nodes[src_idx].node, src->val.nodes[src_idx].pos, src->val.nodes[src_idx].type, 0); |
| 1544 | |
| 1545 | /* cast target set appropriately */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1546 | return lyxp_set_cast(trg, type); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1547 | } |
| 1548 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1549 | /** |
| 1550 | * @brief Set content canonization for comparisons. |
| 1551 | * |
| 1552 | * @param[in] trg Target set to put the canononized source into. |
| 1553 | * @param[in] src Source set. |
| 1554 | * @param[in] xp_node Source XPath node/meta to use for canonization. |
| 1555 | * @return LY_ERR |
| 1556 | */ |
| 1557 | static LY_ERR |
| 1558 | set_comp_canonize(struct lyxp_set *trg, const struct lyxp_set *src, const struct lyxp_set_node *xp_node) |
| 1559 | { |
| 1560 | struct lysc_type *type = NULL; |
| 1561 | struct lyd_value val; |
| 1562 | struct ly_err_item *err = NULL; |
| 1563 | char *str, *ptr; |
| 1564 | int dynamic; |
| 1565 | LY_ERR rc; |
| 1566 | |
| 1567 | /* is there anything to canonize even? */ |
| 1568 | if ((src->type == LYXP_SET_NUMBER) || (src->type == LYXP_SET_STRING)) { |
| 1569 | /* do we have a type to use for canonization? */ |
| 1570 | if ((xp_node->type == LYXP_NODE_ELEM) && (xp_node->node->schema->nodetype & LYD_NODE_TERM)) { |
| 1571 | type = ((struct lyd_node_term *)xp_node->node)->value.realtype; |
| 1572 | } else if (xp_node->type == LYXP_NODE_META) { |
| 1573 | type = ((struct lyd_meta *)xp_node->node)->value.realtype; |
| 1574 | } |
| 1575 | } |
| 1576 | if (!type) { |
| 1577 | goto fill; |
| 1578 | } |
| 1579 | |
| 1580 | if (src->type == LYXP_SET_NUMBER) { |
| 1581 | /* canonize number */ |
| 1582 | if (asprintf(&str, "%Lf", src->val.num) == -1) { |
| 1583 | LOGMEM(src->ctx); |
| 1584 | return LY_EMEM; |
| 1585 | } |
| 1586 | } else { |
| 1587 | /* canonize string */ |
| 1588 | str = strdup(src->val.str); |
| 1589 | } |
| 1590 | |
| 1591 | /* ignore errors, the value may not satisfy schema constraints */ |
| 1592 | rc = type->plugin->store(src->ctx, type, str, strlen(str), |
| 1593 | LY_TYPE_OPTS_STORE | LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_DYNAMIC, NULL, |
| 1594 | NULL, LYD_JSON, NULL, NULL, &val, NULL, &err); |
| 1595 | ly_err_free(err); |
| 1596 | if (rc) { |
| 1597 | /* invalid value */ |
| 1598 | free(str); |
| 1599 | goto fill; |
| 1600 | } |
| 1601 | |
| 1602 | /* storing successful, now print the canonical value */ |
| 1603 | str = (char *)type->plugin->print(&val, LYD_JSON, json_print_get_prefix, NULL, &dynamic); |
| 1604 | |
| 1605 | /* use the canonized value */ |
| 1606 | set_init(trg, src); |
| 1607 | trg->type = src->type; |
| 1608 | if (src->type == LYXP_SET_NUMBER) { |
| 1609 | trg->val.num = strtold(str, &ptr); |
| 1610 | if (dynamic) { |
| 1611 | free(str); |
| 1612 | } |
| 1613 | LY_CHECK_ERR_RET(ptr[0], LOGINT(src->ctx), LY_EINT); |
| 1614 | } else { |
| 1615 | trg->val.str = (dynamic ? str : strdup(str)); |
| 1616 | } |
| 1617 | type->plugin->free(src->ctx, &val); |
| 1618 | return LY_SUCCESS; |
| 1619 | |
| 1620 | fill: |
| 1621 | /* no canonization needed/possible */ |
| 1622 | set_fill_set(trg, src); |
| 1623 | return LY_SUCCESS; |
| 1624 | } |
| 1625 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1626 | #ifndef NDEBUG |
| 1627 | |
| 1628 | /** |
| 1629 | * @brief Bubble sort @p set into XPath document order. |
| 1630 | * Context position aware. Unused in the 'Release' build target. |
| 1631 | * |
| 1632 | * @param[in] set Set to sort. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1633 | * @return How many times the whole set was traversed - 1 (if set was sorted, returns 0). |
| 1634 | */ |
| 1635 | static int |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1636 | set_sort(struct lyxp_set *set) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1637 | { |
| 1638 | uint32_t i, j; |
| 1639 | int ret = 0, cmp, inverted, change; |
| 1640 | const struct lyd_node *root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1641 | struct lyxp_set_node item; |
| 1642 | struct lyxp_set_hash_node hnode; |
| 1643 | uint64_t hash; |
| 1644 | |
Michal Vasko | 3cf8fbf | 2020-05-27 15:21:21 +0200 | [diff] [blame] | 1645 | if ((set->type != LYXP_SET_NODE_SET) || (set->used < 2)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1646 | return 0; |
| 1647 | } |
| 1648 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1649 | /* find first top-level node to be used as anchor for positions */ |
| 1650 | for (root = set->ctx_node; root->parent; root = (const struct lyd_node *)root->parent); |
| 1651 | for (; root->prev->next; root = root->prev); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1652 | |
| 1653 | /* fill positions */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1654 | if (set_assign_pos(set, root, set->root_type)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1655 | return -1; |
| 1656 | } |
| 1657 | |
| 1658 | LOGDBG(LY_LDGXPATH, "SORT BEGIN"); |
| 1659 | print_set_debug(set); |
| 1660 | |
| 1661 | for (i = 0; i < set->used; ++i) { |
| 1662 | inverted = 0; |
| 1663 | change = 0; |
| 1664 | |
| 1665 | for (j = 1; j < set->used - i; ++j) { |
| 1666 | /* compare node positions */ |
| 1667 | if (inverted) { |
| 1668 | cmp = set_sort_compare(&set->val.nodes[j], &set->val.nodes[j - 1]); |
| 1669 | } else { |
| 1670 | cmp = set_sort_compare(&set->val.nodes[j - 1], &set->val.nodes[j]); |
| 1671 | } |
| 1672 | |
| 1673 | /* swap if needed */ |
| 1674 | if ((inverted && (cmp < 0)) || (!inverted && (cmp > 0))) { |
| 1675 | change = 1; |
| 1676 | |
| 1677 | item = set->val.nodes[j - 1]; |
| 1678 | set->val.nodes[j - 1] = set->val.nodes[j]; |
| 1679 | set->val.nodes[j] = item; |
| 1680 | } else { |
| 1681 | /* whether node_pos1 should be smaller than node_pos2 or the other way around */ |
| 1682 | inverted = !inverted; |
| 1683 | } |
| 1684 | } |
| 1685 | |
| 1686 | ++ret; |
| 1687 | |
| 1688 | if (!change) { |
| 1689 | break; |
| 1690 | } |
| 1691 | } |
| 1692 | |
| 1693 | LOGDBG(LY_LDGXPATH, "SORT END %d", ret); |
| 1694 | print_set_debug(set); |
| 1695 | |
| 1696 | /* check node hashes */ |
| 1697 | if (set->used >= LYD_HT_MIN_ITEMS) { |
| 1698 | assert(set->ht); |
| 1699 | for (i = 0; i < set->used; ++i) { |
| 1700 | hnode.node = set->val.nodes[i].node; |
| 1701 | hnode.type = set->val.nodes[i].type; |
| 1702 | |
| 1703 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 1704 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 1705 | hash = dict_hash_multi(hash, NULL, 0); |
| 1706 | |
| 1707 | assert(!lyht_find(set->ht, &hnode, hash, NULL)); |
| 1708 | } |
| 1709 | } |
| 1710 | |
| 1711 | return ret - 1; |
| 1712 | } |
| 1713 | |
| 1714 | /** |
| 1715 | * @brief Remove duplicate entries in a sorted node set. |
| 1716 | * |
| 1717 | * @param[in] set Sorted set to check. |
| 1718 | * @return LY_ERR (LY_EEXIST if some duplicates are found) |
| 1719 | */ |
| 1720 | static LY_ERR |
| 1721 | set_sorted_dup_node_clean(struct lyxp_set *set) |
| 1722 | { |
| 1723 | uint32_t i = 0; |
| 1724 | LY_ERR ret = LY_SUCCESS; |
| 1725 | |
| 1726 | if (set->used > 1) { |
| 1727 | while (i < set->used - 1) { |
| 1728 | if ((set->val.nodes[i].node == set->val.nodes[i + 1].node) |
| 1729 | && (set->val.nodes[i].type == set->val.nodes[i + 1].type)) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1730 | set_remove_node_none(set, i + 1); |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1731 | ret = LY_EEXIST; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1732 | } |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1733 | ++i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1734 | } |
| 1735 | } |
| 1736 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1737 | set_remove_nodes_none(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1738 | return ret; |
| 1739 | } |
| 1740 | |
| 1741 | #endif |
| 1742 | |
| 1743 | /** |
| 1744 | * @brief Merge 2 sorted sets into one. |
| 1745 | * |
| 1746 | * @param[in,out] trg Set to merge into. Duplicates are removed. |
| 1747 | * @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] | 1748 | * @return LY_ERR |
| 1749 | */ |
| 1750 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1751 | set_sorted_merge(struct lyxp_set *trg, struct lyxp_set *src) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1752 | { |
| 1753 | uint32_t i, j, k, count, dup_count; |
| 1754 | int cmp; |
| 1755 | const struct lyd_node *root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1756 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1757 | 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] | 1758 | return LY_EINVAL; |
| 1759 | } |
| 1760 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1761 | if (!src->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1762 | return LY_SUCCESS; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1763 | } else if (!trg->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1764 | set_fill_set(trg, src); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1765 | lyxp_set_free_content(src); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1766 | return LY_SUCCESS; |
| 1767 | } |
| 1768 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1769 | /* find first top-level node to be used as anchor for positions */ |
| 1770 | for (root = trg->ctx_node; root->parent; root = (const struct lyd_node *)root->parent); |
| 1771 | for (; root->prev->next; root = root->prev); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1772 | |
| 1773 | /* fill positions */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1774 | 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] | 1775 | return LY_EINT; |
| 1776 | } |
| 1777 | |
| 1778 | #ifndef NDEBUG |
| 1779 | LOGDBG(LY_LDGXPATH, "MERGE target"); |
| 1780 | print_set_debug(trg); |
| 1781 | LOGDBG(LY_LDGXPATH, "MERGE source"); |
| 1782 | print_set_debug(src); |
| 1783 | #endif |
| 1784 | |
| 1785 | /* make memory for the merge (duplicates are not detected yet, so space |
| 1786 | * will likely be wasted on them, too bad) */ |
| 1787 | if (trg->size - trg->used < src->used) { |
| 1788 | trg->size = trg->used + src->used; |
| 1789 | |
| 1790 | trg->val.nodes = ly_realloc(trg->val.nodes, trg->size * sizeof *trg->val.nodes); |
| 1791 | LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx), LY_EMEM); |
| 1792 | } |
| 1793 | |
| 1794 | i = 0; |
| 1795 | j = 0; |
| 1796 | count = 0; |
| 1797 | dup_count = 0; |
| 1798 | do { |
| 1799 | cmp = set_sort_compare(&src->val.nodes[i], &trg->val.nodes[j]); |
| 1800 | if (!cmp) { |
| 1801 | if (!count) { |
| 1802 | /* duplicate, just skip it */ |
| 1803 | ++i; |
| 1804 | ++j; |
| 1805 | } else { |
| 1806 | /* we are copying something already, so let's copy the duplicate too, |
| 1807 | * we are hoping that afterwards there are some more nodes to |
| 1808 | * copy and this way we can copy them all together */ |
| 1809 | ++count; |
| 1810 | ++dup_count; |
| 1811 | ++i; |
| 1812 | ++j; |
| 1813 | } |
| 1814 | } else if (cmp < 0) { |
| 1815 | /* inserting src node into trg, just remember it for now */ |
| 1816 | ++count; |
| 1817 | ++i; |
| 1818 | |
| 1819 | /* insert the hash now */ |
| 1820 | set_insert_node_hash(trg, src->val.nodes[i - 1].node, src->val.nodes[i - 1].type); |
| 1821 | } else if (count) { |
| 1822 | copy_nodes: |
| 1823 | /* time to actually copy the nodes, we have found the largest block of nodes */ |
| 1824 | memmove(&trg->val.nodes[j + (count - dup_count)], |
| 1825 | &trg->val.nodes[j], |
| 1826 | (trg->used - j) * sizeof *trg->val.nodes); |
| 1827 | memcpy(&trg->val.nodes[j - dup_count], &src->val.nodes[i - count], count * sizeof *src->val.nodes); |
| 1828 | |
| 1829 | trg->used += count - dup_count; |
| 1830 | /* do not change i, except the copying above, we are basically doing exactly what is in the else branch below */ |
| 1831 | j += count - dup_count; |
| 1832 | |
| 1833 | count = 0; |
| 1834 | dup_count = 0; |
| 1835 | } else { |
| 1836 | ++j; |
| 1837 | } |
| 1838 | } while ((i < src->used) && (j < trg->used)); |
| 1839 | |
| 1840 | if ((i < src->used) || count) { |
| 1841 | /* insert all the hashes first */ |
| 1842 | for (k = i; k < src->used; ++k) { |
| 1843 | set_insert_node_hash(trg, src->val.nodes[k].node, src->val.nodes[k].type); |
| 1844 | } |
| 1845 | |
| 1846 | /* loop ended, but we need to copy something at trg end */ |
| 1847 | count += src->used - i; |
| 1848 | i = src->used; |
| 1849 | goto copy_nodes; |
| 1850 | } |
| 1851 | |
| 1852 | /* we are inserting hashes before the actual node insert, which causes |
| 1853 | * situations when there were initially not enough items for a hash table, |
| 1854 | * but even after some were inserted, hash table was not created (during |
| 1855 | * insertion the number of items is not updated yet) */ |
| 1856 | if (!trg->ht && (trg->used >= LYD_HT_MIN_ITEMS)) { |
| 1857 | set_insert_node_hash(trg, NULL, 0); |
| 1858 | } |
| 1859 | |
| 1860 | #ifndef NDEBUG |
| 1861 | LOGDBG(LY_LDGXPATH, "MERGE result"); |
| 1862 | print_set_debug(trg); |
| 1863 | #endif |
| 1864 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1865 | lyxp_set_free_content(src); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1866 | return LY_SUCCESS; |
| 1867 | } |
| 1868 | |
| 1869 | /* |
| 1870 | * (re)parse functions |
| 1871 | * |
| 1872 | * Parse functions parse the expression into |
| 1873 | * tokens (syntactic analysis). |
| 1874 | * |
| 1875 | * Reparse functions perform semantic analysis |
| 1876 | * (do not save the result, just a check) of |
| 1877 | * the expression and fill repeat indices. |
| 1878 | */ |
| 1879 | |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1880 | LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1881 | 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] | 1882 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1883 | if (exp->used == tok_idx) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1884 | if (ctx) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1885 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF); |
| 1886 | } |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1887 | return LY_EINCOMPLETE; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1888 | } |
| 1889 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1890 | if (want_tok && (exp->tokens[tok_idx] != want_tok)) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1891 | if (ctx) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1892 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1893 | 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] | 1894 | } |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1895 | return LY_ENOT; |
| 1896 | } |
| 1897 | |
| 1898 | return LY_SUCCESS; |
| 1899 | } |
| 1900 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1901 | LY_ERR |
| 1902 | lyxp_next_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok) |
| 1903 | { |
| 1904 | LY_CHECK_RET(lyxp_check_token(ctx, exp, *tok_idx, want_tok)); |
| 1905 | |
| 1906 | /* skip the token */ |
| 1907 | ++(*tok_idx); |
| 1908 | |
| 1909 | return LY_SUCCESS; |
| 1910 | } |
| 1911 | |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1912 | /* just like lyxp_check_token() but tests for 2 tokens */ |
| 1913 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1914 | 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] | 1915 | enum lyxp_token want_tok2) |
| 1916 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1917 | if (exp->used == tok_idx) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1918 | if (ctx) { |
| 1919 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF); |
| 1920 | } |
| 1921 | return LY_EINCOMPLETE; |
| 1922 | } |
| 1923 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1924 | 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] | 1925 | if (ctx) { |
| 1926 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1927 | 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] | 1928 | } |
| 1929 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1930 | } |
| 1931 | |
| 1932 | return LY_SUCCESS; |
| 1933 | } |
| 1934 | |
| 1935 | /** |
| 1936 | * @brief Stack operation push on the repeat array. |
| 1937 | * |
| 1938 | * @param[in] exp Expression to use. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1939 | * @param[in] tok_idx Position in the expresion \p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1940 | * @param[in] repeat_op_idx Index from \p exp of the operator token. This value is pushed. |
| 1941 | */ |
| 1942 | static void |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1943 | 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] | 1944 | { |
| 1945 | uint16_t i; |
| 1946 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1947 | if (exp->repeat[tok_idx]) { |
| 1948 | for (i = 0; exp->repeat[tok_idx][i]; ++i); |
| 1949 | exp->repeat[tok_idx] = realloc(exp->repeat[tok_idx], (i + 2) * sizeof *exp->repeat[tok_idx]); |
| 1950 | LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), ); |
| 1951 | exp->repeat[tok_idx][i] = repeat_op_idx; |
| 1952 | exp->repeat[tok_idx][i + 1] = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1953 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1954 | exp->repeat[tok_idx] = calloc(2, sizeof *exp->repeat[tok_idx]); |
| 1955 | LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), ); |
| 1956 | exp->repeat[tok_idx][0] = repeat_op_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1957 | } |
| 1958 | } |
| 1959 | |
| 1960 | /** |
| 1961 | * @brief Reparse Predicate. Logs directly on error. |
| 1962 | * |
| 1963 | * [7] Predicate ::= '[' Expr ']' |
| 1964 | * |
| 1965 | * @param[in] ctx Context for logging. |
| 1966 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1967 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1968 | * @return LY_ERR |
| 1969 | */ |
| 1970 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1971 | 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] | 1972 | { |
| 1973 | LY_ERR rc; |
| 1974 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1975 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1976 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1977 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1978 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1979 | rc = reparse_or_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1980 | LY_CHECK_RET(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_BRACK2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1983 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1984 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1985 | |
| 1986 | return LY_SUCCESS; |
| 1987 | } |
| 1988 | |
| 1989 | /** |
| 1990 | * @brief Reparse RelativeLocationPath. Logs directly on error. |
| 1991 | * |
| 1992 | * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step |
| 1993 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 1994 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 1995 | * |
| 1996 | * @param[in] ctx Context for logging. |
| 1997 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1998 | * @param[in] tok_idx Position in the expression \p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1999 | * @return LY_ERR (LY_EINCOMPLETE on forward reference) |
| 2000 | */ |
| 2001 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2002 | 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] | 2003 | { |
| 2004 | LY_ERR rc; |
| 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); |
| 2008 | |
| 2009 | goto step; |
| 2010 | do { |
| 2011 | /* '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2012 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2013 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2014 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2015 | LY_CHECK_RET(rc); |
| 2016 | step: |
| 2017 | /* Step */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2018 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2019 | case LYXP_TOKEN_DOT: |
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 | break; |
| 2022 | |
| 2023 | case LYXP_TOKEN_DDOT: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2024 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2025 | break; |
| 2026 | |
| 2027 | case LYXP_TOKEN_AT: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2028 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2029 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2030 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2031 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2032 | 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] | 2033 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2034 | 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] | 2035 | return LY_EVALID; |
| 2036 | } |
| 2037 | /* fall through */ |
| 2038 | case LYXP_TOKEN_NAMETEST: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2039 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2040 | goto reparse_predicate; |
| 2041 | break; |
| 2042 | |
| 2043 | case LYXP_TOKEN_NODETYPE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2044 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2045 | |
| 2046 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2047 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2048 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2049 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2050 | |
| 2051 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2052 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2053 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2054 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2055 | |
| 2056 | reparse_predicate: |
| 2057 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2058 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 2059 | rc = reparse_predicate(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2060 | LY_CHECK_RET(rc); |
| 2061 | } |
| 2062 | break; |
| 2063 | default: |
| 2064 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2065 | 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] | 2066 | return LY_EVALID; |
| 2067 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2068 | } 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] | 2069 | |
| 2070 | return LY_SUCCESS; |
| 2071 | } |
| 2072 | |
| 2073 | /** |
| 2074 | * @brief Reparse AbsoluteLocationPath. Logs directly on error. |
| 2075 | * |
| 2076 | * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath |
| 2077 | * |
| 2078 | * @param[in] ctx Context for logging. |
| 2079 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2080 | * @param[in] tok_idx Position in the expression \p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2081 | * @return LY_ERR |
| 2082 | */ |
| 2083 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2084 | 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] | 2085 | { |
| 2086 | LY_ERR rc; |
| 2087 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2088 | 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] | 2089 | |
| 2090 | /* '/' RelativeLocationPath? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2091 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2092 | /* '/' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2093 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2094 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2095 | if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2096 | return LY_SUCCESS; |
| 2097 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2098 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2099 | case LYXP_TOKEN_DOT: |
| 2100 | case LYXP_TOKEN_DDOT: |
| 2101 | case LYXP_TOKEN_AT: |
| 2102 | case LYXP_TOKEN_NAMETEST: |
| 2103 | case LYXP_TOKEN_NODETYPE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2104 | rc = reparse_relative_location_path(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2105 | LY_CHECK_RET(rc); |
| 2106 | /* fall through */ |
| 2107 | default: |
| 2108 | break; |
| 2109 | } |
| 2110 | |
| 2111 | /* '//' RelativeLocationPath */ |
| 2112 | } else { |
| 2113 | /* '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2114 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2115 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2116 | rc = reparse_relative_location_path(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2117 | LY_CHECK_RET(rc); |
| 2118 | } |
| 2119 | |
| 2120 | return LY_SUCCESS; |
| 2121 | } |
| 2122 | |
| 2123 | /** |
| 2124 | * @brief Reparse FunctionCall. Logs directly on error. |
| 2125 | * |
| 2126 | * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')' |
| 2127 | * |
| 2128 | * @param[in] ctx Context for logging. |
| 2129 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2130 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2131 | * @return LY_ERR |
| 2132 | */ |
| 2133 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2134 | 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] | 2135 | { |
| 2136 | int min_arg_count = -1, max_arg_count, arg_count; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2137 | uint16_t func_tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2138 | LY_ERR rc; |
| 2139 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2140 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2141 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2142 | func_tok_idx = *tok_idx; |
| 2143 | switch (exp->tok_len[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2144 | case 3: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2145 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) { |
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]], "sum", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2149 | min_arg_count = 1; |
| 2150 | max_arg_count = 1; |
| 2151 | } |
| 2152 | break; |
| 2153 | case 4: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2154 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) { |
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]], "last", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2158 | min_arg_count = 0; |
| 2159 | max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2160 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2161 | min_arg_count = 0; |
| 2162 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2163 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2164 | min_arg_count = 0; |
| 2165 | max_arg_count = 0; |
| 2166 | } |
| 2167 | break; |
| 2168 | case 5: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2169 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2170 | min_arg_count = 1; |
| 2171 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2172 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2173 | min_arg_count = 0; |
| 2174 | max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2175 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) { |
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]], "round", 5)) { |
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]], "deref", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2182 | min_arg_count = 1; |
| 2183 | max_arg_count = 1; |
| 2184 | } |
| 2185 | break; |
| 2186 | case 6: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2187 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2188 | min_arg_count = 2; |
Michal Vasko | be2e356 | 2019-10-15 15:35:35 +0200 | [diff] [blame] | 2189 | max_arg_count = INT_MAX; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2190 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2191 | min_arg_count = 0; |
| 2192 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2193 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2194 | min_arg_count = 0; |
| 2195 | max_arg_count = 1; |
| 2196 | } |
| 2197 | break; |
| 2198 | case 7: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2199 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2200 | min_arg_count = 1; |
| 2201 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2202 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2203 | min_arg_count = 1; |
| 2204 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2205 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2206 | min_arg_count = 0; |
| 2207 | max_arg_count = 0; |
| 2208 | } |
| 2209 | break; |
| 2210 | case 8: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2211 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2212 | min_arg_count = 2; |
| 2213 | max_arg_count = 2; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2214 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2215 | min_arg_count = 0; |
| 2216 | max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2217 | } 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] | 2218 | min_arg_count = 2; |
| 2219 | max_arg_count = 2; |
| 2220 | } |
| 2221 | break; |
| 2222 | case 9: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2223 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2224 | min_arg_count = 2; |
| 2225 | max_arg_count = 3; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2226 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2227 | min_arg_count = 3; |
| 2228 | max_arg_count = 3; |
| 2229 | } |
| 2230 | break; |
| 2231 | case 10: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2232 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) { |
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]], "enum-value", 10)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2236 | min_arg_count = 1; |
| 2237 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2238 | } 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] | 2239 | min_arg_count = 2; |
| 2240 | max_arg_count = 2; |
| 2241 | } |
| 2242 | break; |
| 2243 | case 11: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2244 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) { |
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 12: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2250 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) { |
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 13: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2256 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2257 | min_arg_count = 0; |
| 2258 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2259 | } 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] | 2260 | min_arg_count = 0; |
| 2261 | max_arg_count = 1; |
| 2262 | } |
| 2263 | break; |
| 2264 | case 15: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2265 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2266 | min_arg_count = 0; |
| 2267 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2268 | } 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] | 2269 | min_arg_count = 2; |
| 2270 | max_arg_count = 2; |
| 2271 | } |
| 2272 | break; |
| 2273 | case 16: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2274 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2275 | min_arg_count = 2; |
| 2276 | max_arg_count = 2; |
| 2277 | } |
| 2278 | break; |
| 2279 | case 20: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2280 | 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] | 2281 | min_arg_count = 2; |
| 2282 | max_arg_count = 2; |
| 2283 | } |
| 2284 | break; |
| 2285 | } |
| 2286 | if (min_arg_count == -1) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2287 | 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] | 2288 | return LY_EINVAL; |
| 2289 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2290 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2291 | |
| 2292 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2293 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2294 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2295 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2296 | |
| 2297 | /* ( Expr ( ',' Expr )* )? */ |
| 2298 | arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2299 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2300 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2301 | if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2302 | ++arg_count; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2303 | rc = reparse_or_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2304 | LY_CHECK_RET(rc); |
| 2305 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2306 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) { |
| 2307 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2308 | |
| 2309 | ++arg_count; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2310 | rc = reparse_or_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2311 | LY_CHECK_RET(rc); |
| 2312 | } |
| 2313 | |
| 2314 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2315 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2316 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2317 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2318 | |
| 2319 | if ((arg_count < min_arg_count) || (arg_count > max_arg_count)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2320 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INARGCOUNT, arg_count, exp->tok_len[func_tok_idx], |
| 2321 | &exp->expr[exp->tok_pos[func_tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2322 | return LY_EVALID; |
| 2323 | } |
| 2324 | |
| 2325 | return LY_SUCCESS; |
| 2326 | } |
| 2327 | |
| 2328 | /** |
| 2329 | * @brief Reparse PathExpr. Logs directly on error. |
| 2330 | * |
| 2331 | * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate* |
| 2332 | * | PrimaryExpr Predicate* '/' RelativeLocationPath |
| 2333 | * | PrimaryExpr Predicate* '//' RelativeLocationPath |
| 2334 | * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath |
| 2335 | * [8] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall |
| 2336 | * |
| 2337 | * @param[in] ctx Context for logging. |
| 2338 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2339 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2340 | * @return LY_ERR |
| 2341 | */ |
| 2342 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2343 | 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] | 2344 | { |
| 2345 | LY_ERR rc; |
| 2346 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2347 | if (lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 2348 | return LY_EVALID; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2349 | } |
| 2350 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2351 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2352 | case LYXP_TOKEN_PAR1: |
| 2353 | /* '(' Expr ')' Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2354 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2355 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2356 | rc = reparse_or_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2357 | LY_CHECK_RET(rc); |
| 2358 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2359 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2360 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2361 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2362 | goto predicate; |
| 2363 | break; |
| 2364 | case LYXP_TOKEN_DOT: |
| 2365 | case LYXP_TOKEN_DDOT: |
| 2366 | case LYXP_TOKEN_AT: |
| 2367 | case LYXP_TOKEN_NAMETEST: |
| 2368 | case LYXP_TOKEN_NODETYPE: |
| 2369 | /* RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2370 | rc = reparse_relative_location_path(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2371 | LY_CHECK_RET(rc); |
| 2372 | break; |
| 2373 | case LYXP_TOKEN_FUNCNAME: |
| 2374 | /* FunctionCall */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2375 | rc = reparse_function_call(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2376 | LY_CHECK_RET(rc); |
| 2377 | goto predicate; |
| 2378 | break; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2379 | case LYXP_TOKEN_OPER_PATH: |
| 2380 | case LYXP_TOKEN_OPER_RPATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2381 | /* AbsoluteLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2382 | rc = reparse_absolute_location_path(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2383 | LY_CHECK_RET(rc); |
| 2384 | break; |
| 2385 | case LYXP_TOKEN_LITERAL: |
| 2386 | /* Literal */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2387 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2388 | goto predicate; |
| 2389 | break; |
| 2390 | case LYXP_TOKEN_NUMBER: |
| 2391 | /* Number */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2392 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2393 | goto predicate; |
| 2394 | break; |
| 2395 | default: |
| 2396 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2397 | 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] | 2398 | return LY_EVALID; |
| 2399 | } |
| 2400 | |
| 2401 | return LY_SUCCESS; |
| 2402 | |
| 2403 | predicate: |
| 2404 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2405 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 2406 | rc = reparse_predicate(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2407 | LY_CHECK_RET(rc); |
| 2408 | } |
| 2409 | |
| 2410 | /* ('/' or '//') RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2411 | 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] | 2412 | |
| 2413 | /* '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2414 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2415 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2416 | rc = reparse_relative_location_path(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2417 | LY_CHECK_RET(rc); |
| 2418 | } |
| 2419 | |
| 2420 | return LY_SUCCESS; |
| 2421 | } |
| 2422 | |
| 2423 | /** |
| 2424 | * @brief Reparse UnaryExpr. Logs directly on error. |
| 2425 | * |
| 2426 | * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr |
| 2427 | * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr |
| 2428 | * |
| 2429 | * @param[in] ctx Context for logging. |
| 2430 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2431 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2432 | * @return LY_ERR |
| 2433 | */ |
| 2434 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2435 | 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] | 2436 | { |
| 2437 | uint16_t prev_exp; |
| 2438 | LY_ERR rc; |
| 2439 | |
| 2440 | /* ('-')* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2441 | prev_exp = *tok_idx; |
| 2442 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) |
| 2443 | && (exp->expr[exp->tok_pos[*tok_idx]] == '-')) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2444 | exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2445 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2446 | } |
| 2447 | |
| 2448 | /* PathExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2449 | prev_exp = *tok_idx; |
| 2450 | rc = reparse_path_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2451 | LY_CHECK_RET(rc); |
| 2452 | |
| 2453 | /* ('|' PathExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2454 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_UNI)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2455 | exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2456 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2457 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2458 | rc = reparse_path_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2459 | LY_CHECK_RET(rc); |
| 2460 | } |
| 2461 | |
| 2462 | return LY_SUCCESS; |
| 2463 | } |
| 2464 | |
| 2465 | /** |
| 2466 | * @brief Reparse AdditiveExpr. Logs directly on error. |
| 2467 | * |
| 2468 | * [15] AdditiveExpr ::= MultiplicativeExpr |
| 2469 | * | AdditiveExpr '+' MultiplicativeExpr |
| 2470 | * | AdditiveExpr '-' MultiplicativeExpr |
| 2471 | * [16] MultiplicativeExpr ::= UnaryExpr |
| 2472 | * | MultiplicativeExpr '*' UnaryExpr |
| 2473 | * | MultiplicativeExpr 'div' UnaryExpr |
| 2474 | * | MultiplicativeExpr 'mod' UnaryExpr |
| 2475 | * |
| 2476 | * @param[in] ctx Context for logging. |
| 2477 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2478 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2479 | * @return LY_ERR |
| 2480 | */ |
| 2481 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2482 | 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] | 2483 | { |
| 2484 | uint16_t prev_add_exp, prev_mul_exp; |
| 2485 | LY_ERR rc; |
| 2486 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2487 | prev_add_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2488 | goto reparse_multiplicative_expr; |
| 2489 | |
| 2490 | /* ('+' / '-' MultiplicativeExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2491 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) |
| 2492 | && ((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] | 2493 | exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2494 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2495 | |
| 2496 | reparse_multiplicative_expr: |
| 2497 | /* UnaryExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2498 | prev_mul_exp = *tok_idx; |
| 2499 | rc = reparse_unary_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2500 | LY_CHECK_RET(rc); |
| 2501 | |
| 2502 | /* ('*' / 'div' / 'mod' UnaryExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2503 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) |
| 2504 | && ((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] | 2505 | exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2506 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2507 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2508 | rc = reparse_unary_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2509 | LY_CHECK_RET(rc); |
| 2510 | } |
| 2511 | } |
| 2512 | |
| 2513 | return LY_SUCCESS; |
| 2514 | } |
| 2515 | |
| 2516 | /** |
| 2517 | * @brief Reparse EqualityExpr. Logs directly on error. |
| 2518 | * |
| 2519 | * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr |
| 2520 | * | EqualityExpr '!=' RelationalExpr |
| 2521 | * [14] RelationalExpr ::= AdditiveExpr |
| 2522 | * | RelationalExpr '<' AdditiveExpr |
| 2523 | * | RelationalExpr '>' AdditiveExpr |
| 2524 | * | RelationalExpr '<=' AdditiveExpr |
| 2525 | * | RelationalExpr '>=' AdditiveExpr |
| 2526 | * |
| 2527 | * @param[in] ctx Context for logging. |
| 2528 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2529 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2530 | * @return LY_ERR |
| 2531 | */ |
| 2532 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2533 | 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] | 2534 | { |
| 2535 | uint16_t prev_eq_exp, prev_rel_exp; |
| 2536 | LY_ERR rc; |
| 2537 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2538 | prev_eq_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2539 | goto reparse_additive_expr; |
| 2540 | |
| 2541 | /* ('=' / '!=' RelationalExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2542 | 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] | 2543 | exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2544 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2545 | |
| 2546 | reparse_additive_expr: |
| 2547 | /* AdditiveExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2548 | prev_rel_exp = *tok_idx; |
| 2549 | rc = reparse_additive_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2550 | LY_CHECK_RET(rc); |
| 2551 | |
| 2552 | /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2553 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_COMP)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2554 | exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2555 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2556 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2557 | rc = reparse_additive_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2558 | LY_CHECK_RET(rc); |
| 2559 | } |
| 2560 | } |
| 2561 | |
| 2562 | return LY_SUCCESS; |
| 2563 | } |
| 2564 | |
| 2565 | /** |
| 2566 | * @brief Reparse OrExpr. Logs directly on error. |
| 2567 | * |
| 2568 | * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr |
| 2569 | * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr |
| 2570 | * |
| 2571 | * @param[in] ctx Context for logging. |
| 2572 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2573 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2574 | * @return LY_ERR |
| 2575 | */ |
| 2576 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2577 | 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] | 2578 | { |
| 2579 | uint16_t prev_or_exp, prev_and_exp; |
| 2580 | LY_ERR rc; |
| 2581 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2582 | prev_or_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2583 | goto reparse_equality_expr; |
| 2584 | |
| 2585 | /* ('or' AndExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2586 | 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] | 2587 | exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2588 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2589 | |
| 2590 | reparse_equality_expr: |
| 2591 | /* EqualityExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2592 | prev_and_exp = *tok_idx; |
| 2593 | rc = reparse_equality_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2594 | LY_CHECK_RET(rc); |
| 2595 | |
| 2596 | /* ('and' EqualityExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2597 | 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] | 2598 | exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2599 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2600 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2601 | rc = reparse_equality_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2602 | LY_CHECK_RET(rc); |
| 2603 | } |
| 2604 | } |
| 2605 | |
| 2606 | return LY_SUCCESS; |
| 2607 | } |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2608 | |
| 2609 | /** |
| 2610 | * @brief Parse NCName. |
| 2611 | * |
| 2612 | * @param[in] ncname Name to parse. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2613 | * @return Length of @p ncname valid bytes. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2614 | */ |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2615 | static long int |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2616 | parse_ncname(const char *ncname) |
| 2617 | { |
| 2618 | unsigned int uc; |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2619 | size_t size; |
| 2620 | long int len = 0; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2621 | |
| 2622 | LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0); |
| 2623 | if (!is_xmlqnamestartchar(uc) || (uc == ':')) { |
| 2624 | return len; |
| 2625 | } |
| 2626 | |
| 2627 | do { |
| 2628 | len += size; |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 2629 | if (!*ncname) { |
| 2630 | break; |
| 2631 | } |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2632 | LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2633 | } while (is_xmlqnamechar(uc) && (uc != ':')); |
| 2634 | |
| 2635 | return len; |
| 2636 | } |
| 2637 | |
| 2638 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2639 | * @brief Add @p token into the expression @p exp. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2640 | * |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2641 | * @param[in] ctx Context for logging. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2642 | * @param[in] exp Expression to use. |
| 2643 | * @param[in] token Token to add. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2644 | * @param[in] tok_pos Token position in the XPath expression. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2645 | * @param[in] tok_len Token length in the XPath expression. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2646 | * @return LY_ERR |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2647 | */ |
| 2648 | static LY_ERR |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 2649 | 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] | 2650 | { |
| 2651 | uint32_t prev; |
| 2652 | |
| 2653 | if (exp->used == exp->size) { |
| 2654 | prev = exp->size; |
| 2655 | exp->size += LYXP_EXPR_SIZE_STEP; |
| 2656 | if (prev > exp->size) { |
| 2657 | LOGINT(ctx); |
| 2658 | return LY_EINT; |
| 2659 | } |
| 2660 | |
| 2661 | exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens); |
| 2662 | LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM); |
| 2663 | exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos); |
| 2664 | LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM); |
| 2665 | exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len); |
| 2666 | LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM); |
| 2667 | } |
| 2668 | |
| 2669 | exp->tokens[exp->used] = token; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2670 | exp->tok_pos[exp->used] = tok_pos; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2671 | exp->tok_len[exp->used] = tok_len; |
| 2672 | ++exp->used; |
| 2673 | return LY_SUCCESS; |
| 2674 | } |
| 2675 | |
| 2676 | void |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 2677 | lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr) |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2678 | { |
| 2679 | uint16_t i; |
| 2680 | |
| 2681 | if (!expr) { |
| 2682 | return; |
| 2683 | } |
| 2684 | |
| 2685 | lydict_remove(ctx, expr->expr); |
| 2686 | free(expr->tokens); |
| 2687 | free(expr->tok_pos); |
| 2688 | free(expr->tok_len); |
| 2689 | if (expr->repeat) { |
| 2690 | for (i = 0; i < expr->used; ++i) { |
| 2691 | free(expr->repeat[i]); |
| 2692 | } |
| 2693 | } |
| 2694 | free(expr->repeat); |
| 2695 | free(expr); |
| 2696 | } |
| 2697 | |
| 2698 | struct lyxp_expr * |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2699 | 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] | 2700 | { |
| 2701 | struct lyxp_expr *ret; |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2702 | size_t parsed = 0, tok_len; |
| 2703 | long int ncname_len; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2704 | enum lyxp_token tok_type; |
| 2705 | int prev_function_check = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2706 | uint16_t tok_idx = 0; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2707 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2708 | if (!expr[0]) { |
| 2709 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF); |
| 2710 | return NULL; |
| 2711 | } |
| 2712 | |
| 2713 | if (!expr_len) { |
| 2714 | expr_len = strlen(expr); |
| 2715 | } |
| 2716 | if (expr_len > UINT16_MAX) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2717 | LOGERR(ctx, LY_EINVAL, "XPath expression cannot be longer than %ud characters.", UINT16_MAX); |
| 2718 | return NULL; |
| 2719 | } |
| 2720 | |
| 2721 | /* init lyxp_expr structure */ |
| 2722 | ret = calloc(1, sizeof *ret); |
| 2723 | LY_CHECK_ERR_GOTO(!ret, LOGMEM(ctx), error); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2724 | ret->expr = lydict_insert(ctx, expr, expr_len); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2725 | LY_CHECK_ERR_GOTO(!ret->expr, LOGMEM(ctx), error); |
| 2726 | ret->used = 0; |
| 2727 | ret->size = LYXP_EXPR_SIZE_START; |
| 2728 | ret->tokens = malloc(ret->size * sizeof *ret->tokens); |
| 2729 | LY_CHECK_ERR_GOTO(!ret->tokens, LOGMEM(ctx), error); |
| 2730 | |
| 2731 | ret->tok_pos = malloc(ret->size * sizeof *ret->tok_pos); |
| 2732 | LY_CHECK_ERR_GOTO(!ret->tok_pos, LOGMEM(ctx), error); |
| 2733 | |
| 2734 | ret->tok_len = malloc(ret->size * sizeof *ret->tok_len); |
| 2735 | LY_CHECK_ERR_GOTO(!ret->tok_len, LOGMEM(ctx), error); |
| 2736 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2737 | /* make expr 0-terminated */ |
| 2738 | expr = ret->expr; |
| 2739 | |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2740 | while (is_xmlws(expr[parsed])) { |
| 2741 | ++parsed; |
| 2742 | } |
| 2743 | |
| 2744 | do { |
| 2745 | if (expr[parsed] == '(') { |
| 2746 | |
| 2747 | /* '(' */ |
| 2748 | tok_len = 1; |
| 2749 | tok_type = LYXP_TOKEN_PAR1; |
| 2750 | |
| 2751 | if (prev_function_check && ret->used && (ret->tokens[ret->used - 1] == LYXP_TOKEN_NAMETEST)) { |
| 2752 | /* it is a NodeType/FunctionName after all */ |
| 2753 | if (((ret->tok_len[ret->used - 1] == 4) |
| 2754 | && (!strncmp(&expr[ret->tok_pos[ret->used - 1]], "node", 4) |
| 2755 | || !strncmp(&expr[ret->tok_pos[ret->used - 1]], "text", 4))) || |
| 2756 | ((ret->tok_len[ret->used - 1] == 7) |
| 2757 | && !strncmp(&expr[ret->tok_pos[ret->used - 1]], "comment", 7))) { |
| 2758 | ret->tokens[ret->used - 1] = LYXP_TOKEN_NODETYPE; |
| 2759 | } else { |
| 2760 | ret->tokens[ret->used - 1] = LYXP_TOKEN_FUNCNAME; |
| 2761 | } |
| 2762 | prev_function_check = 0; |
| 2763 | } |
| 2764 | |
| 2765 | } else if (expr[parsed] == ')') { |
| 2766 | |
| 2767 | /* ')' */ |
| 2768 | tok_len = 1; |
| 2769 | tok_type = LYXP_TOKEN_PAR2; |
| 2770 | |
| 2771 | } else if (expr[parsed] == '[') { |
| 2772 | |
| 2773 | /* '[' */ |
| 2774 | tok_len = 1; |
| 2775 | tok_type = LYXP_TOKEN_BRACK1; |
| 2776 | |
| 2777 | } else if (expr[parsed] == ']') { |
| 2778 | |
| 2779 | /* ']' */ |
| 2780 | tok_len = 1; |
| 2781 | tok_type = LYXP_TOKEN_BRACK2; |
| 2782 | |
| 2783 | } else if (!strncmp(&expr[parsed], "..", 2)) { |
| 2784 | |
| 2785 | /* '..' */ |
| 2786 | tok_len = 2; |
| 2787 | tok_type = LYXP_TOKEN_DDOT; |
| 2788 | |
| 2789 | } else if ((expr[parsed] == '.') && (!isdigit(expr[parsed + 1]))) { |
| 2790 | |
| 2791 | /* '.' */ |
| 2792 | tok_len = 1; |
| 2793 | tok_type = LYXP_TOKEN_DOT; |
| 2794 | |
| 2795 | } else if (expr[parsed] == '@') { |
| 2796 | |
| 2797 | /* '@' */ |
| 2798 | tok_len = 1; |
| 2799 | tok_type = LYXP_TOKEN_AT; |
| 2800 | |
| 2801 | } else if (expr[parsed] == ',') { |
| 2802 | |
| 2803 | /* ',' */ |
| 2804 | tok_len = 1; |
| 2805 | tok_type = LYXP_TOKEN_COMMA; |
| 2806 | |
| 2807 | } else if (expr[parsed] == '\'') { |
| 2808 | |
| 2809 | /* Literal with ' */ |
| 2810 | for (tok_len = 1; (expr[parsed + tok_len] != '\0') && (expr[parsed + tok_len] != '\''); ++tok_len); |
| 2811 | LY_CHECK_ERR_GOTO(expr[parsed + tok_len] == '\0', |
| 2812 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOE, expr[parsed], &expr[parsed]), error); |
| 2813 | ++tok_len; |
| 2814 | tok_type = LYXP_TOKEN_LITERAL; |
| 2815 | |
| 2816 | } else if (expr[parsed] == '\"') { |
| 2817 | |
| 2818 | /* Literal with " */ |
| 2819 | for (tok_len = 1; (expr[parsed + tok_len] != '\0') && (expr[parsed + tok_len] != '\"'); ++tok_len); |
| 2820 | LY_CHECK_ERR_GOTO(expr[parsed + tok_len] == '\0', |
| 2821 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOE, expr[parsed], &expr[parsed]), error); |
| 2822 | ++tok_len; |
| 2823 | tok_type = LYXP_TOKEN_LITERAL; |
| 2824 | |
| 2825 | } else if ((expr[parsed] == '.') || (isdigit(expr[parsed]))) { |
| 2826 | |
| 2827 | /* Number */ |
| 2828 | for (tok_len = 0; isdigit(expr[parsed + tok_len]); ++tok_len); |
| 2829 | if (expr[parsed + tok_len] == '.') { |
| 2830 | ++tok_len; |
| 2831 | for (; isdigit(expr[parsed + tok_len]); ++tok_len); |
| 2832 | } |
| 2833 | tok_type = LYXP_TOKEN_NUMBER; |
| 2834 | |
| 2835 | } else if (expr[parsed] == '/') { |
| 2836 | |
| 2837 | /* Operator '/', '//' */ |
| 2838 | if (!strncmp(&expr[parsed], "//", 2)) { |
| 2839 | tok_len = 2; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2840 | tok_type = LYXP_TOKEN_OPER_RPATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2841 | } else { |
| 2842 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2843 | tok_type = LYXP_TOKEN_OPER_PATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2844 | } |
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 (!strncmp(&expr[parsed], "!=", 2)) { |
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 = 2; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2850 | tok_type = LYXP_TOKEN_OPER_NEQUAL; |
| 2851 | |
| 2852 | } else if (!strncmp(&expr[parsed], "<=", 2) || !strncmp(&expr[parsed], ">=", 2)) { |
| 2853 | |
| 2854 | /* Operator '<=', '>=' */ |
| 2855 | tok_len = 2; |
| 2856 | tok_type = LYXP_TOKEN_OPER_COMP; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2857 | |
| 2858 | } else if (expr[parsed] == '|') { |
| 2859 | |
| 2860 | /* Operator '|' */ |
| 2861 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2862 | tok_type = LYXP_TOKEN_OPER_UNI; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2863 | |
| 2864 | } else if ((expr[parsed] == '+') || (expr[parsed] == '-')) { |
| 2865 | |
| 2866 | /* Operator '+', '-' */ |
| 2867 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2868 | tok_type = LYXP_TOKEN_OPER_MATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2869 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2870 | } else if (expr[parsed] == '=') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2871 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2872 | /* Operator '=' */ |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2873 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2874 | tok_type = LYXP_TOKEN_OPER_EQUAL; |
| 2875 | |
| 2876 | } else if ((expr[parsed] == '<') || (expr[parsed] == '>')) { |
| 2877 | |
| 2878 | /* Operator '<', '>' */ |
| 2879 | tok_len = 1; |
| 2880 | tok_type = LYXP_TOKEN_OPER_COMP; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2881 | |
| 2882 | } else if (ret->used && (ret->tokens[ret->used - 1] != LYXP_TOKEN_AT) |
| 2883 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_PAR1) |
| 2884 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_BRACK1) |
| 2885 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_COMMA) |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2886 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_LOG) |
| 2887 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_EQUAL) |
| 2888 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_NEQUAL) |
| 2889 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_COMP) |
| 2890 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_MATH) |
| 2891 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_UNI) |
| 2892 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_PATH) |
| 2893 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_RPATH)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2894 | |
| 2895 | /* Operator '*', 'or', 'and', 'mod', or 'div' */ |
| 2896 | if (expr[parsed] == '*') { |
| 2897 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2898 | tok_type = LYXP_TOKEN_OPER_MATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2899 | |
| 2900 | } else if (!strncmp(&expr[parsed], "or", 2)) { |
| 2901 | tok_len = 2; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2902 | tok_type = LYXP_TOKEN_OPER_LOG; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2903 | |
| 2904 | } else if (!strncmp(&expr[parsed], "and", 3)) { |
| 2905 | tok_len = 3; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2906 | tok_type = LYXP_TOKEN_OPER_LOG; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2907 | |
| 2908 | } else if (!strncmp(&expr[parsed], "mod", 3) || !strncmp(&expr[parsed], "div", 3)) { |
| 2909 | tok_len = 3; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2910 | tok_type = LYXP_TOKEN_OPER_MATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2911 | |
| 2912 | } else if (prev_function_check) { |
Michal Vasko | 5307857 | 2019-05-24 08:50:15 +0200 | [diff] [blame] | 2913 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Invalid character 0x%x ('%c'), perhaps \"%.*s\" is supposed to be a function call.", |
| 2914 | 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] | 2915 | goto error; |
| 2916 | } else { |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2917 | 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] | 2918 | goto error; |
| 2919 | } |
| 2920 | } else if (expr[parsed] == '*') { |
| 2921 | |
| 2922 | /* NameTest '*' */ |
| 2923 | tok_len = 1; |
| 2924 | tok_type = LYXP_TOKEN_NAMETEST; |
| 2925 | |
| 2926 | } else { |
| 2927 | |
| 2928 | /* NameTest (NCName ':' '*' | QName) or NodeType/FunctionName */ |
| 2929 | ncname_len = parse_ncname(&expr[parsed]); |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2930 | 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] | 2931 | tok_len = ncname_len; |
| 2932 | |
| 2933 | if (expr[parsed + tok_len] == ':') { |
| 2934 | ++tok_len; |
| 2935 | if (expr[parsed + tok_len] == '*') { |
| 2936 | ++tok_len; |
| 2937 | } else { |
| 2938 | ncname_len = parse_ncname(&expr[parsed + tok_len]); |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2939 | 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] | 2940 | tok_len += ncname_len; |
| 2941 | } |
| 2942 | /* remove old flag to prevent ambiguities */ |
| 2943 | prev_function_check = 0; |
| 2944 | tok_type = LYXP_TOKEN_NAMETEST; |
| 2945 | } else { |
| 2946 | /* there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */ |
| 2947 | prev_function_check = 1; |
| 2948 | tok_type = LYXP_TOKEN_NAMETEST; |
| 2949 | } |
| 2950 | } |
| 2951 | |
| 2952 | /* store the token, move on to the next one */ |
| 2953 | LY_CHECK_GOTO(exp_add_token(ctx, ret, tok_type, parsed, tok_len), error); |
| 2954 | parsed += tok_len; |
| 2955 | while (is_xmlws(expr[parsed])) { |
| 2956 | ++parsed; |
| 2957 | } |
| 2958 | |
| 2959 | } while (expr[parsed]); |
| 2960 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2961 | if (reparse) { |
| 2962 | /* prealloc repeat */ |
| 2963 | ret->repeat = calloc(ret->size, sizeof *ret->repeat); |
| 2964 | LY_CHECK_ERR_GOTO(!ret->repeat, LOGMEM(ctx), error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2965 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2966 | /* fill repeat */ |
| 2967 | LY_CHECK_GOTO(reparse_or_expr(ctx, ret, &tok_idx), error); |
| 2968 | if (ret->used > tok_idx) { |
| 2969 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Unparsed characters \"%s\" left at the end of an XPath expression.", |
| 2970 | &ret->expr[ret->tok_pos[tok_idx]]); |
| 2971 | goto error; |
| 2972 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2973 | } |
| 2974 | |
| 2975 | print_expr_struct_debug(ret); |
| 2976 | |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2977 | return ret; |
| 2978 | |
| 2979 | error: |
| 2980 | lyxp_expr_free(ctx, ret); |
| 2981 | return NULL; |
| 2982 | } |
| 2983 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2984 | struct lyxp_expr * |
| 2985 | lyxp_expr_dup(const struct ly_ctx *ctx, const struct lyxp_expr *exp) |
| 2986 | { |
| 2987 | struct lyxp_expr *dup; |
| 2988 | uint32_t i, j; |
| 2989 | |
| 2990 | dup = calloc(1, sizeof *dup); |
| 2991 | LY_CHECK_ERR_GOTO(!dup, LOGMEM(ctx), error); |
| 2992 | |
| 2993 | dup->tokens = malloc(exp->used * sizeof *dup->tokens); |
| 2994 | LY_CHECK_ERR_GOTO(!dup->tokens, LOGMEM(ctx), error); |
| 2995 | memcpy(dup->tokens, exp->tokens, exp->used * sizeof *dup->tokens); |
| 2996 | |
| 2997 | dup->tok_pos = malloc(exp->used * sizeof *dup->tok_pos); |
| 2998 | LY_CHECK_ERR_GOTO(!dup->tok_pos, LOGMEM(ctx), error); |
| 2999 | memcpy(dup->tok_pos, exp->tok_pos, exp->used * sizeof *dup->tok_pos); |
| 3000 | |
| 3001 | dup->tok_len = malloc(exp->used * sizeof *dup->tok_len); |
| 3002 | LY_CHECK_ERR_GOTO(!dup->tok_len, LOGMEM(ctx), error); |
| 3003 | memcpy(dup->tok_len, exp->tok_len, exp->used * sizeof *dup->tok_len); |
| 3004 | |
| 3005 | dup->repeat = malloc(exp->used * sizeof *dup->repeat); |
| 3006 | LY_CHECK_ERR_GOTO(!dup->repeat, LOGMEM(ctx), error); |
| 3007 | for (i = 0; i < exp->used; ++i) { |
| 3008 | if (!exp->repeat[i]) { |
| 3009 | dup->repeat[i] = NULL; |
| 3010 | } else { |
| 3011 | for (j = 0; exp->repeat[i][j]; ++j); |
| 3012 | /* the ending 0 as well */ |
| 3013 | ++j; |
| 3014 | |
Michal Vasko | 99c7164 | 2020-07-03 13:33:36 +0200 | [diff] [blame] | 3015 | dup->repeat[i] = malloc(j * sizeof **dup->repeat); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3016 | LY_CHECK_ERR_GOTO(!dup->repeat[i], LOGMEM(ctx), error); |
| 3017 | memcpy(dup->repeat[i], exp->repeat[i], j * sizeof **dup->repeat); |
| 3018 | dup->repeat[i][j - 1] = 0; |
| 3019 | } |
| 3020 | } |
| 3021 | |
| 3022 | dup->used = exp->used; |
| 3023 | dup->size = exp->used; |
| 3024 | dup->expr = lydict_insert(ctx, exp->expr, 0); |
| 3025 | |
| 3026 | return dup; |
| 3027 | |
| 3028 | error: |
| 3029 | lyxp_expr_free(ctx, dup); |
| 3030 | return NULL; |
| 3031 | } |
| 3032 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3033 | /* |
| 3034 | * warn functions |
| 3035 | * |
| 3036 | * Warn functions check specific reasonable conditions for schema XPath |
| 3037 | * and print a warning if they are not satisfied. |
| 3038 | */ |
| 3039 | |
| 3040 | /** |
| 3041 | * @brief Get the last-added schema node that is currently in the context. |
| 3042 | * |
| 3043 | * @param[in] set Set to search in. |
| 3044 | * @return Last-added schema context node, NULL if no node is in context. |
| 3045 | */ |
| 3046 | static struct lysc_node * |
| 3047 | warn_get_scnode_in_ctx(struct lyxp_set *set) |
| 3048 | { |
| 3049 | uint32_t i; |
| 3050 | |
| 3051 | if (!set || (set->type != LYXP_SET_SCNODE_SET)) { |
| 3052 | return NULL; |
| 3053 | } |
| 3054 | |
| 3055 | i = set->used; |
| 3056 | do { |
| 3057 | --i; |
| 3058 | if (set->val.scnodes[i].in_ctx == 1) { |
| 3059 | /* if there are more, simply return the first found (last added) */ |
| 3060 | return set->val.scnodes[i].scnode; |
| 3061 | } |
| 3062 | } while (i); |
| 3063 | |
| 3064 | return NULL; |
| 3065 | } |
| 3066 | |
| 3067 | /** |
| 3068 | * @brief Test whether a type is numeric - integer type or decimal64. |
| 3069 | * |
| 3070 | * @param[in] type Type to test. |
| 3071 | * @return 1 if numeric, 0 otherwise. |
| 3072 | */ |
| 3073 | static int |
| 3074 | warn_is_numeric_type(struct lysc_type *type) |
| 3075 | { |
| 3076 | struct lysc_type_union *uni; |
| 3077 | int ret; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3078 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3079 | |
| 3080 | switch (type->basetype) { |
| 3081 | case LY_TYPE_DEC64: |
| 3082 | case LY_TYPE_INT8: |
| 3083 | case LY_TYPE_UINT8: |
| 3084 | case LY_TYPE_INT16: |
| 3085 | case LY_TYPE_UINT16: |
| 3086 | case LY_TYPE_INT32: |
| 3087 | case LY_TYPE_UINT32: |
| 3088 | case LY_TYPE_INT64: |
| 3089 | case LY_TYPE_UINT64: |
| 3090 | return 1; |
| 3091 | case LY_TYPE_UNION: |
| 3092 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3093 | LY_ARRAY_FOR(uni->types, u) { |
| 3094 | ret = warn_is_numeric_type(uni->types[u]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3095 | if (ret) { |
| 3096 | /* found a suitable type */ |
| 3097 | return 1; |
| 3098 | } |
| 3099 | } |
| 3100 | /* did not find any suitable type */ |
| 3101 | return 0; |
| 3102 | case LY_TYPE_LEAFREF: |
| 3103 | return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype); |
| 3104 | default: |
| 3105 | return 0; |
| 3106 | } |
| 3107 | } |
| 3108 | |
| 3109 | /** |
| 3110 | * @brief Test whether a type is string-like - no integers, decimal64 or binary. |
| 3111 | * |
| 3112 | * @param[in] type Type to test. |
| 3113 | * @return 1 if string, 0 otherwise. |
| 3114 | */ |
| 3115 | static int |
| 3116 | warn_is_string_type(struct lysc_type *type) |
| 3117 | { |
| 3118 | struct lysc_type_union *uni; |
| 3119 | int ret; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3120 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3121 | |
| 3122 | switch (type->basetype) { |
| 3123 | case LY_TYPE_BITS: |
| 3124 | case LY_TYPE_ENUM: |
| 3125 | case LY_TYPE_IDENT: |
| 3126 | case LY_TYPE_INST: |
| 3127 | case LY_TYPE_STRING: |
| 3128 | return 1; |
| 3129 | case LY_TYPE_UNION: |
| 3130 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3131 | LY_ARRAY_FOR(uni->types, u) { |
| 3132 | ret = warn_is_string_type(uni->types[u]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3133 | if (ret) { |
| 3134 | /* found a suitable type */ |
| 3135 | return 1; |
| 3136 | } |
| 3137 | } |
| 3138 | /* did not find any suitable type */ |
| 3139 | return 0; |
| 3140 | case LY_TYPE_LEAFREF: |
| 3141 | return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype); |
| 3142 | default: |
| 3143 | return 0; |
| 3144 | } |
| 3145 | } |
| 3146 | |
| 3147 | /** |
| 3148 | * @brief Test whether a type is one specific type. |
| 3149 | * |
| 3150 | * @param[in] type Type to test. |
| 3151 | * @param[in] base Expected type. |
| 3152 | * @return 1 if it is, 0 otherwise. |
| 3153 | */ |
| 3154 | static int |
| 3155 | warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base) |
| 3156 | { |
| 3157 | struct lysc_type_union *uni; |
| 3158 | int ret; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3159 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3160 | |
| 3161 | if (type->basetype == base) { |
| 3162 | return 1; |
| 3163 | } else if (type->basetype == LY_TYPE_UNION) { |
| 3164 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3165 | LY_ARRAY_FOR(uni->types, u) { |
| 3166 | ret = warn_is_specific_type(uni->types[u], base); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3167 | if (ret) { |
| 3168 | /* found a suitable type */ |
| 3169 | return 1; |
| 3170 | } |
| 3171 | } |
| 3172 | /* did not find any suitable type */ |
| 3173 | return 0; |
| 3174 | } else if (type->basetype == LY_TYPE_LEAFREF) { |
| 3175 | return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base); |
| 3176 | } |
| 3177 | |
| 3178 | return 0; |
| 3179 | } |
| 3180 | |
| 3181 | /** |
| 3182 | * @brief Get next type of a (union) type. |
| 3183 | * |
| 3184 | * @param[in] type Base type. |
| 3185 | * @param[in] prev_type Previously returned type. |
| 3186 | * @return Next type or NULL. |
| 3187 | */ |
| 3188 | static struct lysc_type * |
| 3189 | warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type) |
| 3190 | { |
| 3191 | struct lysc_type_union *uni; |
| 3192 | int found = 0; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3193 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3194 | |
| 3195 | switch (type->basetype) { |
| 3196 | case LY_TYPE_UNION: |
| 3197 | uni = (struct lysc_type_union *)type; |
| 3198 | if (!prev_type) { |
| 3199 | return uni->types[0]; |
| 3200 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3201 | LY_ARRAY_FOR(uni->types, u) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3202 | if (found) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3203 | return uni->types[u]; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3204 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3205 | if (prev_type == uni->types[u]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3206 | found = 1; |
| 3207 | } |
| 3208 | } |
| 3209 | return NULL; |
| 3210 | default: |
| 3211 | if (prev_type) { |
| 3212 | assert(type == prev_type); |
| 3213 | return NULL; |
| 3214 | } else { |
| 3215 | return type; |
| 3216 | } |
| 3217 | } |
| 3218 | } |
| 3219 | |
| 3220 | /** |
| 3221 | * @brief Test whether 2 types have a common type. |
| 3222 | * |
| 3223 | * @param[in] type1 First type. |
| 3224 | * @param[in] type2 Second type. |
| 3225 | * @return 1 if they do, 0 otherwise. |
| 3226 | */ |
| 3227 | static int |
| 3228 | warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2) |
| 3229 | { |
| 3230 | struct lysc_type *t1, *rt1, *t2, *rt2; |
| 3231 | |
| 3232 | t1 = NULL; |
| 3233 | while ((t1 = warn_is_equal_type_next_type(type1, t1))) { |
| 3234 | if (t1->basetype == LY_TYPE_LEAFREF) { |
| 3235 | rt1 = ((struct lysc_type_leafref *)t1)->realtype; |
| 3236 | } else { |
| 3237 | rt1 = t1; |
| 3238 | } |
| 3239 | |
| 3240 | t2 = NULL; |
| 3241 | while ((t2 = warn_is_equal_type_next_type(type2, t2))) { |
| 3242 | if (t2->basetype == LY_TYPE_LEAFREF) { |
| 3243 | rt2 = ((struct lysc_type_leafref *)t2)->realtype; |
| 3244 | } else { |
| 3245 | rt2 = t2; |
| 3246 | } |
| 3247 | |
| 3248 | if (rt2->basetype == rt1->basetype) { |
| 3249 | /* match found */ |
| 3250 | return 1; |
| 3251 | } |
| 3252 | } |
| 3253 | } |
| 3254 | |
| 3255 | return 0; |
| 3256 | } |
| 3257 | |
| 3258 | /** |
| 3259 | * @brief Check both operands of comparison operators. |
| 3260 | * |
| 3261 | * @param[in] ctx Context for errors. |
| 3262 | * @param[in] set1 First operand set. |
| 3263 | * @param[in] set2 Second operand set. |
| 3264 | * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!='). |
| 3265 | * @param[in] expr Start of the expression to print with the warning. |
| 3266 | * @param[in] tok_pos Token position. |
| 3267 | */ |
| 3268 | static void |
| 3269 | warn_operands(struct ly_ctx *ctx, struct lyxp_set *set1, struct lyxp_set *set2, int numbers_only, const char *expr, uint16_t tok_pos) |
| 3270 | { |
| 3271 | struct lysc_node_leaf *node1, *node2; |
| 3272 | int leaves = 1, warning = 0; |
| 3273 | |
| 3274 | node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1); |
| 3275 | node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2); |
| 3276 | |
| 3277 | if (!node1 && !node2) { |
| 3278 | /* no node-sets involved, nothing to do */ |
| 3279 | return; |
| 3280 | } |
| 3281 | |
| 3282 | if (node1) { |
| 3283 | if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3284 | LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name); |
| 3285 | warning = 1; |
| 3286 | leaves = 0; |
| 3287 | } else if (numbers_only && !warn_is_numeric_type(node1->type)) { |
| 3288 | LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name); |
| 3289 | warning = 1; |
| 3290 | } |
| 3291 | } |
| 3292 | |
| 3293 | if (node2) { |
| 3294 | if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3295 | LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name); |
| 3296 | warning = 1; |
| 3297 | leaves = 0; |
| 3298 | } else if (numbers_only && !warn_is_numeric_type(node2->type)) { |
| 3299 | LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name); |
| 3300 | warning = 1; |
| 3301 | } |
| 3302 | } |
| 3303 | |
| 3304 | if (node1 && node2 && leaves && !numbers_only) { |
| 3305 | if ((warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type)) |
| 3306 | || (!warn_is_numeric_type(node1->type) && warn_is_numeric_type(node2->type)) |
| 3307 | || (!warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type) |
| 3308 | && !warn_is_equal_type(node1->type, node2->type))) { |
| 3309 | LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name); |
| 3310 | warning = 1; |
| 3311 | } |
| 3312 | } |
| 3313 | |
| 3314 | if (warning) { |
| 3315 | LOGWRN(ctx, "Previous warning generated by XPath subexpression[%u] \"%.20s\".", tok_pos, expr + tok_pos); |
| 3316 | } |
| 3317 | } |
| 3318 | |
| 3319 | /** |
| 3320 | * @brief Check that a value is valid for a leaf. If not applicable, does nothing. |
| 3321 | * |
| 3322 | * @param[in] exp Parsed XPath expression. |
| 3323 | * @param[in] set Set with the leaf/leaf-list. |
| 3324 | * @param[in] val_exp Index of the value (literal/number) in @p exp. |
| 3325 | * @param[in] equal_exp Index of the start of the equality expression in @p exp. |
| 3326 | * @param[in] last_equal_exp Index of the end of the equality expression in @p exp. |
| 3327 | */ |
| 3328 | static void |
| 3329 | warn_equality_value(struct lyxp_expr *exp, struct lyxp_set *set, uint16_t val_exp, uint16_t equal_exp, uint16_t last_equal_exp) |
| 3330 | { |
| 3331 | struct lysc_node *scnode; |
| 3332 | struct lysc_type *type; |
| 3333 | char *value; |
| 3334 | LY_ERR rc; |
| 3335 | struct ly_err_item *err = NULL; |
| 3336 | |
| 3337 | if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST)) |
| 3338 | && ((exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) || (exp->tokens[val_exp] == LYXP_TOKEN_NUMBER))) { |
| 3339 | /* check that the node can have the specified value */ |
| 3340 | if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) { |
| 3341 | value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2); |
| 3342 | } else { |
| 3343 | value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]); |
| 3344 | } |
| 3345 | if (!value) { |
| 3346 | LOGMEM(set->ctx); |
| 3347 | return; |
| 3348 | } |
| 3349 | |
| 3350 | if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) { |
| 3351 | LOGWRN(set->ctx, "Identityref \"%s\" comparison with identity \"%s\" without prefix, consider adding" |
| 3352 | " a prefix or best using \"derived-from(-or-self)()\" functions.", scnode->name, value); |
| 3353 | LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp], |
| 3354 | (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp], |
| 3355 | exp->expr + exp->tok_pos[equal_exp]); |
| 3356 | } |
| 3357 | |
| 3358 | type = ((struct lysc_node_leaf *)scnode)->type; |
| 3359 | if (type->basetype != LY_TYPE_IDENT) { |
Michal Vasko | 93b18da | 2020-08-05 13:27:55 +0200 | [diff] [blame] | 3360 | rc = type->plugin->store(set->ctx, type, value, strlen(value), LY_TYPE_OPTS_SCHEMA, lys_resolve_prefix, |
| 3361 | (void *)set->local_mod, LYD_XML, NULL, NULL, NULL, NULL, &err); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3362 | |
| 3363 | if (err) { |
| 3364 | LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg); |
| 3365 | ly_err_free(err); |
| 3366 | } else if (rc != LY_SUCCESS) { |
| 3367 | LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value); |
| 3368 | } |
| 3369 | if (rc != LY_SUCCESS) { |
| 3370 | LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp], |
| 3371 | (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp], |
| 3372 | exp->expr + exp->tok_pos[equal_exp]); |
| 3373 | } |
| 3374 | } |
| 3375 | free(value); |
| 3376 | } |
| 3377 | } |
| 3378 | |
| 3379 | /* |
| 3380 | * XPath functions |
| 3381 | */ |
| 3382 | |
| 3383 | /** |
| 3384 | * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN |
| 3385 | * depending on whether the first node bit value from the second argument is set. |
| 3386 | * |
| 3387 | * @param[in] args Array of arguments. |
| 3388 | * @param[in] arg_count Count of elements in @p args. |
| 3389 | * @param[in,out] set Context and result set at the same time. |
| 3390 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3391 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3392 | */ |
| 3393 | static LY_ERR |
| 3394 | xpath_bit_is_set(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3395 | { |
| 3396 | struct lyd_node_term *leaf; |
| 3397 | struct lysc_node_leaf *sleaf; |
| 3398 | struct lysc_type_bits *bits; |
| 3399 | LY_ERR rc = LY_SUCCESS; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3400 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3401 | |
| 3402 | if (options & LYXP_SCNODE_ALL) { |
| 3403 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3404 | 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] | 3405 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3406 | 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] | 3407 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) { |
| 3408 | 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] | 3409 | } |
| 3410 | |
| 3411 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 3412 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3413 | 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] | 3414 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3415 | 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] | 3416 | } |
| 3417 | } |
| 3418 | set_scnode_clear_ctx(set); |
| 3419 | return rc; |
| 3420 | } |
| 3421 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3422 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3423 | 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)"); |
| 3424 | return LY_EVALID; |
| 3425 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3426 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3427 | LY_CHECK_RET(rc); |
| 3428 | |
| 3429 | set_fill_boolean(set, 0); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3430 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3431 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
| 3432 | if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) |
| 3433 | && (((struct lysc_node_leaf *)leaf->schema)->type->basetype == LY_TYPE_BITS)) { |
| 3434 | bits = (struct lysc_type_bits *)((struct lysc_node_leaf *)leaf->schema)->type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3435 | LY_ARRAY_FOR(bits->bits, u) { |
| 3436 | if (!strcmp(bits->bits[u].name, args[1]->val.str)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3437 | set_fill_boolean(set, 1); |
| 3438 | break; |
| 3439 | } |
| 3440 | } |
| 3441 | } |
| 3442 | } |
| 3443 | |
| 3444 | return LY_SUCCESS; |
| 3445 | } |
| 3446 | |
| 3447 | /** |
| 3448 | * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN |
| 3449 | * with the argument converted to boolean. |
| 3450 | * |
| 3451 | * @param[in] args Array of arguments. |
| 3452 | * @param[in] arg_count Count of elements in @p args. |
| 3453 | * @param[in,out] set Context and result set at the same time. |
| 3454 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3455 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3456 | */ |
| 3457 | static LY_ERR |
| 3458 | xpath_boolean(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3459 | { |
| 3460 | LY_ERR rc; |
| 3461 | |
| 3462 | if (options & LYXP_SCNODE_ALL) { |
| 3463 | set_scnode_clear_ctx(set); |
| 3464 | return LY_SUCCESS; |
| 3465 | } |
| 3466 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3467 | rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3468 | LY_CHECK_RET(rc); |
| 3469 | set_fill_set(set, args[0]); |
| 3470 | |
| 3471 | return LY_SUCCESS; |
| 3472 | } |
| 3473 | |
| 3474 | /** |
| 3475 | * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER |
| 3476 | * with the first argument rounded up to the nearest integer. |
| 3477 | * |
| 3478 | * @param[in] args Array of arguments. |
| 3479 | * @param[in] arg_count Count of elements in @p args. |
| 3480 | * @param[in,out] set Context and result set at the same time. |
| 3481 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3482 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3483 | */ |
| 3484 | static LY_ERR |
| 3485 | xpath_ceiling(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3486 | { |
| 3487 | struct lysc_node_leaf *sleaf; |
| 3488 | LY_ERR rc = LY_SUCCESS; |
| 3489 | |
| 3490 | if (options & LYXP_SCNODE_ALL) { |
| 3491 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3492 | 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] | 3493 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3494 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3495 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) { |
| 3496 | 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] | 3497 | } |
| 3498 | set_scnode_clear_ctx(set); |
| 3499 | return rc; |
| 3500 | } |
| 3501 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3502 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3503 | LY_CHECK_RET(rc); |
| 3504 | if ((long long)args[0]->val.num != args[0]->val.num) { |
| 3505 | set_fill_number(set, ((long long)args[0]->val.num) + 1); |
| 3506 | } else { |
| 3507 | set_fill_number(set, args[0]->val.num); |
| 3508 | } |
| 3509 | |
| 3510 | return LY_SUCCESS; |
| 3511 | } |
| 3512 | |
| 3513 | /** |
| 3514 | * @brief Execute the XPath concat(string, string, string*) function. |
| 3515 | * Returns LYXP_SET_STRING with the concatenation of all the arguments. |
| 3516 | * |
| 3517 | * @param[in] args Array of arguments. |
| 3518 | * @param[in] arg_count Count of elements in @p args. |
| 3519 | * @param[in,out] set Context and result set at the same time. |
| 3520 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3521 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3522 | */ |
| 3523 | static LY_ERR |
| 3524 | xpath_concat(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options) |
| 3525 | { |
| 3526 | uint16_t i; |
| 3527 | char *str = NULL; |
| 3528 | size_t used = 1; |
| 3529 | LY_ERR rc = LY_SUCCESS; |
| 3530 | struct lysc_node_leaf *sleaf; |
| 3531 | |
| 3532 | if (options & LYXP_SCNODE_ALL) { |
| 3533 | for (i = 0; i < arg_count; ++i) { |
| 3534 | if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) { |
| 3535 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3536 | LOGWRN(set->ctx, "Argument #%u of %s is a %s node \"%s\".", |
| 3537 | i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3538 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3539 | LOGWRN(set->ctx, "Argument #%u of %s is node \"%s\", not of string-type.", __func__, i + 1, sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3540 | } |
| 3541 | } |
| 3542 | } |
| 3543 | set_scnode_clear_ctx(set); |
| 3544 | return rc; |
| 3545 | } |
| 3546 | |
| 3547 | for (i = 0; i < arg_count; ++i) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3548 | rc = lyxp_set_cast(args[i], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3549 | if (rc != LY_SUCCESS) { |
| 3550 | free(str); |
| 3551 | return rc; |
| 3552 | } |
| 3553 | |
| 3554 | str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char)); |
| 3555 | LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM); |
| 3556 | strcpy(str + used - 1, args[i]->val.str); |
| 3557 | used += strlen(args[i]->val.str); |
| 3558 | } |
| 3559 | |
| 3560 | /* free, kind of */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3561 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3562 | set->type = LYXP_SET_STRING; |
| 3563 | set->val.str = str; |
| 3564 | |
| 3565 | return LY_SUCCESS; |
| 3566 | } |
| 3567 | |
| 3568 | /** |
| 3569 | * @brief Execute the XPath contains(string, string) function. |
| 3570 | * Returns LYXP_SET_BOOLEAN whether the second argument can |
| 3571 | * be found in the first or not. |
| 3572 | * |
| 3573 | * @param[in] args Array of arguments. |
| 3574 | * @param[in] arg_count Count of elements in @p args. |
| 3575 | * @param[in,out] set Context and result set at the same time. |
| 3576 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3577 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3578 | */ |
| 3579 | static LY_ERR |
| 3580 | xpath_contains(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3581 | { |
| 3582 | struct lysc_node_leaf *sleaf; |
| 3583 | LY_ERR rc = LY_SUCCESS; |
| 3584 | |
| 3585 | if (options & LYXP_SCNODE_ALL) { |
| 3586 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3587 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3588 | 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] | 3589 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3590 | 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] | 3591 | } |
| 3592 | } |
| 3593 | |
| 3594 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 3595 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3596 | 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] | 3597 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3598 | 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] | 3599 | } |
| 3600 | } |
| 3601 | set_scnode_clear_ctx(set); |
| 3602 | return rc; |
| 3603 | } |
| 3604 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3605 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3606 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3607 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3608 | LY_CHECK_RET(rc); |
| 3609 | |
| 3610 | if (strstr(args[0]->val.str, args[1]->val.str)) { |
| 3611 | set_fill_boolean(set, 1); |
| 3612 | } else { |
| 3613 | set_fill_boolean(set, 0); |
| 3614 | } |
| 3615 | |
| 3616 | return LY_SUCCESS; |
| 3617 | } |
| 3618 | |
| 3619 | /** |
| 3620 | * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER |
| 3621 | * with the size of the node-set from the argument. |
| 3622 | * |
| 3623 | * @param[in] args Array of arguments. |
| 3624 | * @param[in] arg_count Count of elements in @p args. |
| 3625 | * @param[in,out] set Context and result set at the same time. |
| 3626 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3627 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3628 | */ |
| 3629 | static LY_ERR |
| 3630 | xpath_count(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3631 | { |
| 3632 | struct lysc_node *scnode = NULL; |
| 3633 | LY_ERR rc = LY_SUCCESS; |
| 3634 | |
| 3635 | if (options & LYXP_SCNODE_ALL) { |
| 3636 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(scnode = warn_get_scnode_in_ctx(args[0]))) { |
| 3637 | 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] | 3638 | } |
| 3639 | set_scnode_clear_ctx(set); |
| 3640 | return rc; |
| 3641 | } |
| 3642 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3643 | if (args[0]->type != LYXP_SET_NODE_SET) { |
| 3644 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "count(node-set)"); |
| 3645 | return LY_EVALID; |
| 3646 | } |
| 3647 | |
| 3648 | set_fill_number(set, args[0]->used); |
| 3649 | return LY_SUCCESS; |
| 3650 | } |
| 3651 | |
| 3652 | /** |
| 3653 | * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET |
| 3654 | * with the context with the intial node. |
| 3655 | * |
| 3656 | * @param[in] args Array of arguments. |
| 3657 | * @param[in] arg_count Count of elements in @p args. |
| 3658 | * @param[in,out] set Context and result set at the same time. |
| 3659 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3660 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3661 | */ |
| 3662 | static LY_ERR |
| 3663 | xpath_current(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options) |
| 3664 | { |
| 3665 | if (arg_count || args) { |
| 3666 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGCOUNT, arg_count, "current()"); |
| 3667 | return LY_EVALID; |
| 3668 | } |
| 3669 | |
| 3670 | if (options & LYXP_SCNODE_ALL) { |
| 3671 | set_scnode_clear_ctx(set); |
| 3672 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 3673 | lyxp_set_scnode_insert_node(set, set->ctx_scnode, LYXP_NODE_ELEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3674 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3675 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3676 | |
| 3677 | /* position is filled later */ |
| 3678 | set_insert_node(set, set->ctx_node, 0, LYXP_NODE_ELEM, 0); |
| 3679 | } |
| 3680 | |
| 3681 | return LY_SUCCESS; |
| 3682 | } |
| 3683 | |
| 3684 | /** |
| 3685 | * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either |
| 3686 | * leafref or instance-identifier target node(s). |
| 3687 | * |
| 3688 | * @param[in] args Array of arguments. |
| 3689 | * @param[in] arg_count Count of elements in @p args. |
| 3690 | * @param[in,out] set Context and result set at the same time. |
| 3691 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3692 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3693 | */ |
| 3694 | static LY_ERR |
| 3695 | xpath_deref(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3696 | { |
| 3697 | struct lyd_node_term *leaf; |
Michal Vasko | 42e497c | 2020-01-06 08:38:25 +0100 | [diff] [blame] | 3698 | struct lysc_node_leaf *sleaf = NULL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3699 | struct lysc_type_leafref *lref; |
Michal Vasko | ae9e4cb | 2019-09-25 08:43:05 +0200 | [diff] [blame] | 3700 | const struct lysc_node *target; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3701 | struct ly_path *p; |
| 3702 | struct lyd_node *node; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3703 | char *errmsg = NULL; |
| 3704 | const char *val; |
| 3705 | int dynamic; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 3706 | uint8_t oper; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3707 | LY_ERR rc = LY_SUCCESS; |
| 3708 | |
| 3709 | if (options & LYXP_SCNODE_ALL) { |
| 3710 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3711 | 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] | 3712 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3713 | 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] | 3714 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) && !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) { |
| 3715 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"leafref\" nor \"instance-identifier\".", |
| 3716 | __func__, sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3717 | } |
| 3718 | set_scnode_clear_ctx(set); |
Michal Vasko | 42e497c | 2020-01-06 08:38:25 +0100 | [diff] [blame] | 3719 | if (sleaf && (sleaf->type->basetype == LY_TYPE_LEAFREF)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3720 | lref = (struct lysc_type_leafref *)sleaf->type; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 3721 | 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] | 3722 | |
| 3723 | /* it was already evaluated on schema, it must succeed */ |
| 3724 | if (set->format == LYD_JSON) { |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 3725 | rc = ly_path_compile(set->ctx, sleaf->module, (struct lysc_node *)sleaf, lref->path, LY_PATH_LREF_TRUE, |
| 3726 | oper, LY_PATH_TARGET_MANY, lydjson_resolve_prefix, NULL, LYD_JSON, &p); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3727 | } else { |
| 3728 | assert(set->format == LYD_SCHEMA); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 3729 | rc = ly_path_compile(set->ctx, sleaf->module, (struct lysc_node *)sleaf, lref->path, LY_PATH_LREF_TRUE, |
| 3730 | oper, LY_PATH_TARGET_MANY, lys_resolve_prefix, lref->path_context, LYD_SCHEMA, &p); |
Michal Vasko | ae9e4cb | 2019-09-25 08:43:05 +0200 | [diff] [blame] | 3731 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3732 | assert(!rc); |
| 3733 | |
| 3734 | /* get the target node */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3735 | target = p[LY_ARRAY_COUNT(p) - 1].node; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3736 | ly_path_free(set->ctx, p); |
| 3737 | |
| 3738 | lyxp_set_scnode_insert_node(set, target, LYXP_NODE_ELEM); |
Michal Vasko | ae9e4cb | 2019-09-25 08:43:05 +0200 | [diff] [blame] | 3739 | } |
| 3740 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3741 | return rc; |
| 3742 | } |
| 3743 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3744 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3745 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "deref(node-set)"); |
| 3746 | return LY_EVALID; |
| 3747 | } |
| 3748 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3749 | lyxp_set_free_content(set); |
| 3750 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3751 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
| 3752 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 3753 | if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 3754 | if (sleaf->type->basetype == LY_TYPE_LEAFREF) { |
| 3755 | /* find leafref target */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3756 | if (ly_type_find_leafref((struct lysc_type_leafref *)sleaf->type, (struct lyd_node *)leaf, |
| 3757 | &leaf->value, set->tree, &node, &errmsg)) { |
| 3758 | LOGERR(set->ctx, LY_EVALID, errmsg); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3759 | free(errmsg); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3760 | return LY_EVALID; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3761 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3762 | } else { |
| 3763 | assert(sleaf->type->basetype == LY_TYPE_INST); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3764 | if (ly_path_eval(leaf->value.target, set->tree, &node)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3765 | val = lyd_value2str(leaf, &dynamic); |
| 3766 | LOGERR(set->ctx, LY_EVALID, "Invalid instance-identifier \"%s\" value - required instance not found.", val); |
| 3767 | if (dynamic) { |
| 3768 | free((char *)val); |
| 3769 | } |
| 3770 | return LY_EVALID; |
| 3771 | } |
| 3772 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3773 | |
| 3774 | /* insert it */ |
| 3775 | set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3776 | } |
| 3777 | } |
| 3778 | |
| 3779 | return LY_SUCCESS; |
| 3780 | } |
| 3781 | |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3782 | static LY_ERR |
| 3783 | xpath_derived_(struct lyxp_set **args, struct lyxp_set *set, int options, int self_match, const char *func) |
| 3784 | { |
| 3785 | uint16_t i; |
| 3786 | LY_ARRAY_COUNT_TYPE u; |
| 3787 | struct lyd_node_term *leaf; |
| 3788 | struct lysc_node_leaf *sleaf; |
| 3789 | struct lyd_meta *meta; |
| 3790 | struct lyd_value data = {0}, *val; |
| 3791 | struct ly_err_item *err = NULL; |
| 3792 | LY_ERR rc = LY_SUCCESS; |
| 3793 | int found; |
| 3794 | |
| 3795 | if (options & LYXP_SCNODE_ALL) { |
| 3796 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3797 | LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", func); |
| 3798 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3799 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype), |
| 3800 | sleaf->name); |
| 3801 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) { |
| 3802 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", func, sleaf->name); |
| 3803 | } |
| 3804 | |
| 3805 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 3806 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3807 | LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype), |
| 3808 | sleaf->name); |
| 3809 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3810 | LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", func, sleaf->name); |
| 3811 | } |
| 3812 | } |
| 3813 | set_scnode_clear_ctx(set); |
| 3814 | return rc; |
| 3815 | } |
| 3816 | |
| 3817 | if (args[0]->type != LYXP_SET_NODE_SET) { |
| 3818 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), |
| 3819 | "derived-from(-or-self)(node-set, string)"); |
| 3820 | return LY_EVALID; |
| 3821 | } |
| 3822 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
| 3823 | LY_CHECK_RET(rc); |
| 3824 | |
| 3825 | set_fill_boolean(set, 0); |
| 3826 | found = 0; |
| 3827 | for (i = 0; i < args[0]->used; ++i) { |
| 3828 | if ((args[0]->val.nodes[i].type != LYXP_NODE_ELEM) && (args[0]->val.nodes[i].type != LYXP_NODE_META)) { |
| 3829 | continue; |
| 3830 | } |
| 3831 | |
| 3832 | if (args[0]->val.nodes[i].type == LYXP_NODE_ELEM) { |
| 3833 | leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node; |
| 3834 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 3835 | val = &leaf->value; |
| 3836 | if (!(sleaf->nodetype & LYD_NODE_TERM) || (leaf->value.realtype->basetype != LY_TYPE_IDENT)) { |
| 3837 | /* uninteresting */ |
| 3838 | continue; |
| 3839 | } |
| 3840 | |
| 3841 | /* store args[1] as ident */ |
| 3842 | rc = val->realtype->plugin->store(set->ctx, val->realtype, args[1]->val.str, strlen(args[1]->val.str), |
Michal Vasko | 93b18da | 2020-08-05 13:27:55 +0200 | [diff] [blame] | 3843 | LY_TYPE_OPTS_STORE, lys_resolve_prefix, (void *)set->local_mod, |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3844 | set->format, (struct lyd_node *)leaf, set->tree, &data, NULL, &err); |
| 3845 | } else { |
| 3846 | meta = args[0]->val.meta[i].meta; |
| 3847 | val = &meta->value; |
| 3848 | if (val->realtype->basetype != LY_TYPE_IDENT) { |
| 3849 | /* uninteresting */ |
| 3850 | continue; |
| 3851 | } |
| 3852 | |
| 3853 | /* store args[1] as ident */ |
| 3854 | rc = val->realtype->plugin->store(set->ctx, val->realtype, args[1]->val.str, strlen(args[1]->val.str), |
| 3855 | LY_TYPE_OPTS_STORE, lys_resolve_prefix, (void *)meta->annotation->module, |
| 3856 | set->format, meta->parent, set->tree, &data, NULL, &err); |
| 3857 | } |
| 3858 | |
| 3859 | if (err) { |
| 3860 | ly_err_print(err); |
| 3861 | ly_err_free(err); |
| 3862 | } |
| 3863 | LY_CHECK_RET(rc); |
| 3864 | |
| 3865 | /* finally check the identity itself */ |
| 3866 | if (self_match && (data.ident == val->ident)) { |
| 3867 | set_fill_boolean(set, 1); |
| 3868 | found = 1; |
| 3869 | } |
| 3870 | if (!found) { |
| 3871 | LY_ARRAY_FOR(data.ident->derived, u) { |
| 3872 | if (data.ident->derived[u] == val->ident) { |
| 3873 | set_fill_boolean(set, 1); |
| 3874 | found = 1; |
| 3875 | break; |
| 3876 | } |
| 3877 | } |
| 3878 | } |
| 3879 | |
| 3880 | /* free temporary value */ |
| 3881 | val->realtype->plugin->free(set->ctx, &data); |
| 3882 | if (found) { |
| 3883 | break; |
| 3884 | } |
| 3885 | } |
| 3886 | |
| 3887 | return LY_SUCCESS; |
| 3888 | } |
| 3889 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3890 | /** |
| 3891 | * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending |
| 3892 | * on whether the first argument nodes contain a node of an identity derived from the second |
| 3893 | * argument identity. |
| 3894 | * |
| 3895 | * @param[in] args Array of arguments. |
| 3896 | * @param[in] arg_count Count of elements in @p args. |
| 3897 | * @param[in,out] set Context and result set at the same time. |
| 3898 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3899 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3900 | */ |
| 3901 | static LY_ERR |
| 3902 | xpath_derived_from(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3903 | { |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3904 | return xpath_derived_(args, set, options, 0, __func__); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3905 | } |
| 3906 | |
| 3907 | /** |
| 3908 | * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending |
| 3909 | * on whether the first argument nodes contain a node of an identity that either is or is derived from |
| 3910 | * the second argument identity. |
| 3911 | * |
| 3912 | * @param[in] args Array of arguments. |
| 3913 | * @param[in] arg_count Count of elements in @p args. |
| 3914 | * @param[in,out] set Context and result set at the same time. |
| 3915 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3916 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3917 | */ |
| 3918 | static LY_ERR |
| 3919 | xpath_derived_from_or_self(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3920 | { |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3921 | return xpath_derived_(args, set, options, 1, __func__); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3922 | } |
| 3923 | |
| 3924 | /** |
| 3925 | * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER |
| 3926 | * with the integer value of the first node's enum value, otherwise NaN. |
| 3927 | * |
| 3928 | * @param[in] args Array of arguments. |
| 3929 | * @param[in] arg_count Count of elements in @p args. |
| 3930 | * @param[in,out] set Context and result set at the same time. |
| 3931 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3932 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3933 | */ |
| 3934 | static LY_ERR |
| 3935 | xpath_enum_value(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3936 | { |
| 3937 | struct lyd_node_term *leaf; |
| 3938 | struct lysc_node_leaf *sleaf; |
| 3939 | LY_ERR rc = LY_SUCCESS; |
| 3940 | |
| 3941 | if (options & LYXP_SCNODE_ALL) { |
| 3942 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3943 | 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] | 3944 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3945 | 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] | 3946 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) { |
| 3947 | 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] | 3948 | } |
| 3949 | set_scnode_clear_ctx(set); |
| 3950 | return rc; |
| 3951 | } |
| 3952 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3953 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3954 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "enum-value(node-set)"); |
| 3955 | return LY_EVALID; |
| 3956 | } |
| 3957 | |
| 3958 | set_fill_number(set, NAN); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3959 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3960 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
| 3961 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 3962 | if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) { |
| 3963 | set_fill_number(set, leaf->value.enum_item->value); |
| 3964 | } |
| 3965 | } |
| 3966 | |
| 3967 | return LY_SUCCESS; |
| 3968 | } |
| 3969 | |
| 3970 | /** |
| 3971 | * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN |
| 3972 | * with false value. |
| 3973 | * |
| 3974 | * @param[in] args Array of arguments. |
| 3975 | * @param[in] arg_count Count of elements in @p args. |
| 3976 | * @param[in,out] set Context and result set at the same time. |
| 3977 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3978 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3979 | */ |
| 3980 | static LY_ERR |
| 3981 | xpath_false(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3982 | { |
| 3983 | if (options & LYXP_SCNODE_ALL) { |
| 3984 | set_scnode_clear_ctx(set); |
| 3985 | return LY_SUCCESS; |
| 3986 | } |
| 3987 | |
| 3988 | set_fill_boolean(set, 0); |
| 3989 | return LY_SUCCESS; |
| 3990 | } |
| 3991 | |
| 3992 | /** |
| 3993 | * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER |
| 3994 | * with the first argument floored (truncated). |
| 3995 | * |
| 3996 | * @param[in] args Array of arguments. |
| 3997 | * @param[in] arg_count Count of elements in @p args. |
| 3998 | * @param[in,out] set Context and result set at the same time. |
| 3999 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4000 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4001 | */ |
| 4002 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4003 | 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] | 4004 | { |
| 4005 | LY_ERR rc; |
| 4006 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4007 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4008 | LY_CHECK_RET(rc); |
| 4009 | if (isfinite(args[0]->val.num)) { |
| 4010 | set_fill_number(set, (long long)args[0]->val.num); |
| 4011 | } |
| 4012 | |
| 4013 | return LY_SUCCESS; |
| 4014 | } |
| 4015 | |
| 4016 | /** |
| 4017 | * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN |
| 4018 | * whether the language of the text matches the one from the argument. |
| 4019 | * |
| 4020 | * @param[in] args Array of arguments. |
| 4021 | * @param[in] arg_count Count of elements in @p args. |
| 4022 | * @param[in,out] set Context and result set at the same time. |
| 4023 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4024 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4025 | */ |
| 4026 | static LY_ERR |
| 4027 | xpath_lang(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 4028 | { |
| 4029 | const struct lyd_node *node; |
| 4030 | struct lysc_node_leaf *sleaf; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4031 | struct lyd_meta *meta = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4032 | const char *val; |
| 4033 | int i, dynamic; |
| 4034 | LY_ERR rc = LY_SUCCESS; |
| 4035 | |
| 4036 | if (options & LYXP_SCNODE_ALL) { |
| 4037 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4038 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4039 | 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] | 4040 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4041 | 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] | 4042 | } |
| 4043 | } |
| 4044 | set_scnode_clear_ctx(set); |
| 4045 | return rc; |
| 4046 | } |
| 4047 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4048 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4049 | LY_CHECK_RET(rc); |
| 4050 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4051 | if (set->type != LYXP_SET_NODE_SET) { |
| 4052 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "lang(string)"); |
| 4053 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4054 | } else if (!set->used) { |
| 4055 | set_fill_boolean(set, 0); |
| 4056 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4057 | } |
| 4058 | |
| 4059 | switch (set->val.nodes[0].type) { |
| 4060 | case LYXP_NODE_ELEM: |
| 4061 | case LYXP_NODE_TEXT: |
| 4062 | node = set->val.nodes[0].node; |
| 4063 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4064 | case LYXP_NODE_META: |
| 4065 | node = set->val.meta[0].meta->parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4066 | break; |
| 4067 | default: |
| 4068 | /* nothing to do with roots */ |
| 4069 | set_fill_boolean(set, 0); |
| 4070 | return LY_SUCCESS; |
| 4071 | } |
| 4072 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4073 | /* find lang metadata */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4074 | for (; node; node = (struct lyd_node *)node->parent) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4075 | for (meta = node->meta; meta; meta = meta->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4076 | /* annotations */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4077 | 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] | 4078 | break; |
| 4079 | } |
| 4080 | } |
| 4081 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4082 | if (meta) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4083 | break; |
| 4084 | } |
| 4085 | } |
| 4086 | |
| 4087 | /* compare languages */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4088 | if (!meta) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4089 | set_fill_boolean(set, 0); |
| 4090 | } else { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4091 | val = lyd_meta2str(meta, &dynamic); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4092 | for (i = 0; args[0]->val.str[i]; ++i) { |
| 4093 | if (tolower(args[0]->val.str[i]) != tolower(val[i])) { |
| 4094 | set_fill_boolean(set, 0); |
| 4095 | break; |
| 4096 | } |
| 4097 | } |
| 4098 | if (!args[0]->val.str[i]) { |
| 4099 | if (!val[i] || (val[i] == '-')) { |
| 4100 | set_fill_boolean(set, 1); |
| 4101 | } else { |
| 4102 | set_fill_boolean(set, 0); |
| 4103 | } |
| 4104 | } |
| 4105 | if (dynamic) { |
| 4106 | free((char *)val); |
| 4107 | } |
| 4108 | } |
| 4109 | |
| 4110 | return LY_SUCCESS; |
| 4111 | } |
| 4112 | |
| 4113 | /** |
| 4114 | * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER |
| 4115 | * with the context size. |
| 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_last(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 4125 | { |
| 4126 | if (options & LYXP_SCNODE_ALL) { |
| 4127 | set_scnode_clear_ctx(set); |
| 4128 | return LY_SUCCESS; |
| 4129 | } |
| 4130 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4131 | if (set->type != LYXP_SET_NODE_SET) { |
| 4132 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "last()"); |
| 4133 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4134 | } else if (!set->used) { |
| 4135 | set_fill_number(set, 0); |
| 4136 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4137 | } |
| 4138 | |
| 4139 | set_fill_number(set, set->ctx_size); |
| 4140 | return LY_SUCCESS; |
| 4141 | } |
| 4142 | |
| 4143 | /** |
| 4144 | * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING |
| 4145 | * with the node name without namespace from the argument or the context. |
| 4146 | * |
| 4147 | * @param[in] args Array of arguments. |
| 4148 | * @param[in] arg_count Count of elements in @p args. |
| 4149 | * @param[in,out] set Context and result set at the same time. |
| 4150 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4151 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4152 | */ |
| 4153 | static LY_ERR |
| 4154 | xpath_local_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options) |
| 4155 | { |
| 4156 | struct lyxp_set_node *item; |
| 4157 | /* suppress unused variable warning */ |
| 4158 | (void)options; |
| 4159 | |
| 4160 | if (options & LYXP_SCNODE_ALL) { |
| 4161 | set_scnode_clear_ctx(set); |
| 4162 | return LY_SUCCESS; |
| 4163 | } |
| 4164 | |
| 4165 | if (arg_count) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4166 | if (args[0]->type != LYXP_SET_NODE_SET) { |
| 4167 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "local-name(node-set?)"); |
| 4168 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4169 | } else if (!args[0]->used) { |
| 4170 | set_fill_string(set, "", 0); |
| 4171 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4172 | } |
| 4173 | |
| 4174 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4175 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4176 | |
| 4177 | item = &args[0]->val.nodes[0]; |
| 4178 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4179 | if (set->type != LYXP_SET_NODE_SET) { |
| 4180 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "local-name(node-set?)"); |
| 4181 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4182 | } else if (!set->used) { |
| 4183 | set_fill_string(set, "", 0); |
| 4184 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4185 | } |
| 4186 | |
| 4187 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4188 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4189 | |
| 4190 | item = &set->val.nodes[0]; |
| 4191 | } |
| 4192 | |
| 4193 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4194 | case LYXP_NODE_NONE: |
| 4195 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4196 | case LYXP_NODE_ROOT: |
| 4197 | case LYXP_NODE_ROOT_CONFIG: |
| 4198 | case LYXP_NODE_TEXT: |
| 4199 | set_fill_string(set, "", 0); |
| 4200 | break; |
| 4201 | case LYXP_NODE_ELEM: |
| 4202 | set_fill_string(set, item->node->schema->name, strlen(item->node->schema->name)); |
| 4203 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4204 | case LYXP_NODE_META: |
| 4205 | 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] | 4206 | break; |
| 4207 | } |
| 4208 | |
| 4209 | return LY_SUCCESS; |
| 4210 | } |
| 4211 | |
| 4212 | /** |
| 4213 | * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING |
| 4214 | * with the node name fully qualified (with namespace) from the argument or the context. |
| 4215 | * |
| 4216 | * @param[in] args Array of arguments. |
| 4217 | * @param[in] arg_count Count of elements in @p args. |
| 4218 | * @param[in,out] set Context and result set at the same time. |
| 4219 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4220 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4221 | */ |
| 4222 | static LY_ERR |
| 4223 | xpath_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options) |
| 4224 | { |
| 4225 | struct lyxp_set_node *item; |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 4226 | struct lys_module *mod = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4227 | char *str; |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 4228 | const char *name = NULL; |
| 4229 | int rc = -1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4230 | |
| 4231 | if (options & LYXP_SCNODE_ALL) { |
| 4232 | set_scnode_clear_ctx(set); |
| 4233 | return LY_SUCCESS; |
| 4234 | } |
| 4235 | |
| 4236 | if (arg_count) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4237 | if (args[0]->type != LYXP_SET_NODE_SET) { |
| 4238 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "name(node-set?)"); |
| 4239 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4240 | } else if (!args[0]->used) { |
| 4241 | set_fill_string(set, "", 0); |
| 4242 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4243 | } |
| 4244 | |
| 4245 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4246 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4247 | |
| 4248 | item = &args[0]->val.nodes[0]; |
| 4249 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4250 | if (set->type != LYXP_SET_NODE_SET) { |
| 4251 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "name(node-set?)"); |
| 4252 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4253 | } else if (!set->used) { |
| 4254 | set_fill_string(set, "", 0); |
| 4255 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4256 | } |
| 4257 | |
| 4258 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4259 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4260 | |
| 4261 | item = &set->val.nodes[0]; |
| 4262 | } |
| 4263 | |
| 4264 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4265 | case LYXP_NODE_NONE: |
| 4266 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4267 | case LYXP_NODE_ROOT: |
| 4268 | case LYXP_NODE_ROOT_CONFIG: |
| 4269 | case LYXP_NODE_TEXT: |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 4270 | /* keep NULL */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4271 | break; |
| 4272 | case LYXP_NODE_ELEM: |
| 4273 | mod = item->node->schema->module; |
| 4274 | name = item->node->schema->name; |
| 4275 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4276 | case LYXP_NODE_META: |
| 4277 | mod = ((struct lyd_meta *)item->node)->annotation->module; |
| 4278 | name = ((struct lyd_meta *)item->node)->name; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4279 | break; |
| 4280 | } |
| 4281 | |
| 4282 | if (mod && name) { |
| 4283 | switch (set->format) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 4284 | case LYD_SCHEMA: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4285 | rc = asprintf(&str, "%s:%s", lys_prefix_find_module(set->local_mod, mod), name); |
| 4286 | break; |
| 4287 | case LYD_JSON: |
| 4288 | rc = asprintf(&str, "%s:%s", mod->name, name); |
| 4289 | break; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 4290 | case LYD_XML: |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 4291 | case LYD_LYB: |
Michal Vasko | 9409ef6 | 2019-09-12 11:47:17 +0200 | [diff] [blame] | 4292 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4293 | } |
| 4294 | LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM); |
| 4295 | set_fill_string(set, str, strlen(str)); |
| 4296 | free(str); |
| 4297 | } else { |
| 4298 | set_fill_string(set, "", 0); |
| 4299 | } |
| 4300 | |
| 4301 | return LY_SUCCESS; |
| 4302 | } |
| 4303 | |
| 4304 | /** |
| 4305 | * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING |
| 4306 | * with the namespace of the node from the argument or the context. |
| 4307 | * |
| 4308 | * @param[in] args Array of arguments. |
| 4309 | * @param[in] arg_count Count of elements in @p args. |
| 4310 | * @param[in,out] set Context and result set at the same time. |
| 4311 | * @param[in] options XPath options. |
| 4312 | * @return LY_ERR (LY_EINVAL for wrong arguments on schema) |
| 4313 | */ |
| 4314 | static LY_ERR |
| 4315 | xpath_namespace_uri(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options) |
| 4316 | { |
| 4317 | struct lyxp_set_node *item; |
| 4318 | struct lys_module *mod; |
| 4319 | /* suppress unused variable warning */ |
| 4320 | (void)options; |
| 4321 | |
| 4322 | if (options & LYXP_SCNODE_ALL) { |
| 4323 | set_scnode_clear_ctx(set); |
| 4324 | return LY_SUCCESS; |
| 4325 | } |
| 4326 | |
| 4327 | if (arg_count) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4328 | if (args[0]->type != LYXP_SET_NODE_SET) { |
| 4329 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), |
| 4330 | "namespace-uri(node-set?)"); |
| 4331 | return LY_EVALID; |
| 4332 | } else if (!args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4333 | set_fill_string(set, "", 0); |
| 4334 | return LY_SUCCESS; |
| 4335 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4336 | |
| 4337 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4338 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4339 | |
| 4340 | item = &args[0]->val.nodes[0]; |
| 4341 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4342 | if (set->type != LYXP_SET_NODE_SET) { |
| 4343 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "namespace-uri(node-set?)"); |
| 4344 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4345 | } else if (!set->used) { |
| 4346 | set_fill_string(set, "", 0); |
| 4347 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4348 | } |
| 4349 | |
| 4350 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4351 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4352 | |
| 4353 | item = &set->val.nodes[0]; |
| 4354 | } |
| 4355 | |
| 4356 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4357 | case LYXP_NODE_NONE: |
| 4358 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4359 | case LYXP_NODE_ROOT: |
| 4360 | case LYXP_NODE_ROOT_CONFIG: |
| 4361 | case LYXP_NODE_TEXT: |
| 4362 | set_fill_string(set, "", 0); |
| 4363 | break; |
| 4364 | case LYXP_NODE_ELEM: |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4365 | case LYXP_NODE_META: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4366 | if (item->type == LYXP_NODE_ELEM) { |
| 4367 | mod = item->node->schema->module; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4368 | } else { /* LYXP_NODE_META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4369 | /* annotations */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4370 | mod = ((struct lyd_meta *)item->node)->annotation->module; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4371 | } |
| 4372 | |
| 4373 | set_fill_string(set, mod->ns, strlen(mod->ns)); |
| 4374 | break; |
| 4375 | } |
| 4376 | |
| 4377 | return LY_SUCCESS; |
| 4378 | } |
| 4379 | |
| 4380 | /** |
| 4381 | * @brief Execute the XPath node() function (node type). Returns LYXP_SET_NODE_SET |
| 4382 | * with only nodes from the context. In practice it either leaves the context |
| 4383 | * as it is or returns an empty node set. |
| 4384 | * |
| 4385 | * @param[in] args Array of arguments. |
| 4386 | * @param[in] arg_count Count of elements in @p args. |
| 4387 | * @param[in,out] set Context and result set at the same time. |
| 4388 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4389 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4390 | */ |
| 4391 | static LY_ERR |
| 4392 | xpath_node(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 4393 | { |
| 4394 | if (options & LYXP_SCNODE_ALL) { |
| 4395 | set_scnode_clear_ctx(set); |
| 4396 | return LY_SUCCESS; |
| 4397 | } |
| 4398 | |
| 4399 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4400 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4401 | } |
| 4402 | return LY_SUCCESS; |
| 4403 | } |
| 4404 | |
| 4405 | /** |
| 4406 | * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING |
| 4407 | * with normalized value (no leading, trailing, double white spaces) of the node |
| 4408 | * from the argument or the context. |
| 4409 | * |
| 4410 | * @param[in] args Array of arguments. |
| 4411 | * @param[in] arg_count Count of elements in @p args. |
| 4412 | * @param[in,out] set Context and result set at the same time. |
| 4413 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4414 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4415 | */ |
| 4416 | static LY_ERR |
| 4417 | xpath_normalize_space(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options) |
| 4418 | { |
| 4419 | uint16_t i, new_used; |
| 4420 | char *new; |
| 4421 | int have_spaces = 0, space_before = 0; |
| 4422 | struct lysc_node_leaf *sleaf; |
| 4423 | LY_ERR rc = LY_SUCCESS; |
| 4424 | |
| 4425 | if (options & LYXP_SCNODE_ALL) { |
| 4426 | if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4427 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4428 | 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] | 4429 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4430 | 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] | 4431 | } |
| 4432 | } |
| 4433 | set_scnode_clear_ctx(set); |
| 4434 | return rc; |
| 4435 | } |
| 4436 | |
| 4437 | if (arg_count) { |
| 4438 | set_fill_set(set, args[0]); |
| 4439 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4440 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4441 | LY_CHECK_RET(rc); |
| 4442 | |
| 4443 | /* is there any normalization necessary? */ |
| 4444 | for (i = 0; set->val.str[i]; ++i) { |
| 4445 | if (is_xmlws(set->val.str[i])) { |
| 4446 | if ((i == 0) || space_before || (!set->val.str[i + 1])) { |
| 4447 | have_spaces = 1; |
| 4448 | break; |
| 4449 | } |
| 4450 | space_before = 1; |
| 4451 | } else { |
| 4452 | space_before = 0; |
| 4453 | } |
| 4454 | } |
| 4455 | |
| 4456 | /* yep, there is */ |
| 4457 | if (have_spaces) { |
| 4458 | /* it's enough, at least one character will go, makes space for ending '\0' */ |
| 4459 | new = malloc(strlen(set->val.str) * sizeof(char)); |
| 4460 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 4461 | new_used = 0; |
| 4462 | |
| 4463 | space_before = 0; |
| 4464 | for (i = 0; set->val.str[i]; ++i) { |
| 4465 | if (is_xmlws(set->val.str[i])) { |
| 4466 | if ((i == 0) || space_before) { |
| 4467 | space_before = 1; |
| 4468 | continue; |
| 4469 | } else { |
| 4470 | space_before = 1; |
| 4471 | } |
| 4472 | } else { |
| 4473 | space_before = 0; |
| 4474 | } |
| 4475 | |
| 4476 | new[new_used] = (space_before ? ' ' : set->val.str[i]); |
| 4477 | ++new_used; |
| 4478 | } |
| 4479 | |
| 4480 | /* at worst there is one trailing space now */ |
| 4481 | if (new_used && is_xmlws(new[new_used - 1])) { |
| 4482 | --new_used; |
| 4483 | } |
| 4484 | |
| 4485 | new = ly_realloc(new, (new_used + 1) * sizeof(char)); |
| 4486 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 4487 | new[new_used] = '\0'; |
| 4488 | |
| 4489 | free(set->val.str); |
| 4490 | set->val.str = new; |
| 4491 | } |
| 4492 | |
| 4493 | return LY_SUCCESS; |
| 4494 | } |
| 4495 | |
| 4496 | /** |
| 4497 | * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN |
| 4498 | * with the argument converted to boolean and logically inverted. |
| 4499 | * |
| 4500 | * @param[in] args Array of arguments. |
| 4501 | * @param[in] arg_count Count of elements in @p args. |
| 4502 | * @param[in,out] set Context and result set at the same time. |
| 4503 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4504 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4505 | */ |
| 4506 | static LY_ERR |
| 4507 | xpath_not(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 4508 | { |
| 4509 | if (options & LYXP_SCNODE_ALL) { |
| 4510 | set_scnode_clear_ctx(set); |
| 4511 | return LY_SUCCESS; |
| 4512 | } |
| 4513 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4514 | lyxp_set_cast(args[0], LYXP_SET_BOOLEAN); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4515 | if (args[0]->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4516 | set_fill_boolean(set, 0); |
| 4517 | } else { |
| 4518 | set_fill_boolean(set, 1); |
| 4519 | } |
| 4520 | |
| 4521 | return LY_SUCCESS; |
| 4522 | } |
| 4523 | |
| 4524 | /** |
| 4525 | * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER |
| 4526 | * with the number representation of either the argument or the context. |
| 4527 | * |
| 4528 | * @param[in] args Array of arguments. |
| 4529 | * @param[in] arg_count Count of elements in @p args. |
| 4530 | * @param[in,out] set Context and result set at the same time. |
| 4531 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4532 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4533 | */ |
| 4534 | static LY_ERR |
| 4535 | xpath_number(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options) |
| 4536 | { |
| 4537 | LY_ERR rc; |
| 4538 | |
| 4539 | if (options & LYXP_SCNODE_ALL) { |
| 4540 | set_scnode_clear_ctx(set); |
| 4541 | return LY_SUCCESS; |
| 4542 | } |
| 4543 | |
| 4544 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4545 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4546 | LY_CHECK_RET(rc); |
| 4547 | set_fill_set(set, args[0]); |
| 4548 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4549 | rc = lyxp_set_cast(set, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4550 | LY_CHECK_RET(rc); |
| 4551 | } |
| 4552 | |
| 4553 | return LY_SUCCESS; |
| 4554 | } |
| 4555 | |
| 4556 | /** |
| 4557 | * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER |
| 4558 | * with the context position. |
| 4559 | * |
| 4560 | * @param[in] args Array of arguments. |
| 4561 | * @param[in] arg_count Count of elements in @p args. |
| 4562 | * @param[in,out] set Context and result set at the same time. |
| 4563 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4564 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4565 | */ |
| 4566 | static LY_ERR |
| 4567 | xpath_position(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 4568 | { |
| 4569 | if (options & LYXP_SCNODE_ALL) { |
| 4570 | set_scnode_clear_ctx(set); |
| 4571 | return LY_SUCCESS; |
| 4572 | } |
| 4573 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4574 | if (set->type != LYXP_SET_NODE_SET) { |
| 4575 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "position()"); |
| 4576 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4577 | } else if (!set->used) { |
| 4578 | set_fill_number(set, 0); |
| 4579 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4580 | } |
| 4581 | |
| 4582 | set_fill_number(set, set->ctx_pos); |
| 4583 | |
| 4584 | /* UNUSED in 'Release' build type */ |
| 4585 | (void)options; |
| 4586 | return LY_SUCCESS; |
| 4587 | } |
| 4588 | |
| 4589 | /** |
| 4590 | * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN |
| 4591 | * depending on whether the second argument regex matches the first argument string. For details refer to |
| 4592 | * YANG 1.1 RFC section 10.2.1. |
| 4593 | * |
| 4594 | * @param[in] args Array of arguments. |
| 4595 | * @param[in] arg_count Count of elements in @p args. |
| 4596 | * @param[in,out] set Context and result set at the same time. |
| 4597 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4598 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4599 | */ |
| 4600 | static LY_ERR |
| 4601 | xpath_re_match(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 4602 | { |
| 4603 | struct lysc_pattern **patterns = NULL, **pattern; |
| 4604 | struct lysc_node_leaf *sleaf; |
| 4605 | char *path; |
| 4606 | LY_ERR rc = LY_SUCCESS; |
| 4607 | struct ly_err_item *err; |
| 4608 | |
| 4609 | if (options & LYXP_SCNODE_ALL) { |
| 4610 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4611 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4612 | 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] | 4613 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4614 | 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] | 4615 | } |
| 4616 | } |
| 4617 | |
| 4618 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4619 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4620 | 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] | 4621 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4622 | 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] | 4623 | } |
| 4624 | } |
| 4625 | set_scnode_clear_ctx(set); |
| 4626 | return rc; |
| 4627 | } |
| 4628 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4629 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4630 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4631 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4632 | LY_CHECK_RET(rc); |
| 4633 | |
| 4634 | LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM); |
| 4635 | *pattern = malloc(sizeof **pattern); |
| 4636 | path = lyd_path(set->ctx_node, LYD_PATH_LOG, NULL, 0); |
| 4637 | rc = lys_compile_type_pattern_check(set->ctx, path, args[1]->val.str, &(*pattern)->code); |
| 4638 | free(path); |
| 4639 | if (rc != LY_SUCCESS) { |
| 4640 | LY_ARRAY_FREE(patterns); |
| 4641 | return rc; |
| 4642 | } |
| 4643 | |
| 4644 | rc = ly_type_validate_patterns(patterns, args[0]->val.str, strlen(args[0]->val.str), &err); |
| 4645 | pcre2_code_free((*pattern)->code); |
| 4646 | free(*pattern); |
| 4647 | LY_ARRAY_FREE(patterns); |
| 4648 | if (rc && (rc != LY_EVALID)) { |
| 4649 | ly_err_print(err); |
| 4650 | ly_err_free(err); |
| 4651 | return rc; |
| 4652 | } |
| 4653 | |
| 4654 | if (rc == LY_EVALID) { |
| 4655 | ly_err_free(err); |
| 4656 | set_fill_boolean(set, 0); |
| 4657 | } else { |
| 4658 | set_fill_boolean(set, 1); |
| 4659 | } |
| 4660 | |
| 4661 | return LY_SUCCESS; |
| 4662 | } |
| 4663 | |
| 4664 | /** |
| 4665 | * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER |
| 4666 | * with the rounded first argument. For details refer to |
| 4667 | * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round. |
| 4668 | * |
| 4669 | * @param[in] args Array of arguments. |
| 4670 | * @param[in] arg_count Count of elements in @p args. |
| 4671 | * @param[in,out] set Context and result set at the same time. |
| 4672 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4673 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4674 | */ |
| 4675 | static LY_ERR |
| 4676 | xpath_round(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 4677 | { |
| 4678 | struct lysc_node_leaf *sleaf; |
| 4679 | LY_ERR rc = LY_SUCCESS; |
| 4680 | |
| 4681 | if (options & LYXP_SCNODE_ALL) { |
| 4682 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4683 | 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] | 4684 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4685 | 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] | 4686 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) { |
| 4687 | 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] | 4688 | } |
| 4689 | set_scnode_clear_ctx(set); |
| 4690 | return rc; |
| 4691 | } |
| 4692 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4693 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4694 | LY_CHECK_RET(rc); |
| 4695 | |
| 4696 | /* cover only the cases where floor can't be used */ |
| 4697 | if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) { |
| 4698 | set_fill_number(set, -0.0f); |
| 4699 | } else { |
| 4700 | args[0]->val.num += 0.5; |
| 4701 | rc = xpath_floor(args, 1, args[0], options); |
| 4702 | LY_CHECK_RET(rc); |
| 4703 | set_fill_number(set, args[0]->val.num); |
| 4704 | } |
| 4705 | |
| 4706 | return LY_SUCCESS; |
| 4707 | } |
| 4708 | |
| 4709 | /** |
| 4710 | * @brief Execute the XPath starts-with(string, string) function. |
| 4711 | * Returns LYXP_SET_BOOLEAN whether the second argument is |
| 4712 | * the prefix of the first or not. |
| 4713 | * |
| 4714 | * @param[in] args Array of arguments. |
| 4715 | * @param[in] arg_count Count of elements in @p args. |
| 4716 | * @param[in,out] set Context and result set at the same time. |
| 4717 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4718 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4719 | */ |
| 4720 | static LY_ERR |
| 4721 | xpath_starts_with(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 4722 | { |
| 4723 | struct lysc_node_leaf *sleaf; |
| 4724 | LY_ERR rc = LY_SUCCESS; |
| 4725 | |
| 4726 | if (options & LYXP_SCNODE_ALL) { |
| 4727 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4728 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4729 | 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] | 4730 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4731 | 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] | 4732 | } |
| 4733 | } |
| 4734 | |
| 4735 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4736 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4737 | 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] | 4738 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4739 | 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] | 4740 | } |
| 4741 | } |
| 4742 | set_scnode_clear_ctx(set); |
| 4743 | return rc; |
| 4744 | } |
| 4745 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4746 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4747 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4748 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4749 | LY_CHECK_RET(rc); |
| 4750 | |
| 4751 | if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) { |
| 4752 | set_fill_boolean(set, 0); |
| 4753 | } else { |
| 4754 | set_fill_boolean(set, 1); |
| 4755 | } |
| 4756 | |
| 4757 | return LY_SUCCESS; |
| 4758 | } |
| 4759 | |
| 4760 | /** |
| 4761 | * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING |
| 4762 | * with the string representation of either the argument or the context. |
| 4763 | * |
| 4764 | * @param[in] args Array of arguments. |
| 4765 | * @param[in] arg_count Count of elements in @p args. |
| 4766 | * @param[in,out] set Context and result set at the same time. |
| 4767 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4768 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4769 | */ |
| 4770 | static LY_ERR |
| 4771 | xpath_string(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options) |
| 4772 | { |
| 4773 | LY_ERR rc; |
| 4774 | |
| 4775 | if (options & LYXP_SCNODE_ALL) { |
| 4776 | set_scnode_clear_ctx(set); |
| 4777 | return LY_SUCCESS; |
| 4778 | } |
| 4779 | |
| 4780 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4781 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4782 | LY_CHECK_RET(rc); |
| 4783 | set_fill_set(set, args[0]); |
| 4784 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4785 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4786 | LY_CHECK_RET(rc); |
| 4787 | } |
| 4788 | |
| 4789 | return LY_SUCCESS; |
| 4790 | } |
| 4791 | |
| 4792 | /** |
| 4793 | * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER |
| 4794 | * with the length of the string in either the argument or the context. |
| 4795 | * |
| 4796 | * @param[in] args Array of arguments. |
| 4797 | * @param[in] arg_count Count of elements in @p args. |
| 4798 | * @param[in,out] set Context and result set at the same time. |
| 4799 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4800 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4801 | */ |
| 4802 | static LY_ERR |
| 4803 | xpath_string_length(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options) |
| 4804 | { |
| 4805 | struct lysc_node_leaf *sleaf; |
| 4806 | LY_ERR rc = LY_SUCCESS; |
| 4807 | |
| 4808 | if (options & LYXP_SCNODE_ALL) { |
| 4809 | if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4810 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4811 | 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] | 4812 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4813 | 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] | 4814 | } |
| 4815 | } |
| 4816 | if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) { |
| 4817 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4818 | 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] | 4819 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4820 | 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] | 4821 | } |
| 4822 | } |
| 4823 | set_scnode_clear_ctx(set); |
| 4824 | return rc; |
| 4825 | } |
| 4826 | |
| 4827 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4828 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4829 | LY_CHECK_RET(rc); |
| 4830 | set_fill_number(set, strlen(args[0]->val.str)); |
| 4831 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4832 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4833 | LY_CHECK_RET(rc); |
| 4834 | set_fill_number(set, strlen(set->val.str)); |
| 4835 | } |
| 4836 | |
| 4837 | return LY_SUCCESS; |
| 4838 | } |
| 4839 | |
| 4840 | /** |
| 4841 | * @brief Execute the XPath substring(string, number, number?) function. |
| 4842 | * Returns LYXP_SET_STRING substring of the first argument starting |
| 4843 | * on the second argument index ending on the third argument index, |
| 4844 | * indexed from 1. For exact definition refer to |
| 4845 | * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring. |
| 4846 | * |
| 4847 | * @param[in] args Array of arguments. |
| 4848 | * @param[in] arg_count Count of elements in @p args. |
| 4849 | * @param[in,out] set Context and result set at the same time. |
| 4850 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4851 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4852 | */ |
| 4853 | static LY_ERR |
| 4854 | xpath_substring(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options) |
| 4855 | { |
| 4856 | int start, len; |
| 4857 | uint16_t str_start, str_len, pos; |
| 4858 | struct lysc_node_leaf *sleaf; |
| 4859 | LY_ERR rc = LY_SUCCESS; |
| 4860 | |
| 4861 | if (options & LYXP_SCNODE_ALL) { |
| 4862 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4863 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4864 | 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] | 4865 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4866 | 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] | 4867 | } |
| 4868 | } |
| 4869 | |
| 4870 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4871 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4872 | 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] | 4873 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 4874 | 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] | 4875 | } |
| 4876 | } |
| 4877 | |
| 4878 | if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET) |
| 4879 | && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) { |
| 4880 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4881 | 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] | 4882 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 4883 | 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] | 4884 | } |
| 4885 | } |
| 4886 | set_scnode_clear_ctx(set); |
| 4887 | return rc; |
| 4888 | } |
| 4889 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4890 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4891 | LY_CHECK_RET(rc); |
| 4892 | |
| 4893 | /* start */ |
| 4894 | if (xpath_round(&args[1], 1, args[1], options)) { |
| 4895 | return -1; |
| 4896 | } |
| 4897 | if (isfinite(args[1]->val.num)) { |
| 4898 | start = args[1]->val.num - 1; |
| 4899 | } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) { |
| 4900 | start = INT_MIN; |
| 4901 | } else { |
| 4902 | start = INT_MAX; |
| 4903 | } |
| 4904 | |
| 4905 | /* len */ |
| 4906 | if (arg_count == 3) { |
| 4907 | rc = xpath_round(&args[2], 1, args[2], options); |
| 4908 | LY_CHECK_RET(rc); |
| 4909 | if (isfinite(args[2]->val.num)) { |
| 4910 | len = args[2]->val.num; |
| 4911 | } else if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) { |
| 4912 | len = 0; |
| 4913 | } else { |
| 4914 | len = INT_MAX; |
| 4915 | } |
| 4916 | } else { |
| 4917 | len = INT_MAX; |
| 4918 | } |
| 4919 | |
| 4920 | /* find matching character positions */ |
| 4921 | str_start = 0; |
| 4922 | str_len = 0; |
| 4923 | for (pos = 0; args[0]->val.str[pos]; ++pos) { |
| 4924 | if (pos < start) { |
| 4925 | ++str_start; |
| 4926 | } else if (pos < start + len) { |
| 4927 | ++str_len; |
| 4928 | } else { |
| 4929 | break; |
| 4930 | } |
| 4931 | } |
| 4932 | |
| 4933 | set_fill_string(set, args[0]->val.str + str_start, str_len); |
| 4934 | return LY_SUCCESS; |
| 4935 | } |
| 4936 | |
| 4937 | /** |
| 4938 | * @brief Execute the XPath substring-after(string, string) function. |
| 4939 | * Returns LYXP_SET_STRING with the string succeeding the occurance |
| 4940 | * of the second argument in the first or an empty string. |
| 4941 | * |
| 4942 | * @param[in] args Array of arguments. |
| 4943 | * @param[in] arg_count Count of elements in @p args. |
| 4944 | * @param[in,out] set Context and result set at the same time. |
| 4945 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4946 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4947 | */ |
| 4948 | static LY_ERR |
| 4949 | xpath_substring_after(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 4950 | { |
| 4951 | char *ptr; |
| 4952 | struct lysc_node_leaf *sleaf; |
| 4953 | LY_ERR rc = LY_SUCCESS; |
| 4954 | |
| 4955 | if (options & LYXP_SCNODE_ALL) { |
| 4956 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4957 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4958 | 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] | 4959 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4960 | 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] | 4961 | } |
| 4962 | } |
| 4963 | |
| 4964 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4965 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4966 | 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] | 4967 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4968 | 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] | 4969 | } |
| 4970 | } |
| 4971 | set_scnode_clear_ctx(set); |
| 4972 | return rc; |
| 4973 | } |
| 4974 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4975 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4976 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4977 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4978 | LY_CHECK_RET(rc); |
| 4979 | |
| 4980 | ptr = strstr(args[0]->val.str, args[1]->val.str); |
| 4981 | if (ptr) { |
| 4982 | set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str))); |
| 4983 | } else { |
| 4984 | set_fill_string(set, "", 0); |
| 4985 | } |
| 4986 | |
| 4987 | return LY_SUCCESS; |
| 4988 | } |
| 4989 | |
| 4990 | /** |
| 4991 | * @brief Execute the XPath substring-before(string, string) function. |
| 4992 | * Returns LYXP_SET_STRING with the string preceding the occurance |
| 4993 | * of the second argument in the first or an empty string. |
| 4994 | * |
| 4995 | * @param[in] args Array of arguments. |
| 4996 | * @param[in] arg_count Count of elements in @p args. |
| 4997 | * @param[in,out] set Context and result set at the same time. |
| 4998 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4999 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5000 | */ |
| 5001 | static LY_ERR |
| 5002 | xpath_substring_before(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 5003 | { |
| 5004 | char *ptr; |
| 5005 | struct lysc_node_leaf *sleaf; |
| 5006 | LY_ERR rc = LY_SUCCESS; |
| 5007 | |
| 5008 | if (options & LYXP_SCNODE_ALL) { |
| 5009 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 5010 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5011 | 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] | 5012 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5013 | 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] | 5014 | } |
| 5015 | } |
| 5016 | |
| 5017 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 5018 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5019 | 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] | 5020 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5021 | 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] | 5022 | } |
| 5023 | } |
| 5024 | set_scnode_clear_ctx(set); |
| 5025 | return rc; |
| 5026 | } |
| 5027 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5028 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5029 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5030 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5031 | LY_CHECK_RET(rc); |
| 5032 | |
| 5033 | ptr = strstr(args[0]->val.str, args[1]->val.str); |
| 5034 | if (ptr) { |
| 5035 | set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str); |
| 5036 | } else { |
| 5037 | set_fill_string(set, "", 0); |
| 5038 | } |
| 5039 | |
| 5040 | return LY_SUCCESS; |
| 5041 | } |
| 5042 | |
| 5043 | /** |
| 5044 | * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER |
| 5045 | * with the sum of all the nodes in the context. |
| 5046 | * |
| 5047 | * @param[in] args Array of arguments. |
| 5048 | * @param[in] arg_count Count of elements in @p args. |
| 5049 | * @param[in,out] set Context and result set at the same time. |
| 5050 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5051 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5052 | */ |
| 5053 | static LY_ERR |
| 5054 | xpath_sum(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 5055 | { |
| 5056 | long double num; |
| 5057 | char *str; |
| 5058 | uint16_t i; |
| 5059 | struct lyxp_set set_item; |
| 5060 | struct lysc_node_leaf *sleaf; |
| 5061 | LY_ERR rc = LY_SUCCESS; |
| 5062 | |
| 5063 | if (options & LYXP_SCNODE_ALL) { |
| 5064 | if (args[0]->type == LYXP_SET_SCNODE_SET) { |
| 5065 | for (i = 0; i < args[0]->used; ++i) { |
| 5066 | if (args[0]->val.scnodes[i].in_ctx == 1) { |
| 5067 | sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode; |
| 5068 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5069 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, |
| 5070 | lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5071 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 5072 | 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] | 5073 | } |
| 5074 | } |
| 5075 | } |
| 5076 | } |
| 5077 | set_scnode_clear_ctx(set); |
| 5078 | return rc; |
| 5079 | } |
| 5080 | |
| 5081 | set_fill_number(set, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5082 | |
| 5083 | if (args[0]->type != LYXP_SET_NODE_SET) { |
| 5084 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "sum(node-set)"); |
| 5085 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5086 | } else if (!args[0]->used) { |
| 5087 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5088 | } |
| 5089 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5090 | set_init(&set_item, set); |
| 5091 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5092 | set_item.type = LYXP_SET_NODE_SET; |
| 5093 | set_item.val.nodes = malloc(sizeof *set_item.val.nodes); |
| 5094 | LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM); |
| 5095 | |
| 5096 | set_item.used = 1; |
| 5097 | set_item.size = 1; |
| 5098 | |
| 5099 | for (i = 0; i < args[0]->used; ++i) { |
| 5100 | set_item.val.nodes[0] = args[0]->val.nodes[i]; |
| 5101 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5102 | rc = cast_node_set_to_string(&set_item, &str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5103 | LY_CHECK_RET(rc); |
| 5104 | num = cast_string_to_number(str); |
| 5105 | free(str); |
| 5106 | set->val.num += num; |
| 5107 | } |
| 5108 | |
| 5109 | free(set_item.val.nodes); |
| 5110 | |
| 5111 | return LY_SUCCESS; |
| 5112 | } |
| 5113 | |
| 5114 | /** |
| 5115 | * @brief Execute the XPath text() function (node type). Returns LYXP_SET_NODE_SET |
| 5116 | * with the text content of the nodes in the context. |
| 5117 | * |
| 5118 | * @param[in] args Array of arguments. |
| 5119 | * @param[in] arg_count Count of elements in @p args. |
| 5120 | * @param[in,out] set Context and result set at the same time. |
| 5121 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5122 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5123 | */ |
| 5124 | static LY_ERR |
| 5125 | xpath_text(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 5126 | { |
| 5127 | uint32_t i; |
| 5128 | |
| 5129 | if (options & LYXP_SCNODE_ALL) { |
| 5130 | set_scnode_clear_ctx(set); |
| 5131 | return LY_SUCCESS; |
| 5132 | } |
| 5133 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5134 | if (set->type != LYXP_SET_NODE_SET) { |
| 5135 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "text()"); |
| 5136 | return LY_EVALID; |
| 5137 | } |
| 5138 | |
| 5139 | for (i = 0; i < set->used;) { |
| 5140 | switch (set->val.nodes[i].type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 5141 | case LYXP_NODE_NONE: |
| 5142 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5143 | case LYXP_NODE_ELEM: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5144 | if (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 5145 | set->val.nodes[i].type = LYXP_NODE_TEXT; |
| 5146 | ++i; |
| 5147 | break; |
| 5148 | } |
| 5149 | /* fall through */ |
| 5150 | case LYXP_NODE_ROOT: |
| 5151 | case LYXP_NODE_ROOT_CONFIG: |
| 5152 | case LYXP_NODE_TEXT: |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5153 | case LYXP_NODE_META: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5154 | set_remove_node(set, i); |
| 5155 | break; |
| 5156 | } |
| 5157 | } |
| 5158 | |
| 5159 | return LY_SUCCESS; |
| 5160 | } |
| 5161 | |
| 5162 | /** |
| 5163 | * @brief Execute the XPath translate(string, string, string) function. |
| 5164 | * Returns LYXP_SET_STRING with the first argument with the characters |
| 5165 | * from the second argument replaced by those on the corresponding |
| 5166 | * positions in the third argument. |
| 5167 | * |
| 5168 | * @param[in] args Array of arguments. |
| 5169 | * @param[in] arg_count Count of elements in @p args. |
| 5170 | * @param[in,out] set Context and result set at the same time. |
| 5171 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5172 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5173 | */ |
| 5174 | static LY_ERR |
| 5175 | xpath_translate(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 5176 | { |
| 5177 | uint16_t i, j, new_used; |
| 5178 | char *new; |
| 5179 | int found, have_removed; |
| 5180 | struct lysc_node_leaf *sleaf; |
| 5181 | LY_ERR rc = LY_SUCCESS; |
| 5182 | |
| 5183 | if (options & LYXP_SCNODE_ALL) { |
| 5184 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 5185 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5186 | 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] | 5187 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5188 | 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] | 5189 | } |
| 5190 | } |
| 5191 | |
| 5192 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 5193 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5194 | 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] | 5195 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5196 | 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] | 5197 | } |
| 5198 | } |
| 5199 | |
| 5200 | if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) { |
| 5201 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5202 | 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] | 5203 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5204 | 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] | 5205 | } |
| 5206 | } |
| 5207 | set_scnode_clear_ctx(set); |
| 5208 | return rc; |
| 5209 | } |
| 5210 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5211 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5212 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5213 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5214 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5215 | rc = lyxp_set_cast(args[2], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5216 | LY_CHECK_RET(rc); |
| 5217 | |
| 5218 | new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char)); |
| 5219 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 5220 | new_used = 0; |
| 5221 | |
| 5222 | have_removed = 0; |
| 5223 | for (i = 0; args[0]->val.str[i]; ++i) { |
| 5224 | found = 0; |
| 5225 | |
| 5226 | for (j = 0; args[1]->val.str[j]; ++j) { |
| 5227 | if (args[0]->val.str[i] == args[1]->val.str[j]) { |
| 5228 | /* removing this char */ |
| 5229 | if (j >= strlen(args[2]->val.str)) { |
| 5230 | have_removed = 1; |
| 5231 | found = 1; |
| 5232 | break; |
| 5233 | } |
| 5234 | /* replacing this char */ |
| 5235 | new[new_used] = args[2]->val.str[j]; |
| 5236 | ++new_used; |
| 5237 | found = 1; |
| 5238 | break; |
| 5239 | } |
| 5240 | } |
| 5241 | |
| 5242 | /* copying this char */ |
| 5243 | if (!found) { |
| 5244 | new[new_used] = args[0]->val.str[i]; |
| 5245 | ++new_used; |
| 5246 | } |
| 5247 | } |
| 5248 | |
| 5249 | if (have_removed) { |
| 5250 | new = ly_realloc(new, (new_used + 1) * sizeof(char)); |
| 5251 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 5252 | } |
| 5253 | new[new_used] = '\0'; |
| 5254 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5255 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5256 | set->type = LYXP_SET_STRING; |
| 5257 | set->val.str = new; |
| 5258 | |
| 5259 | return LY_SUCCESS; |
| 5260 | } |
| 5261 | |
| 5262 | /** |
| 5263 | * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN |
| 5264 | * with true value. |
| 5265 | * |
| 5266 | * @param[in] args Array of arguments. |
| 5267 | * @param[in] arg_count Count of elements in @p args. |
| 5268 | * @param[in,out] set Context and result set at the same time. |
| 5269 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5270 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5271 | */ |
| 5272 | static LY_ERR |
| 5273 | xpath_true(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 5274 | { |
| 5275 | if (options & LYXP_SCNODE_ALL) { |
| 5276 | set_scnode_clear_ctx(set); |
| 5277 | return LY_SUCCESS; |
| 5278 | } |
| 5279 | |
| 5280 | set_fill_boolean(set, 1); |
| 5281 | return LY_SUCCESS; |
| 5282 | } |
| 5283 | |
| 5284 | /* |
| 5285 | * moveto functions |
| 5286 | * |
| 5287 | * They and only they actually change the context (set). |
| 5288 | */ |
| 5289 | |
| 5290 | /** |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5291 | * @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] | 5292 | * |
Michal Vasko | 2104e9f | 2020-03-06 08:23:25 +0100 | [diff] [blame] | 5293 | * XPath @p set is expected to be a (sc)node set! |
| 5294 | * |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5295 | * @param[in,out] qname Qualified node name. If includes prefix, it is skipped. |
| 5296 | * @param[in,out] qname_len Length of @p qname, is updated accordingly. |
| 5297 | * @param[in] set Set with XPath context. |
| 5298 | * @param[out] moveto_mod Expected module of a matching node. |
| 5299 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5300 | */ |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5301 | static LY_ERR |
| 5302 | 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] | 5303 | { |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 5304 | const struct lys_module *mod = NULL; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5305 | const char *ptr; |
| 5306 | int pref_len; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5307 | char *str; |
| 5308 | |
Michal Vasko | 2104e9f | 2020-03-06 08:23:25 +0100 | [diff] [blame] | 5309 | assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET)); |
| 5310 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5311 | if ((ptr = ly_strnchr(*qname, ':', *qname_len))) { |
| 5312 | /* specific module */ |
| 5313 | pref_len = ptr - *qname; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5314 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5315 | switch (set->format) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 5316 | case LYD_SCHEMA: |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5317 | /* schema, search all local module imports */ |
| 5318 | mod = lys_module_find_prefix(set->local_mod, *qname, pref_len); |
| 5319 | break; |
| 5320 | case LYD_JSON: |
| 5321 | /* JSON data, search in context */ |
| 5322 | str = strndup(*qname, pref_len); |
Michal Vasko | 97e76fd | 2020-05-27 15:22:01 +0200 | [diff] [blame] | 5323 | mod = ly_ctx_get_module_implemented(set->ctx, str); |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5324 | free(str); |
| 5325 | break; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 5326 | case LYD_XML: |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 5327 | case LYD_LYB: |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5328 | LOGINT_RET(set->ctx); |
| 5329 | } |
| 5330 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5331 | /* check for errors and non-implemented modules, as they are not valid */ |
Juraj Vijtiuk | d75faa6 | 2019-11-26 14:10:10 +0100 | [diff] [blame] | 5332 | if (!mod || !mod->implemented) { |
Michal Vasko | 2104e9f | 2020-03-06 08:23:25 +0100 | [diff] [blame] | 5333 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 5334 | LOGVAL(set->ctx, LY_VLOG_LYSC, set->ctx_scnode, LY_VCODE_XP_INMOD, pref_len, *qname); |
| 5335 | } else { |
| 5336 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INMOD, pref_len, *qname); |
| 5337 | } |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5338 | return LY_EVALID; |
| 5339 | } |
Juraj Vijtiuk | d75faa6 | 2019-11-26 14:10:10 +0100 | [diff] [blame] | 5340 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5341 | *qname += pref_len + 1; |
| 5342 | *qname_len -= pref_len + 1; |
| 5343 | } else if (((*qname)[0] == '*') && (*qname_len == 1)) { |
| 5344 | /* all modules - special case */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5345 | mod = NULL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5346 | } else if (set->type == LYXP_SET_SCNODE_SET) { |
| 5347 | /* current node module */ |
| 5348 | mod = set->ctx_scnode->module; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5349 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5350 | /* current node module */ |
| 5351 | mod = set->ctx_node->schema->module; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5352 | } |
| 5353 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5354 | *moveto_mod = mod; |
| 5355 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5356 | } |
| 5357 | |
| 5358 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5359 | * @brief Move context @p set to the root. Handles absolute path. |
| 5360 | * Result is LYXP_SET_NODE_SET. |
| 5361 | * |
| 5362 | * @param[in,out] set Set to use. |
| 5363 | * @param[in] options Xpath options. |
| 5364 | */ |
| 5365 | static void |
| 5366 | moveto_root(struct lyxp_set *set, int options) |
| 5367 | { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5368 | if (!set) { |
| 5369 | return; |
| 5370 | } |
| 5371 | |
| 5372 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5373 | set_scnode_clear_ctx(set); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 5374 | lyxp_set_scnode_insert_node(set, NULL, set->root_type); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5375 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5376 | set->type = LYXP_SET_NODE_SET; |
| 5377 | set->used = 0; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5378 | set_insert_node(set, NULL, 0, set->root_type, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5379 | } |
| 5380 | } |
| 5381 | |
| 5382 | /** |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 5383 | * @brief Check whether a node has some unresolved "when". |
| 5384 | * |
| 5385 | * @param[in] node Node to check. |
| 5386 | * @return LY_ERR value (LY_EINCOMPLETE if there are some unresolved "when") |
| 5387 | */ |
| 5388 | static LY_ERR |
| 5389 | moveto_when_check(const struct lyd_node *node) |
| 5390 | { |
| 5391 | const struct lysc_node *schema; |
| 5392 | |
| 5393 | if (!node) { |
| 5394 | return LY_SUCCESS; |
| 5395 | } |
| 5396 | |
| 5397 | schema = node->schema; |
| 5398 | do { |
| 5399 | if (schema->when && !(node->flags & LYD_WHEN_TRUE)) { |
| 5400 | return LY_EINCOMPLETE; |
| 5401 | } |
| 5402 | schema = schema->parent; |
| 5403 | } while (schema && (schema->nodetype & (LYS_CASE | LYS_CHOICE))); |
| 5404 | |
| 5405 | return LY_SUCCESS; |
| 5406 | } |
| 5407 | |
| 5408 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5409 | * @brief Check @p node as a part of NameTest processing. |
| 5410 | * |
| 5411 | * @param[in] node Node to check. |
| 5412 | * @param[in] root_type XPath root node type. |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5413 | * @param[in] context_op XPath operation parent. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5414 | * @param[in] node_name Node name in the dictionary to move to, NULL for any node. |
| 5415 | * @param[in] moveto_mod Expected module of the node, NULL for any. |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5416 | * @return LY_ERR (LY_ENOT if node does not match, LY_EINCOMPLETE on unresolved when, |
| 5417 | * LY_EINVAL if netither node nor any children match) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5418 | */ |
| 5419 | static LY_ERR |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5420 | moveto_node_check(const struct lyd_node *node, enum lyxp_node_type root_type, const struct lysc_node *context_op, |
| 5421 | const char *node_name, const struct lys_module *moveto_mod) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5422 | { |
| 5423 | /* module check */ |
| 5424 | if (moveto_mod && (node->schema->module != moveto_mod)) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5425 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5426 | } |
| 5427 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5428 | /* context check */ |
| 5429 | 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] | 5430 | return LY_EINVAL; |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5431 | } else if (context_op && (node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node->schema != context_op)) { |
| 5432 | return LY_EINVAL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5433 | } |
| 5434 | |
| 5435 | /* name check */ |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5436 | if (node_name && strcmp(node_name, "*") && (node->schema->name != node_name)) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5437 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5438 | } |
| 5439 | |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 5440 | /* when check */ |
| 5441 | if (moveto_when_check(node)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5442 | return LY_EINCOMPLETE; |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 5443 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5444 | |
| 5445 | /* match */ |
| 5446 | return LY_SUCCESS; |
| 5447 | } |
| 5448 | |
| 5449 | /** |
| 5450 | * @brief Check @p node as a part of schema NameTest processing. |
| 5451 | * |
| 5452 | * @param[in] node Schema node to check. |
| 5453 | * @param[in] root_type XPath root node type. |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5454 | * @param[in] context_op XPath operation parent. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5455 | * @param[in] node_name Node name in the dictionary to move to, NULL for any nodes. |
| 5456 | * @param[in] moveto_mod Expected module of the node, NULL for any. |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5457 | * @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] | 5458 | */ |
| 5459 | static LY_ERR |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5460 | moveto_scnode_check(const struct lysc_node *node, enum lyxp_node_type root_type, const struct lysc_node *context_op, |
| 5461 | const char *node_name, const struct lys_module *moveto_mod) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5462 | { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5463 | /* module check */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5464 | if (moveto_mod && (node->module != moveto_mod)) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5465 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5466 | } |
| 5467 | |
| 5468 | /* context check */ |
| 5469 | if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) { |
| 5470 | return LY_EINVAL; |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5471 | } else if (context_op && (node->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node != context_op)) { |
| 5472 | return LY_EINVAL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5473 | } |
| 5474 | |
| 5475 | /* name check */ |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5476 | if (node_name && strcmp(node_name, "*") && (node->name != node_name)) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5477 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5478 | } |
| 5479 | |
| 5480 | /* match */ |
| 5481 | return LY_SUCCESS; |
| 5482 | } |
| 5483 | |
| 5484 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5485 | * @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] | 5486 | * |
| 5487 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5488 | * @param[in] mod Matching node module, NULL for any. |
| 5489 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5490 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 5491 | */ |
| 5492 | static LY_ERR |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5493 | 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] | 5494 | { |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 5495 | uint32_t i; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5496 | int replaced; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5497 | const struct lyd_node *siblings, *sub; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5498 | LY_ERR rc; |
| 5499 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5500 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5501 | return LY_SUCCESS; |
| 5502 | } |
| 5503 | |
| 5504 | if (set->type != LYXP_SET_NODE_SET) { |
| 5505 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 5506 | return LY_EVALID; |
| 5507 | } |
| 5508 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5509 | for (i = 0; i < set->used; ) { |
| 5510 | replaced = 0; |
| 5511 | |
| 5512 | 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] | 5513 | assert(!set->val.nodes[i].node); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5514 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5515 | /* search in all the trees */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5516 | siblings = set->tree; |
Michal Vasko | 5bfd4be | 2020-06-23 13:26:19 +0200 | [diff] [blame] | 5517 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5518 | /* search in children */ |
Michal Vasko | 5bfd4be | 2020-06-23 13:26:19 +0200 | [diff] [blame] | 5519 | siblings = lyd_node_children(set->val.nodes[i].node, 0); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5520 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5521 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5522 | for (sub = siblings; sub; sub = sub->next) { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5523 | 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] | 5524 | if (rc == LY_SUCCESS) { |
| 5525 | if (!replaced) { |
| 5526 | set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i); |
| 5527 | replaced = 1; |
| 5528 | } else { |
| 5529 | set_insert_node(set, sub, 0, LYXP_NODE_ELEM, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5530 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5531 | ++i; |
| 5532 | } else if (rc == LY_EINCOMPLETE) { |
| 5533 | return rc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5534 | } |
| 5535 | } |
| 5536 | |
| 5537 | if (!replaced) { |
| 5538 | /* no match */ |
| 5539 | set_remove_node(set, i); |
| 5540 | } |
| 5541 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5542 | |
| 5543 | return LY_SUCCESS; |
| 5544 | } |
| 5545 | |
| 5546 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5547 | * @brief Move context @p set to a node using hashes. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY). |
| 5548 | * Context position aware. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5549 | * |
| 5550 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5551 | * @param[in] scnode Matching node schema. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5552 | * @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] | 5553 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 5554 | */ |
| 5555 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5556 | 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] | 5557 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5558 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5559 | uint32_t i; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5560 | const struct lyd_node *siblings; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5561 | struct lyd_node *sub, *inst = NULL; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5562 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5563 | assert(scnode && (!(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) || predicates)); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5564 | |
| 5565 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5566 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5567 | } |
| 5568 | |
| 5569 | if (set->type != LYXP_SET_NODE_SET) { |
| 5570 | 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] | 5571 | ret = LY_EVALID; |
| 5572 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5573 | } |
| 5574 | |
| 5575 | /* context check for all the nodes since we have the schema node */ |
| 5576 | if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) { |
| 5577 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5578 | goto cleanup; |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5579 | } else if (set->context_op && (scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) |
| 5580 | && (scnode != set->context_op)) { |
| 5581 | lyxp_set_free_content(set); |
| 5582 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5583 | } |
| 5584 | |
| 5585 | /* create specific data instance if needed */ |
| 5586 | if (scnode->nodetype == LYS_LIST) { |
| 5587 | LY_CHECK_GOTO(ret = lyd_create_list(scnode, predicates, &inst), cleanup); |
| 5588 | } else if (scnode->nodetype == LYS_LEAFLIST) { |
| 5589 | 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] | 5590 | } |
| 5591 | |
| 5592 | for (i = 0; i < set->used; ) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5593 | siblings = NULL; |
| 5594 | |
| 5595 | if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) { |
| 5596 | assert(!set->val.nodes[i].node); |
| 5597 | |
| 5598 | /* search in all the trees */ |
| 5599 | siblings = set->tree; |
| 5600 | } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
| 5601 | /* search in children */ |
Michal Vasko | 5bfd4be | 2020-06-23 13:26:19 +0200 | [diff] [blame] | 5602 | siblings = lyd_node_children(set->val.nodes[i].node, 0); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5603 | } |
| 5604 | |
| 5605 | /* find the node using hashes */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5606 | if (inst) { |
| 5607 | lyd_find_sibling_first(siblings, inst, &sub); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5608 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5609 | lyd_find_sibling_val(siblings, scnode, NULL, 0, &sub); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5610 | } |
| 5611 | |
| 5612 | /* when check */ |
| 5613 | if (sub && moveto_when_check(sub)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5614 | ret = LY_EINCOMPLETE; |
| 5615 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5616 | } |
| 5617 | |
| 5618 | if (sub) { |
| 5619 | /* pos filled later */ |
Michal Vasko | cb1b7c0 | 2020-07-03 13:38:12 +0200 | [diff] [blame] | 5620 | set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5621 | ++i; |
Michal Vasko | cb1b7c0 | 2020-07-03 13:38:12 +0200 | [diff] [blame] | 5622 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5623 | /* no match */ |
| 5624 | set_remove_node(set, i); |
| 5625 | } |
| 5626 | } |
| 5627 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5628 | cleanup: |
| 5629 | lyd_free_tree(inst); |
| 5630 | return ret; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5631 | } |
| 5632 | |
| 5633 | /** |
| 5634 | * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY). |
| 5635 | * |
| 5636 | * @param[in,out] set Set to use. |
| 5637 | * @param[in] mod Matching node module, NULL for any. |
| 5638 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5639 | * @param[in] options XPath options. |
| 5640 | * @return LY_ERR |
| 5641 | */ |
| 5642 | static LY_ERR |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5643 | 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] | 5644 | { |
Michal Vasko | cafad9d | 2019-11-07 15:20:03 +0100 | [diff] [blame] | 5645 | int i, orig_used, idx, temp_ctx = 0, getnext_opts; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5646 | uint32_t mod_idx; |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 5647 | const struct lysc_node *iter, *start_parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5648 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5649 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5650 | return LY_SUCCESS; |
| 5651 | } |
| 5652 | |
| 5653 | if (set->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 5654 | 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] | 5655 | return LY_EVALID; |
| 5656 | } |
| 5657 | |
Michal Vasko | cafad9d | 2019-11-07 15:20:03 +0100 | [diff] [blame] | 5658 | /* getnext opts */ |
| 5659 | getnext_opts = LYS_GETNEXT_NOSTATECHECK; |
| 5660 | if (options & LYXP_SCNODE_OUTPUT) { |
| 5661 | getnext_opts |= LYS_GETNEXT_OUTPUT; |
| 5662 | } |
| 5663 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5664 | orig_used = set->used; |
| 5665 | for (i = 0; i < orig_used; ++i) { |
| 5666 | if (set->val.scnodes[i].in_ctx != 1) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5667 | if (set->val.scnodes[i].in_ctx != -2) { |
| 5668 | continue; |
| 5669 | } |
| 5670 | |
| 5671 | /* remember context node */ |
| 5672 | set->val.scnodes[i].in_ctx = -1; |
Michal Vasko | ec4df48 | 2019-12-16 10:02:18 +0100 | [diff] [blame] | 5673 | } else { |
| 5674 | set->val.scnodes[i].in_ctx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5675 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5676 | |
| 5677 | start_parent = set->val.scnodes[i].scnode; |
| 5678 | |
| 5679 | 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] | 5680 | /* 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] | 5681 | * so use it directly (root node itself is useless in this case) */ |
| 5682 | mod_idx = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5683 | 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] | 5684 | iter = NULL; |
Michal Vasko | 509de4d | 2019-12-10 14:51:30 +0100 | [diff] [blame] | 5685 | /* module may not be implemented */ |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 5686 | while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5687 | 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] | 5688 | idx = lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5689 | /* we need to prevent these nodes from being considered in this moveto */ |
| 5690 | if ((idx < orig_used) && (idx > i)) { |
| 5691 | set->val.scnodes[idx].in_ctx = 2; |
| 5692 | temp_ctx = 1; |
| 5693 | } |
| 5694 | } |
| 5695 | } |
| 5696 | |
| 5697 | if (!mod_idx) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5698 | /* mod was specified, we are not going through the whole context */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5699 | break; |
| 5700 | } |
| 5701 | /* next iteration */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5702 | mod = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5703 | } |
| 5704 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 5705 | } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) { |
| 5706 | iter = NULL; |
| 5707 | while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5708 | 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] | 5709 | idx = lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5710 | if ((idx < orig_used) && (idx > i)) { |
| 5711 | set->val.scnodes[idx].in_ctx = 2; |
| 5712 | temp_ctx = 1; |
| 5713 | } |
| 5714 | } |
| 5715 | } |
| 5716 | } |
| 5717 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5718 | |
| 5719 | /* correct temporary in_ctx values */ |
| 5720 | if (temp_ctx) { |
| 5721 | for (i = 0; i < orig_used; ++i) { |
| 5722 | if (set->val.scnodes[i].in_ctx == 2) { |
| 5723 | set->val.scnodes[i].in_ctx = 1; |
| 5724 | } |
| 5725 | } |
| 5726 | } |
| 5727 | |
| 5728 | return LY_SUCCESS; |
| 5729 | } |
| 5730 | |
| 5731 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5732 | * @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] | 5733 | * Context position aware. |
| 5734 | * |
| 5735 | * @param[in] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5736 | * @param[in] mod Matching node module, NULL for any. |
| 5737 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5738 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 5739 | */ |
| 5740 | static LY_ERR |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5741 | 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] | 5742 | { |
| 5743 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5744 | const struct lyd_node *next, *elem, *start; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5745 | struct lyxp_set ret_set; |
| 5746 | LY_ERR rc; |
| 5747 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5748 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5749 | return LY_SUCCESS; |
| 5750 | } |
| 5751 | |
| 5752 | if (set->type != LYXP_SET_NODE_SET) { |
| 5753 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 5754 | return LY_EVALID; |
| 5755 | } |
| 5756 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5757 | /* 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] | 5758 | rc = moveto_node(set, NULL, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5759 | LY_CHECK_RET(rc); |
| 5760 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5761 | /* 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] | 5762 | set_init(&ret_set, set); |
| 5763 | for (i = 0; i < set->used; ++i) { |
| 5764 | |
| 5765 | /* TREE DFS */ |
| 5766 | start = set->val.nodes[i].node; |
| 5767 | for (elem = next = start; elem; elem = next) { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5768 | 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] | 5769 | if (!rc) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5770 | /* add matching node into result set */ |
| 5771 | set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used); |
| 5772 | if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) { |
| 5773 | /* the node is a duplicate, we'll process it later in the set */ |
| 5774 | goto skip_children; |
| 5775 | } |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5776 | } else if (rc == LY_EINCOMPLETE) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5777 | return rc; |
| 5778 | } else if (rc == LY_EINVAL) { |
| 5779 | goto skip_children; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5780 | } |
| 5781 | |
| 5782 | /* TREE DFS NEXT ELEM */ |
| 5783 | /* select element for the next run - children first */ |
Michal Vasko | 5bfd4be | 2020-06-23 13:26:19 +0200 | [diff] [blame] | 5784 | next = lyd_node_children(elem, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5785 | if (!next) { |
| 5786 | skip_children: |
| 5787 | /* no children, so try siblings, but only if it's not the start, |
| 5788 | * that is considered to be the root and it's siblings are not traversed */ |
| 5789 | if (elem != start) { |
| 5790 | next = elem->next; |
| 5791 | } else { |
| 5792 | break; |
| 5793 | } |
| 5794 | } |
| 5795 | while (!next) { |
| 5796 | /* no siblings, go back through the parents */ |
| 5797 | if ((struct lyd_node *)elem->parent == start) { |
| 5798 | /* we are done, no next element to process */ |
| 5799 | break; |
| 5800 | } |
| 5801 | /* parent is already processed, go to its sibling */ |
| 5802 | elem = (struct lyd_node *)elem->parent; |
| 5803 | next = elem->next; |
| 5804 | } |
| 5805 | } |
| 5806 | } |
| 5807 | |
| 5808 | /* make the temporary set the current one */ |
| 5809 | ret_set.ctx_pos = set->ctx_pos; |
| 5810 | ret_set.ctx_size = set->ctx_size; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5811 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5812 | memcpy(set, &ret_set, sizeof *set); |
| 5813 | |
| 5814 | return LY_SUCCESS; |
| 5815 | } |
| 5816 | |
| 5817 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5818 | * @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] | 5819 | * |
| 5820 | * @param[in] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5821 | * @param[in] mod Matching node module, NULL for any. |
| 5822 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5823 | * @param[in] options XPath options. |
| 5824 | * @return LY_ERR |
| 5825 | */ |
| 5826 | static LY_ERR |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5827 | 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] | 5828 | { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5829 | int i, orig_used, idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5830 | const struct lysc_node *next, *elem, *start; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5831 | LY_ERR rc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5832 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5833 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5834 | return LY_SUCCESS; |
| 5835 | } |
| 5836 | |
| 5837 | if (set->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 5838 | 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] | 5839 | return LY_EVALID; |
| 5840 | } |
| 5841 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5842 | orig_used = set->used; |
| 5843 | for (i = 0; i < orig_used; ++i) { |
| 5844 | if (set->val.scnodes[i].in_ctx != 1) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5845 | if (set->val.scnodes[i].in_ctx != -2) { |
| 5846 | continue; |
| 5847 | } |
| 5848 | |
| 5849 | /* remember context node */ |
| 5850 | set->val.scnodes[i].in_ctx = -1; |
Michal Vasko | ec4df48 | 2019-12-16 10:02:18 +0100 | [diff] [blame] | 5851 | } else { |
| 5852 | set->val.scnodes[i].in_ctx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5853 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5854 | |
| 5855 | /* TREE DFS */ |
| 5856 | start = set->val.scnodes[i].scnode; |
| 5857 | for (elem = next = start; elem; elem = next) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5858 | if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) { |
| 5859 | /* schema-only nodes, skip root */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5860 | goto next_iter; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5861 | } |
| 5862 | |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5863 | 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] | 5864 | if (!rc) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 5865 | 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] | 5866 | set->val.scnodes[idx].in_ctx = 1; |
| 5867 | if (idx > i) { |
| 5868 | /* we will process it later in the set */ |
| 5869 | goto skip_children; |
| 5870 | } |
| 5871 | } else { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 5872 | lyxp_set_scnode_insert_node(set, elem, LYXP_NODE_ELEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5873 | } |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5874 | } else if (rc == LY_EINVAL) { |
| 5875 | goto skip_children; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5876 | } |
| 5877 | |
| 5878 | next_iter: |
| 5879 | /* TREE DFS NEXT ELEM */ |
| 5880 | /* select element for the next run - children first */ |
| 5881 | 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] | 5882 | if (!next) { |
| 5883 | skip_children: |
| 5884 | /* no children, so try siblings, but only if it's not the start, |
| 5885 | * that is considered to be the root and it's siblings are not traversed */ |
| 5886 | if (elem != start) { |
| 5887 | next = elem->next; |
| 5888 | } else { |
| 5889 | break; |
| 5890 | } |
| 5891 | } |
| 5892 | while (!next) { |
| 5893 | /* no siblings, go back through the parents */ |
| 5894 | if (elem->parent == start) { |
| 5895 | /* we are done, no next element to process */ |
| 5896 | break; |
| 5897 | } |
| 5898 | /* parent is already processed, go to its sibling */ |
| 5899 | elem = elem->parent; |
| 5900 | next = elem->next; |
| 5901 | } |
| 5902 | } |
| 5903 | } |
| 5904 | |
| 5905 | return LY_SUCCESS; |
| 5906 | } |
| 5907 | |
| 5908 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5909 | * @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] | 5910 | * Indirectly context position aware. |
| 5911 | * |
| 5912 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5913 | * @param[in] mod Matching metadata module, NULL for any. |
| 5914 | * @param[in] ncname Matching metadata name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5915 | * @return LY_ERR |
| 5916 | */ |
| 5917 | static LY_ERR |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5918 | 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] | 5919 | { |
| 5920 | uint32_t i; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5921 | int replaced; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5922 | struct lyd_meta *sub; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5923 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5924 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5925 | return LY_SUCCESS; |
| 5926 | } |
| 5927 | |
| 5928 | if (set->type != LYXP_SET_NODE_SET) { |
| 5929 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 5930 | return LY_EVALID; |
| 5931 | } |
| 5932 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5933 | for (i = 0; i < set->used; ) { |
| 5934 | replaced = 0; |
| 5935 | |
| 5936 | /* only attributes of an elem (not dummy) can be in the result, skip all the rest; |
| 5937 | * our attributes are always qualified */ |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5938 | if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5939 | for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5940 | |
| 5941 | /* check "namespace" */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5942 | if (mod && (sub->annotation->module != mod)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5943 | continue; |
| 5944 | } |
| 5945 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5946 | if (!ncname || (sub->name == ncname)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5947 | /* match */ |
| 5948 | if (!replaced) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5949 | set->val.meta[i].meta = sub; |
| 5950 | set->val.meta[i].type = LYXP_NODE_META; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5951 | /* pos does not change */ |
| 5952 | replaced = 1; |
| 5953 | } else { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5954 | 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] | 5955 | } |
| 5956 | ++i; |
| 5957 | } |
| 5958 | } |
| 5959 | } |
| 5960 | |
| 5961 | if (!replaced) { |
| 5962 | /* no match */ |
| 5963 | set_remove_node(set, i); |
| 5964 | } |
| 5965 | } |
| 5966 | |
| 5967 | return LY_SUCCESS; |
| 5968 | } |
| 5969 | |
| 5970 | /** |
| 5971 | * @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] | 5972 | * Result is LYXP_SET_NODE_SET. Context position aware. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5973 | * |
| 5974 | * @param[in,out] set1 Set to use for the result. |
| 5975 | * @param[in] set2 Set that is copied to @p set1. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5976 | * @return LY_ERR |
| 5977 | */ |
| 5978 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5979 | moveto_union(struct lyxp_set *set1, struct lyxp_set *set2) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5980 | { |
| 5981 | LY_ERR rc; |
| 5982 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5983 | 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] | 5984 | LOGVAL(set1->ctx, LY_VLOG_LYD, set1->ctx_node, LY_VCODE_XP_INOP_2, "union", print_set_type(set1), print_set_type(set2)); |
| 5985 | return LY_EVALID; |
| 5986 | } |
| 5987 | |
| 5988 | /* set2 is empty or both set1 and set2 */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5989 | if (!set2->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5990 | return LY_SUCCESS; |
| 5991 | } |
| 5992 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5993 | if (!set1->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5994 | memcpy(set1, set2, sizeof *set1); |
| 5995 | /* dynamic memory belongs to set1 now, do not free */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5996 | memset(set2, 0, sizeof *set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5997 | return LY_SUCCESS; |
| 5998 | } |
| 5999 | |
| 6000 | /* we assume sets are sorted */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6001 | assert(!set_sort(set1) && !set_sort(set2)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6002 | |
| 6003 | /* sort, remove duplicates */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6004 | rc = set_sorted_merge(set1, set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6005 | LY_CHECK_RET(rc); |
| 6006 | |
| 6007 | /* final set must be sorted */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6008 | assert(!set_sort(set1)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6009 | |
| 6010 | return LY_SUCCESS; |
| 6011 | } |
| 6012 | |
| 6013 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6014 | * @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] | 6015 | * Context position aware. |
| 6016 | * |
| 6017 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6018 | * @param[in] mod Matching metadata module, NULL for any. |
| 6019 | * @param[in] ncname Matching metadata name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6020 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6021 | */ |
| 6022 | static int |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6023 | 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] | 6024 | { |
| 6025 | uint32_t i; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6026 | int replaced; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6027 | struct lyd_meta *sub; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6028 | struct lyxp_set *set_all_desc = NULL; |
| 6029 | LY_ERR rc; |
| 6030 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6031 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6032 | return LY_SUCCESS; |
| 6033 | } |
| 6034 | |
| 6035 | if (set->type != LYXP_SET_NODE_SET) { |
| 6036 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 6037 | return LY_EVALID; |
| 6038 | } |
| 6039 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6040 | /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory, |
| 6041 | * but it likely won't be used much, so it's a waste of time */ |
| 6042 | /* copy the context */ |
| 6043 | set_all_desc = set_copy(set); |
| 6044 | /* get all descendant nodes (the original context nodes are removed) */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6045 | rc = moveto_node_alldesc(set_all_desc, NULL, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6046 | if (rc != LY_SUCCESS) { |
| 6047 | lyxp_set_free(set_all_desc); |
| 6048 | return rc; |
| 6049 | } |
| 6050 | /* prepend the original context nodes */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6051 | rc = moveto_union(set, set_all_desc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6052 | if (rc != LY_SUCCESS) { |
| 6053 | lyxp_set_free(set_all_desc); |
| 6054 | return rc; |
| 6055 | } |
| 6056 | lyxp_set_free(set_all_desc); |
| 6057 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6058 | for (i = 0; i < set->used; ) { |
| 6059 | replaced = 0; |
| 6060 | |
| 6061 | /* only attributes of an elem can be in the result, skip all the rest, |
| 6062 | * we have all attributes qualified in lyd tree */ |
| 6063 | if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6064 | for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6065 | /* check "namespace" */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6066 | if (mod && (sub->annotation->module != mod)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6067 | continue; |
| 6068 | } |
| 6069 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6070 | if (!ncname || (sub->name == ncname)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6071 | /* match */ |
| 6072 | if (!replaced) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6073 | set->val.meta[i].meta = sub; |
| 6074 | set->val.meta[i].type = LYXP_NODE_META; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6075 | /* pos does not change */ |
| 6076 | replaced = 1; |
| 6077 | } else { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6078 | 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] | 6079 | } |
| 6080 | ++i; |
| 6081 | } |
| 6082 | } |
| 6083 | } |
| 6084 | |
| 6085 | if (!replaced) { |
| 6086 | /* no match */ |
| 6087 | set_remove_node(set, i); |
| 6088 | } |
| 6089 | } |
| 6090 | |
| 6091 | return LY_SUCCESS; |
| 6092 | } |
| 6093 | |
| 6094 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6095 | * @brief Move context @p set to self and al chilren, recursively. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET. |
| 6096 | * Context position aware. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6097 | * |
| 6098 | * @param[in] parent Current parent. |
| 6099 | * @param[in] parent_pos Position of @p parent. |
| 6100 | * @param[in] parent_type Node type of @p parent. |
| 6101 | * @param[in,out] to_set Set to use. |
| 6102 | * @param[in] dup_check_set Set for checking duplicities. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6103 | * @param[in] options XPath options. |
| 6104 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6105 | */ |
| 6106 | static LY_ERR |
| 6107 | 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] | 6108 | 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] | 6109 | { |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6110 | const struct lyd_node *iter, *first; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6111 | LY_ERR rc; |
| 6112 | |
| 6113 | switch (parent_type) { |
| 6114 | case LYXP_NODE_ROOT: |
| 6115 | case LYXP_NODE_ROOT_CONFIG: |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6116 | assert(!parent); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6117 | |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6118 | /* add all top-level nodes as elements */ |
| 6119 | first = to_set->tree; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6120 | break; |
| 6121 | case LYXP_NODE_ELEM: |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6122 | /* add just the text node of this term element node */ |
| 6123 | if (parent->schema->nodetype & (LYD_NODE_TERM | LYD_NODE_ANY)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6124 | if (!set_dup_node_check(dup_check_set, parent, LYXP_NODE_TEXT, -1)) { |
| 6125 | set_insert_node(to_set, parent, parent_pos, LYXP_NODE_TEXT, to_set->used); |
| 6126 | } |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6127 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6128 | } |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6129 | |
| 6130 | /* add all the children of this node */ |
Michal Vasko | 5bfd4be | 2020-06-23 13:26:19 +0200 | [diff] [blame] | 6131 | first = lyd_node_children(parent, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6132 | break; |
| 6133 | default: |
| 6134 | LOGINT_RET(parent->schema->module->ctx); |
| 6135 | } |
| 6136 | |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6137 | /* add all top-level nodes as elements */ |
| 6138 | LY_LIST_FOR(first, iter) { |
| 6139 | /* context check */ |
| 6140 | if ((parent_type == LYXP_NODE_ROOT_CONFIG) && (iter->schema->flags & LYS_CONFIG_R)) { |
| 6141 | continue; |
| 6142 | } |
| 6143 | |
| 6144 | /* when check */ |
| 6145 | if (moveto_when_check(iter)) { |
| 6146 | return LY_EINCOMPLETE; |
| 6147 | } |
| 6148 | |
| 6149 | if (!set_dup_node_check(dup_check_set, iter, LYXP_NODE_ELEM, -1)) { |
| 6150 | set_insert_node(to_set, iter, 0, LYXP_NODE_ELEM, to_set->used); |
| 6151 | |
| 6152 | /* also add all the children of this node, recursively */ |
| 6153 | rc = moveto_self_add_children_r(iter, 0, LYXP_NODE_ELEM, to_set, dup_check_set, options); |
| 6154 | LY_CHECK_RET(rc); |
| 6155 | } |
| 6156 | } |
| 6157 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6158 | return LY_SUCCESS; |
| 6159 | } |
| 6160 | |
| 6161 | /** |
| 6162 | * @brief Move context @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET |
| 6163 | * (or LYXP_SET_EMPTY). Context position aware. |
| 6164 | * |
| 6165 | * @param[in,out] set Set to use. |
| 6166 | * @param[in] all_desc Whether to go to all descendants ('//') or not ('/'). |
| 6167 | * @param[in] options XPath options. |
| 6168 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6169 | */ |
| 6170 | static LY_ERR |
| 6171 | moveto_self(struct lyxp_set *set, int all_desc, int options) |
| 6172 | { |
| 6173 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6174 | struct lyxp_set ret_set; |
| 6175 | LY_ERR rc; |
| 6176 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6177 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6178 | return LY_SUCCESS; |
| 6179 | } |
| 6180 | |
| 6181 | if (set->type != LYXP_SET_NODE_SET) { |
| 6182 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 6183 | return LY_EVALID; |
| 6184 | } |
| 6185 | |
| 6186 | /* nothing to do */ |
| 6187 | if (!all_desc) { |
| 6188 | return LY_SUCCESS; |
| 6189 | } |
| 6190 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6191 | /* add all the children, they get added recursively */ |
| 6192 | set_init(&ret_set, set); |
| 6193 | for (i = 0; i < set->used; ++i) { |
| 6194 | /* copy the current node to tmp */ |
| 6195 | set_insert_node(&ret_set, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, ret_set.used); |
| 6196 | |
| 6197 | /* do not touch attributes and text nodes */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6198 | 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] | 6199 | continue; |
| 6200 | } |
| 6201 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6202 | /* add all the children */ |
| 6203 | 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] | 6204 | set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6205 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6206 | lyxp_set_free_content(&ret_set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6207 | return rc; |
| 6208 | } |
| 6209 | } |
| 6210 | |
| 6211 | /* use the temporary set as the current one */ |
| 6212 | ret_set.ctx_pos = set->ctx_pos; |
| 6213 | ret_set.ctx_size = set->ctx_size; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6214 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6215 | memcpy(set, &ret_set, sizeof *set); |
| 6216 | |
| 6217 | return LY_SUCCESS; |
| 6218 | } |
| 6219 | |
| 6220 | /** |
| 6221 | * @brief Move context schema @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_SCNODE_SET |
| 6222 | * (or LYXP_SET_EMPTY). |
| 6223 | * |
| 6224 | * @param[in,out] set Set to use. |
| 6225 | * @param[in] all_desc Whether to go to all descendants ('//') or not ('/'). |
| 6226 | * @param[in] options XPath options. |
| 6227 | * @return LY_ERR |
| 6228 | */ |
| 6229 | static LY_ERR |
| 6230 | moveto_scnode_self(struct lyxp_set *set, int all_desc, int options) |
| 6231 | { |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6232 | int getnext_opts; |
| 6233 | uint32_t i, mod_idx; |
| 6234 | const struct lysc_node *iter, *start_parent; |
| 6235 | const struct lys_module *mod; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6236 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6237 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6238 | return LY_SUCCESS; |
| 6239 | } |
| 6240 | |
| 6241 | if (set->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 6242 | 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] | 6243 | return LY_EVALID; |
| 6244 | } |
| 6245 | |
| 6246 | /* nothing to do */ |
| 6247 | if (!all_desc) { |
| 6248 | return LY_SUCCESS; |
| 6249 | } |
| 6250 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6251 | /* getnext opts */ |
| 6252 | getnext_opts = LYS_GETNEXT_NOSTATECHECK; |
| 6253 | if (options & LYXP_SCNODE_OUTPUT) { |
| 6254 | getnext_opts |= LYS_GETNEXT_OUTPUT; |
| 6255 | } |
| 6256 | |
| 6257 | /* 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] | 6258 | for (i = 0; i < set->used; ++i) { |
| 6259 | if (set->val.scnodes[i].in_ctx != 1) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6260 | if (set->val.scnodes[i].in_ctx != -2) { |
| 6261 | continue; |
| 6262 | } |
| 6263 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6264 | /* remember context node */ |
| 6265 | set->val.scnodes[i].in_ctx = -1; |
| 6266 | } else { |
| 6267 | set->val.scnodes[i].in_ctx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6268 | } |
| 6269 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6270 | start_parent = set->val.scnodes[i].scnode; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6271 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6272 | if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) { |
| 6273 | /* it can actually be in any module, it's all <running> */ |
| 6274 | mod_idx = 0; |
| 6275 | while ((mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) { |
| 6276 | iter = NULL; |
| 6277 | /* module may not be implemented */ |
| 6278 | while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) { |
| 6279 | /* context check */ |
| 6280 | if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) { |
| 6281 | continue; |
| 6282 | } |
| 6283 | |
| 6284 | lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM); |
| 6285 | /* throw away the insert index, we want to consider that node again, recursively */ |
| 6286 | } |
| 6287 | } |
| 6288 | |
| 6289 | } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) { |
| 6290 | iter = NULL; |
| 6291 | while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6292 | /* context check */ |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6293 | 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] | 6294 | continue; |
| 6295 | } |
| 6296 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6297 | lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6298 | } |
| 6299 | } |
| 6300 | } |
| 6301 | |
| 6302 | return LY_SUCCESS; |
| 6303 | } |
| 6304 | |
| 6305 | /** |
| 6306 | * @brief Move context @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_NODE_SET |
| 6307 | * (or LYXP_SET_EMPTY). Context position aware. |
| 6308 | * |
| 6309 | * @param[in] set Set to use. |
| 6310 | * @param[in] all_desc Whether to go to all descendants ('//') or not ('/'). |
| 6311 | * @param[in] options XPath options. |
| 6312 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6313 | */ |
| 6314 | static LY_ERR |
| 6315 | moveto_parent(struct lyxp_set *set, int all_desc, int options) |
| 6316 | { |
| 6317 | LY_ERR rc; |
| 6318 | uint32_t i; |
| 6319 | struct lyd_node *node, *new_node; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6320 | enum lyxp_node_type new_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6321 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6322 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6323 | return LY_SUCCESS; |
| 6324 | } |
| 6325 | |
| 6326 | if (set->type != LYXP_SET_NODE_SET) { |
| 6327 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 6328 | return LY_EVALID; |
| 6329 | } |
| 6330 | |
| 6331 | if (all_desc) { |
| 6332 | /* <path>//.. == <path>//./.. */ |
| 6333 | rc = moveto_self(set, 1, options); |
| 6334 | LY_CHECK_RET(rc); |
| 6335 | } |
| 6336 | |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 6337 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6338 | node = set->val.nodes[i].node; |
| 6339 | |
| 6340 | if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
| 6341 | new_node = (struct lyd_node *)node->parent; |
| 6342 | } else if (set->val.nodes[i].type == LYXP_NODE_TEXT) { |
| 6343 | new_node = node; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6344 | } else if (set->val.nodes[i].type == LYXP_NODE_META) { |
| 6345 | new_node = set->val.meta[i].meta->parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6346 | if (!new_node) { |
| 6347 | LOGINT_RET(set->ctx); |
| 6348 | } |
| 6349 | } else { |
| 6350 | /* root does not have a parent */ |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6351 | set_remove_node_none(set, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6352 | continue; |
| 6353 | } |
| 6354 | |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 6355 | /* when check */ |
| 6356 | if (moveto_when_check(new_node)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6357 | return LY_EINCOMPLETE; |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 6358 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6359 | |
| 6360 | /* node already there can also be the root */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6361 | if (!new_node) { |
| 6362 | new_type = set->root_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6363 | |
| 6364 | /* node has a standard parent (it can equal the root, it's not the root yet since they are fake) */ |
| 6365 | } else { |
| 6366 | new_type = LYXP_NODE_ELEM; |
| 6367 | } |
| 6368 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6369 | if (set_dup_node_check(set, new_node, new_type, -1)) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6370 | set_remove_node_none(set, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6371 | } else { |
| 6372 | set_replace_node(set, new_node, 0, new_type, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6373 | } |
| 6374 | } |
| 6375 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6376 | set_remove_nodes_none(set); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6377 | assert(!set_sort(set) && !set_sorted_dup_node_clean(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6378 | |
| 6379 | return LY_SUCCESS; |
| 6380 | } |
| 6381 | |
| 6382 | /** |
| 6383 | * @brief Move context schema @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_SCNODE_SET |
| 6384 | * (or LYXP_SET_EMPTY). |
| 6385 | * |
| 6386 | * @param[in] set Set to use. |
| 6387 | * @param[in] all_desc Whether to go to all descendants ('//') or not ('/'). |
| 6388 | * @param[in] options XPath options. |
| 6389 | * @return LY_ERR |
| 6390 | */ |
| 6391 | static LY_ERR |
| 6392 | moveto_scnode_parent(struct lyxp_set *set, int all_desc, int options) |
| 6393 | { |
| 6394 | int idx, i, orig_used, temp_ctx = 0; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6395 | const struct lysc_node *node, *new_node; |
| 6396 | enum lyxp_node_type new_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6397 | LY_ERR rc; |
| 6398 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6399 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6400 | return LY_SUCCESS; |
| 6401 | } |
| 6402 | |
| 6403 | if (set->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 6404 | 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] | 6405 | return LY_EVALID; |
| 6406 | } |
| 6407 | |
| 6408 | if (all_desc) { |
| 6409 | /* <path>//.. == <path>//./.. */ |
| 6410 | rc = moveto_scnode_self(set, 1, options); |
| 6411 | LY_CHECK_RET(rc); |
| 6412 | } |
| 6413 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6414 | orig_used = set->used; |
| 6415 | for (i = 0; i < orig_used; ++i) { |
| 6416 | if (set->val.scnodes[i].in_ctx != 1) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6417 | if (set->val.scnodes[i].in_ctx != -2) { |
| 6418 | continue; |
| 6419 | } |
| 6420 | |
| 6421 | /* remember context node */ |
| 6422 | set->val.scnodes[i].in_ctx = -1; |
Michal Vasko | ec4df48 | 2019-12-16 10:02:18 +0100 | [diff] [blame] | 6423 | } else { |
| 6424 | set->val.scnodes[i].in_ctx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6425 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6426 | |
| 6427 | node = set->val.scnodes[i].scnode; |
| 6428 | |
| 6429 | if (set->val.scnodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6430 | new_node = lysc_data_parent(node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6431 | } else { |
| 6432 | /* root does not have a parent */ |
| 6433 | continue; |
| 6434 | } |
| 6435 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6436 | /* node has no parent */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6437 | if (!new_node) { |
| 6438 | new_type = set->root_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6439 | |
| 6440 | /* node has a standard parent (it can equal the root, it's not the root yet since they are fake) */ |
| 6441 | } else { |
| 6442 | new_type = LYXP_NODE_ELEM; |
| 6443 | } |
| 6444 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6445 | idx = lyxp_set_scnode_insert_node(set, new_node, new_type); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6446 | if ((idx < orig_used) && (idx > i)) { |
| 6447 | set->val.scnodes[idx].in_ctx = 2; |
| 6448 | temp_ctx = 1; |
| 6449 | } |
| 6450 | } |
| 6451 | |
| 6452 | if (temp_ctx) { |
| 6453 | for (i = 0; i < orig_used; ++i) { |
| 6454 | if (set->val.scnodes[i].in_ctx == 2) { |
| 6455 | set->val.scnodes[i].in_ctx = 1; |
| 6456 | } |
| 6457 | } |
| 6458 | } |
| 6459 | |
| 6460 | return LY_SUCCESS; |
| 6461 | } |
| 6462 | |
| 6463 | /** |
| 6464 | * @brief Move context @p set to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'. |
| 6465 | * Result is LYXP_SET_BOOLEAN. Indirectly context position aware. |
| 6466 | * |
| 6467 | * @param[in,out] set1 Set to use for the result. |
| 6468 | * @param[in] set2 Set acting as the second operand for @p op. |
| 6469 | * @param[in] op Comparison operator to process. |
| 6470 | * @param[in] options XPath options. |
| 6471 | * @return LY_ERR |
| 6472 | */ |
| 6473 | static LY_ERR |
| 6474 | moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op, int options) |
| 6475 | { |
| 6476 | /* |
| 6477 | * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING |
| 6478 | * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING) |
| 6479 | * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 6480 | * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 6481 | * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING |
| 6482 | * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6483 | * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 6484 | * |
| 6485 | * '=' or '!=' |
| 6486 | * BOOLEAN + BOOLEAN |
| 6487 | * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 6488 | * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 6489 | * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 6490 | * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 6491 | * NUMBER + NUMBER |
| 6492 | * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6493 | * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 6494 | * STRING + STRING |
| 6495 | * |
| 6496 | * '<=', '<', '>=', '>' |
| 6497 | * NUMBER + NUMBER |
| 6498 | * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 6499 | * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 6500 | * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 6501 | * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6502 | * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 6503 | * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 6504 | * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6505 | * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6506 | */ |
| 6507 | struct lyxp_set iter1, iter2; |
| 6508 | int result; |
| 6509 | int64_t i; |
| 6510 | LY_ERR rc; |
| 6511 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6512 | memset(&iter1, 0, sizeof iter1); |
| 6513 | memset(&iter2, 0, sizeof iter2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6514 | |
| 6515 | /* iterative evaluation with node-sets */ |
| 6516 | if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) { |
| 6517 | if (set1->type == LYXP_SET_NODE_SET) { |
| 6518 | for (i = 0; i < set1->used; ++i) { |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6519 | /* cast set1 */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6520 | switch (set2->type) { |
| 6521 | case LYXP_SET_NUMBER: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6522 | rc = set_comp_cast(&iter1, set1, LYXP_SET_NUMBER, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6523 | break; |
| 6524 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6525 | rc = set_comp_cast(&iter1, set1, LYXP_SET_BOOLEAN, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6526 | break; |
| 6527 | default: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6528 | rc = set_comp_cast(&iter1, set1, LYXP_SET_STRING, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6529 | break; |
| 6530 | } |
| 6531 | LY_CHECK_RET(rc); |
| 6532 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6533 | /* canonize set2 */ |
| 6534 | LY_CHECK_ERR_RET(rc = set_comp_canonize(&iter2, set2, &set1->val.nodes[i]), lyxp_set_free_content(&iter1), rc); |
| 6535 | |
| 6536 | /* compare recursively */ |
| 6537 | rc = moveto_op_comp(&iter1, &iter2, op, options); |
| 6538 | lyxp_set_free_content(&iter2); |
| 6539 | LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6540 | |
| 6541 | /* lazy evaluation until true */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6542 | if (iter1.val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6543 | set_fill_boolean(set1, 1); |
| 6544 | return LY_SUCCESS; |
| 6545 | } |
| 6546 | } |
| 6547 | } else { |
| 6548 | for (i = 0; i < set2->used; ++i) { |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6549 | /* set set2 */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6550 | switch (set1->type) { |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6551 | case LYXP_SET_NUMBER: |
| 6552 | rc = set_comp_cast(&iter2, set2, LYXP_SET_NUMBER, i); |
| 6553 | break; |
| 6554 | case LYXP_SET_BOOLEAN: |
| 6555 | rc = set_comp_cast(&iter2, set2, LYXP_SET_BOOLEAN, i); |
| 6556 | break; |
| 6557 | default: |
| 6558 | rc = set_comp_cast(&iter2, set2, LYXP_SET_STRING, i); |
| 6559 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6560 | } |
| 6561 | LY_CHECK_RET(rc); |
| 6562 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6563 | /* canonize set1 */ |
| 6564 | 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] | 6565 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6566 | /* compare recursively */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6567 | rc = moveto_op_comp(&iter1, &iter2, op, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6568 | lyxp_set_free_content(&iter2); |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6569 | LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6570 | |
| 6571 | /* lazy evaluation until true */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6572 | if (iter1.val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6573 | set_fill_boolean(set1, 1); |
| 6574 | return LY_SUCCESS; |
| 6575 | } |
| 6576 | } |
| 6577 | } |
| 6578 | |
| 6579 | /* false for all nodes */ |
| 6580 | set_fill_boolean(set1, 0); |
| 6581 | return LY_SUCCESS; |
| 6582 | } |
| 6583 | |
| 6584 | /* first convert properly */ |
| 6585 | if ((op[0] == '=') || (op[0] == '!')) { |
| 6586 | if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6587 | lyxp_set_cast(set1, LYXP_SET_BOOLEAN); |
| 6588 | lyxp_set_cast(set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6589 | } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6590 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6591 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6592 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6593 | LY_CHECK_RET(rc); |
| 6594 | } /* else we have 2 strings */ |
| 6595 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6596 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6597 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6598 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6599 | LY_CHECK_RET(rc); |
| 6600 | } |
| 6601 | |
| 6602 | assert(set1->type == set2->type); |
| 6603 | |
| 6604 | /* compute result */ |
| 6605 | if (op[0] == '=') { |
| 6606 | if (set1->type == LYXP_SET_BOOLEAN) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6607 | result = (set1->val.bln == set2->val.bln); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6608 | } else if (set1->type == LYXP_SET_NUMBER) { |
| 6609 | result = (set1->val.num == set2->val.num); |
| 6610 | } else { |
| 6611 | assert(set1->type == LYXP_SET_STRING); |
Michal Vasko | ac6c72f | 2019-11-14 16:09:34 +0100 | [diff] [blame] | 6612 | result = !strcmp(set1->val.str, set2->val.str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6613 | } |
| 6614 | } else if (op[0] == '!') { |
| 6615 | if (set1->type == LYXP_SET_BOOLEAN) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6616 | result = (set1->val.bln != set2->val.bln); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6617 | } else if (set1->type == LYXP_SET_NUMBER) { |
| 6618 | result = (set1->val.num != set2->val.num); |
| 6619 | } else { |
| 6620 | assert(set1->type == LYXP_SET_STRING); |
Michal Vasko | ac6c72f | 2019-11-14 16:09:34 +0100 | [diff] [blame] | 6621 | result = !strcmp(set1->val.str, set2->val.str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6622 | } |
| 6623 | } else { |
| 6624 | assert(set1->type == LYXP_SET_NUMBER); |
| 6625 | if (op[0] == '<') { |
| 6626 | if (op[1] == '=') { |
| 6627 | result = (set1->val.num <= set2->val.num); |
| 6628 | } else { |
| 6629 | result = (set1->val.num < set2->val.num); |
| 6630 | } |
| 6631 | } else { |
| 6632 | if (op[1] == '=') { |
| 6633 | result = (set1->val.num >= set2->val.num); |
| 6634 | } else { |
| 6635 | result = (set1->val.num > set2->val.num); |
| 6636 | } |
| 6637 | } |
| 6638 | } |
| 6639 | |
| 6640 | /* assign result */ |
| 6641 | if (result) { |
| 6642 | set_fill_boolean(set1, 1); |
| 6643 | } else { |
| 6644 | set_fill_boolean(set1, 0); |
| 6645 | } |
| 6646 | |
| 6647 | return LY_SUCCESS; |
| 6648 | } |
| 6649 | |
| 6650 | /** |
| 6651 | * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div', |
| 6652 | * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware. |
| 6653 | * |
| 6654 | * @param[in,out] set1 Set to use for the result. |
| 6655 | * @param[in] set2 Set acting as the second operand for @p op. |
| 6656 | * @param[in] op Operator to process. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6657 | * @return LY_ERR |
| 6658 | */ |
| 6659 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6660 | 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] | 6661 | { |
| 6662 | LY_ERR rc; |
| 6663 | |
| 6664 | /* unary '-' */ |
| 6665 | if (!set2 && (op[0] == '-')) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6666 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6667 | LY_CHECK_RET(rc); |
| 6668 | set1->val.num *= -1; |
| 6669 | lyxp_set_free(set2); |
| 6670 | return LY_SUCCESS; |
| 6671 | } |
| 6672 | |
| 6673 | assert(set1 && set2); |
| 6674 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6675 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6676 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6677 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6678 | LY_CHECK_RET(rc); |
| 6679 | |
| 6680 | switch (op[0]) { |
| 6681 | /* '+' */ |
| 6682 | case '+': |
| 6683 | set1->val.num += set2->val.num; |
| 6684 | break; |
| 6685 | |
| 6686 | /* '-' */ |
| 6687 | case '-': |
| 6688 | set1->val.num -= set2->val.num; |
| 6689 | break; |
| 6690 | |
| 6691 | /* '*' */ |
| 6692 | case '*': |
| 6693 | set1->val.num *= set2->val.num; |
| 6694 | break; |
| 6695 | |
| 6696 | /* 'div' */ |
| 6697 | case 'd': |
| 6698 | set1->val.num /= set2->val.num; |
| 6699 | break; |
| 6700 | |
| 6701 | /* 'mod' */ |
| 6702 | case 'm': |
| 6703 | set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num); |
| 6704 | break; |
| 6705 | |
| 6706 | default: |
| 6707 | LOGINT_RET(set1->ctx); |
| 6708 | } |
| 6709 | |
| 6710 | return LY_SUCCESS; |
| 6711 | } |
| 6712 | |
| 6713 | /* |
| 6714 | * eval functions |
| 6715 | * |
| 6716 | * They execute a parsed XPath expression on some data subtree. |
| 6717 | */ |
| 6718 | |
| 6719 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6720 | * @brief Evaluate Predicate. Logs directly on error. |
| 6721 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6722 | * [9] Predicate ::= '[' Expr ']' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6723 | * |
| 6724 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6725 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6726 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 6727 | * @param[in] options XPath options. |
| 6728 | * @param[in] parent_pos_pred Whether parent predicate was a positional one. |
| 6729 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6730 | */ |
| 6731 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6732 | 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] | 6733 | { |
| 6734 | LY_ERR rc; |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 6735 | uint16_t i, orig_exp; |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6736 | uint32_t orig_pos, orig_size; |
| 6737 | int32_t pred_in_ctx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6738 | struct lyxp_set set2; |
| 6739 | struct lyd_node *orig_parent; |
| 6740 | |
| 6741 | /* '[' */ |
| 6742 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6743 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 6744 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6745 | |
| 6746 | if (!set) { |
| 6747 | only_parse: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6748 | rc = eval_expr_select(exp, tok_idx, 0, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6749 | LY_CHECK_RET(rc); |
| 6750 | } else if (set->type == LYXP_SET_NODE_SET) { |
| 6751 | /* 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] | 6752 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6753 | |
| 6754 | /* empty set, nothing to evaluate */ |
| 6755 | if (!set->used) { |
| 6756 | goto only_parse; |
| 6757 | } |
| 6758 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6759 | orig_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6760 | orig_pos = 0; |
| 6761 | orig_size = set->used; |
| 6762 | orig_parent = NULL; |
Michal Vasko | 39dbf35 | 2020-05-21 10:08:59 +0200 | [diff] [blame] | 6763 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6764 | set_init(&set2, set); |
| 6765 | set_insert_node(&set2, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, 0); |
| 6766 | /* remember the node context position for position() and context size for last(), |
| 6767 | * predicates should always be evaluated with respect to the child axis (since we do |
| 6768 | * not support explicit axes) so we assign positions based on their parents */ |
| 6769 | if (parent_pos_pred && ((struct lyd_node *)set->val.nodes[i].node->parent != orig_parent)) { |
| 6770 | orig_parent = (struct lyd_node *)set->val.nodes[i].node->parent; |
| 6771 | orig_pos = 1; |
| 6772 | } else { |
| 6773 | ++orig_pos; |
| 6774 | } |
| 6775 | |
| 6776 | set2.ctx_pos = orig_pos; |
| 6777 | set2.ctx_size = orig_size; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6778 | *tok_idx = orig_exp; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6779 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6780 | rc = eval_expr_select(exp, tok_idx, 0, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6781 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6782 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6783 | return rc; |
| 6784 | } |
| 6785 | |
| 6786 | /* number is a position */ |
| 6787 | if (set2.type == LYXP_SET_NUMBER) { |
| 6788 | if ((long long)set2.val.num == orig_pos) { |
| 6789 | set2.val.num = 1; |
| 6790 | } else { |
| 6791 | set2.val.num = 0; |
| 6792 | } |
| 6793 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6794 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6795 | |
| 6796 | /* predicate satisfied or not? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6797 | if (!set2.val.bln) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6798 | set_remove_node_none(set, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6799 | } |
| 6800 | } |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6801 | set_remove_nodes_none(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6802 | |
| 6803 | } else if (set->type == LYXP_SET_SCNODE_SET) { |
| 6804 | for (i = 0; i < set->used; ++i) { |
| 6805 | if (set->val.scnodes[i].in_ctx == 1) { |
| 6806 | /* there is a currently-valid node */ |
| 6807 | break; |
| 6808 | } |
| 6809 | } |
| 6810 | /* empty set, nothing to evaluate */ |
| 6811 | if (i == set->used) { |
| 6812 | goto only_parse; |
| 6813 | } |
| 6814 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6815 | orig_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6816 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6817 | /* set special in_ctx to all the valid snodes */ |
| 6818 | pred_in_ctx = set_scnode_new_in_ctx(set); |
| 6819 | |
| 6820 | /* use the valid snodes one-by-one */ |
| 6821 | for (i = 0; i < set->used; ++i) { |
| 6822 | if (set->val.scnodes[i].in_ctx != pred_in_ctx) { |
| 6823 | continue; |
| 6824 | } |
| 6825 | set->val.scnodes[i].in_ctx = 1; |
| 6826 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6827 | *tok_idx = orig_exp; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6828 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6829 | rc = eval_expr_select(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6830 | LY_CHECK_RET(rc); |
| 6831 | |
| 6832 | set->val.scnodes[i].in_ctx = pred_in_ctx; |
| 6833 | } |
| 6834 | |
| 6835 | /* restore the state as it was before the predicate */ |
| 6836 | for (i = 0; i < set->used; ++i) { |
| 6837 | if (set->val.scnodes[i].in_ctx == 1) { |
| 6838 | set->val.scnodes[i].in_ctx = 0; |
| 6839 | } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) { |
| 6840 | set->val.scnodes[i].in_ctx = 1; |
| 6841 | } |
| 6842 | } |
| 6843 | |
| 6844 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6845 | set2.type = LYXP_SET_NODE_SET; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6846 | set_fill_set(&set2, set); |
| 6847 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6848 | rc = eval_expr_select(exp, tok_idx, 0, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6849 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6850 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6851 | return rc; |
| 6852 | } |
| 6853 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6854 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6855 | if (!set2.val.bln) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6856 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6857 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6858 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6859 | } |
| 6860 | |
| 6861 | /* ']' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6862 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6863 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6864 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 6865 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6866 | |
| 6867 | return LY_SUCCESS; |
| 6868 | } |
| 6869 | |
| 6870 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6871 | * @brief Evaluate Literal. Logs directly on error. |
| 6872 | * |
| 6873 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6874 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6875 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 6876 | */ |
| 6877 | static void |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6878 | 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] | 6879 | { |
| 6880 | if (set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6881 | if (exp->tok_len[*tok_idx] == 2) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6882 | set_fill_string(set, "", 0); |
| 6883 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6884 | 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] | 6885 | } |
| 6886 | } |
| 6887 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6888 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 6889 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6890 | } |
| 6891 | |
| 6892 | /** |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6893 | * @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] | 6894 | * |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6895 | * @param[in] exp Full parsed XPath expression. |
| 6896 | * @param[in,out] tok_idx Index in @p exp at the beginning of the predicate, is updated on success. |
| 6897 | * @param[in] scnode Found schema node as context for the predicate. |
| 6898 | * @param[in] format Format of any prefixes in key names/values. |
| 6899 | * @param[out] predicates Parsed predicates. |
| 6900 | * @param[out] pred_type Type of @p predicates. |
| 6901 | * @return LY_SUCCESS on success, |
| 6902 | * @return LY_ERR on any error. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6903 | */ |
| 6904 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6905 | eval_name_test_try_compile_predicates(struct lyxp_expr *exp, uint16_t *tok_idx, const struct lysc_node *scnode, |
| 6906 | LYD_FORMAT format, struct ly_path_predicate **predicates, enum ly_path_pred_type *pred_type) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6907 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6908 | LY_ERR ret = LY_SUCCESS; |
| 6909 | uint16_t key_count, e_idx, pred_idx = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6910 | const struct lysc_node *key; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6911 | size_t pred_len; |
| 6912 | int prev_lo; |
| 6913 | struct lyxp_expr *exp2 = NULL; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6914 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6915 | assert(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6916 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6917 | if (scnode->nodetype == LYS_LIST) { |
| 6918 | /* get key count */ |
| 6919 | if (scnode->flags & LYS_KEYLESS) { |
| 6920 | return LY_EINVAL; |
| 6921 | } |
| 6922 | for (key_count = 0, key = lysc_node_children(scnode, 0); key && (key->flags & LYS_KEY); key = key->next, ++key_count); |
| 6923 | assert(key_count); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6924 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6925 | /* learn where the predicates end */ |
| 6926 | e_idx = *tok_idx; |
| 6927 | while (key_count) { |
| 6928 | /* '[' */ |
| 6929 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) { |
| 6930 | return LY_EINVAL; |
| 6931 | } |
| 6932 | ++e_idx; |
| 6933 | |
| 6934 | /* ']' */ |
| 6935 | while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) { |
| 6936 | ++e_idx; |
| 6937 | } |
| 6938 | ++e_idx; |
| 6939 | |
| 6940 | /* another presumably key predicate parsed */ |
| 6941 | --key_count; |
| 6942 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6943 | } else { |
| 6944 | /* learn just where this single predicate ends */ |
| 6945 | e_idx = *tok_idx; |
| 6946 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6947 | /* '[' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6948 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) { |
| 6949 | return LY_EINVAL; |
| 6950 | } |
| 6951 | ++e_idx; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6952 | |
| 6953 | /* ']' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6954 | while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) { |
| 6955 | ++e_idx; |
| 6956 | } |
| 6957 | ++e_idx; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6958 | } |
| 6959 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6960 | /* get the joined length of all the keys predicates/of the single leaf-list predicate */ |
| 6961 | pred_len = (exp->tok_pos[e_idx - 1] + exp->tok_len[e_idx - 1]) - exp->tok_pos[*tok_idx]; |
| 6962 | |
| 6963 | /* turn logging off */ |
| 6964 | prev_lo = ly_log_options(0); |
| 6965 | |
| 6966 | /* parse the predicate(s) */ |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 6967 | 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] | 6968 | LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp2), cleanup); |
| 6969 | |
| 6970 | /* compile */ |
| 6971 | switch (format) { |
| 6972 | case LYD_SCHEMA: |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 6973 | ret = ly_path_compile_predicate(scnode->module->ctx, scnode, scnode->module, scnode, exp2, &pred_idx, |
| 6974 | lys_resolve_prefix, scnode->module, LYD_SCHEMA, predicates, pred_type); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6975 | break; |
| 6976 | case LYD_JSON: |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 6977 | ret = ly_path_compile_predicate(scnode->module->ctx, scnode, scnode->module, scnode, exp2, &pred_idx, |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 6978 | lydjson_resolve_prefix, NULL, LYD_JSON, predicates, pred_type); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6979 | break; |
| 6980 | case LYD_XML: |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 6981 | case LYD_LYB: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6982 | ret = LY_EINT; |
| 6983 | goto cleanup; |
| 6984 | } |
| 6985 | LY_CHECK_GOTO(ret, cleanup); |
| 6986 | |
| 6987 | /* success, the predicate must include all the needed information for hash-based search */ |
| 6988 | *tok_idx = e_idx; |
| 6989 | |
| 6990 | cleanup: |
| 6991 | ly_log_options(prev_lo); |
| 6992 | lyxp_expr_free(scnode->module->ctx, exp2); |
| 6993 | return ret; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6994 | } |
| 6995 | |
| 6996 | /** |
| 6997 | * @brief Evaluate NameTest and any following Predicates. Logs directly on error. |
| 6998 | * |
| 6999 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 7000 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 7001 | * [7] NameTest ::= '*' | NCName ':' '*' | QName |
| 7002 | * |
| 7003 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7004 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7005 | * @param[in] attr_axis Whether to search attributes or standard nodes. |
| 7006 | * @param[in] all_desc Whether to search all the descendants or children only. |
| 7007 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7008 | * @param[in] options XPath options. |
| 7009 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7010 | */ |
| 7011 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7012 | 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] | 7013 | int options) |
| 7014 | { |
| 7015 | int i; |
| 7016 | char *path; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7017 | const char *ncname, *ncname_dict = NULL; |
| 7018 | uint16_t ncname_len; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7019 | const struct lys_module *moveto_mod; |
| 7020 | const struct lysc_node *scnode = NULL, *tmp; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7021 | struct ly_path_predicate *predicates = NULL; |
| 7022 | enum ly_path_pred_type pred_type = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7023 | LY_ERR rc = LY_SUCCESS; |
| 7024 | |
| 7025 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7026 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7027 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7028 | |
| 7029 | if (!set) { |
| 7030 | goto moveto; |
| 7031 | } |
| 7032 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7033 | ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]]; |
| 7034 | ncname_len = exp->tok_len[*tok_idx - 1]; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7035 | |
| 7036 | /* parse (and skip) module name */ |
| 7037 | rc = moveto_resolve_model(&ncname, &ncname_len, set, &moveto_mod); |
| 7038 | LY_CHECK_GOTO(rc, cleanup); |
| 7039 | |
| 7040 | if (moveto_mod && !attr_axis && !all_desc && (set->type == LYXP_SET_NODE_SET)) { |
| 7041 | /* find the matching schema node in some parent in the context */ |
| 7042 | for (i = 0; i < (signed)set->used; ++i) { |
| 7043 | if (set->val.nodes[i].type == set->root_type) { |
| 7044 | tmp = lys_find_child(NULL, moveto_mod, ncname, ncname_len, 0, 0); |
| 7045 | } else if ((set->val.nodes[i].type == LYXP_NODE_ELEM) |
| 7046 | && (!scnode || (lysc_data_parent(scnode) != set->val.nodes[i].node->schema))) { |
| 7047 | /* do not repeat the same search */ |
| 7048 | 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] | 7049 | } else { |
| 7050 | tmp = NULL; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7051 | } |
| 7052 | |
| 7053 | /* additional context check */ |
| 7054 | if (tmp && (set->root_type == LYXP_NODE_ROOT_CONFIG) && (tmp->flags & LYS_CONFIG_R)) { |
| 7055 | tmp = NULL; |
| 7056 | } |
| 7057 | |
| 7058 | if (tmp) { |
| 7059 | if (scnode) { |
| 7060 | /* we found a schema node with the same name but at different level, give up, too complicated */ |
| 7061 | scnode = NULL; |
| 7062 | break; |
| 7063 | } else { |
| 7064 | /* remember the found schema node and continue to make sure it can be used */ |
| 7065 | scnode = tmp; |
| 7066 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7067 | } |
| 7068 | } |
| 7069 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7070 | if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) { |
| 7071 | /* try to create the predicates */ |
| 7072 | if (eval_name_test_try_compile_predicates(exp, tok_idx, scnode, set->format, &predicates, &pred_type)) { |
| 7073 | /* hashes cannot be used */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7074 | scnode = NULL; |
| 7075 | } |
| 7076 | } |
| 7077 | } |
| 7078 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7079 | if (!scnode && moveto_mod) { |
| 7080 | /* we are not able to match based on a schema node and not all the modules match, |
| 7081 | * use dictionary for efficient comparison */ |
| 7082 | ncname_dict = lydict_insert(set->ctx, ncname, ncname_len); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7083 | } |
| 7084 | |
| 7085 | moveto: |
| 7086 | /* move to the attribute(s), data node(s), or schema node(s) */ |
| 7087 | if (attr_axis) { |
| 7088 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 7089 | set_scnode_clear_ctx(set); |
| 7090 | } else { |
| 7091 | if (all_desc) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7092 | rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7093 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7094 | rc = moveto_attr(set, moveto_mod, ncname_dict); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7095 | } |
| 7096 | LY_CHECK_GOTO(rc, cleanup); |
| 7097 | } |
| 7098 | } else { |
| 7099 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 7100 | if (all_desc) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7101 | rc = moveto_scnode_alldesc(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7102 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7103 | rc = moveto_scnode(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7104 | } |
| 7105 | LY_CHECK_GOTO(rc, cleanup); |
| 7106 | |
| 7107 | for (i = set->used - 1; i > -1; --i) { |
| 7108 | if (set->val.scnodes[i].in_ctx > 0) { |
| 7109 | break; |
| 7110 | } |
| 7111 | } |
| 7112 | if (i == -1) { |
| 7113 | path = lysc_path(set->ctx_scnode, LYSC_PATH_LOG, NULL, 0); |
| 7114 | 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] | 7115 | ncname_len, ncname, ncname - exp->expr, exp->expr, path); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7116 | free(path); |
| 7117 | } |
| 7118 | } else { |
| 7119 | if (all_desc) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7120 | rc = moveto_node_alldesc(set, moveto_mod, ncname_dict); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7121 | } else { |
| 7122 | if (scnode) { |
| 7123 | /* we can find the nodes using hashes */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7124 | rc = moveto_node_hash(set, scnode, predicates); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7125 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7126 | rc = moveto_node(set, moveto_mod, ncname_dict); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7127 | } |
| 7128 | } |
| 7129 | LY_CHECK_GOTO(rc, cleanup); |
| 7130 | } |
| 7131 | } |
| 7132 | |
| 7133 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7134 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 7135 | rc = eval_predicate(exp, tok_idx, set, options, 1); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7136 | LY_CHECK_RET(rc); |
| 7137 | } |
| 7138 | |
| 7139 | cleanup: |
Michal Vasko | db51a8d | 2020-05-27 15:22:29 +0200 | [diff] [blame] | 7140 | if (set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7141 | lydict_remove(set->ctx, ncname_dict); |
| 7142 | ly_path_predicates_free(set->ctx, pred_type, scnode, predicates); |
Michal Vasko | db51a8d | 2020-05-27 15:22:29 +0200 | [diff] [blame] | 7143 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7144 | return rc; |
| 7145 | } |
| 7146 | |
| 7147 | /** |
| 7148 | * @brief Evaluate NodeType and any following Predicates. Logs directly on error. |
| 7149 | * |
| 7150 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 7151 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 7152 | * [8] NodeType ::= 'text' | 'node' |
| 7153 | * |
| 7154 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7155 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7156 | * @param[in] attr_axis Whether to search attributes or standard nodes. |
| 7157 | * @param[in] all_desc Whether to search all the descendants or children only. |
| 7158 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7159 | * @param[in] options XPath options. |
| 7160 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7161 | */ |
| 7162 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7163 | 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] | 7164 | struct lyxp_set *set, int options) |
| 7165 | { |
| 7166 | LY_ERR rc; |
| 7167 | |
| 7168 | /* TODO */ |
| 7169 | (void)attr_axis; |
| 7170 | (void)all_desc; |
| 7171 | |
| 7172 | if (set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7173 | assert(exp->tok_len[*tok_idx] == 4); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7174 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 7175 | set_scnode_clear_ctx(set); |
| 7176 | /* just for the debug message below */ |
| 7177 | set = NULL; |
| 7178 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7179 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7180 | rc = xpath_node(NULL, 0, set, options); |
| 7181 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7182 | assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4)); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7183 | rc = xpath_text(NULL, 0, set, options); |
| 7184 | } |
| 7185 | LY_CHECK_RET(rc); |
| 7186 | } |
| 7187 | } |
| 7188 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7189 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7190 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7191 | |
| 7192 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7193 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7194 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7195 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7196 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7197 | |
| 7198 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7199 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7200 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7201 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7202 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7203 | |
| 7204 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7205 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 7206 | rc = eval_predicate(exp, tok_idx, set, options, 1); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7207 | LY_CHECK_RET(rc); |
| 7208 | } |
| 7209 | |
| 7210 | return LY_SUCCESS; |
| 7211 | } |
| 7212 | |
| 7213 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7214 | * @brief Evaluate RelativeLocationPath. Logs directly on error. |
| 7215 | * |
| 7216 | * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step |
| 7217 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7218 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7219 | * |
| 7220 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7221 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7222 | * @param[in] all_desc Whether to search all the descendants or children only. |
| 7223 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7224 | * @param[in] options XPath options. |
| 7225 | * @return LY_ERR (YL_EINCOMPLETE on unresolved when) |
| 7226 | */ |
| 7227 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7228 | 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] | 7229 | { |
| 7230 | int attr_axis; |
| 7231 | LY_ERR rc; |
| 7232 | |
| 7233 | goto step; |
| 7234 | do { |
| 7235 | /* evaluate '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7236 | if (exp->tok_len[*tok_idx] == 1) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7237 | all_desc = 0; |
| 7238 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7239 | assert(exp->tok_len[*tok_idx] == 2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7240 | all_desc = 1; |
| 7241 | } |
| 7242 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7243 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7244 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7245 | |
| 7246 | step: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7247 | /* evaluate abbreviated axis '@'? if any */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7248 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7249 | attr_axis = 1; |
| 7250 | |
| 7251 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7252 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7253 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7254 | } else { |
| 7255 | attr_axis = 0; |
| 7256 | } |
| 7257 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7258 | /* Step */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7259 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7260 | case LYXP_TOKEN_DOT: |
| 7261 | /* evaluate '.' */ |
| 7262 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 7263 | rc = moveto_scnode_self(set, all_desc, options); |
| 7264 | } else { |
| 7265 | rc = moveto_self(set, all_desc, options); |
| 7266 | } |
| 7267 | LY_CHECK_RET(rc); |
| 7268 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7269 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7270 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7271 | break; |
| 7272 | |
| 7273 | case LYXP_TOKEN_DDOT: |
| 7274 | /* evaluate '..' */ |
| 7275 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 7276 | rc = moveto_scnode_parent(set, all_desc, options); |
| 7277 | } else { |
| 7278 | rc = moveto_parent(set, all_desc, options); |
| 7279 | } |
| 7280 | LY_CHECK_RET(rc); |
| 7281 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7282 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7283 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7284 | break; |
| 7285 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7286 | case LYXP_TOKEN_NAMETEST: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7287 | /* evaluate NameTest Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7288 | 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] | 7289 | LY_CHECK_RET(rc); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7290 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7291 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7292 | case LYXP_TOKEN_NODETYPE: |
| 7293 | /* evaluate NodeType Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7294 | 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] | 7295 | LY_CHECK_RET(rc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7296 | break; |
| 7297 | |
| 7298 | default: |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 7299 | LOGINT_RET(set ? set->ctx : NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7300 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7301 | } 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] | 7302 | |
| 7303 | return LY_SUCCESS; |
| 7304 | } |
| 7305 | |
| 7306 | /** |
| 7307 | * @brief Evaluate AbsoluteLocationPath. Logs directly on error. |
| 7308 | * |
| 7309 | * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath |
| 7310 | * |
| 7311 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7312 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7313 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7314 | * @param[in] options XPath options. |
| 7315 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7316 | */ |
| 7317 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7318 | 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] | 7319 | { |
| 7320 | int all_desc; |
| 7321 | LY_ERR rc; |
| 7322 | |
| 7323 | if (set) { |
| 7324 | /* no matter what tokens follow, we need to be at the root */ |
| 7325 | moveto_root(set, options); |
| 7326 | } |
| 7327 | |
| 7328 | /* '/' RelativeLocationPath? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7329 | if (exp->tok_len[*tok_idx] == 1) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7330 | /* evaluate '/' - deferred */ |
| 7331 | all_desc = 0; |
| 7332 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7333 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7334 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7335 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7336 | if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7337 | return LY_SUCCESS; |
| 7338 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7339 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7340 | case LYXP_TOKEN_DOT: |
| 7341 | case LYXP_TOKEN_DDOT: |
| 7342 | case LYXP_TOKEN_AT: |
| 7343 | case LYXP_TOKEN_NAMETEST: |
| 7344 | case LYXP_TOKEN_NODETYPE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7345 | rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7346 | LY_CHECK_RET(rc); |
| 7347 | break; |
| 7348 | default: |
| 7349 | break; |
| 7350 | } |
| 7351 | |
| 7352 | /* '//' RelativeLocationPath */ |
| 7353 | } else { |
| 7354 | /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */ |
| 7355 | all_desc = 1; |
| 7356 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7357 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7358 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7359 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7360 | rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7361 | LY_CHECK_RET(rc); |
| 7362 | } |
| 7363 | |
| 7364 | return LY_SUCCESS; |
| 7365 | } |
| 7366 | |
| 7367 | /** |
| 7368 | * @brief Evaluate FunctionCall. Logs directly on error. |
| 7369 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7370 | * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7371 | * |
| 7372 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7373 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7374 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7375 | * @param[in] options XPath options. |
| 7376 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7377 | */ |
| 7378 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7379 | 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] | 7380 | { |
| 7381 | LY_ERR rc; |
| 7382 | 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] | 7383 | uint16_t arg_count = 0, i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7384 | struct lyxp_set **args = NULL, **args_aux; |
| 7385 | |
| 7386 | if (set) { |
| 7387 | /* FunctionName */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7388 | switch (exp->tok_len[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7389 | case 3: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7390 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7391 | xpath_func = &xpath_not; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7392 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7393 | xpath_func = &xpath_sum; |
| 7394 | } |
| 7395 | break; |
| 7396 | case 4: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7397 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7398 | xpath_func = &xpath_lang; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7399 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7400 | xpath_func = &xpath_last; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7401 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7402 | xpath_func = &xpath_name; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7403 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7404 | xpath_func = &xpath_true; |
| 7405 | } |
| 7406 | break; |
| 7407 | case 5: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7408 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7409 | xpath_func = &xpath_count; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7410 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7411 | xpath_func = &xpath_false; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7412 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7413 | xpath_func = &xpath_floor; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7414 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7415 | xpath_func = &xpath_round; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7416 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7417 | xpath_func = &xpath_deref; |
| 7418 | } |
| 7419 | break; |
| 7420 | case 6: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7421 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7422 | xpath_func = &xpath_concat; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7423 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7424 | xpath_func = &xpath_number; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7425 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7426 | xpath_func = &xpath_string; |
| 7427 | } |
| 7428 | break; |
| 7429 | case 7: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7430 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7431 | xpath_func = &xpath_boolean; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7432 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7433 | xpath_func = &xpath_ceiling; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7434 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7435 | xpath_func = &xpath_current; |
| 7436 | } |
| 7437 | break; |
| 7438 | case 8: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7439 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7440 | xpath_func = &xpath_contains; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7441 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7442 | xpath_func = &xpath_position; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7443 | } 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] | 7444 | xpath_func = &xpath_re_match; |
| 7445 | } |
| 7446 | break; |
| 7447 | case 9: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7448 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7449 | xpath_func = &xpath_substring; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7450 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7451 | xpath_func = &xpath_translate; |
| 7452 | } |
| 7453 | break; |
| 7454 | case 10: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7455 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7456 | xpath_func = &xpath_local_name; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7457 | } 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] | 7458 | xpath_func = &xpath_enum_value; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7459 | } 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] | 7460 | xpath_func = &xpath_bit_is_set; |
| 7461 | } |
| 7462 | break; |
| 7463 | case 11: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7464 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7465 | xpath_func = &xpath_starts_with; |
| 7466 | } |
| 7467 | break; |
| 7468 | case 12: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7469 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7470 | xpath_func = &xpath_derived_from; |
| 7471 | } |
| 7472 | break; |
| 7473 | case 13: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7474 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7475 | xpath_func = &xpath_namespace_uri; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7476 | } 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] | 7477 | xpath_func = &xpath_string_length; |
| 7478 | } |
| 7479 | break; |
| 7480 | case 15: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7481 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7482 | xpath_func = &xpath_normalize_space; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7483 | } 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] | 7484 | xpath_func = &xpath_substring_after; |
| 7485 | } |
| 7486 | break; |
| 7487 | case 16: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7488 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7489 | xpath_func = &xpath_substring_before; |
| 7490 | } |
| 7491 | break; |
| 7492 | case 20: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7493 | 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] | 7494 | xpath_func = &xpath_derived_from_or_self; |
| 7495 | } |
| 7496 | break; |
| 7497 | } |
| 7498 | |
| 7499 | if (!xpath_func) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7500 | 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] | 7501 | return LY_EVALID; |
| 7502 | } |
| 7503 | } |
| 7504 | |
| 7505 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7506 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7507 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7508 | |
| 7509 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7510 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7511 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7512 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7513 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7514 | |
| 7515 | /* ( Expr ( ',' Expr )* )? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7516 | if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7517 | if (set) { |
| 7518 | args = malloc(sizeof *args); |
| 7519 | LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup); |
| 7520 | arg_count = 1; |
| 7521 | args[0] = set_copy(set); |
| 7522 | if (!args[0]) { |
| 7523 | rc = LY_EMEM; |
| 7524 | goto cleanup; |
| 7525 | } |
| 7526 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7527 | rc = eval_expr_select(exp, tok_idx, 0, args[0], options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7528 | LY_CHECK_GOTO(rc, cleanup); |
| 7529 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7530 | rc = eval_expr_select(exp, tok_idx, 0, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7531 | LY_CHECK_GOTO(rc, cleanup); |
| 7532 | } |
| 7533 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7534 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7535 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7536 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7537 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7538 | |
| 7539 | if (set) { |
| 7540 | ++arg_count; |
| 7541 | args_aux = realloc(args, arg_count * sizeof *args); |
| 7542 | LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup); |
| 7543 | args = args_aux; |
| 7544 | args[arg_count - 1] = set_copy(set); |
| 7545 | if (!args[arg_count - 1]) { |
| 7546 | rc = LY_EMEM; |
| 7547 | goto cleanup; |
| 7548 | } |
| 7549 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7550 | 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] | 7551 | LY_CHECK_GOTO(rc, cleanup); |
| 7552 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7553 | rc = eval_expr_select(exp, tok_idx, 0, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7554 | LY_CHECK_GOTO(rc, cleanup); |
| 7555 | } |
| 7556 | } |
| 7557 | |
| 7558 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7559 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7560 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7561 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7562 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7563 | |
| 7564 | if (set) { |
| 7565 | /* evaluate function */ |
| 7566 | rc = xpath_func(args, arg_count, set, options); |
| 7567 | |
| 7568 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7569 | /* merge all nodes from arg evaluations */ |
| 7570 | for (i = 0; i < arg_count; ++i) { |
| 7571 | set_scnode_clear_ctx(args[i]); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7572 | lyxp_set_scnode_merge(set, args[i]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7573 | } |
| 7574 | } |
| 7575 | } else { |
| 7576 | rc = LY_SUCCESS; |
| 7577 | } |
| 7578 | |
| 7579 | cleanup: |
| 7580 | for (i = 0; i < arg_count; ++i) { |
| 7581 | lyxp_set_free(args[i]); |
| 7582 | } |
| 7583 | free(args); |
| 7584 | |
| 7585 | return rc; |
| 7586 | } |
| 7587 | |
| 7588 | /** |
| 7589 | * @brief Evaluate Number. Logs directly on error. |
| 7590 | * |
| 7591 | * @param[in] ctx Context for errors. |
| 7592 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7593 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7594 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7595 | * @return LY_ERR |
| 7596 | */ |
| 7597 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7598 | 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] | 7599 | { |
| 7600 | long double num; |
| 7601 | char *endptr; |
| 7602 | |
| 7603 | if (set) { |
| 7604 | errno = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7605 | num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7606 | if (errno) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7607 | 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] | 7608 | 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] | 7609 | 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] | 7610 | return LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7611 | } else if (endptr - &exp->expr[exp->tok_pos[*tok_idx]] != exp->tok_len[*tok_idx]) { |
| 7612 | 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] | 7613 | 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] | 7614 | exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7615 | return LY_EVALID; |
| 7616 | } |
| 7617 | |
| 7618 | set_fill_number(set, num); |
| 7619 | } |
| 7620 | |
| 7621 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7622 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7623 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7624 | return LY_SUCCESS; |
| 7625 | } |
| 7626 | |
| 7627 | /** |
| 7628 | * @brief Evaluate PathExpr. Logs directly on error. |
| 7629 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7630 | * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate* |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7631 | * | PrimaryExpr Predicate* '/' RelativeLocationPath |
| 7632 | * | PrimaryExpr Predicate* '//' RelativeLocationPath |
| 7633 | * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7634 | * [10] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7635 | * |
| 7636 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7637 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7638 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7639 | * @param[in] options XPath options. |
| 7640 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7641 | */ |
| 7642 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7643 | 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] | 7644 | { |
| 7645 | int all_desc, parent_pos_pred; |
| 7646 | LY_ERR rc; |
| 7647 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7648 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7649 | case LYXP_TOKEN_PAR1: |
| 7650 | /* '(' Expr ')' */ |
| 7651 | |
| 7652 | /* '(' */ |
| 7653 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7654 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7655 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7656 | |
| 7657 | /* Expr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7658 | rc = eval_expr_select(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7659 | LY_CHECK_RET(rc); |
| 7660 | |
| 7661 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7662 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7663 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7664 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7665 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7666 | |
| 7667 | parent_pos_pred = 0; |
| 7668 | goto predicate; |
| 7669 | |
| 7670 | case LYXP_TOKEN_DOT: |
| 7671 | case LYXP_TOKEN_DDOT: |
| 7672 | case LYXP_TOKEN_AT: |
| 7673 | case LYXP_TOKEN_NAMETEST: |
| 7674 | case LYXP_TOKEN_NODETYPE: |
| 7675 | /* RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7676 | rc = eval_relative_location_path(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7677 | LY_CHECK_RET(rc); |
| 7678 | break; |
| 7679 | |
| 7680 | case LYXP_TOKEN_FUNCNAME: |
| 7681 | /* FunctionCall */ |
| 7682 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7683 | rc = eval_function_call(exp, tok_idx, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7684 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7685 | rc = eval_function_call(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7686 | } |
| 7687 | LY_CHECK_RET(rc); |
| 7688 | |
| 7689 | parent_pos_pred = 1; |
| 7690 | goto predicate; |
| 7691 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 7692 | case LYXP_TOKEN_OPER_PATH: |
| 7693 | case LYXP_TOKEN_OPER_RPATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7694 | /* AbsoluteLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7695 | rc = eval_absolute_location_path(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7696 | LY_CHECK_RET(rc); |
| 7697 | break; |
| 7698 | |
| 7699 | case LYXP_TOKEN_LITERAL: |
| 7700 | /* Literal */ |
| 7701 | if (!set || (options & LYXP_SCNODE_ALL)) { |
| 7702 | if (set) { |
| 7703 | set_scnode_clear_ctx(set); |
| 7704 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7705 | eval_literal(exp, tok_idx, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7706 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7707 | eval_literal(exp, tok_idx, set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7708 | } |
| 7709 | |
| 7710 | parent_pos_pred = 1; |
| 7711 | goto predicate; |
| 7712 | |
| 7713 | case LYXP_TOKEN_NUMBER: |
| 7714 | /* Number */ |
| 7715 | if (!set || (options & LYXP_SCNODE_ALL)) { |
| 7716 | if (set) { |
| 7717 | set_scnode_clear_ctx(set); |
| 7718 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7719 | rc = eval_number(NULL, exp, tok_idx, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7720 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7721 | rc = eval_number(set->ctx, exp, tok_idx, set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7722 | } |
| 7723 | LY_CHECK_RET(rc); |
| 7724 | |
| 7725 | parent_pos_pred = 1; |
| 7726 | goto predicate; |
| 7727 | |
| 7728 | default: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7729 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[*tok_idx]), |
| 7730 | &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7731 | return LY_EVALID; |
| 7732 | } |
| 7733 | |
| 7734 | return LY_SUCCESS; |
| 7735 | |
| 7736 | predicate: |
| 7737 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7738 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 7739 | rc = eval_predicate(exp, tok_idx, set, options, parent_pos_pred); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7740 | LY_CHECK_RET(rc); |
| 7741 | } |
| 7742 | |
| 7743 | /* ('/' or '//') RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7744 | 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] | 7745 | |
| 7746 | /* evaluate '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7747 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7748 | all_desc = 0; |
| 7749 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7750 | all_desc = 1; |
| 7751 | } |
| 7752 | |
| 7753 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7754 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7755 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7756 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7757 | rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7758 | LY_CHECK_RET(rc); |
| 7759 | } |
| 7760 | |
| 7761 | return LY_SUCCESS; |
| 7762 | } |
| 7763 | |
| 7764 | /** |
| 7765 | * @brief Evaluate UnionExpr. Logs directly on error. |
| 7766 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7767 | * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7768 | * |
| 7769 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7770 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7771 | * @param[in] repeat How many times this expression is repeated. |
| 7772 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7773 | * @param[in] options XPath options. |
| 7774 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7775 | */ |
| 7776 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7777 | 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] | 7778 | { |
| 7779 | LY_ERR rc = LY_SUCCESS; |
| 7780 | struct lyxp_set orig_set, set2; |
| 7781 | uint16_t i; |
| 7782 | |
| 7783 | assert(repeat); |
| 7784 | |
| 7785 | set_init(&orig_set, set); |
| 7786 | set_init(&set2, set); |
| 7787 | |
| 7788 | set_fill_set(&orig_set, set); |
| 7789 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7790 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7791 | LY_CHECK_GOTO(rc, cleanup); |
| 7792 | |
| 7793 | /* ('|' PathExpr)* */ |
| 7794 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7795 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7796 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7797 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7798 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7799 | |
| 7800 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7801 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7802 | LY_CHECK_GOTO(rc, cleanup); |
| 7803 | continue; |
| 7804 | } |
| 7805 | |
| 7806 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7807 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7808 | LY_CHECK_GOTO(rc, cleanup); |
| 7809 | |
| 7810 | /* eval */ |
| 7811 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7812 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7813 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7814 | rc = moveto_union(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7815 | LY_CHECK_GOTO(rc, cleanup); |
| 7816 | } |
| 7817 | } |
| 7818 | |
| 7819 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7820 | lyxp_set_free_content(&orig_set); |
| 7821 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7822 | return rc; |
| 7823 | } |
| 7824 | |
| 7825 | /** |
| 7826 | * @brief Evaluate UnaryExpr. Logs directly on error. |
| 7827 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7828 | * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7829 | * |
| 7830 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7831 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7832 | * @param[in] repeat How many times this expression is repeated. |
| 7833 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7834 | * @param[in] options XPath options. |
| 7835 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7836 | */ |
| 7837 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7838 | 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] | 7839 | { |
| 7840 | LY_ERR rc; |
| 7841 | uint16_t this_op, i; |
| 7842 | |
| 7843 | assert(repeat); |
| 7844 | |
| 7845 | /* ('-')+ */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7846 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7847 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7848 | 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] | 7849 | |
| 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 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7855 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7856 | LY_CHECK_RET(rc); |
| 7857 | |
| 7858 | if (set && (repeat % 2)) { |
| 7859 | if (options & LYXP_SCNODE_ALL) { |
| 7860 | warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]); |
| 7861 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7862 | 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] | 7863 | LY_CHECK_RET(rc); |
| 7864 | } |
| 7865 | } |
| 7866 | |
| 7867 | return LY_SUCCESS; |
| 7868 | } |
| 7869 | |
| 7870 | /** |
| 7871 | * @brief Evaluate MultiplicativeExpr. Logs directly on error. |
| 7872 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7873 | * [18] MultiplicativeExpr ::= UnaryExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7874 | * | MultiplicativeExpr '*' UnaryExpr |
| 7875 | * | MultiplicativeExpr 'div' UnaryExpr |
| 7876 | * | MultiplicativeExpr 'mod' UnaryExpr |
| 7877 | * |
| 7878 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7879 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7880 | * @param[in] repeat How many times this expression is repeated. |
| 7881 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7882 | * @param[in] options XPath options. |
| 7883 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7884 | */ |
| 7885 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7886 | 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] | 7887 | { |
| 7888 | LY_ERR rc; |
| 7889 | uint16_t this_op; |
| 7890 | struct lyxp_set orig_set, set2; |
| 7891 | uint16_t i; |
| 7892 | |
| 7893 | assert(repeat); |
| 7894 | |
| 7895 | set_init(&orig_set, set); |
| 7896 | set_init(&set2, set); |
| 7897 | |
| 7898 | set_fill_set(&orig_set, set); |
| 7899 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7900 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7901 | LY_CHECK_GOTO(rc, cleanup); |
| 7902 | |
| 7903 | /* ('*' / 'div' / 'mod' UnaryExpr)* */ |
| 7904 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7905 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7906 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7907 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7908 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7909 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7910 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7911 | |
| 7912 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7913 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7914 | LY_CHECK_GOTO(rc, cleanup); |
| 7915 | continue; |
| 7916 | } |
| 7917 | |
| 7918 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7919 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7920 | LY_CHECK_GOTO(rc, cleanup); |
| 7921 | |
| 7922 | /* eval */ |
| 7923 | if (options & LYXP_SCNODE_ALL) { |
| 7924 | 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] | 7925 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7926 | set_scnode_clear_ctx(set); |
| 7927 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7928 | 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] | 7929 | LY_CHECK_GOTO(rc, cleanup); |
| 7930 | } |
| 7931 | } |
| 7932 | |
| 7933 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7934 | lyxp_set_free_content(&orig_set); |
| 7935 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7936 | return rc; |
| 7937 | } |
| 7938 | |
| 7939 | /** |
| 7940 | * @brief Evaluate AdditiveExpr. Logs directly on error. |
| 7941 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7942 | * [17] AdditiveExpr ::= MultiplicativeExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7943 | * | AdditiveExpr '+' MultiplicativeExpr |
| 7944 | * | AdditiveExpr '-' MultiplicativeExpr |
| 7945 | * |
| 7946 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7947 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7948 | * @param[in] repeat How many times this expression is repeated. |
| 7949 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7950 | * @param[in] options XPath options. |
| 7951 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7952 | */ |
| 7953 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7954 | 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] | 7955 | { |
| 7956 | LY_ERR rc; |
| 7957 | uint16_t this_op; |
| 7958 | struct lyxp_set orig_set, set2; |
| 7959 | uint16_t i; |
| 7960 | |
| 7961 | assert(repeat); |
| 7962 | |
| 7963 | set_init(&orig_set, set); |
| 7964 | set_init(&set2, set); |
| 7965 | |
| 7966 | set_fill_set(&orig_set, set); |
| 7967 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7968 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7969 | LY_CHECK_GOTO(rc, cleanup); |
| 7970 | |
| 7971 | /* ('+' / '-' MultiplicativeExpr)* */ |
| 7972 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7973 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7974 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7975 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7976 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7977 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7978 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7979 | |
| 7980 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7981 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7982 | LY_CHECK_GOTO(rc, cleanup); |
| 7983 | continue; |
| 7984 | } |
| 7985 | |
| 7986 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7987 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7988 | LY_CHECK_GOTO(rc, cleanup); |
| 7989 | |
| 7990 | /* eval */ |
| 7991 | if (options & LYXP_SCNODE_ALL) { |
| 7992 | warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7993 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7994 | set_scnode_clear_ctx(set); |
| 7995 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7996 | rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7997 | LY_CHECK_GOTO(rc, cleanup); |
| 7998 | } |
| 7999 | } |
| 8000 | |
| 8001 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8002 | lyxp_set_free_content(&orig_set); |
| 8003 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8004 | return rc; |
| 8005 | } |
| 8006 | |
| 8007 | /** |
| 8008 | * @brief Evaluate RelationalExpr. Logs directly on error. |
| 8009 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8010 | * [16] RelationalExpr ::= AdditiveExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8011 | * | RelationalExpr '<' AdditiveExpr |
| 8012 | * | RelationalExpr '>' AdditiveExpr |
| 8013 | * | RelationalExpr '<=' AdditiveExpr |
| 8014 | * | RelationalExpr '>=' AdditiveExpr |
| 8015 | * |
| 8016 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8017 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8018 | * @param[in] repeat How many times this expression is repeated. |
| 8019 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8020 | * @param[in] options XPath options. |
| 8021 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8022 | */ |
| 8023 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8024 | 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] | 8025 | { |
| 8026 | LY_ERR rc; |
| 8027 | uint16_t this_op; |
| 8028 | struct lyxp_set orig_set, set2; |
| 8029 | uint16_t i; |
| 8030 | |
| 8031 | assert(repeat); |
| 8032 | |
| 8033 | set_init(&orig_set, set); |
| 8034 | set_init(&set2, set); |
| 8035 | |
| 8036 | set_fill_set(&orig_set, set); |
| 8037 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8038 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8039 | LY_CHECK_GOTO(rc, cleanup); |
| 8040 | |
| 8041 | /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */ |
| 8042 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8043 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8044 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8045 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8046 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8047 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 8048 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8049 | |
| 8050 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8051 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8052 | LY_CHECK_GOTO(rc, cleanup); |
| 8053 | continue; |
| 8054 | } |
| 8055 | |
| 8056 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8057 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8058 | LY_CHECK_GOTO(rc, cleanup); |
| 8059 | |
| 8060 | /* eval */ |
| 8061 | if (options & LYXP_SCNODE_ALL) { |
| 8062 | 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] | 8063 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8064 | set_scnode_clear_ctx(set); |
| 8065 | } else { |
| 8066 | rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options); |
| 8067 | LY_CHECK_GOTO(rc, cleanup); |
| 8068 | } |
| 8069 | } |
| 8070 | |
| 8071 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8072 | lyxp_set_free_content(&orig_set); |
| 8073 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8074 | return rc; |
| 8075 | } |
| 8076 | |
| 8077 | /** |
| 8078 | * @brief Evaluate EqualityExpr. Logs directly on error. |
| 8079 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8080 | * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8081 | * | EqualityExpr '!=' RelationalExpr |
| 8082 | * |
| 8083 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8084 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8085 | * @param[in] repeat How many times this expression is repeated. |
| 8086 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8087 | * @param[in] options XPath options. |
| 8088 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8089 | */ |
| 8090 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8091 | 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] | 8092 | { |
| 8093 | LY_ERR rc; |
| 8094 | uint16_t this_op; |
| 8095 | struct lyxp_set orig_set, set2; |
| 8096 | uint16_t i; |
| 8097 | |
| 8098 | assert(repeat); |
| 8099 | |
| 8100 | set_init(&orig_set, set); |
| 8101 | set_init(&set2, set); |
| 8102 | |
| 8103 | set_fill_set(&orig_set, set); |
| 8104 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8105 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8106 | LY_CHECK_GOTO(rc, cleanup); |
| 8107 | |
| 8108 | /* ('=' / '!=' RelationalExpr)* */ |
| 8109 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8110 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8111 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8112 | 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] | 8113 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8114 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 8115 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8116 | |
| 8117 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8118 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8119 | LY_CHECK_GOTO(rc, cleanup); |
| 8120 | continue; |
| 8121 | } |
| 8122 | |
| 8123 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8124 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8125 | LY_CHECK_GOTO(rc, cleanup); |
| 8126 | |
| 8127 | /* eval */ |
| 8128 | if (options & LYXP_SCNODE_ALL) { |
| 8129 | 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] | 8130 | warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1); |
| 8131 | 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] | 8132 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8133 | set_scnode_clear_ctx(set); |
| 8134 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8135 | rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options); |
| 8136 | LY_CHECK_GOTO(rc, cleanup); |
| 8137 | } |
| 8138 | } |
| 8139 | |
| 8140 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8141 | lyxp_set_free_content(&orig_set); |
| 8142 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8143 | return rc; |
| 8144 | } |
| 8145 | |
| 8146 | /** |
| 8147 | * @brief Evaluate AndExpr. Logs directly on error. |
| 8148 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8149 | * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8150 | * |
| 8151 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8152 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8153 | * @param[in] repeat How many times this expression is repeated. |
| 8154 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8155 | * @param[in] options XPath options. |
| 8156 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8157 | */ |
| 8158 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8159 | 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] | 8160 | { |
| 8161 | LY_ERR rc; |
| 8162 | struct lyxp_set orig_set, set2; |
| 8163 | uint16_t i; |
| 8164 | |
| 8165 | assert(repeat); |
| 8166 | |
| 8167 | set_init(&orig_set, set); |
| 8168 | set_init(&set2, set); |
| 8169 | |
| 8170 | set_fill_set(&orig_set, set); |
| 8171 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8172 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8173 | LY_CHECK_GOTO(rc, cleanup); |
| 8174 | |
| 8175 | /* cast to boolean, we know that will be the final result */ |
| 8176 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 8177 | set_scnode_clear_ctx(set); |
| 8178 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8179 | lyxp_set_cast(set, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8180 | } |
| 8181 | |
| 8182 | /* ('and' EqualityExpr)* */ |
| 8183 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8184 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG); |
| 8185 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || !set->val.bln ? "skipped" : "parsed"), |
| 8186 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 8187 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8188 | |
| 8189 | /* lazy evaluation */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8190 | if (!set || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) { |
| 8191 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8192 | LY_CHECK_GOTO(rc, cleanup); |
| 8193 | continue; |
| 8194 | } |
| 8195 | |
| 8196 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8197 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8198 | LY_CHECK_GOTO(rc, cleanup); |
| 8199 | |
| 8200 | /* eval - just get boolean value actually */ |
| 8201 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 8202 | set_scnode_clear_ctx(&set2); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 8203 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8204 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8205 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8206 | set_fill_set(set, &set2); |
| 8207 | } |
| 8208 | } |
| 8209 | |
| 8210 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8211 | lyxp_set_free_content(&orig_set); |
| 8212 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8213 | return rc; |
| 8214 | } |
| 8215 | |
| 8216 | /** |
| 8217 | * @brief Evaluate OrExpr. Logs directly on error. |
| 8218 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8219 | * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8220 | * |
| 8221 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8222 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8223 | * @param[in] repeat How many times this expression is repeated. |
| 8224 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8225 | * @param[in] options XPath options. |
| 8226 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8227 | */ |
| 8228 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8229 | 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] | 8230 | { |
| 8231 | LY_ERR rc; |
| 8232 | struct lyxp_set orig_set, set2; |
| 8233 | uint16_t i; |
| 8234 | |
| 8235 | assert(repeat); |
| 8236 | |
| 8237 | set_init(&orig_set, set); |
| 8238 | set_init(&set2, set); |
| 8239 | |
| 8240 | set_fill_set(&orig_set, set); |
| 8241 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8242 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8243 | LY_CHECK_GOTO(rc, cleanup); |
| 8244 | |
| 8245 | /* cast to boolean, we know that will be the final result */ |
| 8246 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 8247 | set_scnode_clear_ctx(set); |
| 8248 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8249 | lyxp_set_cast(set, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8250 | } |
| 8251 | |
| 8252 | /* ('or' AndExpr)* */ |
| 8253 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8254 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG); |
| 8255 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || set->val.bln ? "skipped" : "parsed"), |
| 8256 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 8257 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8258 | |
| 8259 | /* lazy evaluation */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8260 | if (!set || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) { |
| 8261 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8262 | LY_CHECK_GOTO(rc, cleanup); |
| 8263 | continue; |
| 8264 | } |
| 8265 | |
| 8266 | set_fill_set(&set2, &orig_set); |
| 8267 | /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one), |
| 8268 | * but it does not matter */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8269 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8270 | LY_CHECK_GOTO(rc, cleanup); |
| 8271 | |
| 8272 | /* eval - just get boolean value actually */ |
| 8273 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 8274 | set_scnode_clear_ctx(&set2); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 8275 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8276 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8277 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8278 | set_fill_set(set, &set2); |
| 8279 | } |
| 8280 | } |
| 8281 | |
| 8282 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8283 | lyxp_set_free_content(&orig_set); |
| 8284 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8285 | return rc; |
| 8286 | } |
| 8287 | |
| 8288 | /** |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8289 | * @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] | 8290 | * |
| 8291 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8292 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8293 | * @param[in] etype Expression type to evaluate. |
| 8294 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8295 | * @param[in] options XPath options. |
| 8296 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8297 | */ |
| 8298 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8299 | 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] | 8300 | { |
| 8301 | uint16_t i, count; |
| 8302 | enum lyxp_expr_type next_etype; |
| 8303 | LY_ERR rc; |
| 8304 | |
| 8305 | /* process operator repeats */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8306 | if (!exp->repeat[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8307 | next_etype = LYXP_EXPR_NONE; |
| 8308 | } else { |
| 8309 | /* find etype repeat */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8310 | for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8311 | |
| 8312 | /* select one-priority lower because etype expression called us */ |
| 8313 | if (i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8314 | next_etype = exp->repeat[*tok_idx][i - 1]; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8315 | /* count repeats for that expression */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8316 | 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] | 8317 | } else { |
| 8318 | next_etype = LYXP_EXPR_NONE; |
| 8319 | } |
| 8320 | } |
| 8321 | |
| 8322 | /* decide what expression are we parsing based on the repeat */ |
| 8323 | switch (next_etype) { |
| 8324 | case LYXP_EXPR_OR: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8325 | rc = eval_or_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8326 | break; |
| 8327 | case LYXP_EXPR_AND: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8328 | rc = eval_and_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8329 | break; |
| 8330 | case LYXP_EXPR_EQUALITY: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8331 | rc = eval_equality_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8332 | break; |
| 8333 | case LYXP_EXPR_RELATIONAL: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8334 | rc = eval_relational_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8335 | break; |
| 8336 | case LYXP_EXPR_ADDITIVE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8337 | rc = eval_additive_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8338 | break; |
| 8339 | case LYXP_EXPR_MULTIPLICATIVE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8340 | rc = eval_multiplicative_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8341 | break; |
| 8342 | case LYXP_EXPR_UNARY: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8343 | rc = eval_unary_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8344 | break; |
| 8345 | case LYXP_EXPR_UNION: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8346 | rc = eval_union_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8347 | break; |
| 8348 | case LYXP_EXPR_NONE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8349 | rc = eval_path_expr(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8350 | break; |
| 8351 | default: |
| 8352 | LOGINT_RET(set->ctx); |
| 8353 | } |
| 8354 | |
| 8355 | return rc; |
| 8356 | } |
| 8357 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8358 | /** |
| 8359 | * @brief Get root type. |
| 8360 | * |
| 8361 | * @param[in] ctx_node Context node. |
| 8362 | * @param[in] ctx_scnode Schema context node. |
| 8363 | * @param[in] options XPath options. |
| 8364 | * @return Root type. |
| 8365 | */ |
| 8366 | static enum lyxp_node_type |
| 8367 | lyxp_get_root_type(const struct lyd_node *ctx_node, const struct lysc_node *ctx_scnode, int options) |
| 8368 | { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 8369 | const struct lysc_node *op; |
| 8370 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8371 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 8372 | for (op = ctx_scnode; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent); |
| 8373 | |
| 8374 | if (op || (options & LYXP_SCNODE)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8375 | /* general root that can access everything */ |
| 8376 | return LYXP_NODE_ROOT; |
| 8377 | } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) { |
| 8378 | /* root context node can access only config data (because we said so, it is unspecified) */ |
| 8379 | return LYXP_NODE_ROOT_CONFIG; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8380 | } |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 8381 | return LYXP_NODE_ROOT; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8382 | } |
| 8383 | |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 8384 | op = ctx_node ? ctx_node->schema : NULL; |
| 8385 | for (; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent); |
| 8386 | |
| 8387 | if (!ctx_node || (!op && (ctx_node->schema->flags & LYS_CONFIG_W))) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8388 | /* root context node can access only config data (because we said so, it is unspecified) */ |
| 8389 | return LYXP_NODE_ROOT_CONFIG; |
| 8390 | } |
| 8391 | |
| 8392 | return LYXP_NODE_ROOT; |
| 8393 | } |
| 8394 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8395 | LY_ERR |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 8396 | lyxp_eval(struct lyxp_expr *exp, LYD_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] | 8397 | 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] | 8398 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8399 | uint16_t tok_idx = 0; |
| 8400 | const struct lyd_node *real_ctx_node; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8401 | LY_ERR rc; |
| 8402 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8403 | LY_CHECK_ARG_RET(NULL, exp, local_mod, ctx_node, set, LY_EINVAL); |
| 8404 | |
| 8405 | if ((ctx_node_type == LYXP_NODE_ROOT) || (ctx_node_type == LYXP_NODE_ROOT_CONFIG)) { |
| 8406 | /* we always need some context node because it is used for resolving unqualified names */ |
| 8407 | real_ctx_node = NULL; |
| 8408 | } else { |
| 8409 | real_ctx_node = ctx_node; |
| 8410 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8411 | |
| 8412 | /* prepare set for evaluation */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8413 | tok_idx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8414 | memset(set, 0, sizeof *set); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8415 | set->type = LYXP_SET_NODE_SET; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8416 | 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] | 8417 | set->ctx = local_mod->ctx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8418 | set->ctx_node = ctx_node; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8419 | set->root_type = lyxp_get_root_type(real_ctx_node, NULL, options); |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 8420 | for (set->context_op = ctx_node->schema; |
| 8421 | set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); |
| 8422 | set->context_op = set->context_op->parent); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8423 | set->local_mod = local_mod; |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 8424 | set->tree = tree; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8425 | set->format = format; |
| 8426 | |
| 8427 | /* evaluate */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8428 | rc = eval_expr_select(exp, &tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8429 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8430 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8431 | } |
| 8432 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8433 | return rc; |
| 8434 | } |
| 8435 | |
| 8436 | #if 0 |
| 8437 | |
| 8438 | /* full xml printing of set elements, not used currently */ |
| 8439 | |
| 8440 | void |
| 8441 | lyxp_set_print_xml(FILE *f, struct lyxp_set *set) |
| 8442 | { |
| 8443 | uint32_t i; |
| 8444 | char *str_num; |
| 8445 | struct lyout out; |
| 8446 | |
| 8447 | memset(&out, 0, sizeof out); |
| 8448 | |
| 8449 | out.type = LYOUT_STREAM; |
| 8450 | out.method.f = f; |
| 8451 | |
| 8452 | switch (set->type) { |
| 8453 | case LYXP_SET_EMPTY: |
| 8454 | ly_print(&out, "Empty XPath set\n\n"); |
| 8455 | break; |
| 8456 | case LYXP_SET_BOOLEAN: |
| 8457 | ly_print(&out, "Boolean XPath set:\n"); |
| 8458 | ly_print(&out, "%s\n\n", set->value.bool ? "true" : "false"); |
| 8459 | break; |
| 8460 | case LYXP_SET_STRING: |
| 8461 | ly_print(&out, "String XPath set:\n"); |
| 8462 | ly_print(&out, "\"%s\"\n\n", set->value.str); |
| 8463 | break; |
| 8464 | case LYXP_SET_NUMBER: |
| 8465 | ly_print(&out, "Number XPath set:\n"); |
| 8466 | |
| 8467 | if (isnan(set->value.num)) { |
| 8468 | str_num = strdup("NaN"); |
| 8469 | } else if ((set->value.num == 0) || (set->value.num == -0.0f)) { |
| 8470 | str_num = strdup("0"); |
| 8471 | } else if (isinf(set->value.num) && !signbit(set->value.num)) { |
| 8472 | str_num = strdup("Infinity"); |
| 8473 | } else if (isinf(set->value.num) && signbit(set->value.num)) { |
| 8474 | str_num = strdup("-Infinity"); |
| 8475 | } else if ((long long)set->value.num == set->value.num) { |
| 8476 | if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) { |
| 8477 | str_num = NULL; |
| 8478 | } |
| 8479 | } else { |
| 8480 | if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) { |
| 8481 | str_num = NULL; |
| 8482 | } |
| 8483 | } |
| 8484 | if (!str_num) { |
| 8485 | LOGMEM; |
| 8486 | return; |
| 8487 | } |
| 8488 | ly_print(&out, "%s\n\n", str_num); |
| 8489 | free(str_num); |
| 8490 | break; |
| 8491 | case LYXP_SET_NODE_SET: |
| 8492 | ly_print(&out, "Node XPath set:\n"); |
| 8493 | |
| 8494 | for (i = 0; i < set->used; ++i) { |
| 8495 | ly_print(&out, "%d. ", i + 1); |
| 8496 | switch (set->node_type[i]) { |
| 8497 | case LYXP_NODE_ROOT_ALL: |
| 8498 | ly_print(&out, "ROOT all\n\n"); |
| 8499 | break; |
| 8500 | case LYXP_NODE_ROOT_CONFIG: |
| 8501 | ly_print(&out, "ROOT config\n\n"); |
| 8502 | break; |
| 8503 | case LYXP_NODE_ROOT_STATE: |
| 8504 | ly_print(&out, "ROOT state\n\n"); |
| 8505 | break; |
| 8506 | case LYXP_NODE_ROOT_NOTIF: |
| 8507 | ly_print(&out, "ROOT notification \"%s\"\n\n", set->value.nodes[i]->schema->name); |
| 8508 | break; |
| 8509 | case LYXP_NODE_ROOT_RPC: |
| 8510 | ly_print(&out, "ROOT rpc \"%s\"\n\n", set->value.nodes[i]->schema->name); |
| 8511 | break; |
| 8512 | case LYXP_NODE_ROOT_OUTPUT: |
| 8513 | ly_print(&out, "ROOT output \"%s\"\n\n", set->value.nodes[i]->schema->name); |
| 8514 | break; |
| 8515 | case LYXP_NODE_ELEM: |
| 8516 | ly_print(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name); |
| 8517 | xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT); |
| 8518 | ly_print(&out, "\n"); |
| 8519 | break; |
| 8520 | case LYXP_NODE_TEXT: |
| 8521 | ly_print(&out, "TEXT \"%s\"\n\n", ((struct lyd_node_leaf_list *)set->value.nodes[i])->value_str); |
| 8522 | break; |
| 8523 | case LYXP_NODE_ATTR: |
| 8524 | ly_print(&out, "ATTR \"%s\" = \"%s\"\n\n", set->value.attrs[i]->name, set->value.attrs[i]->value); |
| 8525 | break; |
| 8526 | } |
| 8527 | } |
| 8528 | break; |
| 8529 | } |
| 8530 | } |
| 8531 | |
| 8532 | #endif |
| 8533 | |
| 8534 | LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8535 | lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8536 | { |
| 8537 | long double num; |
| 8538 | char *str; |
| 8539 | LY_ERR rc; |
| 8540 | |
| 8541 | if (!set || (set->type == target)) { |
| 8542 | return LY_SUCCESS; |
| 8543 | } |
| 8544 | |
| 8545 | /* it's not possible to convert anything into a node set */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8546 | assert(target != LYXP_SET_NODE_SET); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8547 | |
| 8548 | if (set->type == LYXP_SET_SCNODE_SET) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8549 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8550 | return LY_EINVAL; |
| 8551 | } |
| 8552 | |
| 8553 | /* to STRING */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8554 | 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] | 8555 | switch (set->type) { |
| 8556 | case LYXP_SET_NUMBER: |
| 8557 | if (isnan(set->val.num)) { |
| 8558 | set->val.str = strdup("NaN"); |
| 8559 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 8560 | } else if ((set->val.num == 0) || (set->val.num == -0.0f)) { |
| 8561 | set->val.str = strdup("0"); |
| 8562 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 8563 | } else if (isinf(set->val.num) && !signbit(set->val.num)) { |
| 8564 | set->val.str = strdup("Infinity"); |
| 8565 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 8566 | } else if (isinf(set->val.num) && signbit(set->val.num)) { |
| 8567 | set->val.str = strdup("-Infinity"); |
| 8568 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 8569 | } else if ((long long)set->val.num == set->val.num) { |
| 8570 | if (asprintf(&str, "%lld", (long long)set->val.num) == -1) { |
| 8571 | LOGMEM_RET(set->ctx); |
| 8572 | } |
| 8573 | set->val.str = str; |
| 8574 | } else { |
| 8575 | if (asprintf(&str, "%03.1Lf", set->val.num) == -1) { |
| 8576 | LOGMEM_RET(set->ctx); |
| 8577 | } |
| 8578 | set->val.str = str; |
| 8579 | } |
| 8580 | break; |
| 8581 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8582 | if (set->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8583 | set->val.str = strdup("true"); |
| 8584 | } else { |
| 8585 | set->val.str = strdup("false"); |
| 8586 | } |
| 8587 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM); |
| 8588 | break; |
| 8589 | case LYXP_SET_NODE_SET: |
| 8590 | assert(set->used); |
| 8591 | |
| 8592 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8593 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8594 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8595 | rc = cast_node_set_to_string(set, &str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8596 | LY_CHECK_RET(rc); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8597 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8598 | set->val.str = str; |
| 8599 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8600 | default: |
| 8601 | LOGINT_RET(set->ctx); |
| 8602 | } |
| 8603 | set->type = LYXP_SET_STRING; |
| 8604 | } |
| 8605 | |
| 8606 | /* to NUMBER */ |
| 8607 | if (target == LYXP_SET_NUMBER) { |
| 8608 | switch (set->type) { |
| 8609 | case LYXP_SET_STRING: |
| 8610 | num = cast_string_to_number(set->val.str); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8611 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8612 | set->val.num = num; |
| 8613 | break; |
| 8614 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8615 | if (set->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8616 | set->val.num = 1; |
| 8617 | } else { |
| 8618 | set->val.num = 0; |
| 8619 | } |
| 8620 | break; |
| 8621 | default: |
| 8622 | LOGINT_RET(set->ctx); |
| 8623 | } |
| 8624 | set->type = LYXP_SET_NUMBER; |
| 8625 | } |
| 8626 | |
| 8627 | /* to BOOLEAN */ |
| 8628 | if (target == LYXP_SET_BOOLEAN) { |
| 8629 | switch (set->type) { |
| 8630 | case LYXP_SET_NUMBER: |
| 8631 | 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] | 8632 | set->val.bln = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8633 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8634 | set->val.bln = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8635 | } |
| 8636 | break; |
| 8637 | case LYXP_SET_STRING: |
| 8638 | if (set->val.str[0]) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8639 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8640 | set->val.bln = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8641 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8642 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8643 | set->val.bln = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8644 | } |
| 8645 | break; |
| 8646 | case LYXP_SET_NODE_SET: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8647 | if (set->used) { |
| 8648 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8649 | set->val.bln = 1; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8650 | } else { |
| 8651 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8652 | set->val.bln = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8653 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8654 | break; |
| 8655 | default: |
| 8656 | LOGINT_RET(set->ctx); |
| 8657 | } |
| 8658 | set->type = LYXP_SET_BOOLEAN; |
| 8659 | } |
| 8660 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8661 | return LY_SUCCESS; |
| 8662 | } |
| 8663 | |
| 8664 | LY_ERR |
| 8665 | lyxp_atomize(struct lyxp_expr *exp, LYD_FORMAT format, const struct lys_module *local_mod, const struct lysc_node *ctx_scnode, |
| 8666 | enum lyxp_node_type ctx_scnode_type, struct lyxp_set *set, int options) |
| 8667 | { |
| 8668 | struct ly_ctx *ctx; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8669 | const struct lysc_node *real_ctx_scnode; |
| 8670 | uint16_t tok_idx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8671 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8672 | 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] | 8673 | |
| 8674 | ctx = local_mod->ctx; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8675 | if ((ctx_scnode_type == LYXP_NODE_ROOT) || (ctx_scnode_type == LYXP_NODE_ROOT_CONFIG)) { |
| 8676 | /* we always need some context node because it is used for resolving unqualified names */ |
| 8677 | real_ctx_scnode = NULL; |
| 8678 | } else { |
| 8679 | real_ctx_scnode = ctx_scnode; |
| 8680 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8681 | |
| 8682 | /* prepare set for evaluation */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8683 | tok_idx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8684 | memset(set, 0, sizeof *set); |
| 8685 | set->type = LYXP_SET_SCNODE_SET; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8686 | lyxp_set_scnode_insert_node(set, real_ctx_scnode, ctx_scnode_type); |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 8687 | set->val.scnodes[0].in_ctx = -2; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8688 | set->ctx = ctx; |
| 8689 | set->ctx_scnode = ctx_scnode; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8690 | set->root_type = lyxp_get_root_type(NULL, real_ctx_scnode, options); |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 8691 | for (set->context_op = ctx_scnode; |
| 8692 | set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); |
| 8693 | set->context_op = set->context_op->parent); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8694 | set->local_mod = local_mod; |
| 8695 | set->format = format; |
| 8696 | |
| 8697 | /* evaluate */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8698 | return eval_expr_select(exp, &tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8699 | } |