Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 1 | /** |
| 2 | * @file xpath.c |
| 3 | * @author Michal Vasko <mvasko@cesnet.cz> |
| 4 | * @brief YANG XPath evaluation functions |
| 5 | * |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6 | * Copyright (c) 2015 - 2020 CESNET, z.s.p.o. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 7 | * |
| 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * https://opensource.org/licenses/BSD-3-Clause |
| 13 | */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 14 | #define _GNU_SOURCE |
| 15 | |
| 16 | /* needed by libmath functions isfinite(), isinf(), isnan(), signbit(), ... */ |
| 17 | #define _ISOC99_SOURCE |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 18 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 19 | #include "xpath.h" |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 20 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 21 | #include <assert.h> |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 22 | #include <ctype.h> |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 23 | #include <errno.h> |
| 24 | #include <limits.h> |
| 25 | #include <math.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 26 | #include <stdint.h> |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 27 | #include <stdio.h> |
| 28 | #include <stdlib.h> |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 29 | #include <string.h> |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 30 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 31 | #include "common.h" |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 32 | #include "compat.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 33 | #include "context.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 34 | #include "dict.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 35 | #include "hash_table.h" |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 36 | #include "parser_data.h" |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 37 | #include "path.h" |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 38 | #include "plugins_types.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 39 | #include "printer.h" |
| 40 | #include "printer_data.h" |
| 41 | #include "tree.h" |
| 42 | #include "tree_data_internal.h" |
| 43 | #include "tree_schema_internal.h" |
| 44 | #include "xml.h" |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 45 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 46 | static LY_ERR reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx); |
| 47 | static LY_ERR eval_expr_select(struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype, struct lyxp_set *set, int options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 48 | |
| 49 | /** |
| 50 | * @brief Print the type of an XPath \p set. |
| 51 | * |
| 52 | * @param[in] set Set to use. |
| 53 | * @return Set type string. |
| 54 | */ |
| 55 | static const char * |
| 56 | print_set_type(struct lyxp_set *set) |
| 57 | { |
| 58 | switch (set->type) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 59 | case LYXP_SET_NODE_SET: |
| 60 | return "node set"; |
| 61 | case LYXP_SET_SCNODE_SET: |
| 62 | return "schema node set"; |
| 63 | case LYXP_SET_BOOLEAN: |
| 64 | return "boolean"; |
| 65 | case LYXP_SET_NUMBER: |
| 66 | return "number"; |
| 67 | case LYXP_SET_STRING: |
| 68 | return "string"; |
| 69 | } |
| 70 | |
| 71 | return NULL; |
| 72 | } |
| 73 | |
Michal Vasko | 24cddf8 | 2020-06-01 08:17:01 +0200 | [diff] [blame] | 74 | const char * |
| 75 | lyxp_print_token(enum lyxp_token tok) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 76 | { |
| 77 | switch (tok) { |
| 78 | case LYXP_TOKEN_PAR1: |
| 79 | return "("; |
| 80 | case LYXP_TOKEN_PAR2: |
| 81 | return ")"; |
| 82 | case LYXP_TOKEN_BRACK1: |
| 83 | return "["; |
| 84 | case LYXP_TOKEN_BRACK2: |
| 85 | return "]"; |
| 86 | case LYXP_TOKEN_DOT: |
| 87 | return "."; |
| 88 | case LYXP_TOKEN_DDOT: |
| 89 | return ".."; |
| 90 | case LYXP_TOKEN_AT: |
| 91 | return "@"; |
| 92 | case LYXP_TOKEN_COMMA: |
| 93 | return ","; |
| 94 | case LYXP_TOKEN_NAMETEST: |
| 95 | return "NameTest"; |
| 96 | case LYXP_TOKEN_NODETYPE: |
| 97 | return "NodeType"; |
| 98 | case LYXP_TOKEN_FUNCNAME: |
| 99 | return "FunctionName"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 100 | case LYXP_TOKEN_OPER_LOG: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 101 | return "Operator(Logic)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 102 | case LYXP_TOKEN_OPER_EQUAL: |
| 103 | return "Operator(Equal)"; |
| 104 | case LYXP_TOKEN_OPER_NEQUAL: |
| 105 | return "Operator(Non-equal)"; |
| 106 | case LYXP_TOKEN_OPER_COMP: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 107 | return "Operator(Comparison)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 108 | case LYXP_TOKEN_OPER_MATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 109 | return "Operator(Math)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 110 | case LYXP_TOKEN_OPER_UNI: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 111 | return "Operator(Union)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 112 | case LYXP_TOKEN_OPER_PATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 113 | return "Operator(Path)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 114 | case LYXP_TOKEN_OPER_RPATH: |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 115 | return "Operator(Recursive Path)"; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 116 | case LYXP_TOKEN_LITERAL: |
| 117 | return "Literal"; |
| 118 | case LYXP_TOKEN_NUMBER: |
| 119 | return "Number"; |
| 120 | default: |
| 121 | LOGINT(NULL); |
| 122 | return ""; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * @brief Print the whole expression \p exp to debug output. |
| 128 | * |
| 129 | * @param[in] exp Expression to use. |
| 130 | */ |
| 131 | static void |
| 132 | print_expr_struct_debug(struct lyxp_expr *exp) |
| 133 | { |
| 134 | uint16_t i, j; |
| 135 | char tmp[128]; |
| 136 | |
| 137 | if (!exp || (ly_log_level < LY_LLDBG)) { |
| 138 | return; |
| 139 | } |
| 140 | |
| 141 | LOGDBG(LY_LDGXPATH, "expression \"%s\":", exp->expr); |
| 142 | for (i = 0; i < exp->used; ++i) { |
Michal Vasko | 24cddf8 | 2020-06-01 08:17:01 +0200 | [diff] [blame] | 143 | sprintf(tmp, "\ttoken %s, in expression \"%.*s\"", lyxp_print_token(exp->tokens[i]), exp->tok_len[i], |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 144 | &exp->expr[exp->tok_pos[i]]); |
| 145 | if (exp->repeat[i]) { |
| 146 | sprintf(tmp + strlen(tmp), " (repeat %d", exp->repeat[i][0]); |
| 147 | for (j = 1; exp->repeat[i][j]; ++j) { |
| 148 | sprintf(tmp + strlen(tmp), ", %d", exp->repeat[i][j]); |
| 149 | } |
| 150 | strcat(tmp, ")"); |
| 151 | } |
| 152 | LOGDBG(LY_LDGXPATH, tmp); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | #ifndef NDEBUG |
| 157 | |
| 158 | /** |
| 159 | * @brief Print XPath set content to debug output. |
| 160 | * |
| 161 | * @param[in] set Set to print. |
| 162 | */ |
| 163 | static void |
| 164 | print_set_debug(struct lyxp_set *set) |
| 165 | { |
| 166 | uint32_t i; |
| 167 | char *str; |
| 168 | int dynamic; |
| 169 | struct lyxp_set_node *item; |
| 170 | struct lyxp_set_scnode *sitem; |
| 171 | |
| 172 | if (ly_log_level < LY_LLDBG) { |
| 173 | return; |
| 174 | } |
| 175 | |
| 176 | switch (set->type) { |
| 177 | case LYXP_SET_NODE_SET: |
| 178 | LOGDBG(LY_LDGXPATH, "set NODE SET:"); |
| 179 | for (i = 0; i < set->used; ++i) { |
| 180 | item = &set->val.nodes[i]; |
| 181 | |
| 182 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 183 | case LYXP_NODE_NONE: |
| 184 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): NONE", i + 1, item->pos); |
| 185 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 186 | case LYXP_NODE_ROOT: |
| 187 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT", i + 1, item->pos); |
| 188 | break; |
| 189 | case LYXP_NODE_ROOT_CONFIG: |
| 190 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT CONFIG", i + 1, item->pos); |
| 191 | break; |
| 192 | case LYXP_NODE_ELEM: |
| 193 | if ((item->node->schema->nodetype == LYS_LIST) |
| 194 | && (((struct lyd_node_inner *)item->node)->child->schema->nodetype == LYS_LEAF)) { |
| 195 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (1st child val: %s)", i + 1, item->pos, |
| 196 | item->node->schema->name, |
Michal Vasko | 5bfd4be | 2020-06-23 13:26:19 +0200 | [diff] [blame] | 197 | (str = (char *)lyd_value2str((struct lyd_node_term *)lyd_node_children(item->node, 0), &dynamic))); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 198 | if (dynamic) { |
| 199 | free(str); |
| 200 | } |
| 201 | } else if (((struct lyd_node_inner *)item->node)->schema->nodetype == LYS_LEAFLIST) { |
| 202 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (val: %s)", i + 1, item->pos, |
| 203 | item->node->schema->name, |
| 204 | (str = (char *)lyd_value2str((struct lyd_node_term *)item->node, &dynamic))); |
| 205 | if (dynamic) { |
| 206 | free(str); |
| 207 | } |
| 208 | } else { |
| 209 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s", i + 1, item->pos, item->node->schema->name); |
| 210 | } |
| 211 | break; |
| 212 | case LYXP_NODE_TEXT: |
| 213 | if (item->node->schema->nodetype & LYS_ANYDATA) { |
| 214 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT <%s>", i + 1, item->pos, |
| 215 | item->node->schema->nodetype == LYS_ANYXML ? "anyxml" : "anydata"); |
| 216 | } else { |
| 217 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT %s", i + 1, item->pos, |
| 218 | (str = (char *)lyd_value2str((struct lyd_node_term *)item->node, &dynamic))); |
| 219 | if (dynamic) { |
| 220 | free(str); |
| 221 | } |
| 222 | } |
| 223 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 224 | case LYXP_NODE_META: |
| 225 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): META %s = %s", i + 1, item->pos, set->val.meta[i].meta->name, |
| 226 | set->val.meta[i].meta->value); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 227 | break; |
| 228 | } |
| 229 | } |
| 230 | break; |
| 231 | |
| 232 | case LYXP_SET_SCNODE_SET: |
| 233 | LOGDBG(LY_LDGXPATH, "set SCNODE SET:"); |
| 234 | for (i = 0; i < set->used; ++i) { |
| 235 | sitem = &set->val.scnodes[i]; |
| 236 | |
| 237 | switch (sitem->type) { |
| 238 | case LYXP_NODE_ROOT: |
| 239 | LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT", i + 1, sitem->in_ctx); |
| 240 | break; |
| 241 | case LYXP_NODE_ROOT_CONFIG: |
| 242 | LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT CONFIG", i + 1, sitem->in_ctx); |
| 243 | break; |
| 244 | case LYXP_NODE_ELEM: |
| 245 | LOGDBG(LY_LDGXPATH, "\t%d (%u): ELEM %s", i + 1, sitem->in_ctx, sitem->scnode->name); |
| 246 | break; |
| 247 | default: |
| 248 | LOGINT(NULL); |
| 249 | break; |
| 250 | } |
| 251 | } |
| 252 | break; |
| 253 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 254 | case LYXP_SET_BOOLEAN: |
| 255 | LOGDBG(LY_LDGXPATH, "set BOOLEAN"); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 256 | LOGDBG(LY_LDGXPATH, "\t%s", (set->val.bln ? "true" : "false")); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 257 | break; |
| 258 | |
| 259 | case LYXP_SET_STRING: |
| 260 | LOGDBG(LY_LDGXPATH, "set STRING"); |
| 261 | LOGDBG(LY_LDGXPATH, "\t%s", set->val.str); |
| 262 | break; |
| 263 | |
| 264 | case LYXP_SET_NUMBER: |
| 265 | LOGDBG(LY_LDGXPATH, "set NUMBER"); |
| 266 | |
| 267 | if (isnan(set->val.num)) { |
| 268 | str = strdup("NaN"); |
| 269 | } else if ((set->val.num == 0) || (set->val.num == -0.0f)) { |
| 270 | str = strdup("0"); |
| 271 | } else if (isinf(set->val.num) && !signbit(set->val.num)) { |
| 272 | str = strdup("Infinity"); |
| 273 | } else if (isinf(set->val.num) && signbit(set->val.num)) { |
| 274 | str = strdup("-Infinity"); |
| 275 | } else if ((long long)set->val.num == set->val.num) { |
| 276 | if (asprintf(&str, "%lld", (long long)set->val.num) == -1) { |
| 277 | str = NULL; |
| 278 | } |
| 279 | } else { |
| 280 | if (asprintf(&str, "%03.1Lf", set->val.num) == -1) { |
| 281 | str = NULL; |
| 282 | } |
| 283 | } |
| 284 | LY_CHECK_ERR_RET(!str, LOGMEM(NULL), ); |
| 285 | |
| 286 | LOGDBG(LY_LDGXPATH, "\t%s", str); |
| 287 | free(str); |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | #endif |
| 292 | |
| 293 | /** |
| 294 | * @brief Realloc the string \p str. |
| 295 | * |
| 296 | * @param[in] ctx libyang context for logging. |
| 297 | * @param[in] needed How much free space is required. |
| 298 | * @param[in,out] str Pointer to the string to use. |
| 299 | * @param[in,out] used Used bytes in \p str. |
| 300 | * @param[in,out] size Allocated bytes in \p str. |
| 301 | * @return LY_ERR |
| 302 | */ |
| 303 | static LY_ERR |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 304 | cast_string_realloc(const struct ly_ctx *ctx, uint16_t needed, char **str, uint16_t *used, uint16_t *size) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 305 | { |
| 306 | if (*size - *used < needed) { |
| 307 | do { |
| 308 | if ((UINT16_MAX - *size) < LYXP_STRING_CAST_SIZE_STEP) { |
| 309 | LOGERR(ctx, LY_EINVAL, "XPath string length limit (%u) reached.", UINT16_MAX); |
| 310 | return LY_EINVAL; |
| 311 | } |
| 312 | *size += LYXP_STRING_CAST_SIZE_STEP; |
| 313 | } while (*size - *used < needed); |
| 314 | *str = ly_realloc(*str, *size * sizeof(char)); |
| 315 | LY_CHECK_ERR_RET(!(*str), LOGMEM(ctx), LY_EMEM); |
| 316 | } |
| 317 | |
| 318 | return LY_SUCCESS; |
| 319 | } |
| 320 | |
| 321 | /** |
| 322 | * @brief Cast nodes recursively to one string @p str. |
| 323 | * |
| 324 | * @param[in] node Node to cast. |
| 325 | * @param[in] fake_cont Whether to put the data into a "fake" container. |
| 326 | * @param[in] root_type Type of the XPath root. |
| 327 | * @param[in] indent Current indent. |
| 328 | * @param[in,out] str Resulting string. |
| 329 | * @param[in,out] used Used bytes in @p str. |
| 330 | * @param[in,out] size Allocated bytes in @p str. |
| 331 | * @return LY_ERR |
| 332 | */ |
| 333 | static LY_ERR |
| 334 | cast_string_recursive(const struct lyd_node *node, int fake_cont, enum lyxp_node_type root_type, uint16_t indent, char **str, |
| 335 | uint16_t *used, uint16_t *size) |
| 336 | { |
Radek Krejci | 7f769d7 | 2020-07-11 23:13:56 +0200 | [diff] [blame] | 337 | char *buf, *line, *ptr = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 338 | const char *value_str; |
| 339 | int dynamic; |
| 340 | const struct lyd_node *child; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 341 | struct lyd_node *tree; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 342 | struct lyd_node_any *any; |
| 343 | LY_ERR rc; |
| 344 | |
| 345 | if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) { |
| 346 | return LY_SUCCESS; |
| 347 | } |
| 348 | |
| 349 | if (fake_cont) { |
| 350 | rc = cast_string_realloc(LYD_NODE_CTX(node), 1, str, used, size); |
| 351 | LY_CHECK_RET(rc); |
| 352 | strcpy(*str + (*used - 1), "\n"); |
| 353 | ++(*used); |
| 354 | |
| 355 | ++indent; |
| 356 | } |
| 357 | |
| 358 | switch (node->schema->nodetype) { |
| 359 | case LYS_CONTAINER: |
| 360 | case LYS_LIST: |
| 361 | case LYS_RPC: |
| 362 | case LYS_NOTIF: |
| 363 | rc = cast_string_realloc(LYD_NODE_CTX(node), 1, str, used, size); |
| 364 | LY_CHECK_RET(rc); |
| 365 | strcpy(*str + (*used - 1), "\n"); |
| 366 | ++(*used); |
| 367 | |
Michal Vasko | 5bfd4be | 2020-06-23 13:26:19 +0200 | [diff] [blame] | 368 | for (child = lyd_node_children(node, 0); child; child = child->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 369 | rc = cast_string_recursive(child, 0, root_type, indent + 1, str, used, size); |
| 370 | LY_CHECK_RET(rc); |
| 371 | } |
| 372 | |
| 373 | break; |
| 374 | |
| 375 | case LYS_LEAF: |
| 376 | case LYS_LEAFLIST: |
| 377 | value_str = lyd_value2str(((struct lyd_node_term *)node), &dynamic); |
| 378 | |
| 379 | /* print indent */ |
| 380 | rc = cast_string_realloc(LYD_NODE_CTX(node), indent * 2 + strlen(value_str) + 1, str, used, size); |
| 381 | if (rc != LY_SUCCESS) { |
| 382 | if (dynamic) { |
| 383 | free((char *)value_str); |
| 384 | } |
| 385 | return rc; |
| 386 | } |
| 387 | memset(*str + (*used - 1), ' ', indent * 2); |
| 388 | *used += indent * 2; |
| 389 | |
| 390 | /* print value */ |
| 391 | if (*used == 1) { |
| 392 | sprintf(*str + (*used - 1), "%s", value_str); |
| 393 | *used += strlen(value_str); |
| 394 | } else { |
| 395 | sprintf(*str + (*used - 1), "%s\n", value_str); |
| 396 | *used += strlen(value_str) + 1; |
| 397 | } |
| 398 | if (dynamic) { |
| 399 | free((char *)value_str); |
| 400 | } |
| 401 | |
| 402 | break; |
| 403 | |
| 404 | case LYS_ANYXML: |
| 405 | case LYS_ANYDATA: |
| 406 | any = (struct lyd_node_any *)node; |
| 407 | if (!(void *)any->value.tree) { |
| 408 | /* no content */ |
| 409 | buf = strdup(""); |
| 410 | LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_NODE_CTX(node)), LY_EMEM); |
| 411 | } else { |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 412 | struct ly_out *out; |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 413 | |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 414 | if (any->value_type == LYD_ANYDATA_LYB) { |
| 415 | /* try to parse it into a data tree */ |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 416 | if (lyd_parse_data_mem((struct ly_ctx *)LYD_NODE_CTX(node), any->value.mem, LYD_LYB, LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, &tree) == LY_SUCCESS) { |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 417 | /* successfully parsed */ |
| 418 | free(any->value.mem); |
| 419 | any->value.tree = tree; |
| 420 | any->value_type = LYD_ANYDATA_DATATREE; |
| 421 | } |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 422 | /* error is covered by the following switch where LYD_ANYDATA_LYB causes failure */ |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 423 | } |
| 424 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 425 | switch (any->value_type) { |
| 426 | case LYD_ANYDATA_STRING: |
| 427 | case LYD_ANYDATA_XML: |
| 428 | case LYD_ANYDATA_JSON: |
| 429 | buf = strdup(any->value.json); |
| 430 | LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_NODE_CTX(node)), LY_EMEM); |
| 431 | break; |
| 432 | case LYD_ANYDATA_DATATREE: |
Radek Krejci | 84ce7b1 | 2020-06-11 17:28:25 +0200 | [diff] [blame] | 433 | LY_CHECK_RET(ly_out_new_memory(&buf, 0, &out)); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 434 | rc = lyd_print_all(out, any->value.tree, LYD_XML, 0); |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 435 | ly_out_free(out, NULL, 0); |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 436 | LY_CHECK_RET(rc < 0, -rc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 437 | break; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 438 | case LYD_ANYDATA_LYB: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 439 | LOGERR(LYD_NODE_CTX(node), LY_EINVAL, "Cannot convert LYB anydata into string."); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 440 | return LY_EINVAL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 441 | } |
| 442 | } |
| 443 | |
| 444 | line = strtok_r(buf, "\n", &ptr); |
| 445 | do { |
| 446 | rc = cast_string_realloc(LYD_NODE_CTX(node), indent * 2 + strlen(line) + 1, str, used, size); |
| 447 | if (rc != LY_SUCCESS) { |
| 448 | free(buf); |
| 449 | return rc; |
| 450 | } |
| 451 | memset(*str + (*used - 1), ' ', indent * 2); |
| 452 | *used += indent * 2; |
| 453 | |
| 454 | strcpy(*str + (*used - 1), line); |
| 455 | *used += strlen(line); |
| 456 | |
| 457 | strcpy(*str + (*used - 1), "\n"); |
| 458 | *used += 1; |
| 459 | } while ((line = strtok_r(NULL, "\n", &ptr))); |
| 460 | |
| 461 | free(buf); |
| 462 | break; |
| 463 | |
| 464 | default: |
| 465 | LOGINT_RET(LYD_NODE_CTX(node)); |
| 466 | } |
| 467 | |
| 468 | if (fake_cont) { |
| 469 | rc = cast_string_realloc(LYD_NODE_CTX(node), 1, str, used, size); |
| 470 | LY_CHECK_RET(rc); |
| 471 | strcpy(*str + (*used - 1), "\n"); |
| 472 | ++(*used); |
| 473 | |
| 474 | --indent; |
| 475 | } |
| 476 | |
| 477 | return LY_SUCCESS; |
| 478 | } |
| 479 | |
| 480 | /** |
| 481 | * @brief Cast an element into a string. |
| 482 | * |
| 483 | * @param[in] node Node to cast. |
| 484 | * @param[in] fake_cont Whether to put the data into a "fake" container. |
| 485 | * @param[in] root_type Type of the XPath root. |
| 486 | * @param[out] str Element cast to dynamically-allocated string. |
| 487 | * @return LY_ERR |
| 488 | */ |
| 489 | static LY_ERR |
| 490 | cast_string_elem(struct lyd_node *node, int fake_cont, enum lyxp_node_type root_type, char **str) |
| 491 | { |
| 492 | uint16_t used, size; |
| 493 | LY_ERR rc; |
| 494 | |
| 495 | *str = malloc(LYXP_STRING_CAST_SIZE_START * sizeof(char)); |
| 496 | LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_NODE_CTX(node)), LY_EMEM); |
| 497 | (*str)[0] = '\0'; |
| 498 | used = 1; |
| 499 | size = LYXP_STRING_CAST_SIZE_START; |
| 500 | |
| 501 | rc = cast_string_recursive(node, fake_cont, root_type, 0, str, &used, &size); |
| 502 | if (rc != LY_SUCCESS) { |
| 503 | free(*str); |
| 504 | return rc; |
| 505 | } |
| 506 | |
| 507 | if (size > used) { |
| 508 | *str = ly_realloc(*str, used * sizeof(char)); |
| 509 | LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_NODE_CTX(node)), LY_EMEM); |
| 510 | } |
| 511 | return LY_SUCCESS; |
| 512 | } |
| 513 | |
| 514 | /** |
| 515 | * @brief Cast a LYXP_SET_NODE_SET set into a string. |
| 516 | * Context position aware. |
| 517 | * |
| 518 | * @param[in] set Set to cast. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 519 | * @param[out] str Cast dynamically-allocated string. |
| 520 | * @return LY_ERR |
| 521 | */ |
| 522 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 523 | cast_node_set_to_string(struct lyxp_set *set, char **str) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 524 | { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 525 | int dynamic; |
| 526 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 527 | switch (set->val.nodes[0].type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 528 | case LYXP_NODE_NONE: |
| 529 | /* invalid */ |
| 530 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 531 | case LYXP_NODE_ROOT: |
| 532 | case LYXP_NODE_ROOT_CONFIG: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 533 | return cast_string_elem(set->val.nodes[0].node, 1, set->root_type, str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 534 | case LYXP_NODE_ELEM: |
| 535 | case LYXP_NODE_TEXT: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 536 | return cast_string_elem(set->val.nodes[0].node, 0, set->root_type, str); |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 537 | case LYXP_NODE_META: |
| 538 | *str = (char *)lyd_meta2str(set->val.meta[0].meta, &dynamic); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 539 | if (!dynamic) { |
| 540 | *str = strdup(*str); |
| 541 | if (!*str) { |
| 542 | LOGMEM_RET(set->ctx); |
| 543 | } |
| 544 | } |
| 545 | return LY_SUCCESS; |
| 546 | } |
| 547 | |
| 548 | LOGINT_RET(set->ctx); |
| 549 | } |
| 550 | |
| 551 | /** |
| 552 | * @brief Cast a string into an XPath number. |
| 553 | * |
| 554 | * @param[in] str String to use. |
| 555 | * @return Cast number. |
| 556 | */ |
| 557 | static long double |
| 558 | cast_string_to_number(const char *str) |
| 559 | { |
| 560 | long double num; |
| 561 | char *ptr; |
| 562 | |
| 563 | errno = 0; |
| 564 | num = strtold(str, &ptr); |
| 565 | if (errno || *ptr) { |
| 566 | num = NAN; |
| 567 | } |
| 568 | return num; |
| 569 | } |
| 570 | |
| 571 | /** |
| 572 | * @brief Callback for checking value equality. |
| 573 | * |
| 574 | * @param[in] val1_p First value. |
| 575 | * @param[in] val2_p Second value. |
| 576 | * @param[in] mod Whether hash table is being modified. |
| 577 | * @param[in] cb_data Callback data. |
| 578 | * @return 0 if not equal, non-zero if equal. |
| 579 | */ |
| 580 | static int |
| 581 | set_values_equal_cb(void *val1_p, void *val2_p, int UNUSED(mod), void *UNUSED(cb_data)) |
| 582 | { |
| 583 | struct lyxp_set_hash_node *val1, *val2; |
| 584 | |
| 585 | val1 = (struct lyxp_set_hash_node *)val1_p; |
| 586 | val2 = (struct lyxp_set_hash_node *)val2_p; |
| 587 | |
| 588 | if ((val1->node == val2->node) && (val1->type == val2->type)) { |
| 589 | return 1; |
| 590 | } |
| 591 | |
| 592 | return 0; |
| 593 | } |
| 594 | |
| 595 | /** |
| 596 | * @brief Insert node and its hash into set. |
| 597 | * |
| 598 | * @param[in] set et to insert to. |
| 599 | * @param[in] node Node with hash. |
| 600 | * @param[in] type Node type. |
| 601 | */ |
| 602 | static void |
| 603 | set_insert_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type) |
| 604 | { |
| 605 | int r; |
| 606 | uint32_t i, hash; |
| 607 | struct lyxp_set_hash_node hnode; |
| 608 | |
| 609 | if (!set->ht && (set->used >= LYD_HT_MIN_ITEMS)) { |
| 610 | /* create hash table and add all the nodes */ |
| 611 | set->ht = lyht_new(1, sizeof(struct lyxp_set_hash_node), set_values_equal_cb, NULL, 1); |
| 612 | for (i = 0; i < set->used; ++i) { |
| 613 | hnode.node = set->val.nodes[i].node; |
| 614 | hnode.type = set->val.nodes[i].type; |
| 615 | |
| 616 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 617 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 618 | hash = dict_hash_multi(hash, NULL, 0); |
| 619 | |
| 620 | r = lyht_insert(set->ht, &hnode, hash, NULL); |
| 621 | assert(!r); |
| 622 | (void)r; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 623 | |
Michal Vasko | 9d6befd | 2019-12-11 14:56:56 +0100 | [diff] [blame] | 624 | if (hnode.node == node) { |
| 625 | /* it was just added, do not add it twice */ |
| 626 | node = NULL; |
| 627 | } |
| 628 | } |
| 629 | } |
| 630 | |
| 631 | if (set->ht && node) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 632 | /* add the new node into hash table */ |
| 633 | hnode.node = node; |
| 634 | hnode.type = type; |
| 635 | |
| 636 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 637 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 638 | hash = dict_hash_multi(hash, NULL, 0); |
| 639 | |
| 640 | r = lyht_insert(set->ht, &hnode, hash, NULL); |
| 641 | assert(!r); |
| 642 | (void)r; |
| 643 | } |
| 644 | } |
| 645 | |
| 646 | /** |
| 647 | * @brief Remove node and its hash from set. |
| 648 | * |
| 649 | * @param[in] set Set to remove from. |
| 650 | * @param[in] node Node to remove. |
| 651 | * @param[in] type Node type. |
| 652 | */ |
| 653 | static void |
| 654 | set_remove_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type) |
| 655 | { |
| 656 | int r; |
| 657 | struct lyxp_set_hash_node hnode; |
| 658 | uint32_t hash; |
| 659 | |
| 660 | if (set->ht) { |
| 661 | hnode.node = node; |
| 662 | hnode.type = type; |
| 663 | |
| 664 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 665 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 666 | hash = dict_hash_multi(hash, NULL, 0); |
| 667 | |
| 668 | r = lyht_remove(set->ht, &hnode, hash); |
| 669 | assert(!r); |
| 670 | (void)r; |
| 671 | |
| 672 | if (!set->ht->used) { |
| 673 | lyht_free(set->ht); |
| 674 | set->ht = NULL; |
| 675 | } |
| 676 | } |
| 677 | } |
| 678 | |
| 679 | /** |
| 680 | * @brief Check whether node is in set based on its hash. |
| 681 | * |
| 682 | * @param[in] set Set to search in. |
| 683 | * @param[in] node Node to search for. |
| 684 | * @param[in] type Node type. |
| 685 | * @param[in] skip_idx Index in @p set to skip. |
| 686 | * @return LY_ERR |
| 687 | */ |
| 688 | static LY_ERR |
| 689 | set_dup_node_hash_check(const struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type, int skip_idx) |
| 690 | { |
| 691 | struct lyxp_set_hash_node hnode, *match_p; |
| 692 | uint32_t hash; |
| 693 | |
| 694 | hnode.node = node; |
| 695 | hnode.type = type; |
| 696 | |
| 697 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 698 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 699 | hash = dict_hash_multi(hash, NULL, 0); |
| 700 | |
| 701 | if (!lyht_find(set->ht, &hnode, hash, (void **)&match_p)) { |
| 702 | if ((skip_idx > -1) && (set->val.nodes[skip_idx].node == match_p->node) && (set->val.nodes[skip_idx].type == match_p->type)) { |
| 703 | /* we found it on the index that should be skipped, find another */ |
| 704 | hnode = *match_p; |
| 705 | if (lyht_find_next(set->ht, &hnode, hash, (void **)&match_p)) { |
| 706 | /* none other found */ |
| 707 | return LY_SUCCESS; |
| 708 | } |
| 709 | } |
| 710 | |
| 711 | return LY_EEXIST; |
| 712 | } |
| 713 | |
| 714 | /* not found */ |
| 715 | return LY_SUCCESS; |
| 716 | } |
| 717 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 718 | void |
| 719 | lyxp_set_free_content(struct lyxp_set *set) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 720 | { |
| 721 | if (!set) { |
| 722 | return; |
| 723 | } |
| 724 | |
| 725 | if (set->type == LYXP_SET_NODE_SET) { |
| 726 | free(set->val.nodes); |
| 727 | lyht_free(set->ht); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 728 | } else if (set->type == LYXP_SET_SCNODE_SET) { |
| 729 | free(set->val.scnodes); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 730 | lyht_free(set->ht); |
| 731 | } else { |
| 732 | if (set->type == LYXP_SET_STRING) { |
| 733 | free(set->val.str); |
| 734 | } |
| 735 | set->type = LYXP_SET_NODE_SET; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 736 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 737 | |
| 738 | set->val.nodes = NULL; |
| 739 | set->used = 0; |
| 740 | set->size = 0; |
| 741 | set->ht = NULL; |
| 742 | set->ctx_pos = 0; |
| 743 | set->ctx_pos = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 744 | } |
| 745 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 746 | /** |
| 747 | * @brief Free dynamically-allocated set. |
| 748 | * |
| 749 | * @param[in] set Set to free. |
| 750 | */ |
| 751 | static void |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 752 | lyxp_set_free(struct lyxp_set *set) |
| 753 | { |
| 754 | if (!set) { |
| 755 | return; |
| 756 | } |
| 757 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 758 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 759 | free(set); |
| 760 | } |
| 761 | |
| 762 | /** |
| 763 | * @brief Initialize set context. |
| 764 | * |
| 765 | * @param[in] new Set to initialize. |
| 766 | * @param[in] set Arbitrary initialized set. |
| 767 | */ |
| 768 | static void |
| 769 | set_init(struct lyxp_set *new, struct lyxp_set *set) |
| 770 | { |
| 771 | memset(new, 0, sizeof *new); |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 772 | if (set) { |
| 773 | new->ctx = set->ctx; |
| 774 | new->ctx_node = set->ctx_node; |
Michal Vasko | 588112f | 2019-12-10 14:51:53 +0100 | [diff] [blame] | 775 | new->root_type = set->root_type; |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 776 | new->context_op = set->context_op; |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 777 | new->local_mod = set->local_mod; |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 778 | new->tree = set->tree; |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 779 | new->format = set->format; |
| 780 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | /** |
| 784 | * @brief Create a deep copy of a set. |
| 785 | * |
| 786 | * @param[in] set Set to copy. |
| 787 | * @return Copy of @p set. |
| 788 | */ |
| 789 | static struct lyxp_set * |
| 790 | set_copy(struct lyxp_set *set) |
| 791 | { |
| 792 | struct lyxp_set *ret; |
| 793 | uint16_t i; |
Michal Vasko | ba71654 | 2019-12-16 10:01:58 +0100 | [diff] [blame] | 794 | int idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 795 | |
| 796 | if (!set) { |
| 797 | return NULL; |
| 798 | } |
| 799 | |
| 800 | ret = malloc(sizeof *ret); |
| 801 | LY_CHECK_ERR_RET(!ret, LOGMEM(set->ctx), NULL); |
| 802 | set_init(ret, set); |
| 803 | |
| 804 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 805 | ret->type = set->type; |
| 806 | |
| 807 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | ba71654 | 2019-12-16 10:01:58 +0100 | [diff] [blame] | 808 | if ((set->val.scnodes[i].in_ctx == 1) || (set->val.scnodes[i].in_ctx == -2)) { |
| 809 | idx = lyxp_set_scnode_insert_node(ret, set->val.scnodes[i].scnode, set->val.scnodes[i].type); |
Michal Vasko | 3f27c52 | 2020-01-06 08:37:49 +0100 | [diff] [blame] | 810 | /* coverity seems to think scnodes can be NULL */ |
| 811 | if ((idx == -1) || !ret->val.scnodes) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 812 | lyxp_set_free(ret); |
| 813 | return NULL; |
| 814 | } |
Michal Vasko | ba71654 | 2019-12-16 10:01:58 +0100 | [diff] [blame] | 815 | ret->val.scnodes[idx].in_ctx = set->val.scnodes[i].in_ctx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 816 | } |
| 817 | } |
| 818 | } else if (set->type == LYXP_SET_NODE_SET) { |
| 819 | ret->type = set->type; |
| 820 | ret->val.nodes = malloc(set->used * sizeof *ret->val.nodes); |
| 821 | LY_CHECK_ERR_RET(!ret->val.nodes, LOGMEM(set->ctx); free(ret), NULL); |
| 822 | memcpy(ret->val.nodes, set->val.nodes, set->used * sizeof *ret->val.nodes); |
| 823 | |
| 824 | ret->used = ret->size = set->used; |
| 825 | ret->ctx_pos = set->ctx_pos; |
| 826 | ret->ctx_size = set->ctx_size; |
| 827 | ret->ht = lyht_dup(set->ht); |
| 828 | } else { |
| 829 | memcpy(ret, set, sizeof *ret); |
| 830 | if (set->type == LYXP_SET_STRING) { |
| 831 | ret->val.str = strdup(set->val.str); |
| 832 | LY_CHECK_ERR_RET(!ret->val.str, LOGMEM(set->ctx); free(ret), NULL); |
| 833 | } |
| 834 | } |
| 835 | |
| 836 | return ret; |
| 837 | } |
| 838 | |
| 839 | /** |
| 840 | * @brief Fill XPath set with a string. Any current data are disposed of. |
| 841 | * |
| 842 | * @param[in] set Set to fill. |
| 843 | * @param[in] string String to fill into \p set. |
| 844 | * @param[in] str_len Length of \p string. 0 is a valid value! |
| 845 | */ |
| 846 | static void |
| 847 | set_fill_string(struct lyxp_set *set, const char *string, uint16_t str_len) |
| 848 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 849 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 850 | |
| 851 | set->type = LYXP_SET_STRING; |
| 852 | if ((str_len == 0) && (string[0] != '\0')) { |
| 853 | string = ""; |
| 854 | } |
| 855 | set->val.str = strndup(string, str_len); |
| 856 | } |
| 857 | |
| 858 | /** |
| 859 | * @brief Fill XPath set with a number. Any current data are disposed of. |
| 860 | * |
| 861 | * @param[in] set Set to fill. |
| 862 | * @param[in] number Number to fill into \p set. |
| 863 | */ |
| 864 | static void |
| 865 | set_fill_number(struct lyxp_set *set, long double number) |
| 866 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 867 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 868 | |
| 869 | set->type = LYXP_SET_NUMBER; |
| 870 | set->val.num = number; |
| 871 | } |
| 872 | |
| 873 | /** |
| 874 | * @brief Fill XPath set with a boolean. Any current data are disposed of. |
| 875 | * |
| 876 | * @param[in] set Set to fill. |
| 877 | * @param[in] boolean Boolean to fill into \p set. |
| 878 | */ |
| 879 | static void |
| 880 | set_fill_boolean(struct lyxp_set *set, int boolean) |
| 881 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 882 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 883 | |
| 884 | set->type = LYXP_SET_BOOLEAN; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 885 | set->val.bln = boolean; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 886 | } |
| 887 | |
| 888 | /** |
| 889 | * @brief Fill XPath set with the value from another set (deep assign). |
| 890 | * Any current data are disposed of. |
| 891 | * |
| 892 | * @param[in] trg Set to fill. |
| 893 | * @param[in] src Source set to copy into \p trg. |
| 894 | */ |
| 895 | static void |
| 896 | set_fill_set(struct lyxp_set *trg, struct lyxp_set *src) |
| 897 | { |
| 898 | if (!trg || !src) { |
| 899 | return; |
| 900 | } |
| 901 | |
| 902 | if (trg->type == LYXP_SET_NODE_SET) { |
| 903 | free(trg->val.nodes); |
| 904 | } else if (trg->type == LYXP_SET_STRING) { |
| 905 | free(trg->val.str); |
| 906 | } |
| 907 | set_init(trg, src); |
| 908 | |
| 909 | if (src->type == LYXP_SET_SCNODE_SET) { |
| 910 | trg->type = LYXP_SET_SCNODE_SET; |
| 911 | trg->used = src->used; |
| 912 | trg->size = src->used; |
| 913 | |
| 914 | trg->val.scnodes = ly_realloc(trg->val.scnodes, trg->size * sizeof *trg->val.scnodes); |
| 915 | LY_CHECK_ERR_RET(!trg->val.scnodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), ); |
| 916 | memcpy(trg->val.scnodes, src->val.scnodes, src->used * sizeof *src->val.scnodes); |
| 917 | } else if (src->type == LYXP_SET_BOOLEAN) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 918 | set_fill_boolean(trg, src->val.bln); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 919 | } else if (src->type == LYXP_SET_NUMBER) { |
| 920 | set_fill_number(trg, src->val.num); |
| 921 | } else if (src->type == LYXP_SET_STRING) { |
| 922 | set_fill_string(trg, src->val.str, strlen(src->val.str)); |
| 923 | } else { |
| 924 | if (trg->type == LYXP_SET_NODE_SET) { |
| 925 | free(trg->val.nodes); |
| 926 | } else if (trg->type == LYXP_SET_STRING) { |
| 927 | free(trg->val.str); |
| 928 | } |
| 929 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 930 | assert(src->type == LYXP_SET_NODE_SET); |
| 931 | |
| 932 | trg->type = LYXP_SET_NODE_SET; |
| 933 | trg->used = src->used; |
| 934 | trg->size = src->used; |
| 935 | trg->ctx_pos = src->ctx_pos; |
| 936 | trg->ctx_size = src->ctx_size; |
| 937 | |
| 938 | trg->val.nodes = malloc(trg->used * sizeof *trg->val.nodes); |
| 939 | LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), ); |
| 940 | memcpy(trg->val.nodes, src->val.nodes, src->used * sizeof *src->val.nodes); |
| 941 | if (src->ht) { |
| 942 | trg->ht = lyht_dup(src->ht); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 943 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 944 | trg->ht = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 945 | } |
| 946 | } |
| 947 | } |
| 948 | |
| 949 | /** |
| 950 | * @brief Clear context of all schema nodes. |
| 951 | * |
| 952 | * @param[in] set Set to clear. |
| 953 | */ |
| 954 | static void |
| 955 | set_scnode_clear_ctx(struct lyxp_set *set) |
| 956 | { |
| 957 | uint32_t i; |
| 958 | |
| 959 | for (i = 0; i < set->used; ++i) { |
| 960 | if (set->val.scnodes[i].in_ctx == 1) { |
| 961 | set->val.scnodes[i].in_ctx = 0; |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 962 | } else if (set->val.scnodes[i].in_ctx == -2) { |
| 963 | set->val.scnodes[i].in_ctx = -1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 964 | } |
| 965 | } |
| 966 | } |
| 967 | |
| 968 | /** |
| 969 | * @brief Remove a node from a set. Removing last node changes |
| 970 | * set into LYXP_SET_EMPTY. Context position aware. |
| 971 | * |
| 972 | * @param[in] set Set to use. |
| 973 | * @param[in] idx Index from @p set of the node to be removed. |
| 974 | */ |
| 975 | static void |
| 976 | set_remove_node(struct lyxp_set *set, uint32_t idx) |
| 977 | { |
| 978 | assert(set && (set->type == LYXP_SET_NODE_SET)); |
| 979 | assert(idx < set->used); |
| 980 | |
| 981 | set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
| 982 | |
| 983 | --set->used; |
| 984 | if (set->used) { |
| 985 | memmove(&set->val.nodes[idx], &set->val.nodes[idx + 1], |
| 986 | (set->used - idx) * sizeof *set->val.nodes); |
| 987 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 988 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 989 | } |
| 990 | } |
| 991 | |
| 992 | /** |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 993 | * @brief Remove a node from a set by setting its type to LYXP_NODE_NONE. |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 994 | * |
| 995 | * @param[in] set Set to use. |
| 996 | * @param[in] idx Index from @p set of the node to be removed. |
| 997 | */ |
| 998 | static void |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 999 | set_remove_node_none(struct lyxp_set *set, uint32_t idx) |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1000 | { |
| 1001 | assert(set && (set->type == LYXP_SET_NODE_SET)); |
| 1002 | assert(idx < set->used); |
| 1003 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1004 | if (set->val.nodes[idx].type == LYXP_NODE_ELEM) { |
| 1005 | set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
| 1006 | } |
| 1007 | set->val.nodes[idx].type = LYXP_NODE_NONE; |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1008 | } |
| 1009 | |
| 1010 | /** |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1011 | * @brief Remove all LYXP_NODE_NONE nodes from a set. Removing last node changes |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1012 | * set into LYXP_SET_EMPTY. Context position aware. |
| 1013 | * |
| 1014 | * @param[in] set Set to consolidate. |
| 1015 | */ |
| 1016 | static void |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1017 | set_remove_nodes_none(struct lyxp_set *set) |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1018 | { |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 1019 | uint16_t i, orig_used, end = 0; |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1020 | int32_t start; |
| 1021 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1022 | assert(set); |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1023 | |
| 1024 | orig_used = set->used; |
| 1025 | set->used = 0; |
| 1026 | for (i = 0; i < orig_used;) { |
| 1027 | start = -1; |
| 1028 | do { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1029 | if ((set->val.nodes[i].type != LYXP_NODE_NONE) && (start == -1)) { |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1030 | start = i; |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1031 | } else if ((start > -1) && (set->val.nodes[i].type == LYXP_NODE_NONE)) { |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1032 | end = i; |
| 1033 | ++i; |
| 1034 | break; |
| 1035 | } |
| 1036 | |
| 1037 | ++i; |
| 1038 | if (i == orig_used) { |
| 1039 | end = i; |
| 1040 | } |
| 1041 | } while (i < orig_used); |
| 1042 | |
| 1043 | if (start > -1) { |
| 1044 | /* move the whole chunk of valid nodes together */ |
| 1045 | if (set->used != (unsigned)start) { |
| 1046 | memmove(&set->val.nodes[set->used], &set->val.nodes[start], (end - start) * sizeof *set->val.nodes); |
| 1047 | } |
| 1048 | set->used += end - start; |
| 1049 | } |
| 1050 | } |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1051 | } |
| 1052 | |
| 1053 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1054 | * @brief Check for duplicates in a node set. |
| 1055 | * |
| 1056 | * @param[in] set Set to check. |
| 1057 | * @param[in] node Node to look for in @p set. |
| 1058 | * @param[in] node_type Type of @p node. |
| 1059 | * @param[in] skip_idx Index from @p set to skip. |
| 1060 | * @return LY_ERR |
| 1061 | */ |
| 1062 | static LY_ERR |
| 1063 | set_dup_node_check(const struct lyxp_set *set, const struct lyd_node *node, enum lyxp_node_type node_type, int skip_idx) |
| 1064 | { |
| 1065 | uint32_t i; |
| 1066 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1067 | if (set->ht && node) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1068 | return set_dup_node_hash_check(set, (struct lyd_node *)node, node_type, skip_idx); |
| 1069 | } |
| 1070 | |
| 1071 | for (i = 0; i < set->used; ++i) { |
| 1072 | if ((skip_idx > -1) && (i == (unsigned)skip_idx)) { |
| 1073 | continue; |
| 1074 | } |
| 1075 | |
| 1076 | if ((set->val.nodes[i].node == node) && (set->val.nodes[i].type == node_type)) { |
| 1077 | return LY_EEXIST; |
| 1078 | } |
| 1079 | } |
| 1080 | |
| 1081 | return LY_SUCCESS; |
| 1082 | } |
| 1083 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 1084 | int |
| 1085 | lyxp_set_scnode_dup_node_check(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, int skip_idx) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1086 | { |
| 1087 | uint32_t i; |
| 1088 | |
| 1089 | for (i = 0; i < set->used; ++i) { |
| 1090 | if ((skip_idx > -1) && (i == (unsigned)skip_idx)) { |
| 1091 | continue; |
| 1092 | } |
| 1093 | |
| 1094 | if ((set->val.scnodes[i].scnode == node) && (set->val.scnodes[i].type == node_type)) { |
| 1095 | return i; |
| 1096 | } |
| 1097 | } |
| 1098 | |
| 1099 | return -1; |
| 1100 | } |
| 1101 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 1102 | void |
| 1103 | lyxp_set_scnode_merge(struct lyxp_set *set1, struct lyxp_set *set2) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1104 | { |
| 1105 | uint32_t orig_used, i, j; |
| 1106 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1107 | assert((set1->type == LYXP_SET_SCNODE_SET) && (set2->type == LYXP_SET_SCNODE_SET)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1108 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1109 | if (!set2->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1110 | return; |
| 1111 | } |
| 1112 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1113 | if (!set1->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1114 | memcpy(set1, set2, sizeof *set1); |
| 1115 | return; |
| 1116 | } |
| 1117 | |
| 1118 | if (set1->used + set2->used > set1->size) { |
| 1119 | set1->size = set1->used + set2->used; |
| 1120 | set1->val.scnodes = ly_realloc(set1->val.scnodes, set1->size * sizeof *set1->val.scnodes); |
| 1121 | LY_CHECK_ERR_RET(!set1->val.scnodes, LOGMEM(set1->ctx), ); |
| 1122 | } |
| 1123 | |
| 1124 | orig_used = set1->used; |
| 1125 | |
| 1126 | for (i = 0; i < set2->used; ++i) { |
| 1127 | for (j = 0; j < orig_used; ++j) { |
| 1128 | /* detect duplicities */ |
| 1129 | if (set1->val.scnodes[j].scnode == set2->val.scnodes[i].scnode) { |
| 1130 | break; |
| 1131 | } |
| 1132 | } |
| 1133 | |
| 1134 | if (j == orig_used) { |
| 1135 | memcpy(&set1->val.scnodes[set1->used], &set2->val.scnodes[i], sizeof *set2->val.scnodes); |
| 1136 | ++set1->used; |
| 1137 | } |
| 1138 | } |
| 1139 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1140 | lyxp_set_free_content(set2); |
| 1141 | set2->type = LYXP_SET_SCNODE_SET; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1142 | } |
| 1143 | |
| 1144 | /** |
| 1145 | * @brief Insert a node into a set. Context position aware. |
| 1146 | * |
| 1147 | * @param[in] set Set to use. |
| 1148 | * @param[in] node Node to insert to @p set. |
| 1149 | * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting. |
| 1150 | * @param[in] node_type Node type of @p node. |
| 1151 | * @param[in] idx Index in @p set to insert into. |
| 1152 | */ |
| 1153 | static void |
| 1154 | set_insert_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx) |
| 1155 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1156 | assert(set && (set->type == LYXP_SET_NODE_SET)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1157 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1158 | if (!set->size) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1159 | /* first item */ |
| 1160 | if (idx) { |
| 1161 | /* no real harm done, but it is a bug */ |
| 1162 | LOGINT(set->ctx); |
| 1163 | idx = 0; |
| 1164 | } |
| 1165 | set->val.nodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.nodes); |
| 1166 | LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), ); |
| 1167 | set->type = LYXP_SET_NODE_SET; |
| 1168 | set->used = 0; |
| 1169 | set->size = LYXP_SET_SIZE_START; |
| 1170 | set->ctx_pos = 1; |
| 1171 | set->ctx_size = 1; |
| 1172 | set->ht = NULL; |
| 1173 | } else { |
| 1174 | /* not an empty set */ |
| 1175 | if (set->used == set->size) { |
| 1176 | |
| 1177 | /* set is full */ |
| 1178 | set->val.nodes = ly_realloc(set->val.nodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.nodes); |
| 1179 | LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), ); |
| 1180 | set->size += LYXP_SET_SIZE_STEP; |
| 1181 | } |
| 1182 | |
| 1183 | if (idx > set->used) { |
| 1184 | LOGINT(set->ctx); |
| 1185 | idx = set->used; |
| 1186 | } |
| 1187 | |
| 1188 | /* make space for the new node */ |
| 1189 | if (idx < set->used) { |
| 1190 | memmove(&set->val.nodes[idx + 1], &set->val.nodes[idx], (set->used - idx) * sizeof *set->val.nodes); |
| 1191 | } |
| 1192 | } |
| 1193 | |
| 1194 | /* finally assign the value */ |
| 1195 | set->val.nodes[idx].node = (struct lyd_node *)node; |
| 1196 | set->val.nodes[idx].type = node_type; |
| 1197 | set->val.nodes[idx].pos = pos; |
| 1198 | ++set->used; |
| 1199 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1200 | if (set->val.nodes[idx].type == LYXP_NODE_ELEM) { |
| 1201 | set_insert_node_hash(set, (struct lyd_node *)node, node_type); |
| 1202 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1203 | } |
| 1204 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 1205 | int |
| 1206 | lyxp_set_scnode_insert_node(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1207 | { |
| 1208 | int ret; |
| 1209 | |
| 1210 | assert(set->type == LYXP_SET_SCNODE_SET); |
| 1211 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 1212 | ret = lyxp_set_scnode_dup_node_check(set, node, node_type, -1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1213 | if (ret > -1) { |
| 1214 | set->val.scnodes[ret].in_ctx = 1; |
| 1215 | } else { |
| 1216 | if (set->used == set->size) { |
| 1217 | set->val.scnodes = ly_realloc(set->val.scnodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.scnodes); |
| 1218 | LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), -1); |
| 1219 | set->size += LYXP_SET_SIZE_STEP; |
| 1220 | } |
| 1221 | |
| 1222 | ret = set->used; |
| 1223 | set->val.scnodes[ret].scnode = (struct lysc_node *)node; |
| 1224 | set->val.scnodes[ret].type = node_type; |
| 1225 | set->val.scnodes[ret].in_ctx = 1; |
| 1226 | ++set->used; |
| 1227 | } |
| 1228 | |
| 1229 | return ret; |
| 1230 | } |
| 1231 | |
| 1232 | /** |
| 1233 | * @brief Replace a node in a set with another. Context position aware. |
| 1234 | * |
| 1235 | * @param[in] set Set to use. |
| 1236 | * @param[in] node Node to insert to @p set. |
| 1237 | * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting. |
| 1238 | * @param[in] node_type Node type of @p node. |
| 1239 | * @param[in] idx Index in @p set of the node to replace. |
| 1240 | */ |
| 1241 | static void |
| 1242 | set_replace_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx) |
| 1243 | { |
| 1244 | assert(set && (idx < set->used)); |
| 1245 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1246 | if (set->val.nodes[idx].type == LYXP_NODE_ELEM) { |
| 1247 | set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
| 1248 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1249 | set->val.nodes[idx].node = (struct lyd_node *)node; |
| 1250 | set->val.nodes[idx].type = node_type; |
| 1251 | set->val.nodes[idx].pos = pos; |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1252 | if (set->val.nodes[idx].type == LYXP_NODE_ELEM) { |
| 1253 | set_insert_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
| 1254 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1255 | } |
| 1256 | |
| 1257 | /** |
| 1258 | * @brief Set all nodes with ctx 1 to a new unique context value. |
| 1259 | * |
| 1260 | * @param[in] set Set to modify. |
| 1261 | * @return New context value. |
| 1262 | */ |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 1263 | static int32_t |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1264 | set_scnode_new_in_ctx(struct lyxp_set *set) |
| 1265 | { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 1266 | uint32_t i; |
| 1267 | int32_t ret_ctx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1268 | |
| 1269 | assert(set->type == LYXP_SET_SCNODE_SET); |
| 1270 | |
| 1271 | ret_ctx = 3; |
| 1272 | retry: |
| 1273 | for (i = 0; i < set->used; ++i) { |
| 1274 | if (set->val.scnodes[i].in_ctx >= ret_ctx) { |
| 1275 | ret_ctx = set->val.scnodes[i].in_ctx + 1; |
| 1276 | goto retry; |
| 1277 | } |
| 1278 | } |
| 1279 | for (i = 0; i < set->used; ++i) { |
| 1280 | if (set->val.scnodes[i].in_ctx == 1) { |
| 1281 | set->val.scnodes[i].in_ctx = ret_ctx; |
| 1282 | } |
| 1283 | } |
| 1284 | |
| 1285 | return ret_ctx; |
| 1286 | } |
| 1287 | |
| 1288 | /** |
| 1289 | * @brief Get unique @p node position in the data. |
| 1290 | * |
| 1291 | * @param[in] node Node to find. |
| 1292 | * @param[in] node_type Node type of @p node. |
| 1293 | * @param[in] root Root node. |
| 1294 | * @param[in] root_type Type of the XPath @p root node. |
| 1295 | * @param[in] prev Node that we think is before @p node in DFS from @p root. Can optionally |
| 1296 | * be used to increase efficiency and start the DFS from this node. |
| 1297 | * @param[in] prev_pos Node @p prev position. Optional, but must be set if @p prev is set. |
| 1298 | * @return Node position. |
| 1299 | */ |
| 1300 | static uint32_t |
| 1301 | get_node_pos(const struct lyd_node *node, enum lyxp_node_type node_type, const struct lyd_node *root, |
| 1302 | enum lyxp_node_type root_type, const struct lyd_node **prev, uint32_t *prev_pos) |
| 1303 | { |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 1304 | const struct lyd_node *next, *elem = NULL, *top_sibling; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1305 | uint32_t pos = 1; |
| 1306 | |
| 1307 | assert(prev && prev_pos && !root->prev->next); |
| 1308 | |
| 1309 | if ((node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ROOT_CONFIG)) { |
| 1310 | return 0; |
| 1311 | } |
| 1312 | |
| 1313 | if (*prev) { |
| 1314 | /* start from the previous element instead from the root */ |
| 1315 | elem = next = *prev; |
| 1316 | pos = *prev_pos; |
| 1317 | for (top_sibling = elem; top_sibling->parent; top_sibling = (struct lyd_node *)top_sibling->parent); |
| 1318 | goto dfs_search; |
| 1319 | } |
| 1320 | |
| 1321 | for (top_sibling = root; top_sibling; top_sibling = top_sibling->next) { |
| 1322 | /* TREE DFS */ |
| 1323 | LYD_TREE_DFS_BEGIN(top_sibling, next, elem) { |
| 1324 | dfs_search: |
| 1325 | if ((root_type == LYXP_NODE_ROOT_CONFIG) && (elem->schema->flags & LYS_CONFIG_R)) { |
| 1326 | goto skip_children; |
| 1327 | } |
| 1328 | |
| 1329 | if (elem == node) { |
| 1330 | break; |
| 1331 | } |
| 1332 | ++pos; |
| 1333 | |
| 1334 | /* TREE DFS END */ |
| 1335 | /* select element for the next run - children first, |
| 1336 | * child exception for lyd_node_leaf and lyd_node_leaflist, but not the root */ |
| 1337 | if (elem->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
| 1338 | next = NULL; |
| 1339 | } else { |
Michal Vasko | 5bfd4be | 2020-06-23 13:26:19 +0200 | [diff] [blame] | 1340 | next = lyd_node_children(elem, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1341 | } |
| 1342 | if (!next) { |
| 1343 | skip_children: |
| 1344 | /* no children */ |
| 1345 | if (elem == top_sibling) { |
| 1346 | /* we are done, root has no children */ |
| 1347 | elem = NULL; |
| 1348 | break; |
| 1349 | } |
| 1350 | /* try siblings */ |
| 1351 | next = elem->next; |
| 1352 | } |
| 1353 | while (!next) { |
| 1354 | /* no siblings, go back through parents */ |
| 1355 | if (elem->parent == top_sibling->parent) { |
| 1356 | /* we are done, no next element to process */ |
| 1357 | elem = NULL; |
| 1358 | break; |
| 1359 | } |
| 1360 | /* parent is already processed, go to its sibling */ |
| 1361 | elem = (struct lyd_node *)elem->parent; |
| 1362 | next = elem->next; |
| 1363 | } |
| 1364 | } |
| 1365 | |
| 1366 | /* node found */ |
| 1367 | if (elem) { |
| 1368 | break; |
| 1369 | } |
| 1370 | } |
| 1371 | |
| 1372 | if (!elem) { |
| 1373 | if (!(*prev)) { |
| 1374 | /* we went from root and failed to find it, cannot be */ |
| 1375 | LOGINT(node->schema->module->ctx); |
| 1376 | return 0; |
| 1377 | } else { |
| 1378 | *prev = NULL; |
| 1379 | *prev_pos = 0; |
| 1380 | |
| 1381 | elem = next = top_sibling = root; |
| 1382 | pos = 1; |
| 1383 | goto dfs_search; |
| 1384 | } |
| 1385 | } |
| 1386 | |
| 1387 | /* remember the last found node for next time */ |
| 1388 | *prev = node; |
| 1389 | *prev_pos = pos; |
| 1390 | |
| 1391 | return pos; |
| 1392 | } |
| 1393 | |
| 1394 | /** |
| 1395 | * @brief Assign (fill) missing node positions. |
| 1396 | * |
| 1397 | * @param[in] set Set to fill positions in. |
| 1398 | * @param[in] root Context root node. |
| 1399 | * @param[in] root_type Context root type. |
| 1400 | * @return LY_ERR |
| 1401 | */ |
| 1402 | static LY_ERR |
| 1403 | set_assign_pos(struct lyxp_set *set, const struct lyd_node *root, enum lyxp_node_type root_type) |
| 1404 | { |
| 1405 | const struct lyd_node *prev = NULL, *tmp_node; |
| 1406 | uint32_t i, tmp_pos = 0; |
| 1407 | |
| 1408 | for (i = 0; i < set->used; ++i) { |
| 1409 | if (!set->val.nodes[i].pos) { |
| 1410 | tmp_node = NULL; |
| 1411 | switch (set->val.nodes[i].type) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1412 | case LYXP_NODE_META: |
| 1413 | tmp_node = set->val.meta[i].meta->parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1414 | if (!tmp_node) { |
| 1415 | LOGINT_RET(root->schema->module->ctx); |
| 1416 | } |
| 1417 | /* fallthrough */ |
| 1418 | case LYXP_NODE_ELEM: |
| 1419 | case LYXP_NODE_TEXT: |
| 1420 | if (!tmp_node) { |
| 1421 | tmp_node = set->val.nodes[i].node; |
| 1422 | } |
| 1423 | set->val.nodes[i].pos = get_node_pos(tmp_node, set->val.nodes[i].type, root, root_type, &prev, &tmp_pos); |
| 1424 | break; |
| 1425 | default: |
| 1426 | /* all roots have position 0 */ |
| 1427 | break; |
| 1428 | } |
| 1429 | } |
| 1430 | } |
| 1431 | |
| 1432 | return LY_SUCCESS; |
| 1433 | } |
| 1434 | |
| 1435 | /** |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1436 | * @brief Get unique @p meta position in the parent metadata. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1437 | * |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1438 | * @param[in] meta Metadata to use. |
| 1439 | * @return Metadata position. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1440 | */ |
| 1441 | static uint16_t |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1442 | get_meta_pos(struct lyd_meta *meta) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1443 | { |
| 1444 | uint16_t pos = 0; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1445 | struct lyd_meta *meta2; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1446 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1447 | for (meta2 = meta->parent->meta; meta2 && (meta2 != meta); meta2 = meta2->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1448 | ++pos; |
| 1449 | } |
| 1450 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1451 | assert(meta2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1452 | return pos; |
| 1453 | } |
| 1454 | |
| 1455 | /** |
| 1456 | * @brief Compare 2 nodes in respect to XPath document order. |
| 1457 | * |
| 1458 | * @param[in] item1 1st node. |
| 1459 | * @param[in] item2 2nd node. |
| 1460 | * @return If 1st > 2nd returns 1, 1st == 2nd returns 0, and 1st < 2nd returns -1. |
| 1461 | */ |
| 1462 | static int |
| 1463 | set_sort_compare(struct lyxp_set_node *item1, struct lyxp_set_node *item2) |
| 1464 | { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1465 | uint32_t meta_pos1 = 0, meta_pos2 = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1466 | |
| 1467 | if (item1->pos < item2->pos) { |
| 1468 | return -1; |
| 1469 | } |
| 1470 | |
| 1471 | if (item1->pos > item2->pos) { |
| 1472 | return 1; |
| 1473 | } |
| 1474 | |
| 1475 | /* node positions are equal, the fun case */ |
| 1476 | |
| 1477 | /* 1st ELEM - == - 2nd TEXT, 1st TEXT - == - 2nd ELEM */ |
| 1478 | /* special case since text nodes are actually saved as their parents */ |
| 1479 | if ((item1->node == item2->node) && (item1->type != item2->type)) { |
| 1480 | if (item1->type == LYXP_NODE_ELEM) { |
| 1481 | assert(item2->type == LYXP_NODE_TEXT); |
| 1482 | return -1; |
| 1483 | } else { |
| 1484 | assert((item1->type == LYXP_NODE_TEXT) && (item2->type == LYXP_NODE_ELEM)); |
| 1485 | return 1; |
| 1486 | } |
| 1487 | } |
| 1488 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1489 | /* we need meta positions now */ |
| 1490 | if (item1->type == LYXP_NODE_META) { |
| 1491 | meta_pos1 = get_meta_pos((struct lyd_meta *)item1->node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1492 | } |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1493 | if (item2->type == LYXP_NODE_META) { |
| 1494 | meta_pos2 = get_meta_pos((struct lyd_meta *)item2->node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1495 | } |
| 1496 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1497 | /* 1st ROOT - 2nd ROOT, 1st ELEM - 2nd ELEM, 1st TEXT - 2nd TEXT, 1st META - =pos= - 2nd META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1498 | /* check for duplicates */ |
| 1499 | if (item1->node == item2->node) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1500 | assert((item1->type == item2->type) && ((item1->type != LYXP_NODE_META) || (meta_pos1 == meta_pos2))); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1501 | return 0; |
| 1502 | } |
| 1503 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1504 | /* 1st ELEM - 2nd TEXT, 1st ELEM - any pos - 2nd META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1505 | /* elem is always first, 2nd node is after it */ |
| 1506 | if (item1->type == LYXP_NODE_ELEM) { |
| 1507 | assert(item2->type != LYXP_NODE_ELEM); |
| 1508 | return -1; |
| 1509 | } |
| 1510 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1511 | /* 1st TEXT - 2nd ELEM, 1st TEXT - any pos - 2nd META, 1st META - any pos - 2nd ELEM, 1st META - >pos> - 2nd META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1512 | /* 2nd is before 1st */ |
| 1513 | if (((item1->type == LYXP_NODE_TEXT) |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1514 | && ((item2->type == LYXP_NODE_ELEM) || (item2->type == LYXP_NODE_META))) |
| 1515 | || ((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_ELEM)) |
| 1516 | || (((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_META)) |
| 1517 | && (meta_pos1 > meta_pos2))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1518 | return 1; |
| 1519 | } |
| 1520 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1521 | /* 1st META - any pos - 2nd TEXT, 1st META <pos< - 2nd META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1522 | /* 2nd is after 1st */ |
| 1523 | return -1; |
| 1524 | } |
| 1525 | |
| 1526 | /** |
| 1527 | * @brief Set cast for comparisons. |
| 1528 | * |
| 1529 | * @param[in] trg Target set to cast source into. |
| 1530 | * @param[in] src Source set. |
| 1531 | * @param[in] type Target set type. |
| 1532 | * @param[in] src_idx Source set node index. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1533 | * @return LY_ERR |
| 1534 | */ |
| 1535 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1536 | set_comp_cast(struct lyxp_set *trg, struct lyxp_set *src, enum lyxp_set_type type, uint32_t src_idx) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1537 | { |
| 1538 | assert(src->type == LYXP_SET_NODE_SET); |
| 1539 | |
| 1540 | set_init(trg, src); |
| 1541 | |
| 1542 | /* insert node into target set */ |
| 1543 | set_insert_node(trg, src->val.nodes[src_idx].node, src->val.nodes[src_idx].pos, src->val.nodes[src_idx].type, 0); |
| 1544 | |
| 1545 | /* cast target set appropriately */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1546 | return lyxp_set_cast(trg, type); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1547 | } |
| 1548 | |
| 1549 | #ifndef NDEBUG |
| 1550 | |
| 1551 | /** |
| 1552 | * @brief Bubble sort @p set into XPath document order. |
| 1553 | * Context position aware. Unused in the 'Release' build target. |
| 1554 | * |
| 1555 | * @param[in] set Set to sort. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1556 | * @return How many times the whole set was traversed - 1 (if set was sorted, returns 0). |
| 1557 | */ |
| 1558 | static int |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1559 | set_sort(struct lyxp_set *set) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1560 | { |
| 1561 | uint32_t i, j; |
| 1562 | int ret = 0, cmp, inverted, change; |
| 1563 | const struct lyd_node *root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1564 | struct lyxp_set_node item; |
| 1565 | struct lyxp_set_hash_node hnode; |
| 1566 | uint64_t hash; |
| 1567 | |
Michal Vasko | 3cf8fbf | 2020-05-27 15:21:21 +0200 | [diff] [blame] | 1568 | if ((set->type != LYXP_SET_NODE_SET) || (set->used < 2)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1569 | return 0; |
| 1570 | } |
| 1571 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1572 | /* find first top-level node to be used as anchor for positions */ |
| 1573 | for (root = set->ctx_node; root->parent; root = (const struct lyd_node *)root->parent); |
| 1574 | for (; root->prev->next; root = root->prev); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1575 | |
| 1576 | /* fill positions */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1577 | if (set_assign_pos(set, root, set->root_type)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1578 | return -1; |
| 1579 | } |
| 1580 | |
| 1581 | LOGDBG(LY_LDGXPATH, "SORT BEGIN"); |
| 1582 | print_set_debug(set); |
| 1583 | |
| 1584 | for (i = 0; i < set->used; ++i) { |
| 1585 | inverted = 0; |
| 1586 | change = 0; |
| 1587 | |
| 1588 | for (j = 1; j < set->used - i; ++j) { |
| 1589 | /* compare node positions */ |
| 1590 | if (inverted) { |
| 1591 | cmp = set_sort_compare(&set->val.nodes[j], &set->val.nodes[j - 1]); |
| 1592 | } else { |
| 1593 | cmp = set_sort_compare(&set->val.nodes[j - 1], &set->val.nodes[j]); |
| 1594 | } |
| 1595 | |
| 1596 | /* swap if needed */ |
| 1597 | if ((inverted && (cmp < 0)) || (!inverted && (cmp > 0))) { |
| 1598 | change = 1; |
| 1599 | |
| 1600 | item = set->val.nodes[j - 1]; |
| 1601 | set->val.nodes[j - 1] = set->val.nodes[j]; |
| 1602 | set->val.nodes[j] = item; |
| 1603 | } else { |
| 1604 | /* whether node_pos1 should be smaller than node_pos2 or the other way around */ |
| 1605 | inverted = !inverted; |
| 1606 | } |
| 1607 | } |
| 1608 | |
| 1609 | ++ret; |
| 1610 | |
| 1611 | if (!change) { |
| 1612 | break; |
| 1613 | } |
| 1614 | } |
| 1615 | |
| 1616 | LOGDBG(LY_LDGXPATH, "SORT END %d", ret); |
| 1617 | print_set_debug(set); |
| 1618 | |
| 1619 | /* check node hashes */ |
| 1620 | if (set->used >= LYD_HT_MIN_ITEMS) { |
| 1621 | assert(set->ht); |
| 1622 | for (i = 0; i < set->used; ++i) { |
| 1623 | hnode.node = set->val.nodes[i].node; |
| 1624 | hnode.type = set->val.nodes[i].type; |
| 1625 | |
| 1626 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 1627 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 1628 | hash = dict_hash_multi(hash, NULL, 0); |
| 1629 | |
| 1630 | assert(!lyht_find(set->ht, &hnode, hash, NULL)); |
| 1631 | } |
| 1632 | } |
| 1633 | |
| 1634 | return ret - 1; |
| 1635 | } |
| 1636 | |
| 1637 | /** |
| 1638 | * @brief Remove duplicate entries in a sorted node set. |
| 1639 | * |
| 1640 | * @param[in] set Sorted set to check. |
| 1641 | * @return LY_ERR (LY_EEXIST if some duplicates are found) |
| 1642 | */ |
| 1643 | static LY_ERR |
| 1644 | set_sorted_dup_node_clean(struct lyxp_set *set) |
| 1645 | { |
| 1646 | uint32_t i = 0; |
| 1647 | LY_ERR ret = LY_SUCCESS; |
| 1648 | |
| 1649 | if (set->used > 1) { |
| 1650 | while (i < set->used - 1) { |
| 1651 | if ((set->val.nodes[i].node == set->val.nodes[i + 1].node) |
| 1652 | && (set->val.nodes[i].type == set->val.nodes[i + 1].type)) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1653 | set_remove_node_none(set, i + 1); |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1654 | ret = LY_EEXIST; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1655 | } |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1656 | ++i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1657 | } |
| 1658 | } |
| 1659 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1660 | set_remove_nodes_none(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1661 | return ret; |
| 1662 | } |
| 1663 | |
| 1664 | #endif |
| 1665 | |
| 1666 | /** |
| 1667 | * @brief Merge 2 sorted sets into one. |
| 1668 | * |
| 1669 | * @param[in,out] trg Set to merge into. Duplicates are removed. |
| 1670 | * @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] | 1671 | * @return LY_ERR |
| 1672 | */ |
| 1673 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1674 | set_sorted_merge(struct lyxp_set *trg, struct lyxp_set *src) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1675 | { |
| 1676 | uint32_t i, j, k, count, dup_count; |
| 1677 | int cmp; |
| 1678 | const struct lyd_node *root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1679 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1680 | 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] | 1681 | return LY_EINVAL; |
| 1682 | } |
| 1683 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1684 | if (!src->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1685 | return LY_SUCCESS; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1686 | } else if (!trg->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1687 | set_fill_set(trg, src); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1688 | lyxp_set_free_content(src); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1689 | return LY_SUCCESS; |
| 1690 | } |
| 1691 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1692 | /* find first top-level node to be used as anchor for positions */ |
| 1693 | for (root = trg->ctx_node; root->parent; root = (const struct lyd_node *)root->parent); |
| 1694 | for (; root->prev->next; root = root->prev); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1695 | |
| 1696 | /* fill positions */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1697 | 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] | 1698 | return LY_EINT; |
| 1699 | } |
| 1700 | |
| 1701 | #ifndef NDEBUG |
| 1702 | LOGDBG(LY_LDGXPATH, "MERGE target"); |
| 1703 | print_set_debug(trg); |
| 1704 | LOGDBG(LY_LDGXPATH, "MERGE source"); |
| 1705 | print_set_debug(src); |
| 1706 | #endif |
| 1707 | |
| 1708 | /* make memory for the merge (duplicates are not detected yet, so space |
| 1709 | * will likely be wasted on them, too bad) */ |
| 1710 | if (trg->size - trg->used < src->used) { |
| 1711 | trg->size = trg->used + src->used; |
| 1712 | |
| 1713 | trg->val.nodes = ly_realloc(trg->val.nodes, trg->size * sizeof *trg->val.nodes); |
| 1714 | LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx), LY_EMEM); |
| 1715 | } |
| 1716 | |
| 1717 | i = 0; |
| 1718 | j = 0; |
| 1719 | count = 0; |
| 1720 | dup_count = 0; |
| 1721 | do { |
| 1722 | cmp = set_sort_compare(&src->val.nodes[i], &trg->val.nodes[j]); |
| 1723 | if (!cmp) { |
| 1724 | if (!count) { |
| 1725 | /* duplicate, just skip it */ |
| 1726 | ++i; |
| 1727 | ++j; |
| 1728 | } else { |
| 1729 | /* we are copying something already, so let's copy the duplicate too, |
| 1730 | * we are hoping that afterwards there are some more nodes to |
| 1731 | * copy and this way we can copy them all together */ |
| 1732 | ++count; |
| 1733 | ++dup_count; |
| 1734 | ++i; |
| 1735 | ++j; |
| 1736 | } |
| 1737 | } else if (cmp < 0) { |
| 1738 | /* inserting src node into trg, just remember it for now */ |
| 1739 | ++count; |
| 1740 | ++i; |
| 1741 | |
| 1742 | /* insert the hash now */ |
| 1743 | set_insert_node_hash(trg, src->val.nodes[i - 1].node, src->val.nodes[i - 1].type); |
| 1744 | } else if (count) { |
| 1745 | copy_nodes: |
| 1746 | /* time to actually copy the nodes, we have found the largest block of nodes */ |
| 1747 | memmove(&trg->val.nodes[j + (count - dup_count)], |
| 1748 | &trg->val.nodes[j], |
| 1749 | (trg->used - j) * sizeof *trg->val.nodes); |
| 1750 | memcpy(&trg->val.nodes[j - dup_count], &src->val.nodes[i - count], count * sizeof *src->val.nodes); |
| 1751 | |
| 1752 | trg->used += count - dup_count; |
| 1753 | /* do not change i, except the copying above, we are basically doing exactly what is in the else branch below */ |
| 1754 | j += count - dup_count; |
| 1755 | |
| 1756 | count = 0; |
| 1757 | dup_count = 0; |
| 1758 | } else { |
| 1759 | ++j; |
| 1760 | } |
| 1761 | } while ((i < src->used) && (j < trg->used)); |
| 1762 | |
| 1763 | if ((i < src->used) || count) { |
| 1764 | /* insert all the hashes first */ |
| 1765 | for (k = i; k < src->used; ++k) { |
| 1766 | set_insert_node_hash(trg, src->val.nodes[k].node, src->val.nodes[k].type); |
| 1767 | } |
| 1768 | |
| 1769 | /* loop ended, but we need to copy something at trg end */ |
| 1770 | count += src->used - i; |
| 1771 | i = src->used; |
| 1772 | goto copy_nodes; |
| 1773 | } |
| 1774 | |
| 1775 | /* we are inserting hashes before the actual node insert, which causes |
| 1776 | * situations when there were initially not enough items for a hash table, |
| 1777 | * but even after some were inserted, hash table was not created (during |
| 1778 | * insertion the number of items is not updated yet) */ |
| 1779 | if (!trg->ht && (trg->used >= LYD_HT_MIN_ITEMS)) { |
| 1780 | set_insert_node_hash(trg, NULL, 0); |
| 1781 | } |
| 1782 | |
| 1783 | #ifndef NDEBUG |
| 1784 | LOGDBG(LY_LDGXPATH, "MERGE result"); |
| 1785 | print_set_debug(trg); |
| 1786 | #endif |
| 1787 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1788 | lyxp_set_free_content(src); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1789 | return LY_SUCCESS; |
| 1790 | } |
| 1791 | |
| 1792 | /* |
| 1793 | * (re)parse functions |
| 1794 | * |
| 1795 | * Parse functions parse the expression into |
| 1796 | * tokens (syntactic analysis). |
| 1797 | * |
| 1798 | * Reparse functions perform semantic analysis |
| 1799 | * (do not save the result, just a check) of |
| 1800 | * the expression and fill repeat indices. |
| 1801 | */ |
| 1802 | |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1803 | LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1804 | 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] | 1805 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1806 | if (exp->used == tok_idx) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1807 | if (ctx) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1808 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF); |
| 1809 | } |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1810 | return LY_EINCOMPLETE; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1811 | } |
| 1812 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1813 | if (want_tok && (exp->tokens[tok_idx] != want_tok)) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1814 | if (ctx) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1815 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1816 | 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] | 1817 | } |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1818 | return LY_ENOT; |
| 1819 | } |
| 1820 | |
| 1821 | return LY_SUCCESS; |
| 1822 | } |
| 1823 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1824 | LY_ERR |
| 1825 | lyxp_next_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok) |
| 1826 | { |
| 1827 | LY_CHECK_RET(lyxp_check_token(ctx, exp, *tok_idx, want_tok)); |
| 1828 | |
| 1829 | /* skip the token */ |
| 1830 | ++(*tok_idx); |
| 1831 | |
| 1832 | return LY_SUCCESS; |
| 1833 | } |
| 1834 | |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1835 | /* just like lyxp_check_token() but tests for 2 tokens */ |
| 1836 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1837 | 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] | 1838 | enum lyxp_token want_tok2) |
| 1839 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1840 | if (exp->used == tok_idx) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1841 | if (ctx) { |
| 1842 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF); |
| 1843 | } |
| 1844 | return LY_EINCOMPLETE; |
| 1845 | } |
| 1846 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1847 | 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] | 1848 | if (ctx) { |
| 1849 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1850 | 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] | 1851 | } |
| 1852 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1853 | } |
| 1854 | |
| 1855 | return LY_SUCCESS; |
| 1856 | } |
| 1857 | |
| 1858 | /** |
| 1859 | * @brief Stack operation push on the repeat array. |
| 1860 | * |
| 1861 | * @param[in] exp Expression to use. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1862 | * @param[in] tok_idx Position in the expresion \p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1863 | * @param[in] repeat_op_idx Index from \p exp of the operator token. This value is pushed. |
| 1864 | */ |
| 1865 | static void |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1866 | 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] | 1867 | { |
| 1868 | uint16_t i; |
| 1869 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1870 | if (exp->repeat[tok_idx]) { |
| 1871 | for (i = 0; exp->repeat[tok_idx][i]; ++i); |
| 1872 | exp->repeat[tok_idx] = realloc(exp->repeat[tok_idx], (i + 2) * sizeof *exp->repeat[tok_idx]); |
| 1873 | LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), ); |
| 1874 | exp->repeat[tok_idx][i] = repeat_op_idx; |
| 1875 | exp->repeat[tok_idx][i + 1] = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1876 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1877 | exp->repeat[tok_idx] = calloc(2, sizeof *exp->repeat[tok_idx]); |
| 1878 | LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), ); |
| 1879 | exp->repeat[tok_idx][0] = repeat_op_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1880 | } |
| 1881 | } |
| 1882 | |
| 1883 | /** |
| 1884 | * @brief Reparse Predicate. Logs directly on error. |
| 1885 | * |
| 1886 | * [7] Predicate ::= '[' Expr ']' |
| 1887 | * |
| 1888 | * @param[in] ctx Context for logging. |
| 1889 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1890 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1891 | * @return LY_ERR |
| 1892 | */ |
| 1893 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1894 | 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] | 1895 | { |
| 1896 | LY_ERR rc; |
| 1897 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1898 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1899 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1900 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1901 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1902 | rc = reparse_or_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1903 | LY_CHECK_RET(rc); |
| 1904 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1905 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1906 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1907 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1908 | |
| 1909 | return LY_SUCCESS; |
| 1910 | } |
| 1911 | |
| 1912 | /** |
| 1913 | * @brief Reparse RelativeLocationPath. Logs directly on error. |
| 1914 | * |
| 1915 | * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step |
| 1916 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 1917 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 1918 | * |
| 1919 | * @param[in] ctx Context for logging. |
| 1920 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1921 | * @param[in] tok_idx Position in the expression \p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1922 | * @return LY_ERR (LY_EINCOMPLETE on forward reference) |
| 1923 | */ |
| 1924 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1925 | 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] | 1926 | { |
| 1927 | LY_ERR 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_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1930 | LY_CHECK_RET(rc); |
| 1931 | |
| 1932 | goto step; |
| 1933 | do { |
| 1934 | /* '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1935 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1936 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1937 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1938 | LY_CHECK_RET(rc); |
| 1939 | step: |
| 1940 | /* Step */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1941 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1942 | case LYXP_TOKEN_DOT: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1943 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1944 | break; |
| 1945 | |
| 1946 | case LYXP_TOKEN_DDOT: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1947 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1948 | break; |
| 1949 | |
| 1950 | case LYXP_TOKEN_AT: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1951 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 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); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1955 | 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] | 1956 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1957 | 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] | 1958 | return LY_EVALID; |
| 1959 | } |
| 1960 | /* fall through */ |
| 1961 | case LYXP_TOKEN_NAMETEST: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1962 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1963 | goto reparse_predicate; |
| 1964 | break; |
| 1965 | |
| 1966 | case LYXP_TOKEN_NODETYPE: |
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 | |
| 1969 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1970 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1971 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1972 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1973 | |
| 1974 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1975 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1976 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1977 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1978 | |
| 1979 | reparse_predicate: |
| 1980 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1981 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 1982 | rc = reparse_predicate(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1983 | LY_CHECK_RET(rc); |
| 1984 | } |
| 1985 | break; |
| 1986 | default: |
| 1987 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1988 | 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] | 1989 | return LY_EVALID; |
| 1990 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1991 | } 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] | 1992 | |
| 1993 | return LY_SUCCESS; |
| 1994 | } |
| 1995 | |
| 1996 | /** |
| 1997 | * @brief Reparse AbsoluteLocationPath. Logs directly on error. |
| 1998 | * |
| 1999 | * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath |
| 2000 | * |
| 2001 | * @param[in] ctx Context for logging. |
| 2002 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2003 | * @param[in] tok_idx Position in the expression \p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2004 | * @return LY_ERR |
| 2005 | */ |
| 2006 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2007 | 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] | 2008 | { |
| 2009 | LY_ERR rc; |
| 2010 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2011 | 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] | 2012 | |
| 2013 | /* '/' RelativeLocationPath? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2014 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2015 | /* '/' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2016 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2017 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2018 | if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2019 | return LY_SUCCESS; |
| 2020 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2021 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2022 | case LYXP_TOKEN_DOT: |
| 2023 | case LYXP_TOKEN_DDOT: |
| 2024 | case LYXP_TOKEN_AT: |
| 2025 | case LYXP_TOKEN_NAMETEST: |
| 2026 | case LYXP_TOKEN_NODETYPE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2027 | rc = reparse_relative_location_path(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2028 | LY_CHECK_RET(rc); |
| 2029 | /* fall through */ |
| 2030 | default: |
| 2031 | break; |
| 2032 | } |
| 2033 | |
| 2034 | /* '//' RelativeLocationPath */ |
| 2035 | } else { |
| 2036 | /* '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2037 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2038 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2039 | rc = reparse_relative_location_path(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2040 | LY_CHECK_RET(rc); |
| 2041 | } |
| 2042 | |
| 2043 | return LY_SUCCESS; |
| 2044 | } |
| 2045 | |
| 2046 | /** |
| 2047 | * @brief Reparse FunctionCall. Logs directly on error. |
| 2048 | * |
| 2049 | * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')' |
| 2050 | * |
| 2051 | * @param[in] ctx Context for logging. |
| 2052 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2053 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2054 | * @return LY_ERR |
| 2055 | */ |
| 2056 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2057 | reparse_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] | 2058 | { |
| 2059 | int min_arg_count = -1, max_arg_count, arg_count; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2060 | uint16_t func_tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2061 | LY_ERR rc; |
| 2062 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2063 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2064 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2065 | func_tok_idx = *tok_idx; |
| 2066 | switch (exp->tok_len[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2067 | case 3: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2068 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2069 | min_arg_count = 1; |
| 2070 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2071 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2072 | min_arg_count = 1; |
| 2073 | max_arg_count = 1; |
| 2074 | } |
| 2075 | break; |
| 2076 | case 4: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2077 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2078 | min_arg_count = 1; |
| 2079 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2080 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2081 | min_arg_count = 0; |
| 2082 | max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2083 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2084 | min_arg_count = 0; |
| 2085 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2086 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2087 | min_arg_count = 0; |
| 2088 | max_arg_count = 0; |
| 2089 | } |
| 2090 | break; |
| 2091 | case 5: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2092 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) { |
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]], "false", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2096 | min_arg_count = 0; |
| 2097 | max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2098 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2099 | min_arg_count = 1; |
| 2100 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2101 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) { |
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]], "deref", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2105 | min_arg_count = 1; |
| 2106 | max_arg_count = 1; |
| 2107 | } |
| 2108 | break; |
| 2109 | case 6: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2110 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2111 | min_arg_count = 2; |
Michal Vasko | be2e356 | 2019-10-15 15:35:35 +0200 | [diff] [blame] | 2112 | max_arg_count = INT_MAX; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2113 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2114 | min_arg_count = 0; |
| 2115 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2116 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2117 | min_arg_count = 0; |
| 2118 | max_arg_count = 1; |
| 2119 | } |
| 2120 | break; |
| 2121 | case 7: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2122 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) { |
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]], "ceiling", 7)) { |
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]], "current", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2129 | min_arg_count = 0; |
| 2130 | max_arg_count = 0; |
| 2131 | } |
| 2132 | break; |
| 2133 | case 8: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2134 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2135 | min_arg_count = 2; |
| 2136 | max_arg_count = 2; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2137 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2138 | min_arg_count = 0; |
| 2139 | max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2140 | } 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] | 2141 | min_arg_count = 2; |
| 2142 | max_arg_count = 2; |
| 2143 | } |
| 2144 | break; |
| 2145 | case 9: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2146 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2147 | min_arg_count = 2; |
| 2148 | max_arg_count = 3; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2149 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2150 | min_arg_count = 3; |
| 2151 | max_arg_count = 3; |
| 2152 | } |
| 2153 | break; |
| 2154 | case 10: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2155 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2156 | min_arg_count = 0; |
| 2157 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2158 | } 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] | 2159 | min_arg_count = 1; |
| 2160 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2161 | } 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] | 2162 | min_arg_count = 2; |
| 2163 | max_arg_count = 2; |
| 2164 | } |
| 2165 | break; |
| 2166 | case 11: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2167 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2168 | min_arg_count = 2; |
| 2169 | max_arg_count = 2; |
| 2170 | } |
| 2171 | break; |
| 2172 | case 12: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2173 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2174 | min_arg_count = 2; |
| 2175 | max_arg_count = 2; |
| 2176 | } |
| 2177 | break; |
| 2178 | case 13: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2179 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) { |
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]], "string-length", 13)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2183 | min_arg_count = 0; |
| 2184 | max_arg_count = 1; |
| 2185 | } |
| 2186 | break; |
| 2187 | case 15: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2188 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2189 | min_arg_count = 0; |
| 2190 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2191 | } 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] | 2192 | min_arg_count = 2; |
| 2193 | max_arg_count = 2; |
| 2194 | } |
| 2195 | break; |
| 2196 | case 16: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2197 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) { |
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 20: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2203 | 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] | 2204 | min_arg_count = 2; |
| 2205 | max_arg_count = 2; |
| 2206 | } |
| 2207 | break; |
| 2208 | } |
| 2209 | if (min_arg_count == -1) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2210 | 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] | 2211 | return LY_EINVAL; |
| 2212 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2213 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2214 | |
| 2215 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2216 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2217 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2218 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2219 | |
| 2220 | /* ( Expr ( ',' Expr )* )? */ |
| 2221 | arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2222 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2223 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2224 | if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2225 | ++arg_count; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2226 | rc = reparse_or_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2227 | LY_CHECK_RET(rc); |
| 2228 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2229 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) { |
| 2230 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2231 | |
| 2232 | ++arg_count; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2233 | rc = reparse_or_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2234 | LY_CHECK_RET(rc); |
| 2235 | } |
| 2236 | |
| 2237 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2238 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2239 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2240 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2241 | |
| 2242 | if ((arg_count < min_arg_count) || (arg_count > max_arg_count)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2243 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INARGCOUNT, arg_count, exp->tok_len[func_tok_idx], |
| 2244 | &exp->expr[exp->tok_pos[func_tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2245 | return LY_EVALID; |
| 2246 | } |
| 2247 | |
| 2248 | return LY_SUCCESS; |
| 2249 | } |
| 2250 | |
| 2251 | /** |
| 2252 | * @brief Reparse PathExpr. Logs directly on error. |
| 2253 | * |
| 2254 | * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate* |
| 2255 | * | PrimaryExpr Predicate* '/' RelativeLocationPath |
| 2256 | * | PrimaryExpr Predicate* '//' RelativeLocationPath |
| 2257 | * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath |
| 2258 | * [8] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall |
| 2259 | * |
| 2260 | * @param[in] ctx Context for logging. |
| 2261 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2262 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2263 | * @return LY_ERR |
| 2264 | */ |
| 2265 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2266 | 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] | 2267 | { |
| 2268 | LY_ERR rc; |
| 2269 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2270 | if (lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 2271 | return LY_EVALID; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2272 | } |
| 2273 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2274 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2275 | case LYXP_TOKEN_PAR1: |
| 2276 | /* '(' Expr ')' Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2277 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2278 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2279 | rc = reparse_or_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2280 | LY_CHECK_RET(rc); |
| 2281 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2282 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2283 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2284 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2285 | goto predicate; |
| 2286 | break; |
| 2287 | case LYXP_TOKEN_DOT: |
| 2288 | case LYXP_TOKEN_DDOT: |
| 2289 | case LYXP_TOKEN_AT: |
| 2290 | case LYXP_TOKEN_NAMETEST: |
| 2291 | case LYXP_TOKEN_NODETYPE: |
| 2292 | /* RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2293 | rc = reparse_relative_location_path(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2294 | LY_CHECK_RET(rc); |
| 2295 | break; |
| 2296 | case LYXP_TOKEN_FUNCNAME: |
| 2297 | /* FunctionCall */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2298 | rc = reparse_function_call(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2299 | LY_CHECK_RET(rc); |
| 2300 | goto predicate; |
| 2301 | break; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2302 | case LYXP_TOKEN_OPER_PATH: |
| 2303 | case LYXP_TOKEN_OPER_RPATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2304 | /* AbsoluteLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2305 | rc = reparse_absolute_location_path(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2306 | LY_CHECK_RET(rc); |
| 2307 | break; |
| 2308 | case LYXP_TOKEN_LITERAL: |
| 2309 | /* Literal */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2310 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2311 | goto predicate; |
| 2312 | break; |
| 2313 | case LYXP_TOKEN_NUMBER: |
| 2314 | /* Number */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2315 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2316 | goto predicate; |
| 2317 | break; |
| 2318 | default: |
| 2319 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2320 | 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] | 2321 | return LY_EVALID; |
| 2322 | } |
| 2323 | |
| 2324 | return LY_SUCCESS; |
| 2325 | |
| 2326 | predicate: |
| 2327 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2328 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 2329 | rc = reparse_predicate(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2330 | LY_CHECK_RET(rc); |
| 2331 | } |
| 2332 | |
| 2333 | /* ('/' or '//') RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2334 | 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] | 2335 | |
| 2336 | /* '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2337 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2338 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2339 | rc = reparse_relative_location_path(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2340 | LY_CHECK_RET(rc); |
| 2341 | } |
| 2342 | |
| 2343 | return LY_SUCCESS; |
| 2344 | } |
| 2345 | |
| 2346 | /** |
| 2347 | * @brief Reparse UnaryExpr. Logs directly on error. |
| 2348 | * |
| 2349 | * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr |
| 2350 | * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr |
| 2351 | * |
| 2352 | * @param[in] ctx Context for logging. |
| 2353 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2354 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2355 | * @return LY_ERR |
| 2356 | */ |
| 2357 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2358 | 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] | 2359 | { |
| 2360 | uint16_t prev_exp; |
| 2361 | LY_ERR rc; |
| 2362 | |
| 2363 | /* ('-')* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2364 | prev_exp = *tok_idx; |
| 2365 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) |
| 2366 | && (exp->expr[exp->tok_pos[*tok_idx]] == '-')) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2367 | exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2368 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2369 | } |
| 2370 | |
| 2371 | /* PathExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2372 | prev_exp = *tok_idx; |
| 2373 | rc = reparse_path_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2374 | LY_CHECK_RET(rc); |
| 2375 | |
| 2376 | /* ('|' PathExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2377 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_UNI)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2378 | exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2379 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2380 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2381 | rc = reparse_path_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2382 | LY_CHECK_RET(rc); |
| 2383 | } |
| 2384 | |
| 2385 | return LY_SUCCESS; |
| 2386 | } |
| 2387 | |
| 2388 | /** |
| 2389 | * @brief Reparse AdditiveExpr. Logs directly on error. |
| 2390 | * |
| 2391 | * [15] AdditiveExpr ::= MultiplicativeExpr |
| 2392 | * | AdditiveExpr '+' MultiplicativeExpr |
| 2393 | * | AdditiveExpr '-' MultiplicativeExpr |
| 2394 | * [16] MultiplicativeExpr ::= UnaryExpr |
| 2395 | * | MultiplicativeExpr '*' UnaryExpr |
| 2396 | * | MultiplicativeExpr 'div' UnaryExpr |
| 2397 | * | MultiplicativeExpr 'mod' UnaryExpr |
| 2398 | * |
| 2399 | * @param[in] ctx Context for logging. |
| 2400 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2401 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2402 | * @return LY_ERR |
| 2403 | */ |
| 2404 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2405 | 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] | 2406 | { |
| 2407 | uint16_t prev_add_exp, prev_mul_exp; |
| 2408 | LY_ERR rc; |
| 2409 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2410 | prev_add_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2411 | goto reparse_multiplicative_expr; |
| 2412 | |
| 2413 | /* ('+' / '-' MultiplicativeExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2414 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) |
| 2415 | && ((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] | 2416 | exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2417 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2418 | |
| 2419 | reparse_multiplicative_expr: |
| 2420 | /* UnaryExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2421 | prev_mul_exp = *tok_idx; |
| 2422 | rc = reparse_unary_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2423 | LY_CHECK_RET(rc); |
| 2424 | |
| 2425 | /* ('*' / 'div' / 'mod' UnaryExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2426 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) |
| 2427 | && ((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] | 2428 | exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2429 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2430 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2431 | rc = reparse_unary_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2432 | LY_CHECK_RET(rc); |
| 2433 | } |
| 2434 | } |
| 2435 | |
| 2436 | return LY_SUCCESS; |
| 2437 | } |
| 2438 | |
| 2439 | /** |
| 2440 | * @brief Reparse EqualityExpr. Logs directly on error. |
| 2441 | * |
| 2442 | * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr |
| 2443 | * | EqualityExpr '!=' RelationalExpr |
| 2444 | * [14] RelationalExpr ::= AdditiveExpr |
| 2445 | * | RelationalExpr '<' AdditiveExpr |
| 2446 | * | RelationalExpr '>' AdditiveExpr |
| 2447 | * | RelationalExpr '<=' AdditiveExpr |
| 2448 | * | RelationalExpr '>=' AdditiveExpr |
| 2449 | * |
| 2450 | * @param[in] ctx Context for logging. |
| 2451 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2452 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2453 | * @return LY_ERR |
| 2454 | */ |
| 2455 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2456 | 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] | 2457 | { |
| 2458 | uint16_t prev_eq_exp, prev_rel_exp; |
| 2459 | LY_ERR rc; |
| 2460 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2461 | prev_eq_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2462 | goto reparse_additive_expr; |
| 2463 | |
| 2464 | /* ('=' / '!=' RelationalExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2465 | 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] | 2466 | exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2467 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2468 | |
| 2469 | reparse_additive_expr: |
| 2470 | /* AdditiveExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2471 | prev_rel_exp = *tok_idx; |
| 2472 | rc = reparse_additive_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2473 | LY_CHECK_RET(rc); |
| 2474 | |
| 2475 | /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2476 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_COMP)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2477 | exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2478 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2479 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2480 | rc = reparse_additive_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2481 | LY_CHECK_RET(rc); |
| 2482 | } |
| 2483 | } |
| 2484 | |
| 2485 | return LY_SUCCESS; |
| 2486 | } |
| 2487 | |
| 2488 | /** |
| 2489 | * @brief Reparse OrExpr. Logs directly on error. |
| 2490 | * |
| 2491 | * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr |
| 2492 | * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr |
| 2493 | * |
| 2494 | * @param[in] ctx Context for logging. |
| 2495 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2496 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2497 | * @return LY_ERR |
| 2498 | */ |
| 2499 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2500 | 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] | 2501 | { |
| 2502 | uint16_t prev_or_exp, prev_and_exp; |
| 2503 | LY_ERR rc; |
| 2504 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2505 | prev_or_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2506 | goto reparse_equality_expr; |
| 2507 | |
| 2508 | /* ('or' AndExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2509 | 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] | 2510 | exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2511 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2512 | |
| 2513 | reparse_equality_expr: |
| 2514 | /* EqualityExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2515 | prev_and_exp = *tok_idx; |
| 2516 | rc = reparse_equality_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2517 | LY_CHECK_RET(rc); |
| 2518 | |
| 2519 | /* ('and' EqualityExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2520 | 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] | 2521 | exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2522 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2523 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2524 | rc = reparse_equality_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2525 | LY_CHECK_RET(rc); |
| 2526 | } |
| 2527 | } |
| 2528 | |
| 2529 | return LY_SUCCESS; |
| 2530 | } |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2531 | |
| 2532 | /** |
| 2533 | * @brief Parse NCName. |
| 2534 | * |
| 2535 | * @param[in] ncname Name to parse. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2536 | * @return Length of @p ncname valid bytes. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2537 | */ |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2538 | static long int |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2539 | parse_ncname(const char *ncname) |
| 2540 | { |
| 2541 | unsigned int uc; |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2542 | size_t size; |
| 2543 | long int len = 0; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2544 | |
| 2545 | LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0); |
| 2546 | if (!is_xmlqnamestartchar(uc) || (uc == ':')) { |
| 2547 | return len; |
| 2548 | } |
| 2549 | |
| 2550 | do { |
| 2551 | len += size; |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 2552 | if (!*ncname) { |
| 2553 | break; |
| 2554 | } |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2555 | LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2556 | } while (is_xmlqnamechar(uc) && (uc != ':')); |
| 2557 | |
| 2558 | return len; |
| 2559 | } |
| 2560 | |
| 2561 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2562 | * @brief Add @p token into the expression @p exp. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2563 | * |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2564 | * @param[in] ctx Context for logging. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2565 | * @param[in] exp Expression to use. |
| 2566 | * @param[in] token Token to add. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2567 | * @param[in] tok_pos Token position in the XPath expression. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2568 | * @param[in] tok_len Token length in the XPath expression. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2569 | * @return LY_ERR |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2570 | */ |
| 2571 | static LY_ERR |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 2572 | 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] | 2573 | { |
| 2574 | uint32_t prev; |
| 2575 | |
| 2576 | if (exp->used == exp->size) { |
| 2577 | prev = exp->size; |
| 2578 | exp->size += LYXP_EXPR_SIZE_STEP; |
| 2579 | if (prev > exp->size) { |
| 2580 | LOGINT(ctx); |
| 2581 | return LY_EINT; |
| 2582 | } |
| 2583 | |
| 2584 | exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens); |
| 2585 | LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM); |
| 2586 | exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos); |
| 2587 | LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM); |
| 2588 | exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len); |
| 2589 | LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM); |
| 2590 | } |
| 2591 | |
| 2592 | exp->tokens[exp->used] = token; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2593 | exp->tok_pos[exp->used] = tok_pos; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2594 | exp->tok_len[exp->used] = tok_len; |
| 2595 | ++exp->used; |
| 2596 | return LY_SUCCESS; |
| 2597 | } |
| 2598 | |
| 2599 | void |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 2600 | lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr) |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2601 | { |
| 2602 | uint16_t i; |
| 2603 | |
| 2604 | if (!expr) { |
| 2605 | return; |
| 2606 | } |
| 2607 | |
| 2608 | lydict_remove(ctx, expr->expr); |
| 2609 | free(expr->tokens); |
| 2610 | free(expr->tok_pos); |
| 2611 | free(expr->tok_len); |
| 2612 | if (expr->repeat) { |
| 2613 | for (i = 0; i < expr->used; ++i) { |
| 2614 | free(expr->repeat[i]); |
| 2615 | } |
| 2616 | } |
| 2617 | free(expr->repeat); |
| 2618 | free(expr); |
| 2619 | } |
| 2620 | |
| 2621 | struct lyxp_expr * |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2622 | 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] | 2623 | { |
| 2624 | struct lyxp_expr *ret; |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2625 | size_t parsed = 0, tok_len; |
| 2626 | long int ncname_len; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2627 | enum lyxp_token tok_type; |
| 2628 | int prev_function_check = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2629 | uint16_t tok_idx = 0; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2630 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2631 | if (!expr[0]) { |
| 2632 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF); |
| 2633 | return NULL; |
| 2634 | } |
| 2635 | |
| 2636 | if (!expr_len) { |
| 2637 | expr_len = strlen(expr); |
| 2638 | } |
| 2639 | if (expr_len > UINT16_MAX) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2640 | LOGERR(ctx, LY_EINVAL, "XPath expression cannot be longer than %ud characters.", UINT16_MAX); |
| 2641 | return NULL; |
| 2642 | } |
| 2643 | |
| 2644 | /* init lyxp_expr structure */ |
| 2645 | ret = calloc(1, sizeof *ret); |
| 2646 | LY_CHECK_ERR_GOTO(!ret, LOGMEM(ctx), error); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2647 | ret->expr = lydict_insert(ctx, expr, expr_len); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2648 | LY_CHECK_ERR_GOTO(!ret->expr, LOGMEM(ctx), error); |
| 2649 | ret->used = 0; |
| 2650 | ret->size = LYXP_EXPR_SIZE_START; |
| 2651 | ret->tokens = malloc(ret->size * sizeof *ret->tokens); |
| 2652 | LY_CHECK_ERR_GOTO(!ret->tokens, LOGMEM(ctx), error); |
| 2653 | |
| 2654 | ret->tok_pos = malloc(ret->size * sizeof *ret->tok_pos); |
| 2655 | LY_CHECK_ERR_GOTO(!ret->tok_pos, LOGMEM(ctx), error); |
| 2656 | |
| 2657 | ret->tok_len = malloc(ret->size * sizeof *ret->tok_len); |
| 2658 | LY_CHECK_ERR_GOTO(!ret->tok_len, LOGMEM(ctx), error); |
| 2659 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2660 | /* make expr 0-terminated */ |
| 2661 | expr = ret->expr; |
| 2662 | |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2663 | while (is_xmlws(expr[parsed])) { |
| 2664 | ++parsed; |
| 2665 | } |
| 2666 | |
| 2667 | do { |
| 2668 | if (expr[parsed] == '(') { |
| 2669 | |
| 2670 | /* '(' */ |
| 2671 | tok_len = 1; |
| 2672 | tok_type = LYXP_TOKEN_PAR1; |
| 2673 | |
| 2674 | if (prev_function_check && ret->used && (ret->tokens[ret->used - 1] == LYXP_TOKEN_NAMETEST)) { |
| 2675 | /* it is a NodeType/FunctionName after all */ |
| 2676 | if (((ret->tok_len[ret->used - 1] == 4) |
| 2677 | && (!strncmp(&expr[ret->tok_pos[ret->used - 1]], "node", 4) |
| 2678 | || !strncmp(&expr[ret->tok_pos[ret->used - 1]], "text", 4))) || |
| 2679 | ((ret->tok_len[ret->used - 1] == 7) |
| 2680 | && !strncmp(&expr[ret->tok_pos[ret->used - 1]], "comment", 7))) { |
| 2681 | ret->tokens[ret->used - 1] = LYXP_TOKEN_NODETYPE; |
| 2682 | } else { |
| 2683 | ret->tokens[ret->used - 1] = LYXP_TOKEN_FUNCNAME; |
| 2684 | } |
| 2685 | prev_function_check = 0; |
| 2686 | } |
| 2687 | |
| 2688 | } else if (expr[parsed] == ')') { |
| 2689 | |
| 2690 | /* ')' */ |
| 2691 | tok_len = 1; |
| 2692 | tok_type = LYXP_TOKEN_PAR2; |
| 2693 | |
| 2694 | } else if (expr[parsed] == '[') { |
| 2695 | |
| 2696 | /* '[' */ |
| 2697 | tok_len = 1; |
| 2698 | tok_type = LYXP_TOKEN_BRACK1; |
| 2699 | |
| 2700 | } else if (expr[parsed] == ']') { |
| 2701 | |
| 2702 | /* ']' */ |
| 2703 | tok_len = 1; |
| 2704 | tok_type = LYXP_TOKEN_BRACK2; |
| 2705 | |
| 2706 | } else if (!strncmp(&expr[parsed], "..", 2)) { |
| 2707 | |
| 2708 | /* '..' */ |
| 2709 | tok_len = 2; |
| 2710 | tok_type = LYXP_TOKEN_DDOT; |
| 2711 | |
| 2712 | } else if ((expr[parsed] == '.') && (!isdigit(expr[parsed + 1]))) { |
| 2713 | |
| 2714 | /* '.' */ |
| 2715 | tok_len = 1; |
| 2716 | tok_type = LYXP_TOKEN_DOT; |
| 2717 | |
| 2718 | } else if (expr[parsed] == '@') { |
| 2719 | |
| 2720 | /* '@' */ |
| 2721 | tok_len = 1; |
| 2722 | tok_type = LYXP_TOKEN_AT; |
| 2723 | |
| 2724 | } else if (expr[parsed] == ',') { |
| 2725 | |
| 2726 | /* ',' */ |
| 2727 | tok_len = 1; |
| 2728 | tok_type = LYXP_TOKEN_COMMA; |
| 2729 | |
| 2730 | } else if (expr[parsed] == '\'') { |
| 2731 | |
| 2732 | /* Literal with ' */ |
| 2733 | for (tok_len = 1; (expr[parsed + tok_len] != '\0') && (expr[parsed + tok_len] != '\''); ++tok_len); |
| 2734 | LY_CHECK_ERR_GOTO(expr[parsed + tok_len] == '\0', |
| 2735 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOE, expr[parsed], &expr[parsed]), error); |
| 2736 | ++tok_len; |
| 2737 | tok_type = LYXP_TOKEN_LITERAL; |
| 2738 | |
| 2739 | } else if (expr[parsed] == '\"') { |
| 2740 | |
| 2741 | /* Literal with " */ |
| 2742 | for (tok_len = 1; (expr[parsed + tok_len] != '\0') && (expr[parsed + tok_len] != '\"'); ++tok_len); |
| 2743 | LY_CHECK_ERR_GOTO(expr[parsed + tok_len] == '\0', |
| 2744 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOE, expr[parsed], &expr[parsed]), error); |
| 2745 | ++tok_len; |
| 2746 | tok_type = LYXP_TOKEN_LITERAL; |
| 2747 | |
| 2748 | } else if ((expr[parsed] == '.') || (isdigit(expr[parsed]))) { |
| 2749 | |
| 2750 | /* Number */ |
| 2751 | for (tok_len = 0; isdigit(expr[parsed + tok_len]); ++tok_len); |
| 2752 | if (expr[parsed + tok_len] == '.') { |
| 2753 | ++tok_len; |
| 2754 | for (; isdigit(expr[parsed + tok_len]); ++tok_len); |
| 2755 | } |
| 2756 | tok_type = LYXP_TOKEN_NUMBER; |
| 2757 | |
| 2758 | } else if (expr[parsed] == '/') { |
| 2759 | |
| 2760 | /* Operator '/', '//' */ |
| 2761 | if (!strncmp(&expr[parsed], "//", 2)) { |
| 2762 | tok_len = 2; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2763 | tok_type = LYXP_TOKEN_OPER_RPATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2764 | } else { |
| 2765 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2766 | tok_type = LYXP_TOKEN_OPER_PATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2767 | } |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2768 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2769 | } else if (!strncmp(&expr[parsed], "!=", 2)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2770 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2771 | /* Operator '!=' */ |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2772 | tok_len = 2; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2773 | tok_type = LYXP_TOKEN_OPER_NEQUAL; |
| 2774 | |
| 2775 | } else if (!strncmp(&expr[parsed], "<=", 2) || !strncmp(&expr[parsed], ">=", 2)) { |
| 2776 | |
| 2777 | /* Operator '<=', '>=' */ |
| 2778 | tok_len = 2; |
| 2779 | tok_type = LYXP_TOKEN_OPER_COMP; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2780 | |
| 2781 | } else if (expr[parsed] == '|') { |
| 2782 | |
| 2783 | /* Operator '|' */ |
| 2784 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2785 | tok_type = LYXP_TOKEN_OPER_UNI; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2786 | |
| 2787 | } else if ((expr[parsed] == '+') || (expr[parsed] == '-')) { |
| 2788 | |
| 2789 | /* Operator '+', '-' */ |
| 2790 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2791 | tok_type = LYXP_TOKEN_OPER_MATH; |
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 (expr[parsed] == '=') { |
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 = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2797 | tok_type = LYXP_TOKEN_OPER_EQUAL; |
| 2798 | |
| 2799 | } else if ((expr[parsed] == '<') || (expr[parsed] == '>')) { |
| 2800 | |
| 2801 | /* Operator '<', '>' */ |
| 2802 | tok_len = 1; |
| 2803 | tok_type = LYXP_TOKEN_OPER_COMP; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2804 | |
| 2805 | } else if (ret->used && (ret->tokens[ret->used - 1] != LYXP_TOKEN_AT) |
| 2806 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_PAR1) |
| 2807 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_BRACK1) |
| 2808 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_COMMA) |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2809 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_LOG) |
| 2810 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_EQUAL) |
| 2811 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_NEQUAL) |
| 2812 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_COMP) |
| 2813 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_MATH) |
| 2814 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_UNI) |
| 2815 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_PATH) |
| 2816 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_RPATH)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2817 | |
| 2818 | /* Operator '*', 'or', 'and', 'mod', or 'div' */ |
| 2819 | if (expr[parsed] == '*') { |
| 2820 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2821 | tok_type = LYXP_TOKEN_OPER_MATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2822 | |
| 2823 | } else if (!strncmp(&expr[parsed], "or", 2)) { |
| 2824 | tok_len = 2; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2825 | tok_type = LYXP_TOKEN_OPER_LOG; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2826 | |
| 2827 | } else if (!strncmp(&expr[parsed], "and", 3)) { |
| 2828 | tok_len = 3; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2829 | tok_type = LYXP_TOKEN_OPER_LOG; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2830 | |
| 2831 | } else if (!strncmp(&expr[parsed], "mod", 3) || !strncmp(&expr[parsed], "div", 3)) { |
| 2832 | tok_len = 3; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2833 | tok_type = LYXP_TOKEN_OPER_MATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2834 | |
| 2835 | } else if (prev_function_check) { |
Michal Vasko | 5307857 | 2019-05-24 08:50:15 +0200 | [diff] [blame] | 2836 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Invalid character 0x%x ('%c'), perhaps \"%.*s\" is supposed to be a function call.", |
| 2837 | 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] | 2838 | goto error; |
| 2839 | } else { |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2840 | 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] | 2841 | goto error; |
| 2842 | } |
| 2843 | } else if (expr[parsed] == '*') { |
| 2844 | |
| 2845 | /* NameTest '*' */ |
| 2846 | tok_len = 1; |
| 2847 | tok_type = LYXP_TOKEN_NAMETEST; |
| 2848 | |
| 2849 | } else { |
| 2850 | |
| 2851 | /* NameTest (NCName ':' '*' | QName) or NodeType/FunctionName */ |
| 2852 | ncname_len = parse_ncname(&expr[parsed]); |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2853 | 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] | 2854 | tok_len = ncname_len; |
| 2855 | |
| 2856 | if (expr[parsed + tok_len] == ':') { |
| 2857 | ++tok_len; |
| 2858 | if (expr[parsed + tok_len] == '*') { |
| 2859 | ++tok_len; |
| 2860 | } else { |
| 2861 | ncname_len = parse_ncname(&expr[parsed + tok_len]); |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2862 | 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] | 2863 | tok_len += ncname_len; |
| 2864 | } |
| 2865 | /* remove old flag to prevent ambiguities */ |
| 2866 | prev_function_check = 0; |
| 2867 | tok_type = LYXP_TOKEN_NAMETEST; |
| 2868 | } else { |
| 2869 | /* there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */ |
| 2870 | prev_function_check = 1; |
| 2871 | tok_type = LYXP_TOKEN_NAMETEST; |
| 2872 | } |
| 2873 | } |
| 2874 | |
| 2875 | /* store the token, move on to the next one */ |
| 2876 | LY_CHECK_GOTO(exp_add_token(ctx, ret, tok_type, parsed, tok_len), error); |
| 2877 | parsed += tok_len; |
| 2878 | while (is_xmlws(expr[parsed])) { |
| 2879 | ++parsed; |
| 2880 | } |
| 2881 | |
| 2882 | } while (expr[parsed]); |
| 2883 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2884 | if (reparse) { |
| 2885 | /* prealloc repeat */ |
| 2886 | ret->repeat = calloc(ret->size, sizeof *ret->repeat); |
| 2887 | LY_CHECK_ERR_GOTO(!ret->repeat, LOGMEM(ctx), error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2888 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2889 | /* fill repeat */ |
| 2890 | LY_CHECK_GOTO(reparse_or_expr(ctx, ret, &tok_idx), error); |
| 2891 | if (ret->used > tok_idx) { |
| 2892 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Unparsed characters \"%s\" left at the end of an XPath expression.", |
| 2893 | &ret->expr[ret->tok_pos[tok_idx]]); |
| 2894 | goto error; |
| 2895 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2896 | } |
| 2897 | |
| 2898 | print_expr_struct_debug(ret); |
| 2899 | |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2900 | return ret; |
| 2901 | |
| 2902 | error: |
| 2903 | lyxp_expr_free(ctx, ret); |
| 2904 | return NULL; |
| 2905 | } |
| 2906 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2907 | struct lyxp_expr * |
| 2908 | lyxp_expr_dup(const struct ly_ctx *ctx, const struct lyxp_expr *exp) |
| 2909 | { |
| 2910 | struct lyxp_expr *dup; |
| 2911 | uint32_t i, j; |
| 2912 | |
| 2913 | dup = calloc(1, sizeof *dup); |
| 2914 | LY_CHECK_ERR_GOTO(!dup, LOGMEM(ctx), error); |
| 2915 | |
| 2916 | dup->tokens = malloc(exp->used * sizeof *dup->tokens); |
| 2917 | LY_CHECK_ERR_GOTO(!dup->tokens, LOGMEM(ctx), error); |
| 2918 | memcpy(dup->tokens, exp->tokens, exp->used * sizeof *dup->tokens); |
| 2919 | |
| 2920 | dup->tok_pos = malloc(exp->used * sizeof *dup->tok_pos); |
| 2921 | LY_CHECK_ERR_GOTO(!dup->tok_pos, LOGMEM(ctx), error); |
| 2922 | memcpy(dup->tok_pos, exp->tok_pos, exp->used * sizeof *dup->tok_pos); |
| 2923 | |
| 2924 | dup->tok_len = malloc(exp->used * sizeof *dup->tok_len); |
| 2925 | LY_CHECK_ERR_GOTO(!dup->tok_len, LOGMEM(ctx), error); |
| 2926 | memcpy(dup->tok_len, exp->tok_len, exp->used * sizeof *dup->tok_len); |
| 2927 | |
| 2928 | dup->repeat = malloc(exp->used * sizeof *dup->repeat); |
| 2929 | LY_CHECK_ERR_GOTO(!dup->repeat, LOGMEM(ctx), error); |
| 2930 | for (i = 0; i < exp->used; ++i) { |
| 2931 | if (!exp->repeat[i]) { |
| 2932 | dup->repeat[i] = NULL; |
| 2933 | } else { |
| 2934 | for (j = 0; exp->repeat[i][j]; ++j); |
| 2935 | /* the ending 0 as well */ |
| 2936 | ++j; |
| 2937 | |
Michal Vasko | 99c7164 | 2020-07-03 13:33:36 +0200 | [diff] [blame] | 2938 | dup->repeat[i] = malloc(j * sizeof **dup->repeat); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2939 | LY_CHECK_ERR_GOTO(!dup->repeat[i], LOGMEM(ctx), error); |
| 2940 | memcpy(dup->repeat[i], exp->repeat[i], j * sizeof **dup->repeat); |
| 2941 | dup->repeat[i][j - 1] = 0; |
| 2942 | } |
| 2943 | } |
| 2944 | |
| 2945 | dup->used = exp->used; |
| 2946 | dup->size = exp->used; |
| 2947 | dup->expr = lydict_insert(ctx, exp->expr, 0); |
| 2948 | |
| 2949 | return dup; |
| 2950 | |
| 2951 | error: |
| 2952 | lyxp_expr_free(ctx, dup); |
| 2953 | return NULL; |
| 2954 | } |
| 2955 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2956 | /* |
| 2957 | * warn functions |
| 2958 | * |
| 2959 | * Warn functions check specific reasonable conditions for schema XPath |
| 2960 | * and print a warning if they are not satisfied. |
| 2961 | */ |
| 2962 | |
| 2963 | /** |
| 2964 | * @brief Get the last-added schema node that is currently in the context. |
| 2965 | * |
| 2966 | * @param[in] set Set to search in. |
| 2967 | * @return Last-added schema context node, NULL if no node is in context. |
| 2968 | */ |
| 2969 | static struct lysc_node * |
| 2970 | warn_get_scnode_in_ctx(struct lyxp_set *set) |
| 2971 | { |
| 2972 | uint32_t i; |
| 2973 | |
| 2974 | if (!set || (set->type != LYXP_SET_SCNODE_SET)) { |
| 2975 | return NULL; |
| 2976 | } |
| 2977 | |
| 2978 | i = set->used; |
| 2979 | do { |
| 2980 | --i; |
| 2981 | if (set->val.scnodes[i].in_ctx == 1) { |
| 2982 | /* if there are more, simply return the first found (last added) */ |
| 2983 | return set->val.scnodes[i].scnode; |
| 2984 | } |
| 2985 | } while (i); |
| 2986 | |
| 2987 | return NULL; |
| 2988 | } |
| 2989 | |
| 2990 | /** |
| 2991 | * @brief Test whether a type is numeric - integer type or decimal64. |
| 2992 | * |
| 2993 | * @param[in] type Type to test. |
| 2994 | * @return 1 if numeric, 0 otherwise. |
| 2995 | */ |
| 2996 | static int |
| 2997 | warn_is_numeric_type(struct lysc_type *type) |
| 2998 | { |
| 2999 | struct lysc_type_union *uni; |
| 3000 | int ret; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3001 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3002 | |
| 3003 | switch (type->basetype) { |
| 3004 | case LY_TYPE_DEC64: |
| 3005 | case LY_TYPE_INT8: |
| 3006 | case LY_TYPE_UINT8: |
| 3007 | case LY_TYPE_INT16: |
| 3008 | case LY_TYPE_UINT16: |
| 3009 | case LY_TYPE_INT32: |
| 3010 | case LY_TYPE_UINT32: |
| 3011 | case LY_TYPE_INT64: |
| 3012 | case LY_TYPE_UINT64: |
| 3013 | return 1; |
| 3014 | case LY_TYPE_UNION: |
| 3015 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3016 | LY_ARRAY_FOR(uni->types, u) { |
| 3017 | ret = warn_is_numeric_type(uni->types[u]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3018 | if (ret) { |
| 3019 | /* found a suitable type */ |
| 3020 | return 1; |
| 3021 | } |
| 3022 | } |
| 3023 | /* did not find any suitable type */ |
| 3024 | return 0; |
| 3025 | case LY_TYPE_LEAFREF: |
| 3026 | return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype); |
| 3027 | default: |
| 3028 | return 0; |
| 3029 | } |
| 3030 | } |
| 3031 | |
| 3032 | /** |
| 3033 | * @brief Test whether a type is string-like - no integers, decimal64 or binary. |
| 3034 | * |
| 3035 | * @param[in] type Type to test. |
| 3036 | * @return 1 if string, 0 otherwise. |
| 3037 | */ |
| 3038 | static int |
| 3039 | warn_is_string_type(struct lysc_type *type) |
| 3040 | { |
| 3041 | struct lysc_type_union *uni; |
| 3042 | int ret; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3043 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3044 | |
| 3045 | switch (type->basetype) { |
| 3046 | case LY_TYPE_BITS: |
| 3047 | case LY_TYPE_ENUM: |
| 3048 | case LY_TYPE_IDENT: |
| 3049 | case LY_TYPE_INST: |
| 3050 | case LY_TYPE_STRING: |
| 3051 | return 1; |
| 3052 | case LY_TYPE_UNION: |
| 3053 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3054 | LY_ARRAY_FOR(uni->types, u) { |
| 3055 | ret = warn_is_string_type(uni->types[u]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3056 | if (ret) { |
| 3057 | /* found a suitable type */ |
| 3058 | return 1; |
| 3059 | } |
| 3060 | } |
| 3061 | /* did not find any suitable type */ |
| 3062 | return 0; |
| 3063 | case LY_TYPE_LEAFREF: |
| 3064 | return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype); |
| 3065 | default: |
| 3066 | return 0; |
| 3067 | } |
| 3068 | } |
| 3069 | |
| 3070 | /** |
| 3071 | * @brief Test whether a type is one specific type. |
| 3072 | * |
| 3073 | * @param[in] type Type to test. |
| 3074 | * @param[in] base Expected type. |
| 3075 | * @return 1 if it is, 0 otherwise. |
| 3076 | */ |
| 3077 | static int |
| 3078 | warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base) |
| 3079 | { |
| 3080 | struct lysc_type_union *uni; |
| 3081 | int ret; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3082 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3083 | |
| 3084 | if (type->basetype == base) { |
| 3085 | return 1; |
| 3086 | } else if (type->basetype == LY_TYPE_UNION) { |
| 3087 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3088 | LY_ARRAY_FOR(uni->types, u) { |
| 3089 | ret = warn_is_specific_type(uni->types[u], base); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3090 | if (ret) { |
| 3091 | /* found a suitable type */ |
| 3092 | return 1; |
| 3093 | } |
| 3094 | } |
| 3095 | /* did not find any suitable type */ |
| 3096 | return 0; |
| 3097 | } else if (type->basetype == LY_TYPE_LEAFREF) { |
| 3098 | return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base); |
| 3099 | } |
| 3100 | |
| 3101 | return 0; |
| 3102 | } |
| 3103 | |
| 3104 | /** |
| 3105 | * @brief Get next type of a (union) type. |
| 3106 | * |
| 3107 | * @param[in] type Base type. |
| 3108 | * @param[in] prev_type Previously returned type. |
| 3109 | * @return Next type or NULL. |
| 3110 | */ |
| 3111 | static struct lysc_type * |
| 3112 | warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type) |
| 3113 | { |
| 3114 | struct lysc_type_union *uni; |
| 3115 | int found = 0; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3116 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3117 | |
| 3118 | switch (type->basetype) { |
| 3119 | case LY_TYPE_UNION: |
| 3120 | uni = (struct lysc_type_union *)type; |
| 3121 | if (!prev_type) { |
| 3122 | return uni->types[0]; |
| 3123 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3124 | LY_ARRAY_FOR(uni->types, u) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3125 | if (found) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3126 | return uni->types[u]; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3127 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3128 | if (prev_type == uni->types[u]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3129 | found = 1; |
| 3130 | } |
| 3131 | } |
| 3132 | return NULL; |
| 3133 | default: |
| 3134 | if (prev_type) { |
| 3135 | assert(type == prev_type); |
| 3136 | return NULL; |
| 3137 | } else { |
| 3138 | return type; |
| 3139 | } |
| 3140 | } |
| 3141 | } |
| 3142 | |
| 3143 | /** |
| 3144 | * @brief Test whether 2 types have a common type. |
| 3145 | * |
| 3146 | * @param[in] type1 First type. |
| 3147 | * @param[in] type2 Second type. |
| 3148 | * @return 1 if they do, 0 otherwise. |
| 3149 | */ |
| 3150 | static int |
| 3151 | warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2) |
| 3152 | { |
| 3153 | struct lysc_type *t1, *rt1, *t2, *rt2; |
| 3154 | |
| 3155 | t1 = NULL; |
| 3156 | while ((t1 = warn_is_equal_type_next_type(type1, t1))) { |
| 3157 | if (t1->basetype == LY_TYPE_LEAFREF) { |
| 3158 | rt1 = ((struct lysc_type_leafref *)t1)->realtype; |
| 3159 | } else { |
| 3160 | rt1 = t1; |
| 3161 | } |
| 3162 | |
| 3163 | t2 = NULL; |
| 3164 | while ((t2 = warn_is_equal_type_next_type(type2, t2))) { |
| 3165 | if (t2->basetype == LY_TYPE_LEAFREF) { |
| 3166 | rt2 = ((struct lysc_type_leafref *)t2)->realtype; |
| 3167 | } else { |
| 3168 | rt2 = t2; |
| 3169 | } |
| 3170 | |
| 3171 | if (rt2->basetype == rt1->basetype) { |
| 3172 | /* match found */ |
| 3173 | return 1; |
| 3174 | } |
| 3175 | } |
| 3176 | } |
| 3177 | |
| 3178 | return 0; |
| 3179 | } |
| 3180 | |
| 3181 | /** |
| 3182 | * @brief Check both operands of comparison operators. |
| 3183 | * |
| 3184 | * @param[in] ctx Context for errors. |
| 3185 | * @param[in] set1 First operand set. |
| 3186 | * @param[in] set2 Second operand set. |
| 3187 | * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!='). |
| 3188 | * @param[in] expr Start of the expression to print with the warning. |
| 3189 | * @param[in] tok_pos Token position. |
| 3190 | */ |
| 3191 | static void |
| 3192 | warn_operands(struct ly_ctx *ctx, struct lyxp_set *set1, struct lyxp_set *set2, int numbers_only, const char *expr, uint16_t tok_pos) |
| 3193 | { |
| 3194 | struct lysc_node_leaf *node1, *node2; |
| 3195 | int leaves = 1, warning = 0; |
| 3196 | |
| 3197 | node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1); |
| 3198 | node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2); |
| 3199 | |
| 3200 | if (!node1 && !node2) { |
| 3201 | /* no node-sets involved, nothing to do */ |
| 3202 | return; |
| 3203 | } |
| 3204 | |
| 3205 | if (node1) { |
| 3206 | if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3207 | LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name); |
| 3208 | warning = 1; |
| 3209 | leaves = 0; |
| 3210 | } else if (numbers_only && !warn_is_numeric_type(node1->type)) { |
| 3211 | LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name); |
| 3212 | warning = 1; |
| 3213 | } |
| 3214 | } |
| 3215 | |
| 3216 | if (node2) { |
| 3217 | if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3218 | LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name); |
| 3219 | warning = 1; |
| 3220 | leaves = 0; |
| 3221 | } else if (numbers_only && !warn_is_numeric_type(node2->type)) { |
| 3222 | LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name); |
| 3223 | warning = 1; |
| 3224 | } |
| 3225 | } |
| 3226 | |
| 3227 | if (node1 && node2 && leaves && !numbers_only) { |
| 3228 | if ((warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type)) |
| 3229 | || (!warn_is_numeric_type(node1->type) && warn_is_numeric_type(node2->type)) |
| 3230 | || (!warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type) |
| 3231 | && !warn_is_equal_type(node1->type, node2->type))) { |
| 3232 | LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name); |
| 3233 | warning = 1; |
| 3234 | } |
| 3235 | } |
| 3236 | |
| 3237 | if (warning) { |
| 3238 | LOGWRN(ctx, "Previous warning generated by XPath subexpression[%u] \"%.20s\".", tok_pos, expr + tok_pos); |
| 3239 | } |
| 3240 | } |
| 3241 | |
| 3242 | /** |
| 3243 | * @brief Check that a value is valid for a leaf. If not applicable, does nothing. |
| 3244 | * |
| 3245 | * @param[in] exp Parsed XPath expression. |
| 3246 | * @param[in] set Set with the leaf/leaf-list. |
| 3247 | * @param[in] val_exp Index of the value (literal/number) in @p exp. |
| 3248 | * @param[in] equal_exp Index of the start of the equality expression in @p exp. |
| 3249 | * @param[in] last_equal_exp Index of the end of the equality expression in @p exp. |
| 3250 | */ |
| 3251 | static void |
| 3252 | warn_equality_value(struct lyxp_expr *exp, struct lyxp_set *set, uint16_t val_exp, uint16_t equal_exp, uint16_t last_equal_exp) |
| 3253 | { |
| 3254 | struct lysc_node *scnode; |
| 3255 | struct lysc_type *type; |
| 3256 | char *value; |
| 3257 | LY_ERR rc; |
| 3258 | struct ly_err_item *err = NULL; |
| 3259 | |
| 3260 | if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST)) |
| 3261 | && ((exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) || (exp->tokens[val_exp] == LYXP_TOKEN_NUMBER))) { |
| 3262 | /* check that the node can have the specified value */ |
| 3263 | if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) { |
| 3264 | value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2); |
| 3265 | } else { |
| 3266 | value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]); |
| 3267 | } |
| 3268 | if (!value) { |
| 3269 | LOGMEM(set->ctx); |
| 3270 | return; |
| 3271 | } |
| 3272 | |
| 3273 | if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) { |
| 3274 | LOGWRN(set->ctx, "Identityref \"%s\" comparison with identity \"%s\" without prefix, consider adding" |
| 3275 | " a prefix or best using \"derived-from(-or-self)()\" functions.", scnode->name, value); |
| 3276 | LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp], |
| 3277 | (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp], |
| 3278 | exp->expr + exp->tok_pos[equal_exp]); |
| 3279 | } |
| 3280 | |
| 3281 | type = ((struct lysc_node_leaf *)scnode)->type; |
| 3282 | if (type->basetype != LY_TYPE_IDENT) { |
| 3283 | rc = type->plugin->store(set->ctx, type, value, strlen(value), LY_TYPE_OPTS_SCHEMA, |
| 3284 | lys_resolve_prefix, (void *)type->dflt_mod, LYD_XML, NULL, NULL, NULL, NULL, &err); |
| 3285 | |
| 3286 | if (err) { |
| 3287 | LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg); |
| 3288 | ly_err_free(err); |
| 3289 | } else if (rc != LY_SUCCESS) { |
| 3290 | LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value); |
| 3291 | } |
| 3292 | if (rc != LY_SUCCESS) { |
| 3293 | LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp], |
| 3294 | (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp], |
| 3295 | exp->expr + exp->tok_pos[equal_exp]); |
| 3296 | } |
| 3297 | } |
| 3298 | free(value); |
| 3299 | } |
| 3300 | } |
| 3301 | |
| 3302 | /* |
| 3303 | * XPath functions |
| 3304 | */ |
| 3305 | |
| 3306 | /** |
| 3307 | * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN |
| 3308 | * depending on whether the first node bit value from the second argument is set. |
| 3309 | * |
| 3310 | * @param[in] args Array of arguments. |
| 3311 | * @param[in] arg_count Count of elements in @p args. |
| 3312 | * @param[in,out] set Context and result set at the same time. |
| 3313 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3314 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3315 | */ |
| 3316 | static LY_ERR |
| 3317 | xpath_bit_is_set(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3318 | { |
| 3319 | struct lyd_node_term *leaf; |
| 3320 | struct lysc_node_leaf *sleaf; |
| 3321 | struct lysc_type_bits *bits; |
| 3322 | LY_ERR rc = LY_SUCCESS; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3323 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3324 | |
| 3325 | if (options & LYXP_SCNODE_ALL) { |
| 3326 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3327 | 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] | 3328 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3329 | 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] | 3330 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) { |
| 3331 | 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] | 3332 | } |
| 3333 | |
| 3334 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 3335 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3336 | 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] | 3337 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3338 | 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] | 3339 | } |
| 3340 | } |
| 3341 | set_scnode_clear_ctx(set); |
| 3342 | return rc; |
| 3343 | } |
| 3344 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3345 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3346 | 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)"); |
| 3347 | return LY_EVALID; |
| 3348 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3349 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3350 | LY_CHECK_RET(rc); |
| 3351 | |
| 3352 | set_fill_boolean(set, 0); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3353 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3354 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
| 3355 | if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) |
| 3356 | && (((struct lysc_node_leaf *)leaf->schema)->type->basetype == LY_TYPE_BITS)) { |
| 3357 | bits = (struct lysc_type_bits *)((struct lysc_node_leaf *)leaf->schema)->type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3358 | LY_ARRAY_FOR(bits->bits, u) { |
| 3359 | if (!strcmp(bits->bits[u].name, args[1]->val.str)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3360 | set_fill_boolean(set, 1); |
| 3361 | break; |
| 3362 | } |
| 3363 | } |
| 3364 | } |
| 3365 | } |
| 3366 | |
| 3367 | return LY_SUCCESS; |
| 3368 | } |
| 3369 | |
| 3370 | /** |
| 3371 | * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN |
| 3372 | * with the argument converted to boolean. |
| 3373 | * |
| 3374 | * @param[in] args Array of arguments. |
| 3375 | * @param[in] arg_count Count of elements in @p args. |
| 3376 | * @param[in,out] set Context and result set at the same time. |
| 3377 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3378 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3379 | */ |
| 3380 | static LY_ERR |
| 3381 | xpath_boolean(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3382 | { |
| 3383 | LY_ERR rc; |
| 3384 | |
| 3385 | if (options & LYXP_SCNODE_ALL) { |
| 3386 | set_scnode_clear_ctx(set); |
| 3387 | return LY_SUCCESS; |
| 3388 | } |
| 3389 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3390 | rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3391 | LY_CHECK_RET(rc); |
| 3392 | set_fill_set(set, args[0]); |
| 3393 | |
| 3394 | return LY_SUCCESS; |
| 3395 | } |
| 3396 | |
| 3397 | /** |
| 3398 | * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER |
| 3399 | * with the first argument rounded up to the nearest integer. |
| 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_ceiling(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3409 | { |
| 3410 | struct lysc_node_leaf *sleaf; |
| 3411 | LY_ERR rc = LY_SUCCESS; |
| 3412 | |
| 3413 | if (options & LYXP_SCNODE_ALL) { |
| 3414 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3415 | 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] | 3416 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3417 | 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] | 3418 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) { |
| 3419 | 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] | 3420 | } |
| 3421 | set_scnode_clear_ctx(set); |
| 3422 | return rc; |
| 3423 | } |
| 3424 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3425 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3426 | LY_CHECK_RET(rc); |
| 3427 | if ((long long)args[0]->val.num != args[0]->val.num) { |
| 3428 | set_fill_number(set, ((long long)args[0]->val.num) + 1); |
| 3429 | } else { |
| 3430 | set_fill_number(set, args[0]->val.num); |
| 3431 | } |
| 3432 | |
| 3433 | return LY_SUCCESS; |
| 3434 | } |
| 3435 | |
| 3436 | /** |
| 3437 | * @brief Execute the XPath concat(string, string, string*) function. |
| 3438 | * Returns LYXP_SET_STRING with the concatenation of all the arguments. |
| 3439 | * |
| 3440 | * @param[in] args Array of arguments. |
| 3441 | * @param[in] arg_count Count of elements in @p args. |
| 3442 | * @param[in,out] set Context and result set at the same time. |
| 3443 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3444 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3445 | */ |
| 3446 | static LY_ERR |
| 3447 | xpath_concat(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options) |
| 3448 | { |
| 3449 | uint16_t i; |
| 3450 | char *str = NULL; |
| 3451 | size_t used = 1; |
| 3452 | LY_ERR rc = LY_SUCCESS; |
| 3453 | struct lysc_node_leaf *sleaf; |
| 3454 | |
| 3455 | if (options & LYXP_SCNODE_ALL) { |
| 3456 | for (i = 0; i < arg_count; ++i) { |
| 3457 | if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) { |
| 3458 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3459 | LOGWRN(set->ctx, "Argument #%u of %s is a %s node \"%s\".", |
| 3460 | i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3461 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3462 | LOGWRN(set->ctx, "Argument #%u of %s is node \"%s\", not of string-type.", __func__, i + 1, sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3463 | } |
| 3464 | } |
| 3465 | } |
| 3466 | set_scnode_clear_ctx(set); |
| 3467 | return rc; |
| 3468 | } |
| 3469 | |
| 3470 | for (i = 0; i < arg_count; ++i) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3471 | rc = lyxp_set_cast(args[i], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3472 | if (rc != LY_SUCCESS) { |
| 3473 | free(str); |
| 3474 | return rc; |
| 3475 | } |
| 3476 | |
| 3477 | str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char)); |
| 3478 | LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM); |
| 3479 | strcpy(str + used - 1, args[i]->val.str); |
| 3480 | used += strlen(args[i]->val.str); |
| 3481 | } |
| 3482 | |
| 3483 | /* free, kind of */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3484 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3485 | set->type = LYXP_SET_STRING; |
| 3486 | set->val.str = str; |
| 3487 | |
| 3488 | return LY_SUCCESS; |
| 3489 | } |
| 3490 | |
| 3491 | /** |
| 3492 | * @brief Execute the XPath contains(string, string) function. |
| 3493 | * Returns LYXP_SET_BOOLEAN whether the second argument can |
| 3494 | * be found in the first or not. |
| 3495 | * |
| 3496 | * @param[in] args Array of arguments. |
| 3497 | * @param[in] arg_count Count of elements in @p args. |
| 3498 | * @param[in,out] set Context and result set at the same time. |
| 3499 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3500 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3501 | */ |
| 3502 | static LY_ERR |
| 3503 | xpath_contains(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3504 | { |
| 3505 | struct lysc_node_leaf *sleaf; |
| 3506 | LY_ERR rc = LY_SUCCESS; |
| 3507 | |
| 3508 | if (options & LYXP_SCNODE_ALL) { |
| 3509 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3510 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3511 | 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] | 3512 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3513 | 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] | 3514 | } |
| 3515 | } |
| 3516 | |
| 3517 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 3518 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3519 | 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] | 3520 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3521 | 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] | 3522 | } |
| 3523 | } |
| 3524 | set_scnode_clear_ctx(set); |
| 3525 | return rc; |
| 3526 | } |
| 3527 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3528 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3529 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3530 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3531 | LY_CHECK_RET(rc); |
| 3532 | |
| 3533 | if (strstr(args[0]->val.str, args[1]->val.str)) { |
| 3534 | set_fill_boolean(set, 1); |
| 3535 | } else { |
| 3536 | set_fill_boolean(set, 0); |
| 3537 | } |
| 3538 | |
| 3539 | return LY_SUCCESS; |
| 3540 | } |
| 3541 | |
| 3542 | /** |
| 3543 | * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER |
| 3544 | * with the size of the node-set from the argument. |
| 3545 | * |
| 3546 | * @param[in] args Array of arguments. |
| 3547 | * @param[in] arg_count Count of elements in @p args. |
| 3548 | * @param[in,out] set Context and result set at the same time. |
| 3549 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3550 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3551 | */ |
| 3552 | static LY_ERR |
| 3553 | xpath_count(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3554 | { |
| 3555 | struct lysc_node *scnode = NULL; |
| 3556 | LY_ERR rc = LY_SUCCESS; |
| 3557 | |
| 3558 | if (options & LYXP_SCNODE_ALL) { |
| 3559 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(scnode = warn_get_scnode_in_ctx(args[0]))) { |
| 3560 | 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] | 3561 | } |
| 3562 | set_scnode_clear_ctx(set); |
| 3563 | return rc; |
| 3564 | } |
| 3565 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3566 | if (args[0]->type != LYXP_SET_NODE_SET) { |
| 3567 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "count(node-set)"); |
| 3568 | return LY_EVALID; |
| 3569 | } |
| 3570 | |
| 3571 | set_fill_number(set, args[0]->used); |
| 3572 | return LY_SUCCESS; |
| 3573 | } |
| 3574 | |
| 3575 | /** |
| 3576 | * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET |
| 3577 | * with the context with the intial node. |
| 3578 | * |
| 3579 | * @param[in] args Array of arguments. |
| 3580 | * @param[in] arg_count Count of elements in @p args. |
| 3581 | * @param[in,out] set Context and result set at the same time. |
| 3582 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3583 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3584 | */ |
| 3585 | static LY_ERR |
| 3586 | xpath_current(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options) |
| 3587 | { |
| 3588 | if (arg_count || args) { |
| 3589 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGCOUNT, arg_count, "current()"); |
| 3590 | return LY_EVALID; |
| 3591 | } |
| 3592 | |
| 3593 | if (options & LYXP_SCNODE_ALL) { |
| 3594 | set_scnode_clear_ctx(set); |
| 3595 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 3596 | lyxp_set_scnode_insert_node(set, set->ctx_scnode, LYXP_NODE_ELEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3597 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3598 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3599 | |
| 3600 | /* position is filled later */ |
| 3601 | set_insert_node(set, set->ctx_node, 0, LYXP_NODE_ELEM, 0); |
| 3602 | } |
| 3603 | |
| 3604 | return LY_SUCCESS; |
| 3605 | } |
| 3606 | |
| 3607 | /** |
| 3608 | * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either |
| 3609 | * leafref or instance-identifier target node(s). |
| 3610 | * |
| 3611 | * @param[in] args Array of arguments. |
| 3612 | * @param[in] arg_count Count of elements in @p args. |
| 3613 | * @param[in,out] set Context and result set at the same time. |
| 3614 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3615 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3616 | */ |
| 3617 | static LY_ERR |
| 3618 | xpath_deref(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3619 | { |
| 3620 | struct lyd_node_term *leaf; |
Michal Vasko | 42e497c | 2020-01-06 08:38:25 +0100 | [diff] [blame] | 3621 | struct lysc_node_leaf *sleaf = NULL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3622 | struct lysc_type_leafref *lref; |
Michal Vasko | ae9e4cb | 2019-09-25 08:43:05 +0200 | [diff] [blame] | 3623 | const struct lysc_node *target; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3624 | struct ly_path *p; |
| 3625 | struct lyd_node *node; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3626 | char *errmsg = NULL; |
| 3627 | const char *val; |
| 3628 | int dynamic; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 3629 | uint8_t oper; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3630 | LY_ERR rc = LY_SUCCESS; |
| 3631 | |
| 3632 | if (options & LYXP_SCNODE_ALL) { |
| 3633 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3634 | 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] | 3635 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3636 | 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] | 3637 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) && !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) { |
| 3638 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"leafref\" nor \"instance-identifier\".", |
| 3639 | __func__, sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3640 | } |
| 3641 | set_scnode_clear_ctx(set); |
Michal Vasko | 42e497c | 2020-01-06 08:38:25 +0100 | [diff] [blame] | 3642 | if (sleaf && (sleaf->type->basetype == LY_TYPE_LEAFREF)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3643 | lref = (struct lysc_type_leafref *)sleaf->type; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 3644 | 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] | 3645 | |
| 3646 | /* it was already evaluated on schema, it must succeed */ |
| 3647 | if (set->format == LYD_JSON) { |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 3648 | rc = ly_path_compile(set->ctx, sleaf->module, (struct lysc_node *)sleaf, lref->path, LY_PATH_LREF_TRUE, |
| 3649 | oper, LY_PATH_TARGET_MANY, lydjson_resolve_prefix, NULL, LYD_JSON, &p); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3650 | } else { |
| 3651 | assert(set->format == LYD_SCHEMA); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 3652 | rc = ly_path_compile(set->ctx, sleaf->module, (struct lysc_node *)sleaf, lref->path, LY_PATH_LREF_TRUE, |
| 3653 | oper, LY_PATH_TARGET_MANY, lys_resolve_prefix, lref->path_context, LYD_SCHEMA, &p); |
Michal Vasko | ae9e4cb | 2019-09-25 08:43:05 +0200 | [diff] [blame] | 3654 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3655 | assert(!rc); |
| 3656 | |
| 3657 | /* get the target node */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3658 | target = p[LY_ARRAY_COUNT(p) - 1].node; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3659 | ly_path_free(set->ctx, p); |
| 3660 | |
| 3661 | lyxp_set_scnode_insert_node(set, target, LYXP_NODE_ELEM); |
Michal Vasko | ae9e4cb | 2019-09-25 08:43:05 +0200 | [diff] [blame] | 3662 | } |
| 3663 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3664 | return rc; |
| 3665 | } |
| 3666 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3667 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3668 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "deref(node-set)"); |
| 3669 | return LY_EVALID; |
| 3670 | } |
| 3671 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3672 | lyxp_set_free_content(set); |
| 3673 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3674 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
| 3675 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 3676 | if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 3677 | if (sleaf->type->basetype == LY_TYPE_LEAFREF) { |
| 3678 | /* find leafref target */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3679 | if (ly_type_find_leafref((struct lysc_type_leafref *)sleaf->type, (struct lyd_node *)leaf, |
| 3680 | &leaf->value, set->tree, &node, &errmsg)) { |
| 3681 | LOGERR(set->ctx, LY_EVALID, errmsg); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3682 | free(errmsg); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3683 | return LY_EVALID; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3684 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3685 | } else { |
| 3686 | assert(sleaf->type->basetype == LY_TYPE_INST); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3687 | if (ly_path_eval(leaf->value.target, set->tree, &node)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3688 | val = lyd_value2str(leaf, &dynamic); |
| 3689 | LOGERR(set->ctx, LY_EVALID, "Invalid instance-identifier \"%s\" value - required instance not found.", val); |
| 3690 | if (dynamic) { |
| 3691 | free((char *)val); |
| 3692 | } |
| 3693 | return LY_EVALID; |
| 3694 | } |
| 3695 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3696 | |
| 3697 | /* insert it */ |
| 3698 | set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3699 | } |
| 3700 | } |
| 3701 | |
| 3702 | return LY_SUCCESS; |
| 3703 | } |
| 3704 | |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame^] | 3705 | static LY_ERR |
| 3706 | xpath_derived_(struct lyxp_set **args, struct lyxp_set *set, int options, int self_match, const char *func) |
| 3707 | { |
| 3708 | uint16_t i; |
| 3709 | LY_ARRAY_COUNT_TYPE u; |
| 3710 | struct lyd_node_term *leaf; |
| 3711 | struct lysc_node_leaf *sleaf; |
| 3712 | struct lyd_meta *meta; |
| 3713 | struct lyd_value data = {0}, *val; |
| 3714 | struct ly_err_item *err = NULL; |
| 3715 | LY_ERR rc = LY_SUCCESS; |
| 3716 | int found; |
| 3717 | |
| 3718 | if (options & LYXP_SCNODE_ALL) { |
| 3719 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3720 | LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", func); |
| 3721 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3722 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype), |
| 3723 | sleaf->name); |
| 3724 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) { |
| 3725 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", func, sleaf->name); |
| 3726 | } |
| 3727 | |
| 3728 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 3729 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3730 | LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype), |
| 3731 | sleaf->name); |
| 3732 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3733 | LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", func, sleaf->name); |
| 3734 | } |
| 3735 | } |
| 3736 | set_scnode_clear_ctx(set); |
| 3737 | return rc; |
| 3738 | } |
| 3739 | |
| 3740 | if (args[0]->type != LYXP_SET_NODE_SET) { |
| 3741 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), |
| 3742 | "derived-from(-or-self)(node-set, string)"); |
| 3743 | return LY_EVALID; |
| 3744 | } |
| 3745 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
| 3746 | LY_CHECK_RET(rc); |
| 3747 | |
| 3748 | set_fill_boolean(set, 0); |
| 3749 | found = 0; |
| 3750 | for (i = 0; i < args[0]->used; ++i) { |
| 3751 | if ((args[0]->val.nodes[i].type != LYXP_NODE_ELEM) && (args[0]->val.nodes[i].type != LYXP_NODE_META)) { |
| 3752 | continue; |
| 3753 | } |
| 3754 | |
| 3755 | if (args[0]->val.nodes[i].type == LYXP_NODE_ELEM) { |
| 3756 | leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node; |
| 3757 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 3758 | val = &leaf->value; |
| 3759 | if (!(sleaf->nodetype & LYD_NODE_TERM) || (leaf->value.realtype->basetype != LY_TYPE_IDENT)) { |
| 3760 | /* uninteresting */ |
| 3761 | continue; |
| 3762 | } |
| 3763 | |
| 3764 | /* store args[1] as ident */ |
| 3765 | rc = val->realtype->plugin->store(set->ctx, val->realtype, args[1]->val.str, strlen(args[1]->val.str), |
| 3766 | LY_TYPE_OPTS_STORE, lys_resolve_prefix, (void *)sleaf->dflt_mod, |
| 3767 | set->format, (struct lyd_node *)leaf, set->tree, &data, NULL, &err); |
| 3768 | } else { |
| 3769 | meta = args[0]->val.meta[i].meta; |
| 3770 | val = &meta->value; |
| 3771 | if (val->realtype->basetype != LY_TYPE_IDENT) { |
| 3772 | /* uninteresting */ |
| 3773 | continue; |
| 3774 | } |
| 3775 | |
| 3776 | /* store args[1] as ident */ |
| 3777 | rc = val->realtype->plugin->store(set->ctx, val->realtype, args[1]->val.str, strlen(args[1]->val.str), |
| 3778 | LY_TYPE_OPTS_STORE, lys_resolve_prefix, (void *)meta->annotation->module, |
| 3779 | set->format, meta->parent, set->tree, &data, NULL, &err); |
| 3780 | } |
| 3781 | |
| 3782 | if (err) { |
| 3783 | ly_err_print(err); |
| 3784 | ly_err_free(err); |
| 3785 | } |
| 3786 | LY_CHECK_RET(rc); |
| 3787 | |
| 3788 | /* finally check the identity itself */ |
| 3789 | if (self_match && (data.ident == val->ident)) { |
| 3790 | set_fill_boolean(set, 1); |
| 3791 | found = 1; |
| 3792 | } |
| 3793 | if (!found) { |
| 3794 | LY_ARRAY_FOR(data.ident->derived, u) { |
| 3795 | if (data.ident->derived[u] == val->ident) { |
| 3796 | set_fill_boolean(set, 1); |
| 3797 | found = 1; |
| 3798 | break; |
| 3799 | } |
| 3800 | } |
| 3801 | } |
| 3802 | |
| 3803 | /* free temporary value */ |
| 3804 | val->realtype->plugin->free(set->ctx, &data); |
| 3805 | if (found) { |
| 3806 | break; |
| 3807 | } |
| 3808 | } |
| 3809 | |
| 3810 | return LY_SUCCESS; |
| 3811 | } |
| 3812 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3813 | /** |
| 3814 | * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending |
| 3815 | * on whether the first argument nodes contain a node of an identity derived from the second |
| 3816 | * argument identity. |
| 3817 | * |
| 3818 | * @param[in] args Array of arguments. |
| 3819 | * @param[in] arg_count Count of elements in @p args. |
| 3820 | * @param[in,out] set Context and result set at the same time. |
| 3821 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3822 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3823 | */ |
| 3824 | static LY_ERR |
| 3825 | xpath_derived_from(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3826 | { |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame^] | 3827 | return xpath_derived_(args, set, options, 0, __func__); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3828 | } |
| 3829 | |
| 3830 | /** |
| 3831 | * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending |
| 3832 | * on whether the first argument nodes contain a node of an identity that either is or is derived from |
| 3833 | * the second argument identity. |
| 3834 | * |
| 3835 | * @param[in] args Array of arguments. |
| 3836 | * @param[in] arg_count Count of elements in @p args. |
| 3837 | * @param[in,out] set Context and result set at the same time. |
| 3838 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3839 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3840 | */ |
| 3841 | static LY_ERR |
| 3842 | xpath_derived_from_or_self(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3843 | { |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame^] | 3844 | return xpath_derived_(args, set, options, 1, __func__); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3845 | } |
| 3846 | |
| 3847 | /** |
| 3848 | * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER |
| 3849 | * with the integer value of the first node's enum value, otherwise NaN. |
| 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_enum_value(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3859 | { |
| 3860 | struct lyd_node_term *leaf; |
| 3861 | struct lysc_node_leaf *sleaf; |
| 3862 | LY_ERR rc = LY_SUCCESS; |
| 3863 | |
| 3864 | if (options & LYXP_SCNODE_ALL) { |
| 3865 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3866 | 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] | 3867 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3868 | 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] | 3869 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) { |
| 3870 | 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] | 3871 | } |
| 3872 | set_scnode_clear_ctx(set); |
| 3873 | return rc; |
| 3874 | } |
| 3875 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3876 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3877 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "enum-value(node-set)"); |
| 3878 | return LY_EVALID; |
| 3879 | } |
| 3880 | |
| 3881 | set_fill_number(set, NAN); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3882 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3883 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
| 3884 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 3885 | if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) { |
| 3886 | set_fill_number(set, leaf->value.enum_item->value); |
| 3887 | } |
| 3888 | } |
| 3889 | |
| 3890 | return LY_SUCCESS; |
| 3891 | } |
| 3892 | |
| 3893 | /** |
| 3894 | * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN |
| 3895 | * with false value. |
| 3896 | * |
| 3897 | * @param[in] args Array of arguments. |
| 3898 | * @param[in] arg_count Count of elements in @p args. |
| 3899 | * @param[in,out] set Context and result set at the same time. |
| 3900 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3901 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3902 | */ |
| 3903 | static LY_ERR |
| 3904 | xpath_false(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3905 | { |
| 3906 | if (options & LYXP_SCNODE_ALL) { |
| 3907 | set_scnode_clear_ctx(set); |
| 3908 | return LY_SUCCESS; |
| 3909 | } |
| 3910 | |
| 3911 | set_fill_boolean(set, 0); |
| 3912 | return LY_SUCCESS; |
| 3913 | } |
| 3914 | |
| 3915 | /** |
| 3916 | * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER |
| 3917 | * with the first argument floored (truncated). |
| 3918 | * |
| 3919 | * @param[in] args Array of arguments. |
| 3920 | * @param[in] arg_count Count of elements in @p args. |
| 3921 | * @param[in,out] set Context and result set at the same time. |
| 3922 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3923 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3924 | */ |
| 3925 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3926 | 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] | 3927 | { |
| 3928 | LY_ERR rc; |
| 3929 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3930 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3931 | LY_CHECK_RET(rc); |
| 3932 | if (isfinite(args[0]->val.num)) { |
| 3933 | set_fill_number(set, (long long)args[0]->val.num); |
| 3934 | } |
| 3935 | |
| 3936 | return LY_SUCCESS; |
| 3937 | } |
| 3938 | |
| 3939 | /** |
| 3940 | * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN |
| 3941 | * whether the language of the text matches the one from the argument. |
| 3942 | * |
| 3943 | * @param[in] args Array of arguments. |
| 3944 | * @param[in] arg_count Count of elements in @p args. |
| 3945 | * @param[in,out] set Context and result set at the same time. |
| 3946 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3947 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3948 | */ |
| 3949 | static LY_ERR |
| 3950 | xpath_lang(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3951 | { |
| 3952 | const struct lyd_node *node; |
| 3953 | struct lysc_node_leaf *sleaf; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 3954 | struct lyd_meta *meta = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3955 | const char *val; |
| 3956 | int i, dynamic; |
| 3957 | LY_ERR rc = LY_SUCCESS; |
| 3958 | |
| 3959 | if (options & LYXP_SCNODE_ALL) { |
| 3960 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3961 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3962 | 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] | 3963 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3964 | 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] | 3965 | } |
| 3966 | } |
| 3967 | set_scnode_clear_ctx(set); |
| 3968 | return rc; |
| 3969 | } |
| 3970 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3971 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3972 | LY_CHECK_RET(rc); |
| 3973 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3974 | if (set->type != LYXP_SET_NODE_SET) { |
| 3975 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "lang(string)"); |
| 3976 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3977 | } else if (!set->used) { |
| 3978 | set_fill_boolean(set, 0); |
| 3979 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3980 | } |
| 3981 | |
| 3982 | switch (set->val.nodes[0].type) { |
| 3983 | case LYXP_NODE_ELEM: |
| 3984 | case LYXP_NODE_TEXT: |
| 3985 | node = set->val.nodes[0].node; |
| 3986 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 3987 | case LYXP_NODE_META: |
| 3988 | node = set->val.meta[0].meta->parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3989 | break; |
| 3990 | default: |
| 3991 | /* nothing to do with roots */ |
| 3992 | set_fill_boolean(set, 0); |
| 3993 | return LY_SUCCESS; |
| 3994 | } |
| 3995 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 3996 | /* find lang metadata */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3997 | for (; node; node = (struct lyd_node *)node->parent) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 3998 | for (meta = node->meta; meta; meta = meta->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3999 | /* annotations */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4000 | 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] | 4001 | break; |
| 4002 | } |
| 4003 | } |
| 4004 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4005 | if (meta) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4006 | break; |
| 4007 | } |
| 4008 | } |
| 4009 | |
| 4010 | /* compare languages */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4011 | if (!meta) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4012 | set_fill_boolean(set, 0); |
| 4013 | } else { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4014 | val = lyd_meta2str(meta, &dynamic); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4015 | for (i = 0; args[0]->val.str[i]; ++i) { |
| 4016 | if (tolower(args[0]->val.str[i]) != tolower(val[i])) { |
| 4017 | set_fill_boolean(set, 0); |
| 4018 | break; |
| 4019 | } |
| 4020 | } |
| 4021 | if (!args[0]->val.str[i]) { |
| 4022 | if (!val[i] || (val[i] == '-')) { |
| 4023 | set_fill_boolean(set, 1); |
| 4024 | } else { |
| 4025 | set_fill_boolean(set, 0); |
| 4026 | } |
| 4027 | } |
| 4028 | if (dynamic) { |
| 4029 | free((char *)val); |
| 4030 | } |
| 4031 | } |
| 4032 | |
| 4033 | return LY_SUCCESS; |
| 4034 | } |
| 4035 | |
| 4036 | /** |
| 4037 | * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER |
| 4038 | * with the context size. |
| 4039 | * |
| 4040 | * @param[in] args Array of arguments. |
| 4041 | * @param[in] arg_count Count of elements in @p args. |
| 4042 | * @param[in,out] set Context and result set at the same time. |
| 4043 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4044 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4045 | */ |
| 4046 | static LY_ERR |
| 4047 | xpath_last(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 4048 | { |
| 4049 | if (options & LYXP_SCNODE_ALL) { |
| 4050 | set_scnode_clear_ctx(set); |
| 4051 | return LY_SUCCESS; |
| 4052 | } |
| 4053 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4054 | if (set->type != LYXP_SET_NODE_SET) { |
| 4055 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "last()"); |
| 4056 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4057 | } else if (!set->used) { |
| 4058 | set_fill_number(set, 0); |
| 4059 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4060 | } |
| 4061 | |
| 4062 | set_fill_number(set, set->ctx_size); |
| 4063 | return LY_SUCCESS; |
| 4064 | } |
| 4065 | |
| 4066 | /** |
| 4067 | * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING |
| 4068 | * with the node name without namespace from the argument or the context. |
| 4069 | * |
| 4070 | * @param[in] args Array of arguments. |
| 4071 | * @param[in] arg_count Count of elements in @p args. |
| 4072 | * @param[in,out] set Context and result set at the same time. |
| 4073 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4074 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4075 | */ |
| 4076 | static LY_ERR |
| 4077 | xpath_local_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options) |
| 4078 | { |
| 4079 | struct lyxp_set_node *item; |
| 4080 | /* suppress unused variable warning */ |
| 4081 | (void)options; |
| 4082 | |
| 4083 | if (options & LYXP_SCNODE_ALL) { |
| 4084 | set_scnode_clear_ctx(set); |
| 4085 | return LY_SUCCESS; |
| 4086 | } |
| 4087 | |
| 4088 | if (arg_count) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4089 | if (args[0]->type != LYXP_SET_NODE_SET) { |
| 4090 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "local-name(node-set?)"); |
| 4091 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4092 | } else if (!args[0]->used) { |
| 4093 | set_fill_string(set, "", 0); |
| 4094 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4095 | } |
| 4096 | |
| 4097 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4098 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4099 | |
| 4100 | item = &args[0]->val.nodes[0]; |
| 4101 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4102 | if (set->type != LYXP_SET_NODE_SET) { |
| 4103 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "local-name(node-set?)"); |
| 4104 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4105 | } else if (!set->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(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4112 | |
| 4113 | item = &set->val.nodes[0]; |
| 4114 | } |
| 4115 | |
| 4116 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4117 | case LYXP_NODE_NONE: |
| 4118 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4119 | case LYXP_NODE_ROOT: |
| 4120 | case LYXP_NODE_ROOT_CONFIG: |
| 4121 | case LYXP_NODE_TEXT: |
| 4122 | set_fill_string(set, "", 0); |
| 4123 | break; |
| 4124 | case LYXP_NODE_ELEM: |
| 4125 | set_fill_string(set, item->node->schema->name, strlen(item->node->schema->name)); |
| 4126 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4127 | case LYXP_NODE_META: |
| 4128 | 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] | 4129 | break; |
| 4130 | } |
| 4131 | |
| 4132 | return LY_SUCCESS; |
| 4133 | } |
| 4134 | |
| 4135 | /** |
| 4136 | * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING |
| 4137 | * with the node name fully qualified (with namespace) from the argument or the context. |
| 4138 | * |
| 4139 | * @param[in] args Array of arguments. |
| 4140 | * @param[in] arg_count Count of elements in @p args. |
| 4141 | * @param[in,out] set Context and result set at the same time. |
| 4142 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4143 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4144 | */ |
| 4145 | static LY_ERR |
| 4146 | xpath_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options) |
| 4147 | { |
| 4148 | struct lyxp_set_node *item; |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 4149 | struct lys_module *mod = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4150 | char *str; |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 4151 | const char *name = NULL; |
| 4152 | int rc = -1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4153 | |
| 4154 | if (options & LYXP_SCNODE_ALL) { |
| 4155 | set_scnode_clear_ctx(set); |
| 4156 | return LY_SUCCESS; |
| 4157 | } |
| 4158 | |
| 4159 | if (arg_count) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4160 | if (args[0]->type != LYXP_SET_NODE_SET) { |
| 4161 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "name(node-set?)"); |
| 4162 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4163 | } else if (!args[0]->used) { |
| 4164 | set_fill_string(set, "", 0); |
| 4165 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4166 | } |
| 4167 | |
| 4168 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4169 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4170 | |
| 4171 | item = &args[0]->val.nodes[0]; |
| 4172 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4173 | if (set->type != LYXP_SET_NODE_SET) { |
| 4174 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "name(node-set?)"); |
| 4175 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4176 | } else if (!set->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(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4183 | |
| 4184 | item = &set->val.nodes[0]; |
| 4185 | } |
| 4186 | |
| 4187 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4188 | case LYXP_NODE_NONE: |
| 4189 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4190 | case LYXP_NODE_ROOT: |
| 4191 | case LYXP_NODE_ROOT_CONFIG: |
| 4192 | case LYXP_NODE_TEXT: |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 4193 | /* keep NULL */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4194 | break; |
| 4195 | case LYXP_NODE_ELEM: |
| 4196 | mod = item->node->schema->module; |
| 4197 | name = item->node->schema->name; |
| 4198 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4199 | case LYXP_NODE_META: |
| 4200 | mod = ((struct lyd_meta *)item->node)->annotation->module; |
| 4201 | name = ((struct lyd_meta *)item->node)->name; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4202 | break; |
| 4203 | } |
| 4204 | |
| 4205 | if (mod && name) { |
| 4206 | switch (set->format) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 4207 | case LYD_SCHEMA: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4208 | rc = asprintf(&str, "%s:%s", lys_prefix_find_module(set->local_mod, mod), name); |
| 4209 | break; |
| 4210 | case LYD_JSON: |
| 4211 | rc = asprintf(&str, "%s:%s", mod->name, name); |
| 4212 | break; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 4213 | case LYD_XML: |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 4214 | case LYD_LYB: |
Michal Vasko | 9409ef6 | 2019-09-12 11:47:17 +0200 | [diff] [blame] | 4215 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4216 | } |
| 4217 | LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM); |
| 4218 | set_fill_string(set, str, strlen(str)); |
| 4219 | free(str); |
| 4220 | } else { |
| 4221 | set_fill_string(set, "", 0); |
| 4222 | } |
| 4223 | |
| 4224 | return LY_SUCCESS; |
| 4225 | } |
| 4226 | |
| 4227 | /** |
| 4228 | * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING |
| 4229 | * with the namespace of the node from the argument or the context. |
| 4230 | * |
| 4231 | * @param[in] args Array of arguments. |
| 4232 | * @param[in] arg_count Count of elements in @p args. |
| 4233 | * @param[in,out] set Context and result set at the same time. |
| 4234 | * @param[in] options XPath options. |
| 4235 | * @return LY_ERR (LY_EINVAL for wrong arguments on schema) |
| 4236 | */ |
| 4237 | static LY_ERR |
| 4238 | xpath_namespace_uri(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options) |
| 4239 | { |
| 4240 | struct lyxp_set_node *item; |
| 4241 | struct lys_module *mod; |
| 4242 | /* suppress unused variable warning */ |
| 4243 | (void)options; |
| 4244 | |
| 4245 | if (options & LYXP_SCNODE_ALL) { |
| 4246 | set_scnode_clear_ctx(set); |
| 4247 | return LY_SUCCESS; |
| 4248 | } |
| 4249 | |
| 4250 | if (arg_count) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4251 | if (args[0]->type != LYXP_SET_NODE_SET) { |
| 4252 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), |
| 4253 | "namespace-uri(node-set?)"); |
| 4254 | return LY_EVALID; |
| 4255 | } else if (!args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4256 | set_fill_string(set, "", 0); |
| 4257 | return LY_SUCCESS; |
| 4258 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4259 | |
| 4260 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4261 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4262 | |
| 4263 | item = &args[0]->val.nodes[0]; |
| 4264 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4265 | if (set->type != LYXP_SET_NODE_SET) { |
| 4266 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "namespace-uri(node-set?)"); |
| 4267 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4268 | } else if (!set->used) { |
| 4269 | set_fill_string(set, "", 0); |
| 4270 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4271 | } |
| 4272 | |
| 4273 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4274 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4275 | |
| 4276 | item = &set->val.nodes[0]; |
| 4277 | } |
| 4278 | |
| 4279 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4280 | case LYXP_NODE_NONE: |
| 4281 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4282 | case LYXP_NODE_ROOT: |
| 4283 | case LYXP_NODE_ROOT_CONFIG: |
| 4284 | case LYXP_NODE_TEXT: |
| 4285 | set_fill_string(set, "", 0); |
| 4286 | break; |
| 4287 | case LYXP_NODE_ELEM: |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4288 | case LYXP_NODE_META: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4289 | if (item->type == LYXP_NODE_ELEM) { |
| 4290 | mod = item->node->schema->module; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4291 | } else { /* LYXP_NODE_META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4292 | /* annotations */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4293 | mod = ((struct lyd_meta *)item->node)->annotation->module; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4294 | } |
| 4295 | |
| 4296 | set_fill_string(set, mod->ns, strlen(mod->ns)); |
| 4297 | break; |
| 4298 | } |
| 4299 | |
| 4300 | return LY_SUCCESS; |
| 4301 | } |
| 4302 | |
| 4303 | /** |
| 4304 | * @brief Execute the XPath node() function (node type). Returns LYXP_SET_NODE_SET |
| 4305 | * with only nodes from the context. In practice it either leaves the context |
| 4306 | * as it is or returns an empty node set. |
| 4307 | * |
| 4308 | * @param[in] args Array of arguments. |
| 4309 | * @param[in] arg_count Count of elements in @p args. |
| 4310 | * @param[in,out] set Context and result set at the same time. |
| 4311 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4312 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4313 | */ |
| 4314 | static LY_ERR |
| 4315 | xpath_node(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 4316 | { |
| 4317 | if (options & LYXP_SCNODE_ALL) { |
| 4318 | set_scnode_clear_ctx(set); |
| 4319 | return LY_SUCCESS; |
| 4320 | } |
| 4321 | |
| 4322 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4323 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4324 | } |
| 4325 | return LY_SUCCESS; |
| 4326 | } |
| 4327 | |
| 4328 | /** |
| 4329 | * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING |
| 4330 | * with normalized value (no leading, trailing, double white spaces) of the node |
| 4331 | * from the argument or the context. |
| 4332 | * |
| 4333 | * @param[in] args Array of arguments. |
| 4334 | * @param[in] arg_count Count of elements in @p args. |
| 4335 | * @param[in,out] set Context and result set at the same time. |
| 4336 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4337 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4338 | */ |
| 4339 | static LY_ERR |
| 4340 | xpath_normalize_space(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options) |
| 4341 | { |
| 4342 | uint16_t i, new_used; |
| 4343 | char *new; |
| 4344 | int have_spaces = 0, space_before = 0; |
| 4345 | struct lysc_node_leaf *sleaf; |
| 4346 | LY_ERR rc = LY_SUCCESS; |
| 4347 | |
| 4348 | if (options & LYXP_SCNODE_ALL) { |
| 4349 | if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4350 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4351 | 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] | 4352 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4353 | 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] | 4354 | } |
| 4355 | } |
| 4356 | set_scnode_clear_ctx(set); |
| 4357 | return rc; |
| 4358 | } |
| 4359 | |
| 4360 | if (arg_count) { |
| 4361 | set_fill_set(set, args[0]); |
| 4362 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4363 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4364 | LY_CHECK_RET(rc); |
| 4365 | |
| 4366 | /* is there any normalization necessary? */ |
| 4367 | for (i = 0; set->val.str[i]; ++i) { |
| 4368 | if (is_xmlws(set->val.str[i])) { |
| 4369 | if ((i == 0) || space_before || (!set->val.str[i + 1])) { |
| 4370 | have_spaces = 1; |
| 4371 | break; |
| 4372 | } |
| 4373 | space_before = 1; |
| 4374 | } else { |
| 4375 | space_before = 0; |
| 4376 | } |
| 4377 | } |
| 4378 | |
| 4379 | /* yep, there is */ |
| 4380 | if (have_spaces) { |
| 4381 | /* it's enough, at least one character will go, makes space for ending '\0' */ |
| 4382 | new = malloc(strlen(set->val.str) * sizeof(char)); |
| 4383 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 4384 | new_used = 0; |
| 4385 | |
| 4386 | space_before = 0; |
| 4387 | for (i = 0; set->val.str[i]; ++i) { |
| 4388 | if (is_xmlws(set->val.str[i])) { |
| 4389 | if ((i == 0) || space_before) { |
| 4390 | space_before = 1; |
| 4391 | continue; |
| 4392 | } else { |
| 4393 | space_before = 1; |
| 4394 | } |
| 4395 | } else { |
| 4396 | space_before = 0; |
| 4397 | } |
| 4398 | |
| 4399 | new[new_used] = (space_before ? ' ' : set->val.str[i]); |
| 4400 | ++new_used; |
| 4401 | } |
| 4402 | |
| 4403 | /* at worst there is one trailing space now */ |
| 4404 | if (new_used && is_xmlws(new[new_used - 1])) { |
| 4405 | --new_used; |
| 4406 | } |
| 4407 | |
| 4408 | new = ly_realloc(new, (new_used + 1) * sizeof(char)); |
| 4409 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 4410 | new[new_used] = '\0'; |
| 4411 | |
| 4412 | free(set->val.str); |
| 4413 | set->val.str = new; |
| 4414 | } |
| 4415 | |
| 4416 | return LY_SUCCESS; |
| 4417 | } |
| 4418 | |
| 4419 | /** |
| 4420 | * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN |
| 4421 | * with the argument converted to boolean and logically inverted. |
| 4422 | * |
| 4423 | * @param[in] args Array of arguments. |
| 4424 | * @param[in] arg_count Count of elements in @p args. |
| 4425 | * @param[in,out] set Context and result set at the same time. |
| 4426 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4427 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4428 | */ |
| 4429 | static LY_ERR |
| 4430 | xpath_not(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 4431 | { |
| 4432 | if (options & LYXP_SCNODE_ALL) { |
| 4433 | set_scnode_clear_ctx(set); |
| 4434 | return LY_SUCCESS; |
| 4435 | } |
| 4436 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4437 | lyxp_set_cast(args[0], LYXP_SET_BOOLEAN); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4438 | if (args[0]->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4439 | set_fill_boolean(set, 0); |
| 4440 | } else { |
| 4441 | set_fill_boolean(set, 1); |
| 4442 | } |
| 4443 | |
| 4444 | return LY_SUCCESS; |
| 4445 | } |
| 4446 | |
| 4447 | /** |
| 4448 | * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER |
| 4449 | * with the number representation of either the argument or the context. |
| 4450 | * |
| 4451 | * @param[in] args Array of arguments. |
| 4452 | * @param[in] arg_count Count of elements in @p args. |
| 4453 | * @param[in,out] set Context and result set at the same time. |
| 4454 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4455 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4456 | */ |
| 4457 | static LY_ERR |
| 4458 | xpath_number(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options) |
| 4459 | { |
| 4460 | LY_ERR rc; |
| 4461 | |
| 4462 | if (options & LYXP_SCNODE_ALL) { |
| 4463 | set_scnode_clear_ctx(set); |
| 4464 | return LY_SUCCESS; |
| 4465 | } |
| 4466 | |
| 4467 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4468 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4469 | LY_CHECK_RET(rc); |
| 4470 | set_fill_set(set, args[0]); |
| 4471 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4472 | rc = lyxp_set_cast(set, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4473 | LY_CHECK_RET(rc); |
| 4474 | } |
| 4475 | |
| 4476 | return LY_SUCCESS; |
| 4477 | } |
| 4478 | |
| 4479 | /** |
| 4480 | * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER |
| 4481 | * with the context position. |
| 4482 | * |
| 4483 | * @param[in] args Array of arguments. |
| 4484 | * @param[in] arg_count Count of elements in @p args. |
| 4485 | * @param[in,out] set Context and result set at the same time. |
| 4486 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4487 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4488 | */ |
| 4489 | static LY_ERR |
| 4490 | xpath_position(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 4491 | { |
| 4492 | if (options & LYXP_SCNODE_ALL) { |
| 4493 | set_scnode_clear_ctx(set); |
| 4494 | return LY_SUCCESS; |
| 4495 | } |
| 4496 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4497 | if (set->type != LYXP_SET_NODE_SET) { |
| 4498 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "position()"); |
| 4499 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4500 | } else if (!set->used) { |
| 4501 | set_fill_number(set, 0); |
| 4502 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4503 | } |
| 4504 | |
| 4505 | set_fill_number(set, set->ctx_pos); |
| 4506 | |
| 4507 | /* UNUSED in 'Release' build type */ |
| 4508 | (void)options; |
| 4509 | return LY_SUCCESS; |
| 4510 | } |
| 4511 | |
| 4512 | /** |
| 4513 | * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN |
| 4514 | * depending on whether the second argument regex matches the first argument string. For details refer to |
| 4515 | * YANG 1.1 RFC section 10.2.1. |
| 4516 | * |
| 4517 | * @param[in] args Array of arguments. |
| 4518 | * @param[in] arg_count Count of elements in @p args. |
| 4519 | * @param[in,out] set Context and result set at the same time. |
| 4520 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4521 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4522 | */ |
| 4523 | static LY_ERR |
| 4524 | xpath_re_match(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 4525 | { |
| 4526 | struct lysc_pattern **patterns = NULL, **pattern; |
| 4527 | struct lysc_node_leaf *sleaf; |
| 4528 | char *path; |
| 4529 | LY_ERR rc = LY_SUCCESS; |
| 4530 | struct ly_err_item *err; |
| 4531 | |
| 4532 | if (options & LYXP_SCNODE_ALL) { |
| 4533 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4534 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4535 | 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] | 4536 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4537 | 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] | 4538 | } |
| 4539 | } |
| 4540 | |
| 4541 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4542 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4543 | 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] | 4544 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4545 | 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] | 4546 | } |
| 4547 | } |
| 4548 | set_scnode_clear_ctx(set); |
| 4549 | return rc; |
| 4550 | } |
| 4551 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4552 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4553 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4554 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4555 | LY_CHECK_RET(rc); |
| 4556 | |
| 4557 | LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM); |
| 4558 | *pattern = malloc(sizeof **pattern); |
| 4559 | path = lyd_path(set->ctx_node, LYD_PATH_LOG, NULL, 0); |
| 4560 | rc = lys_compile_type_pattern_check(set->ctx, path, args[1]->val.str, &(*pattern)->code); |
| 4561 | free(path); |
| 4562 | if (rc != LY_SUCCESS) { |
| 4563 | LY_ARRAY_FREE(patterns); |
| 4564 | return rc; |
| 4565 | } |
| 4566 | |
| 4567 | rc = ly_type_validate_patterns(patterns, args[0]->val.str, strlen(args[0]->val.str), &err); |
| 4568 | pcre2_code_free((*pattern)->code); |
| 4569 | free(*pattern); |
| 4570 | LY_ARRAY_FREE(patterns); |
| 4571 | if (rc && (rc != LY_EVALID)) { |
| 4572 | ly_err_print(err); |
| 4573 | ly_err_free(err); |
| 4574 | return rc; |
| 4575 | } |
| 4576 | |
| 4577 | if (rc == LY_EVALID) { |
| 4578 | ly_err_free(err); |
| 4579 | set_fill_boolean(set, 0); |
| 4580 | } else { |
| 4581 | set_fill_boolean(set, 1); |
| 4582 | } |
| 4583 | |
| 4584 | return LY_SUCCESS; |
| 4585 | } |
| 4586 | |
| 4587 | /** |
| 4588 | * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER |
| 4589 | * with the rounded first argument. For details refer to |
| 4590 | * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round. |
| 4591 | * |
| 4592 | * @param[in] args Array of arguments. |
| 4593 | * @param[in] arg_count Count of elements in @p args. |
| 4594 | * @param[in,out] set Context and result set at the same time. |
| 4595 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4596 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4597 | */ |
| 4598 | static LY_ERR |
| 4599 | xpath_round(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 4600 | { |
| 4601 | struct lysc_node_leaf *sleaf; |
| 4602 | LY_ERR rc = LY_SUCCESS; |
| 4603 | |
| 4604 | if (options & LYXP_SCNODE_ALL) { |
| 4605 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4606 | 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] | 4607 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4608 | 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] | 4609 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) { |
| 4610 | 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] | 4611 | } |
| 4612 | set_scnode_clear_ctx(set); |
| 4613 | return rc; |
| 4614 | } |
| 4615 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4616 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4617 | LY_CHECK_RET(rc); |
| 4618 | |
| 4619 | /* cover only the cases where floor can't be used */ |
| 4620 | if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) { |
| 4621 | set_fill_number(set, -0.0f); |
| 4622 | } else { |
| 4623 | args[0]->val.num += 0.5; |
| 4624 | rc = xpath_floor(args, 1, args[0], options); |
| 4625 | LY_CHECK_RET(rc); |
| 4626 | set_fill_number(set, args[0]->val.num); |
| 4627 | } |
| 4628 | |
| 4629 | return LY_SUCCESS; |
| 4630 | } |
| 4631 | |
| 4632 | /** |
| 4633 | * @brief Execute the XPath starts-with(string, string) function. |
| 4634 | * Returns LYXP_SET_BOOLEAN whether the second argument is |
| 4635 | * the prefix of the first or not. |
| 4636 | * |
| 4637 | * @param[in] args Array of arguments. |
| 4638 | * @param[in] arg_count Count of elements in @p args. |
| 4639 | * @param[in,out] set Context and result set at the same time. |
| 4640 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4641 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4642 | */ |
| 4643 | static LY_ERR |
| 4644 | xpath_starts_with(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 4645 | { |
| 4646 | struct lysc_node_leaf *sleaf; |
| 4647 | LY_ERR rc = LY_SUCCESS; |
| 4648 | |
| 4649 | if (options & LYXP_SCNODE_ALL) { |
| 4650 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4651 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4652 | 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] | 4653 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4654 | 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] | 4655 | } |
| 4656 | } |
| 4657 | |
| 4658 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4659 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4660 | 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] | 4661 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4662 | 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] | 4663 | } |
| 4664 | } |
| 4665 | set_scnode_clear_ctx(set); |
| 4666 | return rc; |
| 4667 | } |
| 4668 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4669 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4670 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4671 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4672 | LY_CHECK_RET(rc); |
| 4673 | |
| 4674 | if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) { |
| 4675 | set_fill_boolean(set, 0); |
| 4676 | } else { |
| 4677 | set_fill_boolean(set, 1); |
| 4678 | } |
| 4679 | |
| 4680 | return LY_SUCCESS; |
| 4681 | } |
| 4682 | |
| 4683 | /** |
| 4684 | * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING |
| 4685 | * with the string representation of either the argument or the context. |
| 4686 | * |
| 4687 | * @param[in] args Array of arguments. |
| 4688 | * @param[in] arg_count Count of elements in @p args. |
| 4689 | * @param[in,out] set Context and result set at the same time. |
| 4690 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4691 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4692 | */ |
| 4693 | static LY_ERR |
| 4694 | xpath_string(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options) |
| 4695 | { |
| 4696 | LY_ERR rc; |
| 4697 | |
| 4698 | if (options & LYXP_SCNODE_ALL) { |
| 4699 | set_scnode_clear_ctx(set); |
| 4700 | return LY_SUCCESS; |
| 4701 | } |
| 4702 | |
| 4703 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4704 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4705 | LY_CHECK_RET(rc); |
| 4706 | set_fill_set(set, args[0]); |
| 4707 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4708 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4709 | LY_CHECK_RET(rc); |
| 4710 | } |
| 4711 | |
| 4712 | return LY_SUCCESS; |
| 4713 | } |
| 4714 | |
| 4715 | /** |
| 4716 | * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER |
| 4717 | * with the length of the string in either the argument or the context. |
| 4718 | * |
| 4719 | * @param[in] args Array of arguments. |
| 4720 | * @param[in] arg_count Count of elements in @p args. |
| 4721 | * @param[in,out] set Context and result set at the same time. |
| 4722 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4723 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4724 | */ |
| 4725 | static LY_ERR |
| 4726 | xpath_string_length(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options) |
| 4727 | { |
| 4728 | struct lysc_node_leaf *sleaf; |
| 4729 | LY_ERR rc = LY_SUCCESS; |
| 4730 | |
| 4731 | if (options & LYXP_SCNODE_ALL) { |
| 4732 | if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4733 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4734 | 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] | 4735 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4736 | 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] | 4737 | } |
| 4738 | } |
| 4739 | if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) { |
| 4740 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4741 | 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] | 4742 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4743 | 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] | 4744 | } |
| 4745 | } |
| 4746 | set_scnode_clear_ctx(set); |
| 4747 | return rc; |
| 4748 | } |
| 4749 | |
| 4750 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4751 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4752 | LY_CHECK_RET(rc); |
| 4753 | set_fill_number(set, strlen(args[0]->val.str)); |
| 4754 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4755 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4756 | LY_CHECK_RET(rc); |
| 4757 | set_fill_number(set, strlen(set->val.str)); |
| 4758 | } |
| 4759 | |
| 4760 | return LY_SUCCESS; |
| 4761 | } |
| 4762 | |
| 4763 | /** |
| 4764 | * @brief Execute the XPath substring(string, number, number?) function. |
| 4765 | * Returns LYXP_SET_STRING substring of the first argument starting |
| 4766 | * on the second argument index ending on the third argument index, |
| 4767 | * indexed from 1. For exact definition refer to |
| 4768 | * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring. |
| 4769 | * |
| 4770 | * @param[in] args Array of arguments. |
| 4771 | * @param[in] arg_count Count of elements in @p args. |
| 4772 | * @param[in,out] set Context and result set at the same time. |
| 4773 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4774 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4775 | */ |
| 4776 | static LY_ERR |
| 4777 | xpath_substring(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options) |
| 4778 | { |
| 4779 | int start, len; |
| 4780 | uint16_t str_start, str_len, pos; |
| 4781 | struct lysc_node_leaf *sleaf; |
| 4782 | LY_ERR rc = LY_SUCCESS; |
| 4783 | |
| 4784 | if (options & LYXP_SCNODE_ALL) { |
| 4785 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4786 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4787 | 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] | 4788 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4789 | 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] | 4790 | } |
| 4791 | } |
| 4792 | |
| 4793 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4794 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4795 | 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] | 4796 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 4797 | 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] | 4798 | } |
| 4799 | } |
| 4800 | |
| 4801 | if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET) |
| 4802 | && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) { |
| 4803 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4804 | 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] | 4805 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 4806 | 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] | 4807 | } |
| 4808 | } |
| 4809 | set_scnode_clear_ctx(set); |
| 4810 | return rc; |
| 4811 | } |
| 4812 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4813 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4814 | LY_CHECK_RET(rc); |
| 4815 | |
| 4816 | /* start */ |
| 4817 | if (xpath_round(&args[1], 1, args[1], options)) { |
| 4818 | return -1; |
| 4819 | } |
| 4820 | if (isfinite(args[1]->val.num)) { |
| 4821 | start = args[1]->val.num - 1; |
| 4822 | } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) { |
| 4823 | start = INT_MIN; |
| 4824 | } else { |
| 4825 | start = INT_MAX; |
| 4826 | } |
| 4827 | |
| 4828 | /* len */ |
| 4829 | if (arg_count == 3) { |
| 4830 | rc = xpath_round(&args[2], 1, args[2], options); |
| 4831 | LY_CHECK_RET(rc); |
| 4832 | if (isfinite(args[2]->val.num)) { |
| 4833 | len = args[2]->val.num; |
| 4834 | } else if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) { |
| 4835 | len = 0; |
| 4836 | } else { |
| 4837 | len = INT_MAX; |
| 4838 | } |
| 4839 | } else { |
| 4840 | len = INT_MAX; |
| 4841 | } |
| 4842 | |
| 4843 | /* find matching character positions */ |
| 4844 | str_start = 0; |
| 4845 | str_len = 0; |
| 4846 | for (pos = 0; args[0]->val.str[pos]; ++pos) { |
| 4847 | if (pos < start) { |
| 4848 | ++str_start; |
| 4849 | } else if (pos < start + len) { |
| 4850 | ++str_len; |
| 4851 | } else { |
| 4852 | break; |
| 4853 | } |
| 4854 | } |
| 4855 | |
| 4856 | set_fill_string(set, args[0]->val.str + str_start, str_len); |
| 4857 | return LY_SUCCESS; |
| 4858 | } |
| 4859 | |
| 4860 | /** |
| 4861 | * @brief Execute the XPath substring-after(string, string) function. |
| 4862 | * Returns LYXP_SET_STRING with the string succeeding the occurance |
| 4863 | * of the second argument in the first or an empty string. |
| 4864 | * |
| 4865 | * @param[in] args Array of arguments. |
| 4866 | * @param[in] arg_count Count of elements in @p args. |
| 4867 | * @param[in,out] set Context and result set at the same time. |
| 4868 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4869 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4870 | */ |
| 4871 | static LY_ERR |
| 4872 | xpath_substring_after(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 4873 | { |
| 4874 | char *ptr; |
| 4875 | struct lysc_node_leaf *sleaf; |
| 4876 | LY_ERR rc = LY_SUCCESS; |
| 4877 | |
| 4878 | if (options & LYXP_SCNODE_ALL) { |
| 4879 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4880 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4881 | 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] | 4882 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4883 | 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] | 4884 | } |
| 4885 | } |
| 4886 | |
| 4887 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4888 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4889 | 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] | 4890 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4891 | 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] | 4892 | } |
| 4893 | } |
| 4894 | set_scnode_clear_ctx(set); |
| 4895 | return rc; |
| 4896 | } |
| 4897 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4898 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4899 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4900 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4901 | LY_CHECK_RET(rc); |
| 4902 | |
| 4903 | ptr = strstr(args[0]->val.str, args[1]->val.str); |
| 4904 | if (ptr) { |
| 4905 | set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str))); |
| 4906 | } else { |
| 4907 | set_fill_string(set, "", 0); |
| 4908 | } |
| 4909 | |
| 4910 | return LY_SUCCESS; |
| 4911 | } |
| 4912 | |
| 4913 | /** |
| 4914 | * @brief Execute the XPath substring-before(string, string) function. |
| 4915 | * Returns LYXP_SET_STRING with the string preceding the occurance |
| 4916 | * of the second argument in the first or an empty string. |
| 4917 | * |
| 4918 | * @param[in] args Array of arguments. |
| 4919 | * @param[in] arg_count Count of elements in @p args. |
| 4920 | * @param[in,out] set Context and result set at the same time. |
| 4921 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4922 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4923 | */ |
| 4924 | static LY_ERR |
| 4925 | xpath_substring_before(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 4926 | { |
| 4927 | char *ptr; |
| 4928 | struct lysc_node_leaf *sleaf; |
| 4929 | LY_ERR rc = LY_SUCCESS; |
| 4930 | |
| 4931 | if (options & LYXP_SCNODE_ALL) { |
| 4932 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4933 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4934 | 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] | 4935 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4936 | 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] | 4937 | } |
| 4938 | } |
| 4939 | |
| 4940 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4941 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4942 | 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] | 4943 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4944 | 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] | 4945 | } |
| 4946 | } |
| 4947 | set_scnode_clear_ctx(set); |
| 4948 | return rc; |
| 4949 | } |
| 4950 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4951 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4952 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4953 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4954 | LY_CHECK_RET(rc); |
| 4955 | |
| 4956 | ptr = strstr(args[0]->val.str, args[1]->val.str); |
| 4957 | if (ptr) { |
| 4958 | set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str); |
| 4959 | } else { |
| 4960 | set_fill_string(set, "", 0); |
| 4961 | } |
| 4962 | |
| 4963 | return LY_SUCCESS; |
| 4964 | } |
| 4965 | |
| 4966 | /** |
| 4967 | * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER |
| 4968 | * with the sum of all the nodes in the context. |
| 4969 | * |
| 4970 | * @param[in] args Array of arguments. |
| 4971 | * @param[in] arg_count Count of elements in @p args. |
| 4972 | * @param[in,out] set Context and result set at the same time. |
| 4973 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4974 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4975 | */ |
| 4976 | static LY_ERR |
| 4977 | xpath_sum(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 4978 | { |
| 4979 | long double num; |
| 4980 | char *str; |
| 4981 | uint16_t i; |
| 4982 | struct lyxp_set set_item; |
| 4983 | struct lysc_node_leaf *sleaf; |
| 4984 | LY_ERR rc = LY_SUCCESS; |
| 4985 | |
| 4986 | if (options & LYXP_SCNODE_ALL) { |
| 4987 | if (args[0]->type == LYXP_SET_SCNODE_SET) { |
| 4988 | for (i = 0; i < args[0]->used; ++i) { |
| 4989 | if (args[0]->val.scnodes[i].in_ctx == 1) { |
| 4990 | sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode; |
| 4991 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4992 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, |
| 4993 | lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4994 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 4995 | 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] | 4996 | } |
| 4997 | } |
| 4998 | } |
| 4999 | } |
| 5000 | set_scnode_clear_ctx(set); |
| 5001 | return rc; |
| 5002 | } |
| 5003 | |
| 5004 | set_fill_number(set, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5005 | |
| 5006 | if (args[0]->type != LYXP_SET_NODE_SET) { |
| 5007 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "sum(node-set)"); |
| 5008 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5009 | } else if (!args[0]->used) { |
| 5010 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5011 | } |
| 5012 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5013 | set_init(&set_item, set); |
| 5014 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5015 | set_item.type = LYXP_SET_NODE_SET; |
| 5016 | set_item.val.nodes = malloc(sizeof *set_item.val.nodes); |
| 5017 | LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM); |
| 5018 | |
| 5019 | set_item.used = 1; |
| 5020 | set_item.size = 1; |
| 5021 | |
| 5022 | for (i = 0; i < args[0]->used; ++i) { |
| 5023 | set_item.val.nodes[0] = args[0]->val.nodes[i]; |
| 5024 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5025 | rc = cast_node_set_to_string(&set_item, &str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5026 | LY_CHECK_RET(rc); |
| 5027 | num = cast_string_to_number(str); |
| 5028 | free(str); |
| 5029 | set->val.num += num; |
| 5030 | } |
| 5031 | |
| 5032 | free(set_item.val.nodes); |
| 5033 | |
| 5034 | return LY_SUCCESS; |
| 5035 | } |
| 5036 | |
| 5037 | /** |
| 5038 | * @brief Execute the XPath text() function (node type). Returns LYXP_SET_NODE_SET |
| 5039 | * with the text content of the nodes in the context. |
| 5040 | * |
| 5041 | * @param[in] args Array of arguments. |
| 5042 | * @param[in] arg_count Count of elements in @p args. |
| 5043 | * @param[in,out] set Context and result set at the same time. |
| 5044 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5045 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5046 | */ |
| 5047 | static LY_ERR |
| 5048 | xpath_text(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 5049 | { |
| 5050 | uint32_t i; |
| 5051 | |
| 5052 | if (options & LYXP_SCNODE_ALL) { |
| 5053 | set_scnode_clear_ctx(set); |
| 5054 | return LY_SUCCESS; |
| 5055 | } |
| 5056 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5057 | if (set->type != LYXP_SET_NODE_SET) { |
| 5058 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "text()"); |
| 5059 | return LY_EVALID; |
| 5060 | } |
| 5061 | |
| 5062 | for (i = 0; i < set->used;) { |
| 5063 | switch (set->val.nodes[i].type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 5064 | case LYXP_NODE_NONE: |
| 5065 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5066 | case LYXP_NODE_ELEM: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5067 | if (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 5068 | set->val.nodes[i].type = LYXP_NODE_TEXT; |
| 5069 | ++i; |
| 5070 | break; |
| 5071 | } |
| 5072 | /* fall through */ |
| 5073 | case LYXP_NODE_ROOT: |
| 5074 | case LYXP_NODE_ROOT_CONFIG: |
| 5075 | case LYXP_NODE_TEXT: |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5076 | case LYXP_NODE_META: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5077 | set_remove_node(set, i); |
| 5078 | break; |
| 5079 | } |
| 5080 | } |
| 5081 | |
| 5082 | return LY_SUCCESS; |
| 5083 | } |
| 5084 | |
| 5085 | /** |
| 5086 | * @brief Execute the XPath translate(string, string, string) function. |
| 5087 | * Returns LYXP_SET_STRING with the first argument with the characters |
| 5088 | * from the second argument replaced by those on the corresponding |
| 5089 | * positions in the third argument. |
| 5090 | * |
| 5091 | * @param[in] args Array of arguments. |
| 5092 | * @param[in] arg_count Count of elements in @p args. |
| 5093 | * @param[in,out] set Context and result set at the same time. |
| 5094 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5095 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5096 | */ |
| 5097 | static LY_ERR |
| 5098 | xpath_translate(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 5099 | { |
| 5100 | uint16_t i, j, new_used; |
| 5101 | char *new; |
| 5102 | int found, have_removed; |
| 5103 | struct lysc_node_leaf *sleaf; |
| 5104 | LY_ERR rc = LY_SUCCESS; |
| 5105 | |
| 5106 | if (options & LYXP_SCNODE_ALL) { |
| 5107 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 5108 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5109 | 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] | 5110 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5111 | 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] | 5112 | } |
| 5113 | } |
| 5114 | |
| 5115 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 5116 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5117 | 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] | 5118 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5119 | 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] | 5120 | } |
| 5121 | } |
| 5122 | |
| 5123 | if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) { |
| 5124 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5125 | 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] | 5126 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5127 | 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] | 5128 | } |
| 5129 | } |
| 5130 | set_scnode_clear_ctx(set); |
| 5131 | return rc; |
| 5132 | } |
| 5133 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5134 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5135 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5136 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5137 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5138 | rc = lyxp_set_cast(args[2], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5139 | LY_CHECK_RET(rc); |
| 5140 | |
| 5141 | new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char)); |
| 5142 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 5143 | new_used = 0; |
| 5144 | |
| 5145 | have_removed = 0; |
| 5146 | for (i = 0; args[0]->val.str[i]; ++i) { |
| 5147 | found = 0; |
| 5148 | |
| 5149 | for (j = 0; args[1]->val.str[j]; ++j) { |
| 5150 | if (args[0]->val.str[i] == args[1]->val.str[j]) { |
| 5151 | /* removing this char */ |
| 5152 | if (j >= strlen(args[2]->val.str)) { |
| 5153 | have_removed = 1; |
| 5154 | found = 1; |
| 5155 | break; |
| 5156 | } |
| 5157 | /* replacing this char */ |
| 5158 | new[new_used] = args[2]->val.str[j]; |
| 5159 | ++new_used; |
| 5160 | found = 1; |
| 5161 | break; |
| 5162 | } |
| 5163 | } |
| 5164 | |
| 5165 | /* copying this char */ |
| 5166 | if (!found) { |
| 5167 | new[new_used] = args[0]->val.str[i]; |
| 5168 | ++new_used; |
| 5169 | } |
| 5170 | } |
| 5171 | |
| 5172 | if (have_removed) { |
| 5173 | new = ly_realloc(new, (new_used + 1) * sizeof(char)); |
| 5174 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 5175 | } |
| 5176 | new[new_used] = '\0'; |
| 5177 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5178 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5179 | set->type = LYXP_SET_STRING; |
| 5180 | set->val.str = new; |
| 5181 | |
| 5182 | return LY_SUCCESS; |
| 5183 | } |
| 5184 | |
| 5185 | /** |
| 5186 | * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN |
| 5187 | * with true value. |
| 5188 | * |
| 5189 | * @param[in] args Array of arguments. |
| 5190 | * @param[in] arg_count Count of elements in @p args. |
| 5191 | * @param[in,out] set Context and result set at the same time. |
| 5192 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5193 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5194 | */ |
| 5195 | static LY_ERR |
| 5196 | xpath_true(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 5197 | { |
| 5198 | if (options & LYXP_SCNODE_ALL) { |
| 5199 | set_scnode_clear_ctx(set); |
| 5200 | return LY_SUCCESS; |
| 5201 | } |
| 5202 | |
| 5203 | set_fill_boolean(set, 1); |
| 5204 | return LY_SUCCESS; |
| 5205 | } |
| 5206 | |
| 5207 | /* |
| 5208 | * moveto functions |
| 5209 | * |
| 5210 | * They and only they actually change the context (set). |
| 5211 | */ |
| 5212 | |
| 5213 | /** |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5214 | * @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] | 5215 | * |
Michal Vasko | 2104e9f | 2020-03-06 08:23:25 +0100 | [diff] [blame] | 5216 | * XPath @p set is expected to be a (sc)node set! |
| 5217 | * |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5218 | * @param[in,out] qname Qualified node name. If includes prefix, it is skipped. |
| 5219 | * @param[in,out] qname_len Length of @p qname, is updated accordingly. |
| 5220 | * @param[in] set Set with XPath context. |
| 5221 | * @param[out] moveto_mod Expected module of a matching node. |
| 5222 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5223 | */ |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5224 | static LY_ERR |
| 5225 | 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] | 5226 | { |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 5227 | const struct lys_module *mod = NULL; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5228 | const char *ptr; |
| 5229 | int pref_len; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5230 | char *str; |
| 5231 | |
Michal Vasko | 2104e9f | 2020-03-06 08:23:25 +0100 | [diff] [blame] | 5232 | assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET)); |
| 5233 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5234 | if ((ptr = ly_strnchr(*qname, ':', *qname_len))) { |
| 5235 | /* specific module */ |
| 5236 | pref_len = ptr - *qname; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5237 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5238 | switch (set->format) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 5239 | case LYD_SCHEMA: |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5240 | /* schema, search all local module imports */ |
| 5241 | mod = lys_module_find_prefix(set->local_mod, *qname, pref_len); |
| 5242 | break; |
| 5243 | case LYD_JSON: |
| 5244 | /* JSON data, search in context */ |
| 5245 | str = strndup(*qname, pref_len); |
Michal Vasko | 97e76fd | 2020-05-27 15:22:01 +0200 | [diff] [blame] | 5246 | mod = ly_ctx_get_module_implemented(set->ctx, str); |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5247 | free(str); |
| 5248 | break; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 5249 | case LYD_XML: |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 5250 | case LYD_LYB: |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5251 | LOGINT_RET(set->ctx); |
| 5252 | } |
| 5253 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5254 | /* check for errors and non-implemented modules, as they are not valid */ |
Juraj Vijtiuk | d75faa6 | 2019-11-26 14:10:10 +0100 | [diff] [blame] | 5255 | if (!mod || !mod->implemented) { |
Michal Vasko | 2104e9f | 2020-03-06 08:23:25 +0100 | [diff] [blame] | 5256 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 5257 | LOGVAL(set->ctx, LY_VLOG_LYSC, set->ctx_scnode, LY_VCODE_XP_INMOD, pref_len, *qname); |
| 5258 | } else { |
| 5259 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INMOD, pref_len, *qname); |
| 5260 | } |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5261 | return LY_EVALID; |
| 5262 | } |
Juraj Vijtiuk | d75faa6 | 2019-11-26 14:10:10 +0100 | [diff] [blame] | 5263 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5264 | *qname += pref_len + 1; |
| 5265 | *qname_len -= pref_len + 1; |
| 5266 | } else if (((*qname)[0] == '*') && (*qname_len == 1)) { |
| 5267 | /* all modules - special case */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5268 | mod = NULL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5269 | } else if (set->type == LYXP_SET_SCNODE_SET) { |
| 5270 | /* current node module */ |
| 5271 | mod = set->ctx_scnode->module; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5272 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5273 | /* current node module */ |
| 5274 | mod = set->ctx_node->schema->module; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5275 | } |
| 5276 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5277 | *moveto_mod = mod; |
| 5278 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5279 | } |
| 5280 | |
| 5281 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5282 | * @brief Move context @p set to the root. Handles absolute path. |
| 5283 | * Result is LYXP_SET_NODE_SET. |
| 5284 | * |
| 5285 | * @param[in,out] set Set to use. |
| 5286 | * @param[in] options Xpath options. |
| 5287 | */ |
| 5288 | static void |
| 5289 | moveto_root(struct lyxp_set *set, int options) |
| 5290 | { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5291 | if (!set) { |
| 5292 | return; |
| 5293 | } |
| 5294 | |
| 5295 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5296 | set_scnode_clear_ctx(set); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 5297 | lyxp_set_scnode_insert_node(set, NULL, set->root_type); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5298 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5299 | set->type = LYXP_SET_NODE_SET; |
| 5300 | set->used = 0; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5301 | set_insert_node(set, NULL, 0, set->root_type, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5302 | } |
| 5303 | } |
| 5304 | |
| 5305 | /** |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 5306 | * @brief Check whether a node has some unresolved "when". |
| 5307 | * |
| 5308 | * @param[in] node Node to check. |
| 5309 | * @return LY_ERR value (LY_EINCOMPLETE if there are some unresolved "when") |
| 5310 | */ |
| 5311 | static LY_ERR |
| 5312 | moveto_when_check(const struct lyd_node *node) |
| 5313 | { |
| 5314 | const struct lysc_node *schema; |
| 5315 | |
| 5316 | if (!node) { |
| 5317 | return LY_SUCCESS; |
| 5318 | } |
| 5319 | |
| 5320 | schema = node->schema; |
| 5321 | do { |
| 5322 | if (schema->when && !(node->flags & LYD_WHEN_TRUE)) { |
| 5323 | return LY_EINCOMPLETE; |
| 5324 | } |
| 5325 | schema = schema->parent; |
| 5326 | } while (schema && (schema->nodetype & (LYS_CASE | LYS_CHOICE))); |
| 5327 | |
| 5328 | return LY_SUCCESS; |
| 5329 | } |
| 5330 | |
| 5331 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5332 | * @brief Check @p node as a part of NameTest processing. |
| 5333 | * |
| 5334 | * @param[in] node Node to check. |
| 5335 | * @param[in] root_type XPath root node type. |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5336 | * @param[in] context_op XPath operation parent. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5337 | * @param[in] node_name Node name in the dictionary to move to, NULL for any node. |
| 5338 | * @param[in] moveto_mod Expected module of the node, NULL for any. |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5339 | * @return LY_ERR (LY_ENOT if node does not match, LY_EINCOMPLETE on unresolved when, |
| 5340 | * LY_EINVAL if netither node nor any children match) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5341 | */ |
| 5342 | static LY_ERR |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5343 | moveto_node_check(const struct lyd_node *node, enum lyxp_node_type root_type, const struct lysc_node *context_op, |
| 5344 | const char *node_name, const struct lys_module *moveto_mod) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5345 | { |
| 5346 | /* module check */ |
| 5347 | if (moveto_mod && (node->schema->module != moveto_mod)) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5348 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5349 | } |
| 5350 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5351 | /* context check */ |
| 5352 | 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] | 5353 | return LY_EINVAL; |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5354 | } else if (context_op && (node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node->schema != context_op)) { |
| 5355 | return LY_EINVAL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5356 | } |
| 5357 | |
| 5358 | /* name check */ |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5359 | if (node_name && strcmp(node_name, "*") && (node->schema->name != node_name)) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5360 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5361 | } |
| 5362 | |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 5363 | /* when check */ |
| 5364 | if (moveto_when_check(node)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5365 | return LY_EINCOMPLETE; |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 5366 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5367 | |
| 5368 | /* match */ |
| 5369 | return LY_SUCCESS; |
| 5370 | } |
| 5371 | |
| 5372 | /** |
| 5373 | * @brief Check @p node as a part of schema NameTest processing. |
| 5374 | * |
| 5375 | * @param[in] node Schema node to check. |
| 5376 | * @param[in] root_type XPath root node type. |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5377 | * @param[in] context_op XPath operation parent. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5378 | * @param[in] node_name Node name in the dictionary to move to, NULL for any nodes. |
| 5379 | * @param[in] moveto_mod Expected module of the node, NULL for any. |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5380 | * @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] | 5381 | */ |
| 5382 | static LY_ERR |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5383 | moveto_scnode_check(const struct lysc_node *node, enum lyxp_node_type root_type, const struct lysc_node *context_op, |
| 5384 | const char *node_name, const struct lys_module *moveto_mod) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5385 | { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5386 | /* module check */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5387 | if (moveto_mod && (node->module != moveto_mod)) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5388 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5389 | } |
| 5390 | |
| 5391 | /* context check */ |
| 5392 | if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) { |
| 5393 | return LY_EINVAL; |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5394 | } else if (context_op && (node->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node != context_op)) { |
| 5395 | return LY_EINVAL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5396 | } |
| 5397 | |
| 5398 | /* name check */ |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5399 | if (node_name && strcmp(node_name, "*") && (node->name != node_name)) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5400 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5401 | } |
| 5402 | |
| 5403 | /* match */ |
| 5404 | return LY_SUCCESS; |
| 5405 | } |
| 5406 | |
| 5407 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5408 | * @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] | 5409 | * |
| 5410 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5411 | * @param[in] mod Matching node module, NULL for any. |
| 5412 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5413 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 5414 | */ |
| 5415 | static LY_ERR |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5416 | 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] | 5417 | { |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 5418 | uint32_t i; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5419 | int replaced; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5420 | const struct lyd_node *siblings, *sub; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5421 | LY_ERR rc; |
| 5422 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5423 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5424 | return LY_SUCCESS; |
| 5425 | } |
| 5426 | |
| 5427 | if (set->type != LYXP_SET_NODE_SET) { |
| 5428 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 5429 | return LY_EVALID; |
| 5430 | } |
| 5431 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5432 | for (i = 0; i < set->used; ) { |
| 5433 | replaced = 0; |
| 5434 | |
| 5435 | 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] | 5436 | assert(!set->val.nodes[i].node); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5437 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5438 | /* search in all the trees */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5439 | siblings = set->tree; |
Michal Vasko | 5bfd4be | 2020-06-23 13:26:19 +0200 | [diff] [blame] | 5440 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5441 | /* search in children */ |
Michal Vasko | 5bfd4be | 2020-06-23 13:26:19 +0200 | [diff] [blame] | 5442 | siblings = lyd_node_children(set->val.nodes[i].node, 0); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5443 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5444 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5445 | for (sub = siblings; sub; sub = sub->next) { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5446 | 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] | 5447 | if (rc == LY_SUCCESS) { |
| 5448 | if (!replaced) { |
| 5449 | set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i); |
| 5450 | replaced = 1; |
| 5451 | } else { |
| 5452 | set_insert_node(set, sub, 0, LYXP_NODE_ELEM, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5453 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5454 | ++i; |
| 5455 | } else if (rc == LY_EINCOMPLETE) { |
| 5456 | return rc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5457 | } |
| 5458 | } |
| 5459 | |
| 5460 | if (!replaced) { |
| 5461 | /* no match */ |
| 5462 | set_remove_node(set, i); |
| 5463 | } |
| 5464 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5465 | |
| 5466 | return LY_SUCCESS; |
| 5467 | } |
| 5468 | |
| 5469 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5470 | * @brief Move context @p set to a node using hashes. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY). |
| 5471 | * Context position aware. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5472 | * |
| 5473 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5474 | * @param[in] scnode Matching node schema. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5475 | * @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] | 5476 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 5477 | */ |
| 5478 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5479 | 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] | 5480 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5481 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5482 | uint32_t i; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5483 | const struct lyd_node *siblings; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5484 | struct lyd_node *sub, *inst = NULL; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5485 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5486 | assert(scnode && (!(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) || predicates)); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5487 | |
| 5488 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5489 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5490 | } |
| 5491 | |
| 5492 | if (set->type != LYXP_SET_NODE_SET) { |
| 5493 | 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] | 5494 | ret = LY_EVALID; |
| 5495 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5496 | } |
| 5497 | |
| 5498 | /* context check for all the nodes since we have the schema node */ |
| 5499 | if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) { |
| 5500 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5501 | goto cleanup; |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5502 | } else if (set->context_op && (scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) |
| 5503 | && (scnode != set->context_op)) { |
| 5504 | lyxp_set_free_content(set); |
| 5505 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5506 | } |
| 5507 | |
| 5508 | /* create specific data instance if needed */ |
| 5509 | if (scnode->nodetype == LYS_LIST) { |
| 5510 | LY_CHECK_GOTO(ret = lyd_create_list(scnode, predicates, &inst), cleanup); |
| 5511 | } else if (scnode->nodetype == LYS_LEAFLIST) { |
| 5512 | 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] | 5513 | } |
| 5514 | |
| 5515 | for (i = 0; i < set->used; ) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5516 | siblings = NULL; |
| 5517 | |
| 5518 | if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) { |
| 5519 | assert(!set->val.nodes[i].node); |
| 5520 | |
| 5521 | /* search in all the trees */ |
| 5522 | siblings = set->tree; |
| 5523 | } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
| 5524 | /* search in children */ |
Michal Vasko | 5bfd4be | 2020-06-23 13:26:19 +0200 | [diff] [blame] | 5525 | siblings = lyd_node_children(set->val.nodes[i].node, 0); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5526 | } |
| 5527 | |
| 5528 | /* find the node using hashes */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5529 | if (inst) { |
| 5530 | lyd_find_sibling_first(siblings, inst, &sub); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5531 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5532 | lyd_find_sibling_val(siblings, scnode, NULL, 0, &sub); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5533 | } |
| 5534 | |
| 5535 | /* when check */ |
| 5536 | if (sub && moveto_when_check(sub)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5537 | ret = LY_EINCOMPLETE; |
| 5538 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5539 | } |
| 5540 | |
| 5541 | if (sub) { |
| 5542 | /* pos filled later */ |
Michal Vasko | cb1b7c0 | 2020-07-03 13:38:12 +0200 | [diff] [blame] | 5543 | set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5544 | ++i; |
Michal Vasko | cb1b7c0 | 2020-07-03 13:38:12 +0200 | [diff] [blame] | 5545 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5546 | /* no match */ |
| 5547 | set_remove_node(set, i); |
| 5548 | } |
| 5549 | } |
| 5550 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5551 | cleanup: |
| 5552 | lyd_free_tree(inst); |
| 5553 | return ret; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5554 | } |
| 5555 | |
| 5556 | /** |
| 5557 | * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY). |
| 5558 | * |
| 5559 | * @param[in,out] set Set to use. |
| 5560 | * @param[in] mod Matching node module, NULL for any. |
| 5561 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5562 | * @param[in] options XPath options. |
| 5563 | * @return LY_ERR |
| 5564 | */ |
| 5565 | static LY_ERR |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5566 | 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] | 5567 | { |
Michal Vasko | cafad9d | 2019-11-07 15:20:03 +0100 | [diff] [blame] | 5568 | int i, orig_used, idx, temp_ctx = 0, getnext_opts; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5569 | uint32_t mod_idx; |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 5570 | const struct lysc_node *iter, *start_parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5571 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5572 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5573 | return LY_SUCCESS; |
| 5574 | } |
| 5575 | |
| 5576 | if (set->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 5577 | 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] | 5578 | return LY_EVALID; |
| 5579 | } |
| 5580 | |
Michal Vasko | cafad9d | 2019-11-07 15:20:03 +0100 | [diff] [blame] | 5581 | /* getnext opts */ |
| 5582 | getnext_opts = LYS_GETNEXT_NOSTATECHECK; |
| 5583 | if (options & LYXP_SCNODE_OUTPUT) { |
| 5584 | getnext_opts |= LYS_GETNEXT_OUTPUT; |
| 5585 | } |
| 5586 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5587 | orig_used = set->used; |
| 5588 | for (i = 0; i < orig_used; ++i) { |
| 5589 | if (set->val.scnodes[i].in_ctx != 1) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5590 | if (set->val.scnodes[i].in_ctx != -2) { |
| 5591 | continue; |
| 5592 | } |
| 5593 | |
| 5594 | /* remember context node */ |
| 5595 | set->val.scnodes[i].in_ctx = -1; |
Michal Vasko | ec4df48 | 2019-12-16 10:02:18 +0100 | [diff] [blame] | 5596 | } else { |
| 5597 | set->val.scnodes[i].in_ctx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5598 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5599 | |
| 5600 | start_parent = set->val.scnodes[i].scnode; |
| 5601 | |
| 5602 | 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] | 5603 | /* 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] | 5604 | * so use it directly (root node itself is useless in this case) */ |
| 5605 | mod_idx = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5606 | 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] | 5607 | iter = NULL; |
Michal Vasko | 509de4d | 2019-12-10 14:51:30 +0100 | [diff] [blame] | 5608 | /* module may not be implemented */ |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 5609 | while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5610 | 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] | 5611 | idx = lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5612 | /* we need to prevent these nodes from being considered in this moveto */ |
| 5613 | if ((idx < orig_used) && (idx > i)) { |
| 5614 | set->val.scnodes[idx].in_ctx = 2; |
| 5615 | temp_ctx = 1; |
| 5616 | } |
| 5617 | } |
| 5618 | } |
| 5619 | |
| 5620 | if (!mod_idx) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5621 | /* mod was specified, we are not going through the whole context */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5622 | break; |
| 5623 | } |
| 5624 | /* next iteration */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5625 | mod = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5626 | } |
| 5627 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 5628 | } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) { |
| 5629 | iter = NULL; |
| 5630 | while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5631 | if (!moveto_scnode_check(iter, set->root_type, set->context_op, ncname, (mod ? mod : set->local_mod))) { |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 5632 | idx = lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5633 | if ((idx < orig_used) && (idx > i)) { |
| 5634 | set->val.scnodes[idx].in_ctx = 2; |
| 5635 | temp_ctx = 1; |
| 5636 | } |
| 5637 | } |
| 5638 | } |
| 5639 | } |
| 5640 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5641 | |
| 5642 | /* correct temporary in_ctx values */ |
| 5643 | if (temp_ctx) { |
| 5644 | for (i = 0; i < orig_used; ++i) { |
| 5645 | if (set->val.scnodes[i].in_ctx == 2) { |
| 5646 | set->val.scnodes[i].in_ctx = 1; |
| 5647 | } |
| 5648 | } |
| 5649 | } |
| 5650 | |
| 5651 | return LY_SUCCESS; |
| 5652 | } |
| 5653 | |
| 5654 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5655 | * @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] | 5656 | * Context position aware. |
| 5657 | * |
| 5658 | * @param[in] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5659 | * @param[in] mod Matching node module, NULL for any. |
| 5660 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5661 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 5662 | */ |
| 5663 | static LY_ERR |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5664 | 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] | 5665 | { |
| 5666 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5667 | const struct lyd_node *next, *elem, *start; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5668 | struct lyxp_set ret_set; |
| 5669 | LY_ERR rc; |
| 5670 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5671 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5672 | return LY_SUCCESS; |
| 5673 | } |
| 5674 | |
| 5675 | if (set->type != LYXP_SET_NODE_SET) { |
| 5676 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 5677 | return LY_EVALID; |
| 5678 | } |
| 5679 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5680 | /* 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] | 5681 | rc = moveto_node(set, NULL, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5682 | LY_CHECK_RET(rc); |
| 5683 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5684 | /* 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] | 5685 | set_init(&ret_set, set); |
| 5686 | for (i = 0; i < set->used; ++i) { |
| 5687 | |
| 5688 | /* TREE DFS */ |
| 5689 | start = set->val.nodes[i].node; |
| 5690 | for (elem = next = start; elem; elem = next) { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5691 | 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] | 5692 | if (!rc) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5693 | /* add matching node into result set */ |
| 5694 | set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used); |
| 5695 | if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) { |
| 5696 | /* the node is a duplicate, we'll process it later in the set */ |
| 5697 | goto skip_children; |
| 5698 | } |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5699 | } else if (rc == LY_EINCOMPLETE) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5700 | return rc; |
| 5701 | } else if (rc == LY_EINVAL) { |
| 5702 | goto skip_children; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5703 | } |
| 5704 | |
| 5705 | /* TREE DFS NEXT ELEM */ |
| 5706 | /* select element for the next run - children first */ |
Michal Vasko | 5bfd4be | 2020-06-23 13:26:19 +0200 | [diff] [blame] | 5707 | next = lyd_node_children(elem, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5708 | if (!next) { |
| 5709 | skip_children: |
| 5710 | /* no children, so try siblings, but only if it's not the start, |
| 5711 | * that is considered to be the root and it's siblings are not traversed */ |
| 5712 | if (elem != start) { |
| 5713 | next = elem->next; |
| 5714 | } else { |
| 5715 | break; |
| 5716 | } |
| 5717 | } |
| 5718 | while (!next) { |
| 5719 | /* no siblings, go back through the parents */ |
| 5720 | if ((struct lyd_node *)elem->parent == start) { |
| 5721 | /* we are done, no next element to process */ |
| 5722 | break; |
| 5723 | } |
| 5724 | /* parent is already processed, go to its sibling */ |
| 5725 | elem = (struct lyd_node *)elem->parent; |
| 5726 | next = elem->next; |
| 5727 | } |
| 5728 | } |
| 5729 | } |
| 5730 | |
| 5731 | /* make the temporary set the current one */ |
| 5732 | ret_set.ctx_pos = set->ctx_pos; |
| 5733 | ret_set.ctx_size = set->ctx_size; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5734 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5735 | memcpy(set, &ret_set, sizeof *set); |
| 5736 | |
| 5737 | return LY_SUCCESS; |
| 5738 | } |
| 5739 | |
| 5740 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5741 | * @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] | 5742 | * |
| 5743 | * @param[in] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5744 | * @param[in] mod Matching node module, NULL for any. |
| 5745 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5746 | * @param[in] options XPath options. |
| 5747 | * @return LY_ERR |
| 5748 | */ |
| 5749 | static LY_ERR |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5750 | 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] | 5751 | { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5752 | int i, orig_used, idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5753 | const struct lysc_node *next, *elem, *start; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5754 | LY_ERR rc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5755 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5756 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5757 | return LY_SUCCESS; |
| 5758 | } |
| 5759 | |
| 5760 | if (set->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 5761 | 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] | 5762 | return LY_EVALID; |
| 5763 | } |
| 5764 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5765 | orig_used = set->used; |
| 5766 | for (i = 0; i < orig_used; ++i) { |
| 5767 | if (set->val.scnodes[i].in_ctx != 1) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5768 | if (set->val.scnodes[i].in_ctx != -2) { |
| 5769 | continue; |
| 5770 | } |
| 5771 | |
| 5772 | /* remember context node */ |
| 5773 | set->val.scnodes[i].in_ctx = -1; |
Michal Vasko | ec4df48 | 2019-12-16 10:02:18 +0100 | [diff] [blame] | 5774 | } else { |
| 5775 | set->val.scnodes[i].in_ctx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5776 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5777 | |
| 5778 | /* TREE DFS */ |
| 5779 | start = set->val.scnodes[i].scnode; |
| 5780 | for (elem = next = start; elem; elem = next) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5781 | if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) { |
| 5782 | /* schema-only nodes, skip root */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5783 | goto next_iter; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5784 | } |
| 5785 | |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5786 | 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] | 5787 | if (!rc) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 5788 | 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] | 5789 | set->val.scnodes[idx].in_ctx = 1; |
| 5790 | if (idx > i) { |
| 5791 | /* we will process it later in the set */ |
| 5792 | goto skip_children; |
| 5793 | } |
| 5794 | } else { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 5795 | lyxp_set_scnode_insert_node(set, elem, LYXP_NODE_ELEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5796 | } |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5797 | } else if (rc == LY_EINVAL) { |
| 5798 | goto skip_children; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5799 | } |
| 5800 | |
| 5801 | next_iter: |
| 5802 | /* TREE DFS NEXT ELEM */ |
| 5803 | /* select element for the next run - children first */ |
| 5804 | 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] | 5805 | if (!next) { |
| 5806 | skip_children: |
| 5807 | /* no children, so try siblings, but only if it's not the start, |
| 5808 | * that is considered to be the root and it's siblings are not traversed */ |
| 5809 | if (elem != start) { |
| 5810 | next = elem->next; |
| 5811 | } else { |
| 5812 | break; |
| 5813 | } |
| 5814 | } |
| 5815 | while (!next) { |
| 5816 | /* no siblings, go back through the parents */ |
| 5817 | if (elem->parent == start) { |
| 5818 | /* we are done, no next element to process */ |
| 5819 | break; |
| 5820 | } |
| 5821 | /* parent is already processed, go to its sibling */ |
| 5822 | elem = elem->parent; |
| 5823 | next = elem->next; |
| 5824 | } |
| 5825 | } |
| 5826 | } |
| 5827 | |
| 5828 | return LY_SUCCESS; |
| 5829 | } |
| 5830 | |
| 5831 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5832 | * @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] | 5833 | * Indirectly context position aware. |
| 5834 | * |
| 5835 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5836 | * @param[in] mod Matching metadata module, NULL for any. |
| 5837 | * @param[in] ncname Matching metadata name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5838 | * @return LY_ERR |
| 5839 | */ |
| 5840 | static LY_ERR |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5841 | 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] | 5842 | { |
| 5843 | uint32_t i; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5844 | int replaced; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5845 | struct lyd_meta *sub; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5846 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5847 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5848 | return LY_SUCCESS; |
| 5849 | } |
| 5850 | |
| 5851 | if (set->type != LYXP_SET_NODE_SET) { |
| 5852 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 5853 | return LY_EVALID; |
| 5854 | } |
| 5855 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5856 | for (i = 0; i < set->used; ) { |
| 5857 | replaced = 0; |
| 5858 | |
| 5859 | /* only attributes of an elem (not dummy) can be in the result, skip all the rest; |
| 5860 | * our attributes are always qualified */ |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5861 | if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5862 | for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5863 | |
| 5864 | /* check "namespace" */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5865 | if (mod && (sub->annotation->module != mod)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5866 | continue; |
| 5867 | } |
| 5868 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5869 | if (!ncname || (sub->name == ncname)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5870 | /* match */ |
| 5871 | if (!replaced) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5872 | set->val.meta[i].meta = sub; |
| 5873 | set->val.meta[i].type = LYXP_NODE_META; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5874 | /* pos does not change */ |
| 5875 | replaced = 1; |
| 5876 | } else { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5877 | set_insert_node(set, (struct lyd_node *)sub, set->val.nodes[i].pos, LYXP_NODE_META, i + 1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5878 | } |
| 5879 | ++i; |
| 5880 | } |
| 5881 | } |
| 5882 | } |
| 5883 | |
| 5884 | if (!replaced) { |
| 5885 | /* no match */ |
| 5886 | set_remove_node(set, i); |
| 5887 | } |
| 5888 | } |
| 5889 | |
| 5890 | return LY_SUCCESS; |
| 5891 | } |
| 5892 | |
| 5893 | /** |
| 5894 | * @brief Move context @p set1 to union with @p set2. @p set2 is emptied afterwards. |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5895 | * Result is LYXP_SET_NODE_SET. Context position aware. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5896 | * |
| 5897 | * @param[in,out] set1 Set to use for the result. |
| 5898 | * @param[in] set2 Set that is copied to @p set1. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5899 | * @return LY_ERR |
| 5900 | */ |
| 5901 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5902 | moveto_union(struct lyxp_set *set1, struct lyxp_set *set2) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5903 | { |
| 5904 | LY_ERR rc; |
| 5905 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5906 | if ((set1->type != LYXP_SET_NODE_SET) || (set2->type != LYXP_SET_NODE_SET)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5907 | LOGVAL(set1->ctx, LY_VLOG_LYD, set1->ctx_node, LY_VCODE_XP_INOP_2, "union", print_set_type(set1), print_set_type(set2)); |
| 5908 | return LY_EVALID; |
| 5909 | } |
| 5910 | |
| 5911 | /* set2 is empty or both set1 and set2 */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5912 | if (!set2->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5913 | return LY_SUCCESS; |
| 5914 | } |
| 5915 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5916 | if (!set1->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5917 | memcpy(set1, set2, sizeof *set1); |
| 5918 | /* dynamic memory belongs to set1 now, do not free */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5919 | memset(set2, 0, sizeof *set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5920 | return LY_SUCCESS; |
| 5921 | } |
| 5922 | |
| 5923 | /* we assume sets are sorted */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5924 | assert(!set_sort(set1) && !set_sort(set2)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5925 | |
| 5926 | /* sort, remove duplicates */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5927 | rc = set_sorted_merge(set1, set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5928 | LY_CHECK_RET(rc); |
| 5929 | |
| 5930 | /* final set must be sorted */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5931 | assert(!set_sort(set1)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5932 | |
| 5933 | return LY_SUCCESS; |
| 5934 | } |
| 5935 | |
| 5936 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5937 | * @brief Move context @p set to an attribute in any of the descendants. Result is LYXP_SET_NODE_SET. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5938 | * Context position aware. |
| 5939 | * |
| 5940 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5941 | * @param[in] mod Matching metadata module, NULL for any. |
| 5942 | * @param[in] ncname Matching metadata name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5943 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 5944 | */ |
| 5945 | static int |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5946 | moveto_attr_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5947 | { |
| 5948 | uint32_t i; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5949 | int replaced; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5950 | struct lyd_meta *sub; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5951 | struct lyxp_set *set_all_desc = NULL; |
| 5952 | LY_ERR rc; |
| 5953 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5954 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5955 | return LY_SUCCESS; |
| 5956 | } |
| 5957 | |
| 5958 | if (set->type != LYXP_SET_NODE_SET) { |
| 5959 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 5960 | return LY_EVALID; |
| 5961 | } |
| 5962 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5963 | /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory, |
| 5964 | * but it likely won't be used much, so it's a waste of time */ |
| 5965 | /* copy the context */ |
| 5966 | set_all_desc = set_copy(set); |
| 5967 | /* get all descendant nodes (the original context nodes are removed) */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5968 | rc = moveto_node_alldesc(set_all_desc, NULL, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5969 | if (rc != LY_SUCCESS) { |
| 5970 | lyxp_set_free(set_all_desc); |
| 5971 | return rc; |
| 5972 | } |
| 5973 | /* prepend the original context nodes */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5974 | rc = moveto_union(set, set_all_desc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5975 | if (rc != LY_SUCCESS) { |
| 5976 | lyxp_set_free(set_all_desc); |
| 5977 | return rc; |
| 5978 | } |
| 5979 | lyxp_set_free(set_all_desc); |
| 5980 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5981 | for (i = 0; i < set->used; ) { |
| 5982 | replaced = 0; |
| 5983 | |
| 5984 | /* only attributes of an elem can be in the result, skip all the rest, |
| 5985 | * we have all attributes qualified in lyd tree */ |
| 5986 | if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5987 | for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5988 | /* check "namespace" */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5989 | if (mod && (sub->annotation->module != mod)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5990 | continue; |
| 5991 | } |
| 5992 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5993 | if (!ncname || (sub->name == ncname)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5994 | /* match */ |
| 5995 | if (!replaced) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5996 | set->val.meta[i].meta = sub; |
| 5997 | set->val.meta[i].type = LYXP_NODE_META; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5998 | /* pos does not change */ |
| 5999 | replaced = 1; |
| 6000 | } else { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6001 | 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] | 6002 | } |
| 6003 | ++i; |
| 6004 | } |
| 6005 | } |
| 6006 | } |
| 6007 | |
| 6008 | if (!replaced) { |
| 6009 | /* no match */ |
| 6010 | set_remove_node(set, i); |
| 6011 | } |
| 6012 | } |
| 6013 | |
| 6014 | return LY_SUCCESS; |
| 6015 | } |
| 6016 | |
| 6017 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6018 | * @brief Move context @p set to self and al chilren, recursively. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET. |
| 6019 | * Context position aware. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6020 | * |
| 6021 | * @param[in] parent Current parent. |
| 6022 | * @param[in] parent_pos Position of @p parent. |
| 6023 | * @param[in] parent_type Node type of @p parent. |
| 6024 | * @param[in,out] to_set Set to use. |
| 6025 | * @param[in] dup_check_set Set for checking duplicities. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6026 | * @param[in] options XPath options. |
| 6027 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6028 | */ |
| 6029 | static LY_ERR |
| 6030 | 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] | 6031 | 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] | 6032 | { |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6033 | const struct lyd_node *iter, *first; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6034 | LY_ERR rc; |
| 6035 | |
| 6036 | switch (parent_type) { |
| 6037 | case LYXP_NODE_ROOT: |
| 6038 | case LYXP_NODE_ROOT_CONFIG: |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6039 | assert(!parent); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6040 | |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6041 | /* add all top-level nodes as elements */ |
| 6042 | first = to_set->tree; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6043 | break; |
| 6044 | case LYXP_NODE_ELEM: |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6045 | /* add just the text node of this term element node */ |
| 6046 | if (parent->schema->nodetype & (LYD_NODE_TERM | LYD_NODE_ANY)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6047 | if (!set_dup_node_check(dup_check_set, parent, LYXP_NODE_TEXT, -1)) { |
| 6048 | set_insert_node(to_set, parent, parent_pos, LYXP_NODE_TEXT, to_set->used); |
| 6049 | } |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6050 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6051 | } |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6052 | |
| 6053 | /* add all the children of this node */ |
Michal Vasko | 5bfd4be | 2020-06-23 13:26:19 +0200 | [diff] [blame] | 6054 | first = lyd_node_children(parent, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6055 | break; |
| 6056 | default: |
| 6057 | LOGINT_RET(parent->schema->module->ctx); |
| 6058 | } |
| 6059 | |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6060 | /* add all top-level nodes as elements */ |
| 6061 | LY_LIST_FOR(first, iter) { |
| 6062 | /* context check */ |
| 6063 | if ((parent_type == LYXP_NODE_ROOT_CONFIG) && (iter->schema->flags & LYS_CONFIG_R)) { |
| 6064 | continue; |
| 6065 | } |
| 6066 | |
| 6067 | /* when check */ |
| 6068 | if (moveto_when_check(iter)) { |
| 6069 | return LY_EINCOMPLETE; |
| 6070 | } |
| 6071 | |
| 6072 | if (!set_dup_node_check(dup_check_set, iter, LYXP_NODE_ELEM, -1)) { |
| 6073 | set_insert_node(to_set, iter, 0, LYXP_NODE_ELEM, to_set->used); |
| 6074 | |
| 6075 | /* also add all the children of this node, recursively */ |
| 6076 | rc = moveto_self_add_children_r(iter, 0, LYXP_NODE_ELEM, to_set, dup_check_set, options); |
| 6077 | LY_CHECK_RET(rc); |
| 6078 | } |
| 6079 | } |
| 6080 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6081 | return LY_SUCCESS; |
| 6082 | } |
| 6083 | |
| 6084 | /** |
| 6085 | * @brief Move context @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET |
| 6086 | * (or LYXP_SET_EMPTY). Context position aware. |
| 6087 | * |
| 6088 | * @param[in,out] set Set to use. |
| 6089 | * @param[in] all_desc Whether to go to all descendants ('//') or not ('/'). |
| 6090 | * @param[in] options XPath options. |
| 6091 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6092 | */ |
| 6093 | static LY_ERR |
| 6094 | moveto_self(struct lyxp_set *set, int all_desc, int options) |
| 6095 | { |
| 6096 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6097 | struct lyxp_set ret_set; |
| 6098 | LY_ERR rc; |
| 6099 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6100 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6101 | return LY_SUCCESS; |
| 6102 | } |
| 6103 | |
| 6104 | if (set->type != LYXP_SET_NODE_SET) { |
| 6105 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 6106 | return LY_EVALID; |
| 6107 | } |
| 6108 | |
| 6109 | /* nothing to do */ |
| 6110 | if (!all_desc) { |
| 6111 | return LY_SUCCESS; |
| 6112 | } |
| 6113 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6114 | /* add all the children, they get added recursively */ |
| 6115 | set_init(&ret_set, set); |
| 6116 | for (i = 0; i < set->used; ++i) { |
| 6117 | /* copy the current node to tmp */ |
| 6118 | set_insert_node(&ret_set, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, ret_set.used); |
| 6119 | |
| 6120 | /* do not touch attributes and text nodes */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6121 | 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] | 6122 | continue; |
| 6123 | } |
| 6124 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6125 | /* add all the children */ |
| 6126 | 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] | 6127 | set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6128 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6129 | lyxp_set_free_content(&ret_set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6130 | return rc; |
| 6131 | } |
| 6132 | } |
| 6133 | |
| 6134 | /* use the temporary set as the current one */ |
| 6135 | ret_set.ctx_pos = set->ctx_pos; |
| 6136 | ret_set.ctx_size = set->ctx_size; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6137 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6138 | memcpy(set, &ret_set, sizeof *set); |
| 6139 | |
| 6140 | return LY_SUCCESS; |
| 6141 | } |
| 6142 | |
| 6143 | /** |
| 6144 | * @brief Move context schema @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_SCNODE_SET |
| 6145 | * (or LYXP_SET_EMPTY). |
| 6146 | * |
| 6147 | * @param[in,out] set Set to use. |
| 6148 | * @param[in] all_desc Whether to go to all descendants ('//') or not ('/'). |
| 6149 | * @param[in] options XPath options. |
| 6150 | * @return LY_ERR |
| 6151 | */ |
| 6152 | static LY_ERR |
| 6153 | moveto_scnode_self(struct lyxp_set *set, int all_desc, int options) |
| 6154 | { |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6155 | int getnext_opts; |
| 6156 | uint32_t i, mod_idx; |
| 6157 | const struct lysc_node *iter, *start_parent; |
| 6158 | const struct lys_module *mod; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6159 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6160 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6161 | return LY_SUCCESS; |
| 6162 | } |
| 6163 | |
| 6164 | if (set->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 6165 | 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] | 6166 | return LY_EVALID; |
| 6167 | } |
| 6168 | |
| 6169 | /* nothing to do */ |
| 6170 | if (!all_desc) { |
| 6171 | return LY_SUCCESS; |
| 6172 | } |
| 6173 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6174 | /* getnext opts */ |
| 6175 | getnext_opts = LYS_GETNEXT_NOSTATECHECK; |
| 6176 | if (options & LYXP_SCNODE_OUTPUT) { |
| 6177 | getnext_opts |= LYS_GETNEXT_OUTPUT; |
| 6178 | } |
| 6179 | |
| 6180 | /* 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] | 6181 | for (i = 0; i < set->used; ++i) { |
| 6182 | if (set->val.scnodes[i].in_ctx != 1) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6183 | if (set->val.scnodes[i].in_ctx != -2) { |
| 6184 | continue; |
| 6185 | } |
| 6186 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6187 | /* remember context node */ |
| 6188 | set->val.scnodes[i].in_ctx = -1; |
| 6189 | } else { |
| 6190 | set->val.scnodes[i].in_ctx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6191 | } |
| 6192 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6193 | start_parent = set->val.scnodes[i].scnode; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6194 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6195 | if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) { |
| 6196 | /* it can actually be in any module, it's all <running> */ |
| 6197 | mod_idx = 0; |
| 6198 | while ((mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) { |
| 6199 | iter = NULL; |
| 6200 | /* module may not be implemented */ |
| 6201 | while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) { |
| 6202 | /* context check */ |
| 6203 | if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) { |
| 6204 | continue; |
| 6205 | } |
| 6206 | |
| 6207 | lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM); |
| 6208 | /* throw away the insert index, we want to consider that node again, recursively */ |
| 6209 | } |
| 6210 | } |
| 6211 | |
| 6212 | } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) { |
| 6213 | iter = NULL; |
| 6214 | while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6215 | /* context check */ |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6216 | 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] | 6217 | continue; |
| 6218 | } |
| 6219 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6220 | lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6221 | } |
| 6222 | } |
| 6223 | } |
| 6224 | |
| 6225 | return LY_SUCCESS; |
| 6226 | } |
| 6227 | |
| 6228 | /** |
| 6229 | * @brief Move context @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_NODE_SET |
| 6230 | * (or LYXP_SET_EMPTY). Context position aware. |
| 6231 | * |
| 6232 | * @param[in] set Set to use. |
| 6233 | * @param[in] all_desc Whether to go to all descendants ('//') or not ('/'). |
| 6234 | * @param[in] options XPath options. |
| 6235 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6236 | */ |
| 6237 | static LY_ERR |
| 6238 | moveto_parent(struct lyxp_set *set, int all_desc, int options) |
| 6239 | { |
| 6240 | LY_ERR rc; |
| 6241 | uint32_t i; |
| 6242 | struct lyd_node *node, *new_node; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6243 | enum lyxp_node_type new_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6244 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6245 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6246 | return LY_SUCCESS; |
| 6247 | } |
| 6248 | |
| 6249 | if (set->type != LYXP_SET_NODE_SET) { |
| 6250 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 6251 | return LY_EVALID; |
| 6252 | } |
| 6253 | |
| 6254 | if (all_desc) { |
| 6255 | /* <path>//.. == <path>//./.. */ |
| 6256 | rc = moveto_self(set, 1, options); |
| 6257 | LY_CHECK_RET(rc); |
| 6258 | } |
| 6259 | |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 6260 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6261 | node = set->val.nodes[i].node; |
| 6262 | |
| 6263 | if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
| 6264 | new_node = (struct lyd_node *)node->parent; |
| 6265 | } else if (set->val.nodes[i].type == LYXP_NODE_TEXT) { |
| 6266 | new_node = node; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6267 | } else if (set->val.nodes[i].type == LYXP_NODE_META) { |
| 6268 | new_node = set->val.meta[i].meta->parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6269 | if (!new_node) { |
| 6270 | LOGINT_RET(set->ctx); |
| 6271 | } |
| 6272 | } else { |
| 6273 | /* root does not have a parent */ |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6274 | set_remove_node_none(set, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6275 | continue; |
| 6276 | } |
| 6277 | |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 6278 | /* when check */ |
| 6279 | if (moveto_when_check(new_node)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6280 | return LY_EINCOMPLETE; |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 6281 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6282 | |
| 6283 | /* node already there can also be the root */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6284 | if (!new_node) { |
| 6285 | new_type = set->root_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6286 | |
| 6287 | /* node has a standard parent (it can equal the root, it's not the root yet since they are fake) */ |
| 6288 | } else { |
| 6289 | new_type = LYXP_NODE_ELEM; |
| 6290 | } |
| 6291 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6292 | if (set_dup_node_check(set, new_node, new_type, -1)) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6293 | set_remove_node_none(set, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6294 | } else { |
| 6295 | set_replace_node(set, new_node, 0, new_type, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6296 | } |
| 6297 | } |
| 6298 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6299 | set_remove_nodes_none(set); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6300 | assert(!set_sort(set) && !set_sorted_dup_node_clean(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6301 | |
| 6302 | return LY_SUCCESS; |
| 6303 | } |
| 6304 | |
| 6305 | /** |
| 6306 | * @brief Move context schema @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_SCNODE_SET |
| 6307 | * (or LYXP_SET_EMPTY). |
| 6308 | * |
| 6309 | * @param[in] set Set to use. |
| 6310 | * @param[in] all_desc Whether to go to all descendants ('//') or not ('/'). |
| 6311 | * @param[in] options XPath options. |
| 6312 | * @return LY_ERR |
| 6313 | */ |
| 6314 | static LY_ERR |
| 6315 | moveto_scnode_parent(struct lyxp_set *set, int all_desc, int options) |
| 6316 | { |
| 6317 | int idx, i, orig_used, temp_ctx = 0; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6318 | const struct lysc_node *node, *new_node; |
| 6319 | enum lyxp_node_type new_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6320 | LY_ERR rc; |
| 6321 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6322 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6323 | return LY_SUCCESS; |
| 6324 | } |
| 6325 | |
| 6326 | if (set->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 6327 | 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] | 6328 | return LY_EVALID; |
| 6329 | } |
| 6330 | |
| 6331 | if (all_desc) { |
| 6332 | /* <path>//.. == <path>//./.. */ |
| 6333 | rc = moveto_scnode_self(set, 1, options); |
| 6334 | LY_CHECK_RET(rc); |
| 6335 | } |
| 6336 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6337 | orig_used = set->used; |
| 6338 | for (i = 0; i < orig_used; ++i) { |
| 6339 | if (set->val.scnodes[i].in_ctx != 1) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6340 | if (set->val.scnodes[i].in_ctx != -2) { |
| 6341 | continue; |
| 6342 | } |
| 6343 | |
| 6344 | /* remember context node */ |
| 6345 | set->val.scnodes[i].in_ctx = -1; |
Michal Vasko | ec4df48 | 2019-12-16 10:02:18 +0100 | [diff] [blame] | 6346 | } else { |
| 6347 | set->val.scnodes[i].in_ctx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6348 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6349 | |
| 6350 | node = set->val.scnodes[i].scnode; |
| 6351 | |
| 6352 | if (set->val.scnodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6353 | new_node = lysc_data_parent(node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6354 | } else { |
| 6355 | /* root does not have a parent */ |
| 6356 | continue; |
| 6357 | } |
| 6358 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6359 | /* node has no parent */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6360 | if (!new_node) { |
| 6361 | new_type = set->root_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6362 | |
| 6363 | /* node has a standard parent (it can equal the root, it's not the root yet since they are fake) */ |
| 6364 | } else { |
| 6365 | new_type = LYXP_NODE_ELEM; |
| 6366 | } |
| 6367 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6368 | idx = lyxp_set_scnode_insert_node(set, new_node, new_type); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6369 | if ((idx < orig_used) && (idx > i)) { |
| 6370 | set->val.scnodes[idx].in_ctx = 2; |
| 6371 | temp_ctx = 1; |
| 6372 | } |
| 6373 | } |
| 6374 | |
| 6375 | if (temp_ctx) { |
| 6376 | for (i = 0; i < orig_used; ++i) { |
| 6377 | if (set->val.scnodes[i].in_ctx == 2) { |
| 6378 | set->val.scnodes[i].in_ctx = 1; |
| 6379 | } |
| 6380 | } |
| 6381 | } |
| 6382 | |
| 6383 | return LY_SUCCESS; |
| 6384 | } |
| 6385 | |
| 6386 | /** |
| 6387 | * @brief Move context @p set to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'. |
| 6388 | * Result is LYXP_SET_BOOLEAN. Indirectly context position aware. |
| 6389 | * |
| 6390 | * @param[in,out] set1 Set to use for the result. |
| 6391 | * @param[in] set2 Set acting as the second operand for @p op. |
| 6392 | * @param[in] op Comparison operator to process. |
| 6393 | * @param[in] options XPath options. |
| 6394 | * @return LY_ERR |
| 6395 | */ |
| 6396 | static LY_ERR |
| 6397 | moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op, int options) |
| 6398 | { |
| 6399 | /* |
| 6400 | * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING |
| 6401 | * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING) |
| 6402 | * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 6403 | * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 6404 | * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING |
| 6405 | * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6406 | * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 6407 | * |
| 6408 | * '=' or '!=' |
| 6409 | * BOOLEAN + BOOLEAN |
| 6410 | * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 6411 | * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 6412 | * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 6413 | * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 6414 | * NUMBER + NUMBER |
| 6415 | * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6416 | * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 6417 | * STRING + STRING |
| 6418 | * |
| 6419 | * '<=', '<', '>=', '>' |
| 6420 | * NUMBER + NUMBER |
| 6421 | * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 6422 | * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 6423 | * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 6424 | * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6425 | * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 6426 | * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 6427 | * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6428 | * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6429 | */ |
| 6430 | struct lyxp_set iter1, iter2; |
| 6431 | int result; |
| 6432 | int64_t i; |
| 6433 | LY_ERR rc; |
| 6434 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6435 | memset(&iter1, 0, sizeof iter1); |
| 6436 | memset(&iter2, 0, sizeof iter2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6437 | |
| 6438 | /* iterative evaluation with node-sets */ |
| 6439 | if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) { |
| 6440 | if (set1->type == LYXP_SET_NODE_SET) { |
| 6441 | for (i = 0; i < set1->used; ++i) { |
| 6442 | switch (set2->type) { |
| 6443 | case LYXP_SET_NUMBER: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6444 | rc = set_comp_cast(&iter1, set1, LYXP_SET_NUMBER, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6445 | break; |
| 6446 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6447 | rc = set_comp_cast(&iter1, set1, LYXP_SET_BOOLEAN, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6448 | break; |
| 6449 | default: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6450 | rc = set_comp_cast(&iter1, set1, LYXP_SET_STRING, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6451 | break; |
| 6452 | } |
| 6453 | LY_CHECK_RET(rc); |
| 6454 | |
| 6455 | rc = moveto_op_comp(&iter1, set2, op, options); |
| 6456 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6457 | lyxp_set_free_content(&iter1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6458 | return rc; |
| 6459 | } |
| 6460 | |
| 6461 | /* lazy evaluation until true */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6462 | if (iter1.val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6463 | set_fill_boolean(set1, 1); |
| 6464 | return LY_SUCCESS; |
| 6465 | } |
| 6466 | } |
| 6467 | } else { |
| 6468 | for (i = 0; i < set2->used; ++i) { |
| 6469 | switch (set1->type) { |
| 6470 | case LYXP_SET_NUMBER: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6471 | rc = set_comp_cast(&iter2, set2, LYXP_SET_NUMBER, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6472 | break; |
| 6473 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6474 | rc = set_comp_cast(&iter2, set2, LYXP_SET_BOOLEAN, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6475 | break; |
| 6476 | default: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6477 | rc = set_comp_cast(&iter2, set2, LYXP_SET_STRING, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6478 | break; |
| 6479 | } |
| 6480 | LY_CHECK_RET(rc); |
| 6481 | |
| 6482 | set_fill_set(&iter1, set1); |
| 6483 | |
| 6484 | rc = moveto_op_comp(&iter1, &iter2, op, options); |
| 6485 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6486 | lyxp_set_free_content(&iter1); |
| 6487 | lyxp_set_free_content(&iter2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6488 | return rc; |
| 6489 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6490 | lyxp_set_free_content(&iter2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6491 | |
| 6492 | /* lazy evaluation until true */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6493 | if (iter1.val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6494 | set_fill_boolean(set1, 1); |
| 6495 | return LY_SUCCESS; |
| 6496 | } |
| 6497 | } |
| 6498 | } |
| 6499 | |
| 6500 | /* false for all nodes */ |
| 6501 | set_fill_boolean(set1, 0); |
| 6502 | return LY_SUCCESS; |
| 6503 | } |
| 6504 | |
| 6505 | /* first convert properly */ |
| 6506 | if ((op[0] == '=') || (op[0] == '!')) { |
| 6507 | if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6508 | lyxp_set_cast(set1, LYXP_SET_BOOLEAN); |
| 6509 | lyxp_set_cast(set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6510 | } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6511 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6512 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6513 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6514 | LY_CHECK_RET(rc); |
| 6515 | } /* else we have 2 strings */ |
| 6516 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6517 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6518 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6519 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6520 | LY_CHECK_RET(rc); |
| 6521 | } |
| 6522 | |
| 6523 | assert(set1->type == set2->type); |
| 6524 | |
| 6525 | /* compute result */ |
| 6526 | if (op[0] == '=') { |
| 6527 | if (set1->type == LYXP_SET_BOOLEAN) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6528 | result = (set1->val.bln == set2->val.bln); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6529 | } else if (set1->type == LYXP_SET_NUMBER) { |
| 6530 | result = (set1->val.num == set2->val.num); |
| 6531 | } else { |
| 6532 | assert(set1->type == LYXP_SET_STRING); |
Michal Vasko | ac6c72f | 2019-11-14 16:09:34 +0100 | [diff] [blame] | 6533 | result = !strcmp(set1->val.str, set2->val.str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6534 | } |
| 6535 | } else if (op[0] == '!') { |
| 6536 | if (set1->type == LYXP_SET_BOOLEAN) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6537 | result = (set1->val.bln != set2->val.bln); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6538 | } else if (set1->type == LYXP_SET_NUMBER) { |
| 6539 | result = (set1->val.num != set2->val.num); |
| 6540 | } else { |
| 6541 | assert(set1->type == LYXP_SET_STRING); |
Michal Vasko | ac6c72f | 2019-11-14 16:09:34 +0100 | [diff] [blame] | 6542 | result = !strcmp(set1->val.str, set2->val.str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6543 | } |
| 6544 | } else { |
| 6545 | assert(set1->type == LYXP_SET_NUMBER); |
| 6546 | if (op[0] == '<') { |
| 6547 | if (op[1] == '=') { |
| 6548 | result = (set1->val.num <= set2->val.num); |
| 6549 | } else { |
| 6550 | result = (set1->val.num < set2->val.num); |
| 6551 | } |
| 6552 | } else { |
| 6553 | if (op[1] == '=') { |
| 6554 | result = (set1->val.num >= set2->val.num); |
| 6555 | } else { |
| 6556 | result = (set1->val.num > set2->val.num); |
| 6557 | } |
| 6558 | } |
| 6559 | } |
| 6560 | |
| 6561 | /* assign result */ |
| 6562 | if (result) { |
| 6563 | set_fill_boolean(set1, 1); |
| 6564 | } else { |
| 6565 | set_fill_boolean(set1, 0); |
| 6566 | } |
| 6567 | |
| 6568 | return LY_SUCCESS; |
| 6569 | } |
| 6570 | |
| 6571 | /** |
| 6572 | * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div', |
| 6573 | * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware. |
| 6574 | * |
| 6575 | * @param[in,out] set1 Set to use for the result. |
| 6576 | * @param[in] set2 Set acting as the second operand for @p op. |
| 6577 | * @param[in] op Operator to process. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6578 | * @return LY_ERR |
| 6579 | */ |
| 6580 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6581 | 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] | 6582 | { |
| 6583 | LY_ERR rc; |
| 6584 | |
| 6585 | /* unary '-' */ |
| 6586 | if (!set2 && (op[0] == '-')) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6587 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6588 | LY_CHECK_RET(rc); |
| 6589 | set1->val.num *= -1; |
| 6590 | lyxp_set_free(set2); |
| 6591 | return LY_SUCCESS; |
| 6592 | } |
| 6593 | |
| 6594 | assert(set1 && set2); |
| 6595 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6596 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6597 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6598 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6599 | LY_CHECK_RET(rc); |
| 6600 | |
| 6601 | switch (op[0]) { |
| 6602 | /* '+' */ |
| 6603 | case '+': |
| 6604 | set1->val.num += set2->val.num; |
| 6605 | break; |
| 6606 | |
| 6607 | /* '-' */ |
| 6608 | case '-': |
| 6609 | set1->val.num -= set2->val.num; |
| 6610 | break; |
| 6611 | |
| 6612 | /* '*' */ |
| 6613 | case '*': |
| 6614 | set1->val.num *= set2->val.num; |
| 6615 | break; |
| 6616 | |
| 6617 | /* 'div' */ |
| 6618 | case 'd': |
| 6619 | set1->val.num /= set2->val.num; |
| 6620 | break; |
| 6621 | |
| 6622 | /* 'mod' */ |
| 6623 | case 'm': |
| 6624 | set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num); |
| 6625 | break; |
| 6626 | |
| 6627 | default: |
| 6628 | LOGINT_RET(set1->ctx); |
| 6629 | } |
| 6630 | |
| 6631 | return LY_SUCCESS; |
| 6632 | } |
| 6633 | |
| 6634 | /* |
| 6635 | * eval functions |
| 6636 | * |
| 6637 | * They execute a parsed XPath expression on some data subtree. |
| 6638 | */ |
| 6639 | |
| 6640 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6641 | * @brief Evaluate Predicate. Logs directly on error. |
| 6642 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6643 | * [9] Predicate ::= '[' Expr ']' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6644 | * |
| 6645 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6646 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6647 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 6648 | * @param[in] options XPath options. |
| 6649 | * @param[in] parent_pos_pred Whether parent predicate was a positional one. |
| 6650 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6651 | */ |
| 6652 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6653 | 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] | 6654 | { |
| 6655 | LY_ERR rc; |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 6656 | uint16_t i, orig_exp; |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6657 | uint32_t orig_pos, orig_size; |
| 6658 | int32_t pred_in_ctx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6659 | struct lyxp_set set2; |
| 6660 | struct lyd_node *orig_parent; |
| 6661 | |
| 6662 | /* '[' */ |
| 6663 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6664 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 6665 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6666 | |
| 6667 | if (!set) { |
| 6668 | only_parse: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6669 | rc = eval_expr_select(exp, tok_idx, 0, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6670 | LY_CHECK_RET(rc); |
| 6671 | } else if (set->type == LYXP_SET_NODE_SET) { |
| 6672 | /* 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] | 6673 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6674 | |
| 6675 | /* empty set, nothing to evaluate */ |
| 6676 | if (!set->used) { |
| 6677 | goto only_parse; |
| 6678 | } |
| 6679 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6680 | orig_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6681 | orig_pos = 0; |
| 6682 | orig_size = set->used; |
| 6683 | orig_parent = NULL; |
Michal Vasko | 39dbf35 | 2020-05-21 10:08:59 +0200 | [diff] [blame] | 6684 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6685 | set_init(&set2, set); |
| 6686 | set_insert_node(&set2, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, 0); |
| 6687 | /* remember the node context position for position() and context size for last(), |
| 6688 | * predicates should always be evaluated with respect to the child axis (since we do |
| 6689 | * not support explicit axes) so we assign positions based on their parents */ |
| 6690 | if (parent_pos_pred && ((struct lyd_node *)set->val.nodes[i].node->parent != orig_parent)) { |
| 6691 | orig_parent = (struct lyd_node *)set->val.nodes[i].node->parent; |
| 6692 | orig_pos = 1; |
| 6693 | } else { |
| 6694 | ++orig_pos; |
| 6695 | } |
| 6696 | |
| 6697 | set2.ctx_pos = orig_pos; |
| 6698 | set2.ctx_size = orig_size; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6699 | *tok_idx = orig_exp; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6700 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6701 | rc = eval_expr_select(exp, tok_idx, 0, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6702 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6703 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6704 | return rc; |
| 6705 | } |
| 6706 | |
| 6707 | /* number is a position */ |
| 6708 | if (set2.type == LYXP_SET_NUMBER) { |
| 6709 | if ((long long)set2.val.num == orig_pos) { |
| 6710 | set2.val.num = 1; |
| 6711 | } else { |
| 6712 | set2.val.num = 0; |
| 6713 | } |
| 6714 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6715 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6716 | |
| 6717 | /* predicate satisfied or not? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6718 | if (!set2.val.bln) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6719 | set_remove_node_none(set, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6720 | } |
| 6721 | } |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6722 | set_remove_nodes_none(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6723 | |
| 6724 | } else if (set->type == LYXP_SET_SCNODE_SET) { |
| 6725 | for (i = 0; i < set->used; ++i) { |
| 6726 | if (set->val.scnodes[i].in_ctx == 1) { |
| 6727 | /* there is a currently-valid node */ |
| 6728 | break; |
| 6729 | } |
| 6730 | } |
| 6731 | /* empty set, nothing to evaluate */ |
| 6732 | if (i == set->used) { |
| 6733 | goto only_parse; |
| 6734 | } |
| 6735 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6736 | orig_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6737 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6738 | /* set special in_ctx to all the valid snodes */ |
| 6739 | pred_in_ctx = set_scnode_new_in_ctx(set); |
| 6740 | |
| 6741 | /* use the valid snodes one-by-one */ |
| 6742 | for (i = 0; i < set->used; ++i) { |
| 6743 | if (set->val.scnodes[i].in_ctx != pred_in_ctx) { |
| 6744 | continue; |
| 6745 | } |
| 6746 | set->val.scnodes[i].in_ctx = 1; |
| 6747 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6748 | *tok_idx = orig_exp; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6749 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6750 | rc = eval_expr_select(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6751 | LY_CHECK_RET(rc); |
| 6752 | |
| 6753 | set->val.scnodes[i].in_ctx = pred_in_ctx; |
| 6754 | } |
| 6755 | |
| 6756 | /* restore the state as it was before the predicate */ |
| 6757 | for (i = 0; i < set->used; ++i) { |
| 6758 | if (set->val.scnodes[i].in_ctx == 1) { |
| 6759 | set->val.scnodes[i].in_ctx = 0; |
| 6760 | } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) { |
| 6761 | set->val.scnodes[i].in_ctx = 1; |
| 6762 | } |
| 6763 | } |
| 6764 | |
| 6765 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6766 | set2.type = LYXP_SET_NODE_SET; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6767 | set_fill_set(&set2, set); |
| 6768 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6769 | rc = eval_expr_select(exp, tok_idx, 0, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6770 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6771 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6772 | return rc; |
| 6773 | } |
| 6774 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6775 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6776 | if (!set2.val.bln) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6777 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6778 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6779 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6780 | } |
| 6781 | |
| 6782 | /* ']' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6783 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6784 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6785 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 6786 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6787 | |
| 6788 | return LY_SUCCESS; |
| 6789 | } |
| 6790 | |
| 6791 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6792 | * @brief Evaluate Literal. Logs directly on error. |
| 6793 | * |
| 6794 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6795 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6796 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 6797 | */ |
| 6798 | static void |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6799 | 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] | 6800 | { |
| 6801 | if (set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6802 | if (exp->tok_len[*tok_idx] == 2) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6803 | set_fill_string(set, "", 0); |
| 6804 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6805 | 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] | 6806 | } |
| 6807 | } |
| 6808 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6809 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 6810 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6811 | } |
| 6812 | |
| 6813 | /** |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6814 | * @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] | 6815 | * |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6816 | * @param[in] exp Full parsed XPath expression. |
| 6817 | * @param[in,out] tok_idx Index in @p exp at the beginning of the predicate, is updated on success. |
| 6818 | * @param[in] scnode Found schema node as context for the predicate. |
| 6819 | * @param[in] format Format of any prefixes in key names/values. |
| 6820 | * @param[out] predicates Parsed predicates. |
| 6821 | * @param[out] pred_type Type of @p predicates. |
| 6822 | * @return LY_SUCCESS on success, |
| 6823 | * @return LY_ERR on any error. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6824 | */ |
| 6825 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6826 | eval_name_test_try_compile_predicates(struct lyxp_expr *exp, uint16_t *tok_idx, const struct lysc_node *scnode, |
| 6827 | LYD_FORMAT format, struct ly_path_predicate **predicates, enum ly_path_pred_type *pred_type) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6828 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6829 | LY_ERR ret = LY_SUCCESS; |
| 6830 | uint16_t key_count, e_idx, pred_idx = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6831 | const struct lysc_node *key; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6832 | size_t pred_len; |
| 6833 | int prev_lo; |
| 6834 | struct lyxp_expr *exp2 = NULL; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6835 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6836 | assert(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6837 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6838 | if (scnode->nodetype == LYS_LIST) { |
| 6839 | /* get key count */ |
| 6840 | if (scnode->flags & LYS_KEYLESS) { |
| 6841 | return LY_EINVAL; |
| 6842 | } |
| 6843 | for (key_count = 0, key = lysc_node_children(scnode, 0); key && (key->flags & LYS_KEY); key = key->next, ++key_count); |
| 6844 | assert(key_count); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6845 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6846 | /* learn where the predicates end */ |
| 6847 | e_idx = *tok_idx; |
| 6848 | while (key_count) { |
| 6849 | /* '[' */ |
| 6850 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) { |
| 6851 | return LY_EINVAL; |
| 6852 | } |
| 6853 | ++e_idx; |
| 6854 | |
| 6855 | /* ']' */ |
| 6856 | while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) { |
| 6857 | ++e_idx; |
| 6858 | } |
| 6859 | ++e_idx; |
| 6860 | |
| 6861 | /* another presumably key predicate parsed */ |
| 6862 | --key_count; |
| 6863 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6864 | } else { |
| 6865 | /* learn just where this single predicate ends */ |
| 6866 | e_idx = *tok_idx; |
| 6867 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6868 | /* '[' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6869 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) { |
| 6870 | return LY_EINVAL; |
| 6871 | } |
| 6872 | ++e_idx; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6873 | |
| 6874 | /* ']' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6875 | while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) { |
| 6876 | ++e_idx; |
| 6877 | } |
| 6878 | ++e_idx; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6879 | } |
| 6880 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6881 | /* get the joined length of all the keys predicates/of the single leaf-list predicate */ |
| 6882 | pred_len = (exp->tok_pos[e_idx - 1] + exp->tok_len[e_idx - 1]) - exp->tok_pos[*tok_idx]; |
| 6883 | |
| 6884 | /* turn logging off */ |
| 6885 | prev_lo = ly_log_options(0); |
| 6886 | |
| 6887 | /* parse the predicate(s) */ |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 6888 | LY_CHECK_GOTO(ret = ly_path_parse_predicate(scnode->module->ctx, scnode, exp->expr + exp->tok_pos[*tok_idx], pred_len, |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6889 | LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp2), cleanup); |
| 6890 | |
| 6891 | /* compile */ |
| 6892 | switch (format) { |
| 6893 | case LYD_SCHEMA: |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 6894 | ret = ly_path_compile_predicate(scnode->module->ctx, scnode, scnode->module, scnode, exp2, &pred_idx, |
| 6895 | lys_resolve_prefix, scnode->module, LYD_SCHEMA, predicates, pred_type); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6896 | break; |
| 6897 | case LYD_JSON: |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 6898 | ret = ly_path_compile_predicate(scnode->module->ctx, scnode, scnode->module, scnode, exp2, &pred_idx, |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 6899 | lydjson_resolve_prefix, NULL, LYD_JSON, predicates, pred_type); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6900 | break; |
| 6901 | case LYD_XML: |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 6902 | case LYD_LYB: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6903 | ret = LY_EINT; |
| 6904 | goto cleanup; |
| 6905 | } |
| 6906 | LY_CHECK_GOTO(ret, cleanup); |
| 6907 | |
| 6908 | /* success, the predicate must include all the needed information for hash-based search */ |
| 6909 | *tok_idx = e_idx; |
| 6910 | |
| 6911 | cleanup: |
| 6912 | ly_log_options(prev_lo); |
| 6913 | lyxp_expr_free(scnode->module->ctx, exp2); |
| 6914 | return ret; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6915 | } |
| 6916 | |
| 6917 | /** |
| 6918 | * @brief Evaluate NameTest and any following Predicates. Logs directly on error. |
| 6919 | * |
| 6920 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 6921 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 6922 | * [7] NameTest ::= '*' | NCName ':' '*' | QName |
| 6923 | * |
| 6924 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6925 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6926 | * @param[in] attr_axis Whether to search attributes or standard nodes. |
| 6927 | * @param[in] all_desc Whether to search all the descendants or children only. |
| 6928 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 6929 | * @param[in] options XPath options. |
| 6930 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6931 | */ |
| 6932 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6933 | 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] | 6934 | int options) |
| 6935 | { |
| 6936 | int i; |
| 6937 | char *path; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6938 | const char *ncname, *ncname_dict = NULL; |
| 6939 | uint16_t ncname_len; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6940 | const struct lys_module *moveto_mod; |
| 6941 | const struct lysc_node *scnode = NULL, *tmp; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6942 | struct ly_path_predicate *predicates = NULL; |
| 6943 | enum ly_path_pred_type pred_type = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6944 | LY_ERR rc = LY_SUCCESS; |
| 6945 | |
| 6946 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6947 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 6948 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6949 | |
| 6950 | if (!set) { |
| 6951 | goto moveto; |
| 6952 | } |
| 6953 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6954 | ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]]; |
| 6955 | ncname_len = exp->tok_len[*tok_idx - 1]; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6956 | |
| 6957 | /* parse (and skip) module name */ |
| 6958 | rc = moveto_resolve_model(&ncname, &ncname_len, set, &moveto_mod); |
| 6959 | LY_CHECK_GOTO(rc, cleanup); |
| 6960 | |
| 6961 | if (moveto_mod && !attr_axis && !all_desc && (set->type == LYXP_SET_NODE_SET)) { |
| 6962 | /* find the matching schema node in some parent in the context */ |
| 6963 | for (i = 0; i < (signed)set->used; ++i) { |
| 6964 | if (set->val.nodes[i].type == set->root_type) { |
| 6965 | tmp = lys_find_child(NULL, moveto_mod, ncname, ncname_len, 0, 0); |
| 6966 | } else if ((set->val.nodes[i].type == LYXP_NODE_ELEM) |
| 6967 | && (!scnode || (lysc_data_parent(scnode) != set->val.nodes[i].node->schema))) { |
| 6968 | /* do not repeat the same search */ |
| 6969 | 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] | 6970 | } else { |
| 6971 | tmp = NULL; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6972 | } |
| 6973 | |
| 6974 | /* additional context check */ |
| 6975 | if (tmp && (set->root_type == LYXP_NODE_ROOT_CONFIG) && (tmp->flags & LYS_CONFIG_R)) { |
| 6976 | tmp = NULL; |
| 6977 | } |
| 6978 | |
| 6979 | if (tmp) { |
| 6980 | if (scnode) { |
| 6981 | /* we found a schema node with the same name but at different level, give up, too complicated */ |
| 6982 | scnode = NULL; |
| 6983 | break; |
| 6984 | } else { |
| 6985 | /* remember the found schema node and continue to make sure it can be used */ |
| 6986 | scnode = tmp; |
| 6987 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6988 | } |
| 6989 | } |
| 6990 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6991 | if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) { |
| 6992 | /* try to create the predicates */ |
| 6993 | if (eval_name_test_try_compile_predicates(exp, tok_idx, scnode, set->format, &predicates, &pred_type)) { |
| 6994 | /* hashes cannot be used */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6995 | scnode = NULL; |
| 6996 | } |
| 6997 | } |
| 6998 | } |
| 6999 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7000 | if (!scnode && moveto_mod) { |
| 7001 | /* we are not able to match based on a schema node and not all the modules match, |
| 7002 | * use dictionary for efficient comparison */ |
| 7003 | ncname_dict = lydict_insert(set->ctx, ncname, ncname_len); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7004 | } |
| 7005 | |
| 7006 | moveto: |
| 7007 | /* move to the attribute(s), data node(s), or schema node(s) */ |
| 7008 | if (attr_axis) { |
| 7009 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 7010 | set_scnode_clear_ctx(set); |
| 7011 | } else { |
| 7012 | if (all_desc) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7013 | rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7014 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7015 | rc = moveto_attr(set, moveto_mod, ncname_dict); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7016 | } |
| 7017 | LY_CHECK_GOTO(rc, cleanup); |
| 7018 | } |
| 7019 | } else { |
| 7020 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 7021 | if (all_desc) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7022 | rc = moveto_scnode_alldesc(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7023 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7024 | rc = moveto_scnode(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7025 | } |
| 7026 | LY_CHECK_GOTO(rc, cleanup); |
| 7027 | |
| 7028 | for (i = set->used - 1; i > -1; --i) { |
| 7029 | if (set->val.scnodes[i].in_ctx > 0) { |
| 7030 | break; |
| 7031 | } |
| 7032 | } |
| 7033 | if (i == -1) { |
| 7034 | path = lysc_path(set->ctx_scnode, LYSC_PATH_LOG, NULL, 0); |
| 7035 | 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] | 7036 | ncname_len, ncname, ncname - exp->expr, exp->expr, path); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7037 | free(path); |
| 7038 | } |
| 7039 | } else { |
| 7040 | if (all_desc) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7041 | rc = moveto_node_alldesc(set, moveto_mod, ncname_dict); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7042 | } else { |
| 7043 | if (scnode) { |
| 7044 | /* we can find the nodes using hashes */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7045 | rc = moveto_node_hash(set, scnode, predicates); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7046 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7047 | rc = moveto_node(set, moveto_mod, ncname_dict); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7048 | } |
| 7049 | } |
| 7050 | LY_CHECK_GOTO(rc, cleanup); |
| 7051 | } |
| 7052 | } |
| 7053 | |
| 7054 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7055 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 7056 | rc = eval_predicate(exp, tok_idx, set, options, 1); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7057 | LY_CHECK_RET(rc); |
| 7058 | } |
| 7059 | |
| 7060 | cleanup: |
Michal Vasko | db51a8d | 2020-05-27 15:22:29 +0200 | [diff] [blame] | 7061 | if (set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7062 | lydict_remove(set->ctx, ncname_dict); |
| 7063 | ly_path_predicates_free(set->ctx, pred_type, scnode, predicates); |
Michal Vasko | db51a8d | 2020-05-27 15:22:29 +0200 | [diff] [blame] | 7064 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7065 | return rc; |
| 7066 | } |
| 7067 | |
| 7068 | /** |
| 7069 | * @brief Evaluate NodeType and any following Predicates. Logs directly on error. |
| 7070 | * |
| 7071 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 7072 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 7073 | * [8] NodeType ::= 'text' | 'node' |
| 7074 | * |
| 7075 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7076 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7077 | * @param[in] attr_axis Whether to search attributes or standard nodes. |
| 7078 | * @param[in] all_desc Whether to search all the descendants or children only. |
| 7079 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7080 | * @param[in] options XPath options. |
| 7081 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7082 | */ |
| 7083 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7084 | 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] | 7085 | struct lyxp_set *set, int options) |
| 7086 | { |
| 7087 | LY_ERR rc; |
| 7088 | |
| 7089 | /* TODO */ |
| 7090 | (void)attr_axis; |
| 7091 | (void)all_desc; |
| 7092 | |
| 7093 | if (set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7094 | assert(exp->tok_len[*tok_idx] == 4); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7095 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 7096 | set_scnode_clear_ctx(set); |
| 7097 | /* just for the debug message below */ |
| 7098 | set = NULL; |
| 7099 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7100 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7101 | rc = xpath_node(NULL, 0, set, options); |
| 7102 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7103 | assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4)); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7104 | rc = xpath_text(NULL, 0, set, options); |
| 7105 | } |
| 7106 | LY_CHECK_RET(rc); |
| 7107 | } |
| 7108 | } |
| 7109 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7110 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7111 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7112 | |
| 7113 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7114 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7115 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7116 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7117 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7118 | |
| 7119 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7120 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7121 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7122 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7123 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7124 | |
| 7125 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7126 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 7127 | rc = eval_predicate(exp, tok_idx, set, options, 1); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7128 | LY_CHECK_RET(rc); |
| 7129 | } |
| 7130 | |
| 7131 | return LY_SUCCESS; |
| 7132 | } |
| 7133 | |
| 7134 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7135 | * @brief Evaluate RelativeLocationPath. Logs directly on error. |
| 7136 | * |
| 7137 | * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step |
| 7138 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7139 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7140 | * |
| 7141 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7142 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7143 | * @param[in] all_desc Whether to search all the descendants or children only. |
| 7144 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7145 | * @param[in] options XPath options. |
| 7146 | * @return LY_ERR (YL_EINCOMPLETE on unresolved when) |
| 7147 | */ |
| 7148 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7149 | 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] | 7150 | { |
| 7151 | int attr_axis; |
| 7152 | LY_ERR rc; |
| 7153 | |
| 7154 | goto step; |
| 7155 | do { |
| 7156 | /* evaluate '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7157 | if (exp->tok_len[*tok_idx] == 1) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7158 | all_desc = 0; |
| 7159 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7160 | assert(exp->tok_len[*tok_idx] == 2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7161 | all_desc = 1; |
| 7162 | } |
| 7163 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7164 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7165 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7166 | |
| 7167 | step: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7168 | /* evaluate abbreviated axis '@'? if any */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7169 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7170 | attr_axis = 1; |
| 7171 | |
| 7172 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7173 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7174 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7175 | } else { |
| 7176 | attr_axis = 0; |
| 7177 | } |
| 7178 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7179 | /* Step */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7180 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7181 | case LYXP_TOKEN_DOT: |
| 7182 | /* evaluate '.' */ |
| 7183 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 7184 | rc = moveto_scnode_self(set, all_desc, options); |
| 7185 | } else { |
| 7186 | rc = moveto_self(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 | |
| 7194 | case LYXP_TOKEN_DDOT: |
| 7195 | /* evaluate '..' */ |
| 7196 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 7197 | rc = moveto_scnode_parent(set, all_desc, options); |
| 7198 | } else { |
| 7199 | rc = moveto_parent(set, all_desc, options); |
| 7200 | } |
| 7201 | LY_CHECK_RET(rc); |
| 7202 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7203 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7204 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7205 | break; |
| 7206 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7207 | case LYXP_TOKEN_NAMETEST: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7208 | /* evaluate NameTest Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7209 | 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] | 7210 | LY_CHECK_RET(rc); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7211 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7212 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7213 | case LYXP_TOKEN_NODETYPE: |
| 7214 | /* evaluate NodeType Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7215 | 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] | 7216 | LY_CHECK_RET(rc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7217 | break; |
| 7218 | |
| 7219 | default: |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 7220 | LOGINT_RET(set ? set->ctx : NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7221 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7222 | } 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] | 7223 | |
| 7224 | return LY_SUCCESS; |
| 7225 | } |
| 7226 | |
| 7227 | /** |
| 7228 | * @brief Evaluate AbsoluteLocationPath. Logs directly on error. |
| 7229 | * |
| 7230 | * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath |
| 7231 | * |
| 7232 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7233 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7234 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7235 | * @param[in] options XPath options. |
| 7236 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7237 | */ |
| 7238 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7239 | 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] | 7240 | { |
| 7241 | int all_desc; |
| 7242 | LY_ERR rc; |
| 7243 | |
| 7244 | if (set) { |
| 7245 | /* no matter what tokens follow, we need to be at the root */ |
| 7246 | moveto_root(set, options); |
| 7247 | } |
| 7248 | |
| 7249 | /* '/' RelativeLocationPath? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7250 | if (exp->tok_len[*tok_idx] == 1) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7251 | /* evaluate '/' - deferred */ |
| 7252 | all_desc = 0; |
| 7253 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7254 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7255 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7256 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7257 | if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7258 | return LY_SUCCESS; |
| 7259 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7260 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7261 | case LYXP_TOKEN_DOT: |
| 7262 | case LYXP_TOKEN_DDOT: |
| 7263 | case LYXP_TOKEN_AT: |
| 7264 | case LYXP_TOKEN_NAMETEST: |
| 7265 | case LYXP_TOKEN_NODETYPE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7266 | rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7267 | LY_CHECK_RET(rc); |
| 7268 | break; |
| 7269 | default: |
| 7270 | break; |
| 7271 | } |
| 7272 | |
| 7273 | /* '//' RelativeLocationPath */ |
| 7274 | } else { |
| 7275 | /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */ |
| 7276 | all_desc = 1; |
| 7277 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7278 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7279 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7280 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7281 | rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7282 | LY_CHECK_RET(rc); |
| 7283 | } |
| 7284 | |
| 7285 | return LY_SUCCESS; |
| 7286 | } |
| 7287 | |
| 7288 | /** |
| 7289 | * @brief Evaluate FunctionCall. Logs directly on error. |
| 7290 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7291 | * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7292 | * |
| 7293 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7294 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7295 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7296 | * @param[in] options XPath options. |
| 7297 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7298 | */ |
| 7299 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7300 | 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] | 7301 | { |
| 7302 | LY_ERR rc; |
| 7303 | 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] | 7304 | uint16_t arg_count = 0, i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7305 | struct lyxp_set **args = NULL, **args_aux; |
| 7306 | |
| 7307 | if (set) { |
| 7308 | /* FunctionName */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7309 | switch (exp->tok_len[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7310 | case 3: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7311 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7312 | xpath_func = &xpath_not; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7313 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7314 | xpath_func = &xpath_sum; |
| 7315 | } |
| 7316 | break; |
| 7317 | case 4: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7318 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7319 | xpath_func = &xpath_lang; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7320 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7321 | xpath_func = &xpath_last; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7322 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7323 | xpath_func = &xpath_name; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7324 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7325 | xpath_func = &xpath_true; |
| 7326 | } |
| 7327 | break; |
| 7328 | case 5: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7329 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7330 | xpath_func = &xpath_count; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7331 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7332 | xpath_func = &xpath_false; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7333 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7334 | xpath_func = &xpath_floor; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7335 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7336 | xpath_func = &xpath_round; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7337 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7338 | xpath_func = &xpath_deref; |
| 7339 | } |
| 7340 | break; |
| 7341 | case 6: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7342 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7343 | xpath_func = &xpath_concat; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7344 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7345 | xpath_func = &xpath_number; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7346 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7347 | xpath_func = &xpath_string; |
| 7348 | } |
| 7349 | break; |
| 7350 | case 7: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7351 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7352 | xpath_func = &xpath_boolean; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7353 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7354 | xpath_func = &xpath_ceiling; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7355 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7356 | xpath_func = &xpath_current; |
| 7357 | } |
| 7358 | break; |
| 7359 | case 8: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7360 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7361 | xpath_func = &xpath_contains; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7362 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7363 | xpath_func = &xpath_position; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7364 | } 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] | 7365 | xpath_func = &xpath_re_match; |
| 7366 | } |
| 7367 | break; |
| 7368 | case 9: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7369 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7370 | xpath_func = &xpath_substring; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7371 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7372 | xpath_func = &xpath_translate; |
| 7373 | } |
| 7374 | break; |
| 7375 | case 10: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7376 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7377 | xpath_func = &xpath_local_name; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7378 | } 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] | 7379 | xpath_func = &xpath_enum_value; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7380 | } 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] | 7381 | xpath_func = &xpath_bit_is_set; |
| 7382 | } |
| 7383 | break; |
| 7384 | case 11: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7385 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7386 | xpath_func = &xpath_starts_with; |
| 7387 | } |
| 7388 | break; |
| 7389 | case 12: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7390 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7391 | xpath_func = &xpath_derived_from; |
| 7392 | } |
| 7393 | break; |
| 7394 | case 13: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7395 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7396 | xpath_func = &xpath_namespace_uri; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7397 | } 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] | 7398 | xpath_func = &xpath_string_length; |
| 7399 | } |
| 7400 | break; |
| 7401 | case 15: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7402 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7403 | xpath_func = &xpath_normalize_space; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7404 | } 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] | 7405 | xpath_func = &xpath_substring_after; |
| 7406 | } |
| 7407 | break; |
| 7408 | case 16: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7409 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7410 | xpath_func = &xpath_substring_before; |
| 7411 | } |
| 7412 | break; |
| 7413 | case 20: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7414 | 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] | 7415 | xpath_func = &xpath_derived_from_or_self; |
| 7416 | } |
| 7417 | break; |
| 7418 | } |
| 7419 | |
| 7420 | if (!xpath_func) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7421 | 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] | 7422 | return LY_EVALID; |
| 7423 | } |
| 7424 | } |
| 7425 | |
| 7426 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7427 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7428 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7429 | |
| 7430 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7431 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7432 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7433 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7434 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7435 | |
| 7436 | /* ( Expr ( ',' Expr )* )? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7437 | if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7438 | if (set) { |
| 7439 | args = malloc(sizeof *args); |
| 7440 | LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup); |
| 7441 | arg_count = 1; |
| 7442 | args[0] = set_copy(set); |
| 7443 | if (!args[0]) { |
| 7444 | rc = LY_EMEM; |
| 7445 | goto cleanup; |
| 7446 | } |
| 7447 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7448 | rc = eval_expr_select(exp, tok_idx, 0, args[0], options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7449 | LY_CHECK_GOTO(rc, cleanup); |
| 7450 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7451 | rc = eval_expr_select(exp, tok_idx, 0, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7452 | LY_CHECK_GOTO(rc, cleanup); |
| 7453 | } |
| 7454 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7455 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7456 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7457 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7458 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7459 | |
| 7460 | if (set) { |
| 7461 | ++arg_count; |
| 7462 | args_aux = realloc(args, arg_count * sizeof *args); |
| 7463 | LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup); |
| 7464 | args = args_aux; |
| 7465 | args[arg_count - 1] = set_copy(set); |
| 7466 | if (!args[arg_count - 1]) { |
| 7467 | rc = LY_EMEM; |
| 7468 | goto cleanup; |
| 7469 | } |
| 7470 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7471 | 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] | 7472 | LY_CHECK_GOTO(rc, cleanup); |
| 7473 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7474 | rc = eval_expr_select(exp, tok_idx, 0, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7475 | LY_CHECK_GOTO(rc, cleanup); |
| 7476 | } |
| 7477 | } |
| 7478 | |
| 7479 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7480 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7481 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7482 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7483 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7484 | |
| 7485 | if (set) { |
| 7486 | /* evaluate function */ |
| 7487 | rc = xpath_func(args, arg_count, set, options); |
| 7488 | |
| 7489 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7490 | /* merge all nodes from arg evaluations */ |
| 7491 | for (i = 0; i < arg_count; ++i) { |
| 7492 | set_scnode_clear_ctx(args[i]); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7493 | lyxp_set_scnode_merge(set, args[i]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7494 | } |
| 7495 | } |
| 7496 | } else { |
| 7497 | rc = LY_SUCCESS; |
| 7498 | } |
| 7499 | |
| 7500 | cleanup: |
| 7501 | for (i = 0; i < arg_count; ++i) { |
| 7502 | lyxp_set_free(args[i]); |
| 7503 | } |
| 7504 | free(args); |
| 7505 | |
| 7506 | return rc; |
| 7507 | } |
| 7508 | |
| 7509 | /** |
| 7510 | * @brief Evaluate Number. Logs directly on error. |
| 7511 | * |
| 7512 | * @param[in] ctx Context for errors. |
| 7513 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7514 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7515 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7516 | * @return LY_ERR |
| 7517 | */ |
| 7518 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7519 | 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] | 7520 | { |
| 7521 | long double num; |
| 7522 | char *endptr; |
| 7523 | |
| 7524 | if (set) { |
| 7525 | errno = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7526 | num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7527 | if (errno) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7528 | 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] | 7529 | 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] | 7530 | 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] | 7531 | return LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7532 | } else if (endptr - &exp->expr[exp->tok_pos[*tok_idx]] != exp->tok_len[*tok_idx]) { |
| 7533 | 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] | 7534 | 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] | 7535 | exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7536 | return LY_EVALID; |
| 7537 | } |
| 7538 | |
| 7539 | set_fill_number(set, num); |
| 7540 | } |
| 7541 | |
| 7542 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7543 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7544 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7545 | return LY_SUCCESS; |
| 7546 | } |
| 7547 | |
| 7548 | /** |
| 7549 | * @brief Evaluate PathExpr. Logs directly on error. |
| 7550 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7551 | * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate* |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7552 | * | PrimaryExpr Predicate* '/' RelativeLocationPath |
| 7553 | * | PrimaryExpr Predicate* '//' RelativeLocationPath |
| 7554 | * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7555 | * [10] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7556 | * |
| 7557 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7558 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7559 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7560 | * @param[in] options XPath options. |
| 7561 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7562 | */ |
| 7563 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7564 | 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] | 7565 | { |
| 7566 | int all_desc, parent_pos_pred; |
| 7567 | LY_ERR rc; |
| 7568 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7569 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7570 | case LYXP_TOKEN_PAR1: |
| 7571 | /* '(' Expr ')' */ |
| 7572 | |
| 7573 | /* '(' */ |
| 7574 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7575 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7576 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7577 | |
| 7578 | /* Expr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7579 | rc = eval_expr_select(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7580 | LY_CHECK_RET(rc); |
| 7581 | |
| 7582 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7583 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7584 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7585 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7586 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7587 | |
| 7588 | parent_pos_pred = 0; |
| 7589 | goto predicate; |
| 7590 | |
| 7591 | case LYXP_TOKEN_DOT: |
| 7592 | case LYXP_TOKEN_DDOT: |
| 7593 | case LYXP_TOKEN_AT: |
| 7594 | case LYXP_TOKEN_NAMETEST: |
| 7595 | case LYXP_TOKEN_NODETYPE: |
| 7596 | /* RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7597 | rc = eval_relative_location_path(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7598 | LY_CHECK_RET(rc); |
| 7599 | break; |
| 7600 | |
| 7601 | case LYXP_TOKEN_FUNCNAME: |
| 7602 | /* FunctionCall */ |
| 7603 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7604 | rc = eval_function_call(exp, tok_idx, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7605 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7606 | rc = eval_function_call(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7607 | } |
| 7608 | LY_CHECK_RET(rc); |
| 7609 | |
| 7610 | parent_pos_pred = 1; |
| 7611 | goto predicate; |
| 7612 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 7613 | case LYXP_TOKEN_OPER_PATH: |
| 7614 | case LYXP_TOKEN_OPER_RPATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7615 | /* AbsoluteLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7616 | rc = eval_absolute_location_path(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7617 | LY_CHECK_RET(rc); |
| 7618 | break; |
| 7619 | |
| 7620 | case LYXP_TOKEN_LITERAL: |
| 7621 | /* Literal */ |
| 7622 | if (!set || (options & LYXP_SCNODE_ALL)) { |
| 7623 | if (set) { |
| 7624 | set_scnode_clear_ctx(set); |
| 7625 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7626 | eval_literal(exp, tok_idx, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7627 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7628 | eval_literal(exp, tok_idx, set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7629 | } |
| 7630 | |
| 7631 | parent_pos_pred = 1; |
| 7632 | goto predicate; |
| 7633 | |
| 7634 | case LYXP_TOKEN_NUMBER: |
| 7635 | /* Number */ |
| 7636 | if (!set || (options & LYXP_SCNODE_ALL)) { |
| 7637 | if (set) { |
| 7638 | set_scnode_clear_ctx(set); |
| 7639 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7640 | rc = eval_number(NULL, exp, tok_idx, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7641 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7642 | rc = eval_number(set->ctx, exp, tok_idx, set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7643 | } |
| 7644 | LY_CHECK_RET(rc); |
| 7645 | |
| 7646 | parent_pos_pred = 1; |
| 7647 | goto predicate; |
| 7648 | |
| 7649 | default: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7650 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[*tok_idx]), |
| 7651 | &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7652 | return LY_EVALID; |
| 7653 | } |
| 7654 | |
| 7655 | return LY_SUCCESS; |
| 7656 | |
| 7657 | predicate: |
| 7658 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7659 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 7660 | rc = eval_predicate(exp, tok_idx, set, options, parent_pos_pred); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7661 | LY_CHECK_RET(rc); |
| 7662 | } |
| 7663 | |
| 7664 | /* ('/' or '//') RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7665 | 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] | 7666 | |
| 7667 | /* evaluate '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7668 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7669 | all_desc = 0; |
| 7670 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7671 | all_desc = 1; |
| 7672 | } |
| 7673 | |
| 7674 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7675 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7676 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7677 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7678 | rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7679 | LY_CHECK_RET(rc); |
| 7680 | } |
| 7681 | |
| 7682 | return LY_SUCCESS; |
| 7683 | } |
| 7684 | |
| 7685 | /** |
| 7686 | * @brief Evaluate UnionExpr. Logs directly on error. |
| 7687 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7688 | * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7689 | * |
| 7690 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7691 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7692 | * @param[in] repeat How many times this expression is repeated. |
| 7693 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7694 | * @param[in] options XPath options. |
| 7695 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7696 | */ |
| 7697 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7698 | 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] | 7699 | { |
| 7700 | LY_ERR rc = LY_SUCCESS; |
| 7701 | struct lyxp_set orig_set, set2; |
| 7702 | uint16_t i; |
| 7703 | |
| 7704 | assert(repeat); |
| 7705 | |
| 7706 | set_init(&orig_set, set); |
| 7707 | set_init(&set2, set); |
| 7708 | |
| 7709 | set_fill_set(&orig_set, set); |
| 7710 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7711 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7712 | LY_CHECK_GOTO(rc, cleanup); |
| 7713 | |
| 7714 | /* ('|' PathExpr)* */ |
| 7715 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7716 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7717 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7718 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7719 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7720 | |
| 7721 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7722 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7723 | LY_CHECK_GOTO(rc, cleanup); |
| 7724 | continue; |
| 7725 | } |
| 7726 | |
| 7727 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7728 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7729 | LY_CHECK_GOTO(rc, cleanup); |
| 7730 | |
| 7731 | /* eval */ |
| 7732 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7733 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7734 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7735 | rc = moveto_union(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7736 | LY_CHECK_GOTO(rc, cleanup); |
| 7737 | } |
| 7738 | } |
| 7739 | |
| 7740 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7741 | lyxp_set_free_content(&orig_set); |
| 7742 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7743 | return rc; |
| 7744 | } |
| 7745 | |
| 7746 | /** |
| 7747 | * @brief Evaluate UnaryExpr. Logs directly on error. |
| 7748 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7749 | * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7750 | * |
| 7751 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7752 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7753 | * @param[in] repeat How many times this expression is repeated. |
| 7754 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7755 | * @param[in] options XPath options. |
| 7756 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7757 | */ |
| 7758 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7759 | 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] | 7760 | { |
| 7761 | LY_ERR rc; |
| 7762 | uint16_t this_op, i; |
| 7763 | |
| 7764 | assert(repeat); |
| 7765 | |
| 7766 | /* ('-')+ */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7767 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7768 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7769 | 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] | 7770 | |
| 7771 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7772 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7773 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7774 | } |
| 7775 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7776 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7777 | LY_CHECK_RET(rc); |
| 7778 | |
| 7779 | if (set && (repeat % 2)) { |
| 7780 | if (options & LYXP_SCNODE_ALL) { |
| 7781 | warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]); |
| 7782 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7783 | 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] | 7784 | LY_CHECK_RET(rc); |
| 7785 | } |
| 7786 | } |
| 7787 | |
| 7788 | return LY_SUCCESS; |
| 7789 | } |
| 7790 | |
| 7791 | /** |
| 7792 | * @brief Evaluate MultiplicativeExpr. Logs directly on error. |
| 7793 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7794 | * [18] MultiplicativeExpr ::= UnaryExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7795 | * | MultiplicativeExpr '*' UnaryExpr |
| 7796 | * | MultiplicativeExpr 'div' UnaryExpr |
| 7797 | * | MultiplicativeExpr 'mod' UnaryExpr |
| 7798 | * |
| 7799 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7800 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7801 | * @param[in] repeat How many times this expression is repeated. |
| 7802 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7803 | * @param[in] options XPath options. |
| 7804 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7805 | */ |
| 7806 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7807 | 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] | 7808 | { |
| 7809 | LY_ERR rc; |
| 7810 | uint16_t this_op; |
| 7811 | struct lyxp_set orig_set, set2; |
| 7812 | uint16_t i; |
| 7813 | |
| 7814 | assert(repeat); |
| 7815 | |
| 7816 | set_init(&orig_set, set); |
| 7817 | set_init(&set2, set); |
| 7818 | |
| 7819 | set_fill_set(&orig_set, set); |
| 7820 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7821 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7822 | LY_CHECK_GOTO(rc, cleanup); |
| 7823 | |
| 7824 | /* ('*' / 'div' / 'mod' UnaryExpr)* */ |
| 7825 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7826 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7827 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7828 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7829 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7830 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7831 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7832 | |
| 7833 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7834 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7835 | LY_CHECK_GOTO(rc, cleanup); |
| 7836 | continue; |
| 7837 | } |
| 7838 | |
| 7839 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7840 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7841 | LY_CHECK_GOTO(rc, cleanup); |
| 7842 | |
| 7843 | /* eval */ |
| 7844 | if (options & LYXP_SCNODE_ALL) { |
| 7845 | 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] | 7846 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7847 | set_scnode_clear_ctx(set); |
| 7848 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7849 | 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] | 7850 | LY_CHECK_GOTO(rc, cleanup); |
| 7851 | } |
| 7852 | } |
| 7853 | |
| 7854 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7855 | lyxp_set_free_content(&orig_set); |
| 7856 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7857 | return rc; |
| 7858 | } |
| 7859 | |
| 7860 | /** |
| 7861 | * @brief Evaluate AdditiveExpr. Logs directly on error. |
| 7862 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7863 | * [17] AdditiveExpr ::= MultiplicativeExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7864 | * | AdditiveExpr '+' MultiplicativeExpr |
| 7865 | * | AdditiveExpr '-' MultiplicativeExpr |
| 7866 | * |
| 7867 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7868 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7869 | * @param[in] repeat How many times this expression is repeated. |
| 7870 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7871 | * @param[in] options XPath options. |
| 7872 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7873 | */ |
| 7874 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7875 | 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] | 7876 | { |
| 7877 | LY_ERR rc; |
| 7878 | uint16_t this_op; |
| 7879 | struct lyxp_set orig_set, set2; |
| 7880 | uint16_t i; |
| 7881 | |
| 7882 | assert(repeat); |
| 7883 | |
| 7884 | set_init(&orig_set, set); |
| 7885 | set_init(&set2, set); |
| 7886 | |
| 7887 | set_fill_set(&orig_set, set); |
| 7888 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7889 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7890 | LY_CHECK_GOTO(rc, cleanup); |
| 7891 | |
| 7892 | /* ('+' / '-' MultiplicativeExpr)* */ |
| 7893 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7894 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7895 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7896 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7897 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7898 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7899 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7900 | |
| 7901 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7902 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7903 | LY_CHECK_GOTO(rc, cleanup); |
| 7904 | continue; |
| 7905 | } |
| 7906 | |
| 7907 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7908 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7909 | LY_CHECK_GOTO(rc, cleanup); |
| 7910 | |
| 7911 | /* eval */ |
| 7912 | if (options & LYXP_SCNODE_ALL) { |
| 7913 | 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] | 7914 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7915 | set_scnode_clear_ctx(set); |
| 7916 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7917 | 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] | 7918 | LY_CHECK_GOTO(rc, cleanup); |
| 7919 | } |
| 7920 | } |
| 7921 | |
| 7922 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7923 | lyxp_set_free_content(&orig_set); |
| 7924 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7925 | return rc; |
| 7926 | } |
| 7927 | |
| 7928 | /** |
| 7929 | * @brief Evaluate RelationalExpr. Logs directly on error. |
| 7930 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7931 | * [16] RelationalExpr ::= AdditiveExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7932 | * | RelationalExpr '<' AdditiveExpr |
| 7933 | * | RelationalExpr '>' AdditiveExpr |
| 7934 | * | RelationalExpr '<=' AdditiveExpr |
| 7935 | * | RelationalExpr '>=' AdditiveExpr |
| 7936 | * |
| 7937 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7938 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7939 | * @param[in] repeat How many times this expression is repeated. |
| 7940 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7941 | * @param[in] options XPath options. |
| 7942 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7943 | */ |
| 7944 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7945 | 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] | 7946 | { |
| 7947 | LY_ERR rc; |
| 7948 | uint16_t this_op; |
| 7949 | struct lyxp_set orig_set, set2; |
| 7950 | uint16_t i; |
| 7951 | |
| 7952 | assert(repeat); |
| 7953 | |
| 7954 | set_init(&orig_set, set); |
| 7955 | set_init(&set2, set); |
| 7956 | |
| 7957 | set_fill_set(&orig_set, set); |
| 7958 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7959 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7960 | LY_CHECK_GOTO(rc, cleanup); |
| 7961 | |
| 7962 | /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */ |
| 7963 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7964 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7965 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7966 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7967 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7968 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7969 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7970 | |
| 7971 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7972 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7973 | LY_CHECK_GOTO(rc, cleanup); |
| 7974 | continue; |
| 7975 | } |
| 7976 | |
| 7977 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7978 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7979 | LY_CHECK_GOTO(rc, cleanup); |
| 7980 | |
| 7981 | /* eval */ |
| 7982 | if (options & LYXP_SCNODE_ALL) { |
| 7983 | 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] | 7984 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7985 | set_scnode_clear_ctx(set); |
| 7986 | } else { |
| 7987 | rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options); |
| 7988 | LY_CHECK_GOTO(rc, cleanup); |
| 7989 | } |
| 7990 | } |
| 7991 | |
| 7992 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7993 | lyxp_set_free_content(&orig_set); |
| 7994 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7995 | return rc; |
| 7996 | } |
| 7997 | |
| 7998 | /** |
| 7999 | * @brief Evaluate EqualityExpr. Logs directly on error. |
| 8000 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8001 | * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8002 | * | EqualityExpr '!=' RelationalExpr |
| 8003 | * |
| 8004 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8005 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8006 | * @param[in] repeat How many times this expression is repeated. |
| 8007 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8008 | * @param[in] options XPath options. |
| 8009 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8010 | */ |
| 8011 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8012 | 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] | 8013 | { |
| 8014 | LY_ERR rc; |
| 8015 | uint16_t this_op; |
| 8016 | struct lyxp_set orig_set, set2; |
| 8017 | uint16_t i; |
| 8018 | |
| 8019 | assert(repeat); |
| 8020 | |
| 8021 | set_init(&orig_set, set); |
| 8022 | set_init(&set2, set); |
| 8023 | |
| 8024 | set_fill_set(&orig_set, set); |
| 8025 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8026 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8027 | LY_CHECK_GOTO(rc, cleanup); |
| 8028 | |
| 8029 | /* ('=' / '!=' RelationalExpr)* */ |
| 8030 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8031 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8032 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8033 | 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] | 8034 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8035 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 8036 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8037 | |
| 8038 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8039 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8040 | LY_CHECK_GOTO(rc, cleanup); |
| 8041 | continue; |
| 8042 | } |
| 8043 | |
| 8044 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8045 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8046 | LY_CHECK_GOTO(rc, cleanup); |
| 8047 | |
| 8048 | /* eval */ |
| 8049 | if (options & LYXP_SCNODE_ALL) { |
| 8050 | 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] | 8051 | warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1); |
| 8052 | 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] | 8053 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8054 | set_scnode_clear_ctx(set); |
| 8055 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8056 | rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options); |
| 8057 | LY_CHECK_GOTO(rc, cleanup); |
| 8058 | } |
| 8059 | } |
| 8060 | |
| 8061 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8062 | lyxp_set_free_content(&orig_set); |
| 8063 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8064 | return rc; |
| 8065 | } |
| 8066 | |
| 8067 | /** |
| 8068 | * @brief Evaluate AndExpr. Logs directly on error. |
| 8069 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8070 | * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8071 | * |
| 8072 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8073 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8074 | * @param[in] repeat How many times this expression is repeated. |
| 8075 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8076 | * @param[in] options XPath options. |
| 8077 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8078 | */ |
| 8079 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8080 | 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] | 8081 | { |
| 8082 | LY_ERR rc; |
| 8083 | struct lyxp_set orig_set, set2; |
| 8084 | uint16_t i; |
| 8085 | |
| 8086 | assert(repeat); |
| 8087 | |
| 8088 | set_init(&orig_set, set); |
| 8089 | set_init(&set2, set); |
| 8090 | |
| 8091 | set_fill_set(&orig_set, set); |
| 8092 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8093 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8094 | LY_CHECK_GOTO(rc, cleanup); |
| 8095 | |
| 8096 | /* cast to boolean, we know that will be the final result */ |
| 8097 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 8098 | set_scnode_clear_ctx(set); |
| 8099 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8100 | lyxp_set_cast(set, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8101 | } |
| 8102 | |
| 8103 | /* ('and' EqualityExpr)* */ |
| 8104 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8105 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG); |
| 8106 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || !set->val.bln ? "skipped" : "parsed"), |
| 8107 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 8108 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8109 | |
| 8110 | /* lazy evaluation */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8111 | if (!set || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) { |
| 8112 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8113 | LY_CHECK_GOTO(rc, cleanup); |
| 8114 | continue; |
| 8115 | } |
| 8116 | |
| 8117 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8118 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8119 | LY_CHECK_GOTO(rc, cleanup); |
| 8120 | |
| 8121 | /* eval - just get boolean value actually */ |
| 8122 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 8123 | set_scnode_clear_ctx(&set2); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 8124 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8125 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8126 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8127 | set_fill_set(set, &set2); |
| 8128 | } |
| 8129 | } |
| 8130 | |
| 8131 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8132 | lyxp_set_free_content(&orig_set); |
| 8133 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8134 | return rc; |
| 8135 | } |
| 8136 | |
| 8137 | /** |
| 8138 | * @brief Evaluate OrExpr. Logs directly on error. |
| 8139 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8140 | * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8141 | * |
| 8142 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8143 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8144 | * @param[in] repeat How many times this expression is repeated. |
| 8145 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8146 | * @param[in] options XPath options. |
| 8147 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8148 | */ |
| 8149 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8150 | 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] | 8151 | { |
| 8152 | LY_ERR rc; |
| 8153 | struct lyxp_set orig_set, set2; |
| 8154 | uint16_t i; |
| 8155 | |
| 8156 | assert(repeat); |
| 8157 | |
| 8158 | set_init(&orig_set, set); |
| 8159 | set_init(&set2, set); |
| 8160 | |
| 8161 | set_fill_set(&orig_set, set); |
| 8162 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8163 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8164 | LY_CHECK_GOTO(rc, cleanup); |
| 8165 | |
| 8166 | /* cast to boolean, we know that will be the final result */ |
| 8167 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 8168 | set_scnode_clear_ctx(set); |
| 8169 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8170 | lyxp_set_cast(set, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8171 | } |
| 8172 | |
| 8173 | /* ('or' AndExpr)* */ |
| 8174 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8175 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG); |
| 8176 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || set->val.bln ? "skipped" : "parsed"), |
| 8177 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 8178 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8179 | |
| 8180 | /* lazy evaluation */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8181 | if (!set || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) { |
| 8182 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8183 | LY_CHECK_GOTO(rc, cleanup); |
| 8184 | continue; |
| 8185 | } |
| 8186 | |
| 8187 | set_fill_set(&set2, &orig_set); |
| 8188 | /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one), |
| 8189 | * but it does not matter */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8190 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8191 | LY_CHECK_GOTO(rc, cleanup); |
| 8192 | |
| 8193 | /* eval - just get boolean value actually */ |
| 8194 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 8195 | set_scnode_clear_ctx(&set2); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 8196 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8197 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8198 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8199 | set_fill_set(set, &set2); |
| 8200 | } |
| 8201 | } |
| 8202 | |
| 8203 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8204 | lyxp_set_free_content(&orig_set); |
| 8205 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8206 | return rc; |
| 8207 | } |
| 8208 | |
| 8209 | /** |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8210 | * @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] | 8211 | * |
| 8212 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8213 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8214 | * @param[in] etype Expression type to evaluate. |
| 8215 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8216 | * @param[in] options XPath options. |
| 8217 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8218 | */ |
| 8219 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8220 | 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] | 8221 | { |
| 8222 | uint16_t i, count; |
| 8223 | enum lyxp_expr_type next_etype; |
| 8224 | LY_ERR rc; |
| 8225 | |
| 8226 | /* process operator repeats */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8227 | if (!exp->repeat[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8228 | next_etype = LYXP_EXPR_NONE; |
| 8229 | } else { |
| 8230 | /* find etype repeat */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8231 | for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8232 | |
| 8233 | /* select one-priority lower because etype expression called us */ |
| 8234 | if (i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8235 | next_etype = exp->repeat[*tok_idx][i - 1]; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8236 | /* count repeats for that expression */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8237 | 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] | 8238 | } else { |
| 8239 | next_etype = LYXP_EXPR_NONE; |
| 8240 | } |
| 8241 | } |
| 8242 | |
| 8243 | /* decide what expression are we parsing based on the repeat */ |
| 8244 | switch (next_etype) { |
| 8245 | case LYXP_EXPR_OR: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8246 | rc = eval_or_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8247 | break; |
| 8248 | case LYXP_EXPR_AND: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8249 | rc = eval_and_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8250 | break; |
| 8251 | case LYXP_EXPR_EQUALITY: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8252 | rc = eval_equality_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8253 | break; |
| 8254 | case LYXP_EXPR_RELATIONAL: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8255 | rc = eval_relational_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8256 | break; |
| 8257 | case LYXP_EXPR_ADDITIVE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8258 | rc = eval_additive_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8259 | break; |
| 8260 | case LYXP_EXPR_MULTIPLICATIVE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8261 | rc = eval_multiplicative_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8262 | break; |
| 8263 | case LYXP_EXPR_UNARY: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8264 | rc = eval_unary_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8265 | break; |
| 8266 | case LYXP_EXPR_UNION: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8267 | rc = eval_union_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8268 | break; |
| 8269 | case LYXP_EXPR_NONE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8270 | rc = eval_path_expr(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8271 | break; |
| 8272 | default: |
| 8273 | LOGINT_RET(set->ctx); |
| 8274 | } |
| 8275 | |
| 8276 | return rc; |
| 8277 | } |
| 8278 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8279 | /** |
| 8280 | * @brief Get root type. |
| 8281 | * |
| 8282 | * @param[in] ctx_node Context node. |
| 8283 | * @param[in] ctx_scnode Schema context node. |
| 8284 | * @param[in] options XPath options. |
| 8285 | * @return Root type. |
| 8286 | */ |
| 8287 | static enum lyxp_node_type |
| 8288 | lyxp_get_root_type(const struct lyd_node *ctx_node, const struct lysc_node *ctx_scnode, int options) |
| 8289 | { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 8290 | const struct lysc_node *op; |
| 8291 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8292 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 8293 | for (op = ctx_scnode; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent); |
| 8294 | |
| 8295 | if (op || (options & LYXP_SCNODE)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8296 | /* general root that can access everything */ |
| 8297 | return LYXP_NODE_ROOT; |
| 8298 | } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) { |
| 8299 | /* root context node can access only config data (because we said so, it is unspecified) */ |
| 8300 | return LYXP_NODE_ROOT_CONFIG; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8301 | } |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 8302 | return LYXP_NODE_ROOT; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8303 | } |
| 8304 | |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 8305 | op = ctx_node ? ctx_node->schema : NULL; |
| 8306 | for (; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent); |
| 8307 | |
| 8308 | if (!ctx_node || (!op && (ctx_node->schema->flags & LYS_CONFIG_W))) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8309 | /* root context node can access only config data (because we said so, it is unspecified) */ |
| 8310 | return LYXP_NODE_ROOT_CONFIG; |
| 8311 | } |
| 8312 | |
| 8313 | return LYXP_NODE_ROOT; |
| 8314 | } |
| 8315 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8316 | LY_ERR |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 8317 | lyxp_eval(struct lyxp_expr *exp, LYD_FORMAT format, const struct lys_module *local_mod, const struct lyd_node *ctx_node, |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 8318 | 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] | 8319 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8320 | uint16_t tok_idx = 0; |
| 8321 | const struct lyd_node *real_ctx_node; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8322 | LY_ERR rc; |
| 8323 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8324 | LY_CHECK_ARG_RET(NULL, exp, local_mod, ctx_node, set, LY_EINVAL); |
| 8325 | |
| 8326 | if ((ctx_node_type == LYXP_NODE_ROOT) || (ctx_node_type == LYXP_NODE_ROOT_CONFIG)) { |
| 8327 | /* we always need some context node because it is used for resolving unqualified names */ |
| 8328 | real_ctx_node = NULL; |
| 8329 | } else { |
| 8330 | real_ctx_node = ctx_node; |
| 8331 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8332 | |
| 8333 | /* prepare set for evaluation */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8334 | tok_idx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8335 | memset(set, 0, sizeof *set); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8336 | set->type = LYXP_SET_NODE_SET; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8337 | 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] | 8338 | set->ctx = local_mod->ctx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8339 | set->ctx_node = ctx_node; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8340 | set->root_type = lyxp_get_root_type(real_ctx_node, NULL, options); |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 8341 | for (set->context_op = ctx_node->schema; |
| 8342 | set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); |
| 8343 | set->context_op = set->context_op->parent); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8344 | set->local_mod = local_mod; |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 8345 | set->tree = tree; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8346 | set->format = format; |
| 8347 | |
| 8348 | /* evaluate */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8349 | rc = eval_expr_select(exp, &tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8350 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8351 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8352 | } |
| 8353 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8354 | return rc; |
| 8355 | } |
| 8356 | |
| 8357 | #if 0 |
| 8358 | |
| 8359 | /* full xml printing of set elements, not used currently */ |
| 8360 | |
| 8361 | void |
| 8362 | lyxp_set_print_xml(FILE *f, struct lyxp_set *set) |
| 8363 | { |
| 8364 | uint32_t i; |
| 8365 | char *str_num; |
| 8366 | struct lyout out; |
| 8367 | |
| 8368 | memset(&out, 0, sizeof out); |
| 8369 | |
| 8370 | out.type = LYOUT_STREAM; |
| 8371 | out.method.f = f; |
| 8372 | |
| 8373 | switch (set->type) { |
| 8374 | case LYXP_SET_EMPTY: |
| 8375 | ly_print(&out, "Empty XPath set\n\n"); |
| 8376 | break; |
| 8377 | case LYXP_SET_BOOLEAN: |
| 8378 | ly_print(&out, "Boolean XPath set:\n"); |
| 8379 | ly_print(&out, "%s\n\n", set->value.bool ? "true" : "false"); |
| 8380 | break; |
| 8381 | case LYXP_SET_STRING: |
| 8382 | ly_print(&out, "String XPath set:\n"); |
| 8383 | ly_print(&out, "\"%s\"\n\n", set->value.str); |
| 8384 | break; |
| 8385 | case LYXP_SET_NUMBER: |
| 8386 | ly_print(&out, "Number XPath set:\n"); |
| 8387 | |
| 8388 | if (isnan(set->value.num)) { |
| 8389 | str_num = strdup("NaN"); |
| 8390 | } else if ((set->value.num == 0) || (set->value.num == -0.0f)) { |
| 8391 | str_num = strdup("0"); |
| 8392 | } else if (isinf(set->value.num) && !signbit(set->value.num)) { |
| 8393 | str_num = strdup("Infinity"); |
| 8394 | } else if (isinf(set->value.num) && signbit(set->value.num)) { |
| 8395 | str_num = strdup("-Infinity"); |
| 8396 | } else if ((long long)set->value.num == set->value.num) { |
| 8397 | if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) { |
| 8398 | str_num = NULL; |
| 8399 | } |
| 8400 | } else { |
| 8401 | if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) { |
| 8402 | str_num = NULL; |
| 8403 | } |
| 8404 | } |
| 8405 | if (!str_num) { |
| 8406 | LOGMEM; |
| 8407 | return; |
| 8408 | } |
| 8409 | ly_print(&out, "%s\n\n", str_num); |
| 8410 | free(str_num); |
| 8411 | break; |
| 8412 | case LYXP_SET_NODE_SET: |
| 8413 | ly_print(&out, "Node XPath set:\n"); |
| 8414 | |
| 8415 | for (i = 0; i < set->used; ++i) { |
| 8416 | ly_print(&out, "%d. ", i + 1); |
| 8417 | switch (set->node_type[i]) { |
| 8418 | case LYXP_NODE_ROOT_ALL: |
| 8419 | ly_print(&out, "ROOT all\n\n"); |
| 8420 | break; |
| 8421 | case LYXP_NODE_ROOT_CONFIG: |
| 8422 | ly_print(&out, "ROOT config\n\n"); |
| 8423 | break; |
| 8424 | case LYXP_NODE_ROOT_STATE: |
| 8425 | ly_print(&out, "ROOT state\n\n"); |
| 8426 | break; |
| 8427 | case LYXP_NODE_ROOT_NOTIF: |
| 8428 | ly_print(&out, "ROOT notification \"%s\"\n\n", set->value.nodes[i]->schema->name); |
| 8429 | break; |
| 8430 | case LYXP_NODE_ROOT_RPC: |
| 8431 | ly_print(&out, "ROOT rpc \"%s\"\n\n", set->value.nodes[i]->schema->name); |
| 8432 | break; |
| 8433 | case LYXP_NODE_ROOT_OUTPUT: |
| 8434 | ly_print(&out, "ROOT output \"%s\"\n\n", set->value.nodes[i]->schema->name); |
| 8435 | break; |
| 8436 | case LYXP_NODE_ELEM: |
| 8437 | ly_print(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name); |
| 8438 | xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT); |
| 8439 | ly_print(&out, "\n"); |
| 8440 | break; |
| 8441 | case LYXP_NODE_TEXT: |
| 8442 | ly_print(&out, "TEXT \"%s\"\n\n", ((struct lyd_node_leaf_list *)set->value.nodes[i])->value_str); |
| 8443 | break; |
| 8444 | case LYXP_NODE_ATTR: |
| 8445 | ly_print(&out, "ATTR \"%s\" = \"%s\"\n\n", set->value.attrs[i]->name, set->value.attrs[i]->value); |
| 8446 | break; |
| 8447 | } |
| 8448 | } |
| 8449 | break; |
| 8450 | } |
| 8451 | } |
| 8452 | |
| 8453 | #endif |
| 8454 | |
| 8455 | LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8456 | lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8457 | { |
| 8458 | long double num; |
| 8459 | char *str; |
| 8460 | LY_ERR rc; |
| 8461 | |
| 8462 | if (!set || (set->type == target)) { |
| 8463 | return LY_SUCCESS; |
| 8464 | } |
| 8465 | |
| 8466 | /* it's not possible to convert anything into a node set */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8467 | assert(target != LYXP_SET_NODE_SET); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8468 | |
| 8469 | if (set->type == LYXP_SET_SCNODE_SET) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8470 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8471 | return LY_EINVAL; |
| 8472 | } |
| 8473 | |
| 8474 | /* to STRING */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8475 | 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] | 8476 | switch (set->type) { |
| 8477 | case LYXP_SET_NUMBER: |
| 8478 | if (isnan(set->val.num)) { |
| 8479 | set->val.str = strdup("NaN"); |
| 8480 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 8481 | } else if ((set->val.num == 0) || (set->val.num == -0.0f)) { |
| 8482 | set->val.str = strdup("0"); |
| 8483 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 8484 | } else if (isinf(set->val.num) && !signbit(set->val.num)) { |
| 8485 | set->val.str = strdup("Infinity"); |
| 8486 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 8487 | } else if (isinf(set->val.num) && signbit(set->val.num)) { |
| 8488 | set->val.str = strdup("-Infinity"); |
| 8489 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 8490 | } else if ((long long)set->val.num == set->val.num) { |
| 8491 | if (asprintf(&str, "%lld", (long long)set->val.num) == -1) { |
| 8492 | LOGMEM_RET(set->ctx); |
| 8493 | } |
| 8494 | set->val.str = str; |
| 8495 | } else { |
| 8496 | if (asprintf(&str, "%03.1Lf", set->val.num) == -1) { |
| 8497 | LOGMEM_RET(set->ctx); |
| 8498 | } |
| 8499 | set->val.str = str; |
| 8500 | } |
| 8501 | break; |
| 8502 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8503 | if (set->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8504 | set->val.str = strdup("true"); |
| 8505 | } else { |
| 8506 | set->val.str = strdup("false"); |
| 8507 | } |
| 8508 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM); |
| 8509 | break; |
| 8510 | case LYXP_SET_NODE_SET: |
| 8511 | assert(set->used); |
| 8512 | |
| 8513 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8514 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8515 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8516 | rc = cast_node_set_to_string(set, &str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8517 | LY_CHECK_RET(rc); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8518 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8519 | set->val.str = str; |
| 8520 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8521 | default: |
| 8522 | LOGINT_RET(set->ctx); |
| 8523 | } |
| 8524 | set->type = LYXP_SET_STRING; |
| 8525 | } |
| 8526 | |
| 8527 | /* to NUMBER */ |
| 8528 | if (target == LYXP_SET_NUMBER) { |
| 8529 | switch (set->type) { |
| 8530 | case LYXP_SET_STRING: |
| 8531 | num = cast_string_to_number(set->val.str); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8532 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8533 | set->val.num = num; |
| 8534 | break; |
| 8535 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8536 | if (set->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8537 | set->val.num = 1; |
| 8538 | } else { |
| 8539 | set->val.num = 0; |
| 8540 | } |
| 8541 | break; |
| 8542 | default: |
| 8543 | LOGINT_RET(set->ctx); |
| 8544 | } |
| 8545 | set->type = LYXP_SET_NUMBER; |
| 8546 | } |
| 8547 | |
| 8548 | /* to BOOLEAN */ |
| 8549 | if (target == LYXP_SET_BOOLEAN) { |
| 8550 | switch (set->type) { |
| 8551 | case LYXP_SET_NUMBER: |
| 8552 | 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] | 8553 | set->val.bln = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8554 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8555 | set->val.bln = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8556 | } |
| 8557 | break; |
| 8558 | case LYXP_SET_STRING: |
| 8559 | if (set->val.str[0]) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8560 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8561 | set->val.bln = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8562 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8563 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8564 | set->val.bln = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8565 | } |
| 8566 | break; |
| 8567 | case LYXP_SET_NODE_SET: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8568 | if (set->used) { |
| 8569 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8570 | set->val.bln = 1; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8571 | } else { |
| 8572 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8573 | set->val.bln = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8574 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8575 | break; |
| 8576 | default: |
| 8577 | LOGINT_RET(set->ctx); |
| 8578 | } |
| 8579 | set->type = LYXP_SET_BOOLEAN; |
| 8580 | } |
| 8581 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8582 | return LY_SUCCESS; |
| 8583 | } |
| 8584 | |
| 8585 | LY_ERR |
| 8586 | lyxp_atomize(struct lyxp_expr *exp, LYD_FORMAT format, const struct lys_module *local_mod, const struct lysc_node *ctx_scnode, |
| 8587 | enum lyxp_node_type ctx_scnode_type, struct lyxp_set *set, int options) |
| 8588 | { |
| 8589 | struct ly_ctx *ctx; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8590 | const struct lysc_node *real_ctx_scnode; |
| 8591 | uint16_t tok_idx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8592 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8593 | 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] | 8594 | |
| 8595 | ctx = local_mod->ctx; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8596 | if ((ctx_scnode_type == LYXP_NODE_ROOT) || (ctx_scnode_type == LYXP_NODE_ROOT_CONFIG)) { |
| 8597 | /* we always need some context node because it is used for resolving unqualified names */ |
| 8598 | real_ctx_scnode = NULL; |
| 8599 | } else { |
| 8600 | real_ctx_scnode = ctx_scnode; |
| 8601 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8602 | |
| 8603 | /* prepare set for evaluation */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8604 | tok_idx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8605 | memset(set, 0, sizeof *set); |
| 8606 | set->type = LYXP_SET_SCNODE_SET; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8607 | lyxp_set_scnode_insert_node(set, real_ctx_scnode, ctx_scnode_type); |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 8608 | set->val.scnodes[0].in_ctx = -2; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8609 | set->ctx = ctx; |
| 8610 | set->ctx_scnode = ctx_scnode; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8611 | set->root_type = lyxp_get_root_type(NULL, real_ctx_scnode, options); |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 8612 | for (set->context_op = ctx_scnode; |
| 8613 | set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); |
| 8614 | set->context_op = set->context_op->parent); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8615 | set->local_mod = local_mod; |
| 8616 | set->format = format; |
| 8617 | |
| 8618 | /* evaluate */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8619 | return eval_expr_select(exp, &tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8620 | } |