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; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 168 | struct lyxp_set_node *item; |
| 169 | struct lyxp_set_scnode *sitem; |
| 170 | |
| 171 | if (ly_log_level < LY_LLDBG) { |
| 172 | return; |
| 173 | } |
| 174 | |
| 175 | switch (set->type) { |
| 176 | case LYXP_SET_NODE_SET: |
| 177 | LOGDBG(LY_LDGXPATH, "set NODE SET:"); |
| 178 | for (i = 0; i < set->used; ++i) { |
| 179 | item = &set->val.nodes[i]; |
| 180 | |
| 181 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 182 | case LYXP_NODE_NONE: |
| 183 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): NONE", i + 1, item->pos); |
| 184 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 185 | case LYXP_NODE_ROOT: |
| 186 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT", i + 1, item->pos); |
| 187 | break; |
| 188 | case LYXP_NODE_ROOT_CONFIG: |
| 189 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT CONFIG", i + 1, item->pos); |
| 190 | break; |
| 191 | case LYXP_NODE_ELEM: |
| 192 | if ((item->node->schema->nodetype == LYS_LIST) |
| 193 | && (((struct lyd_node_inner *)item->node)->child->schema->nodetype == LYS_LEAF)) { |
| 194 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (1st child val: %s)", i + 1, item->pos, |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame^] | 195 | item->node->schema->name, LYD_CANONICAL(lyd_node_children(item->node, 0))); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 196 | } else if (((struct lyd_node_inner *)item->node)->schema->nodetype == LYS_LEAFLIST) { |
| 197 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (val: %s)", i + 1, item->pos, |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame^] | 198 | item->node->schema->name, LYD_CANONICAL(item->node)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 199 | } else { |
| 200 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s", i + 1, item->pos, item->node->schema->name); |
| 201 | } |
| 202 | break; |
| 203 | case LYXP_NODE_TEXT: |
| 204 | if (item->node->schema->nodetype & LYS_ANYDATA) { |
| 205 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT <%s>", i + 1, item->pos, |
| 206 | item->node->schema->nodetype == LYS_ANYXML ? "anyxml" : "anydata"); |
| 207 | } else { |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame^] | 208 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT %s", i + 1, item->pos, LYD_CANONICAL(item->node)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 209 | } |
| 210 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 211 | case LYXP_NODE_META: |
| 212 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): META %s = %s", i + 1, item->pos, set->val.meta[i].meta->name, |
| 213 | set->val.meta[i].meta->value); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 214 | break; |
| 215 | } |
| 216 | } |
| 217 | break; |
| 218 | |
| 219 | case LYXP_SET_SCNODE_SET: |
| 220 | LOGDBG(LY_LDGXPATH, "set SCNODE SET:"); |
| 221 | for (i = 0; i < set->used; ++i) { |
| 222 | sitem = &set->val.scnodes[i]; |
| 223 | |
| 224 | switch (sitem->type) { |
| 225 | case LYXP_NODE_ROOT: |
| 226 | LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT", i + 1, sitem->in_ctx); |
| 227 | break; |
| 228 | case LYXP_NODE_ROOT_CONFIG: |
| 229 | LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT CONFIG", i + 1, sitem->in_ctx); |
| 230 | break; |
| 231 | case LYXP_NODE_ELEM: |
| 232 | LOGDBG(LY_LDGXPATH, "\t%d (%u): ELEM %s", i + 1, sitem->in_ctx, sitem->scnode->name); |
| 233 | break; |
| 234 | default: |
| 235 | LOGINT(NULL); |
| 236 | break; |
| 237 | } |
| 238 | } |
| 239 | break; |
| 240 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 241 | case LYXP_SET_BOOLEAN: |
| 242 | LOGDBG(LY_LDGXPATH, "set BOOLEAN"); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 243 | LOGDBG(LY_LDGXPATH, "\t%s", (set->val.bln ? "true" : "false")); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 244 | break; |
| 245 | |
| 246 | case LYXP_SET_STRING: |
| 247 | LOGDBG(LY_LDGXPATH, "set STRING"); |
| 248 | LOGDBG(LY_LDGXPATH, "\t%s", set->val.str); |
| 249 | break; |
| 250 | |
| 251 | case LYXP_SET_NUMBER: |
| 252 | LOGDBG(LY_LDGXPATH, "set NUMBER"); |
| 253 | |
| 254 | if (isnan(set->val.num)) { |
| 255 | str = strdup("NaN"); |
| 256 | } else if ((set->val.num == 0) || (set->val.num == -0.0f)) { |
| 257 | str = strdup("0"); |
| 258 | } else if (isinf(set->val.num) && !signbit(set->val.num)) { |
| 259 | str = strdup("Infinity"); |
| 260 | } else if (isinf(set->val.num) && signbit(set->val.num)) { |
| 261 | str = strdup("-Infinity"); |
| 262 | } else if ((long long)set->val.num == set->val.num) { |
| 263 | if (asprintf(&str, "%lld", (long long)set->val.num) == -1) { |
| 264 | str = NULL; |
| 265 | } |
| 266 | } else { |
| 267 | if (asprintf(&str, "%03.1Lf", set->val.num) == -1) { |
| 268 | str = NULL; |
| 269 | } |
| 270 | } |
| 271 | LY_CHECK_ERR_RET(!str, LOGMEM(NULL), ); |
| 272 | |
| 273 | LOGDBG(LY_LDGXPATH, "\t%s", str); |
| 274 | free(str); |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | #endif |
| 279 | |
| 280 | /** |
| 281 | * @brief Realloc the string \p str. |
| 282 | * |
| 283 | * @param[in] ctx libyang context for logging. |
| 284 | * @param[in] needed How much free space is required. |
| 285 | * @param[in,out] str Pointer to the string to use. |
| 286 | * @param[in,out] used Used bytes in \p str. |
| 287 | * @param[in,out] size Allocated bytes in \p str. |
| 288 | * @return LY_ERR |
| 289 | */ |
| 290 | static LY_ERR |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 291 | cast_string_realloc(const struct ly_ctx *ctx, uint16_t needed, char **str, uint16_t *used, uint16_t *size) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 292 | { |
| 293 | if (*size - *used < needed) { |
| 294 | do { |
| 295 | if ((UINT16_MAX - *size) < LYXP_STRING_CAST_SIZE_STEP) { |
| 296 | LOGERR(ctx, LY_EINVAL, "XPath string length limit (%u) reached.", UINT16_MAX); |
| 297 | return LY_EINVAL; |
| 298 | } |
| 299 | *size += LYXP_STRING_CAST_SIZE_STEP; |
| 300 | } while (*size - *used < needed); |
| 301 | *str = ly_realloc(*str, *size * sizeof(char)); |
| 302 | LY_CHECK_ERR_RET(!(*str), LOGMEM(ctx), LY_EMEM); |
| 303 | } |
| 304 | |
| 305 | return LY_SUCCESS; |
| 306 | } |
| 307 | |
| 308 | /** |
| 309 | * @brief Cast nodes recursively to one string @p str. |
| 310 | * |
| 311 | * @param[in] node Node to cast. |
| 312 | * @param[in] fake_cont Whether to put the data into a "fake" container. |
| 313 | * @param[in] root_type Type of the XPath root. |
| 314 | * @param[in] indent Current indent. |
| 315 | * @param[in,out] str Resulting string. |
| 316 | * @param[in,out] used Used bytes in @p str. |
| 317 | * @param[in,out] size Allocated bytes in @p str. |
| 318 | * @return LY_ERR |
| 319 | */ |
| 320 | static LY_ERR |
| 321 | cast_string_recursive(const struct lyd_node *node, int fake_cont, enum lyxp_node_type root_type, uint16_t indent, char **str, |
| 322 | uint16_t *used, uint16_t *size) |
| 323 | { |
Radek Krejci | 7f769d7 | 2020-07-11 23:13:56 +0200 | [diff] [blame] | 324 | char *buf, *line, *ptr = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 325 | const char *value_str; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 326 | const struct lyd_node *child; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 327 | struct lyd_node *tree; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 328 | struct lyd_node_any *any; |
| 329 | LY_ERR rc; |
| 330 | |
| 331 | if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) { |
| 332 | return LY_SUCCESS; |
| 333 | } |
| 334 | |
| 335 | if (fake_cont) { |
| 336 | rc = cast_string_realloc(LYD_NODE_CTX(node), 1, str, used, size); |
| 337 | LY_CHECK_RET(rc); |
| 338 | strcpy(*str + (*used - 1), "\n"); |
| 339 | ++(*used); |
| 340 | |
| 341 | ++indent; |
| 342 | } |
| 343 | |
| 344 | switch (node->schema->nodetype) { |
| 345 | case LYS_CONTAINER: |
| 346 | case LYS_LIST: |
| 347 | case LYS_RPC: |
| 348 | case LYS_NOTIF: |
| 349 | rc = cast_string_realloc(LYD_NODE_CTX(node), 1, str, used, size); |
| 350 | LY_CHECK_RET(rc); |
| 351 | strcpy(*str + (*used - 1), "\n"); |
| 352 | ++(*used); |
| 353 | |
Michal Vasko | 5bfd4be | 2020-06-23 13:26:19 +0200 | [diff] [blame] | 354 | for (child = lyd_node_children(node, 0); child; child = child->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 355 | rc = cast_string_recursive(child, 0, root_type, indent + 1, str, used, size); |
| 356 | LY_CHECK_RET(rc); |
| 357 | } |
| 358 | |
| 359 | break; |
| 360 | |
| 361 | case LYS_LEAF: |
| 362 | case LYS_LEAFLIST: |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame^] | 363 | value_str = LYD_CANONICAL(node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 364 | |
| 365 | /* print indent */ |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame^] | 366 | LY_CHECK_RET(cast_string_realloc(LYD_NODE_CTX(node), indent * 2 + strlen(value_str) + 1, str, used, size)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 367 | memset(*str + (*used - 1), ' ', indent * 2); |
| 368 | *used += indent * 2; |
| 369 | |
| 370 | /* print value */ |
| 371 | if (*used == 1) { |
| 372 | sprintf(*str + (*used - 1), "%s", value_str); |
| 373 | *used += strlen(value_str); |
| 374 | } else { |
| 375 | sprintf(*str + (*used - 1), "%s\n", value_str); |
| 376 | *used += strlen(value_str) + 1; |
| 377 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 378 | |
| 379 | break; |
| 380 | |
| 381 | case LYS_ANYXML: |
| 382 | case LYS_ANYDATA: |
| 383 | any = (struct lyd_node_any *)node; |
| 384 | if (!(void *)any->value.tree) { |
| 385 | /* no content */ |
| 386 | buf = strdup(""); |
| 387 | LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_NODE_CTX(node)), LY_EMEM); |
| 388 | } else { |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 389 | struct ly_out *out; |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 390 | |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 391 | if (any->value_type == LYD_ANYDATA_LYB) { |
| 392 | /* try to parse it into a data tree */ |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 393 | 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] | 394 | /* successfully parsed */ |
| 395 | free(any->value.mem); |
| 396 | any->value.tree = tree; |
| 397 | any->value_type = LYD_ANYDATA_DATATREE; |
| 398 | } |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 399 | /* error is covered by the following switch where LYD_ANYDATA_LYB causes failure */ |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 400 | } |
| 401 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 402 | switch (any->value_type) { |
| 403 | case LYD_ANYDATA_STRING: |
| 404 | case LYD_ANYDATA_XML: |
| 405 | case LYD_ANYDATA_JSON: |
| 406 | buf = strdup(any->value.json); |
| 407 | LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_NODE_CTX(node)), LY_EMEM); |
| 408 | break; |
| 409 | case LYD_ANYDATA_DATATREE: |
Radek Krejci | 84ce7b1 | 2020-06-11 17:28:25 +0200 | [diff] [blame] | 410 | LY_CHECK_RET(ly_out_new_memory(&buf, 0, &out)); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 411 | rc = lyd_print_all(out, any->value.tree, LYD_XML, 0); |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 412 | ly_out_free(out, NULL, 0); |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 413 | LY_CHECK_RET(rc < 0, -rc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 414 | break; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 415 | case LYD_ANYDATA_LYB: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 416 | 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] | 417 | return LY_EINVAL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 418 | } |
| 419 | } |
| 420 | |
| 421 | line = strtok_r(buf, "\n", &ptr); |
| 422 | do { |
| 423 | rc = cast_string_realloc(LYD_NODE_CTX(node), indent * 2 + strlen(line) + 1, str, used, size); |
| 424 | if (rc != LY_SUCCESS) { |
| 425 | free(buf); |
| 426 | return rc; |
| 427 | } |
| 428 | memset(*str + (*used - 1), ' ', indent * 2); |
| 429 | *used += indent * 2; |
| 430 | |
| 431 | strcpy(*str + (*used - 1), line); |
| 432 | *used += strlen(line); |
| 433 | |
| 434 | strcpy(*str + (*used - 1), "\n"); |
| 435 | *used += 1; |
| 436 | } while ((line = strtok_r(NULL, "\n", &ptr))); |
| 437 | |
| 438 | free(buf); |
| 439 | break; |
| 440 | |
| 441 | default: |
| 442 | LOGINT_RET(LYD_NODE_CTX(node)); |
| 443 | } |
| 444 | |
| 445 | if (fake_cont) { |
| 446 | rc = cast_string_realloc(LYD_NODE_CTX(node), 1, str, used, size); |
| 447 | LY_CHECK_RET(rc); |
| 448 | strcpy(*str + (*used - 1), "\n"); |
| 449 | ++(*used); |
| 450 | |
| 451 | --indent; |
| 452 | } |
| 453 | |
| 454 | return LY_SUCCESS; |
| 455 | } |
| 456 | |
| 457 | /** |
| 458 | * @brief Cast an element into a string. |
| 459 | * |
| 460 | * @param[in] node Node to cast. |
| 461 | * @param[in] fake_cont Whether to put the data into a "fake" container. |
| 462 | * @param[in] root_type Type of the XPath root. |
| 463 | * @param[out] str Element cast to dynamically-allocated string. |
| 464 | * @return LY_ERR |
| 465 | */ |
| 466 | static LY_ERR |
| 467 | cast_string_elem(struct lyd_node *node, int fake_cont, enum lyxp_node_type root_type, char **str) |
| 468 | { |
| 469 | uint16_t used, size; |
| 470 | LY_ERR rc; |
| 471 | |
| 472 | *str = malloc(LYXP_STRING_CAST_SIZE_START * sizeof(char)); |
| 473 | LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_NODE_CTX(node)), LY_EMEM); |
| 474 | (*str)[0] = '\0'; |
| 475 | used = 1; |
| 476 | size = LYXP_STRING_CAST_SIZE_START; |
| 477 | |
| 478 | rc = cast_string_recursive(node, fake_cont, root_type, 0, str, &used, &size); |
| 479 | if (rc != LY_SUCCESS) { |
| 480 | free(*str); |
| 481 | return rc; |
| 482 | } |
| 483 | |
| 484 | if (size > used) { |
| 485 | *str = ly_realloc(*str, used * sizeof(char)); |
| 486 | LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_NODE_CTX(node)), LY_EMEM); |
| 487 | } |
| 488 | return LY_SUCCESS; |
| 489 | } |
| 490 | |
| 491 | /** |
| 492 | * @brief Cast a LYXP_SET_NODE_SET set into a string. |
| 493 | * Context position aware. |
| 494 | * |
| 495 | * @param[in] set Set to cast. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 496 | * @param[out] str Cast dynamically-allocated string. |
| 497 | * @return LY_ERR |
| 498 | */ |
| 499 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 500 | cast_node_set_to_string(struct lyxp_set *set, char **str) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 501 | { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 502 | switch (set->val.nodes[0].type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 503 | case LYXP_NODE_NONE: |
| 504 | /* invalid */ |
| 505 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 506 | case LYXP_NODE_ROOT: |
| 507 | case LYXP_NODE_ROOT_CONFIG: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 508 | return cast_string_elem(set->val.nodes[0].node, 1, set->root_type, str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 509 | case LYXP_NODE_ELEM: |
| 510 | case LYXP_NODE_TEXT: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 511 | return cast_string_elem(set->val.nodes[0].node, 0, set->root_type, str); |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 512 | case LYXP_NODE_META: |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame^] | 513 | *str = strdup(set->val.meta[0].meta->value.canonical); |
| 514 | if (!*str) { |
| 515 | LOGMEM_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 516 | } |
| 517 | return LY_SUCCESS; |
| 518 | } |
| 519 | |
| 520 | LOGINT_RET(set->ctx); |
| 521 | } |
| 522 | |
| 523 | /** |
| 524 | * @brief Cast a string into an XPath number. |
| 525 | * |
| 526 | * @param[in] str String to use. |
| 527 | * @return Cast number. |
| 528 | */ |
| 529 | static long double |
| 530 | cast_string_to_number(const char *str) |
| 531 | { |
| 532 | long double num; |
| 533 | char *ptr; |
| 534 | |
| 535 | errno = 0; |
| 536 | num = strtold(str, &ptr); |
| 537 | if (errno || *ptr) { |
| 538 | num = NAN; |
| 539 | } |
| 540 | return num; |
| 541 | } |
| 542 | |
| 543 | /** |
| 544 | * @brief Callback for checking value equality. |
| 545 | * |
| 546 | * @param[in] val1_p First value. |
| 547 | * @param[in] val2_p Second value. |
| 548 | * @param[in] mod Whether hash table is being modified. |
| 549 | * @param[in] cb_data Callback data. |
| 550 | * @return 0 if not equal, non-zero if equal. |
| 551 | */ |
| 552 | static int |
| 553 | set_values_equal_cb(void *val1_p, void *val2_p, int UNUSED(mod), void *UNUSED(cb_data)) |
| 554 | { |
| 555 | struct lyxp_set_hash_node *val1, *val2; |
| 556 | |
| 557 | val1 = (struct lyxp_set_hash_node *)val1_p; |
| 558 | val2 = (struct lyxp_set_hash_node *)val2_p; |
| 559 | |
| 560 | if ((val1->node == val2->node) && (val1->type == val2->type)) { |
| 561 | return 1; |
| 562 | } |
| 563 | |
| 564 | return 0; |
| 565 | } |
| 566 | |
| 567 | /** |
| 568 | * @brief Insert node and its hash into set. |
| 569 | * |
| 570 | * @param[in] set et to insert to. |
| 571 | * @param[in] node Node with hash. |
| 572 | * @param[in] type Node type. |
| 573 | */ |
| 574 | static void |
| 575 | set_insert_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type) |
| 576 | { |
| 577 | int r; |
| 578 | uint32_t i, hash; |
| 579 | struct lyxp_set_hash_node hnode; |
| 580 | |
| 581 | if (!set->ht && (set->used >= LYD_HT_MIN_ITEMS)) { |
| 582 | /* create hash table and add all the nodes */ |
| 583 | set->ht = lyht_new(1, sizeof(struct lyxp_set_hash_node), set_values_equal_cb, NULL, 1); |
| 584 | for (i = 0; i < set->used; ++i) { |
| 585 | hnode.node = set->val.nodes[i].node; |
| 586 | hnode.type = set->val.nodes[i].type; |
| 587 | |
| 588 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 589 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 590 | hash = dict_hash_multi(hash, NULL, 0); |
| 591 | |
| 592 | r = lyht_insert(set->ht, &hnode, hash, NULL); |
| 593 | assert(!r); |
| 594 | (void)r; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 595 | |
Michal Vasko | 9d6befd | 2019-12-11 14:56:56 +0100 | [diff] [blame] | 596 | if (hnode.node == node) { |
| 597 | /* it was just added, do not add it twice */ |
| 598 | node = NULL; |
| 599 | } |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | if (set->ht && node) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 604 | /* add the new node into hash table */ |
| 605 | hnode.node = node; |
| 606 | hnode.type = type; |
| 607 | |
| 608 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 609 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 610 | hash = dict_hash_multi(hash, NULL, 0); |
| 611 | |
| 612 | r = lyht_insert(set->ht, &hnode, hash, NULL); |
| 613 | assert(!r); |
| 614 | (void)r; |
| 615 | } |
| 616 | } |
| 617 | |
| 618 | /** |
| 619 | * @brief Remove node and its hash from set. |
| 620 | * |
| 621 | * @param[in] set Set to remove from. |
| 622 | * @param[in] node Node to remove. |
| 623 | * @param[in] type Node type. |
| 624 | */ |
| 625 | static void |
| 626 | set_remove_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type) |
| 627 | { |
| 628 | int r; |
| 629 | struct lyxp_set_hash_node hnode; |
| 630 | uint32_t hash; |
| 631 | |
| 632 | if (set->ht) { |
| 633 | hnode.node = node; |
| 634 | hnode.type = type; |
| 635 | |
| 636 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 637 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 638 | hash = dict_hash_multi(hash, NULL, 0); |
| 639 | |
| 640 | r = lyht_remove(set->ht, &hnode, hash); |
| 641 | assert(!r); |
| 642 | (void)r; |
| 643 | |
| 644 | if (!set->ht->used) { |
| 645 | lyht_free(set->ht); |
| 646 | set->ht = NULL; |
| 647 | } |
| 648 | } |
| 649 | } |
| 650 | |
| 651 | /** |
| 652 | * @brief Check whether node is in set based on its hash. |
| 653 | * |
| 654 | * @param[in] set Set to search in. |
| 655 | * @param[in] node Node to search for. |
| 656 | * @param[in] type Node type. |
| 657 | * @param[in] skip_idx Index in @p set to skip. |
| 658 | * @return LY_ERR |
| 659 | */ |
| 660 | static LY_ERR |
| 661 | set_dup_node_hash_check(const struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type, int skip_idx) |
| 662 | { |
| 663 | struct lyxp_set_hash_node hnode, *match_p; |
| 664 | uint32_t hash; |
| 665 | |
| 666 | hnode.node = node; |
| 667 | hnode.type = type; |
| 668 | |
| 669 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 670 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 671 | hash = dict_hash_multi(hash, NULL, 0); |
| 672 | |
| 673 | if (!lyht_find(set->ht, &hnode, hash, (void **)&match_p)) { |
| 674 | if ((skip_idx > -1) && (set->val.nodes[skip_idx].node == match_p->node) && (set->val.nodes[skip_idx].type == match_p->type)) { |
| 675 | /* we found it on the index that should be skipped, find another */ |
| 676 | hnode = *match_p; |
| 677 | if (lyht_find_next(set->ht, &hnode, hash, (void **)&match_p)) { |
| 678 | /* none other found */ |
| 679 | return LY_SUCCESS; |
| 680 | } |
| 681 | } |
| 682 | |
| 683 | return LY_EEXIST; |
| 684 | } |
| 685 | |
| 686 | /* not found */ |
| 687 | return LY_SUCCESS; |
| 688 | } |
| 689 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 690 | void |
| 691 | lyxp_set_free_content(struct lyxp_set *set) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 692 | { |
| 693 | if (!set) { |
| 694 | return; |
| 695 | } |
| 696 | |
| 697 | if (set->type == LYXP_SET_NODE_SET) { |
| 698 | free(set->val.nodes); |
| 699 | lyht_free(set->ht); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 700 | } else if (set->type == LYXP_SET_SCNODE_SET) { |
| 701 | free(set->val.scnodes); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 702 | lyht_free(set->ht); |
| 703 | } else { |
| 704 | if (set->type == LYXP_SET_STRING) { |
| 705 | free(set->val.str); |
| 706 | } |
| 707 | set->type = LYXP_SET_NODE_SET; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 708 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 709 | |
| 710 | set->val.nodes = NULL; |
| 711 | set->used = 0; |
| 712 | set->size = 0; |
| 713 | set->ht = NULL; |
| 714 | set->ctx_pos = 0; |
| 715 | set->ctx_pos = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 716 | } |
| 717 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 718 | /** |
| 719 | * @brief Free dynamically-allocated set. |
| 720 | * |
| 721 | * @param[in] set Set to free. |
| 722 | */ |
| 723 | static void |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 724 | lyxp_set_free(struct lyxp_set *set) |
| 725 | { |
| 726 | if (!set) { |
| 727 | return; |
| 728 | } |
| 729 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 730 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 731 | free(set); |
| 732 | } |
| 733 | |
| 734 | /** |
| 735 | * @brief Initialize set context. |
| 736 | * |
| 737 | * @param[in] new Set to initialize. |
| 738 | * @param[in] set Arbitrary initialized set. |
| 739 | */ |
| 740 | static void |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 741 | set_init(struct lyxp_set *new, const struct lyxp_set *set) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 742 | { |
| 743 | memset(new, 0, sizeof *new); |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 744 | if (set) { |
| 745 | new->ctx = set->ctx; |
| 746 | new->ctx_node = set->ctx_node; |
Michal Vasko | 588112f | 2019-12-10 14:51:53 +0100 | [diff] [blame] | 747 | new->root_type = set->root_type; |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 748 | new->context_op = set->context_op; |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 749 | new->local_mod = set->local_mod; |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 750 | new->tree = set->tree; |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 751 | new->format = set->format; |
| 752 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 753 | } |
| 754 | |
| 755 | /** |
| 756 | * @brief Create a deep copy of a set. |
| 757 | * |
| 758 | * @param[in] set Set to copy. |
| 759 | * @return Copy of @p set. |
| 760 | */ |
| 761 | static struct lyxp_set * |
| 762 | set_copy(struct lyxp_set *set) |
| 763 | { |
| 764 | struct lyxp_set *ret; |
| 765 | uint16_t i; |
Michal Vasko | ba71654 | 2019-12-16 10:01:58 +0100 | [diff] [blame] | 766 | int idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 767 | |
| 768 | if (!set) { |
| 769 | return NULL; |
| 770 | } |
| 771 | |
| 772 | ret = malloc(sizeof *ret); |
| 773 | LY_CHECK_ERR_RET(!ret, LOGMEM(set->ctx), NULL); |
| 774 | set_init(ret, set); |
| 775 | |
| 776 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 777 | ret->type = set->type; |
| 778 | |
| 779 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | ba71654 | 2019-12-16 10:01:58 +0100 | [diff] [blame] | 780 | if ((set->val.scnodes[i].in_ctx == 1) || (set->val.scnodes[i].in_ctx == -2)) { |
| 781 | 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] | 782 | /* coverity seems to think scnodes can be NULL */ |
| 783 | if ((idx == -1) || !ret->val.scnodes) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 784 | lyxp_set_free(ret); |
| 785 | return NULL; |
| 786 | } |
Michal Vasko | ba71654 | 2019-12-16 10:01:58 +0100 | [diff] [blame] | 787 | ret->val.scnodes[idx].in_ctx = set->val.scnodes[i].in_ctx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 788 | } |
| 789 | } |
| 790 | } else if (set->type == LYXP_SET_NODE_SET) { |
| 791 | ret->type = set->type; |
| 792 | ret->val.nodes = malloc(set->used * sizeof *ret->val.nodes); |
| 793 | LY_CHECK_ERR_RET(!ret->val.nodes, LOGMEM(set->ctx); free(ret), NULL); |
| 794 | memcpy(ret->val.nodes, set->val.nodes, set->used * sizeof *ret->val.nodes); |
| 795 | |
| 796 | ret->used = ret->size = set->used; |
| 797 | ret->ctx_pos = set->ctx_pos; |
| 798 | ret->ctx_size = set->ctx_size; |
| 799 | ret->ht = lyht_dup(set->ht); |
| 800 | } else { |
| 801 | memcpy(ret, set, sizeof *ret); |
| 802 | if (set->type == LYXP_SET_STRING) { |
| 803 | ret->val.str = strdup(set->val.str); |
| 804 | LY_CHECK_ERR_RET(!ret->val.str, LOGMEM(set->ctx); free(ret), NULL); |
| 805 | } |
| 806 | } |
| 807 | |
| 808 | return ret; |
| 809 | } |
| 810 | |
| 811 | /** |
| 812 | * @brief Fill XPath set with a string. Any current data are disposed of. |
| 813 | * |
| 814 | * @param[in] set Set to fill. |
| 815 | * @param[in] string String to fill into \p set. |
| 816 | * @param[in] str_len Length of \p string. 0 is a valid value! |
| 817 | */ |
| 818 | static void |
| 819 | set_fill_string(struct lyxp_set *set, const char *string, uint16_t str_len) |
| 820 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 821 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 822 | |
| 823 | set->type = LYXP_SET_STRING; |
| 824 | if ((str_len == 0) && (string[0] != '\0')) { |
| 825 | string = ""; |
| 826 | } |
| 827 | set->val.str = strndup(string, str_len); |
| 828 | } |
| 829 | |
| 830 | /** |
| 831 | * @brief Fill XPath set with a number. Any current data are disposed of. |
| 832 | * |
| 833 | * @param[in] set Set to fill. |
| 834 | * @param[in] number Number to fill into \p set. |
| 835 | */ |
| 836 | static void |
| 837 | set_fill_number(struct lyxp_set *set, long double number) |
| 838 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 839 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 840 | |
| 841 | set->type = LYXP_SET_NUMBER; |
| 842 | set->val.num = number; |
| 843 | } |
| 844 | |
| 845 | /** |
| 846 | * @brief Fill XPath set with a boolean. Any current data are disposed of. |
| 847 | * |
| 848 | * @param[in] set Set to fill. |
| 849 | * @param[in] boolean Boolean to fill into \p set. |
| 850 | */ |
| 851 | static void |
| 852 | set_fill_boolean(struct lyxp_set *set, int boolean) |
| 853 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 854 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 855 | |
| 856 | set->type = LYXP_SET_BOOLEAN; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 857 | set->val.bln = boolean; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 858 | } |
| 859 | |
| 860 | /** |
| 861 | * @brief Fill XPath set with the value from another set (deep assign). |
| 862 | * Any current data are disposed of. |
| 863 | * |
| 864 | * @param[in] trg Set to fill. |
| 865 | * @param[in] src Source set to copy into \p trg. |
| 866 | */ |
| 867 | static void |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 868 | set_fill_set(struct lyxp_set *trg, const struct lyxp_set *src) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 869 | { |
| 870 | if (!trg || !src) { |
| 871 | return; |
| 872 | } |
| 873 | |
| 874 | if (trg->type == LYXP_SET_NODE_SET) { |
| 875 | free(trg->val.nodes); |
| 876 | } else if (trg->type == LYXP_SET_STRING) { |
| 877 | free(trg->val.str); |
| 878 | } |
| 879 | set_init(trg, src); |
| 880 | |
| 881 | if (src->type == LYXP_SET_SCNODE_SET) { |
| 882 | trg->type = LYXP_SET_SCNODE_SET; |
| 883 | trg->used = src->used; |
| 884 | trg->size = src->used; |
| 885 | |
| 886 | trg->val.scnodes = ly_realloc(trg->val.scnodes, trg->size * sizeof *trg->val.scnodes); |
| 887 | LY_CHECK_ERR_RET(!trg->val.scnodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), ); |
| 888 | memcpy(trg->val.scnodes, src->val.scnodes, src->used * sizeof *src->val.scnodes); |
| 889 | } else if (src->type == LYXP_SET_BOOLEAN) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 890 | set_fill_boolean(trg, src->val.bln); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 891 | } else if (src->type == LYXP_SET_NUMBER) { |
| 892 | set_fill_number(trg, src->val.num); |
| 893 | } else if (src->type == LYXP_SET_STRING) { |
| 894 | set_fill_string(trg, src->val.str, strlen(src->val.str)); |
| 895 | } else { |
| 896 | if (trg->type == LYXP_SET_NODE_SET) { |
| 897 | free(trg->val.nodes); |
| 898 | } else if (trg->type == LYXP_SET_STRING) { |
| 899 | free(trg->val.str); |
| 900 | } |
| 901 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 902 | assert(src->type == LYXP_SET_NODE_SET); |
| 903 | |
| 904 | trg->type = LYXP_SET_NODE_SET; |
| 905 | trg->used = src->used; |
| 906 | trg->size = src->used; |
| 907 | trg->ctx_pos = src->ctx_pos; |
| 908 | trg->ctx_size = src->ctx_size; |
| 909 | |
| 910 | trg->val.nodes = malloc(trg->used * sizeof *trg->val.nodes); |
| 911 | LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), ); |
| 912 | memcpy(trg->val.nodes, src->val.nodes, src->used * sizeof *src->val.nodes); |
| 913 | if (src->ht) { |
| 914 | trg->ht = lyht_dup(src->ht); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 915 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 916 | trg->ht = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 917 | } |
| 918 | } |
| 919 | } |
| 920 | |
| 921 | /** |
| 922 | * @brief Clear context of all schema nodes. |
| 923 | * |
| 924 | * @param[in] set Set to clear. |
| 925 | */ |
| 926 | static void |
| 927 | set_scnode_clear_ctx(struct lyxp_set *set) |
| 928 | { |
| 929 | uint32_t i; |
| 930 | |
| 931 | for (i = 0; i < set->used; ++i) { |
| 932 | if (set->val.scnodes[i].in_ctx == 1) { |
| 933 | set->val.scnodes[i].in_ctx = 0; |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 934 | } else if (set->val.scnodes[i].in_ctx == -2) { |
| 935 | set->val.scnodes[i].in_ctx = -1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 936 | } |
| 937 | } |
| 938 | } |
| 939 | |
| 940 | /** |
| 941 | * @brief Remove a node from a set. Removing last node changes |
| 942 | * set into LYXP_SET_EMPTY. Context position aware. |
| 943 | * |
| 944 | * @param[in] set Set to use. |
| 945 | * @param[in] idx Index from @p set of the node to be removed. |
| 946 | */ |
| 947 | static void |
| 948 | set_remove_node(struct lyxp_set *set, uint32_t idx) |
| 949 | { |
| 950 | assert(set && (set->type == LYXP_SET_NODE_SET)); |
| 951 | assert(idx < set->used); |
| 952 | |
| 953 | set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
| 954 | |
| 955 | --set->used; |
| 956 | if (set->used) { |
| 957 | memmove(&set->val.nodes[idx], &set->val.nodes[idx + 1], |
| 958 | (set->used - idx) * sizeof *set->val.nodes); |
| 959 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 960 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 961 | } |
| 962 | } |
| 963 | |
| 964 | /** |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 965 | * @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] | 966 | * |
| 967 | * @param[in] set Set to use. |
| 968 | * @param[in] idx Index from @p set of the node to be removed. |
| 969 | */ |
| 970 | static void |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 971 | set_remove_node_none(struct lyxp_set *set, uint32_t idx) |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 972 | { |
| 973 | assert(set && (set->type == LYXP_SET_NODE_SET)); |
| 974 | assert(idx < set->used); |
| 975 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 976 | if (set->val.nodes[idx].type == LYXP_NODE_ELEM) { |
| 977 | set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
| 978 | } |
| 979 | set->val.nodes[idx].type = LYXP_NODE_NONE; |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 980 | } |
| 981 | |
| 982 | /** |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 983 | * @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] | 984 | * set into LYXP_SET_EMPTY. Context position aware. |
| 985 | * |
| 986 | * @param[in] set Set to consolidate. |
| 987 | */ |
| 988 | static void |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 989 | set_remove_nodes_none(struct lyxp_set *set) |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 990 | { |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 991 | uint16_t i, orig_used, end = 0; |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 992 | int32_t start; |
| 993 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 994 | assert(set); |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 995 | |
| 996 | orig_used = set->used; |
| 997 | set->used = 0; |
| 998 | for (i = 0; i < orig_used;) { |
| 999 | start = -1; |
| 1000 | do { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1001 | if ((set->val.nodes[i].type != LYXP_NODE_NONE) && (start == -1)) { |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1002 | start = i; |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1003 | } else if ((start > -1) && (set->val.nodes[i].type == LYXP_NODE_NONE)) { |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1004 | end = i; |
| 1005 | ++i; |
| 1006 | break; |
| 1007 | } |
| 1008 | |
| 1009 | ++i; |
| 1010 | if (i == orig_used) { |
| 1011 | end = i; |
| 1012 | } |
| 1013 | } while (i < orig_used); |
| 1014 | |
| 1015 | if (start > -1) { |
| 1016 | /* move the whole chunk of valid nodes together */ |
| 1017 | if (set->used != (unsigned)start) { |
| 1018 | memmove(&set->val.nodes[set->used], &set->val.nodes[start], (end - start) * sizeof *set->val.nodes); |
| 1019 | } |
| 1020 | set->used += end - start; |
| 1021 | } |
| 1022 | } |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1023 | } |
| 1024 | |
| 1025 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1026 | * @brief Check for duplicates in a node set. |
| 1027 | * |
| 1028 | * @param[in] set Set to check. |
| 1029 | * @param[in] node Node to look for in @p set. |
| 1030 | * @param[in] node_type Type of @p node. |
| 1031 | * @param[in] skip_idx Index from @p set to skip. |
| 1032 | * @return LY_ERR |
| 1033 | */ |
| 1034 | static LY_ERR |
| 1035 | set_dup_node_check(const struct lyxp_set *set, const struct lyd_node *node, enum lyxp_node_type node_type, int skip_idx) |
| 1036 | { |
| 1037 | uint32_t i; |
| 1038 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1039 | if (set->ht && node) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1040 | return set_dup_node_hash_check(set, (struct lyd_node *)node, node_type, skip_idx); |
| 1041 | } |
| 1042 | |
| 1043 | for (i = 0; i < set->used; ++i) { |
| 1044 | if ((skip_idx > -1) && (i == (unsigned)skip_idx)) { |
| 1045 | continue; |
| 1046 | } |
| 1047 | |
| 1048 | if ((set->val.nodes[i].node == node) && (set->val.nodes[i].type == node_type)) { |
| 1049 | return LY_EEXIST; |
| 1050 | } |
| 1051 | } |
| 1052 | |
| 1053 | return LY_SUCCESS; |
| 1054 | } |
| 1055 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 1056 | int |
| 1057 | 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] | 1058 | { |
| 1059 | uint32_t i; |
| 1060 | |
| 1061 | for (i = 0; i < set->used; ++i) { |
| 1062 | if ((skip_idx > -1) && (i == (unsigned)skip_idx)) { |
| 1063 | continue; |
| 1064 | } |
| 1065 | |
| 1066 | if ((set->val.scnodes[i].scnode == node) && (set->val.scnodes[i].type == node_type)) { |
| 1067 | return i; |
| 1068 | } |
| 1069 | } |
| 1070 | |
| 1071 | return -1; |
| 1072 | } |
| 1073 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 1074 | void |
| 1075 | lyxp_set_scnode_merge(struct lyxp_set *set1, struct lyxp_set *set2) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1076 | { |
| 1077 | uint32_t orig_used, i, j; |
| 1078 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1079 | 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] | 1080 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1081 | if (!set2->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1082 | return; |
| 1083 | } |
| 1084 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1085 | if (!set1->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1086 | memcpy(set1, set2, sizeof *set1); |
| 1087 | return; |
| 1088 | } |
| 1089 | |
| 1090 | if (set1->used + set2->used > set1->size) { |
| 1091 | set1->size = set1->used + set2->used; |
| 1092 | set1->val.scnodes = ly_realloc(set1->val.scnodes, set1->size * sizeof *set1->val.scnodes); |
| 1093 | LY_CHECK_ERR_RET(!set1->val.scnodes, LOGMEM(set1->ctx), ); |
| 1094 | } |
| 1095 | |
| 1096 | orig_used = set1->used; |
| 1097 | |
| 1098 | for (i = 0; i < set2->used; ++i) { |
| 1099 | for (j = 0; j < orig_used; ++j) { |
| 1100 | /* detect duplicities */ |
| 1101 | if (set1->val.scnodes[j].scnode == set2->val.scnodes[i].scnode) { |
| 1102 | break; |
| 1103 | } |
| 1104 | } |
| 1105 | |
| 1106 | if (j == orig_used) { |
| 1107 | memcpy(&set1->val.scnodes[set1->used], &set2->val.scnodes[i], sizeof *set2->val.scnodes); |
| 1108 | ++set1->used; |
| 1109 | } |
| 1110 | } |
| 1111 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1112 | lyxp_set_free_content(set2); |
| 1113 | set2->type = LYXP_SET_SCNODE_SET; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1114 | } |
| 1115 | |
| 1116 | /** |
| 1117 | * @brief Insert a node into a set. Context position aware. |
| 1118 | * |
| 1119 | * @param[in] set Set to use. |
| 1120 | * @param[in] node Node to insert to @p set. |
| 1121 | * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting. |
| 1122 | * @param[in] node_type Node type of @p node. |
| 1123 | * @param[in] idx Index in @p set to insert into. |
| 1124 | */ |
| 1125 | static void |
| 1126 | set_insert_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx) |
| 1127 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1128 | assert(set && (set->type == LYXP_SET_NODE_SET)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1129 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1130 | if (!set->size) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1131 | /* first item */ |
| 1132 | if (idx) { |
| 1133 | /* no real harm done, but it is a bug */ |
| 1134 | LOGINT(set->ctx); |
| 1135 | idx = 0; |
| 1136 | } |
| 1137 | set->val.nodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.nodes); |
| 1138 | LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), ); |
| 1139 | set->type = LYXP_SET_NODE_SET; |
| 1140 | set->used = 0; |
| 1141 | set->size = LYXP_SET_SIZE_START; |
| 1142 | set->ctx_pos = 1; |
| 1143 | set->ctx_size = 1; |
| 1144 | set->ht = NULL; |
| 1145 | } else { |
| 1146 | /* not an empty set */ |
| 1147 | if (set->used == set->size) { |
| 1148 | |
| 1149 | /* set is full */ |
| 1150 | set->val.nodes = ly_realloc(set->val.nodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.nodes); |
| 1151 | LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), ); |
| 1152 | set->size += LYXP_SET_SIZE_STEP; |
| 1153 | } |
| 1154 | |
| 1155 | if (idx > set->used) { |
| 1156 | LOGINT(set->ctx); |
| 1157 | idx = set->used; |
| 1158 | } |
| 1159 | |
| 1160 | /* make space for the new node */ |
| 1161 | if (idx < set->used) { |
| 1162 | memmove(&set->val.nodes[idx + 1], &set->val.nodes[idx], (set->used - idx) * sizeof *set->val.nodes); |
| 1163 | } |
| 1164 | } |
| 1165 | |
| 1166 | /* finally assign the value */ |
| 1167 | set->val.nodes[idx].node = (struct lyd_node *)node; |
| 1168 | set->val.nodes[idx].type = node_type; |
| 1169 | set->val.nodes[idx].pos = pos; |
| 1170 | ++set->used; |
| 1171 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1172 | if (set->val.nodes[idx].type == LYXP_NODE_ELEM) { |
| 1173 | set_insert_node_hash(set, (struct lyd_node *)node, node_type); |
| 1174 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1175 | } |
| 1176 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 1177 | int |
| 1178 | 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] | 1179 | { |
| 1180 | int ret; |
| 1181 | |
| 1182 | assert(set->type == LYXP_SET_SCNODE_SET); |
| 1183 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 1184 | ret = lyxp_set_scnode_dup_node_check(set, node, node_type, -1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1185 | if (ret > -1) { |
| 1186 | set->val.scnodes[ret].in_ctx = 1; |
| 1187 | } else { |
| 1188 | if (set->used == set->size) { |
| 1189 | set->val.scnodes = ly_realloc(set->val.scnodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.scnodes); |
| 1190 | LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), -1); |
| 1191 | set->size += LYXP_SET_SIZE_STEP; |
| 1192 | } |
| 1193 | |
| 1194 | ret = set->used; |
| 1195 | set->val.scnodes[ret].scnode = (struct lysc_node *)node; |
| 1196 | set->val.scnodes[ret].type = node_type; |
| 1197 | set->val.scnodes[ret].in_ctx = 1; |
| 1198 | ++set->used; |
| 1199 | } |
| 1200 | |
| 1201 | return ret; |
| 1202 | } |
| 1203 | |
| 1204 | /** |
| 1205 | * @brief Replace a node in a set with another. Context position aware. |
| 1206 | * |
| 1207 | * @param[in] set Set to use. |
| 1208 | * @param[in] node Node to insert to @p set. |
| 1209 | * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting. |
| 1210 | * @param[in] node_type Node type of @p node. |
| 1211 | * @param[in] idx Index in @p set of the node to replace. |
| 1212 | */ |
| 1213 | static void |
| 1214 | set_replace_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx) |
| 1215 | { |
| 1216 | assert(set && (idx < set->used)); |
| 1217 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1218 | if (set->val.nodes[idx].type == LYXP_NODE_ELEM) { |
| 1219 | set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
| 1220 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1221 | set->val.nodes[idx].node = (struct lyd_node *)node; |
| 1222 | set->val.nodes[idx].type = node_type; |
| 1223 | set->val.nodes[idx].pos = pos; |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1224 | if (set->val.nodes[idx].type == LYXP_NODE_ELEM) { |
| 1225 | set_insert_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
| 1226 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1227 | } |
| 1228 | |
| 1229 | /** |
| 1230 | * @brief Set all nodes with ctx 1 to a new unique context value. |
| 1231 | * |
| 1232 | * @param[in] set Set to modify. |
| 1233 | * @return New context value. |
| 1234 | */ |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 1235 | static int32_t |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1236 | set_scnode_new_in_ctx(struct lyxp_set *set) |
| 1237 | { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 1238 | uint32_t i; |
| 1239 | int32_t ret_ctx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1240 | |
| 1241 | assert(set->type == LYXP_SET_SCNODE_SET); |
| 1242 | |
| 1243 | ret_ctx = 3; |
| 1244 | retry: |
| 1245 | for (i = 0; i < set->used; ++i) { |
| 1246 | if (set->val.scnodes[i].in_ctx >= ret_ctx) { |
| 1247 | ret_ctx = set->val.scnodes[i].in_ctx + 1; |
| 1248 | goto retry; |
| 1249 | } |
| 1250 | } |
| 1251 | for (i = 0; i < set->used; ++i) { |
| 1252 | if (set->val.scnodes[i].in_ctx == 1) { |
| 1253 | set->val.scnodes[i].in_ctx = ret_ctx; |
| 1254 | } |
| 1255 | } |
| 1256 | |
| 1257 | return ret_ctx; |
| 1258 | } |
| 1259 | |
| 1260 | /** |
| 1261 | * @brief Get unique @p node position in the data. |
| 1262 | * |
| 1263 | * @param[in] node Node to find. |
| 1264 | * @param[in] node_type Node type of @p node. |
| 1265 | * @param[in] root Root node. |
| 1266 | * @param[in] root_type Type of the XPath @p root node. |
| 1267 | * @param[in] prev Node that we think is before @p node in DFS from @p root. Can optionally |
| 1268 | * be used to increase efficiency and start the DFS from this node. |
| 1269 | * @param[in] prev_pos Node @p prev position. Optional, but must be set if @p prev is set. |
| 1270 | * @return Node position. |
| 1271 | */ |
| 1272 | static uint32_t |
| 1273 | get_node_pos(const struct lyd_node *node, enum lyxp_node_type node_type, const struct lyd_node *root, |
| 1274 | enum lyxp_node_type root_type, const struct lyd_node **prev, uint32_t *prev_pos) |
| 1275 | { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1276 | const struct lyd_node *elem = NULL, *top_sibling; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1277 | uint32_t pos = 1; |
| 1278 | |
| 1279 | assert(prev && prev_pos && !root->prev->next); |
| 1280 | |
| 1281 | if ((node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ROOT_CONFIG)) { |
| 1282 | return 0; |
| 1283 | } |
| 1284 | |
| 1285 | if (*prev) { |
| 1286 | /* start from the previous element instead from the root */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1287 | pos = *prev_pos; |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1288 | for (top_sibling = *prev; top_sibling->parent; top_sibling = (struct lyd_node *)top_sibling->parent); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1289 | goto dfs_search; |
| 1290 | } |
| 1291 | |
| 1292 | for (top_sibling = root; top_sibling; top_sibling = top_sibling->next) { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1293 | LYD_TREE_DFS_BEGIN(top_sibling, elem) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1294 | dfs_search: |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1295 | if (*prev && !elem) { |
| 1296 | /* resume previous DFS */ |
| 1297 | elem = LYD_TREE_DFS_next = (struct lyd_node *)*prev; |
| 1298 | LYD_TREE_DFS_continue = 0; |
| 1299 | } |
| 1300 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1301 | if ((root_type == LYXP_NODE_ROOT_CONFIG) && (elem->schema->flags & LYS_CONFIG_R)) { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1302 | /* skip */ |
| 1303 | LYD_TREE_DFS_continue = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1304 | } else { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1305 | if (elem == node) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1306 | break; |
| 1307 | } |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1308 | ++pos; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1309 | } |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1310 | |
| 1311 | LYD_TREE_DFS_END(top_sibling, elem); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1312 | } |
| 1313 | |
| 1314 | /* node found */ |
| 1315 | if (elem) { |
| 1316 | break; |
| 1317 | } |
| 1318 | } |
| 1319 | |
| 1320 | if (!elem) { |
| 1321 | if (!(*prev)) { |
| 1322 | /* we went from root and failed to find it, cannot be */ |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1323 | LOGINT(LYD_NODE_CTX(node)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1324 | return 0; |
| 1325 | } else { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1326 | /* start the search again from the beginning */ |
| 1327 | *prev = root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1328 | |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1329 | top_sibling = root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1330 | pos = 1; |
| 1331 | goto dfs_search; |
| 1332 | } |
| 1333 | } |
| 1334 | |
| 1335 | /* remember the last found node for next time */ |
| 1336 | *prev = node; |
| 1337 | *prev_pos = pos; |
| 1338 | |
| 1339 | return pos; |
| 1340 | } |
| 1341 | |
| 1342 | /** |
| 1343 | * @brief Assign (fill) missing node positions. |
| 1344 | * |
| 1345 | * @param[in] set Set to fill positions in. |
| 1346 | * @param[in] root Context root node. |
| 1347 | * @param[in] root_type Context root type. |
| 1348 | * @return LY_ERR |
| 1349 | */ |
| 1350 | static LY_ERR |
| 1351 | set_assign_pos(struct lyxp_set *set, const struct lyd_node *root, enum lyxp_node_type root_type) |
| 1352 | { |
| 1353 | const struct lyd_node *prev = NULL, *tmp_node; |
| 1354 | uint32_t i, tmp_pos = 0; |
| 1355 | |
| 1356 | for (i = 0; i < set->used; ++i) { |
| 1357 | if (!set->val.nodes[i].pos) { |
| 1358 | tmp_node = NULL; |
| 1359 | switch (set->val.nodes[i].type) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1360 | case LYXP_NODE_META: |
| 1361 | tmp_node = set->val.meta[i].meta->parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1362 | if (!tmp_node) { |
| 1363 | LOGINT_RET(root->schema->module->ctx); |
| 1364 | } |
| 1365 | /* fallthrough */ |
| 1366 | case LYXP_NODE_ELEM: |
| 1367 | case LYXP_NODE_TEXT: |
| 1368 | if (!tmp_node) { |
| 1369 | tmp_node = set->val.nodes[i].node; |
| 1370 | } |
| 1371 | set->val.nodes[i].pos = get_node_pos(tmp_node, set->val.nodes[i].type, root, root_type, &prev, &tmp_pos); |
| 1372 | break; |
| 1373 | default: |
| 1374 | /* all roots have position 0 */ |
| 1375 | break; |
| 1376 | } |
| 1377 | } |
| 1378 | } |
| 1379 | |
| 1380 | return LY_SUCCESS; |
| 1381 | } |
| 1382 | |
| 1383 | /** |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1384 | * @brief Get unique @p meta position in the parent metadata. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1385 | * |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1386 | * @param[in] meta Metadata to use. |
| 1387 | * @return Metadata position. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1388 | */ |
| 1389 | static uint16_t |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1390 | get_meta_pos(struct lyd_meta *meta) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1391 | { |
| 1392 | uint16_t pos = 0; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1393 | struct lyd_meta *meta2; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1394 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1395 | for (meta2 = meta->parent->meta; meta2 && (meta2 != meta); meta2 = meta2->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1396 | ++pos; |
| 1397 | } |
| 1398 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1399 | assert(meta2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1400 | return pos; |
| 1401 | } |
| 1402 | |
| 1403 | /** |
| 1404 | * @brief Compare 2 nodes in respect to XPath document order. |
| 1405 | * |
| 1406 | * @param[in] item1 1st node. |
| 1407 | * @param[in] item2 2nd node. |
| 1408 | * @return If 1st > 2nd returns 1, 1st == 2nd returns 0, and 1st < 2nd returns -1. |
| 1409 | */ |
| 1410 | static int |
| 1411 | set_sort_compare(struct lyxp_set_node *item1, struct lyxp_set_node *item2) |
| 1412 | { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1413 | uint32_t meta_pos1 = 0, meta_pos2 = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1414 | |
| 1415 | if (item1->pos < item2->pos) { |
| 1416 | return -1; |
| 1417 | } |
| 1418 | |
| 1419 | if (item1->pos > item2->pos) { |
| 1420 | return 1; |
| 1421 | } |
| 1422 | |
| 1423 | /* node positions are equal, the fun case */ |
| 1424 | |
| 1425 | /* 1st ELEM - == - 2nd TEXT, 1st TEXT - == - 2nd ELEM */ |
| 1426 | /* special case since text nodes are actually saved as their parents */ |
| 1427 | if ((item1->node == item2->node) && (item1->type != item2->type)) { |
| 1428 | if (item1->type == LYXP_NODE_ELEM) { |
| 1429 | assert(item2->type == LYXP_NODE_TEXT); |
| 1430 | return -1; |
| 1431 | } else { |
| 1432 | assert((item1->type == LYXP_NODE_TEXT) && (item2->type == LYXP_NODE_ELEM)); |
| 1433 | return 1; |
| 1434 | } |
| 1435 | } |
| 1436 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1437 | /* we need meta positions now */ |
| 1438 | if (item1->type == LYXP_NODE_META) { |
| 1439 | meta_pos1 = get_meta_pos((struct lyd_meta *)item1->node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1440 | } |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1441 | if (item2->type == LYXP_NODE_META) { |
| 1442 | meta_pos2 = get_meta_pos((struct lyd_meta *)item2->node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1443 | } |
| 1444 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1445 | /* 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] | 1446 | /* check for duplicates */ |
| 1447 | if (item1->node == item2->node) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1448 | 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] | 1449 | return 0; |
| 1450 | } |
| 1451 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1452 | /* 1st ELEM - 2nd TEXT, 1st ELEM - any pos - 2nd META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1453 | /* elem is always first, 2nd node is after it */ |
| 1454 | if (item1->type == LYXP_NODE_ELEM) { |
| 1455 | assert(item2->type != LYXP_NODE_ELEM); |
| 1456 | return -1; |
| 1457 | } |
| 1458 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1459 | /* 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] | 1460 | /* 2nd is before 1st */ |
| 1461 | if (((item1->type == LYXP_NODE_TEXT) |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1462 | && ((item2->type == LYXP_NODE_ELEM) || (item2->type == LYXP_NODE_META))) |
| 1463 | || ((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_ELEM)) |
| 1464 | || (((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_META)) |
| 1465 | && (meta_pos1 > meta_pos2))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1466 | return 1; |
| 1467 | } |
| 1468 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1469 | /* 1st META - any pos - 2nd TEXT, 1st META <pos< - 2nd META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1470 | /* 2nd is after 1st */ |
| 1471 | return -1; |
| 1472 | } |
| 1473 | |
| 1474 | /** |
| 1475 | * @brief Set cast for comparisons. |
| 1476 | * |
| 1477 | * @param[in] trg Target set to cast source into. |
| 1478 | * @param[in] src Source set. |
| 1479 | * @param[in] type Target set type. |
| 1480 | * @param[in] src_idx Source set node index. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1481 | * @return LY_ERR |
| 1482 | */ |
| 1483 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1484 | 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] | 1485 | { |
| 1486 | assert(src->type == LYXP_SET_NODE_SET); |
| 1487 | |
| 1488 | set_init(trg, src); |
| 1489 | |
| 1490 | /* insert node into target set */ |
| 1491 | set_insert_node(trg, src->val.nodes[src_idx].node, src->val.nodes[src_idx].pos, src->val.nodes[src_idx].type, 0); |
| 1492 | |
| 1493 | /* cast target set appropriately */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1494 | return lyxp_set_cast(trg, type); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1495 | } |
| 1496 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1497 | /** |
| 1498 | * @brief Set content canonization for comparisons. |
| 1499 | * |
| 1500 | * @param[in] trg Target set to put the canononized source into. |
| 1501 | * @param[in] src Source set. |
| 1502 | * @param[in] xp_node Source XPath node/meta to use for canonization. |
| 1503 | * @return LY_ERR |
| 1504 | */ |
| 1505 | static LY_ERR |
| 1506 | set_comp_canonize(struct lyxp_set *trg, const struct lyxp_set *src, const struct lyxp_set_node *xp_node) |
| 1507 | { |
| 1508 | struct lysc_type *type = NULL; |
| 1509 | struct lyd_value val; |
| 1510 | struct ly_err_item *err = NULL; |
| 1511 | char *str, *ptr; |
| 1512 | int dynamic; |
| 1513 | LY_ERR rc; |
| 1514 | |
| 1515 | /* is there anything to canonize even? */ |
| 1516 | if ((src->type == LYXP_SET_NUMBER) || (src->type == LYXP_SET_STRING)) { |
| 1517 | /* do we have a type to use for canonization? */ |
| 1518 | if ((xp_node->type == LYXP_NODE_ELEM) && (xp_node->node->schema->nodetype & LYD_NODE_TERM)) { |
| 1519 | type = ((struct lyd_node_term *)xp_node->node)->value.realtype; |
| 1520 | } else if (xp_node->type == LYXP_NODE_META) { |
| 1521 | type = ((struct lyd_meta *)xp_node->node)->value.realtype; |
| 1522 | } |
| 1523 | } |
| 1524 | if (!type) { |
| 1525 | goto fill; |
| 1526 | } |
| 1527 | |
| 1528 | if (src->type == LYXP_SET_NUMBER) { |
| 1529 | /* canonize number */ |
| 1530 | if (asprintf(&str, "%Lf", src->val.num) == -1) { |
| 1531 | LOGMEM(src->ctx); |
| 1532 | return LY_EMEM; |
| 1533 | } |
| 1534 | } else { |
| 1535 | /* canonize string */ |
| 1536 | str = strdup(src->val.str); |
| 1537 | } |
| 1538 | |
| 1539 | /* ignore errors, the value may not satisfy schema constraints */ |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame^] | 1540 | rc = type->plugin->store(src->ctx, type, str, strlen(str), LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_DYNAMIC, |
| 1541 | LY_PREF_JSON, NULL, NULL, NULL, &val, &err); |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1542 | ly_err_free(err); |
| 1543 | if (rc) { |
| 1544 | /* invalid value */ |
| 1545 | free(str); |
| 1546 | goto fill; |
| 1547 | } |
| 1548 | |
| 1549 | /* storing successful, now print the canonical value */ |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 1550 | str = (char *)type->plugin->print(&val, LY_PREF_JSON, NULL, &dynamic); |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1551 | |
| 1552 | /* use the canonized value */ |
| 1553 | set_init(trg, src); |
| 1554 | trg->type = src->type; |
| 1555 | if (src->type == LYXP_SET_NUMBER) { |
| 1556 | trg->val.num = strtold(str, &ptr); |
| 1557 | if (dynamic) { |
| 1558 | free(str); |
| 1559 | } |
| 1560 | LY_CHECK_ERR_RET(ptr[0], LOGINT(src->ctx), LY_EINT); |
| 1561 | } else { |
| 1562 | trg->val.str = (dynamic ? str : strdup(str)); |
| 1563 | } |
| 1564 | type->plugin->free(src->ctx, &val); |
| 1565 | return LY_SUCCESS; |
| 1566 | |
| 1567 | fill: |
| 1568 | /* no canonization needed/possible */ |
| 1569 | set_fill_set(trg, src); |
| 1570 | return LY_SUCCESS; |
| 1571 | } |
| 1572 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1573 | #ifndef NDEBUG |
| 1574 | |
| 1575 | /** |
| 1576 | * @brief Bubble sort @p set into XPath document order. |
| 1577 | * Context position aware. Unused in the 'Release' build target. |
| 1578 | * |
| 1579 | * @param[in] set Set to sort. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1580 | * @return How many times the whole set was traversed - 1 (if set was sorted, returns 0). |
| 1581 | */ |
| 1582 | static int |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1583 | set_sort(struct lyxp_set *set) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1584 | { |
| 1585 | uint32_t i, j; |
| 1586 | int ret = 0, cmp, inverted, change; |
| 1587 | const struct lyd_node *root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1588 | struct lyxp_set_node item; |
| 1589 | struct lyxp_set_hash_node hnode; |
| 1590 | uint64_t hash; |
| 1591 | |
Michal Vasko | 3cf8fbf | 2020-05-27 15:21:21 +0200 | [diff] [blame] | 1592 | if ((set->type != LYXP_SET_NODE_SET) || (set->used < 2)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1593 | return 0; |
| 1594 | } |
| 1595 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1596 | /* find first top-level node to be used as anchor for positions */ |
| 1597 | for (root = set->ctx_node; root->parent; root = (const struct lyd_node *)root->parent); |
| 1598 | for (; root->prev->next; root = root->prev); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1599 | |
| 1600 | /* fill positions */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1601 | if (set_assign_pos(set, root, set->root_type)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1602 | return -1; |
| 1603 | } |
| 1604 | |
| 1605 | LOGDBG(LY_LDGXPATH, "SORT BEGIN"); |
| 1606 | print_set_debug(set); |
| 1607 | |
| 1608 | for (i = 0; i < set->used; ++i) { |
| 1609 | inverted = 0; |
| 1610 | change = 0; |
| 1611 | |
| 1612 | for (j = 1; j < set->used - i; ++j) { |
| 1613 | /* compare node positions */ |
| 1614 | if (inverted) { |
| 1615 | cmp = set_sort_compare(&set->val.nodes[j], &set->val.nodes[j - 1]); |
| 1616 | } else { |
| 1617 | cmp = set_sort_compare(&set->val.nodes[j - 1], &set->val.nodes[j]); |
| 1618 | } |
| 1619 | |
| 1620 | /* swap if needed */ |
| 1621 | if ((inverted && (cmp < 0)) || (!inverted && (cmp > 0))) { |
| 1622 | change = 1; |
| 1623 | |
| 1624 | item = set->val.nodes[j - 1]; |
| 1625 | set->val.nodes[j - 1] = set->val.nodes[j]; |
| 1626 | set->val.nodes[j] = item; |
| 1627 | } else { |
| 1628 | /* whether node_pos1 should be smaller than node_pos2 or the other way around */ |
| 1629 | inverted = !inverted; |
| 1630 | } |
| 1631 | } |
| 1632 | |
| 1633 | ++ret; |
| 1634 | |
| 1635 | if (!change) { |
| 1636 | break; |
| 1637 | } |
| 1638 | } |
| 1639 | |
| 1640 | LOGDBG(LY_LDGXPATH, "SORT END %d", ret); |
| 1641 | print_set_debug(set); |
| 1642 | |
| 1643 | /* check node hashes */ |
| 1644 | if (set->used >= LYD_HT_MIN_ITEMS) { |
| 1645 | assert(set->ht); |
| 1646 | for (i = 0; i < set->used; ++i) { |
| 1647 | hnode.node = set->val.nodes[i].node; |
| 1648 | hnode.type = set->val.nodes[i].type; |
| 1649 | |
| 1650 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 1651 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 1652 | hash = dict_hash_multi(hash, NULL, 0); |
| 1653 | |
| 1654 | assert(!lyht_find(set->ht, &hnode, hash, NULL)); |
| 1655 | } |
| 1656 | } |
| 1657 | |
| 1658 | return ret - 1; |
| 1659 | } |
| 1660 | |
| 1661 | /** |
| 1662 | * @brief Remove duplicate entries in a sorted node set. |
| 1663 | * |
| 1664 | * @param[in] set Sorted set to check. |
| 1665 | * @return LY_ERR (LY_EEXIST if some duplicates are found) |
| 1666 | */ |
| 1667 | static LY_ERR |
| 1668 | set_sorted_dup_node_clean(struct lyxp_set *set) |
| 1669 | { |
| 1670 | uint32_t i = 0; |
| 1671 | LY_ERR ret = LY_SUCCESS; |
| 1672 | |
| 1673 | if (set->used > 1) { |
| 1674 | while (i < set->used - 1) { |
| 1675 | if ((set->val.nodes[i].node == set->val.nodes[i + 1].node) |
| 1676 | && (set->val.nodes[i].type == set->val.nodes[i + 1].type)) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1677 | set_remove_node_none(set, i + 1); |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1678 | ret = LY_EEXIST; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1679 | } |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1680 | ++i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1681 | } |
| 1682 | } |
| 1683 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1684 | set_remove_nodes_none(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1685 | return ret; |
| 1686 | } |
| 1687 | |
| 1688 | #endif |
| 1689 | |
| 1690 | /** |
| 1691 | * @brief Merge 2 sorted sets into one. |
| 1692 | * |
| 1693 | * @param[in,out] trg Set to merge into. Duplicates are removed. |
| 1694 | * @param[in] src Set to be merged into @p trg. It is cast to #LYXP_SET_EMPTY on success. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1695 | * @return LY_ERR |
| 1696 | */ |
| 1697 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1698 | set_sorted_merge(struct lyxp_set *trg, struct lyxp_set *src) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1699 | { |
| 1700 | uint32_t i, j, k, count, dup_count; |
| 1701 | int cmp; |
| 1702 | const struct lyd_node *root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1703 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1704 | if ((trg->type != LYXP_SET_NODE_SET) || (src->type != LYXP_SET_NODE_SET)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1705 | return LY_EINVAL; |
| 1706 | } |
| 1707 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1708 | if (!src->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1709 | return LY_SUCCESS; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1710 | } else if (!trg->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1711 | set_fill_set(trg, src); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1712 | lyxp_set_free_content(src); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1713 | return LY_SUCCESS; |
| 1714 | } |
| 1715 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1716 | /* find first top-level node to be used as anchor for positions */ |
| 1717 | for (root = trg->ctx_node; root->parent; root = (const struct lyd_node *)root->parent); |
| 1718 | for (; root->prev->next; root = root->prev); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1719 | |
| 1720 | /* fill positions */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1721 | if (set_assign_pos(trg, root, trg->root_type) || set_assign_pos(src, root, src->root_type)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1722 | return LY_EINT; |
| 1723 | } |
| 1724 | |
| 1725 | #ifndef NDEBUG |
| 1726 | LOGDBG(LY_LDGXPATH, "MERGE target"); |
| 1727 | print_set_debug(trg); |
| 1728 | LOGDBG(LY_LDGXPATH, "MERGE source"); |
| 1729 | print_set_debug(src); |
| 1730 | #endif |
| 1731 | |
| 1732 | /* make memory for the merge (duplicates are not detected yet, so space |
| 1733 | * will likely be wasted on them, too bad) */ |
| 1734 | if (trg->size - trg->used < src->used) { |
| 1735 | trg->size = trg->used + src->used; |
| 1736 | |
| 1737 | trg->val.nodes = ly_realloc(trg->val.nodes, trg->size * sizeof *trg->val.nodes); |
| 1738 | LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx), LY_EMEM); |
| 1739 | } |
| 1740 | |
| 1741 | i = 0; |
| 1742 | j = 0; |
| 1743 | count = 0; |
| 1744 | dup_count = 0; |
| 1745 | do { |
| 1746 | cmp = set_sort_compare(&src->val.nodes[i], &trg->val.nodes[j]); |
| 1747 | if (!cmp) { |
| 1748 | if (!count) { |
| 1749 | /* duplicate, just skip it */ |
| 1750 | ++i; |
| 1751 | ++j; |
| 1752 | } else { |
| 1753 | /* we are copying something already, so let's copy the duplicate too, |
| 1754 | * we are hoping that afterwards there are some more nodes to |
| 1755 | * copy and this way we can copy them all together */ |
| 1756 | ++count; |
| 1757 | ++dup_count; |
| 1758 | ++i; |
| 1759 | ++j; |
| 1760 | } |
| 1761 | } else if (cmp < 0) { |
| 1762 | /* inserting src node into trg, just remember it for now */ |
| 1763 | ++count; |
| 1764 | ++i; |
| 1765 | |
| 1766 | /* insert the hash now */ |
| 1767 | set_insert_node_hash(trg, src->val.nodes[i - 1].node, src->val.nodes[i - 1].type); |
| 1768 | } else if (count) { |
| 1769 | copy_nodes: |
| 1770 | /* time to actually copy the nodes, we have found the largest block of nodes */ |
| 1771 | memmove(&trg->val.nodes[j + (count - dup_count)], |
| 1772 | &trg->val.nodes[j], |
| 1773 | (trg->used - j) * sizeof *trg->val.nodes); |
| 1774 | memcpy(&trg->val.nodes[j - dup_count], &src->val.nodes[i - count], count * sizeof *src->val.nodes); |
| 1775 | |
| 1776 | trg->used += count - dup_count; |
| 1777 | /* do not change i, except the copying above, we are basically doing exactly what is in the else branch below */ |
| 1778 | j += count - dup_count; |
| 1779 | |
| 1780 | count = 0; |
| 1781 | dup_count = 0; |
| 1782 | } else { |
| 1783 | ++j; |
| 1784 | } |
| 1785 | } while ((i < src->used) && (j < trg->used)); |
| 1786 | |
| 1787 | if ((i < src->used) || count) { |
| 1788 | /* insert all the hashes first */ |
| 1789 | for (k = i; k < src->used; ++k) { |
| 1790 | set_insert_node_hash(trg, src->val.nodes[k].node, src->val.nodes[k].type); |
| 1791 | } |
| 1792 | |
| 1793 | /* loop ended, but we need to copy something at trg end */ |
| 1794 | count += src->used - i; |
| 1795 | i = src->used; |
| 1796 | goto copy_nodes; |
| 1797 | } |
| 1798 | |
| 1799 | /* we are inserting hashes before the actual node insert, which causes |
| 1800 | * situations when there were initially not enough items for a hash table, |
| 1801 | * but even after some were inserted, hash table was not created (during |
| 1802 | * insertion the number of items is not updated yet) */ |
| 1803 | if (!trg->ht && (trg->used >= LYD_HT_MIN_ITEMS)) { |
| 1804 | set_insert_node_hash(trg, NULL, 0); |
| 1805 | } |
| 1806 | |
| 1807 | #ifndef NDEBUG |
| 1808 | LOGDBG(LY_LDGXPATH, "MERGE result"); |
| 1809 | print_set_debug(trg); |
| 1810 | #endif |
| 1811 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1812 | lyxp_set_free_content(src); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1813 | return LY_SUCCESS; |
| 1814 | } |
| 1815 | |
| 1816 | /* |
| 1817 | * (re)parse functions |
| 1818 | * |
| 1819 | * Parse functions parse the expression into |
| 1820 | * tokens (syntactic analysis). |
| 1821 | * |
| 1822 | * Reparse functions perform semantic analysis |
| 1823 | * (do not save the result, just a check) of |
| 1824 | * the expression and fill repeat indices. |
| 1825 | */ |
| 1826 | |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1827 | LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1828 | lyxp_check_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t tok_idx, enum lyxp_token want_tok) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1829 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1830 | if (exp->used == tok_idx) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1831 | if (ctx) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1832 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF); |
| 1833 | } |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1834 | return LY_EINCOMPLETE; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1835 | } |
| 1836 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1837 | if (want_tok && (exp->tokens[tok_idx] != want_tok)) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1838 | if (ctx) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1839 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1840 | lyxp_print_token(exp->tokens[tok_idx]), &exp->expr[exp->tok_pos[tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1841 | } |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1842 | return LY_ENOT; |
| 1843 | } |
| 1844 | |
| 1845 | return LY_SUCCESS; |
| 1846 | } |
| 1847 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1848 | LY_ERR |
| 1849 | lyxp_next_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok) |
| 1850 | { |
| 1851 | LY_CHECK_RET(lyxp_check_token(ctx, exp, *tok_idx, want_tok)); |
| 1852 | |
| 1853 | /* skip the token */ |
| 1854 | ++(*tok_idx); |
| 1855 | |
| 1856 | return LY_SUCCESS; |
| 1857 | } |
| 1858 | |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1859 | /* just like lyxp_check_token() but tests for 2 tokens */ |
| 1860 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1861 | exp_check_token2(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t tok_idx, enum lyxp_token want_tok1, |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1862 | enum lyxp_token want_tok2) |
| 1863 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1864 | if (exp->used == tok_idx) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1865 | if (ctx) { |
| 1866 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF); |
| 1867 | } |
| 1868 | return LY_EINCOMPLETE; |
| 1869 | } |
| 1870 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1871 | if ((exp->tokens[tok_idx] != want_tok1) && (exp->tokens[tok_idx] != want_tok2)) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1872 | if (ctx) { |
| 1873 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1874 | lyxp_print_token(exp->tokens[tok_idx]), &exp->expr[exp->tok_pos[tok_idx]]); |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1875 | } |
| 1876 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1877 | } |
| 1878 | |
| 1879 | return LY_SUCCESS; |
| 1880 | } |
| 1881 | |
| 1882 | /** |
| 1883 | * @brief Stack operation push on the repeat array. |
| 1884 | * |
| 1885 | * @param[in] exp Expression to use. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1886 | * @param[in] tok_idx Position in the expresion \p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1887 | * @param[in] repeat_op_idx Index from \p exp of the operator token. This value is pushed. |
| 1888 | */ |
| 1889 | static void |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1890 | exp_repeat_push(struct lyxp_expr *exp, uint16_t tok_idx, uint16_t repeat_op_idx) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1891 | { |
| 1892 | uint16_t i; |
| 1893 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1894 | if (exp->repeat[tok_idx]) { |
| 1895 | for (i = 0; exp->repeat[tok_idx][i]; ++i); |
| 1896 | exp->repeat[tok_idx] = realloc(exp->repeat[tok_idx], (i + 2) * sizeof *exp->repeat[tok_idx]); |
| 1897 | LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), ); |
| 1898 | exp->repeat[tok_idx][i] = repeat_op_idx; |
| 1899 | exp->repeat[tok_idx][i + 1] = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1900 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1901 | exp->repeat[tok_idx] = calloc(2, sizeof *exp->repeat[tok_idx]); |
| 1902 | LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), ); |
| 1903 | exp->repeat[tok_idx][0] = repeat_op_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1904 | } |
| 1905 | } |
| 1906 | |
| 1907 | /** |
| 1908 | * @brief Reparse Predicate. Logs directly on error. |
| 1909 | * |
| 1910 | * [7] Predicate ::= '[' Expr ']' |
| 1911 | * |
| 1912 | * @param[in] ctx Context for logging. |
| 1913 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1914 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1915 | * @return LY_ERR |
| 1916 | */ |
| 1917 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1918 | reparse_predicate(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1919 | { |
| 1920 | LY_ERR rc; |
| 1921 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1922 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1923 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1924 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1925 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1926 | rc = reparse_or_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1927 | LY_CHECK_RET(rc); |
| 1928 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1929 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1930 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1931 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1932 | |
| 1933 | return LY_SUCCESS; |
| 1934 | } |
| 1935 | |
| 1936 | /** |
| 1937 | * @brief Reparse RelativeLocationPath. Logs directly on error. |
| 1938 | * |
| 1939 | * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step |
| 1940 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 1941 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 1942 | * |
| 1943 | * @param[in] ctx Context for logging. |
| 1944 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1945 | * @param[in] tok_idx Position in the expression \p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1946 | * @return LY_ERR (LY_EINCOMPLETE on forward reference) |
| 1947 | */ |
| 1948 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1949 | reparse_relative_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1950 | { |
| 1951 | LY_ERR rc; |
| 1952 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1953 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1954 | LY_CHECK_RET(rc); |
| 1955 | |
| 1956 | goto step; |
| 1957 | do { |
| 1958 | /* '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1959 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1960 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1961 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1962 | LY_CHECK_RET(rc); |
| 1963 | step: |
| 1964 | /* Step */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1965 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1966 | case LYXP_TOKEN_DOT: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1967 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1968 | break; |
| 1969 | |
| 1970 | case LYXP_TOKEN_DDOT: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1971 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1972 | break; |
| 1973 | |
| 1974 | case LYXP_TOKEN_AT: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1975 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1976 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1977 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1978 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1979 | if ((exp->tokens[*tok_idx] != LYXP_TOKEN_NAMETEST) && (exp->tokens[*tok_idx] != LYXP_TOKEN_NODETYPE)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1980 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1981 | lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1982 | return LY_EVALID; |
| 1983 | } |
| 1984 | /* fall through */ |
| 1985 | case LYXP_TOKEN_NAMETEST: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1986 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1987 | goto reparse_predicate; |
| 1988 | break; |
| 1989 | |
| 1990 | case LYXP_TOKEN_NODETYPE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1991 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1992 | |
| 1993 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1994 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1995 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1996 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1997 | |
| 1998 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1999 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2000 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2001 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2002 | |
| 2003 | reparse_predicate: |
| 2004 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2005 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 2006 | rc = reparse_predicate(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2007 | LY_CHECK_RET(rc); |
| 2008 | } |
| 2009 | break; |
| 2010 | default: |
| 2011 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2012 | lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2013 | return LY_EVALID; |
| 2014 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2015 | } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2016 | |
| 2017 | return LY_SUCCESS; |
| 2018 | } |
| 2019 | |
| 2020 | /** |
| 2021 | * @brief Reparse AbsoluteLocationPath. Logs directly on error. |
| 2022 | * |
| 2023 | * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath |
| 2024 | * |
| 2025 | * @param[in] ctx Context for logging. |
| 2026 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2027 | * @param[in] tok_idx Position in the expression \p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2028 | * @return LY_ERR |
| 2029 | */ |
| 2030 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2031 | reparse_absolute_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2032 | { |
| 2033 | LY_ERR rc; |
| 2034 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2035 | LY_CHECK_RET(exp_check_token2(ctx, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2036 | |
| 2037 | /* '/' RelativeLocationPath? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2038 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2039 | /* '/' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2040 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2041 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2042 | if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2043 | return LY_SUCCESS; |
| 2044 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2045 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2046 | case LYXP_TOKEN_DOT: |
| 2047 | case LYXP_TOKEN_DDOT: |
| 2048 | case LYXP_TOKEN_AT: |
| 2049 | case LYXP_TOKEN_NAMETEST: |
| 2050 | case LYXP_TOKEN_NODETYPE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2051 | rc = reparse_relative_location_path(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2052 | LY_CHECK_RET(rc); |
| 2053 | /* fall through */ |
| 2054 | default: |
| 2055 | break; |
| 2056 | } |
| 2057 | |
| 2058 | /* '//' RelativeLocationPath */ |
| 2059 | } else { |
| 2060 | /* '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2061 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2062 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2063 | rc = reparse_relative_location_path(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2064 | LY_CHECK_RET(rc); |
| 2065 | } |
| 2066 | |
| 2067 | return LY_SUCCESS; |
| 2068 | } |
| 2069 | |
| 2070 | /** |
| 2071 | * @brief Reparse FunctionCall. Logs directly on error. |
| 2072 | * |
| 2073 | * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')' |
| 2074 | * |
| 2075 | * @param[in] ctx Context for logging. |
| 2076 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2077 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2078 | * @return LY_ERR |
| 2079 | */ |
| 2080 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2081 | 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] | 2082 | { |
| 2083 | int min_arg_count = -1, max_arg_count, arg_count; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2084 | uint16_t func_tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2085 | LY_ERR rc; |
| 2086 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2087 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2088 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2089 | func_tok_idx = *tok_idx; |
| 2090 | switch (exp->tok_len[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2091 | case 3: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2092 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2093 | min_arg_count = 1; |
| 2094 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2095 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2096 | min_arg_count = 1; |
| 2097 | max_arg_count = 1; |
| 2098 | } |
| 2099 | break; |
| 2100 | case 4: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2101 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2102 | min_arg_count = 1; |
| 2103 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2104 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2105 | min_arg_count = 0; |
| 2106 | max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2107 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2108 | min_arg_count = 0; |
| 2109 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2110 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2111 | min_arg_count = 0; |
| 2112 | max_arg_count = 0; |
| 2113 | } |
| 2114 | break; |
| 2115 | case 5: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2116 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2117 | min_arg_count = 1; |
| 2118 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2119 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2120 | min_arg_count = 0; |
| 2121 | max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2122 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2123 | min_arg_count = 1; |
| 2124 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2125 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2126 | min_arg_count = 1; |
| 2127 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2128 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2129 | min_arg_count = 1; |
| 2130 | max_arg_count = 1; |
| 2131 | } |
| 2132 | break; |
| 2133 | case 6: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2134 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2135 | min_arg_count = 2; |
Michal Vasko | be2e356 | 2019-10-15 15:35:35 +0200 | [diff] [blame] | 2136 | max_arg_count = INT_MAX; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2137 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2138 | min_arg_count = 0; |
| 2139 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2140 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2141 | min_arg_count = 0; |
| 2142 | max_arg_count = 1; |
| 2143 | } |
| 2144 | break; |
| 2145 | case 7: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2146 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2147 | min_arg_count = 1; |
| 2148 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2149 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2150 | min_arg_count = 1; |
| 2151 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2152 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2153 | min_arg_count = 0; |
| 2154 | max_arg_count = 0; |
| 2155 | } |
| 2156 | break; |
| 2157 | case 8: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2158 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2159 | min_arg_count = 2; |
| 2160 | max_arg_count = 2; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2161 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2162 | min_arg_count = 0; |
| 2163 | max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2164 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2165 | min_arg_count = 2; |
| 2166 | max_arg_count = 2; |
| 2167 | } |
| 2168 | break; |
| 2169 | case 9: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2170 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2171 | min_arg_count = 2; |
| 2172 | max_arg_count = 3; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2173 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2174 | min_arg_count = 3; |
| 2175 | max_arg_count = 3; |
| 2176 | } |
| 2177 | break; |
| 2178 | case 10: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2179 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2180 | min_arg_count = 0; |
| 2181 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2182 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2183 | min_arg_count = 1; |
| 2184 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2185 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2186 | min_arg_count = 2; |
| 2187 | max_arg_count = 2; |
| 2188 | } |
| 2189 | break; |
| 2190 | case 11: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2191 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2192 | min_arg_count = 2; |
| 2193 | max_arg_count = 2; |
| 2194 | } |
| 2195 | break; |
| 2196 | case 12: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2197 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2198 | min_arg_count = 2; |
| 2199 | max_arg_count = 2; |
| 2200 | } |
| 2201 | break; |
| 2202 | case 13: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2203 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2204 | min_arg_count = 0; |
| 2205 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2206 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2207 | min_arg_count = 0; |
| 2208 | max_arg_count = 1; |
| 2209 | } |
| 2210 | break; |
| 2211 | case 15: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2212 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2213 | min_arg_count = 0; |
| 2214 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2215 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2216 | min_arg_count = 2; |
| 2217 | max_arg_count = 2; |
| 2218 | } |
| 2219 | break; |
| 2220 | case 16: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2221 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2222 | min_arg_count = 2; |
| 2223 | max_arg_count = 2; |
| 2224 | } |
| 2225 | break; |
| 2226 | case 20: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2227 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2228 | min_arg_count = 2; |
| 2229 | max_arg_count = 2; |
| 2230 | } |
| 2231 | break; |
| 2232 | } |
| 2233 | if (min_arg_count == -1) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2234 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INFUNC, exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2235 | return LY_EINVAL; |
| 2236 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2237 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2238 | |
| 2239 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2240 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2241 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2242 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2243 | |
| 2244 | /* ( Expr ( ',' Expr )* )? */ |
| 2245 | arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2246 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2247 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2248 | if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2249 | ++arg_count; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2250 | rc = reparse_or_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2251 | LY_CHECK_RET(rc); |
| 2252 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2253 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) { |
| 2254 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2255 | |
| 2256 | ++arg_count; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2257 | rc = reparse_or_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2258 | LY_CHECK_RET(rc); |
| 2259 | } |
| 2260 | |
| 2261 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2262 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2263 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2264 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2265 | |
| 2266 | if ((arg_count < min_arg_count) || (arg_count > max_arg_count)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2267 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INARGCOUNT, arg_count, exp->tok_len[func_tok_idx], |
| 2268 | &exp->expr[exp->tok_pos[func_tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2269 | return LY_EVALID; |
| 2270 | } |
| 2271 | |
| 2272 | return LY_SUCCESS; |
| 2273 | } |
| 2274 | |
| 2275 | /** |
| 2276 | * @brief Reparse PathExpr. Logs directly on error. |
| 2277 | * |
| 2278 | * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate* |
| 2279 | * | PrimaryExpr Predicate* '/' RelativeLocationPath |
| 2280 | * | PrimaryExpr Predicate* '//' RelativeLocationPath |
| 2281 | * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath |
| 2282 | * [8] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall |
| 2283 | * |
| 2284 | * @param[in] ctx Context for logging. |
| 2285 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2286 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2287 | * @return LY_ERR |
| 2288 | */ |
| 2289 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2290 | reparse_path_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2291 | { |
| 2292 | LY_ERR rc; |
| 2293 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2294 | if (lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 2295 | return LY_EVALID; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2296 | } |
| 2297 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2298 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2299 | case LYXP_TOKEN_PAR1: |
| 2300 | /* '(' Expr ')' Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2301 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2302 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2303 | rc = reparse_or_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2304 | LY_CHECK_RET(rc); |
| 2305 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2306 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2307 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2308 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2309 | goto predicate; |
| 2310 | break; |
| 2311 | case LYXP_TOKEN_DOT: |
| 2312 | case LYXP_TOKEN_DDOT: |
| 2313 | case LYXP_TOKEN_AT: |
| 2314 | case LYXP_TOKEN_NAMETEST: |
| 2315 | case LYXP_TOKEN_NODETYPE: |
| 2316 | /* RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2317 | rc = reparse_relative_location_path(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2318 | LY_CHECK_RET(rc); |
| 2319 | break; |
| 2320 | case LYXP_TOKEN_FUNCNAME: |
| 2321 | /* FunctionCall */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2322 | rc = reparse_function_call(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2323 | LY_CHECK_RET(rc); |
| 2324 | goto predicate; |
| 2325 | break; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2326 | case LYXP_TOKEN_OPER_PATH: |
| 2327 | case LYXP_TOKEN_OPER_RPATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2328 | /* AbsoluteLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2329 | rc = reparse_absolute_location_path(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2330 | LY_CHECK_RET(rc); |
| 2331 | break; |
| 2332 | case LYXP_TOKEN_LITERAL: |
| 2333 | /* Literal */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2334 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2335 | goto predicate; |
| 2336 | break; |
| 2337 | case LYXP_TOKEN_NUMBER: |
| 2338 | /* Number */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2339 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2340 | goto predicate; |
| 2341 | break; |
| 2342 | default: |
| 2343 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2344 | lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2345 | return LY_EVALID; |
| 2346 | } |
| 2347 | |
| 2348 | return LY_SUCCESS; |
| 2349 | |
| 2350 | predicate: |
| 2351 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2352 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 2353 | rc = reparse_predicate(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2354 | LY_CHECK_RET(rc); |
| 2355 | } |
| 2356 | |
| 2357 | /* ('/' or '//') RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2358 | if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2359 | |
| 2360 | /* '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2361 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2362 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2363 | rc = reparse_relative_location_path(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2364 | LY_CHECK_RET(rc); |
| 2365 | } |
| 2366 | |
| 2367 | return LY_SUCCESS; |
| 2368 | } |
| 2369 | |
| 2370 | /** |
| 2371 | * @brief Reparse UnaryExpr. Logs directly on error. |
| 2372 | * |
| 2373 | * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr |
| 2374 | * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr |
| 2375 | * |
| 2376 | * @param[in] ctx Context for logging. |
| 2377 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2378 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2379 | * @return LY_ERR |
| 2380 | */ |
| 2381 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2382 | reparse_unary_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2383 | { |
| 2384 | uint16_t prev_exp; |
| 2385 | LY_ERR rc; |
| 2386 | |
| 2387 | /* ('-')* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2388 | prev_exp = *tok_idx; |
| 2389 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) |
| 2390 | && (exp->expr[exp->tok_pos[*tok_idx]] == '-')) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2391 | exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2392 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2393 | } |
| 2394 | |
| 2395 | /* PathExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2396 | prev_exp = *tok_idx; |
| 2397 | rc = reparse_path_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2398 | LY_CHECK_RET(rc); |
| 2399 | |
| 2400 | /* ('|' PathExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2401 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_UNI)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2402 | exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2403 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2404 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2405 | rc = reparse_path_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2406 | LY_CHECK_RET(rc); |
| 2407 | } |
| 2408 | |
| 2409 | return LY_SUCCESS; |
| 2410 | } |
| 2411 | |
| 2412 | /** |
| 2413 | * @brief Reparse AdditiveExpr. Logs directly on error. |
| 2414 | * |
| 2415 | * [15] AdditiveExpr ::= MultiplicativeExpr |
| 2416 | * | AdditiveExpr '+' MultiplicativeExpr |
| 2417 | * | AdditiveExpr '-' MultiplicativeExpr |
| 2418 | * [16] MultiplicativeExpr ::= UnaryExpr |
| 2419 | * | MultiplicativeExpr '*' UnaryExpr |
| 2420 | * | MultiplicativeExpr 'div' UnaryExpr |
| 2421 | * | MultiplicativeExpr 'mod' UnaryExpr |
| 2422 | * |
| 2423 | * @param[in] ctx Context for logging. |
| 2424 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2425 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2426 | * @return LY_ERR |
| 2427 | */ |
| 2428 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2429 | reparse_additive_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2430 | { |
| 2431 | uint16_t prev_add_exp, prev_mul_exp; |
| 2432 | LY_ERR rc; |
| 2433 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2434 | prev_add_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2435 | goto reparse_multiplicative_expr; |
| 2436 | |
| 2437 | /* ('+' / '-' MultiplicativeExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2438 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) |
| 2439 | && ((exp->expr[exp->tok_pos[*tok_idx]] == '+') || (exp->expr[exp->tok_pos[*tok_idx]] == '-'))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2440 | exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2441 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2442 | |
| 2443 | reparse_multiplicative_expr: |
| 2444 | /* UnaryExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2445 | prev_mul_exp = *tok_idx; |
| 2446 | rc = reparse_unary_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2447 | LY_CHECK_RET(rc); |
| 2448 | |
| 2449 | /* ('*' / 'div' / 'mod' UnaryExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2450 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) |
| 2451 | && ((exp->expr[exp->tok_pos[*tok_idx]] == '*') || (exp->tok_len[*tok_idx] == 3))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2452 | exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2453 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2454 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2455 | rc = reparse_unary_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2456 | LY_CHECK_RET(rc); |
| 2457 | } |
| 2458 | } |
| 2459 | |
| 2460 | return LY_SUCCESS; |
| 2461 | } |
| 2462 | |
| 2463 | /** |
| 2464 | * @brief Reparse EqualityExpr. Logs directly on error. |
| 2465 | * |
| 2466 | * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr |
| 2467 | * | EqualityExpr '!=' RelationalExpr |
| 2468 | * [14] RelationalExpr ::= AdditiveExpr |
| 2469 | * | RelationalExpr '<' AdditiveExpr |
| 2470 | * | RelationalExpr '>' AdditiveExpr |
| 2471 | * | RelationalExpr '<=' AdditiveExpr |
| 2472 | * | RelationalExpr '>=' AdditiveExpr |
| 2473 | * |
| 2474 | * @param[in] ctx Context for logging. |
| 2475 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2476 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2477 | * @return LY_ERR |
| 2478 | */ |
| 2479 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2480 | reparse_equality_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2481 | { |
| 2482 | uint16_t prev_eq_exp, prev_rel_exp; |
| 2483 | LY_ERR rc; |
| 2484 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2485 | prev_eq_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2486 | goto reparse_additive_expr; |
| 2487 | |
| 2488 | /* ('=' / '!=' RelationalExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2489 | while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_EQUAL, LYXP_TOKEN_OPER_NEQUAL)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2490 | exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2491 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2492 | |
| 2493 | reparse_additive_expr: |
| 2494 | /* AdditiveExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2495 | prev_rel_exp = *tok_idx; |
| 2496 | rc = reparse_additive_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2497 | LY_CHECK_RET(rc); |
| 2498 | |
| 2499 | /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2500 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_COMP)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2501 | exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2502 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2503 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2504 | rc = reparse_additive_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2505 | LY_CHECK_RET(rc); |
| 2506 | } |
| 2507 | } |
| 2508 | |
| 2509 | return LY_SUCCESS; |
| 2510 | } |
| 2511 | |
| 2512 | /** |
| 2513 | * @brief Reparse OrExpr. Logs directly on error. |
| 2514 | * |
| 2515 | * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr |
| 2516 | * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr |
| 2517 | * |
| 2518 | * @param[in] ctx Context for logging. |
| 2519 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2520 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2521 | * @return LY_ERR |
| 2522 | */ |
| 2523 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2524 | reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2525 | { |
| 2526 | uint16_t prev_or_exp, prev_and_exp; |
| 2527 | LY_ERR rc; |
| 2528 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2529 | prev_or_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2530 | goto reparse_equality_expr; |
| 2531 | |
| 2532 | /* ('or' AndExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2533 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_LOG) && (exp->tok_len[*tok_idx] == 2)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2534 | exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2535 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2536 | |
| 2537 | reparse_equality_expr: |
| 2538 | /* EqualityExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2539 | prev_and_exp = *tok_idx; |
| 2540 | rc = reparse_equality_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2541 | LY_CHECK_RET(rc); |
| 2542 | |
| 2543 | /* ('and' EqualityExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2544 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_LOG) && (exp->tok_len[*tok_idx] == 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2545 | exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2546 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2547 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2548 | rc = reparse_equality_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2549 | LY_CHECK_RET(rc); |
| 2550 | } |
| 2551 | } |
| 2552 | |
| 2553 | return LY_SUCCESS; |
| 2554 | } |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2555 | |
| 2556 | /** |
| 2557 | * @brief Parse NCName. |
| 2558 | * |
| 2559 | * @param[in] ncname Name to parse. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2560 | * @return Length of @p ncname valid bytes. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2561 | */ |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2562 | static long int |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2563 | parse_ncname(const char *ncname) |
| 2564 | { |
| 2565 | unsigned int uc; |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2566 | size_t size; |
| 2567 | long int len = 0; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2568 | |
| 2569 | LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0); |
| 2570 | if (!is_xmlqnamestartchar(uc) || (uc == ':')) { |
| 2571 | return len; |
| 2572 | } |
| 2573 | |
| 2574 | do { |
| 2575 | len += size; |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 2576 | if (!*ncname) { |
| 2577 | break; |
| 2578 | } |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2579 | LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2580 | } while (is_xmlqnamechar(uc) && (uc != ':')); |
| 2581 | |
| 2582 | return len; |
| 2583 | } |
| 2584 | |
| 2585 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2586 | * @brief Add @p token into the expression @p exp. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2587 | * |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2588 | * @param[in] ctx Context for logging. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2589 | * @param[in] exp Expression to use. |
| 2590 | * @param[in] token Token to add. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2591 | * @param[in] tok_pos Token position in the XPath expression. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2592 | * @param[in] tok_len Token length in the XPath expression. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2593 | * @return LY_ERR |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2594 | */ |
| 2595 | static LY_ERR |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 2596 | exp_add_token(const struct ly_ctx *ctx, struct lyxp_expr *exp, enum lyxp_token token, uint16_t tok_pos, uint16_t tok_len) |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2597 | { |
| 2598 | uint32_t prev; |
| 2599 | |
| 2600 | if (exp->used == exp->size) { |
| 2601 | prev = exp->size; |
| 2602 | exp->size += LYXP_EXPR_SIZE_STEP; |
| 2603 | if (prev > exp->size) { |
| 2604 | LOGINT(ctx); |
| 2605 | return LY_EINT; |
| 2606 | } |
| 2607 | |
| 2608 | exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens); |
| 2609 | LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM); |
| 2610 | exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos); |
| 2611 | LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM); |
| 2612 | exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len); |
| 2613 | LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM); |
| 2614 | } |
| 2615 | |
| 2616 | exp->tokens[exp->used] = token; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2617 | exp->tok_pos[exp->used] = tok_pos; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2618 | exp->tok_len[exp->used] = tok_len; |
| 2619 | ++exp->used; |
| 2620 | return LY_SUCCESS; |
| 2621 | } |
| 2622 | |
| 2623 | void |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 2624 | lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr) |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2625 | { |
| 2626 | uint16_t i; |
| 2627 | |
| 2628 | if (!expr) { |
| 2629 | return; |
| 2630 | } |
| 2631 | |
| 2632 | lydict_remove(ctx, expr->expr); |
| 2633 | free(expr->tokens); |
| 2634 | free(expr->tok_pos); |
| 2635 | free(expr->tok_len); |
| 2636 | if (expr->repeat) { |
| 2637 | for (i = 0; i < expr->used; ++i) { |
| 2638 | free(expr->repeat[i]); |
| 2639 | } |
| 2640 | } |
| 2641 | free(expr->repeat); |
| 2642 | free(expr); |
| 2643 | } |
| 2644 | |
| 2645 | struct lyxp_expr * |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2646 | 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] | 2647 | { |
| 2648 | struct lyxp_expr *ret; |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2649 | size_t parsed = 0, tok_len; |
| 2650 | long int ncname_len; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2651 | enum lyxp_token tok_type; |
| 2652 | int prev_function_check = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2653 | uint16_t tok_idx = 0; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2654 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2655 | if (!expr[0]) { |
| 2656 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF); |
| 2657 | return NULL; |
| 2658 | } |
| 2659 | |
| 2660 | if (!expr_len) { |
| 2661 | expr_len = strlen(expr); |
| 2662 | } |
| 2663 | if (expr_len > UINT16_MAX) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2664 | LOGERR(ctx, LY_EINVAL, "XPath expression cannot be longer than %ud characters.", UINT16_MAX); |
| 2665 | return NULL; |
| 2666 | } |
| 2667 | |
| 2668 | /* init lyxp_expr structure */ |
| 2669 | ret = calloc(1, sizeof *ret); |
| 2670 | LY_CHECK_ERR_GOTO(!ret, LOGMEM(ctx), error); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2671 | ret->expr = lydict_insert(ctx, expr, expr_len); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2672 | LY_CHECK_ERR_GOTO(!ret->expr, LOGMEM(ctx), error); |
| 2673 | ret->used = 0; |
| 2674 | ret->size = LYXP_EXPR_SIZE_START; |
| 2675 | ret->tokens = malloc(ret->size * sizeof *ret->tokens); |
| 2676 | LY_CHECK_ERR_GOTO(!ret->tokens, LOGMEM(ctx), error); |
| 2677 | |
| 2678 | ret->tok_pos = malloc(ret->size * sizeof *ret->tok_pos); |
| 2679 | LY_CHECK_ERR_GOTO(!ret->tok_pos, LOGMEM(ctx), error); |
| 2680 | |
| 2681 | ret->tok_len = malloc(ret->size * sizeof *ret->tok_len); |
| 2682 | LY_CHECK_ERR_GOTO(!ret->tok_len, LOGMEM(ctx), error); |
| 2683 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2684 | /* make expr 0-terminated */ |
| 2685 | expr = ret->expr; |
| 2686 | |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2687 | while (is_xmlws(expr[parsed])) { |
| 2688 | ++parsed; |
| 2689 | } |
| 2690 | |
| 2691 | do { |
| 2692 | if (expr[parsed] == '(') { |
| 2693 | |
| 2694 | /* '(' */ |
| 2695 | tok_len = 1; |
| 2696 | tok_type = LYXP_TOKEN_PAR1; |
| 2697 | |
| 2698 | if (prev_function_check && ret->used && (ret->tokens[ret->used - 1] == LYXP_TOKEN_NAMETEST)) { |
| 2699 | /* it is a NodeType/FunctionName after all */ |
| 2700 | if (((ret->tok_len[ret->used - 1] == 4) |
| 2701 | && (!strncmp(&expr[ret->tok_pos[ret->used - 1]], "node", 4) |
| 2702 | || !strncmp(&expr[ret->tok_pos[ret->used - 1]], "text", 4))) || |
| 2703 | ((ret->tok_len[ret->used - 1] == 7) |
| 2704 | && !strncmp(&expr[ret->tok_pos[ret->used - 1]], "comment", 7))) { |
| 2705 | ret->tokens[ret->used - 1] = LYXP_TOKEN_NODETYPE; |
| 2706 | } else { |
| 2707 | ret->tokens[ret->used - 1] = LYXP_TOKEN_FUNCNAME; |
| 2708 | } |
| 2709 | prev_function_check = 0; |
| 2710 | } |
| 2711 | |
| 2712 | } else if (expr[parsed] == ')') { |
| 2713 | |
| 2714 | /* ')' */ |
| 2715 | tok_len = 1; |
| 2716 | tok_type = LYXP_TOKEN_PAR2; |
| 2717 | |
| 2718 | } else if (expr[parsed] == '[') { |
| 2719 | |
| 2720 | /* '[' */ |
| 2721 | tok_len = 1; |
| 2722 | tok_type = LYXP_TOKEN_BRACK1; |
| 2723 | |
| 2724 | } else if (expr[parsed] == ']') { |
| 2725 | |
| 2726 | /* ']' */ |
| 2727 | tok_len = 1; |
| 2728 | tok_type = LYXP_TOKEN_BRACK2; |
| 2729 | |
| 2730 | } else if (!strncmp(&expr[parsed], "..", 2)) { |
| 2731 | |
| 2732 | /* '..' */ |
| 2733 | tok_len = 2; |
| 2734 | tok_type = LYXP_TOKEN_DDOT; |
| 2735 | |
| 2736 | } else if ((expr[parsed] == '.') && (!isdigit(expr[parsed + 1]))) { |
| 2737 | |
| 2738 | /* '.' */ |
| 2739 | tok_len = 1; |
| 2740 | tok_type = LYXP_TOKEN_DOT; |
| 2741 | |
| 2742 | } else if (expr[parsed] == '@') { |
| 2743 | |
| 2744 | /* '@' */ |
| 2745 | tok_len = 1; |
| 2746 | tok_type = LYXP_TOKEN_AT; |
| 2747 | |
| 2748 | } else if (expr[parsed] == ',') { |
| 2749 | |
| 2750 | /* ',' */ |
| 2751 | tok_len = 1; |
| 2752 | tok_type = LYXP_TOKEN_COMMA; |
| 2753 | |
| 2754 | } else if (expr[parsed] == '\'') { |
| 2755 | |
| 2756 | /* Literal with ' */ |
| 2757 | for (tok_len = 1; (expr[parsed + tok_len] != '\0') && (expr[parsed + tok_len] != '\''); ++tok_len); |
| 2758 | LY_CHECK_ERR_GOTO(expr[parsed + tok_len] == '\0', |
| 2759 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOE, expr[parsed], &expr[parsed]), error); |
| 2760 | ++tok_len; |
| 2761 | tok_type = LYXP_TOKEN_LITERAL; |
| 2762 | |
| 2763 | } else if (expr[parsed] == '\"') { |
| 2764 | |
| 2765 | /* Literal with " */ |
| 2766 | for (tok_len = 1; (expr[parsed + tok_len] != '\0') && (expr[parsed + tok_len] != '\"'); ++tok_len); |
| 2767 | LY_CHECK_ERR_GOTO(expr[parsed + tok_len] == '\0', |
| 2768 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOE, expr[parsed], &expr[parsed]), error); |
| 2769 | ++tok_len; |
| 2770 | tok_type = LYXP_TOKEN_LITERAL; |
| 2771 | |
| 2772 | } else if ((expr[parsed] == '.') || (isdigit(expr[parsed]))) { |
| 2773 | |
| 2774 | /* Number */ |
| 2775 | for (tok_len = 0; isdigit(expr[parsed + tok_len]); ++tok_len); |
| 2776 | if (expr[parsed + tok_len] == '.') { |
| 2777 | ++tok_len; |
| 2778 | for (; isdigit(expr[parsed + tok_len]); ++tok_len); |
| 2779 | } |
| 2780 | tok_type = LYXP_TOKEN_NUMBER; |
| 2781 | |
| 2782 | } else if (expr[parsed] == '/') { |
| 2783 | |
| 2784 | /* Operator '/', '//' */ |
| 2785 | if (!strncmp(&expr[parsed], "//", 2)) { |
| 2786 | tok_len = 2; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2787 | tok_type = LYXP_TOKEN_OPER_RPATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2788 | } else { |
| 2789 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2790 | tok_type = LYXP_TOKEN_OPER_PATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2791 | } |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2792 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2793 | } else if (!strncmp(&expr[parsed], "!=", 2)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2794 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2795 | /* Operator '!=' */ |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2796 | tok_len = 2; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2797 | tok_type = LYXP_TOKEN_OPER_NEQUAL; |
| 2798 | |
| 2799 | } else if (!strncmp(&expr[parsed], "<=", 2) || !strncmp(&expr[parsed], ">=", 2)) { |
| 2800 | |
| 2801 | /* Operator '<=', '>=' */ |
| 2802 | tok_len = 2; |
| 2803 | tok_type = LYXP_TOKEN_OPER_COMP; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2804 | |
| 2805 | } else if (expr[parsed] == '|') { |
| 2806 | |
| 2807 | /* Operator '|' */ |
| 2808 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2809 | tok_type = LYXP_TOKEN_OPER_UNI; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2810 | |
| 2811 | } else if ((expr[parsed] == '+') || (expr[parsed] == '-')) { |
| 2812 | |
| 2813 | /* Operator '+', '-' */ |
| 2814 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2815 | tok_type = LYXP_TOKEN_OPER_MATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2816 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2817 | } else if (expr[parsed] == '=') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2818 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2819 | /* Operator '=' */ |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2820 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2821 | tok_type = LYXP_TOKEN_OPER_EQUAL; |
| 2822 | |
| 2823 | } else if ((expr[parsed] == '<') || (expr[parsed] == '>')) { |
| 2824 | |
| 2825 | /* Operator '<', '>' */ |
| 2826 | tok_len = 1; |
| 2827 | tok_type = LYXP_TOKEN_OPER_COMP; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2828 | |
| 2829 | } else if (ret->used && (ret->tokens[ret->used - 1] != LYXP_TOKEN_AT) |
| 2830 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_PAR1) |
| 2831 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_BRACK1) |
| 2832 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_COMMA) |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2833 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_LOG) |
| 2834 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_EQUAL) |
| 2835 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_NEQUAL) |
| 2836 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_COMP) |
| 2837 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_MATH) |
| 2838 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_UNI) |
| 2839 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_PATH) |
| 2840 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_RPATH)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2841 | |
| 2842 | /* Operator '*', 'or', 'and', 'mod', or 'div' */ |
| 2843 | if (expr[parsed] == '*') { |
| 2844 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2845 | tok_type = LYXP_TOKEN_OPER_MATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2846 | |
| 2847 | } else if (!strncmp(&expr[parsed], "or", 2)) { |
| 2848 | tok_len = 2; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2849 | tok_type = LYXP_TOKEN_OPER_LOG; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2850 | |
| 2851 | } else if (!strncmp(&expr[parsed], "and", 3)) { |
| 2852 | tok_len = 3; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2853 | tok_type = LYXP_TOKEN_OPER_LOG; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2854 | |
| 2855 | } else if (!strncmp(&expr[parsed], "mod", 3) || !strncmp(&expr[parsed], "div", 3)) { |
| 2856 | tok_len = 3; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2857 | tok_type = LYXP_TOKEN_OPER_MATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2858 | |
| 2859 | } else if (prev_function_check) { |
Michal Vasko | 5307857 | 2019-05-24 08:50:15 +0200 | [diff] [blame] | 2860 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Invalid character 0x%x ('%c'), perhaps \"%.*s\" is supposed to be a function call.", |
| 2861 | 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] | 2862 | goto error; |
| 2863 | } else { |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2864 | 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] | 2865 | goto error; |
| 2866 | } |
| 2867 | } else if (expr[parsed] == '*') { |
| 2868 | |
| 2869 | /* NameTest '*' */ |
| 2870 | tok_len = 1; |
| 2871 | tok_type = LYXP_TOKEN_NAMETEST; |
| 2872 | |
| 2873 | } else { |
| 2874 | |
| 2875 | /* NameTest (NCName ':' '*' | QName) or NodeType/FunctionName */ |
| 2876 | ncname_len = parse_ncname(&expr[parsed]); |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2877 | 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] | 2878 | tok_len = ncname_len; |
| 2879 | |
| 2880 | if (expr[parsed + tok_len] == ':') { |
| 2881 | ++tok_len; |
| 2882 | if (expr[parsed + tok_len] == '*') { |
| 2883 | ++tok_len; |
| 2884 | } else { |
| 2885 | ncname_len = parse_ncname(&expr[parsed + tok_len]); |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2886 | 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] | 2887 | tok_len += ncname_len; |
| 2888 | } |
| 2889 | /* remove old flag to prevent ambiguities */ |
| 2890 | prev_function_check = 0; |
| 2891 | tok_type = LYXP_TOKEN_NAMETEST; |
| 2892 | } else { |
| 2893 | /* there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */ |
| 2894 | prev_function_check = 1; |
| 2895 | tok_type = LYXP_TOKEN_NAMETEST; |
| 2896 | } |
| 2897 | } |
| 2898 | |
| 2899 | /* store the token, move on to the next one */ |
| 2900 | LY_CHECK_GOTO(exp_add_token(ctx, ret, tok_type, parsed, tok_len), error); |
| 2901 | parsed += tok_len; |
| 2902 | while (is_xmlws(expr[parsed])) { |
| 2903 | ++parsed; |
| 2904 | } |
| 2905 | |
| 2906 | } while (expr[parsed]); |
| 2907 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2908 | if (reparse) { |
| 2909 | /* prealloc repeat */ |
| 2910 | ret->repeat = calloc(ret->size, sizeof *ret->repeat); |
| 2911 | LY_CHECK_ERR_GOTO(!ret->repeat, LOGMEM(ctx), error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2912 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2913 | /* fill repeat */ |
| 2914 | LY_CHECK_GOTO(reparse_or_expr(ctx, ret, &tok_idx), error); |
| 2915 | if (ret->used > tok_idx) { |
| 2916 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Unparsed characters \"%s\" left at the end of an XPath expression.", |
| 2917 | &ret->expr[ret->tok_pos[tok_idx]]); |
| 2918 | goto error; |
| 2919 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2920 | } |
| 2921 | |
| 2922 | print_expr_struct_debug(ret); |
| 2923 | |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2924 | return ret; |
| 2925 | |
| 2926 | error: |
| 2927 | lyxp_expr_free(ctx, ret); |
| 2928 | return NULL; |
| 2929 | } |
| 2930 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2931 | struct lyxp_expr * |
| 2932 | lyxp_expr_dup(const struct ly_ctx *ctx, const struct lyxp_expr *exp) |
| 2933 | { |
| 2934 | struct lyxp_expr *dup; |
| 2935 | uint32_t i, j; |
| 2936 | |
| 2937 | dup = calloc(1, sizeof *dup); |
| 2938 | LY_CHECK_ERR_GOTO(!dup, LOGMEM(ctx), error); |
| 2939 | |
| 2940 | dup->tokens = malloc(exp->used * sizeof *dup->tokens); |
| 2941 | LY_CHECK_ERR_GOTO(!dup->tokens, LOGMEM(ctx), error); |
| 2942 | memcpy(dup->tokens, exp->tokens, exp->used * sizeof *dup->tokens); |
| 2943 | |
| 2944 | dup->tok_pos = malloc(exp->used * sizeof *dup->tok_pos); |
| 2945 | LY_CHECK_ERR_GOTO(!dup->tok_pos, LOGMEM(ctx), error); |
| 2946 | memcpy(dup->tok_pos, exp->tok_pos, exp->used * sizeof *dup->tok_pos); |
| 2947 | |
| 2948 | dup->tok_len = malloc(exp->used * sizeof *dup->tok_len); |
| 2949 | LY_CHECK_ERR_GOTO(!dup->tok_len, LOGMEM(ctx), error); |
| 2950 | memcpy(dup->tok_len, exp->tok_len, exp->used * sizeof *dup->tok_len); |
| 2951 | |
| 2952 | dup->repeat = malloc(exp->used * sizeof *dup->repeat); |
| 2953 | LY_CHECK_ERR_GOTO(!dup->repeat, LOGMEM(ctx), error); |
| 2954 | for (i = 0; i < exp->used; ++i) { |
| 2955 | if (!exp->repeat[i]) { |
| 2956 | dup->repeat[i] = NULL; |
| 2957 | } else { |
| 2958 | for (j = 0; exp->repeat[i][j]; ++j); |
| 2959 | /* the ending 0 as well */ |
| 2960 | ++j; |
| 2961 | |
Michal Vasko | 99c7164 | 2020-07-03 13:33:36 +0200 | [diff] [blame] | 2962 | dup->repeat[i] = malloc(j * sizeof **dup->repeat); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2963 | LY_CHECK_ERR_GOTO(!dup->repeat[i], LOGMEM(ctx), error); |
| 2964 | memcpy(dup->repeat[i], exp->repeat[i], j * sizeof **dup->repeat); |
| 2965 | dup->repeat[i][j - 1] = 0; |
| 2966 | } |
| 2967 | } |
| 2968 | |
| 2969 | dup->used = exp->used; |
| 2970 | dup->size = exp->used; |
| 2971 | dup->expr = lydict_insert(ctx, exp->expr, 0); |
| 2972 | |
| 2973 | return dup; |
| 2974 | |
| 2975 | error: |
| 2976 | lyxp_expr_free(ctx, dup); |
| 2977 | return NULL; |
| 2978 | } |
| 2979 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2980 | /* |
| 2981 | * warn functions |
| 2982 | * |
| 2983 | * Warn functions check specific reasonable conditions for schema XPath |
| 2984 | * and print a warning if they are not satisfied. |
| 2985 | */ |
| 2986 | |
| 2987 | /** |
| 2988 | * @brief Get the last-added schema node that is currently in the context. |
| 2989 | * |
| 2990 | * @param[in] set Set to search in. |
| 2991 | * @return Last-added schema context node, NULL if no node is in context. |
| 2992 | */ |
| 2993 | static struct lysc_node * |
| 2994 | warn_get_scnode_in_ctx(struct lyxp_set *set) |
| 2995 | { |
| 2996 | uint32_t i; |
| 2997 | |
| 2998 | if (!set || (set->type != LYXP_SET_SCNODE_SET)) { |
| 2999 | return NULL; |
| 3000 | } |
| 3001 | |
| 3002 | i = set->used; |
| 3003 | do { |
| 3004 | --i; |
| 3005 | if (set->val.scnodes[i].in_ctx == 1) { |
| 3006 | /* if there are more, simply return the first found (last added) */ |
| 3007 | return set->val.scnodes[i].scnode; |
| 3008 | } |
| 3009 | } while (i); |
| 3010 | |
| 3011 | return NULL; |
| 3012 | } |
| 3013 | |
| 3014 | /** |
| 3015 | * @brief Test whether a type is numeric - integer type or decimal64. |
| 3016 | * |
| 3017 | * @param[in] type Type to test. |
| 3018 | * @return 1 if numeric, 0 otherwise. |
| 3019 | */ |
| 3020 | static int |
| 3021 | warn_is_numeric_type(struct lysc_type *type) |
| 3022 | { |
| 3023 | struct lysc_type_union *uni; |
| 3024 | int ret; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3025 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3026 | |
| 3027 | switch (type->basetype) { |
| 3028 | case LY_TYPE_DEC64: |
| 3029 | case LY_TYPE_INT8: |
| 3030 | case LY_TYPE_UINT8: |
| 3031 | case LY_TYPE_INT16: |
| 3032 | case LY_TYPE_UINT16: |
| 3033 | case LY_TYPE_INT32: |
| 3034 | case LY_TYPE_UINT32: |
| 3035 | case LY_TYPE_INT64: |
| 3036 | case LY_TYPE_UINT64: |
| 3037 | return 1; |
| 3038 | case LY_TYPE_UNION: |
| 3039 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3040 | LY_ARRAY_FOR(uni->types, u) { |
| 3041 | ret = warn_is_numeric_type(uni->types[u]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3042 | if (ret) { |
| 3043 | /* found a suitable type */ |
| 3044 | return 1; |
| 3045 | } |
| 3046 | } |
| 3047 | /* did not find any suitable type */ |
| 3048 | return 0; |
| 3049 | case LY_TYPE_LEAFREF: |
| 3050 | return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype); |
| 3051 | default: |
| 3052 | return 0; |
| 3053 | } |
| 3054 | } |
| 3055 | |
| 3056 | /** |
| 3057 | * @brief Test whether a type is string-like - no integers, decimal64 or binary. |
| 3058 | * |
| 3059 | * @param[in] type Type to test. |
| 3060 | * @return 1 if string, 0 otherwise. |
| 3061 | */ |
| 3062 | static int |
| 3063 | warn_is_string_type(struct lysc_type *type) |
| 3064 | { |
| 3065 | struct lysc_type_union *uni; |
| 3066 | int ret; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3067 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3068 | |
| 3069 | switch (type->basetype) { |
| 3070 | case LY_TYPE_BITS: |
| 3071 | case LY_TYPE_ENUM: |
| 3072 | case LY_TYPE_IDENT: |
| 3073 | case LY_TYPE_INST: |
| 3074 | case LY_TYPE_STRING: |
| 3075 | return 1; |
| 3076 | case LY_TYPE_UNION: |
| 3077 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3078 | LY_ARRAY_FOR(uni->types, u) { |
| 3079 | ret = warn_is_string_type(uni->types[u]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3080 | if (ret) { |
| 3081 | /* found a suitable type */ |
| 3082 | return 1; |
| 3083 | } |
| 3084 | } |
| 3085 | /* did not find any suitable type */ |
| 3086 | return 0; |
| 3087 | case LY_TYPE_LEAFREF: |
| 3088 | return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype); |
| 3089 | default: |
| 3090 | return 0; |
| 3091 | } |
| 3092 | } |
| 3093 | |
| 3094 | /** |
| 3095 | * @brief Test whether a type is one specific type. |
| 3096 | * |
| 3097 | * @param[in] type Type to test. |
| 3098 | * @param[in] base Expected type. |
| 3099 | * @return 1 if it is, 0 otherwise. |
| 3100 | */ |
| 3101 | static int |
| 3102 | warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base) |
| 3103 | { |
| 3104 | struct lysc_type_union *uni; |
| 3105 | int ret; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3106 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3107 | |
| 3108 | if (type->basetype == base) { |
| 3109 | return 1; |
| 3110 | } else if (type->basetype == LY_TYPE_UNION) { |
| 3111 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3112 | LY_ARRAY_FOR(uni->types, u) { |
| 3113 | ret = warn_is_specific_type(uni->types[u], base); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3114 | if (ret) { |
| 3115 | /* found a suitable type */ |
| 3116 | return 1; |
| 3117 | } |
| 3118 | } |
| 3119 | /* did not find any suitable type */ |
| 3120 | return 0; |
| 3121 | } else if (type->basetype == LY_TYPE_LEAFREF) { |
| 3122 | return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base); |
| 3123 | } |
| 3124 | |
| 3125 | return 0; |
| 3126 | } |
| 3127 | |
| 3128 | /** |
| 3129 | * @brief Get next type of a (union) type. |
| 3130 | * |
| 3131 | * @param[in] type Base type. |
| 3132 | * @param[in] prev_type Previously returned type. |
| 3133 | * @return Next type or NULL. |
| 3134 | */ |
| 3135 | static struct lysc_type * |
| 3136 | warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type) |
| 3137 | { |
| 3138 | struct lysc_type_union *uni; |
| 3139 | int found = 0; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3140 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3141 | |
| 3142 | switch (type->basetype) { |
| 3143 | case LY_TYPE_UNION: |
| 3144 | uni = (struct lysc_type_union *)type; |
| 3145 | if (!prev_type) { |
| 3146 | return uni->types[0]; |
| 3147 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3148 | LY_ARRAY_FOR(uni->types, u) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3149 | if (found) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3150 | return uni->types[u]; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3151 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3152 | if (prev_type == uni->types[u]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3153 | found = 1; |
| 3154 | } |
| 3155 | } |
| 3156 | return NULL; |
| 3157 | default: |
| 3158 | if (prev_type) { |
| 3159 | assert(type == prev_type); |
| 3160 | return NULL; |
| 3161 | } else { |
| 3162 | return type; |
| 3163 | } |
| 3164 | } |
| 3165 | } |
| 3166 | |
| 3167 | /** |
| 3168 | * @brief Test whether 2 types have a common type. |
| 3169 | * |
| 3170 | * @param[in] type1 First type. |
| 3171 | * @param[in] type2 Second type. |
| 3172 | * @return 1 if they do, 0 otherwise. |
| 3173 | */ |
| 3174 | static int |
| 3175 | warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2) |
| 3176 | { |
| 3177 | struct lysc_type *t1, *rt1, *t2, *rt2; |
| 3178 | |
| 3179 | t1 = NULL; |
| 3180 | while ((t1 = warn_is_equal_type_next_type(type1, t1))) { |
| 3181 | if (t1->basetype == LY_TYPE_LEAFREF) { |
| 3182 | rt1 = ((struct lysc_type_leafref *)t1)->realtype; |
| 3183 | } else { |
| 3184 | rt1 = t1; |
| 3185 | } |
| 3186 | |
| 3187 | t2 = NULL; |
| 3188 | while ((t2 = warn_is_equal_type_next_type(type2, t2))) { |
| 3189 | if (t2->basetype == LY_TYPE_LEAFREF) { |
| 3190 | rt2 = ((struct lysc_type_leafref *)t2)->realtype; |
| 3191 | } else { |
| 3192 | rt2 = t2; |
| 3193 | } |
| 3194 | |
| 3195 | if (rt2->basetype == rt1->basetype) { |
| 3196 | /* match found */ |
| 3197 | return 1; |
| 3198 | } |
| 3199 | } |
| 3200 | } |
| 3201 | |
| 3202 | return 0; |
| 3203 | } |
| 3204 | |
| 3205 | /** |
| 3206 | * @brief Check both operands of comparison operators. |
| 3207 | * |
| 3208 | * @param[in] ctx Context for errors. |
| 3209 | * @param[in] set1 First operand set. |
| 3210 | * @param[in] set2 Second operand set. |
| 3211 | * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!='). |
| 3212 | * @param[in] expr Start of the expression to print with the warning. |
| 3213 | * @param[in] tok_pos Token position. |
| 3214 | */ |
| 3215 | static void |
| 3216 | warn_operands(struct ly_ctx *ctx, struct lyxp_set *set1, struct lyxp_set *set2, int numbers_only, const char *expr, uint16_t tok_pos) |
| 3217 | { |
| 3218 | struct lysc_node_leaf *node1, *node2; |
| 3219 | int leaves = 1, warning = 0; |
| 3220 | |
| 3221 | node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1); |
| 3222 | node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2); |
| 3223 | |
| 3224 | if (!node1 && !node2) { |
| 3225 | /* no node-sets involved, nothing to do */ |
| 3226 | return; |
| 3227 | } |
| 3228 | |
| 3229 | if (node1) { |
| 3230 | if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3231 | LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name); |
| 3232 | warning = 1; |
| 3233 | leaves = 0; |
| 3234 | } else if (numbers_only && !warn_is_numeric_type(node1->type)) { |
| 3235 | LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name); |
| 3236 | warning = 1; |
| 3237 | } |
| 3238 | } |
| 3239 | |
| 3240 | if (node2) { |
| 3241 | if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3242 | LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name); |
| 3243 | warning = 1; |
| 3244 | leaves = 0; |
| 3245 | } else if (numbers_only && !warn_is_numeric_type(node2->type)) { |
| 3246 | LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name); |
| 3247 | warning = 1; |
| 3248 | } |
| 3249 | } |
| 3250 | |
| 3251 | if (node1 && node2 && leaves && !numbers_only) { |
| 3252 | if ((warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type)) |
| 3253 | || (!warn_is_numeric_type(node1->type) && warn_is_numeric_type(node2->type)) |
| 3254 | || (!warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type) |
| 3255 | && !warn_is_equal_type(node1->type, node2->type))) { |
| 3256 | LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name); |
| 3257 | warning = 1; |
| 3258 | } |
| 3259 | } |
| 3260 | |
| 3261 | if (warning) { |
| 3262 | LOGWRN(ctx, "Previous warning generated by XPath subexpression[%u] \"%.20s\".", tok_pos, expr + tok_pos); |
| 3263 | } |
| 3264 | } |
| 3265 | |
| 3266 | /** |
| 3267 | * @brief Check that a value is valid for a leaf. If not applicable, does nothing. |
| 3268 | * |
| 3269 | * @param[in] exp Parsed XPath expression. |
| 3270 | * @param[in] set Set with the leaf/leaf-list. |
| 3271 | * @param[in] val_exp Index of the value (literal/number) in @p exp. |
| 3272 | * @param[in] equal_exp Index of the start of the equality expression in @p exp. |
| 3273 | * @param[in] last_equal_exp Index of the end of the equality expression in @p exp. |
| 3274 | */ |
| 3275 | static void |
| 3276 | warn_equality_value(struct lyxp_expr *exp, struct lyxp_set *set, uint16_t val_exp, uint16_t equal_exp, uint16_t last_equal_exp) |
| 3277 | { |
| 3278 | struct lysc_node *scnode; |
| 3279 | struct lysc_type *type; |
| 3280 | char *value; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame^] | 3281 | struct lyd_value storage; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3282 | LY_ERR rc; |
| 3283 | struct ly_err_item *err = NULL; |
| 3284 | |
| 3285 | if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST)) |
| 3286 | && ((exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) || (exp->tokens[val_exp] == LYXP_TOKEN_NUMBER))) { |
| 3287 | /* check that the node can have the specified value */ |
| 3288 | if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) { |
| 3289 | value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2); |
| 3290 | } else { |
| 3291 | value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]); |
| 3292 | } |
| 3293 | if (!value) { |
| 3294 | LOGMEM(set->ctx); |
| 3295 | return; |
| 3296 | } |
| 3297 | |
| 3298 | if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) { |
| 3299 | LOGWRN(set->ctx, "Identityref \"%s\" comparison with identity \"%s\" without prefix, consider adding" |
| 3300 | " a prefix or best using \"derived-from(-or-self)()\" functions.", scnode->name, value); |
| 3301 | LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp], |
| 3302 | (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp], |
| 3303 | exp->expr + exp->tok_pos[equal_exp]); |
| 3304 | } |
| 3305 | |
| 3306 | type = ((struct lysc_node_leaf *)scnode)->type; |
| 3307 | if (type->basetype != LY_TYPE_IDENT) { |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 3308 | rc = type->plugin->store(set->ctx, type, value, strlen(value), LY_TYPE_OPTS_SCHEMA, LY_PREF_SCHEMA, |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame^] | 3309 | (void *)set->local_mod, NULL, NULL, &storage, &err); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3310 | |
| 3311 | if (err) { |
| 3312 | LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg); |
| 3313 | ly_err_free(err); |
| 3314 | } else if (rc != LY_SUCCESS) { |
| 3315 | LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value); |
| 3316 | } |
| 3317 | if (rc != LY_SUCCESS) { |
| 3318 | LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp], |
| 3319 | (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp], |
| 3320 | exp->expr + exp->tok_pos[equal_exp]); |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame^] | 3321 | } else { |
| 3322 | type->plugin->free(set->ctx, &storage); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3323 | } |
| 3324 | } |
| 3325 | free(value); |
| 3326 | } |
| 3327 | } |
| 3328 | |
| 3329 | /* |
| 3330 | * XPath functions |
| 3331 | */ |
| 3332 | |
| 3333 | /** |
| 3334 | * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN |
| 3335 | * depending on whether the first node bit value from the second argument is set. |
| 3336 | * |
| 3337 | * @param[in] args Array of arguments. |
| 3338 | * @param[in] arg_count Count of elements in @p args. |
| 3339 | * @param[in,out] set Context and result set at the same time. |
| 3340 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3341 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3342 | */ |
| 3343 | static LY_ERR |
| 3344 | xpath_bit_is_set(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3345 | { |
| 3346 | struct lyd_node_term *leaf; |
| 3347 | struct lysc_node_leaf *sleaf; |
| 3348 | struct lysc_type_bits *bits; |
| 3349 | LY_ERR rc = LY_SUCCESS; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3350 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3351 | |
| 3352 | if (options & LYXP_SCNODE_ALL) { |
| 3353 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3354 | 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] | 3355 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3356 | 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] | 3357 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) { |
| 3358 | 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] | 3359 | } |
| 3360 | |
| 3361 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 3362 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3363 | 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] | 3364 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3365 | 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] | 3366 | } |
| 3367 | } |
| 3368 | set_scnode_clear_ctx(set); |
| 3369 | return rc; |
| 3370 | } |
| 3371 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3372 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3373 | 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)"); |
| 3374 | return LY_EVALID; |
| 3375 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3376 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3377 | LY_CHECK_RET(rc); |
| 3378 | |
| 3379 | set_fill_boolean(set, 0); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3380 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3381 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
| 3382 | if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) |
| 3383 | && (((struct lysc_node_leaf *)leaf->schema)->type->basetype == LY_TYPE_BITS)) { |
| 3384 | bits = (struct lysc_type_bits *)((struct lysc_node_leaf *)leaf->schema)->type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3385 | LY_ARRAY_FOR(bits->bits, u) { |
| 3386 | if (!strcmp(bits->bits[u].name, args[1]->val.str)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3387 | set_fill_boolean(set, 1); |
| 3388 | break; |
| 3389 | } |
| 3390 | } |
| 3391 | } |
| 3392 | } |
| 3393 | |
| 3394 | return LY_SUCCESS; |
| 3395 | } |
| 3396 | |
| 3397 | /** |
| 3398 | * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN |
| 3399 | * with the argument converted to boolean. |
| 3400 | * |
| 3401 | * @param[in] args Array of arguments. |
| 3402 | * @param[in] arg_count Count of elements in @p args. |
| 3403 | * @param[in,out] set Context and result set at the same time. |
| 3404 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3405 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3406 | */ |
| 3407 | static LY_ERR |
| 3408 | xpath_boolean(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3409 | { |
| 3410 | LY_ERR rc; |
| 3411 | |
| 3412 | if (options & LYXP_SCNODE_ALL) { |
| 3413 | set_scnode_clear_ctx(set); |
| 3414 | return LY_SUCCESS; |
| 3415 | } |
| 3416 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3417 | rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3418 | LY_CHECK_RET(rc); |
| 3419 | set_fill_set(set, args[0]); |
| 3420 | |
| 3421 | return LY_SUCCESS; |
| 3422 | } |
| 3423 | |
| 3424 | /** |
| 3425 | * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER |
| 3426 | * with the first argument rounded up to the nearest integer. |
| 3427 | * |
| 3428 | * @param[in] args Array of arguments. |
| 3429 | * @param[in] arg_count Count of elements in @p args. |
| 3430 | * @param[in,out] set Context and result set at the same time. |
| 3431 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3432 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3433 | */ |
| 3434 | static LY_ERR |
| 3435 | xpath_ceiling(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3436 | { |
| 3437 | struct lysc_node_leaf *sleaf; |
| 3438 | LY_ERR rc = LY_SUCCESS; |
| 3439 | |
| 3440 | if (options & LYXP_SCNODE_ALL) { |
| 3441 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3442 | 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] | 3443 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3444 | 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] | 3445 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) { |
| 3446 | 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] | 3447 | } |
| 3448 | set_scnode_clear_ctx(set); |
| 3449 | return rc; |
| 3450 | } |
| 3451 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3452 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3453 | LY_CHECK_RET(rc); |
| 3454 | if ((long long)args[0]->val.num != args[0]->val.num) { |
| 3455 | set_fill_number(set, ((long long)args[0]->val.num) + 1); |
| 3456 | } else { |
| 3457 | set_fill_number(set, args[0]->val.num); |
| 3458 | } |
| 3459 | |
| 3460 | return LY_SUCCESS; |
| 3461 | } |
| 3462 | |
| 3463 | /** |
| 3464 | * @brief Execute the XPath concat(string, string, string*) function. |
| 3465 | * Returns LYXP_SET_STRING with the concatenation of all the arguments. |
| 3466 | * |
| 3467 | * @param[in] args Array of arguments. |
| 3468 | * @param[in] arg_count Count of elements in @p args. |
| 3469 | * @param[in,out] set Context and result set at the same time. |
| 3470 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3471 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3472 | */ |
| 3473 | static LY_ERR |
| 3474 | xpath_concat(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options) |
| 3475 | { |
| 3476 | uint16_t i; |
| 3477 | char *str = NULL; |
| 3478 | size_t used = 1; |
| 3479 | LY_ERR rc = LY_SUCCESS; |
| 3480 | struct lysc_node_leaf *sleaf; |
| 3481 | |
| 3482 | if (options & LYXP_SCNODE_ALL) { |
| 3483 | for (i = 0; i < arg_count; ++i) { |
| 3484 | if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) { |
| 3485 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3486 | LOGWRN(set->ctx, "Argument #%u of %s is a %s node \"%s\".", |
| 3487 | i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3488 | } else if (!warn_is_string_type(sleaf->type)) { |
Radek Krejci | 70124c8 | 2020-08-14 22:17:03 +0200 | [diff] [blame] | 3489 | LOGWRN(set->ctx, "Argument #%u of %s is node \"%s\", not of string-type.", i + 1, __func__, sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3490 | } |
| 3491 | } |
| 3492 | } |
| 3493 | set_scnode_clear_ctx(set); |
| 3494 | return rc; |
| 3495 | } |
| 3496 | |
| 3497 | for (i = 0; i < arg_count; ++i) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3498 | rc = lyxp_set_cast(args[i], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3499 | if (rc != LY_SUCCESS) { |
| 3500 | free(str); |
| 3501 | return rc; |
| 3502 | } |
| 3503 | |
| 3504 | str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char)); |
| 3505 | LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM); |
| 3506 | strcpy(str + used - 1, args[i]->val.str); |
| 3507 | used += strlen(args[i]->val.str); |
| 3508 | } |
| 3509 | |
| 3510 | /* free, kind of */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3511 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3512 | set->type = LYXP_SET_STRING; |
| 3513 | set->val.str = str; |
| 3514 | |
| 3515 | return LY_SUCCESS; |
| 3516 | } |
| 3517 | |
| 3518 | /** |
| 3519 | * @brief Execute the XPath contains(string, string) function. |
| 3520 | * Returns LYXP_SET_BOOLEAN whether the second argument can |
| 3521 | * be found in the first or not. |
| 3522 | * |
| 3523 | * @param[in] args Array of arguments. |
| 3524 | * @param[in] arg_count Count of elements in @p args. |
| 3525 | * @param[in,out] set Context and result set at the same time. |
| 3526 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3527 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3528 | */ |
| 3529 | static LY_ERR |
| 3530 | xpath_contains(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3531 | { |
| 3532 | struct lysc_node_leaf *sleaf; |
| 3533 | LY_ERR rc = LY_SUCCESS; |
| 3534 | |
| 3535 | if (options & LYXP_SCNODE_ALL) { |
| 3536 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3537 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3538 | 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] | 3539 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3540 | 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] | 3541 | } |
| 3542 | } |
| 3543 | |
| 3544 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 3545 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3546 | 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] | 3547 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3548 | 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] | 3549 | } |
| 3550 | } |
| 3551 | set_scnode_clear_ctx(set); |
| 3552 | return rc; |
| 3553 | } |
| 3554 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3555 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3556 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3557 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3558 | LY_CHECK_RET(rc); |
| 3559 | |
| 3560 | if (strstr(args[0]->val.str, args[1]->val.str)) { |
| 3561 | set_fill_boolean(set, 1); |
| 3562 | } else { |
| 3563 | set_fill_boolean(set, 0); |
| 3564 | } |
| 3565 | |
| 3566 | return LY_SUCCESS; |
| 3567 | } |
| 3568 | |
| 3569 | /** |
| 3570 | * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER |
| 3571 | * with the size of the node-set from the argument. |
| 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_count(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3581 | { |
| 3582 | struct lysc_node *scnode = NULL; |
| 3583 | LY_ERR rc = LY_SUCCESS; |
| 3584 | |
| 3585 | if (options & LYXP_SCNODE_ALL) { |
| 3586 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(scnode = warn_get_scnode_in_ctx(args[0]))) { |
| 3587 | 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] | 3588 | } |
| 3589 | set_scnode_clear_ctx(set); |
| 3590 | return rc; |
| 3591 | } |
| 3592 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3593 | if (args[0]->type != LYXP_SET_NODE_SET) { |
| 3594 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "count(node-set)"); |
| 3595 | return LY_EVALID; |
| 3596 | } |
| 3597 | |
| 3598 | set_fill_number(set, args[0]->used); |
| 3599 | return LY_SUCCESS; |
| 3600 | } |
| 3601 | |
| 3602 | /** |
| 3603 | * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET |
| 3604 | * with the context with the intial node. |
| 3605 | * |
| 3606 | * @param[in] args Array of arguments. |
| 3607 | * @param[in] arg_count Count of elements in @p args. |
| 3608 | * @param[in,out] set Context and result set at the same time. |
| 3609 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3610 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3611 | */ |
| 3612 | static LY_ERR |
| 3613 | xpath_current(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options) |
| 3614 | { |
| 3615 | if (arg_count || args) { |
| 3616 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGCOUNT, arg_count, "current()"); |
| 3617 | return LY_EVALID; |
| 3618 | } |
| 3619 | |
| 3620 | if (options & LYXP_SCNODE_ALL) { |
| 3621 | set_scnode_clear_ctx(set); |
| 3622 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 3623 | lyxp_set_scnode_insert_node(set, set->ctx_scnode, LYXP_NODE_ELEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3624 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3625 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3626 | |
| 3627 | /* position is filled later */ |
| 3628 | set_insert_node(set, set->ctx_node, 0, LYXP_NODE_ELEM, 0); |
| 3629 | } |
| 3630 | |
| 3631 | return LY_SUCCESS; |
| 3632 | } |
| 3633 | |
| 3634 | /** |
| 3635 | * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either |
| 3636 | * leafref or instance-identifier target node(s). |
| 3637 | * |
| 3638 | * @param[in] args Array of arguments. |
| 3639 | * @param[in] arg_count Count of elements in @p args. |
| 3640 | * @param[in,out] set Context and result set at the same time. |
| 3641 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3642 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3643 | */ |
| 3644 | static LY_ERR |
| 3645 | xpath_deref(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3646 | { |
| 3647 | struct lyd_node_term *leaf; |
Michal Vasko | 42e497c | 2020-01-06 08:38:25 +0100 | [diff] [blame] | 3648 | struct lysc_node_leaf *sleaf = NULL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3649 | struct lysc_type_leafref *lref; |
Michal Vasko | ae9e4cb | 2019-09-25 08:43:05 +0200 | [diff] [blame] | 3650 | const struct lysc_node *target; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3651 | struct ly_path *p; |
| 3652 | struct lyd_node *node; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3653 | char *errmsg = NULL; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 3654 | uint8_t oper; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3655 | LY_ERR rc = LY_SUCCESS; |
| 3656 | |
| 3657 | if (options & LYXP_SCNODE_ALL) { |
| 3658 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3659 | 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] | 3660 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3661 | 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] | 3662 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) && !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) { |
| 3663 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"leafref\" nor \"instance-identifier\".", |
| 3664 | __func__, sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3665 | } |
| 3666 | set_scnode_clear_ctx(set); |
Michal Vasko | 42e497c | 2020-01-06 08:38:25 +0100 | [diff] [blame] | 3667 | if (sleaf && (sleaf->type->basetype == LY_TYPE_LEAFREF)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3668 | lref = (struct lysc_type_leafref *)sleaf->type; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 3669 | 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] | 3670 | |
| 3671 | /* it was already evaluated on schema, it must succeed */ |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 3672 | rc = ly_path_compile(set->ctx, sleaf->module, (struct lysc_node *)sleaf, lref->path, LY_PATH_LREF_TRUE, |
| 3673 | oper, LY_PATH_TARGET_MANY, set->format, lref->path_context, &p); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3674 | assert(!rc); |
| 3675 | |
| 3676 | /* get the target node */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3677 | target = p[LY_ARRAY_COUNT(p) - 1].node; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3678 | ly_path_free(set->ctx, p); |
| 3679 | |
| 3680 | lyxp_set_scnode_insert_node(set, target, LYXP_NODE_ELEM); |
Michal Vasko | ae9e4cb | 2019-09-25 08:43:05 +0200 | [diff] [blame] | 3681 | } |
| 3682 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3683 | return rc; |
| 3684 | } |
| 3685 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3686 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3687 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "deref(node-set)"); |
| 3688 | return LY_EVALID; |
| 3689 | } |
| 3690 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3691 | lyxp_set_free_content(set); |
| 3692 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3693 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
| 3694 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 3695 | if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 3696 | if (sleaf->type->basetype == LY_TYPE_LEAFREF) { |
| 3697 | /* find leafref target */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3698 | if (ly_type_find_leafref((struct lysc_type_leafref *)sleaf->type, (struct lyd_node *)leaf, |
| 3699 | &leaf->value, set->tree, &node, &errmsg)) { |
| 3700 | LOGERR(set->ctx, LY_EVALID, errmsg); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3701 | free(errmsg); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3702 | return LY_EVALID; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3703 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3704 | } else { |
| 3705 | assert(sleaf->type->basetype == LY_TYPE_INST); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3706 | if (ly_path_eval(leaf->value.target, set->tree, &node)) { |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame^] | 3707 | LOGERR(set->ctx, LY_EVALID, "Invalid instance-identifier \"%s\" value - required instance not found.", |
| 3708 | LYD_CANONICAL(leaf)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3709 | return LY_EVALID; |
| 3710 | } |
| 3711 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3712 | |
| 3713 | /* insert it */ |
| 3714 | set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3715 | } |
| 3716 | } |
| 3717 | |
| 3718 | return LY_SUCCESS; |
| 3719 | } |
| 3720 | |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3721 | static LY_ERR |
| 3722 | xpath_derived_(struct lyxp_set **args, struct lyxp_set *set, int options, int self_match, const char *func) |
| 3723 | { |
| 3724 | uint16_t i; |
| 3725 | LY_ARRAY_COUNT_TYPE u; |
| 3726 | struct lyd_node_term *leaf; |
| 3727 | struct lysc_node_leaf *sleaf; |
| 3728 | struct lyd_meta *meta; |
| 3729 | struct lyd_value data = {0}, *val; |
| 3730 | struct ly_err_item *err = NULL; |
| 3731 | LY_ERR rc = LY_SUCCESS; |
| 3732 | int found; |
| 3733 | |
| 3734 | if (options & LYXP_SCNODE_ALL) { |
| 3735 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3736 | LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", func); |
| 3737 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3738 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype), |
| 3739 | sleaf->name); |
| 3740 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) { |
| 3741 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", func, sleaf->name); |
| 3742 | } |
| 3743 | |
| 3744 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 3745 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3746 | LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype), |
| 3747 | sleaf->name); |
| 3748 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3749 | LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", func, sleaf->name); |
| 3750 | } |
| 3751 | } |
| 3752 | set_scnode_clear_ctx(set); |
| 3753 | return rc; |
| 3754 | } |
| 3755 | |
| 3756 | if (args[0]->type != LYXP_SET_NODE_SET) { |
| 3757 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), |
| 3758 | "derived-from(-or-self)(node-set, string)"); |
| 3759 | return LY_EVALID; |
| 3760 | } |
| 3761 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
| 3762 | LY_CHECK_RET(rc); |
| 3763 | |
| 3764 | set_fill_boolean(set, 0); |
| 3765 | found = 0; |
| 3766 | for (i = 0; i < args[0]->used; ++i) { |
| 3767 | if ((args[0]->val.nodes[i].type != LYXP_NODE_ELEM) && (args[0]->val.nodes[i].type != LYXP_NODE_META)) { |
| 3768 | continue; |
| 3769 | } |
| 3770 | |
| 3771 | if (args[0]->val.nodes[i].type == LYXP_NODE_ELEM) { |
| 3772 | leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node; |
| 3773 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 3774 | val = &leaf->value; |
| 3775 | if (!(sleaf->nodetype & LYD_NODE_TERM) || (leaf->value.realtype->basetype != LY_TYPE_IDENT)) { |
| 3776 | /* uninteresting */ |
| 3777 | continue; |
| 3778 | } |
| 3779 | |
| 3780 | /* store args[1] as ident */ |
| 3781 | rc = val->realtype->plugin->store(set->ctx, val->realtype, args[1]->val.str, strlen(args[1]->val.str), |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame^] | 3782 | 0, set->format, (void *)set->local_mod, |
| 3783 | (struct lyd_node *)leaf, set->tree, &data, &err); |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3784 | } else { |
| 3785 | meta = args[0]->val.meta[i].meta; |
| 3786 | val = &meta->value; |
| 3787 | if (val->realtype->basetype != LY_TYPE_IDENT) { |
| 3788 | /* uninteresting */ |
| 3789 | continue; |
| 3790 | } |
| 3791 | |
| 3792 | /* store args[1] as ident */ |
| 3793 | rc = val->realtype->plugin->store(set->ctx, val->realtype, args[1]->val.str, strlen(args[1]->val.str), |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame^] | 3794 | 0, set->format, (void *)meta->annotation->module, |
| 3795 | meta->parent, set->tree, &data, &err); |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3796 | } |
| 3797 | |
| 3798 | if (err) { |
| 3799 | ly_err_print(err); |
| 3800 | ly_err_free(err); |
| 3801 | } |
| 3802 | LY_CHECK_RET(rc); |
| 3803 | |
| 3804 | /* finally check the identity itself */ |
| 3805 | if (self_match && (data.ident == val->ident)) { |
| 3806 | set_fill_boolean(set, 1); |
| 3807 | found = 1; |
| 3808 | } |
| 3809 | if (!found) { |
| 3810 | LY_ARRAY_FOR(data.ident->derived, u) { |
| 3811 | if (data.ident->derived[u] == val->ident) { |
| 3812 | set_fill_boolean(set, 1); |
| 3813 | found = 1; |
| 3814 | break; |
| 3815 | } |
| 3816 | } |
| 3817 | } |
| 3818 | |
| 3819 | /* free temporary value */ |
| 3820 | val->realtype->plugin->free(set->ctx, &data); |
| 3821 | if (found) { |
| 3822 | break; |
| 3823 | } |
| 3824 | } |
| 3825 | |
| 3826 | return LY_SUCCESS; |
| 3827 | } |
| 3828 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3829 | /** |
| 3830 | * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending |
| 3831 | * on whether the first argument nodes contain a node of an identity derived from the second |
| 3832 | * argument identity. |
| 3833 | * |
| 3834 | * @param[in] args Array of arguments. |
| 3835 | * @param[in] arg_count Count of elements in @p args. |
| 3836 | * @param[in,out] set Context and result set at the same time. |
| 3837 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3838 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3839 | */ |
| 3840 | static LY_ERR |
| 3841 | xpath_derived_from(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3842 | { |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3843 | return xpath_derived_(args, set, options, 0, __func__); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3844 | } |
| 3845 | |
| 3846 | /** |
| 3847 | * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending |
| 3848 | * on whether the first argument nodes contain a node of an identity that either is or is derived from |
| 3849 | * the second argument identity. |
| 3850 | * |
| 3851 | * @param[in] args Array of arguments. |
| 3852 | * @param[in] arg_count Count of elements in @p args. |
| 3853 | * @param[in,out] set Context and result set at the same time. |
| 3854 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3855 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3856 | */ |
| 3857 | static LY_ERR |
| 3858 | xpath_derived_from_or_self(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3859 | { |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3860 | return xpath_derived_(args, set, options, 1, __func__); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3861 | } |
| 3862 | |
| 3863 | /** |
| 3864 | * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER |
| 3865 | * with the integer value of the first node's enum value, otherwise NaN. |
| 3866 | * |
| 3867 | * @param[in] args Array of arguments. |
| 3868 | * @param[in] arg_count Count of elements in @p args. |
| 3869 | * @param[in,out] set Context and result set at the same time. |
| 3870 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3871 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3872 | */ |
| 3873 | static LY_ERR |
| 3874 | xpath_enum_value(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3875 | { |
| 3876 | struct lyd_node_term *leaf; |
| 3877 | struct lysc_node_leaf *sleaf; |
| 3878 | LY_ERR rc = LY_SUCCESS; |
| 3879 | |
| 3880 | if (options & LYXP_SCNODE_ALL) { |
| 3881 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3882 | 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] | 3883 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3884 | 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] | 3885 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) { |
| 3886 | 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] | 3887 | } |
| 3888 | set_scnode_clear_ctx(set); |
| 3889 | return rc; |
| 3890 | } |
| 3891 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3892 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3893 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "enum-value(node-set)"); |
| 3894 | return LY_EVALID; |
| 3895 | } |
| 3896 | |
| 3897 | set_fill_number(set, NAN); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3898 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3899 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
| 3900 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 3901 | if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) { |
| 3902 | set_fill_number(set, leaf->value.enum_item->value); |
| 3903 | } |
| 3904 | } |
| 3905 | |
| 3906 | return LY_SUCCESS; |
| 3907 | } |
| 3908 | |
| 3909 | /** |
| 3910 | * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN |
| 3911 | * with false value. |
| 3912 | * |
| 3913 | * @param[in] args Array of arguments. |
| 3914 | * @param[in] arg_count Count of elements in @p args. |
| 3915 | * @param[in,out] set Context and result set at the same time. |
| 3916 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3917 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3918 | */ |
| 3919 | static LY_ERR |
| 3920 | xpath_false(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3921 | { |
| 3922 | if (options & LYXP_SCNODE_ALL) { |
| 3923 | set_scnode_clear_ctx(set); |
| 3924 | return LY_SUCCESS; |
| 3925 | } |
| 3926 | |
| 3927 | set_fill_boolean(set, 0); |
| 3928 | return LY_SUCCESS; |
| 3929 | } |
| 3930 | |
| 3931 | /** |
| 3932 | * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER |
| 3933 | * with the first argument floored (truncated). |
| 3934 | * |
| 3935 | * @param[in] args Array of arguments. |
| 3936 | * @param[in] arg_count Count of elements in @p args. |
| 3937 | * @param[in,out] set Context and result set at the same time. |
| 3938 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3939 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3940 | */ |
| 3941 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3942 | 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] | 3943 | { |
| 3944 | LY_ERR rc; |
| 3945 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3946 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3947 | LY_CHECK_RET(rc); |
| 3948 | if (isfinite(args[0]->val.num)) { |
| 3949 | set_fill_number(set, (long long)args[0]->val.num); |
| 3950 | } |
| 3951 | |
| 3952 | return LY_SUCCESS; |
| 3953 | } |
| 3954 | |
| 3955 | /** |
| 3956 | * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN |
| 3957 | * whether the language of the text matches the one from the argument. |
| 3958 | * |
| 3959 | * @param[in] args Array of arguments. |
| 3960 | * @param[in] arg_count Count of elements in @p args. |
| 3961 | * @param[in,out] set Context and result set at the same time. |
| 3962 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3963 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3964 | */ |
| 3965 | static LY_ERR |
| 3966 | xpath_lang(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3967 | { |
| 3968 | const struct lyd_node *node; |
| 3969 | struct lysc_node_leaf *sleaf; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 3970 | struct lyd_meta *meta = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3971 | const char *val; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame^] | 3972 | int i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3973 | LY_ERR rc = LY_SUCCESS; |
| 3974 | |
| 3975 | if (options & LYXP_SCNODE_ALL) { |
| 3976 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3977 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3978 | 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] | 3979 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3980 | 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] | 3981 | } |
| 3982 | } |
| 3983 | set_scnode_clear_ctx(set); |
| 3984 | return rc; |
| 3985 | } |
| 3986 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3987 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3988 | LY_CHECK_RET(rc); |
| 3989 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3990 | if (set->type != LYXP_SET_NODE_SET) { |
| 3991 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "lang(string)"); |
| 3992 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3993 | } else if (!set->used) { |
| 3994 | set_fill_boolean(set, 0); |
| 3995 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3996 | } |
| 3997 | |
| 3998 | switch (set->val.nodes[0].type) { |
| 3999 | case LYXP_NODE_ELEM: |
| 4000 | case LYXP_NODE_TEXT: |
| 4001 | node = set->val.nodes[0].node; |
| 4002 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4003 | case LYXP_NODE_META: |
| 4004 | node = set->val.meta[0].meta->parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4005 | break; |
| 4006 | default: |
| 4007 | /* nothing to do with roots */ |
| 4008 | set_fill_boolean(set, 0); |
| 4009 | return LY_SUCCESS; |
| 4010 | } |
| 4011 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4012 | /* find lang metadata */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4013 | for (; node; node = (struct lyd_node *)node->parent) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4014 | for (meta = node->meta; meta; meta = meta->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4015 | /* annotations */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4016 | 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] | 4017 | break; |
| 4018 | } |
| 4019 | } |
| 4020 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4021 | if (meta) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4022 | break; |
| 4023 | } |
| 4024 | } |
| 4025 | |
| 4026 | /* compare languages */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4027 | if (!meta) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4028 | set_fill_boolean(set, 0); |
| 4029 | } else { |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame^] | 4030 | val = meta->value.canonical; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4031 | for (i = 0; args[0]->val.str[i]; ++i) { |
| 4032 | if (tolower(args[0]->val.str[i]) != tolower(val[i])) { |
| 4033 | set_fill_boolean(set, 0); |
| 4034 | break; |
| 4035 | } |
| 4036 | } |
| 4037 | if (!args[0]->val.str[i]) { |
| 4038 | if (!val[i] || (val[i] == '-')) { |
| 4039 | set_fill_boolean(set, 1); |
| 4040 | } else { |
| 4041 | set_fill_boolean(set, 0); |
| 4042 | } |
| 4043 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4044 | } |
| 4045 | |
| 4046 | return LY_SUCCESS; |
| 4047 | } |
| 4048 | |
| 4049 | /** |
| 4050 | * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER |
| 4051 | * with the context size. |
| 4052 | * |
| 4053 | * @param[in] args Array of arguments. |
| 4054 | * @param[in] arg_count Count of elements in @p args. |
| 4055 | * @param[in,out] set Context and result set at the same time. |
| 4056 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4057 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4058 | */ |
| 4059 | static LY_ERR |
| 4060 | xpath_last(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 4061 | { |
| 4062 | if (options & LYXP_SCNODE_ALL) { |
| 4063 | set_scnode_clear_ctx(set); |
| 4064 | return LY_SUCCESS; |
| 4065 | } |
| 4066 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4067 | if (set->type != LYXP_SET_NODE_SET) { |
| 4068 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "last()"); |
| 4069 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4070 | } else if (!set->used) { |
| 4071 | set_fill_number(set, 0); |
| 4072 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4073 | } |
| 4074 | |
| 4075 | set_fill_number(set, set->ctx_size); |
| 4076 | return LY_SUCCESS; |
| 4077 | } |
| 4078 | |
| 4079 | /** |
| 4080 | * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING |
| 4081 | * with the node name without namespace from the argument or the context. |
| 4082 | * |
| 4083 | * @param[in] args Array of arguments. |
| 4084 | * @param[in] arg_count Count of elements in @p args. |
| 4085 | * @param[in,out] set Context and result set at the same time. |
| 4086 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4087 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4088 | */ |
| 4089 | static LY_ERR |
| 4090 | xpath_local_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options) |
| 4091 | { |
| 4092 | struct lyxp_set_node *item; |
| 4093 | /* suppress unused variable warning */ |
| 4094 | (void)options; |
| 4095 | |
| 4096 | if (options & LYXP_SCNODE_ALL) { |
| 4097 | set_scnode_clear_ctx(set); |
| 4098 | return LY_SUCCESS; |
| 4099 | } |
| 4100 | |
| 4101 | if (arg_count) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4102 | if (args[0]->type != LYXP_SET_NODE_SET) { |
| 4103 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "local-name(node-set?)"); |
| 4104 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4105 | } else if (!args[0]->used) { |
| 4106 | set_fill_string(set, "", 0); |
| 4107 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4108 | } |
| 4109 | |
| 4110 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4111 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4112 | |
| 4113 | item = &args[0]->val.nodes[0]; |
| 4114 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4115 | if (set->type != LYXP_SET_NODE_SET) { |
| 4116 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "local-name(node-set?)"); |
| 4117 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4118 | } else if (!set->used) { |
| 4119 | set_fill_string(set, "", 0); |
| 4120 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4121 | } |
| 4122 | |
| 4123 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4124 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4125 | |
| 4126 | item = &set->val.nodes[0]; |
| 4127 | } |
| 4128 | |
| 4129 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4130 | case LYXP_NODE_NONE: |
| 4131 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4132 | case LYXP_NODE_ROOT: |
| 4133 | case LYXP_NODE_ROOT_CONFIG: |
| 4134 | case LYXP_NODE_TEXT: |
| 4135 | set_fill_string(set, "", 0); |
| 4136 | break; |
| 4137 | case LYXP_NODE_ELEM: |
| 4138 | set_fill_string(set, item->node->schema->name, strlen(item->node->schema->name)); |
| 4139 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4140 | case LYXP_NODE_META: |
| 4141 | set_fill_string(set, ((struct lyd_meta *)item->node)->name, strlen(((struct lyd_meta *)item->node)->name)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4142 | break; |
| 4143 | } |
| 4144 | |
| 4145 | return LY_SUCCESS; |
| 4146 | } |
| 4147 | |
| 4148 | /** |
| 4149 | * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING |
| 4150 | * with the node name fully qualified (with namespace) from the argument or the context. |
| 4151 | * |
| 4152 | * @param[in] args Array of arguments. |
| 4153 | * @param[in] arg_count Count of elements in @p args. |
| 4154 | * @param[in,out] set Context and result set at the same time. |
| 4155 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4156 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4157 | */ |
| 4158 | static LY_ERR |
| 4159 | xpath_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options) |
| 4160 | { |
| 4161 | struct lyxp_set_node *item; |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 4162 | struct lys_module *mod = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4163 | char *str; |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 4164 | const char *name = NULL; |
| 4165 | int rc = -1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4166 | |
| 4167 | if (options & LYXP_SCNODE_ALL) { |
| 4168 | set_scnode_clear_ctx(set); |
| 4169 | return LY_SUCCESS; |
| 4170 | } |
| 4171 | |
| 4172 | if (arg_count) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4173 | if (args[0]->type != LYXP_SET_NODE_SET) { |
| 4174 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "name(node-set?)"); |
| 4175 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4176 | } else if (!args[0]->used) { |
| 4177 | set_fill_string(set, "", 0); |
| 4178 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4179 | } |
| 4180 | |
| 4181 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4182 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4183 | |
| 4184 | item = &args[0]->val.nodes[0]; |
| 4185 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4186 | if (set->type != LYXP_SET_NODE_SET) { |
| 4187 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "name(node-set?)"); |
| 4188 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4189 | } else if (!set->used) { |
| 4190 | set_fill_string(set, "", 0); |
| 4191 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4192 | } |
| 4193 | |
| 4194 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4195 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4196 | |
| 4197 | item = &set->val.nodes[0]; |
| 4198 | } |
| 4199 | |
| 4200 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4201 | case LYXP_NODE_NONE: |
| 4202 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4203 | case LYXP_NODE_ROOT: |
| 4204 | case LYXP_NODE_ROOT_CONFIG: |
| 4205 | case LYXP_NODE_TEXT: |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 4206 | /* keep NULL */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4207 | break; |
| 4208 | case LYXP_NODE_ELEM: |
| 4209 | mod = item->node->schema->module; |
| 4210 | name = item->node->schema->name; |
| 4211 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4212 | case LYXP_NODE_META: |
| 4213 | mod = ((struct lyd_meta *)item->node)->annotation->module; |
| 4214 | name = ((struct lyd_meta *)item->node)->name; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4215 | break; |
| 4216 | } |
| 4217 | |
| 4218 | if (mod && name) { |
| 4219 | switch (set->format) { |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 4220 | case LY_PREF_SCHEMA: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4221 | rc = asprintf(&str, "%s:%s", lys_prefix_find_module(set->local_mod, mod), name); |
| 4222 | break; |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 4223 | case LY_PREF_JSON: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4224 | rc = asprintf(&str, "%s:%s", mod->name, name); |
| 4225 | break; |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 4226 | case LY_PREF_XML: |
Michal Vasko | 9409ef6 | 2019-09-12 11:47:17 +0200 | [diff] [blame] | 4227 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4228 | } |
| 4229 | LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM); |
| 4230 | set_fill_string(set, str, strlen(str)); |
| 4231 | free(str); |
| 4232 | } else { |
| 4233 | set_fill_string(set, "", 0); |
| 4234 | } |
| 4235 | |
| 4236 | return LY_SUCCESS; |
| 4237 | } |
| 4238 | |
| 4239 | /** |
| 4240 | * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING |
| 4241 | * with the namespace of the node from the argument or the context. |
| 4242 | * |
| 4243 | * @param[in] args Array of arguments. |
| 4244 | * @param[in] arg_count Count of elements in @p args. |
| 4245 | * @param[in,out] set Context and result set at the same time. |
| 4246 | * @param[in] options XPath options. |
| 4247 | * @return LY_ERR (LY_EINVAL for wrong arguments on schema) |
| 4248 | */ |
| 4249 | static LY_ERR |
| 4250 | xpath_namespace_uri(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options) |
| 4251 | { |
| 4252 | struct lyxp_set_node *item; |
| 4253 | struct lys_module *mod; |
| 4254 | /* suppress unused variable warning */ |
| 4255 | (void)options; |
| 4256 | |
| 4257 | if (options & LYXP_SCNODE_ALL) { |
| 4258 | set_scnode_clear_ctx(set); |
| 4259 | return LY_SUCCESS; |
| 4260 | } |
| 4261 | |
| 4262 | if (arg_count) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4263 | if (args[0]->type != LYXP_SET_NODE_SET) { |
| 4264 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), |
| 4265 | "namespace-uri(node-set?)"); |
| 4266 | return LY_EVALID; |
| 4267 | } else if (!args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4268 | set_fill_string(set, "", 0); |
| 4269 | return LY_SUCCESS; |
| 4270 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4271 | |
| 4272 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4273 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4274 | |
| 4275 | item = &args[0]->val.nodes[0]; |
| 4276 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4277 | if (set->type != LYXP_SET_NODE_SET) { |
| 4278 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "namespace-uri(node-set?)"); |
| 4279 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4280 | } else if (!set->used) { |
| 4281 | set_fill_string(set, "", 0); |
| 4282 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4283 | } |
| 4284 | |
| 4285 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4286 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4287 | |
| 4288 | item = &set->val.nodes[0]; |
| 4289 | } |
| 4290 | |
| 4291 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4292 | case LYXP_NODE_NONE: |
| 4293 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4294 | case LYXP_NODE_ROOT: |
| 4295 | case LYXP_NODE_ROOT_CONFIG: |
| 4296 | case LYXP_NODE_TEXT: |
| 4297 | set_fill_string(set, "", 0); |
| 4298 | break; |
| 4299 | case LYXP_NODE_ELEM: |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4300 | case LYXP_NODE_META: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4301 | if (item->type == LYXP_NODE_ELEM) { |
| 4302 | mod = item->node->schema->module; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4303 | } else { /* LYXP_NODE_META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4304 | /* annotations */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4305 | mod = ((struct lyd_meta *)item->node)->annotation->module; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4306 | } |
| 4307 | |
| 4308 | set_fill_string(set, mod->ns, strlen(mod->ns)); |
| 4309 | break; |
| 4310 | } |
| 4311 | |
| 4312 | return LY_SUCCESS; |
| 4313 | } |
| 4314 | |
| 4315 | /** |
| 4316 | * @brief Execute the XPath node() function (node type). Returns LYXP_SET_NODE_SET |
| 4317 | * with only nodes from the context. In practice it either leaves the context |
| 4318 | * as it is or returns an empty node set. |
| 4319 | * |
| 4320 | * @param[in] args Array of arguments. |
| 4321 | * @param[in] arg_count Count of elements in @p args. |
| 4322 | * @param[in,out] set Context and result set at the same time. |
| 4323 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4324 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4325 | */ |
| 4326 | static LY_ERR |
| 4327 | xpath_node(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 4328 | { |
| 4329 | if (options & LYXP_SCNODE_ALL) { |
| 4330 | set_scnode_clear_ctx(set); |
| 4331 | return LY_SUCCESS; |
| 4332 | } |
| 4333 | |
| 4334 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4335 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4336 | } |
| 4337 | return LY_SUCCESS; |
| 4338 | } |
| 4339 | |
| 4340 | /** |
| 4341 | * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING |
| 4342 | * with normalized value (no leading, trailing, double white spaces) of the node |
| 4343 | * from the argument or the context. |
| 4344 | * |
| 4345 | * @param[in] args Array of arguments. |
| 4346 | * @param[in] arg_count Count of elements in @p args. |
| 4347 | * @param[in,out] set Context and result set at the same time. |
| 4348 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4349 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4350 | */ |
| 4351 | static LY_ERR |
| 4352 | xpath_normalize_space(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options) |
| 4353 | { |
| 4354 | uint16_t i, new_used; |
| 4355 | char *new; |
| 4356 | int have_spaces = 0, space_before = 0; |
| 4357 | struct lysc_node_leaf *sleaf; |
| 4358 | LY_ERR rc = LY_SUCCESS; |
| 4359 | |
| 4360 | if (options & LYXP_SCNODE_ALL) { |
| 4361 | if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4362 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4363 | 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] | 4364 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4365 | 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] | 4366 | } |
| 4367 | } |
| 4368 | set_scnode_clear_ctx(set); |
| 4369 | return rc; |
| 4370 | } |
| 4371 | |
| 4372 | if (arg_count) { |
| 4373 | set_fill_set(set, args[0]); |
| 4374 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4375 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4376 | LY_CHECK_RET(rc); |
| 4377 | |
| 4378 | /* is there any normalization necessary? */ |
| 4379 | for (i = 0; set->val.str[i]; ++i) { |
| 4380 | if (is_xmlws(set->val.str[i])) { |
| 4381 | if ((i == 0) || space_before || (!set->val.str[i + 1])) { |
| 4382 | have_spaces = 1; |
| 4383 | break; |
| 4384 | } |
| 4385 | space_before = 1; |
| 4386 | } else { |
| 4387 | space_before = 0; |
| 4388 | } |
| 4389 | } |
| 4390 | |
| 4391 | /* yep, there is */ |
| 4392 | if (have_spaces) { |
| 4393 | /* it's enough, at least one character will go, makes space for ending '\0' */ |
| 4394 | new = malloc(strlen(set->val.str) * sizeof(char)); |
| 4395 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 4396 | new_used = 0; |
| 4397 | |
| 4398 | space_before = 0; |
| 4399 | for (i = 0; set->val.str[i]; ++i) { |
| 4400 | if (is_xmlws(set->val.str[i])) { |
| 4401 | if ((i == 0) || space_before) { |
| 4402 | space_before = 1; |
| 4403 | continue; |
| 4404 | } else { |
| 4405 | space_before = 1; |
| 4406 | } |
| 4407 | } else { |
| 4408 | space_before = 0; |
| 4409 | } |
| 4410 | |
| 4411 | new[new_used] = (space_before ? ' ' : set->val.str[i]); |
| 4412 | ++new_used; |
| 4413 | } |
| 4414 | |
| 4415 | /* at worst there is one trailing space now */ |
| 4416 | if (new_used && is_xmlws(new[new_used - 1])) { |
| 4417 | --new_used; |
| 4418 | } |
| 4419 | |
| 4420 | new = ly_realloc(new, (new_used + 1) * sizeof(char)); |
| 4421 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 4422 | new[new_used] = '\0'; |
| 4423 | |
| 4424 | free(set->val.str); |
| 4425 | set->val.str = new; |
| 4426 | } |
| 4427 | |
| 4428 | return LY_SUCCESS; |
| 4429 | } |
| 4430 | |
| 4431 | /** |
| 4432 | * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN |
| 4433 | * with the argument converted to boolean and logically inverted. |
| 4434 | * |
| 4435 | * @param[in] args Array of arguments. |
| 4436 | * @param[in] arg_count Count of elements in @p args. |
| 4437 | * @param[in,out] set Context and result set at the same time. |
| 4438 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4439 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4440 | */ |
| 4441 | static LY_ERR |
| 4442 | xpath_not(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 4443 | { |
| 4444 | if (options & LYXP_SCNODE_ALL) { |
| 4445 | set_scnode_clear_ctx(set); |
| 4446 | return LY_SUCCESS; |
| 4447 | } |
| 4448 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4449 | lyxp_set_cast(args[0], LYXP_SET_BOOLEAN); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4450 | if (args[0]->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4451 | set_fill_boolean(set, 0); |
| 4452 | } else { |
| 4453 | set_fill_boolean(set, 1); |
| 4454 | } |
| 4455 | |
| 4456 | return LY_SUCCESS; |
| 4457 | } |
| 4458 | |
| 4459 | /** |
| 4460 | * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER |
| 4461 | * with the number representation of either the argument or the context. |
| 4462 | * |
| 4463 | * @param[in] args Array of arguments. |
| 4464 | * @param[in] arg_count Count of elements in @p args. |
| 4465 | * @param[in,out] set Context and result set at the same time. |
| 4466 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4467 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4468 | */ |
| 4469 | static LY_ERR |
| 4470 | xpath_number(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options) |
| 4471 | { |
| 4472 | LY_ERR rc; |
| 4473 | |
| 4474 | if (options & LYXP_SCNODE_ALL) { |
| 4475 | set_scnode_clear_ctx(set); |
| 4476 | return LY_SUCCESS; |
| 4477 | } |
| 4478 | |
| 4479 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4480 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4481 | LY_CHECK_RET(rc); |
| 4482 | set_fill_set(set, args[0]); |
| 4483 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4484 | rc = lyxp_set_cast(set, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4485 | LY_CHECK_RET(rc); |
| 4486 | } |
| 4487 | |
| 4488 | return LY_SUCCESS; |
| 4489 | } |
| 4490 | |
| 4491 | /** |
| 4492 | * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER |
| 4493 | * with the context position. |
| 4494 | * |
| 4495 | * @param[in] args Array of arguments. |
| 4496 | * @param[in] arg_count Count of elements in @p args. |
| 4497 | * @param[in,out] set Context and result set at the same time. |
| 4498 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4499 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4500 | */ |
| 4501 | static LY_ERR |
| 4502 | xpath_position(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 4503 | { |
| 4504 | if (options & LYXP_SCNODE_ALL) { |
| 4505 | set_scnode_clear_ctx(set); |
| 4506 | return LY_SUCCESS; |
| 4507 | } |
| 4508 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4509 | if (set->type != LYXP_SET_NODE_SET) { |
| 4510 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "position()"); |
| 4511 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4512 | } else if (!set->used) { |
| 4513 | set_fill_number(set, 0); |
| 4514 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4515 | } |
| 4516 | |
| 4517 | set_fill_number(set, set->ctx_pos); |
| 4518 | |
| 4519 | /* UNUSED in 'Release' build type */ |
| 4520 | (void)options; |
| 4521 | return LY_SUCCESS; |
| 4522 | } |
| 4523 | |
| 4524 | /** |
| 4525 | * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN |
| 4526 | * depending on whether the second argument regex matches the first argument string. For details refer to |
| 4527 | * YANG 1.1 RFC section 10.2.1. |
| 4528 | * |
| 4529 | * @param[in] args Array of arguments. |
| 4530 | * @param[in] arg_count Count of elements in @p args. |
| 4531 | * @param[in,out] set Context and result set at the same time. |
| 4532 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4533 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4534 | */ |
| 4535 | static LY_ERR |
| 4536 | xpath_re_match(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 4537 | { |
| 4538 | struct lysc_pattern **patterns = NULL, **pattern; |
| 4539 | struct lysc_node_leaf *sleaf; |
| 4540 | char *path; |
| 4541 | LY_ERR rc = LY_SUCCESS; |
| 4542 | struct ly_err_item *err; |
| 4543 | |
| 4544 | if (options & LYXP_SCNODE_ALL) { |
| 4545 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4546 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4547 | 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] | 4548 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4549 | 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] | 4550 | } |
| 4551 | } |
| 4552 | |
| 4553 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4554 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4555 | 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] | 4556 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4557 | 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] | 4558 | } |
| 4559 | } |
| 4560 | set_scnode_clear_ctx(set); |
| 4561 | return rc; |
| 4562 | } |
| 4563 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4564 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4565 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4566 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4567 | LY_CHECK_RET(rc); |
| 4568 | |
| 4569 | LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM); |
| 4570 | *pattern = malloc(sizeof **pattern); |
| 4571 | path = lyd_path(set->ctx_node, LYD_PATH_LOG, NULL, 0); |
| 4572 | rc = lys_compile_type_pattern_check(set->ctx, path, args[1]->val.str, &(*pattern)->code); |
| 4573 | free(path); |
| 4574 | if (rc != LY_SUCCESS) { |
| 4575 | LY_ARRAY_FREE(patterns); |
| 4576 | return rc; |
| 4577 | } |
| 4578 | |
| 4579 | rc = ly_type_validate_patterns(patterns, args[0]->val.str, strlen(args[0]->val.str), &err); |
| 4580 | pcre2_code_free((*pattern)->code); |
| 4581 | free(*pattern); |
| 4582 | LY_ARRAY_FREE(patterns); |
| 4583 | if (rc && (rc != LY_EVALID)) { |
| 4584 | ly_err_print(err); |
| 4585 | ly_err_free(err); |
| 4586 | return rc; |
| 4587 | } |
| 4588 | |
| 4589 | if (rc == LY_EVALID) { |
| 4590 | ly_err_free(err); |
| 4591 | set_fill_boolean(set, 0); |
| 4592 | } else { |
| 4593 | set_fill_boolean(set, 1); |
| 4594 | } |
| 4595 | |
| 4596 | return LY_SUCCESS; |
| 4597 | } |
| 4598 | |
| 4599 | /** |
| 4600 | * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER |
| 4601 | * with the rounded first argument. For details refer to |
| 4602 | * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round. |
| 4603 | * |
| 4604 | * @param[in] args Array of arguments. |
| 4605 | * @param[in] arg_count Count of elements in @p args. |
| 4606 | * @param[in,out] set Context and result set at the same time. |
| 4607 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4608 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4609 | */ |
| 4610 | static LY_ERR |
| 4611 | xpath_round(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 4612 | { |
| 4613 | struct lysc_node_leaf *sleaf; |
| 4614 | LY_ERR rc = LY_SUCCESS; |
| 4615 | |
| 4616 | if (options & LYXP_SCNODE_ALL) { |
| 4617 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4618 | 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] | 4619 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4620 | 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] | 4621 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) { |
| 4622 | 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] | 4623 | } |
| 4624 | set_scnode_clear_ctx(set); |
| 4625 | return rc; |
| 4626 | } |
| 4627 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4628 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4629 | LY_CHECK_RET(rc); |
| 4630 | |
| 4631 | /* cover only the cases where floor can't be used */ |
| 4632 | if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) { |
| 4633 | set_fill_number(set, -0.0f); |
| 4634 | } else { |
| 4635 | args[0]->val.num += 0.5; |
| 4636 | rc = xpath_floor(args, 1, args[0], options); |
| 4637 | LY_CHECK_RET(rc); |
| 4638 | set_fill_number(set, args[0]->val.num); |
| 4639 | } |
| 4640 | |
| 4641 | return LY_SUCCESS; |
| 4642 | } |
| 4643 | |
| 4644 | /** |
| 4645 | * @brief Execute the XPath starts-with(string, string) function. |
| 4646 | * Returns LYXP_SET_BOOLEAN whether the second argument is |
| 4647 | * the prefix of the first or not. |
| 4648 | * |
| 4649 | * @param[in] args Array of arguments. |
| 4650 | * @param[in] arg_count Count of elements in @p args. |
| 4651 | * @param[in,out] set Context and result set at the same time. |
| 4652 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4653 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4654 | */ |
| 4655 | static LY_ERR |
| 4656 | xpath_starts_with(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 4657 | { |
| 4658 | struct lysc_node_leaf *sleaf; |
| 4659 | LY_ERR rc = LY_SUCCESS; |
| 4660 | |
| 4661 | if (options & LYXP_SCNODE_ALL) { |
| 4662 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4663 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4664 | 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] | 4665 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4666 | 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] | 4667 | } |
| 4668 | } |
| 4669 | |
| 4670 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4671 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4672 | 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] | 4673 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4674 | 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] | 4675 | } |
| 4676 | } |
| 4677 | set_scnode_clear_ctx(set); |
| 4678 | return rc; |
| 4679 | } |
| 4680 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4681 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4682 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4683 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4684 | LY_CHECK_RET(rc); |
| 4685 | |
| 4686 | if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) { |
| 4687 | set_fill_boolean(set, 0); |
| 4688 | } else { |
| 4689 | set_fill_boolean(set, 1); |
| 4690 | } |
| 4691 | |
| 4692 | return LY_SUCCESS; |
| 4693 | } |
| 4694 | |
| 4695 | /** |
| 4696 | * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING |
| 4697 | * with the string representation of either the argument or the context. |
| 4698 | * |
| 4699 | * @param[in] args Array of arguments. |
| 4700 | * @param[in] arg_count Count of elements in @p args. |
| 4701 | * @param[in,out] set Context and result set at the same time. |
| 4702 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4703 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4704 | */ |
| 4705 | static LY_ERR |
| 4706 | xpath_string(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options) |
| 4707 | { |
| 4708 | LY_ERR rc; |
| 4709 | |
| 4710 | if (options & LYXP_SCNODE_ALL) { |
| 4711 | set_scnode_clear_ctx(set); |
| 4712 | return LY_SUCCESS; |
| 4713 | } |
| 4714 | |
| 4715 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4716 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4717 | LY_CHECK_RET(rc); |
| 4718 | set_fill_set(set, args[0]); |
| 4719 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4720 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4721 | LY_CHECK_RET(rc); |
| 4722 | } |
| 4723 | |
| 4724 | return LY_SUCCESS; |
| 4725 | } |
| 4726 | |
| 4727 | /** |
| 4728 | * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER |
| 4729 | * with the length of the string in either the argument or the context. |
| 4730 | * |
| 4731 | * @param[in] args Array of arguments. |
| 4732 | * @param[in] arg_count Count of elements in @p args. |
| 4733 | * @param[in,out] set Context and result set at the same time. |
| 4734 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4735 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4736 | */ |
| 4737 | static LY_ERR |
| 4738 | xpath_string_length(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options) |
| 4739 | { |
| 4740 | struct lysc_node_leaf *sleaf; |
| 4741 | LY_ERR rc = LY_SUCCESS; |
| 4742 | |
| 4743 | if (options & LYXP_SCNODE_ALL) { |
| 4744 | if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4745 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4746 | 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] | 4747 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4748 | 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] | 4749 | } |
| 4750 | } |
| 4751 | if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) { |
| 4752 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4753 | 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] | 4754 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4755 | 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] | 4756 | } |
| 4757 | } |
| 4758 | set_scnode_clear_ctx(set); |
| 4759 | return rc; |
| 4760 | } |
| 4761 | |
| 4762 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4763 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4764 | LY_CHECK_RET(rc); |
| 4765 | set_fill_number(set, strlen(args[0]->val.str)); |
| 4766 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4767 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4768 | LY_CHECK_RET(rc); |
| 4769 | set_fill_number(set, strlen(set->val.str)); |
| 4770 | } |
| 4771 | |
| 4772 | return LY_SUCCESS; |
| 4773 | } |
| 4774 | |
| 4775 | /** |
| 4776 | * @brief Execute the XPath substring(string, number, number?) function. |
| 4777 | * Returns LYXP_SET_STRING substring of the first argument starting |
| 4778 | * on the second argument index ending on the third argument index, |
| 4779 | * indexed from 1. For exact definition refer to |
| 4780 | * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring. |
| 4781 | * |
| 4782 | * @param[in] args Array of arguments. |
| 4783 | * @param[in] arg_count Count of elements in @p args. |
| 4784 | * @param[in,out] set Context and result set at the same time. |
| 4785 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4786 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4787 | */ |
| 4788 | static LY_ERR |
| 4789 | xpath_substring(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options) |
| 4790 | { |
| 4791 | int start, len; |
| 4792 | uint16_t str_start, str_len, pos; |
| 4793 | struct lysc_node_leaf *sleaf; |
| 4794 | LY_ERR rc = LY_SUCCESS; |
| 4795 | |
| 4796 | if (options & LYXP_SCNODE_ALL) { |
| 4797 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4798 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4799 | 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] | 4800 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4801 | 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] | 4802 | } |
| 4803 | } |
| 4804 | |
| 4805 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4806 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4807 | 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] | 4808 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 4809 | 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] | 4810 | } |
| 4811 | } |
| 4812 | |
| 4813 | if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET) |
| 4814 | && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) { |
| 4815 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4816 | 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] | 4817 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 4818 | 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] | 4819 | } |
| 4820 | } |
| 4821 | set_scnode_clear_ctx(set); |
| 4822 | return rc; |
| 4823 | } |
| 4824 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4825 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4826 | LY_CHECK_RET(rc); |
| 4827 | |
| 4828 | /* start */ |
| 4829 | if (xpath_round(&args[1], 1, args[1], options)) { |
| 4830 | return -1; |
| 4831 | } |
| 4832 | if (isfinite(args[1]->val.num)) { |
| 4833 | start = args[1]->val.num - 1; |
| 4834 | } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) { |
| 4835 | start = INT_MIN; |
| 4836 | } else { |
| 4837 | start = INT_MAX; |
| 4838 | } |
| 4839 | |
| 4840 | /* len */ |
| 4841 | if (arg_count == 3) { |
| 4842 | rc = xpath_round(&args[2], 1, args[2], options); |
| 4843 | LY_CHECK_RET(rc); |
| 4844 | if (isfinite(args[2]->val.num)) { |
| 4845 | len = args[2]->val.num; |
| 4846 | } else if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) { |
| 4847 | len = 0; |
| 4848 | } else { |
| 4849 | len = INT_MAX; |
| 4850 | } |
| 4851 | } else { |
| 4852 | len = INT_MAX; |
| 4853 | } |
| 4854 | |
| 4855 | /* find matching character positions */ |
| 4856 | str_start = 0; |
| 4857 | str_len = 0; |
| 4858 | for (pos = 0; args[0]->val.str[pos]; ++pos) { |
| 4859 | if (pos < start) { |
| 4860 | ++str_start; |
| 4861 | } else if (pos < start + len) { |
| 4862 | ++str_len; |
| 4863 | } else { |
| 4864 | break; |
| 4865 | } |
| 4866 | } |
| 4867 | |
| 4868 | set_fill_string(set, args[0]->val.str + str_start, str_len); |
| 4869 | return LY_SUCCESS; |
| 4870 | } |
| 4871 | |
| 4872 | /** |
| 4873 | * @brief Execute the XPath substring-after(string, string) function. |
| 4874 | * Returns LYXP_SET_STRING with the string succeeding the occurance |
| 4875 | * of the second argument in the first or an empty string. |
| 4876 | * |
| 4877 | * @param[in] args Array of arguments. |
| 4878 | * @param[in] arg_count Count of elements in @p args. |
| 4879 | * @param[in,out] set Context and result set at the same time. |
| 4880 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4881 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4882 | */ |
| 4883 | static LY_ERR |
| 4884 | xpath_substring_after(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 4885 | { |
| 4886 | char *ptr; |
| 4887 | struct lysc_node_leaf *sleaf; |
| 4888 | LY_ERR rc = LY_SUCCESS; |
| 4889 | |
| 4890 | if (options & LYXP_SCNODE_ALL) { |
| 4891 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4892 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4893 | 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] | 4894 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4895 | 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] | 4896 | } |
| 4897 | } |
| 4898 | |
| 4899 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4900 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4901 | 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] | 4902 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4903 | 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] | 4904 | } |
| 4905 | } |
| 4906 | set_scnode_clear_ctx(set); |
| 4907 | return rc; |
| 4908 | } |
| 4909 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4910 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4911 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4912 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4913 | LY_CHECK_RET(rc); |
| 4914 | |
| 4915 | ptr = strstr(args[0]->val.str, args[1]->val.str); |
| 4916 | if (ptr) { |
| 4917 | set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str))); |
| 4918 | } else { |
| 4919 | set_fill_string(set, "", 0); |
| 4920 | } |
| 4921 | |
| 4922 | return LY_SUCCESS; |
| 4923 | } |
| 4924 | |
| 4925 | /** |
| 4926 | * @brief Execute the XPath substring-before(string, string) function. |
| 4927 | * Returns LYXP_SET_STRING with the string preceding the occurance |
| 4928 | * of the second argument in the first or an empty string. |
| 4929 | * |
| 4930 | * @param[in] args Array of arguments. |
| 4931 | * @param[in] arg_count Count of elements in @p args. |
| 4932 | * @param[in,out] set Context and result set at the same time. |
| 4933 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4934 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4935 | */ |
| 4936 | static LY_ERR |
| 4937 | xpath_substring_before(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 4938 | { |
| 4939 | char *ptr; |
| 4940 | struct lysc_node_leaf *sleaf; |
| 4941 | LY_ERR rc = LY_SUCCESS; |
| 4942 | |
| 4943 | if (options & LYXP_SCNODE_ALL) { |
| 4944 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4945 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4946 | 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] | 4947 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4948 | 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] | 4949 | } |
| 4950 | } |
| 4951 | |
| 4952 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4953 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4954 | 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] | 4955 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4956 | 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] | 4957 | } |
| 4958 | } |
| 4959 | set_scnode_clear_ctx(set); |
| 4960 | return rc; |
| 4961 | } |
| 4962 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4963 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4964 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4965 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4966 | LY_CHECK_RET(rc); |
| 4967 | |
| 4968 | ptr = strstr(args[0]->val.str, args[1]->val.str); |
| 4969 | if (ptr) { |
| 4970 | set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str); |
| 4971 | } else { |
| 4972 | set_fill_string(set, "", 0); |
| 4973 | } |
| 4974 | |
| 4975 | return LY_SUCCESS; |
| 4976 | } |
| 4977 | |
| 4978 | /** |
| 4979 | * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER |
| 4980 | * with the sum of all the nodes in the context. |
| 4981 | * |
| 4982 | * @param[in] args Array of arguments. |
| 4983 | * @param[in] arg_count Count of elements in @p args. |
| 4984 | * @param[in,out] set Context and result set at the same time. |
| 4985 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4986 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4987 | */ |
| 4988 | static LY_ERR |
| 4989 | xpath_sum(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 4990 | { |
| 4991 | long double num; |
| 4992 | char *str; |
| 4993 | uint16_t i; |
| 4994 | struct lyxp_set set_item; |
| 4995 | struct lysc_node_leaf *sleaf; |
| 4996 | LY_ERR rc = LY_SUCCESS; |
| 4997 | |
| 4998 | if (options & LYXP_SCNODE_ALL) { |
| 4999 | if (args[0]->type == LYXP_SET_SCNODE_SET) { |
| 5000 | for (i = 0; i < args[0]->used; ++i) { |
| 5001 | if (args[0]->val.scnodes[i].in_ctx == 1) { |
| 5002 | sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode; |
| 5003 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5004 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, |
| 5005 | lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5006 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 5007 | 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] | 5008 | } |
| 5009 | } |
| 5010 | } |
| 5011 | } |
| 5012 | set_scnode_clear_ctx(set); |
| 5013 | return rc; |
| 5014 | } |
| 5015 | |
| 5016 | set_fill_number(set, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5017 | |
| 5018 | if (args[0]->type != LYXP_SET_NODE_SET) { |
| 5019 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "sum(node-set)"); |
| 5020 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5021 | } else if (!args[0]->used) { |
| 5022 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5023 | } |
| 5024 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5025 | set_init(&set_item, set); |
| 5026 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5027 | set_item.type = LYXP_SET_NODE_SET; |
| 5028 | set_item.val.nodes = malloc(sizeof *set_item.val.nodes); |
| 5029 | LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM); |
| 5030 | |
| 5031 | set_item.used = 1; |
| 5032 | set_item.size = 1; |
| 5033 | |
| 5034 | for (i = 0; i < args[0]->used; ++i) { |
| 5035 | set_item.val.nodes[0] = args[0]->val.nodes[i]; |
| 5036 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5037 | rc = cast_node_set_to_string(&set_item, &str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5038 | LY_CHECK_RET(rc); |
| 5039 | num = cast_string_to_number(str); |
| 5040 | free(str); |
| 5041 | set->val.num += num; |
| 5042 | } |
| 5043 | |
| 5044 | free(set_item.val.nodes); |
| 5045 | |
| 5046 | return LY_SUCCESS; |
| 5047 | } |
| 5048 | |
| 5049 | /** |
| 5050 | * @brief Execute the XPath text() function (node type). Returns LYXP_SET_NODE_SET |
| 5051 | * with the text content of the nodes in the context. |
| 5052 | * |
| 5053 | * @param[in] args Array of arguments. |
| 5054 | * @param[in] arg_count Count of elements in @p args. |
| 5055 | * @param[in,out] set Context and result set at the same time. |
| 5056 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5057 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5058 | */ |
| 5059 | static LY_ERR |
| 5060 | xpath_text(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 5061 | { |
| 5062 | uint32_t i; |
| 5063 | |
| 5064 | if (options & LYXP_SCNODE_ALL) { |
| 5065 | set_scnode_clear_ctx(set); |
| 5066 | return LY_SUCCESS; |
| 5067 | } |
| 5068 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5069 | if (set->type != LYXP_SET_NODE_SET) { |
| 5070 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "text()"); |
| 5071 | return LY_EVALID; |
| 5072 | } |
| 5073 | |
| 5074 | for (i = 0; i < set->used;) { |
| 5075 | switch (set->val.nodes[i].type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 5076 | case LYXP_NODE_NONE: |
| 5077 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5078 | case LYXP_NODE_ELEM: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5079 | if (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 5080 | set->val.nodes[i].type = LYXP_NODE_TEXT; |
| 5081 | ++i; |
| 5082 | break; |
| 5083 | } |
| 5084 | /* fall through */ |
| 5085 | case LYXP_NODE_ROOT: |
| 5086 | case LYXP_NODE_ROOT_CONFIG: |
| 5087 | case LYXP_NODE_TEXT: |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5088 | case LYXP_NODE_META: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5089 | set_remove_node(set, i); |
| 5090 | break; |
| 5091 | } |
| 5092 | } |
| 5093 | |
| 5094 | return LY_SUCCESS; |
| 5095 | } |
| 5096 | |
| 5097 | /** |
| 5098 | * @brief Execute the XPath translate(string, string, string) function. |
| 5099 | * Returns LYXP_SET_STRING with the first argument with the characters |
| 5100 | * from the second argument replaced by those on the corresponding |
| 5101 | * positions in the third argument. |
| 5102 | * |
| 5103 | * @param[in] args Array of arguments. |
| 5104 | * @param[in] arg_count Count of elements in @p args. |
| 5105 | * @param[in,out] set Context and result set at the same time. |
| 5106 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5107 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5108 | */ |
| 5109 | static LY_ERR |
| 5110 | xpath_translate(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 5111 | { |
| 5112 | uint16_t i, j, new_used; |
| 5113 | char *new; |
| 5114 | int found, have_removed; |
| 5115 | struct lysc_node_leaf *sleaf; |
| 5116 | LY_ERR rc = LY_SUCCESS; |
| 5117 | |
| 5118 | if (options & LYXP_SCNODE_ALL) { |
| 5119 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 5120 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5121 | 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] | 5122 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5123 | 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] | 5124 | } |
| 5125 | } |
| 5126 | |
| 5127 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 5128 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5129 | 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] | 5130 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5131 | 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] | 5132 | } |
| 5133 | } |
| 5134 | |
| 5135 | if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) { |
| 5136 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5137 | 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] | 5138 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5139 | 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] | 5140 | } |
| 5141 | } |
| 5142 | set_scnode_clear_ctx(set); |
| 5143 | return rc; |
| 5144 | } |
| 5145 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5146 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5147 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5148 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5149 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5150 | rc = lyxp_set_cast(args[2], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5151 | LY_CHECK_RET(rc); |
| 5152 | |
| 5153 | new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char)); |
| 5154 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 5155 | new_used = 0; |
| 5156 | |
| 5157 | have_removed = 0; |
| 5158 | for (i = 0; args[0]->val.str[i]; ++i) { |
| 5159 | found = 0; |
| 5160 | |
| 5161 | for (j = 0; args[1]->val.str[j]; ++j) { |
| 5162 | if (args[0]->val.str[i] == args[1]->val.str[j]) { |
| 5163 | /* removing this char */ |
| 5164 | if (j >= strlen(args[2]->val.str)) { |
| 5165 | have_removed = 1; |
| 5166 | found = 1; |
| 5167 | break; |
| 5168 | } |
| 5169 | /* replacing this char */ |
| 5170 | new[new_used] = args[2]->val.str[j]; |
| 5171 | ++new_used; |
| 5172 | found = 1; |
| 5173 | break; |
| 5174 | } |
| 5175 | } |
| 5176 | |
| 5177 | /* copying this char */ |
| 5178 | if (!found) { |
| 5179 | new[new_used] = args[0]->val.str[i]; |
| 5180 | ++new_used; |
| 5181 | } |
| 5182 | } |
| 5183 | |
| 5184 | if (have_removed) { |
| 5185 | new = ly_realloc(new, (new_used + 1) * sizeof(char)); |
| 5186 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 5187 | } |
| 5188 | new[new_used] = '\0'; |
| 5189 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5190 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5191 | set->type = LYXP_SET_STRING; |
| 5192 | set->val.str = new; |
| 5193 | |
| 5194 | return LY_SUCCESS; |
| 5195 | } |
| 5196 | |
| 5197 | /** |
| 5198 | * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN |
| 5199 | * with true value. |
| 5200 | * |
| 5201 | * @param[in] args Array of arguments. |
| 5202 | * @param[in] arg_count Count of elements in @p args. |
| 5203 | * @param[in,out] set Context and result set at the same time. |
| 5204 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5205 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5206 | */ |
| 5207 | static LY_ERR |
| 5208 | xpath_true(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 5209 | { |
| 5210 | if (options & LYXP_SCNODE_ALL) { |
| 5211 | set_scnode_clear_ctx(set); |
| 5212 | return LY_SUCCESS; |
| 5213 | } |
| 5214 | |
| 5215 | set_fill_boolean(set, 1); |
| 5216 | return LY_SUCCESS; |
| 5217 | } |
| 5218 | |
| 5219 | /* |
| 5220 | * moveto functions |
| 5221 | * |
| 5222 | * They and only they actually change the context (set). |
| 5223 | */ |
| 5224 | |
| 5225 | /** |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5226 | * @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] | 5227 | * |
Michal Vasko | 2104e9f | 2020-03-06 08:23:25 +0100 | [diff] [blame] | 5228 | * XPath @p set is expected to be a (sc)node set! |
| 5229 | * |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5230 | * @param[in,out] qname Qualified node name. If includes prefix, it is skipped. |
| 5231 | * @param[in,out] qname_len Length of @p qname, is updated accordingly. |
| 5232 | * @param[in] set Set with XPath context. |
| 5233 | * @param[out] moveto_mod Expected module of a matching node. |
| 5234 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5235 | */ |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5236 | static LY_ERR |
| 5237 | 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] | 5238 | { |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 5239 | const struct lys_module *mod = NULL; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5240 | const char *ptr; |
| 5241 | int pref_len; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5242 | |
Michal Vasko | 2104e9f | 2020-03-06 08:23:25 +0100 | [diff] [blame] | 5243 | assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET)); |
| 5244 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5245 | if ((ptr = ly_strnchr(*qname, ':', *qname_len))) { |
| 5246 | /* specific module */ |
| 5247 | pref_len = ptr - *qname; |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 5248 | mod = ly_resolve_prefix(set->ctx, *qname, pref_len, set->format, (void *)set->local_mod); |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5249 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5250 | /* check for errors and non-implemented modules, as they are not valid */ |
Juraj Vijtiuk | d75faa6 | 2019-11-26 14:10:10 +0100 | [diff] [blame] | 5251 | if (!mod || !mod->implemented) { |
Michal Vasko | 2104e9f | 2020-03-06 08:23:25 +0100 | [diff] [blame] | 5252 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 5253 | LOGVAL(set->ctx, LY_VLOG_LYSC, set->ctx_scnode, LY_VCODE_XP_INMOD, pref_len, *qname); |
| 5254 | } else { |
| 5255 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INMOD, pref_len, *qname); |
| 5256 | } |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5257 | return LY_EVALID; |
| 5258 | } |
Juraj Vijtiuk | d75faa6 | 2019-11-26 14:10:10 +0100 | [diff] [blame] | 5259 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5260 | *qname += pref_len + 1; |
| 5261 | *qname_len -= pref_len + 1; |
| 5262 | } else if (((*qname)[0] == '*') && (*qname_len == 1)) { |
| 5263 | /* all modules - special case */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5264 | mod = NULL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5265 | } else if (set->type == LYXP_SET_SCNODE_SET) { |
| 5266 | /* current node module */ |
| 5267 | mod = set->ctx_scnode->module; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5268 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5269 | /* current node module */ |
| 5270 | mod = set->ctx_node->schema->module; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5271 | } |
| 5272 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5273 | *moveto_mod = mod; |
| 5274 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5275 | } |
| 5276 | |
| 5277 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5278 | * @brief Move context @p set to the root. Handles absolute path. |
| 5279 | * Result is LYXP_SET_NODE_SET. |
| 5280 | * |
| 5281 | * @param[in,out] set Set to use. |
| 5282 | * @param[in] options Xpath options. |
| 5283 | */ |
| 5284 | static void |
| 5285 | moveto_root(struct lyxp_set *set, int options) |
| 5286 | { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5287 | if (!set) { |
| 5288 | return; |
| 5289 | } |
| 5290 | |
| 5291 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5292 | set_scnode_clear_ctx(set); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 5293 | lyxp_set_scnode_insert_node(set, NULL, set->root_type); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5294 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5295 | set->type = LYXP_SET_NODE_SET; |
| 5296 | set->used = 0; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5297 | set_insert_node(set, NULL, 0, set->root_type, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5298 | } |
| 5299 | } |
| 5300 | |
| 5301 | /** |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 5302 | * @brief Check whether a node has some unresolved "when". |
| 5303 | * |
| 5304 | * @param[in] node Node to check. |
| 5305 | * @return LY_ERR value (LY_EINCOMPLETE if there are some unresolved "when") |
| 5306 | */ |
| 5307 | static LY_ERR |
| 5308 | moveto_when_check(const struct lyd_node *node) |
| 5309 | { |
| 5310 | const struct lysc_node *schema; |
| 5311 | |
| 5312 | if (!node) { |
| 5313 | return LY_SUCCESS; |
| 5314 | } |
| 5315 | |
| 5316 | schema = node->schema; |
| 5317 | do { |
| 5318 | if (schema->when && !(node->flags & LYD_WHEN_TRUE)) { |
| 5319 | return LY_EINCOMPLETE; |
| 5320 | } |
| 5321 | schema = schema->parent; |
| 5322 | } while (schema && (schema->nodetype & (LYS_CASE | LYS_CHOICE))); |
| 5323 | |
| 5324 | return LY_SUCCESS; |
| 5325 | } |
| 5326 | |
| 5327 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5328 | * @brief Check @p node as a part of NameTest processing. |
| 5329 | * |
| 5330 | * @param[in] node Node to check. |
| 5331 | * @param[in] root_type XPath root node type. |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5332 | * @param[in] context_op XPath operation parent. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5333 | * @param[in] node_name Node name in the dictionary to move to, NULL for any node. |
| 5334 | * @param[in] moveto_mod Expected module of the node, NULL for any. |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5335 | * @return LY_ERR (LY_ENOT if node does not match, LY_EINCOMPLETE on unresolved when, |
| 5336 | * LY_EINVAL if netither node nor any children match) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5337 | */ |
| 5338 | static LY_ERR |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5339 | moveto_node_check(const struct lyd_node *node, enum lyxp_node_type root_type, const struct lysc_node *context_op, |
| 5340 | const char *node_name, const struct lys_module *moveto_mod) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5341 | { |
| 5342 | /* module check */ |
| 5343 | if (moveto_mod && (node->schema->module != moveto_mod)) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5344 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5345 | } |
| 5346 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5347 | /* context check */ |
| 5348 | 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] | 5349 | return LY_EINVAL; |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5350 | } else if (context_op && (node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node->schema != context_op)) { |
| 5351 | return LY_EINVAL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5352 | } |
| 5353 | |
| 5354 | /* name check */ |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5355 | if (node_name && strcmp(node_name, "*") && (node->schema->name != node_name)) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5356 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5357 | } |
| 5358 | |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 5359 | /* when check */ |
| 5360 | if (moveto_when_check(node)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5361 | return LY_EINCOMPLETE; |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 5362 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5363 | |
| 5364 | /* match */ |
| 5365 | return LY_SUCCESS; |
| 5366 | } |
| 5367 | |
| 5368 | /** |
| 5369 | * @brief Check @p node as a part of schema NameTest processing. |
| 5370 | * |
| 5371 | * @param[in] node Schema node to check. |
| 5372 | * @param[in] root_type XPath root node type. |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5373 | * @param[in] context_op XPath operation parent. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5374 | * @param[in] node_name Node name in the dictionary to move to, NULL for any nodes. |
| 5375 | * @param[in] moveto_mod Expected module of the node, NULL for any. |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5376 | * @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] | 5377 | */ |
| 5378 | static LY_ERR |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5379 | moveto_scnode_check(const struct lysc_node *node, enum lyxp_node_type root_type, const struct lysc_node *context_op, |
| 5380 | const char *node_name, const struct lys_module *moveto_mod) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5381 | { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5382 | /* module check */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5383 | if (moveto_mod && (node->module != moveto_mod)) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5384 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5385 | } |
| 5386 | |
| 5387 | /* context check */ |
| 5388 | if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) { |
| 5389 | return LY_EINVAL; |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5390 | } else if (context_op && (node->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node != context_op)) { |
| 5391 | return LY_EINVAL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5392 | } |
| 5393 | |
| 5394 | /* name check */ |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5395 | if (node_name && strcmp(node_name, "*") && (node->name != node_name)) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5396 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5397 | } |
| 5398 | |
| 5399 | /* match */ |
| 5400 | return LY_SUCCESS; |
| 5401 | } |
| 5402 | |
| 5403 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5404 | * @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] | 5405 | * |
| 5406 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5407 | * @param[in] mod Matching node module, NULL for any. |
| 5408 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5409 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 5410 | */ |
| 5411 | static LY_ERR |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5412 | 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] | 5413 | { |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 5414 | uint32_t i; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5415 | int replaced; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5416 | const struct lyd_node *siblings, *sub; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5417 | LY_ERR rc; |
| 5418 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5419 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5420 | return LY_SUCCESS; |
| 5421 | } |
| 5422 | |
| 5423 | if (set->type != LYXP_SET_NODE_SET) { |
| 5424 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 5425 | return LY_EVALID; |
| 5426 | } |
| 5427 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5428 | for (i = 0; i < set->used; ) { |
| 5429 | replaced = 0; |
| 5430 | |
| 5431 | if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5432 | assert(!set->val.nodes[i].node); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5433 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5434 | /* search in all the trees */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5435 | siblings = set->tree; |
Michal Vasko | 5bfd4be | 2020-06-23 13:26:19 +0200 | [diff] [blame] | 5436 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5437 | /* search in children */ |
Michal Vasko | 5bfd4be | 2020-06-23 13:26:19 +0200 | [diff] [blame] | 5438 | siblings = lyd_node_children(set->val.nodes[i].node, 0); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5439 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5440 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5441 | for (sub = siblings; sub; sub = sub->next) { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5442 | rc = moveto_node_check(sub, set->root_type, set->context_op, ncname, mod); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5443 | if (rc == LY_SUCCESS) { |
| 5444 | if (!replaced) { |
| 5445 | set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i); |
| 5446 | replaced = 1; |
| 5447 | } else { |
| 5448 | set_insert_node(set, sub, 0, LYXP_NODE_ELEM, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5449 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5450 | ++i; |
| 5451 | } else if (rc == LY_EINCOMPLETE) { |
| 5452 | return rc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5453 | } |
| 5454 | } |
| 5455 | |
| 5456 | if (!replaced) { |
| 5457 | /* no match */ |
| 5458 | set_remove_node(set, i); |
| 5459 | } |
| 5460 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5461 | |
| 5462 | return LY_SUCCESS; |
| 5463 | } |
| 5464 | |
| 5465 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5466 | * @brief Move context @p set to a node using hashes. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY). |
| 5467 | * Context position aware. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5468 | * |
| 5469 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5470 | * @param[in] scnode Matching node schema. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5471 | * @param[in] predicates If @p scnode is ::LYS_LIST or ::LYS_LEAFLIST, the predicates specifying a single instance. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5472 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 5473 | */ |
| 5474 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5475 | moveto_node_hash(struct lyxp_set *set, const struct lysc_node *scnode, const struct ly_path_predicate *predicates) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5476 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5477 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5478 | uint32_t i; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5479 | const struct lyd_node *siblings; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5480 | struct lyd_node *sub, *inst = NULL; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5481 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5482 | assert(scnode && (!(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) || predicates)); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5483 | |
| 5484 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5485 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5486 | } |
| 5487 | |
| 5488 | if (set->type != LYXP_SET_NODE_SET) { |
| 5489 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5490 | ret = LY_EVALID; |
| 5491 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5492 | } |
| 5493 | |
| 5494 | /* context check for all the nodes since we have the schema node */ |
| 5495 | if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) { |
| 5496 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5497 | goto cleanup; |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5498 | } else if (set->context_op && (scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) |
| 5499 | && (scnode != set->context_op)) { |
| 5500 | lyxp_set_free_content(set); |
| 5501 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5502 | } |
| 5503 | |
| 5504 | /* create specific data instance if needed */ |
| 5505 | if (scnode->nodetype == LYS_LIST) { |
| 5506 | LY_CHECK_GOTO(ret = lyd_create_list(scnode, predicates, &inst), cleanup); |
| 5507 | } else if (scnode->nodetype == LYS_LEAFLIST) { |
| 5508 | LY_CHECK_GOTO(ret = lyd_create_term2(scnode, &predicates[0].value, &inst), cleanup); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5509 | } |
| 5510 | |
| 5511 | for (i = 0; i < set->used; ) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5512 | siblings = NULL; |
| 5513 | |
| 5514 | if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) { |
| 5515 | assert(!set->val.nodes[i].node); |
| 5516 | |
| 5517 | /* search in all the trees */ |
| 5518 | siblings = set->tree; |
| 5519 | } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
| 5520 | /* search in children */ |
Michal Vasko | 5bfd4be | 2020-06-23 13:26:19 +0200 | [diff] [blame] | 5521 | siblings = lyd_node_children(set->val.nodes[i].node, 0); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5522 | } |
| 5523 | |
| 5524 | /* find the node using hashes */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5525 | if (inst) { |
| 5526 | lyd_find_sibling_first(siblings, inst, &sub); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5527 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5528 | lyd_find_sibling_val(siblings, scnode, NULL, 0, &sub); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5529 | } |
| 5530 | |
| 5531 | /* when check */ |
| 5532 | if (sub && moveto_when_check(sub)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5533 | ret = LY_EINCOMPLETE; |
| 5534 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5535 | } |
| 5536 | |
| 5537 | if (sub) { |
| 5538 | /* pos filled later */ |
Michal Vasko | cb1b7c0 | 2020-07-03 13:38:12 +0200 | [diff] [blame] | 5539 | set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5540 | ++i; |
Michal Vasko | cb1b7c0 | 2020-07-03 13:38:12 +0200 | [diff] [blame] | 5541 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5542 | /* no match */ |
| 5543 | set_remove_node(set, i); |
| 5544 | } |
| 5545 | } |
| 5546 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5547 | cleanup: |
| 5548 | lyd_free_tree(inst); |
| 5549 | return ret; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5550 | } |
| 5551 | |
| 5552 | /** |
| 5553 | * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY). |
| 5554 | * |
| 5555 | * @param[in,out] set Set to use. |
| 5556 | * @param[in] mod Matching node module, NULL for any. |
| 5557 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5558 | * @param[in] options XPath options. |
| 5559 | * @return LY_ERR |
| 5560 | */ |
| 5561 | static LY_ERR |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5562 | 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] | 5563 | { |
Michal Vasko | cafad9d | 2019-11-07 15:20:03 +0100 | [diff] [blame] | 5564 | int i, orig_used, idx, temp_ctx = 0, getnext_opts; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5565 | uint32_t mod_idx; |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 5566 | const struct lysc_node *iter, *start_parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5567 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5568 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5569 | return LY_SUCCESS; |
| 5570 | } |
| 5571 | |
| 5572 | if (set->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 5573 | 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] | 5574 | return LY_EVALID; |
| 5575 | } |
| 5576 | |
Michal Vasko | cafad9d | 2019-11-07 15:20:03 +0100 | [diff] [blame] | 5577 | /* getnext opts */ |
| 5578 | getnext_opts = LYS_GETNEXT_NOSTATECHECK; |
| 5579 | if (options & LYXP_SCNODE_OUTPUT) { |
| 5580 | getnext_opts |= LYS_GETNEXT_OUTPUT; |
| 5581 | } |
| 5582 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5583 | orig_used = set->used; |
| 5584 | for (i = 0; i < orig_used; ++i) { |
| 5585 | if (set->val.scnodes[i].in_ctx != 1) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5586 | if (set->val.scnodes[i].in_ctx != -2) { |
| 5587 | continue; |
| 5588 | } |
| 5589 | |
| 5590 | /* remember context node */ |
| 5591 | set->val.scnodes[i].in_ctx = -1; |
Michal Vasko | ec4df48 | 2019-12-16 10:02:18 +0100 | [diff] [blame] | 5592 | } else { |
| 5593 | set->val.scnodes[i].in_ctx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5594 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5595 | |
| 5596 | start_parent = set->val.scnodes[i].scnode; |
| 5597 | |
| 5598 | 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] | 5599 | /* 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] | 5600 | * so use it directly (root node itself is useless in this case) */ |
| 5601 | mod_idx = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5602 | 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] | 5603 | iter = NULL; |
Michal Vasko | 509de4d | 2019-12-10 14:51:30 +0100 | [diff] [blame] | 5604 | /* module may not be implemented */ |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 5605 | while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5606 | 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] | 5607 | idx = lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5608 | /* we need to prevent these nodes from being considered in this moveto */ |
| 5609 | if ((idx < orig_used) && (idx > i)) { |
| 5610 | set->val.scnodes[idx].in_ctx = 2; |
| 5611 | temp_ctx = 1; |
| 5612 | } |
| 5613 | } |
| 5614 | } |
| 5615 | |
| 5616 | if (!mod_idx) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5617 | /* mod was specified, we are not going through the whole context */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5618 | break; |
| 5619 | } |
| 5620 | /* next iteration */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5621 | mod = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5622 | } |
| 5623 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 5624 | } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) { |
| 5625 | iter = NULL; |
| 5626 | while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5627 | 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] | 5628 | idx = lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5629 | if ((idx < orig_used) && (idx > i)) { |
| 5630 | set->val.scnodes[idx].in_ctx = 2; |
| 5631 | temp_ctx = 1; |
| 5632 | } |
| 5633 | } |
| 5634 | } |
| 5635 | } |
| 5636 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5637 | |
| 5638 | /* correct temporary in_ctx values */ |
| 5639 | if (temp_ctx) { |
| 5640 | for (i = 0; i < orig_used; ++i) { |
| 5641 | if (set->val.scnodes[i].in_ctx == 2) { |
| 5642 | set->val.scnodes[i].in_ctx = 1; |
| 5643 | } |
| 5644 | } |
| 5645 | } |
| 5646 | |
| 5647 | return LY_SUCCESS; |
| 5648 | } |
| 5649 | |
| 5650 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5651 | * @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] | 5652 | * Context position aware. |
| 5653 | * |
| 5654 | * @param[in] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5655 | * @param[in] mod Matching node module, NULL for any. |
| 5656 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5657 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 5658 | */ |
| 5659 | static LY_ERR |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5660 | 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] | 5661 | { |
| 5662 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5663 | const struct lyd_node *next, *elem, *start; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5664 | struct lyxp_set ret_set; |
| 5665 | LY_ERR rc; |
| 5666 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5667 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5668 | return LY_SUCCESS; |
| 5669 | } |
| 5670 | |
| 5671 | if (set->type != LYXP_SET_NODE_SET) { |
| 5672 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 5673 | return LY_EVALID; |
| 5674 | } |
| 5675 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5676 | /* 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] | 5677 | rc = moveto_node(set, NULL, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5678 | LY_CHECK_RET(rc); |
| 5679 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5680 | /* 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] | 5681 | set_init(&ret_set, set); |
| 5682 | for (i = 0; i < set->used; ++i) { |
| 5683 | |
| 5684 | /* TREE DFS */ |
| 5685 | start = set->val.nodes[i].node; |
| 5686 | for (elem = next = start; elem; elem = next) { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5687 | 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] | 5688 | if (!rc) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5689 | /* add matching node into result set */ |
| 5690 | set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used); |
| 5691 | if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) { |
| 5692 | /* the node is a duplicate, we'll process it later in the set */ |
| 5693 | goto skip_children; |
| 5694 | } |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5695 | } else if (rc == LY_EINCOMPLETE) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5696 | return rc; |
| 5697 | } else if (rc == LY_EINVAL) { |
| 5698 | goto skip_children; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5699 | } |
| 5700 | |
| 5701 | /* TREE DFS NEXT ELEM */ |
| 5702 | /* select element for the next run - children first */ |
Michal Vasko | 5bfd4be | 2020-06-23 13:26:19 +0200 | [diff] [blame] | 5703 | next = lyd_node_children(elem, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5704 | if (!next) { |
| 5705 | skip_children: |
| 5706 | /* no children, so try siblings, but only if it's not the start, |
| 5707 | * that is considered to be the root and it's siblings are not traversed */ |
| 5708 | if (elem != start) { |
| 5709 | next = elem->next; |
| 5710 | } else { |
| 5711 | break; |
| 5712 | } |
| 5713 | } |
| 5714 | while (!next) { |
| 5715 | /* no siblings, go back through the parents */ |
| 5716 | if ((struct lyd_node *)elem->parent == start) { |
| 5717 | /* we are done, no next element to process */ |
| 5718 | break; |
| 5719 | } |
| 5720 | /* parent is already processed, go to its sibling */ |
| 5721 | elem = (struct lyd_node *)elem->parent; |
| 5722 | next = elem->next; |
| 5723 | } |
| 5724 | } |
| 5725 | } |
| 5726 | |
| 5727 | /* make the temporary set the current one */ |
| 5728 | ret_set.ctx_pos = set->ctx_pos; |
| 5729 | ret_set.ctx_size = set->ctx_size; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5730 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5731 | memcpy(set, &ret_set, sizeof *set); |
| 5732 | |
| 5733 | return LY_SUCCESS; |
| 5734 | } |
| 5735 | |
| 5736 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5737 | * @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] | 5738 | * |
| 5739 | * @param[in] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5740 | * @param[in] mod Matching node module, NULL for any. |
| 5741 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5742 | * @param[in] options XPath options. |
| 5743 | * @return LY_ERR |
| 5744 | */ |
| 5745 | static LY_ERR |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5746 | 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] | 5747 | { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5748 | int i, orig_used, idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5749 | const struct lysc_node *next, *elem, *start; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5750 | LY_ERR rc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5751 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5752 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5753 | return LY_SUCCESS; |
| 5754 | } |
| 5755 | |
| 5756 | if (set->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 5757 | 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] | 5758 | return LY_EVALID; |
| 5759 | } |
| 5760 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5761 | orig_used = set->used; |
| 5762 | for (i = 0; i < orig_used; ++i) { |
| 5763 | if (set->val.scnodes[i].in_ctx != 1) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5764 | if (set->val.scnodes[i].in_ctx != -2) { |
| 5765 | continue; |
| 5766 | } |
| 5767 | |
| 5768 | /* remember context node */ |
| 5769 | set->val.scnodes[i].in_ctx = -1; |
Michal Vasko | ec4df48 | 2019-12-16 10:02:18 +0100 | [diff] [blame] | 5770 | } else { |
| 5771 | set->val.scnodes[i].in_ctx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5772 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5773 | |
| 5774 | /* TREE DFS */ |
| 5775 | start = set->val.scnodes[i].scnode; |
| 5776 | for (elem = next = start; elem; elem = next) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5777 | if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) { |
| 5778 | /* schema-only nodes, skip root */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5779 | goto next_iter; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5780 | } |
| 5781 | |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5782 | 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] | 5783 | if (!rc) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 5784 | 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] | 5785 | set->val.scnodes[idx].in_ctx = 1; |
| 5786 | if (idx > i) { |
| 5787 | /* we will process it later in the set */ |
| 5788 | goto skip_children; |
| 5789 | } |
| 5790 | } else { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 5791 | lyxp_set_scnode_insert_node(set, elem, LYXP_NODE_ELEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5792 | } |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5793 | } else if (rc == LY_EINVAL) { |
| 5794 | goto skip_children; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5795 | } |
| 5796 | |
| 5797 | next_iter: |
| 5798 | /* TREE DFS NEXT ELEM */ |
| 5799 | /* select element for the next run - children first */ |
| 5800 | 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] | 5801 | if (!next) { |
| 5802 | skip_children: |
| 5803 | /* no children, so try siblings, but only if it's not the start, |
| 5804 | * that is considered to be the root and it's siblings are not traversed */ |
| 5805 | if (elem != start) { |
| 5806 | next = elem->next; |
| 5807 | } else { |
| 5808 | break; |
| 5809 | } |
| 5810 | } |
| 5811 | while (!next) { |
| 5812 | /* no siblings, go back through the parents */ |
| 5813 | if (elem->parent == start) { |
| 5814 | /* we are done, no next element to process */ |
| 5815 | break; |
| 5816 | } |
| 5817 | /* parent is already processed, go to its sibling */ |
| 5818 | elem = elem->parent; |
| 5819 | next = elem->next; |
| 5820 | } |
| 5821 | } |
| 5822 | } |
| 5823 | |
| 5824 | return LY_SUCCESS; |
| 5825 | } |
| 5826 | |
| 5827 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5828 | * @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] | 5829 | * Indirectly context position aware. |
| 5830 | * |
| 5831 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5832 | * @param[in] mod Matching metadata module, NULL for any. |
| 5833 | * @param[in] ncname Matching metadata name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5834 | * @return LY_ERR |
| 5835 | */ |
| 5836 | static LY_ERR |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5837 | 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] | 5838 | { |
| 5839 | uint32_t i; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5840 | int replaced; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5841 | struct lyd_meta *sub; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5842 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5843 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5844 | return LY_SUCCESS; |
| 5845 | } |
| 5846 | |
| 5847 | if (set->type != LYXP_SET_NODE_SET) { |
| 5848 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 5849 | return LY_EVALID; |
| 5850 | } |
| 5851 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5852 | for (i = 0; i < set->used; ) { |
| 5853 | replaced = 0; |
| 5854 | |
| 5855 | /* only attributes of an elem (not dummy) can be in the result, skip all the rest; |
| 5856 | * our attributes are always qualified */ |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5857 | if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5858 | for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5859 | |
| 5860 | /* check "namespace" */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5861 | if (mod && (sub->annotation->module != mod)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5862 | continue; |
| 5863 | } |
| 5864 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5865 | if (!ncname || (sub->name == ncname)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5866 | /* match */ |
| 5867 | if (!replaced) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5868 | set->val.meta[i].meta = sub; |
| 5869 | set->val.meta[i].type = LYXP_NODE_META; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5870 | /* pos does not change */ |
| 5871 | replaced = 1; |
| 5872 | } else { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5873 | 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] | 5874 | } |
| 5875 | ++i; |
| 5876 | } |
| 5877 | } |
| 5878 | } |
| 5879 | |
| 5880 | if (!replaced) { |
| 5881 | /* no match */ |
| 5882 | set_remove_node(set, i); |
| 5883 | } |
| 5884 | } |
| 5885 | |
| 5886 | return LY_SUCCESS; |
| 5887 | } |
| 5888 | |
| 5889 | /** |
| 5890 | * @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] | 5891 | * Result is LYXP_SET_NODE_SET. Context position aware. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5892 | * |
| 5893 | * @param[in,out] set1 Set to use for the result. |
| 5894 | * @param[in] set2 Set that is copied to @p set1. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5895 | * @return LY_ERR |
| 5896 | */ |
| 5897 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5898 | moveto_union(struct lyxp_set *set1, struct lyxp_set *set2) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5899 | { |
| 5900 | LY_ERR rc; |
| 5901 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5902 | 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] | 5903 | LOGVAL(set1->ctx, LY_VLOG_LYD, set1->ctx_node, LY_VCODE_XP_INOP_2, "union", print_set_type(set1), print_set_type(set2)); |
| 5904 | return LY_EVALID; |
| 5905 | } |
| 5906 | |
| 5907 | /* set2 is empty or both set1 and set2 */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5908 | if (!set2->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5909 | return LY_SUCCESS; |
| 5910 | } |
| 5911 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5912 | if (!set1->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5913 | memcpy(set1, set2, sizeof *set1); |
| 5914 | /* dynamic memory belongs to set1 now, do not free */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5915 | memset(set2, 0, sizeof *set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5916 | return LY_SUCCESS; |
| 5917 | } |
| 5918 | |
| 5919 | /* we assume sets are sorted */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5920 | assert(!set_sort(set1) && !set_sort(set2)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5921 | |
| 5922 | /* sort, remove duplicates */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5923 | rc = set_sorted_merge(set1, set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5924 | LY_CHECK_RET(rc); |
| 5925 | |
| 5926 | /* final set must be sorted */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5927 | assert(!set_sort(set1)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5928 | |
| 5929 | return LY_SUCCESS; |
| 5930 | } |
| 5931 | |
| 5932 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5933 | * @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] | 5934 | * Context position aware. |
| 5935 | * |
| 5936 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5937 | * @param[in] mod Matching metadata module, NULL for any. |
| 5938 | * @param[in] ncname Matching metadata name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5939 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 5940 | */ |
| 5941 | static int |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5942 | 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] | 5943 | { |
| 5944 | uint32_t i; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5945 | int replaced; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5946 | struct lyd_meta *sub; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5947 | struct lyxp_set *set_all_desc = NULL; |
| 5948 | LY_ERR rc; |
| 5949 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5950 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5951 | return LY_SUCCESS; |
| 5952 | } |
| 5953 | |
| 5954 | if (set->type != LYXP_SET_NODE_SET) { |
| 5955 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 5956 | return LY_EVALID; |
| 5957 | } |
| 5958 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5959 | /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory, |
| 5960 | * but it likely won't be used much, so it's a waste of time */ |
| 5961 | /* copy the context */ |
| 5962 | set_all_desc = set_copy(set); |
| 5963 | /* get all descendant nodes (the original context nodes are removed) */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5964 | rc = moveto_node_alldesc(set_all_desc, NULL, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5965 | if (rc != LY_SUCCESS) { |
| 5966 | lyxp_set_free(set_all_desc); |
| 5967 | return rc; |
| 5968 | } |
| 5969 | /* prepend the original context nodes */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5970 | rc = moveto_union(set, set_all_desc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5971 | if (rc != LY_SUCCESS) { |
| 5972 | lyxp_set_free(set_all_desc); |
| 5973 | return rc; |
| 5974 | } |
| 5975 | lyxp_set_free(set_all_desc); |
| 5976 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5977 | for (i = 0; i < set->used; ) { |
| 5978 | replaced = 0; |
| 5979 | |
| 5980 | /* only attributes of an elem can be in the result, skip all the rest, |
| 5981 | * we have all attributes qualified in lyd tree */ |
| 5982 | if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5983 | for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5984 | /* check "namespace" */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5985 | if (mod && (sub->annotation->module != mod)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5986 | continue; |
| 5987 | } |
| 5988 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5989 | if (!ncname || (sub->name == ncname)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5990 | /* match */ |
| 5991 | if (!replaced) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5992 | set->val.meta[i].meta = sub; |
| 5993 | set->val.meta[i].type = LYXP_NODE_META; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5994 | /* pos does not change */ |
| 5995 | replaced = 1; |
| 5996 | } else { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5997 | 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] | 5998 | } |
| 5999 | ++i; |
| 6000 | } |
| 6001 | } |
| 6002 | } |
| 6003 | |
| 6004 | if (!replaced) { |
| 6005 | /* no match */ |
| 6006 | set_remove_node(set, i); |
| 6007 | } |
| 6008 | } |
| 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 self and al chilren, recursively. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET. |
| 6015 | * Context position aware. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6016 | * |
| 6017 | * @param[in] parent Current parent. |
| 6018 | * @param[in] parent_pos Position of @p parent. |
| 6019 | * @param[in] parent_type Node type of @p parent. |
| 6020 | * @param[in,out] to_set Set to use. |
| 6021 | * @param[in] dup_check_set Set for checking duplicities. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6022 | * @param[in] options XPath options. |
| 6023 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6024 | */ |
| 6025 | static LY_ERR |
| 6026 | 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] | 6027 | 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] | 6028 | { |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6029 | const struct lyd_node *iter, *first; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6030 | LY_ERR rc; |
| 6031 | |
| 6032 | switch (parent_type) { |
| 6033 | case LYXP_NODE_ROOT: |
| 6034 | case LYXP_NODE_ROOT_CONFIG: |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6035 | assert(!parent); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6036 | |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6037 | /* add all top-level nodes as elements */ |
| 6038 | first = to_set->tree; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6039 | break; |
| 6040 | case LYXP_NODE_ELEM: |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6041 | /* add just the text node of this term element node */ |
| 6042 | if (parent->schema->nodetype & (LYD_NODE_TERM | LYD_NODE_ANY)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6043 | if (!set_dup_node_check(dup_check_set, parent, LYXP_NODE_TEXT, -1)) { |
| 6044 | set_insert_node(to_set, parent, parent_pos, LYXP_NODE_TEXT, to_set->used); |
| 6045 | } |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6046 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6047 | } |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6048 | |
| 6049 | /* add all the children of this node */ |
Michal Vasko | 5bfd4be | 2020-06-23 13:26:19 +0200 | [diff] [blame] | 6050 | first = lyd_node_children(parent, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6051 | break; |
| 6052 | default: |
| 6053 | LOGINT_RET(parent->schema->module->ctx); |
| 6054 | } |
| 6055 | |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6056 | /* add all top-level nodes as elements */ |
| 6057 | LY_LIST_FOR(first, iter) { |
| 6058 | /* context check */ |
| 6059 | if ((parent_type == LYXP_NODE_ROOT_CONFIG) && (iter->schema->flags & LYS_CONFIG_R)) { |
| 6060 | continue; |
| 6061 | } |
| 6062 | |
| 6063 | /* when check */ |
| 6064 | if (moveto_when_check(iter)) { |
| 6065 | return LY_EINCOMPLETE; |
| 6066 | } |
| 6067 | |
| 6068 | if (!set_dup_node_check(dup_check_set, iter, LYXP_NODE_ELEM, -1)) { |
| 6069 | set_insert_node(to_set, iter, 0, LYXP_NODE_ELEM, to_set->used); |
| 6070 | |
| 6071 | /* also add all the children of this node, recursively */ |
| 6072 | rc = moveto_self_add_children_r(iter, 0, LYXP_NODE_ELEM, to_set, dup_check_set, options); |
| 6073 | LY_CHECK_RET(rc); |
| 6074 | } |
| 6075 | } |
| 6076 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6077 | return LY_SUCCESS; |
| 6078 | } |
| 6079 | |
| 6080 | /** |
| 6081 | * @brief Move context @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET |
| 6082 | * (or LYXP_SET_EMPTY). Context position aware. |
| 6083 | * |
| 6084 | * @param[in,out] set Set to use. |
| 6085 | * @param[in] all_desc Whether to go to all descendants ('//') or not ('/'). |
| 6086 | * @param[in] options XPath options. |
| 6087 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6088 | */ |
| 6089 | static LY_ERR |
| 6090 | moveto_self(struct lyxp_set *set, int all_desc, int options) |
| 6091 | { |
| 6092 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6093 | struct lyxp_set ret_set; |
| 6094 | LY_ERR rc; |
| 6095 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6096 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6097 | return LY_SUCCESS; |
| 6098 | } |
| 6099 | |
| 6100 | if (set->type != LYXP_SET_NODE_SET) { |
| 6101 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 6102 | return LY_EVALID; |
| 6103 | } |
| 6104 | |
| 6105 | /* nothing to do */ |
| 6106 | if (!all_desc) { |
| 6107 | return LY_SUCCESS; |
| 6108 | } |
| 6109 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6110 | /* add all the children, they get added recursively */ |
| 6111 | set_init(&ret_set, set); |
| 6112 | for (i = 0; i < set->used; ++i) { |
| 6113 | /* copy the current node to tmp */ |
| 6114 | set_insert_node(&ret_set, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, ret_set.used); |
| 6115 | |
| 6116 | /* do not touch attributes and text nodes */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6117 | 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] | 6118 | continue; |
| 6119 | } |
| 6120 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6121 | /* add all the children */ |
| 6122 | 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] | 6123 | set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6124 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6125 | lyxp_set_free_content(&ret_set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6126 | return rc; |
| 6127 | } |
| 6128 | } |
| 6129 | |
| 6130 | /* use the temporary set as the current one */ |
| 6131 | ret_set.ctx_pos = set->ctx_pos; |
| 6132 | ret_set.ctx_size = set->ctx_size; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6133 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6134 | memcpy(set, &ret_set, sizeof *set); |
| 6135 | |
| 6136 | return LY_SUCCESS; |
| 6137 | } |
| 6138 | |
| 6139 | /** |
| 6140 | * @brief Move context schema @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_SCNODE_SET |
| 6141 | * (or LYXP_SET_EMPTY). |
| 6142 | * |
| 6143 | * @param[in,out] set Set to use. |
| 6144 | * @param[in] all_desc Whether to go to all descendants ('//') or not ('/'). |
| 6145 | * @param[in] options XPath options. |
| 6146 | * @return LY_ERR |
| 6147 | */ |
| 6148 | static LY_ERR |
| 6149 | moveto_scnode_self(struct lyxp_set *set, int all_desc, int options) |
| 6150 | { |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6151 | int getnext_opts; |
| 6152 | uint32_t i, mod_idx; |
| 6153 | const struct lysc_node *iter, *start_parent; |
| 6154 | const struct lys_module *mod; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6155 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6156 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6157 | return LY_SUCCESS; |
| 6158 | } |
| 6159 | |
| 6160 | if (set->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 6161 | 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] | 6162 | return LY_EVALID; |
| 6163 | } |
| 6164 | |
| 6165 | /* nothing to do */ |
| 6166 | if (!all_desc) { |
| 6167 | return LY_SUCCESS; |
| 6168 | } |
| 6169 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6170 | /* getnext opts */ |
| 6171 | getnext_opts = LYS_GETNEXT_NOSTATECHECK; |
| 6172 | if (options & LYXP_SCNODE_OUTPUT) { |
| 6173 | getnext_opts |= LYS_GETNEXT_OUTPUT; |
| 6174 | } |
| 6175 | |
| 6176 | /* 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] | 6177 | for (i = 0; i < set->used; ++i) { |
| 6178 | if (set->val.scnodes[i].in_ctx != 1) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6179 | if (set->val.scnodes[i].in_ctx != -2) { |
| 6180 | continue; |
| 6181 | } |
| 6182 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6183 | /* remember context node */ |
| 6184 | set->val.scnodes[i].in_ctx = -1; |
| 6185 | } else { |
| 6186 | set->val.scnodes[i].in_ctx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6187 | } |
| 6188 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6189 | start_parent = set->val.scnodes[i].scnode; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6190 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6191 | if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) { |
| 6192 | /* it can actually be in any module, it's all <running> */ |
| 6193 | mod_idx = 0; |
| 6194 | while ((mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) { |
| 6195 | iter = NULL; |
| 6196 | /* module may not be implemented */ |
| 6197 | while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) { |
| 6198 | /* context check */ |
| 6199 | if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) { |
| 6200 | continue; |
| 6201 | } |
| 6202 | |
| 6203 | lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM); |
| 6204 | /* throw away the insert index, we want to consider that node again, recursively */ |
| 6205 | } |
| 6206 | } |
| 6207 | |
| 6208 | } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) { |
| 6209 | iter = NULL; |
| 6210 | while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6211 | /* context check */ |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6212 | 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] | 6213 | continue; |
| 6214 | } |
| 6215 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6216 | lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6217 | } |
| 6218 | } |
| 6219 | } |
| 6220 | |
| 6221 | return LY_SUCCESS; |
| 6222 | } |
| 6223 | |
| 6224 | /** |
| 6225 | * @brief Move context @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_NODE_SET |
| 6226 | * (or LYXP_SET_EMPTY). Context position aware. |
| 6227 | * |
| 6228 | * @param[in] set Set to use. |
| 6229 | * @param[in] all_desc Whether to go to all descendants ('//') or not ('/'). |
| 6230 | * @param[in] options XPath options. |
| 6231 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6232 | */ |
| 6233 | static LY_ERR |
| 6234 | moveto_parent(struct lyxp_set *set, int all_desc, int options) |
| 6235 | { |
| 6236 | LY_ERR rc; |
| 6237 | uint32_t i; |
| 6238 | struct lyd_node *node, *new_node; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6239 | enum lyxp_node_type new_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6240 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6241 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6242 | return LY_SUCCESS; |
| 6243 | } |
| 6244 | |
| 6245 | if (set->type != LYXP_SET_NODE_SET) { |
| 6246 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 6247 | return LY_EVALID; |
| 6248 | } |
| 6249 | |
| 6250 | if (all_desc) { |
| 6251 | /* <path>//.. == <path>//./.. */ |
| 6252 | rc = moveto_self(set, 1, options); |
| 6253 | LY_CHECK_RET(rc); |
| 6254 | } |
| 6255 | |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 6256 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6257 | node = set->val.nodes[i].node; |
| 6258 | |
| 6259 | if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
| 6260 | new_node = (struct lyd_node *)node->parent; |
| 6261 | } else if (set->val.nodes[i].type == LYXP_NODE_TEXT) { |
| 6262 | new_node = node; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6263 | } else if (set->val.nodes[i].type == LYXP_NODE_META) { |
| 6264 | new_node = set->val.meta[i].meta->parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6265 | if (!new_node) { |
| 6266 | LOGINT_RET(set->ctx); |
| 6267 | } |
| 6268 | } else { |
| 6269 | /* root does not have a parent */ |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6270 | set_remove_node_none(set, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6271 | continue; |
| 6272 | } |
| 6273 | |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 6274 | /* when check */ |
| 6275 | if (moveto_when_check(new_node)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6276 | return LY_EINCOMPLETE; |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 6277 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6278 | |
| 6279 | /* node already there can also be the root */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6280 | if (!new_node) { |
| 6281 | new_type = set->root_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6282 | |
| 6283 | /* node has a standard parent (it can equal the root, it's not the root yet since they are fake) */ |
| 6284 | } else { |
| 6285 | new_type = LYXP_NODE_ELEM; |
| 6286 | } |
| 6287 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6288 | if (set_dup_node_check(set, new_node, new_type, -1)) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6289 | set_remove_node_none(set, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6290 | } else { |
| 6291 | set_replace_node(set, new_node, 0, new_type, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6292 | } |
| 6293 | } |
| 6294 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6295 | set_remove_nodes_none(set); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6296 | assert(!set_sort(set) && !set_sorted_dup_node_clean(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6297 | |
| 6298 | return LY_SUCCESS; |
| 6299 | } |
| 6300 | |
| 6301 | /** |
| 6302 | * @brief Move context schema @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_SCNODE_SET |
| 6303 | * (or LYXP_SET_EMPTY). |
| 6304 | * |
| 6305 | * @param[in] set Set to use. |
| 6306 | * @param[in] all_desc Whether to go to all descendants ('//') or not ('/'). |
| 6307 | * @param[in] options XPath options. |
| 6308 | * @return LY_ERR |
| 6309 | */ |
| 6310 | static LY_ERR |
| 6311 | moveto_scnode_parent(struct lyxp_set *set, int all_desc, int options) |
| 6312 | { |
| 6313 | int idx, i, orig_used, temp_ctx = 0; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6314 | const struct lysc_node *node, *new_node; |
| 6315 | enum lyxp_node_type new_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6316 | LY_ERR rc; |
| 6317 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6318 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6319 | return LY_SUCCESS; |
| 6320 | } |
| 6321 | |
| 6322 | if (set->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 6323 | 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] | 6324 | return LY_EVALID; |
| 6325 | } |
| 6326 | |
| 6327 | if (all_desc) { |
| 6328 | /* <path>//.. == <path>//./.. */ |
| 6329 | rc = moveto_scnode_self(set, 1, options); |
| 6330 | LY_CHECK_RET(rc); |
| 6331 | } |
| 6332 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6333 | orig_used = set->used; |
| 6334 | for (i = 0; i < orig_used; ++i) { |
| 6335 | if (set->val.scnodes[i].in_ctx != 1) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6336 | if (set->val.scnodes[i].in_ctx != -2) { |
| 6337 | continue; |
| 6338 | } |
| 6339 | |
| 6340 | /* remember context node */ |
| 6341 | set->val.scnodes[i].in_ctx = -1; |
Michal Vasko | ec4df48 | 2019-12-16 10:02:18 +0100 | [diff] [blame] | 6342 | } else { |
| 6343 | set->val.scnodes[i].in_ctx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6344 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6345 | |
| 6346 | node = set->val.scnodes[i].scnode; |
| 6347 | |
| 6348 | if (set->val.scnodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6349 | new_node = lysc_data_parent(node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6350 | } else { |
| 6351 | /* root does not have a parent */ |
| 6352 | continue; |
| 6353 | } |
| 6354 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6355 | /* node has no parent */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6356 | if (!new_node) { |
| 6357 | new_type = set->root_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6358 | |
| 6359 | /* node has a standard parent (it can equal the root, it's not the root yet since they are fake) */ |
| 6360 | } else { |
| 6361 | new_type = LYXP_NODE_ELEM; |
| 6362 | } |
| 6363 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6364 | idx = lyxp_set_scnode_insert_node(set, new_node, new_type); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6365 | if ((idx < orig_used) && (idx > i)) { |
| 6366 | set->val.scnodes[idx].in_ctx = 2; |
| 6367 | temp_ctx = 1; |
| 6368 | } |
| 6369 | } |
| 6370 | |
| 6371 | if (temp_ctx) { |
| 6372 | for (i = 0; i < orig_used; ++i) { |
| 6373 | if (set->val.scnodes[i].in_ctx == 2) { |
| 6374 | set->val.scnodes[i].in_ctx = 1; |
| 6375 | } |
| 6376 | } |
| 6377 | } |
| 6378 | |
| 6379 | return LY_SUCCESS; |
| 6380 | } |
| 6381 | |
| 6382 | /** |
| 6383 | * @brief Move context @p set to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'. |
| 6384 | * Result is LYXP_SET_BOOLEAN. Indirectly context position aware. |
| 6385 | * |
| 6386 | * @param[in,out] set1 Set to use for the result. |
| 6387 | * @param[in] set2 Set acting as the second operand for @p op. |
| 6388 | * @param[in] op Comparison operator to process. |
| 6389 | * @param[in] options XPath options. |
| 6390 | * @return LY_ERR |
| 6391 | */ |
| 6392 | static LY_ERR |
| 6393 | moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op, int options) |
| 6394 | { |
| 6395 | /* |
| 6396 | * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING |
| 6397 | * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING) |
| 6398 | * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 6399 | * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 6400 | * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING |
| 6401 | * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6402 | * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 6403 | * |
| 6404 | * '=' or '!=' |
| 6405 | * BOOLEAN + BOOLEAN |
| 6406 | * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 6407 | * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 6408 | * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 6409 | * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 6410 | * NUMBER + NUMBER |
| 6411 | * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6412 | * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 6413 | * STRING + STRING |
| 6414 | * |
| 6415 | * '<=', '<', '>=', '>' |
| 6416 | * NUMBER + NUMBER |
| 6417 | * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 6418 | * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 6419 | * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 6420 | * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6421 | * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 6422 | * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 6423 | * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6424 | * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6425 | */ |
| 6426 | struct lyxp_set iter1, iter2; |
| 6427 | int result; |
| 6428 | int64_t i; |
| 6429 | LY_ERR rc; |
| 6430 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6431 | memset(&iter1, 0, sizeof iter1); |
| 6432 | memset(&iter2, 0, sizeof iter2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6433 | |
| 6434 | /* iterative evaluation with node-sets */ |
| 6435 | if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) { |
| 6436 | if (set1->type == LYXP_SET_NODE_SET) { |
| 6437 | for (i = 0; i < set1->used; ++i) { |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6438 | /* cast set1 */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6439 | switch (set2->type) { |
| 6440 | case LYXP_SET_NUMBER: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6441 | rc = set_comp_cast(&iter1, set1, LYXP_SET_NUMBER, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6442 | break; |
| 6443 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6444 | rc = set_comp_cast(&iter1, set1, LYXP_SET_BOOLEAN, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6445 | break; |
| 6446 | default: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6447 | rc = set_comp_cast(&iter1, set1, LYXP_SET_STRING, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6448 | break; |
| 6449 | } |
| 6450 | LY_CHECK_RET(rc); |
| 6451 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6452 | /* canonize set2 */ |
| 6453 | LY_CHECK_ERR_RET(rc = set_comp_canonize(&iter2, set2, &set1->val.nodes[i]), lyxp_set_free_content(&iter1), rc); |
| 6454 | |
| 6455 | /* compare recursively */ |
| 6456 | rc = moveto_op_comp(&iter1, &iter2, op, options); |
| 6457 | lyxp_set_free_content(&iter2); |
| 6458 | LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6459 | |
| 6460 | /* lazy evaluation until true */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6461 | if (iter1.val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6462 | set_fill_boolean(set1, 1); |
| 6463 | return LY_SUCCESS; |
| 6464 | } |
| 6465 | } |
| 6466 | } else { |
| 6467 | for (i = 0; i < set2->used; ++i) { |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6468 | /* set set2 */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6469 | switch (set1->type) { |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6470 | case LYXP_SET_NUMBER: |
| 6471 | rc = set_comp_cast(&iter2, set2, LYXP_SET_NUMBER, i); |
| 6472 | break; |
| 6473 | case LYXP_SET_BOOLEAN: |
| 6474 | rc = set_comp_cast(&iter2, set2, LYXP_SET_BOOLEAN, i); |
| 6475 | break; |
| 6476 | default: |
| 6477 | rc = set_comp_cast(&iter2, set2, LYXP_SET_STRING, i); |
| 6478 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6479 | } |
| 6480 | LY_CHECK_RET(rc); |
| 6481 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6482 | /* canonize set1 */ |
| 6483 | 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] | 6484 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6485 | /* compare recursively */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6486 | rc = moveto_op_comp(&iter1, &iter2, op, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6487 | lyxp_set_free_content(&iter2); |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6488 | LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6489 | |
| 6490 | /* lazy evaluation until true */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6491 | if (iter1.val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6492 | set_fill_boolean(set1, 1); |
| 6493 | return LY_SUCCESS; |
| 6494 | } |
| 6495 | } |
| 6496 | } |
| 6497 | |
| 6498 | /* false for all nodes */ |
| 6499 | set_fill_boolean(set1, 0); |
| 6500 | return LY_SUCCESS; |
| 6501 | } |
| 6502 | |
| 6503 | /* first convert properly */ |
| 6504 | if ((op[0] == '=') || (op[0] == '!')) { |
| 6505 | if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6506 | lyxp_set_cast(set1, LYXP_SET_BOOLEAN); |
| 6507 | lyxp_set_cast(set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6508 | } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6509 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6510 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6511 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6512 | LY_CHECK_RET(rc); |
| 6513 | } /* else we have 2 strings */ |
| 6514 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6515 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6516 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6517 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6518 | LY_CHECK_RET(rc); |
| 6519 | } |
| 6520 | |
| 6521 | assert(set1->type == set2->type); |
| 6522 | |
| 6523 | /* compute result */ |
| 6524 | if (op[0] == '=') { |
| 6525 | if (set1->type == LYXP_SET_BOOLEAN) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6526 | result = (set1->val.bln == set2->val.bln); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6527 | } else if (set1->type == LYXP_SET_NUMBER) { |
| 6528 | result = (set1->val.num == set2->val.num); |
| 6529 | } else { |
| 6530 | assert(set1->type == LYXP_SET_STRING); |
Michal Vasko | ac6c72f | 2019-11-14 16:09:34 +0100 | [diff] [blame] | 6531 | result = !strcmp(set1->val.str, set2->val.str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6532 | } |
| 6533 | } else if (op[0] == '!') { |
| 6534 | if (set1->type == LYXP_SET_BOOLEAN) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6535 | result = (set1->val.bln != set2->val.bln); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6536 | } else if (set1->type == LYXP_SET_NUMBER) { |
| 6537 | result = (set1->val.num != set2->val.num); |
| 6538 | } else { |
| 6539 | assert(set1->type == LYXP_SET_STRING); |
Michal Vasko | ac6c72f | 2019-11-14 16:09:34 +0100 | [diff] [blame] | 6540 | result = !strcmp(set1->val.str, set2->val.str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6541 | } |
| 6542 | } else { |
| 6543 | assert(set1->type == LYXP_SET_NUMBER); |
| 6544 | if (op[0] == '<') { |
| 6545 | if (op[1] == '=') { |
| 6546 | result = (set1->val.num <= set2->val.num); |
| 6547 | } else { |
| 6548 | result = (set1->val.num < set2->val.num); |
| 6549 | } |
| 6550 | } else { |
| 6551 | if (op[1] == '=') { |
| 6552 | result = (set1->val.num >= set2->val.num); |
| 6553 | } else { |
| 6554 | result = (set1->val.num > set2->val.num); |
| 6555 | } |
| 6556 | } |
| 6557 | } |
| 6558 | |
| 6559 | /* assign result */ |
| 6560 | if (result) { |
| 6561 | set_fill_boolean(set1, 1); |
| 6562 | } else { |
| 6563 | set_fill_boolean(set1, 0); |
| 6564 | } |
| 6565 | |
| 6566 | return LY_SUCCESS; |
| 6567 | } |
| 6568 | |
| 6569 | /** |
| 6570 | * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div', |
| 6571 | * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware. |
| 6572 | * |
| 6573 | * @param[in,out] set1 Set to use for the result. |
| 6574 | * @param[in] set2 Set acting as the second operand for @p op. |
| 6575 | * @param[in] op Operator to process. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6576 | * @return LY_ERR |
| 6577 | */ |
| 6578 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6579 | 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] | 6580 | { |
| 6581 | LY_ERR rc; |
| 6582 | |
| 6583 | /* unary '-' */ |
| 6584 | if (!set2 && (op[0] == '-')) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6585 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6586 | LY_CHECK_RET(rc); |
| 6587 | set1->val.num *= -1; |
| 6588 | lyxp_set_free(set2); |
| 6589 | return LY_SUCCESS; |
| 6590 | } |
| 6591 | |
| 6592 | assert(set1 && set2); |
| 6593 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6594 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6595 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6596 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6597 | LY_CHECK_RET(rc); |
| 6598 | |
| 6599 | switch (op[0]) { |
| 6600 | /* '+' */ |
| 6601 | case '+': |
| 6602 | set1->val.num += set2->val.num; |
| 6603 | break; |
| 6604 | |
| 6605 | /* '-' */ |
| 6606 | case '-': |
| 6607 | set1->val.num -= set2->val.num; |
| 6608 | break; |
| 6609 | |
| 6610 | /* '*' */ |
| 6611 | case '*': |
| 6612 | set1->val.num *= set2->val.num; |
| 6613 | break; |
| 6614 | |
| 6615 | /* 'div' */ |
| 6616 | case 'd': |
| 6617 | set1->val.num /= set2->val.num; |
| 6618 | break; |
| 6619 | |
| 6620 | /* 'mod' */ |
| 6621 | case 'm': |
| 6622 | set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num); |
| 6623 | break; |
| 6624 | |
| 6625 | default: |
| 6626 | LOGINT_RET(set1->ctx); |
| 6627 | } |
| 6628 | |
| 6629 | return LY_SUCCESS; |
| 6630 | } |
| 6631 | |
| 6632 | /* |
| 6633 | * eval functions |
| 6634 | * |
| 6635 | * They execute a parsed XPath expression on some data subtree. |
| 6636 | */ |
| 6637 | |
| 6638 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6639 | * @brief Evaluate Predicate. Logs directly on error. |
| 6640 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6641 | * [9] Predicate ::= '[' Expr ']' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6642 | * |
| 6643 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6644 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6645 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 6646 | * @param[in] options XPath options. |
| 6647 | * @param[in] parent_pos_pred Whether parent predicate was a positional one. |
| 6648 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6649 | */ |
| 6650 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6651 | 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] | 6652 | { |
| 6653 | LY_ERR rc; |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 6654 | uint16_t i, orig_exp; |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6655 | uint32_t orig_pos, orig_size; |
| 6656 | int32_t pred_in_ctx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6657 | struct lyxp_set set2; |
| 6658 | struct lyd_node *orig_parent; |
| 6659 | |
| 6660 | /* '[' */ |
| 6661 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6662 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 6663 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6664 | |
| 6665 | if (!set) { |
| 6666 | only_parse: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6667 | rc = eval_expr_select(exp, tok_idx, 0, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6668 | LY_CHECK_RET(rc); |
| 6669 | } else if (set->type == LYXP_SET_NODE_SET) { |
| 6670 | /* 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] | 6671 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6672 | |
| 6673 | /* empty set, nothing to evaluate */ |
| 6674 | if (!set->used) { |
| 6675 | goto only_parse; |
| 6676 | } |
| 6677 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6678 | orig_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6679 | orig_pos = 0; |
| 6680 | orig_size = set->used; |
| 6681 | orig_parent = NULL; |
Michal Vasko | 39dbf35 | 2020-05-21 10:08:59 +0200 | [diff] [blame] | 6682 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6683 | set_init(&set2, set); |
| 6684 | set_insert_node(&set2, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, 0); |
| 6685 | /* remember the node context position for position() and context size for last(), |
| 6686 | * predicates should always be evaluated with respect to the child axis (since we do |
| 6687 | * not support explicit axes) so we assign positions based on their parents */ |
| 6688 | if (parent_pos_pred && ((struct lyd_node *)set->val.nodes[i].node->parent != orig_parent)) { |
| 6689 | orig_parent = (struct lyd_node *)set->val.nodes[i].node->parent; |
| 6690 | orig_pos = 1; |
| 6691 | } else { |
| 6692 | ++orig_pos; |
| 6693 | } |
| 6694 | |
| 6695 | set2.ctx_pos = orig_pos; |
| 6696 | set2.ctx_size = orig_size; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6697 | *tok_idx = orig_exp; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6698 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6699 | rc = eval_expr_select(exp, tok_idx, 0, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6700 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6701 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6702 | return rc; |
| 6703 | } |
| 6704 | |
| 6705 | /* number is a position */ |
| 6706 | if (set2.type == LYXP_SET_NUMBER) { |
| 6707 | if ((long long)set2.val.num == orig_pos) { |
| 6708 | set2.val.num = 1; |
| 6709 | } else { |
| 6710 | set2.val.num = 0; |
| 6711 | } |
| 6712 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6713 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6714 | |
| 6715 | /* predicate satisfied or not? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6716 | if (!set2.val.bln) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6717 | set_remove_node_none(set, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6718 | } |
| 6719 | } |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6720 | set_remove_nodes_none(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6721 | |
| 6722 | } else if (set->type == LYXP_SET_SCNODE_SET) { |
| 6723 | for (i = 0; i < set->used; ++i) { |
| 6724 | if (set->val.scnodes[i].in_ctx == 1) { |
| 6725 | /* there is a currently-valid node */ |
| 6726 | break; |
| 6727 | } |
| 6728 | } |
| 6729 | /* empty set, nothing to evaluate */ |
| 6730 | if (i == set->used) { |
| 6731 | goto only_parse; |
| 6732 | } |
| 6733 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6734 | orig_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6735 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6736 | /* set special in_ctx to all the valid snodes */ |
| 6737 | pred_in_ctx = set_scnode_new_in_ctx(set); |
| 6738 | |
| 6739 | /* use the valid snodes one-by-one */ |
| 6740 | for (i = 0; i < set->used; ++i) { |
| 6741 | if (set->val.scnodes[i].in_ctx != pred_in_ctx) { |
| 6742 | continue; |
| 6743 | } |
| 6744 | set->val.scnodes[i].in_ctx = 1; |
| 6745 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6746 | *tok_idx = orig_exp; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6747 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6748 | rc = eval_expr_select(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6749 | LY_CHECK_RET(rc); |
| 6750 | |
| 6751 | set->val.scnodes[i].in_ctx = pred_in_ctx; |
| 6752 | } |
| 6753 | |
| 6754 | /* restore the state as it was before the predicate */ |
| 6755 | for (i = 0; i < set->used; ++i) { |
| 6756 | if (set->val.scnodes[i].in_ctx == 1) { |
| 6757 | set->val.scnodes[i].in_ctx = 0; |
| 6758 | } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) { |
| 6759 | set->val.scnodes[i].in_ctx = 1; |
| 6760 | } |
| 6761 | } |
| 6762 | |
| 6763 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6764 | set2.type = LYXP_SET_NODE_SET; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6765 | set_fill_set(&set2, set); |
| 6766 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6767 | rc = eval_expr_select(exp, tok_idx, 0, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6768 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6769 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6770 | return rc; |
| 6771 | } |
| 6772 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6773 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6774 | if (!set2.val.bln) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6775 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6776 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6777 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6778 | } |
| 6779 | |
| 6780 | /* ']' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6781 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6782 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6783 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 6784 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6785 | |
| 6786 | return LY_SUCCESS; |
| 6787 | } |
| 6788 | |
| 6789 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6790 | * @brief Evaluate Literal. Logs directly on error. |
| 6791 | * |
| 6792 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6793 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6794 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 6795 | */ |
| 6796 | static void |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6797 | 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] | 6798 | { |
| 6799 | if (set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6800 | if (exp->tok_len[*tok_idx] == 2) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6801 | set_fill_string(set, "", 0); |
| 6802 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6803 | 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] | 6804 | } |
| 6805 | } |
| 6806 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6807 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 6808 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6809 | } |
| 6810 | |
| 6811 | /** |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6812 | * @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] | 6813 | * |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6814 | * @param[in] exp Full parsed XPath expression. |
| 6815 | * @param[in,out] tok_idx Index in @p exp at the beginning of the predicate, is updated on success. |
| 6816 | * @param[in] scnode Found schema node as context for the predicate. |
| 6817 | * @param[in] format Format of any prefixes in key names/values. |
| 6818 | * @param[out] predicates Parsed predicates. |
| 6819 | * @param[out] pred_type Type of @p predicates. |
| 6820 | * @return LY_SUCCESS on success, |
| 6821 | * @return LY_ERR on any error. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6822 | */ |
| 6823 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6824 | eval_name_test_try_compile_predicates(struct lyxp_expr *exp, uint16_t *tok_idx, const struct lysc_node *scnode, |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 6825 | LY_PREFIX_FORMAT format, struct ly_path_predicate **predicates, |
| 6826 | enum ly_path_pred_type *pred_type) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6827 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6828 | LY_ERR ret = LY_SUCCESS; |
| 6829 | uint16_t key_count, e_idx, pred_idx = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6830 | const struct lysc_node *key; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6831 | size_t pred_len; |
| 6832 | int prev_lo; |
| 6833 | struct lyxp_expr *exp2 = NULL; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6834 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6835 | assert(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6836 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6837 | if (scnode->nodetype == LYS_LIST) { |
| 6838 | /* get key count */ |
| 6839 | if (scnode->flags & LYS_KEYLESS) { |
| 6840 | return LY_EINVAL; |
| 6841 | } |
| 6842 | for (key_count = 0, key = lysc_node_children(scnode, 0); key && (key->flags & LYS_KEY); key = key->next, ++key_count); |
| 6843 | assert(key_count); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6844 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6845 | /* learn where the predicates end */ |
| 6846 | e_idx = *tok_idx; |
| 6847 | while (key_count) { |
| 6848 | /* '[' */ |
| 6849 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) { |
| 6850 | return LY_EINVAL; |
| 6851 | } |
| 6852 | ++e_idx; |
| 6853 | |
| 6854 | /* ']' */ |
| 6855 | while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) { |
| 6856 | ++e_idx; |
| 6857 | } |
| 6858 | ++e_idx; |
| 6859 | |
| 6860 | /* another presumably key predicate parsed */ |
| 6861 | --key_count; |
| 6862 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6863 | } else { |
| 6864 | /* learn just where this single predicate ends */ |
| 6865 | e_idx = *tok_idx; |
| 6866 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6867 | /* '[' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6868 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) { |
| 6869 | return LY_EINVAL; |
| 6870 | } |
| 6871 | ++e_idx; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6872 | |
| 6873 | /* ']' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6874 | while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) { |
| 6875 | ++e_idx; |
| 6876 | } |
| 6877 | ++e_idx; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6878 | } |
| 6879 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6880 | /* get the joined length of all the keys predicates/of the single leaf-list predicate */ |
| 6881 | pred_len = (exp->tok_pos[e_idx - 1] + exp->tok_len[e_idx - 1]) - exp->tok_pos[*tok_idx]; |
| 6882 | |
| 6883 | /* turn logging off */ |
| 6884 | prev_lo = ly_log_options(0); |
| 6885 | |
| 6886 | /* parse the predicate(s) */ |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 6887 | 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] | 6888 | LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp2), cleanup); |
| 6889 | |
| 6890 | /* compile */ |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 6891 | ret = ly_path_compile_predicate(scnode->module->ctx, scnode, scnode->module, scnode, exp2, &pred_idx, |
| 6892 | format, scnode->module, predicates, pred_type); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6893 | LY_CHECK_GOTO(ret, cleanup); |
| 6894 | |
| 6895 | /* success, the predicate must include all the needed information for hash-based search */ |
| 6896 | *tok_idx = e_idx; |
| 6897 | |
| 6898 | cleanup: |
| 6899 | ly_log_options(prev_lo); |
| 6900 | lyxp_expr_free(scnode->module->ctx, exp2); |
| 6901 | return ret; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6902 | } |
| 6903 | |
| 6904 | /** |
| 6905 | * @brief Evaluate NameTest and any following Predicates. Logs directly on error. |
| 6906 | * |
| 6907 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 6908 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 6909 | * [7] NameTest ::= '*' | NCName ':' '*' | QName |
| 6910 | * |
| 6911 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6912 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6913 | * @param[in] attr_axis Whether to search attributes or standard nodes. |
| 6914 | * @param[in] all_desc Whether to search all the descendants or children only. |
| 6915 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 6916 | * @param[in] options XPath options. |
| 6917 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6918 | */ |
| 6919 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6920 | 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] | 6921 | int options) |
| 6922 | { |
| 6923 | int i; |
| 6924 | char *path; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6925 | const char *ncname, *ncname_dict = NULL; |
| 6926 | uint16_t ncname_len; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6927 | const struct lys_module *moveto_mod; |
| 6928 | const struct lysc_node *scnode = NULL, *tmp; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6929 | struct ly_path_predicate *predicates = NULL; |
| 6930 | enum ly_path_pred_type pred_type = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6931 | LY_ERR rc = LY_SUCCESS; |
| 6932 | |
| 6933 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6934 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 6935 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6936 | |
| 6937 | if (!set) { |
| 6938 | goto moveto; |
| 6939 | } |
| 6940 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6941 | ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]]; |
| 6942 | ncname_len = exp->tok_len[*tok_idx - 1]; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6943 | |
| 6944 | /* parse (and skip) module name */ |
| 6945 | rc = moveto_resolve_model(&ncname, &ncname_len, set, &moveto_mod); |
| 6946 | LY_CHECK_GOTO(rc, cleanup); |
| 6947 | |
| 6948 | if (moveto_mod && !attr_axis && !all_desc && (set->type == LYXP_SET_NODE_SET)) { |
| 6949 | /* find the matching schema node in some parent in the context */ |
| 6950 | for (i = 0; i < (signed)set->used; ++i) { |
| 6951 | if (set->val.nodes[i].type == set->root_type) { |
| 6952 | tmp = lys_find_child(NULL, moveto_mod, ncname, ncname_len, 0, 0); |
| 6953 | } else if ((set->val.nodes[i].type == LYXP_NODE_ELEM) |
| 6954 | && (!scnode || (lysc_data_parent(scnode) != set->val.nodes[i].node->schema))) { |
| 6955 | /* do not repeat the same search */ |
| 6956 | tmp = lys_find_child(set->val.nodes[i].node->schema, moveto_mod, ncname, ncname_len, 0, 0); |
Radek Krejci | b124784 | 2020-07-02 16:22:38 +0200 | [diff] [blame] | 6957 | } else { |
| 6958 | tmp = NULL; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6959 | } |
| 6960 | |
| 6961 | /* additional context check */ |
| 6962 | if (tmp && (set->root_type == LYXP_NODE_ROOT_CONFIG) && (tmp->flags & LYS_CONFIG_R)) { |
| 6963 | tmp = NULL; |
| 6964 | } |
| 6965 | |
| 6966 | if (tmp) { |
| 6967 | if (scnode) { |
| 6968 | /* we found a schema node with the same name but at different level, give up, too complicated */ |
| 6969 | scnode = NULL; |
| 6970 | break; |
| 6971 | } else { |
| 6972 | /* remember the found schema node and continue to make sure it can be used */ |
| 6973 | scnode = tmp; |
| 6974 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6975 | } |
| 6976 | } |
| 6977 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6978 | if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) { |
| 6979 | /* try to create the predicates */ |
| 6980 | if (eval_name_test_try_compile_predicates(exp, tok_idx, scnode, set->format, &predicates, &pred_type)) { |
| 6981 | /* hashes cannot be used */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6982 | scnode = NULL; |
| 6983 | } |
| 6984 | } |
| 6985 | } |
| 6986 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6987 | if (!scnode && moveto_mod) { |
| 6988 | /* we are not able to match based on a schema node and not all the modules match, |
| 6989 | * use dictionary for efficient comparison */ |
| 6990 | ncname_dict = lydict_insert(set->ctx, ncname, ncname_len); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6991 | } |
| 6992 | |
| 6993 | moveto: |
| 6994 | /* move to the attribute(s), data node(s), or schema node(s) */ |
| 6995 | if (attr_axis) { |
| 6996 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 6997 | set_scnode_clear_ctx(set); |
| 6998 | } else { |
| 6999 | if (all_desc) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7000 | rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7001 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7002 | rc = moveto_attr(set, moveto_mod, ncname_dict); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7003 | } |
| 7004 | LY_CHECK_GOTO(rc, cleanup); |
| 7005 | } |
| 7006 | } else { |
| 7007 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 7008 | if (all_desc) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7009 | rc = moveto_scnode_alldesc(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7010 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7011 | rc = moveto_scnode(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7012 | } |
| 7013 | LY_CHECK_GOTO(rc, cleanup); |
| 7014 | |
| 7015 | for (i = set->used - 1; i > -1; --i) { |
| 7016 | if (set->val.scnodes[i].in_ctx > 0) { |
| 7017 | break; |
| 7018 | } |
| 7019 | } |
| 7020 | if (i == -1) { |
| 7021 | path = lysc_path(set->ctx_scnode, LYSC_PATH_LOG, NULL, 0); |
| 7022 | 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] | 7023 | ncname_len, ncname, ncname - exp->expr, exp->expr, path); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7024 | free(path); |
| 7025 | } |
| 7026 | } else { |
| 7027 | if (all_desc) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7028 | rc = moveto_node_alldesc(set, moveto_mod, ncname_dict); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7029 | } else { |
| 7030 | if (scnode) { |
| 7031 | /* we can find the nodes using hashes */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7032 | rc = moveto_node_hash(set, scnode, predicates); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7033 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7034 | rc = moveto_node(set, moveto_mod, ncname_dict); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7035 | } |
| 7036 | } |
| 7037 | LY_CHECK_GOTO(rc, cleanup); |
| 7038 | } |
| 7039 | } |
| 7040 | |
| 7041 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7042 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 7043 | rc = eval_predicate(exp, tok_idx, set, options, 1); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7044 | LY_CHECK_RET(rc); |
| 7045 | } |
| 7046 | |
| 7047 | cleanup: |
Michal Vasko | db51a8d | 2020-05-27 15:22:29 +0200 | [diff] [blame] | 7048 | if (set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7049 | lydict_remove(set->ctx, ncname_dict); |
| 7050 | ly_path_predicates_free(set->ctx, pred_type, scnode, predicates); |
Michal Vasko | db51a8d | 2020-05-27 15:22:29 +0200 | [diff] [blame] | 7051 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7052 | return rc; |
| 7053 | } |
| 7054 | |
| 7055 | /** |
| 7056 | * @brief Evaluate NodeType and any following Predicates. Logs directly on error. |
| 7057 | * |
| 7058 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 7059 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 7060 | * [8] NodeType ::= 'text' | 'node' |
| 7061 | * |
| 7062 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7063 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7064 | * @param[in] attr_axis Whether to search attributes or standard nodes. |
| 7065 | * @param[in] all_desc Whether to search all the descendants or children only. |
| 7066 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7067 | * @param[in] options XPath options. |
| 7068 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7069 | */ |
| 7070 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7071 | 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] | 7072 | struct lyxp_set *set, int options) |
| 7073 | { |
| 7074 | LY_ERR rc; |
| 7075 | |
| 7076 | /* TODO */ |
| 7077 | (void)attr_axis; |
| 7078 | (void)all_desc; |
| 7079 | |
| 7080 | if (set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7081 | assert(exp->tok_len[*tok_idx] == 4); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7082 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 7083 | set_scnode_clear_ctx(set); |
| 7084 | /* just for the debug message below */ |
| 7085 | set = NULL; |
| 7086 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7087 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7088 | rc = xpath_node(NULL, 0, set, options); |
| 7089 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7090 | assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4)); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7091 | rc = xpath_text(NULL, 0, set, options); |
| 7092 | } |
| 7093 | LY_CHECK_RET(rc); |
| 7094 | } |
| 7095 | } |
| 7096 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7097 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7098 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7099 | |
| 7100 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7101 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7102 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7103 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7104 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7105 | |
| 7106 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7107 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7108 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7109 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7110 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7111 | |
| 7112 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7113 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 7114 | rc = eval_predicate(exp, tok_idx, set, options, 1); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7115 | LY_CHECK_RET(rc); |
| 7116 | } |
| 7117 | |
| 7118 | return LY_SUCCESS; |
| 7119 | } |
| 7120 | |
| 7121 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7122 | * @brief Evaluate RelativeLocationPath. Logs directly on error. |
| 7123 | * |
| 7124 | * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step |
| 7125 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7126 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7127 | * |
| 7128 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7129 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7130 | * @param[in] all_desc Whether to search all the descendants or children only. |
| 7131 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7132 | * @param[in] options XPath options. |
| 7133 | * @return LY_ERR (YL_EINCOMPLETE on unresolved when) |
| 7134 | */ |
| 7135 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7136 | 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] | 7137 | { |
| 7138 | int attr_axis; |
| 7139 | LY_ERR rc; |
| 7140 | |
| 7141 | goto step; |
| 7142 | do { |
| 7143 | /* evaluate '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7144 | if (exp->tok_len[*tok_idx] == 1) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7145 | all_desc = 0; |
| 7146 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7147 | assert(exp->tok_len[*tok_idx] == 2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7148 | all_desc = 1; |
| 7149 | } |
| 7150 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7151 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7152 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7153 | |
| 7154 | step: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7155 | /* evaluate abbreviated axis '@'? if any */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7156 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7157 | attr_axis = 1; |
| 7158 | |
| 7159 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7160 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7161 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7162 | } else { |
| 7163 | attr_axis = 0; |
| 7164 | } |
| 7165 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7166 | /* Step */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7167 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7168 | case LYXP_TOKEN_DOT: |
| 7169 | /* evaluate '.' */ |
| 7170 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 7171 | rc = moveto_scnode_self(set, all_desc, options); |
| 7172 | } else { |
| 7173 | rc = moveto_self(set, all_desc, options); |
| 7174 | } |
| 7175 | LY_CHECK_RET(rc); |
| 7176 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7177 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7178 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7179 | break; |
| 7180 | |
| 7181 | case LYXP_TOKEN_DDOT: |
| 7182 | /* evaluate '..' */ |
| 7183 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 7184 | rc = moveto_scnode_parent(set, all_desc, options); |
| 7185 | } else { |
| 7186 | rc = moveto_parent(set, all_desc, options); |
| 7187 | } |
| 7188 | LY_CHECK_RET(rc); |
| 7189 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7190 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7191 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7192 | break; |
| 7193 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7194 | case LYXP_TOKEN_NAMETEST: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7195 | /* evaluate NameTest Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7196 | 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] | 7197 | LY_CHECK_RET(rc); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7198 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7199 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7200 | case LYXP_TOKEN_NODETYPE: |
| 7201 | /* evaluate NodeType Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7202 | 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] | 7203 | LY_CHECK_RET(rc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7204 | break; |
| 7205 | |
| 7206 | default: |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 7207 | LOGINT_RET(set ? set->ctx : NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7208 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7209 | } 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] | 7210 | |
| 7211 | return LY_SUCCESS; |
| 7212 | } |
| 7213 | |
| 7214 | /** |
| 7215 | * @brief Evaluate AbsoluteLocationPath. Logs directly on error. |
| 7216 | * |
| 7217 | * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath |
| 7218 | * |
| 7219 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7220 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7221 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7222 | * @param[in] options XPath options. |
| 7223 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7224 | */ |
| 7225 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7226 | 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] | 7227 | { |
| 7228 | int all_desc; |
| 7229 | LY_ERR rc; |
| 7230 | |
| 7231 | if (set) { |
| 7232 | /* no matter what tokens follow, we need to be at the root */ |
| 7233 | moveto_root(set, options); |
| 7234 | } |
| 7235 | |
| 7236 | /* '/' RelativeLocationPath? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7237 | if (exp->tok_len[*tok_idx] == 1) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7238 | /* evaluate '/' - deferred */ |
| 7239 | all_desc = 0; |
| 7240 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7241 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7242 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7243 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7244 | if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7245 | return LY_SUCCESS; |
| 7246 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7247 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7248 | case LYXP_TOKEN_DOT: |
| 7249 | case LYXP_TOKEN_DDOT: |
| 7250 | case LYXP_TOKEN_AT: |
| 7251 | case LYXP_TOKEN_NAMETEST: |
| 7252 | case LYXP_TOKEN_NODETYPE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7253 | rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7254 | LY_CHECK_RET(rc); |
| 7255 | break; |
| 7256 | default: |
| 7257 | break; |
| 7258 | } |
| 7259 | |
| 7260 | /* '//' RelativeLocationPath */ |
| 7261 | } else { |
| 7262 | /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */ |
| 7263 | all_desc = 1; |
| 7264 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7265 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7266 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7267 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7268 | rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7269 | LY_CHECK_RET(rc); |
| 7270 | } |
| 7271 | |
| 7272 | return LY_SUCCESS; |
| 7273 | } |
| 7274 | |
| 7275 | /** |
| 7276 | * @brief Evaluate FunctionCall. Logs directly on error. |
| 7277 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7278 | * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7279 | * |
| 7280 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7281 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7282 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7283 | * @param[in] options XPath options. |
| 7284 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7285 | */ |
| 7286 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7287 | 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] | 7288 | { |
| 7289 | LY_ERR rc; |
| 7290 | 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] | 7291 | uint16_t arg_count = 0, i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7292 | struct lyxp_set **args = NULL, **args_aux; |
| 7293 | |
| 7294 | if (set) { |
| 7295 | /* FunctionName */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7296 | switch (exp->tok_len[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7297 | case 3: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7298 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7299 | xpath_func = &xpath_not; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7300 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7301 | xpath_func = &xpath_sum; |
| 7302 | } |
| 7303 | break; |
| 7304 | case 4: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7305 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7306 | xpath_func = &xpath_lang; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7307 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7308 | xpath_func = &xpath_last; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7309 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7310 | xpath_func = &xpath_name; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7311 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7312 | xpath_func = &xpath_true; |
| 7313 | } |
| 7314 | break; |
| 7315 | case 5: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7316 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7317 | xpath_func = &xpath_count; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7318 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7319 | xpath_func = &xpath_false; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7320 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7321 | xpath_func = &xpath_floor; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7322 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7323 | xpath_func = &xpath_round; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7324 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7325 | xpath_func = &xpath_deref; |
| 7326 | } |
| 7327 | break; |
| 7328 | case 6: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7329 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7330 | xpath_func = &xpath_concat; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7331 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7332 | xpath_func = &xpath_number; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7333 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7334 | xpath_func = &xpath_string; |
| 7335 | } |
| 7336 | break; |
| 7337 | case 7: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7338 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7339 | xpath_func = &xpath_boolean; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7340 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7341 | xpath_func = &xpath_ceiling; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7342 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7343 | xpath_func = &xpath_current; |
| 7344 | } |
| 7345 | break; |
| 7346 | case 8: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7347 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7348 | xpath_func = &xpath_contains; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7349 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7350 | xpath_func = &xpath_position; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7351 | } 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] | 7352 | xpath_func = &xpath_re_match; |
| 7353 | } |
| 7354 | break; |
| 7355 | case 9: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7356 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7357 | xpath_func = &xpath_substring; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7358 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7359 | xpath_func = &xpath_translate; |
| 7360 | } |
| 7361 | break; |
| 7362 | case 10: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7363 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7364 | xpath_func = &xpath_local_name; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7365 | } 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] | 7366 | xpath_func = &xpath_enum_value; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7367 | } 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] | 7368 | xpath_func = &xpath_bit_is_set; |
| 7369 | } |
| 7370 | break; |
| 7371 | case 11: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7372 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7373 | xpath_func = &xpath_starts_with; |
| 7374 | } |
| 7375 | break; |
| 7376 | case 12: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7377 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7378 | xpath_func = &xpath_derived_from; |
| 7379 | } |
| 7380 | break; |
| 7381 | case 13: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7382 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7383 | xpath_func = &xpath_namespace_uri; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7384 | } 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] | 7385 | xpath_func = &xpath_string_length; |
| 7386 | } |
| 7387 | break; |
| 7388 | case 15: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7389 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7390 | xpath_func = &xpath_normalize_space; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7391 | } 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] | 7392 | xpath_func = &xpath_substring_after; |
| 7393 | } |
| 7394 | break; |
| 7395 | case 16: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7396 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7397 | xpath_func = &xpath_substring_before; |
| 7398 | } |
| 7399 | break; |
| 7400 | case 20: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7401 | 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] | 7402 | xpath_func = &xpath_derived_from_or_self; |
| 7403 | } |
| 7404 | break; |
| 7405 | } |
| 7406 | |
| 7407 | if (!xpath_func) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7408 | 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] | 7409 | return LY_EVALID; |
| 7410 | } |
| 7411 | } |
| 7412 | |
| 7413 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7414 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7415 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7416 | |
| 7417 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7418 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7419 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7420 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7421 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7422 | |
| 7423 | /* ( Expr ( ',' Expr )* )? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7424 | if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7425 | if (set) { |
| 7426 | args = malloc(sizeof *args); |
| 7427 | LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup); |
| 7428 | arg_count = 1; |
| 7429 | args[0] = set_copy(set); |
| 7430 | if (!args[0]) { |
| 7431 | rc = LY_EMEM; |
| 7432 | goto cleanup; |
| 7433 | } |
| 7434 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7435 | rc = eval_expr_select(exp, tok_idx, 0, args[0], options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7436 | LY_CHECK_GOTO(rc, cleanup); |
| 7437 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7438 | rc = eval_expr_select(exp, tok_idx, 0, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7439 | LY_CHECK_GOTO(rc, cleanup); |
| 7440 | } |
| 7441 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7442 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7443 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7444 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7445 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7446 | |
| 7447 | if (set) { |
| 7448 | ++arg_count; |
| 7449 | args_aux = realloc(args, arg_count * sizeof *args); |
| 7450 | LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup); |
| 7451 | args = args_aux; |
| 7452 | args[arg_count - 1] = set_copy(set); |
| 7453 | if (!args[arg_count - 1]) { |
| 7454 | rc = LY_EMEM; |
| 7455 | goto cleanup; |
| 7456 | } |
| 7457 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7458 | 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] | 7459 | LY_CHECK_GOTO(rc, cleanup); |
| 7460 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7461 | rc = eval_expr_select(exp, tok_idx, 0, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7462 | LY_CHECK_GOTO(rc, cleanup); |
| 7463 | } |
| 7464 | } |
| 7465 | |
| 7466 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7467 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7468 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7469 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7470 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7471 | |
| 7472 | if (set) { |
| 7473 | /* evaluate function */ |
| 7474 | rc = xpath_func(args, arg_count, set, options); |
| 7475 | |
| 7476 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7477 | /* merge all nodes from arg evaluations */ |
| 7478 | for (i = 0; i < arg_count; ++i) { |
| 7479 | set_scnode_clear_ctx(args[i]); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7480 | lyxp_set_scnode_merge(set, args[i]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7481 | } |
| 7482 | } |
| 7483 | } else { |
| 7484 | rc = LY_SUCCESS; |
| 7485 | } |
| 7486 | |
| 7487 | cleanup: |
| 7488 | for (i = 0; i < arg_count; ++i) { |
| 7489 | lyxp_set_free(args[i]); |
| 7490 | } |
| 7491 | free(args); |
| 7492 | |
| 7493 | return rc; |
| 7494 | } |
| 7495 | |
| 7496 | /** |
| 7497 | * @brief Evaluate Number. Logs directly on error. |
| 7498 | * |
| 7499 | * @param[in] ctx Context for errors. |
| 7500 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7501 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7502 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7503 | * @return LY_ERR |
| 7504 | */ |
| 7505 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7506 | 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] | 7507 | { |
| 7508 | long double num; |
| 7509 | char *endptr; |
| 7510 | |
| 7511 | if (set) { |
| 7512 | errno = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7513 | num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7514 | if (errno) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7515 | 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] | 7516 | 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] | 7517 | 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] | 7518 | return LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7519 | } else if (endptr - &exp->expr[exp->tok_pos[*tok_idx]] != exp->tok_len[*tok_idx]) { |
| 7520 | 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] | 7521 | 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] | 7522 | exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7523 | return LY_EVALID; |
| 7524 | } |
| 7525 | |
| 7526 | set_fill_number(set, num); |
| 7527 | } |
| 7528 | |
| 7529 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7530 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7531 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7532 | return LY_SUCCESS; |
| 7533 | } |
| 7534 | |
| 7535 | /** |
| 7536 | * @brief Evaluate PathExpr. Logs directly on error. |
| 7537 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7538 | * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate* |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7539 | * | PrimaryExpr Predicate* '/' RelativeLocationPath |
| 7540 | * | PrimaryExpr Predicate* '//' RelativeLocationPath |
| 7541 | * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7542 | * [10] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7543 | * |
| 7544 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7545 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7546 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7547 | * @param[in] options XPath options. |
| 7548 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7549 | */ |
| 7550 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7551 | 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] | 7552 | { |
| 7553 | int all_desc, parent_pos_pred; |
| 7554 | LY_ERR rc; |
| 7555 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7556 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7557 | case LYXP_TOKEN_PAR1: |
| 7558 | /* '(' Expr ')' */ |
| 7559 | |
| 7560 | /* '(' */ |
| 7561 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7562 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7563 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7564 | |
| 7565 | /* Expr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7566 | rc = eval_expr_select(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7567 | LY_CHECK_RET(rc); |
| 7568 | |
| 7569 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7570 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7571 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7572 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7573 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7574 | |
| 7575 | parent_pos_pred = 0; |
| 7576 | goto predicate; |
| 7577 | |
| 7578 | case LYXP_TOKEN_DOT: |
| 7579 | case LYXP_TOKEN_DDOT: |
| 7580 | case LYXP_TOKEN_AT: |
| 7581 | case LYXP_TOKEN_NAMETEST: |
| 7582 | case LYXP_TOKEN_NODETYPE: |
| 7583 | /* RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7584 | rc = eval_relative_location_path(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7585 | LY_CHECK_RET(rc); |
| 7586 | break; |
| 7587 | |
| 7588 | case LYXP_TOKEN_FUNCNAME: |
| 7589 | /* FunctionCall */ |
| 7590 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7591 | rc = eval_function_call(exp, tok_idx, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7592 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7593 | rc = eval_function_call(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7594 | } |
| 7595 | LY_CHECK_RET(rc); |
| 7596 | |
| 7597 | parent_pos_pred = 1; |
| 7598 | goto predicate; |
| 7599 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 7600 | case LYXP_TOKEN_OPER_PATH: |
| 7601 | case LYXP_TOKEN_OPER_RPATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7602 | /* AbsoluteLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7603 | rc = eval_absolute_location_path(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7604 | LY_CHECK_RET(rc); |
| 7605 | break; |
| 7606 | |
| 7607 | case LYXP_TOKEN_LITERAL: |
| 7608 | /* Literal */ |
| 7609 | if (!set || (options & LYXP_SCNODE_ALL)) { |
| 7610 | if (set) { |
| 7611 | set_scnode_clear_ctx(set); |
| 7612 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7613 | eval_literal(exp, tok_idx, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7614 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7615 | eval_literal(exp, tok_idx, set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7616 | } |
| 7617 | |
| 7618 | parent_pos_pred = 1; |
| 7619 | goto predicate; |
| 7620 | |
| 7621 | case LYXP_TOKEN_NUMBER: |
| 7622 | /* Number */ |
| 7623 | if (!set || (options & LYXP_SCNODE_ALL)) { |
| 7624 | if (set) { |
| 7625 | set_scnode_clear_ctx(set); |
| 7626 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7627 | rc = eval_number(NULL, exp, tok_idx, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7628 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7629 | rc = eval_number(set->ctx, exp, tok_idx, set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7630 | } |
| 7631 | LY_CHECK_RET(rc); |
| 7632 | |
| 7633 | parent_pos_pred = 1; |
| 7634 | goto predicate; |
| 7635 | |
| 7636 | default: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7637 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[*tok_idx]), |
| 7638 | &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7639 | return LY_EVALID; |
| 7640 | } |
| 7641 | |
| 7642 | return LY_SUCCESS; |
| 7643 | |
| 7644 | predicate: |
| 7645 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7646 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 7647 | rc = eval_predicate(exp, tok_idx, set, options, parent_pos_pred); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7648 | LY_CHECK_RET(rc); |
| 7649 | } |
| 7650 | |
| 7651 | /* ('/' or '//') RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7652 | 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] | 7653 | |
| 7654 | /* evaluate '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7655 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7656 | all_desc = 0; |
| 7657 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7658 | all_desc = 1; |
| 7659 | } |
| 7660 | |
| 7661 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7662 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7663 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7664 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7665 | rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7666 | LY_CHECK_RET(rc); |
| 7667 | } |
| 7668 | |
| 7669 | return LY_SUCCESS; |
| 7670 | } |
| 7671 | |
| 7672 | /** |
| 7673 | * @brief Evaluate UnionExpr. Logs directly on error. |
| 7674 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7675 | * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7676 | * |
| 7677 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7678 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7679 | * @param[in] repeat How many times this expression is repeated. |
| 7680 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7681 | * @param[in] options XPath options. |
| 7682 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7683 | */ |
| 7684 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7685 | 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] | 7686 | { |
| 7687 | LY_ERR rc = LY_SUCCESS; |
| 7688 | struct lyxp_set orig_set, set2; |
| 7689 | uint16_t i; |
| 7690 | |
| 7691 | assert(repeat); |
| 7692 | |
| 7693 | set_init(&orig_set, set); |
| 7694 | set_init(&set2, set); |
| 7695 | |
| 7696 | set_fill_set(&orig_set, set); |
| 7697 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7698 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7699 | LY_CHECK_GOTO(rc, cleanup); |
| 7700 | |
| 7701 | /* ('|' PathExpr)* */ |
| 7702 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7703 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7704 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7705 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7706 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7707 | |
| 7708 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7709 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7710 | LY_CHECK_GOTO(rc, cleanup); |
| 7711 | continue; |
| 7712 | } |
| 7713 | |
| 7714 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7715 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7716 | LY_CHECK_GOTO(rc, cleanup); |
| 7717 | |
| 7718 | /* eval */ |
| 7719 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7720 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7721 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7722 | rc = moveto_union(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7723 | LY_CHECK_GOTO(rc, cleanup); |
| 7724 | } |
| 7725 | } |
| 7726 | |
| 7727 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7728 | lyxp_set_free_content(&orig_set); |
| 7729 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7730 | return rc; |
| 7731 | } |
| 7732 | |
| 7733 | /** |
| 7734 | * @brief Evaluate UnaryExpr. Logs directly on error. |
| 7735 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7736 | * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7737 | * |
| 7738 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7739 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7740 | * @param[in] repeat How many times this expression is repeated. |
| 7741 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7742 | * @param[in] options XPath options. |
| 7743 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7744 | */ |
| 7745 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7746 | 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] | 7747 | { |
| 7748 | LY_ERR rc; |
| 7749 | uint16_t this_op, i; |
| 7750 | |
| 7751 | assert(repeat); |
| 7752 | |
| 7753 | /* ('-')+ */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7754 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7755 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7756 | 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] | 7757 | |
| 7758 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7759 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7760 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7761 | } |
| 7762 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7763 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7764 | LY_CHECK_RET(rc); |
| 7765 | |
| 7766 | if (set && (repeat % 2)) { |
| 7767 | if (options & LYXP_SCNODE_ALL) { |
| 7768 | warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]); |
| 7769 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7770 | 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] | 7771 | LY_CHECK_RET(rc); |
| 7772 | } |
| 7773 | } |
| 7774 | |
| 7775 | return LY_SUCCESS; |
| 7776 | } |
| 7777 | |
| 7778 | /** |
| 7779 | * @brief Evaluate MultiplicativeExpr. Logs directly on error. |
| 7780 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7781 | * [18] MultiplicativeExpr ::= UnaryExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7782 | * | MultiplicativeExpr '*' UnaryExpr |
| 7783 | * | MultiplicativeExpr 'div' UnaryExpr |
| 7784 | * | MultiplicativeExpr 'mod' UnaryExpr |
| 7785 | * |
| 7786 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7787 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7788 | * @param[in] repeat How many times this expression is repeated. |
| 7789 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7790 | * @param[in] options XPath options. |
| 7791 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7792 | */ |
| 7793 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7794 | 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] | 7795 | { |
| 7796 | LY_ERR rc; |
| 7797 | uint16_t this_op; |
| 7798 | struct lyxp_set orig_set, set2; |
| 7799 | uint16_t i; |
| 7800 | |
| 7801 | assert(repeat); |
| 7802 | |
| 7803 | set_init(&orig_set, set); |
| 7804 | set_init(&set2, set); |
| 7805 | |
| 7806 | set_fill_set(&orig_set, set); |
| 7807 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7808 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7809 | LY_CHECK_GOTO(rc, cleanup); |
| 7810 | |
| 7811 | /* ('*' / 'div' / 'mod' UnaryExpr)* */ |
| 7812 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7813 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7814 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7815 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7816 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7817 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7818 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7819 | |
| 7820 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7821 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7822 | LY_CHECK_GOTO(rc, cleanup); |
| 7823 | continue; |
| 7824 | } |
| 7825 | |
| 7826 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7827 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7828 | LY_CHECK_GOTO(rc, cleanup); |
| 7829 | |
| 7830 | /* eval */ |
| 7831 | if (options & LYXP_SCNODE_ALL) { |
| 7832 | 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] | 7833 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7834 | set_scnode_clear_ctx(set); |
| 7835 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7836 | 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] | 7837 | LY_CHECK_GOTO(rc, cleanup); |
| 7838 | } |
| 7839 | } |
| 7840 | |
| 7841 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7842 | lyxp_set_free_content(&orig_set); |
| 7843 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7844 | return rc; |
| 7845 | } |
| 7846 | |
| 7847 | /** |
| 7848 | * @brief Evaluate AdditiveExpr. Logs directly on error. |
| 7849 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7850 | * [17] AdditiveExpr ::= MultiplicativeExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7851 | * | AdditiveExpr '+' MultiplicativeExpr |
| 7852 | * | AdditiveExpr '-' MultiplicativeExpr |
| 7853 | * |
| 7854 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7855 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7856 | * @param[in] repeat How many times this expression is repeated. |
| 7857 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7858 | * @param[in] options XPath options. |
| 7859 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7860 | */ |
| 7861 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7862 | 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] | 7863 | { |
| 7864 | LY_ERR rc; |
| 7865 | uint16_t this_op; |
| 7866 | struct lyxp_set orig_set, set2; |
| 7867 | uint16_t i; |
| 7868 | |
| 7869 | assert(repeat); |
| 7870 | |
| 7871 | set_init(&orig_set, set); |
| 7872 | set_init(&set2, set); |
| 7873 | |
| 7874 | set_fill_set(&orig_set, set); |
| 7875 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7876 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7877 | LY_CHECK_GOTO(rc, cleanup); |
| 7878 | |
| 7879 | /* ('+' / '-' MultiplicativeExpr)* */ |
| 7880 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7881 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7882 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7883 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7884 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7885 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7886 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7887 | |
| 7888 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7889 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7890 | LY_CHECK_GOTO(rc, cleanup); |
| 7891 | continue; |
| 7892 | } |
| 7893 | |
| 7894 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7895 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7896 | LY_CHECK_GOTO(rc, cleanup); |
| 7897 | |
| 7898 | /* eval */ |
| 7899 | if (options & LYXP_SCNODE_ALL) { |
| 7900 | 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] | 7901 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7902 | set_scnode_clear_ctx(set); |
| 7903 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7904 | 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] | 7905 | LY_CHECK_GOTO(rc, cleanup); |
| 7906 | } |
| 7907 | } |
| 7908 | |
| 7909 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7910 | lyxp_set_free_content(&orig_set); |
| 7911 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7912 | return rc; |
| 7913 | } |
| 7914 | |
| 7915 | /** |
| 7916 | * @brief Evaluate RelationalExpr. Logs directly on error. |
| 7917 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7918 | * [16] RelationalExpr ::= AdditiveExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7919 | * | RelationalExpr '<' AdditiveExpr |
| 7920 | * | RelationalExpr '>' AdditiveExpr |
| 7921 | * | RelationalExpr '<=' AdditiveExpr |
| 7922 | * | RelationalExpr '>=' AdditiveExpr |
| 7923 | * |
| 7924 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7925 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7926 | * @param[in] repeat How many times this expression is repeated. |
| 7927 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7928 | * @param[in] options XPath options. |
| 7929 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7930 | */ |
| 7931 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7932 | 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] | 7933 | { |
| 7934 | LY_ERR rc; |
| 7935 | uint16_t this_op; |
| 7936 | struct lyxp_set orig_set, set2; |
| 7937 | uint16_t i; |
| 7938 | |
| 7939 | assert(repeat); |
| 7940 | |
| 7941 | set_init(&orig_set, set); |
| 7942 | set_init(&set2, set); |
| 7943 | |
| 7944 | set_fill_set(&orig_set, set); |
| 7945 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7946 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7947 | LY_CHECK_GOTO(rc, cleanup); |
| 7948 | |
| 7949 | /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */ |
| 7950 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7951 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7952 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7953 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7954 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7955 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7956 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7957 | |
| 7958 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7959 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7960 | LY_CHECK_GOTO(rc, cleanup); |
| 7961 | continue; |
| 7962 | } |
| 7963 | |
| 7964 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7965 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7966 | LY_CHECK_GOTO(rc, cleanup); |
| 7967 | |
| 7968 | /* eval */ |
| 7969 | if (options & LYXP_SCNODE_ALL) { |
| 7970 | 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] | 7971 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7972 | set_scnode_clear_ctx(set); |
| 7973 | } else { |
| 7974 | rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options); |
| 7975 | LY_CHECK_GOTO(rc, cleanup); |
| 7976 | } |
| 7977 | } |
| 7978 | |
| 7979 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7980 | lyxp_set_free_content(&orig_set); |
| 7981 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7982 | return rc; |
| 7983 | } |
| 7984 | |
| 7985 | /** |
| 7986 | * @brief Evaluate EqualityExpr. Logs directly on error. |
| 7987 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7988 | * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7989 | * | EqualityExpr '!=' RelationalExpr |
| 7990 | * |
| 7991 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7992 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7993 | * @param[in] repeat How many times this expression is repeated. |
| 7994 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7995 | * @param[in] options XPath options. |
| 7996 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7997 | */ |
| 7998 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7999 | 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] | 8000 | { |
| 8001 | LY_ERR rc; |
| 8002 | uint16_t this_op; |
| 8003 | struct lyxp_set orig_set, set2; |
| 8004 | uint16_t i; |
| 8005 | |
| 8006 | assert(repeat); |
| 8007 | |
| 8008 | set_init(&orig_set, set); |
| 8009 | set_init(&set2, set); |
| 8010 | |
| 8011 | set_fill_set(&orig_set, set); |
| 8012 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8013 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8014 | LY_CHECK_GOTO(rc, cleanup); |
| 8015 | |
| 8016 | /* ('=' / '!=' RelationalExpr)* */ |
| 8017 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8018 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8019 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8020 | 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] | 8021 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8022 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 8023 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8024 | |
| 8025 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8026 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8027 | LY_CHECK_GOTO(rc, cleanup); |
| 8028 | continue; |
| 8029 | } |
| 8030 | |
| 8031 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8032 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8033 | LY_CHECK_GOTO(rc, cleanup); |
| 8034 | |
| 8035 | /* eval */ |
| 8036 | if (options & LYXP_SCNODE_ALL) { |
| 8037 | 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] | 8038 | warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1); |
| 8039 | 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] | 8040 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8041 | set_scnode_clear_ctx(set); |
| 8042 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8043 | rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options); |
| 8044 | LY_CHECK_GOTO(rc, cleanup); |
| 8045 | } |
| 8046 | } |
| 8047 | |
| 8048 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8049 | lyxp_set_free_content(&orig_set); |
| 8050 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8051 | return rc; |
| 8052 | } |
| 8053 | |
| 8054 | /** |
| 8055 | * @brief Evaluate AndExpr. Logs directly on error. |
| 8056 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8057 | * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8058 | * |
| 8059 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8060 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8061 | * @param[in] repeat How many times this expression is repeated. |
| 8062 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8063 | * @param[in] options XPath options. |
| 8064 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8065 | */ |
| 8066 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8067 | 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] | 8068 | { |
| 8069 | LY_ERR rc; |
| 8070 | struct lyxp_set orig_set, set2; |
| 8071 | uint16_t i; |
| 8072 | |
| 8073 | assert(repeat); |
| 8074 | |
| 8075 | set_init(&orig_set, set); |
| 8076 | set_init(&set2, set); |
| 8077 | |
| 8078 | set_fill_set(&orig_set, set); |
| 8079 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8080 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8081 | LY_CHECK_GOTO(rc, cleanup); |
| 8082 | |
| 8083 | /* cast to boolean, we know that will be the final result */ |
| 8084 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 8085 | set_scnode_clear_ctx(set); |
| 8086 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8087 | lyxp_set_cast(set, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8088 | } |
| 8089 | |
| 8090 | /* ('and' EqualityExpr)* */ |
| 8091 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8092 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG); |
| 8093 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || !set->val.bln ? "skipped" : "parsed"), |
| 8094 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 8095 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8096 | |
| 8097 | /* lazy evaluation */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8098 | if (!set || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) { |
| 8099 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8100 | LY_CHECK_GOTO(rc, cleanup); |
| 8101 | continue; |
| 8102 | } |
| 8103 | |
| 8104 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8105 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8106 | LY_CHECK_GOTO(rc, cleanup); |
| 8107 | |
| 8108 | /* eval - just get boolean value actually */ |
| 8109 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 8110 | set_scnode_clear_ctx(&set2); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 8111 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8112 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8113 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8114 | set_fill_set(set, &set2); |
| 8115 | } |
| 8116 | } |
| 8117 | |
| 8118 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8119 | lyxp_set_free_content(&orig_set); |
| 8120 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8121 | return rc; |
| 8122 | } |
| 8123 | |
| 8124 | /** |
| 8125 | * @brief Evaluate OrExpr. Logs directly on error. |
| 8126 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8127 | * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8128 | * |
| 8129 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8130 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8131 | * @param[in] repeat How many times this expression is repeated. |
| 8132 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8133 | * @param[in] options XPath options. |
| 8134 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8135 | */ |
| 8136 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8137 | 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] | 8138 | { |
| 8139 | LY_ERR rc; |
| 8140 | struct lyxp_set orig_set, set2; |
| 8141 | uint16_t i; |
| 8142 | |
| 8143 | assert(repeat); |
| 8144 | |
| 8145 | set_init(&orig_set, set); |
| 8146 | set_init(&set2, set); |
| 8147 | |
| 8148 | set_fill_set(&orig_set, set); |
| 8149 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8150 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8151 | LY_CHECK_GOTO(rc, cleanup); |
| 8152 | |
| 8153 | /* cast to boolean, we know that will be the final result */ |
| 8154 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 8155 | set_scnode_clear_ctx(set); |
| 8156 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8157 | lyxp_set_cast(set, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8158 | } |
| 8159 | |
| 8160 | /* ('or' AndExpr)* */ |
| 8161 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8162 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG); |
| 8163 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || set->val.bln ? "skipped" : "parsed"), |
| 8164 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 8165 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8166 | |
| 8167 | /* lazy evaluation */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8168 | if (!set || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) { |
| 8169 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8170 | LY_CHECK_GOTO(rc, cleanup); |
| 8171 | continue; |
| 8172 | } |
| 8173 | |
| 8174 | set_fill_set(&set2, &orig_set); |
| 8175 | /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one), |
| 8176 | * but it does not matter */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8177 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8178 | LY_CHECK_GOTO(rc, cleanup); |
| 8179 | |
| 8180 | /* eval - just get boolean value actually */ |
| 8181 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 8182 | set_scnode_clear_ctx(&set2); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 8183 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8184 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8185 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8186 | set_fill_set(set, &set2); |
| 8187 | } |
| 8188 | } |
| 8189 | |
| 8190 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8191 | lyxp_set_free_content(&orig_set); |
| 8192 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8193 | return rc; |
| 8194 | } |
| 8195 | |
| 8196 | /** |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8197 | * @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] | 8198 | * |
| 8199 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8200 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8201 | * @param[in] etype Expression type to evaluate. |
| 8202 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8203 | * @param[in] options XPath options. |
| 8204 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8205 | */ |
| 8206 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8207 | 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] | 8208 | { |
| 8209 | uint16_t i, count; |
| 8210 | enum lyxp_expr_type next_etype; |
| 8211 | LY_ERR rc; |
| 8212 | |
| 8213 | /* process operator repeats */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8214 | if (!exp->repeat[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8215 | next_etype = LYXP_EXPR_NONE; |
| 8216 | } else { |
| 8217 | /* find etype repeat */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8218 | for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8219 | |
| 8220 | /* select one-priority lower because etype expression called us */ |
| 8221 | if (i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8222 | next_etype = exp->repeat[*tok_idx][i - 1]; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8223 | /* count repeats for that expression */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8224 | 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] | 8225 | } else { |
| 8226 | next_etype = LYXP_EXPR_NONE; |
| 8227 | } |
| 8228 | } |
| 8229 | |
| 8230 | /* decide what expression are we parsing based on the repeat */ |
| 8231 | switch (next_etype) { |
| 8232 | case LYXP_EXPR_OR: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8233 | rc = eval_or_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8234 | break; |
| 8235 | case LYXP_EXPR_AND: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8236 | rc = eval_and_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8237 | break; |
| 8238 | case LYXP_EXPR_EQUALITY: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8239 | rc = eval_equality_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8240 | break; |
| 8241 | case LYXP_EXPR_RELATIONAL: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8242 | rc = eval_relational_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8243 | break; |
| 8244 | case LYXP_EXPR_ADDITIVE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8245 | rc = eval_additive_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8246 | break; |
| 8247 | case LYXP_EXPR_MULTIPLICATIVE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8248 | rc = eval_multiplicative_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8249 | break; |
| 8250 | case LYXP_EXPR_UNARY: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8251 | rc = eval_unary_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8252 | break; |
| 8253 | case LYXP_EXPR_UNION: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8254 | rc = eval_union_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8255 | break; |
| 8256 | case LYXP_EXPR_NONE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8257 | rc = eval_path_expr(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8258 | break; |
| 8259 | default: |
| 8260 | LOGINT_RET(set->ctx); |
| 8261 | } |
| 8262 | |
| 8263 | return rc; |
| 8264 | } |
| 8265 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8266 | /** |
| 8267 | * @brief Get root type. |
| 8268 | * |
| 8269 | * @param[in] ctx_node Context node. |
| 8270 | * @param[in] ctx_scnode Schema context node. |
| 8271 | * @param[in] options XPath options. |
| 8272 | * @return Root type. |
| 8273 | */ |
| 8274 | static enum lyxp_node_type |
| 8275 | lyxp_get_root_type(const struct lyd_node *ctx_node, const struct lysc_node *ctx_scnode, int options) |
| 8276 | { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 8277 | const struct lysc_node *op; |
| 8278 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8279 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 8280 | for (op = ctx_scnode; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent); |
| 8281 | |
| 8282 | if (op || (options & LYXP_SCNODE)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8283 | /* general root that can access everything */ |
| 8284 | return LYXP_NODE_ROOT; |
| 8285 | } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) { |
| 8286 | /* root context node can access only config data (because we said so, it is unspecified) */ |
| 8287 | return LYXP_NODE_ROOT_CONFIG; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8288 | } |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 8289 | return LYXP_NODE_ROOT; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8290 | } |
| 8291 | |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 8292 | op = ctx_node ? ctx_node->schema : NULL; |
| 8293 | for (; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent); |
| 8294 | |
| 8295 | if (!ctx_node || (!op && (ctx_node->schema->flags & LYS_CONFIG_W))) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8296 | /* root context node can access only config data (because we said so, it is unspecified) */ |
| 8297 | return LYXP_NODE_ROOT_CONFIG; |
| 8298 | } |
| 8299 | |
| 8300 | return LYXP_NODE_ROOT; |
| 8301 | } |
| 8302 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8303 | LY_ERR |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 8304 | lyxp_eval(struct lyxp_expr *exp, LY_PREFIX_FORMAT format, const struct lys_module *local_mod, const struct lyd_node *ctx_node, |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 8305 | 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] | 8306 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8307 | uint16_t tok_idx = 0; |
| 8308 | const struct lyd_node *real_ctx_node; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8309 | LY_ERR rc; |
| 8310 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8311 | LY_CHECK_ARG_RET(NULL, exp, local_mod, ctx_node, set, LY_EINVAL); |
| 8312 | |
| 8313 | if ((ctx_node_type == LYXP_NODE_ROOT) || (ctx_node_type == LYXP_NODE_ROOT_CONFIG)) { |
| 8314 | /* we always need some context node because it is used for resolving unqualified names */ |
| 8315 | real_ctx_node = NULL; |
| 8316 | } else { |
| 8317 | real_ctx_node = ctx_node; |
| 8318 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8319 | |
| 8320 | /* prepare set for evaluation */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8321 | tok_idx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8322 | memset(set, 0, sizeof *set); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8323 | set->type = LYXP_SET_NODE_SET; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8324 | 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] | 8325 | set->ctx = local_mod->ctx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8326 | set->ctx_node = ctx_node; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8327 | set->root_type = lyxp_get_root_type(real_ctx_node, NULL, options); |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 8328 | for (set->context_op = ctx_node->schema; |
| 8329 | set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); |
| 8330 | set->context_op = set->context_op->parent); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8331 | set->local_mod = local_mod; |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 8332 | set->tree = tree; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8333 | set->format = format; |
| 8334 | |
| 8335 | /* evaluate */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8336 | rc = eval_expr_select(exp, &tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8337 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8338 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8339 | } |
| 8340 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8341 | return rc; |
| 8342 | } |
| 8343 | |
| 8344 | #if 0 |
| 8345 | |
| 8346 | /* full xml printing of set elements, not used currently */ |
| 8347 | |
| 8348 | void |
| 8349 | lyxp_set_print_xml(FILE *f, struct lyxp_set *set) |
| 8350 | { |
| 8351 | uint32_t i; |
| 8352 | char *str_num; |
| 8353 | struct lyout out; |
| 8354 | |
| 8355 | memset(&out, 0, sizeof out); |
| 8356 | |
| 8357 | out.type = LYOUT_STREAM; |
| 8358 | out.method.f = f; |
| 8359 | |
| 8360 | switch (set->type) { |
| 8361 | case LYXP_SET_EMPTY: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8362 | ly_print_(&out, "Empty XPath set\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8363 | break; |
| 8364 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8365 | ly_print_(&out, "Boolean XPath set:\n"); |
| 8366 | ly_print_(&out, "%s\n\n", set->value.bool ? "true" : "false"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8367 | break; |
| 8368 | case LYXP_SET_STRING: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8369 | ly_print_(&out, "String XPath set:\n"); |
| 8370 | ly_print_(&out, "\"%s\"\n\n", set->value.str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8371 | break; |
| 8372 | case LYXP_SET_NUMBER: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8373 | ly_print_(&out, "Number XPath set:\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8374 | |
| 8375 | if (isnan(set->value.num)) { |
| 8376 | str_num = strdup("NaN"); |
| 8377 | } else if ((set->value.num == 0) || (set->value.num == -0.0f)) { |
| 8378 | str_num = strdup("0"); |
| 8379 | } else if (isinf(set->value.num) && !signbit(set->value.num)) { |
| 8380 | str_num = strdup("Infinity"); |
| 8381 | } else if (isinf(set->value.num) && signbit(set->value.num)) { |
| 8382 | str_num = strdup("-Infinity"); |
| 8383 | } else if ((long long)set->value.num == set->value.num) { |
| 8384 | if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) { |
| 8385 | str_num = NULL; |
| 8386 | } |
| 8387 | } else { |
| 8388 | if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) { |
| 8389 | str_num = NULL; |
| 8390 | } |
| 8391 | } |
| 8392 | if (!str_num) { |
| 8393 | LOGMEM; |
| 8394 | return; |
| 8395 | } |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8396 | ly_print_(&out, "%s\n\n", str_num); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8397 | free(str_num); |
| 8398 | break; |
| 8399 | case LYXP_SET_NODE_SET: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8400 | ly_print_(&out, "Node XPath set:\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8401 | |
| 8402 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8403 | ly_print_(&out, "%d. ", i + 1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8404 | switch (set->node_type[i]) { |
| 8405 | case LYXP_NODE_ROOT_ALL: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8406 | ly_print_(&out, "ROOT all\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8407 | break; |
| 8408 | case LYXP_NODE_ROOT_CONFIG: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8409 | ly_print_(&out, "ROOT config\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8410 | break; |
| 8411 | case LYXP_NODE_ROOT_STATE: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8412 | ly_print_(&out, "ROOT state\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8413 | break; |
| 8414 | case LYXP_NODE_ROOT_NOTIF: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8415 | ly_print_(&out, "ROOT notification \"%s\"\n\n", set->value.nodes[i]->schema->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8416 | break; |
| 8417 | case LYXP_NODE_ROOT_RPC: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8418 | ly_print_(&out, "ROOT rpc \"%s\"\n\n", set->value.nodes[i]->schema->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8419 | break; |
| 8420 | case LYXP_NODE_ROOT_OUTPUT: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8421 | ly_print_(&out, "ROOT output \"%s\"\n\n", set->value.nodes[i]->schema->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8422 | break; |
| 8423 | case LYXP_NODE_ELEM: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8424 | ly_print_(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8425 | xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT); |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8426 | ly_print_(&out, "\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8427 | break; |
| 8428 | case LYXP_NODE_TEXT: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8429 | ly_print_(&out, "TEXT \"%s\"\n\n", ((struct lyd_node_leaf_list *)set->value.nodes[i])->value_str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8430 | break; |
| 8431 | case LYXP_NODE_ATTR: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8432 | ly_print_(&out, "ATTR \"%s\" = \"%s\"\n\n", set->value.attrs[i]->name, set->value.attrs[i]->value); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8433 | break; |
| 8434 | } |
| 8435 | } |
| 8436 | break; |
| 8437 | } |
| 8438 | } |
| 8439 | |
| 8440 | #endif |
| 8441 | |
| 8442 | LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8443 | lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8444 | { |
| 8445 | long double num; |
| 8446 | char *str; |
| 8447 | LY_ERR rc; |
| 8448 | |
| 8449 | if (!set || (set->type == target)) { |
| 8450 | return LY_SUCCESS; |
| 8451 | } |
| 8452 | |
| 8453 | /* it's not possible to convert anything into a node set */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8454 | assert(target != LYXP_SET_NODE_SET); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8455 | |
| 8456 | if (set->type == LYXP_SET_SCNODE_SET) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8457 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8458 | return LY_EINVAL; |
| 8459 | } |
| 8460 | |
| 8461 | /* to STRING */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8462 | 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] | 8463 | switch (set->type) { |
| 8464 | case LYXP_SET_NUMBER: |
| 8465 | if (isnan(set->val.num)) { |
| 8466 | set->val.str = strdup("NaN"); |
| 8467 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 8468 | } else if ((set->val.num == 0) || (set->val.num == -0.0f)) { |
| 8469 | set->val.str = strdup("0"); |
| 8470 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 8471 | } else if (isinf(set->val.num) && !signbit(set->val.num)) { |
| 8472 | set->val.str = strdup("Infinity"); |
| 8473 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 8474 | } else if (isinf(set->val.num) && signbit(set->val.num)) { |
| 8475 | set->val.str = strdup("-Infinity"); |
| 8476 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 8477 | } else if ((long long)set->val.num == set->val.num) { |
| 8478 | if (asprintf(&str, "%lld", (long long)set->val.num) == -1) { |
| 8479 | LOGMEM_RET(set->ctx); |
| 8480 | } |
| 8481 | set->val.str = str; |
| 8482 | } else { |
| 8483 | if (asprintf(&str, "%03.1Lf", set->val.num) == -1) { |
| 8484 | LOGMEM_RET(set->ctx); |
| 8485 | } |
| 8486 | set->val.str = str; |
| 8487 | } |
| 8488 | break; |
| 8489 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8490 | if (set->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8491 | set->val.str = strdup("true"); |
| 8492 | } else { |
| 8493 | set->val.str = strdup("false"); |
| 8494 | } |
| 8495 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM); |
| 8496 | break; |
| 8497 | case LYXP_SET_NODE_SET: |
| 8498 | assert(set->used); |
| 8499 | |
| 8500 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8501 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8502 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8503 | rc = cast_node_set_to_string(set, &str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8504 | LY_CHECK_RET(rc); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8505 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8506 | set->val.str = str; |
| 8507 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8508 | default: |
| 8509 | LOGINT_RET(set->ctx); |
| 8510 | } |
| 8511 | set->type = LYXP_SET_STRING; |
| 8512 | } |
| 8513 | |
| 8514 | /* to NUMBER */ |
| 8515 | if (target == LYXP_SET_NUMBER) { |
| 8516 | switch (set->type) { |
| 8517 | case LYXP_SET_STRING: |
| 8518 | num = cast_string_to_number(set->val.str); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8519 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8520 | set->val.num = num; |
| 8521 | break; |
| 8522 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8523 | if (set->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8524 | set->val.num = 1; |
| 8525 | } else { |
| 8526 | set->val.num = 0; |
| 8527 | } |
| 8528 | break; |
| 8529 | default: |
| 8530 | LOGINT_RET(set->ctx); |
| 8531 | } |
| 8532 | set->type = LYXP_SET_NUMBER; |
| 8533 | } |
| 8534 | |
| 8535 | /* to BOOLEAN */ |
| 8536 | if (target == LYXP_SET_BOOLEAN) { |
| 8537 | switch (set->type) { |
| 8538 | case LYXP_SET_NUMBER: |
| 8539 | 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] | 8540 | set->val.bln = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8541 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8542 | set->val.bln = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8543 | } |
| 8544 | break; |
| 8545 | case LYXP_SET_STRING: |
| 8546 | if (set->val.str[0]) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8547 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8548 | set->val.bln = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8549 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8550 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8551 | set->val.bln = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8552 | } |
| 8553 | break; |
| 8554 | case LYXP_SET_NODE_SET: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8555 | if (set->used) { |
| 8556 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8557 | set->val.bln = 1; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8558 | } else { |
| 8559 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8560 | set->val.bln = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8561 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8562 | break; |
| 8563 | default: |
| 8564 | LOGINT_RET(set->ctx); |
| 8565 | } |
| 8566 | set->type = LYXP_SET_BOOLEAN; |
| 8567 | } |
| 8568 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8569 | return LY_SUCCESS; |
| 8570 | } |
| 8571 | |
| 8572 | LY_ERR |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 8573 | lyxp_atomize(struct lyxp_expr *exp, LY_PREFIX_FORMAT format, const struct lys_module *local_mod, |
| 8574 | const struct lysc_node *ctx_scnode, enum lyxp_node_type ctx_scnode_type, struct lyxp_set *set, int options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8575 | { |
| 8576 | struct ly_ctx *ctx; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8577 | const struct lysc_node *real_ctx_scnode; |
| 8578 | uint16_t tok_idx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8579 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8580 | 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] | 8581 | |
| 8582 | ctx = local_mod->ctx; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8583 | if ((ctx_scnode_type == LYXP_NODE_ROOT) || (ctx_scnode_type == LYXP_NODE_ROOT_CONFIG)) { |
| 8584 | /* we always need some context node because it is used for resolving unqualified names */ |
| 8585 | real_ctx_scnode = NULL; |
| 8586 | } else { |
| 8587 | real_ctx_scnode = ctx_scnode; |
| 8588 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8589 | |
| 8590 | /* prepare set for evaluation */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8591 | tok_idx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8592 | memset(set, 0, sizeof *set); |
| 8593 | set->type = LYXP_SET_SCNODE_SET; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8594 | lyxp_set_scnode_insert_node(set, real_ctx_scnode, ctx_scnode_type); |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 8595 | set->val.scnodes[0].in_ctx = -2; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8596 | set->ctx = ctx; |
| 8597 | set->ctx_scnode = ctx_scnode; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8598 | set->root_type = lyxp_get_root_type(NULL, real_ctx_scnode, options); |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 8599 | for (set->context_op = ctx_scnode; |
| 8600 | set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); |
| 8601 | set->context_op = set->context_op->parent); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8602 | set->local_mod = local_mod; |
| 8603 | set->format = format; |
| 8604 | |
| 8605 | /* evaluate */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8606 | return eval_expr_select(exp, &tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8607 | } |
Michal Vasko | d43d71a | 2020-08-07 14:54:58 +0200 | [diff] [blame] | 8608 | |
| 8609 | API const char * |
| 8610 | lyxp_get_expr(const struct lyxp_expr *path) |
| 8611 | { |
| 8612 | if (!path) { |
| 8613 | return NULL; |
| 8614 | } |
| 8615 | |
| 8616 | return path->expr; |
| 8617 | } |