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 | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 776 | new->local_mod = set->local_mod; |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 777 | new->tree = set->tree; |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 778 | new->format = set->format; |
| 779 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | /** |
| 783 | * @brief Create a deep copy of a set. |
| 784 | * |
| 785 | * @param[in] set Set to copy. |
| 786 | * @return Copy of @p set. |
| 787 | */ |
| 788 | static struct lyxp_set * |
| 789 | set_copy(struct lyxp_set *set) |
| 790 | { |
| 791 | struct lyxp_set *ret; |
| 792 | uint16_t i; |
Michal Vasko | ba71654 | 2019-12-16 10:01:58 +0100 | [diff] [blame] | 793 | int idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 794 | |
| 795 | if (!set) { |
| 796 | return NULL; |
| 797 | } |
| 798 | |
| 799 | ret = malloc(sizeof *ret); |
| 800 | LY_CHECK_ERR_RET(!ret, LOGMEM(set->ctx), NULL); |
| 801 | set_init(ret, set); |
| 802 | |
| 803 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 804 | ret->type = set->type; |
| 805 | |
| 806 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | ba71654 | 2019-12-16 10:01:58 +0100 | [diff] [blame] | 807 | if ((set->val.scnodes[i].in_ctx == 1) || (set->val.scnodes[i].in_ctx == -2)) { |
| 808 | 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] | 809 | /* coverity seems to think scnodes can be NULL */ |
| 810 | if ((idx == -1) || !ret->val.scnodes) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 811 | lyxp_set_free(ret); |
| 812 | return NULL; |
| 813 | } |
Michal Vasko | ba71654 | 2019-12-16 10:01:58 +0100 | [diff] [blame] | 814 | ret->val.scnodes[idx].in_ctx = set->val.scnodes[i].in_ctx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 815 | } |
| 816 | } |
| 817 | } else if (set->type == LYXP_SET_NODE_SET) { |
| 818 | ret->type = set->type; |
| 819 | ret->val.nodes = malloc(set->used * sizeof *ret->val.nodes); |
| 820 | LY_CHECK_ERR_RET(!ret->val.nodes, LOGMEM(set->ctx); free(ret), NULL); |
| 821 | memcpy(ret->val.nodes, set->val.nodes, set->used * sizeof *ret->val.nodes); |
| 822 | |
| 823 | ret->used = ret->size = set->used; |
| 824 | ret->ctx_pos = set->ctx_pos; |
| 825 | ret->ctx_size = set->ctx_size; |
| 826 | ret->ht = lyht_dup(set->ht); |
| 827 | } else { |
| 828 | memcpy(ret, set, sizeof *ret); |
| 829 | if (set->type == LYXP_SET_STRING) { |
| 830 | ret->val.str = strdup(set->val.str); |
| 831 | LY_CHECK_ERR_RET(!ret->val.str, LOGMEM(set->ctx); free(ret), NULL); |
| 832 | } |
| 833 | } |
| 834 | |
| 835 | return ret; |
| 836 | } |
| 837 | |
| 838 | /** |
| 839 | * @brief Fill XPath set with a string. Any current data are disposed of. |
| 840 | * |
| 841 | * @param[in] set Set to fill. |
| 842 | * @param[in] string String to fill into \p set. |
| 843 | * @param[in] str_len Length of \p string. 0 is a valid value! |
| 844 | */ |
| 845 | static void |
| 846 | set_fill_string(struct lyxp_set *set, const char *string, uint16_t str_len) |
| 847 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 848 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 849 | |
| 850 | set->type = LYXP_SET_STRING; |
| 851 | if ((str_len == 0) && (string[0] != '\0')) { |
| 852 | string = ""; |
| 853 | } |
| 854 | set->val.str = strndup(string, str_len); |
| 855 | } |
| 856 | |
| 857 | /** |
| 858 | * @brief Fill XPath set with a number. Any current data are disposed of. |
| 859 | * |
| 860 | * @param[in] set Set to fill. |
| 861 | * @param[in] number Number to fill into \p set. |
| 862 | */ |
| 863 | static void |
| 864 | set_fill_number(struct lyxp_set *set, long double number) |
| 865 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 866 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 867 | |
| 868 | set->type = LYXP_SET_NUMBER; |
| 869 | set->val.num = number; |
| 870 | } |
| 871 | |
| 872 | /** |
| 873 | * @brief Fill XPath set with a boolean. Any current data are disposed of. |
| 874 | * |
| 875 | * @param[in] set Set to fill. |
| 876 | * @param[in] boolean Boolean to fill into \p set. |
| 877 | */ |
| 878 | static void |
| 879 | set_fill_boolean(struct lyxp_set *set, int boolean) |
| 880 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 881 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 882 | |
| 883 | set->type = LYXP_SET_BOOLEAN; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 884 | set->val.bln = boolean; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 885 | } |
| 886 | |
| 887 | /** |
| 888 | * @brief Fill XPath set with the value from another set (deep assign). |
| 889 | * Any current data are disposed of. |
| 890 | * |
| 891 | * @param[in] trg Set to fill. |
| 892 | * @param[in] src Source set to copy into \p trg. |
| 893 | */ |
| 894 | static void |
| 895 | set_fill_set(struct lyxp_set *trg, struct lyxp_set *src) |
| 896 | { |
| 897 | if (!trg || !src) { |
| 898 | return; |
| 899 | } |
| 900 | |
| 901 | if (trg->type == LYXP_SET_NODE_SET) { |
| 902 | free(trg->val.nodes); |
| 903 | } else if (trg->type == LYXP_SET_STRING) { |
| 904 | free(trg->val.str); |
| 905 | } |
| 906 | set_init(trg, src); |
| 907 | |
| 908 | if (src->type == LYXP_SET_SCNODE_SET) { |
| 909 | trg->type = LYXP_SET_SCNODE_SET; |
| 910 | trg->used = src->used; |
| 911 | trg->size = src->used; |
| 912 | |
| 913 | trg->val.scnodes = ly_realloc(trg->val.scnodes, trg->size * sizeof *trg->val.scnodes); |
| 914 | LY_CHECK_ERR_RET(!trg->val.scnodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), ); |
| 915 | memcpy(trg->val.scnodes, src->val.scnodes, src->used * sizeof *src->val.scnodes); |
| 916 | } else if (src->type == LYXP_SET_BOOLEAN) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 917 | set_fill_boolean(trg, src->val.bln); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 918 | } else if (src->type == LYXP_SET_NUMBER) { |
| 919 | set_fill_number(trg, src->val.num); |
| 920 | } else if (src->type == LYXP_SET_STRING) { |
| 921 | set_fill_string(trg, src->val.str, strlen(src->val.str)); |
| 922 | } else { |
| 923 | if (trg->type == LYXP_SET_NODE_SET) { |
| 924 | free(trg->val.nodes); |
| 925 | } else if (trg->type == LYXP_SET_STRING) { |
| 926 | free(trg->val.str); |
| 927 | } |
| 928 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 929 | assert(src->type == LYXP_SET_NODE_SET); |
| 930 | |
| 931 | trg->type = LYXP_SET_NODE_SET; |
| 932 | trg->used = src->used; |
| 933 | trg->size = src->used; |
| 934 | trg->ctx_pos = src->ctx_pos; |
| 935 | trg->ctx_size = src->ctx_size; |
| 936 | |
| 937 | trg->val.nodes = malloc(trg->used * sizeof *trg->val.nodes); |
| 938 | LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), ); |
| 939 | memcpy(trg->val.nodes, src->val.nodes, src->used * sizeof *src->val.nodes); |
| 940 | if (src->ht) { |
| 941 | trg->ht = lyht_dup(src->ht); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 942 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 943 | trg->ht = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 944 | } |
| 945 | } |
| 946 | } |
| 947 | |
| 948 | /** |
| 949 | * @brief Clear context of all schema nodes. |
| 950 | * |
| 951 | * @param[in] set Set to clear. |
| 952 | */ |
| 953 | static void |
| 954 | set_scnode_clear_ctx(struct lyxp_set *set) |
| 955 | { |
| 956 | uint32_t i; |
| 957 | |
| 958 | for (i = 0; i < set->used; ++i) { |
| 959 | if (set->val.scnodes[i].in_ctx == 1) { |
| 960 | set->val.scnodes[i].in_ctx = 0; |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 961 | } else if (set->val.scnodes[i].in_ctx == -2) { |
| 962 | set->val.scnodes[i].in_ctx = -1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 963 | } |
| 964 | } |
| 965 | } |
| 966 | |
| 967 | /** |
| 968 | * @brief Remove a node from a set. Removing last node changes |
| 969 | * set into LYXP_SET_EMPTY. Context position aware. |
| 970 | * |
| 971 | * @param[in] set Set to use. |
| 972 | * @param[in] idx Index from @p set of the node to be removed. |
| 973 | */ |
| 974 | static void |
| 975 | set_remove_node(struct lyxp_set *set, uint32_t idx) |
| 976 | { |
| 977 | assert(set && (set->type == LYXP_SET_NODE_SET)); |
| 978 | assert(idx < set->used); |
| 979 | |
| 980 | set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
| 981 | |
| 982 | --set->used; |
| 983 | if (set->used) { |
| 984 | memmove(&set->val.nodes[idx], &set->val.nodes[idx + 1], |
| 985 | (set->used - idx) * sizeof *set->val.nodes); |
| 986 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 987 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 988 | } |
| 989 | } |
| 990 | |
| 991 | /** |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 992 | * @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] | 993 | * |
| 994 | * @param[in] set Set to use. |
| 995 | * @param[in] idx Index from @p set of the node to be removed. |
| 996 | */ |
| 997 | static void |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 998 | set_remove_node_none(struct lyxp_set *set, uint32_t idx) |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 999 | { |
| 1000 | assert(set && (set->type == LYXP_SET_NODE_SET)); |
| 1001 | assert(idx < set->used); |
| 1002 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1003 | if (set->val.nodes[idx].type == LYXP_NODE_ELEM) { |
| 1004 | set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
| 1005 | } |
| 1006 | set->val.nodes[idx].type = LYXP_NODE_NONE; |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1007 | } |
| 1008 | |
| 1009 | /** |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1010 | * @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] | 1011 | * set into LYXP_SET_EMPTY. Context position aware. |
| 1012 | * |
| 1013 | * @param[in] set Set to consolidate. |
| 1014 | */ |
| 1015 | static void |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1016 | set_remove_nodes_none(struct lyxp_set *set) |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1017 | { |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 1018 | uint16_t i, orig_used, end = 0; |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1019 | int32_t start; |
| 1020 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1021 | assert(set); |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1022 | |
| 1023 | orig_used = set->used; |
| 1024 | set->used = 0; |
| 1025 | for (i = 0; i < orig_used;) { |
| 1026 | start = -1; |
| 1027 | do { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1028 | if ((set->val.nodes[i].type != LYXP_NODE_NONE) && (start == -1)) { |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1029 | start = i; |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1030 | } else if ((start > -1) && (set->val.nodes[i].type == LYXP_NODE_NONE)) { |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1031 | end = i; |
| 1032 | ++i; |
| 1033 | break; |
| 1034 | } |
| 1035 | |
| 1036 | ++i; |
| 1037 | if (i == orig_used) { |
| 1038 | end = i; |
| 1039 | } |
| 1040 | } while (i < orig_used); |
| 1041 | |
| 1042 | if (start > -1) { |
| 1043 | /* move the whole chunk of valid nodes together */ |
| 1044 | if (set->used != (unsigned)start) { |
| 1045 | memmove(&set->val.nodes[set->used], &set->val.nodes[start], (end - start) * sizeof *set->val.nodes); |
| 1046 | } |
| 1047 | set->used += end - start; |
| 1048 | } |
| 1049 | } |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1050 | } |
| 1051 | |
| 1052 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1053 | * @brief Check for duplicates in a node set. |
| 1054 | * |
| 1055 | * @param[in] set Set to check. |
| 1056 | * @param[in] node Node to look for in @p set. |
| 1057 | * @param[in] node_type Type of @p node. |
| 1058 | * @param[in] skip_idx Index from @p set to skip. |
| 1059 | * @return LY_ERR |
| 1060 | */ |
| 1061 | static LY_ERR |
| 1062 | set_dup_node_check(const struct lyxp_set *set, const struct lyd_node *node, enum lyxp_node_type node_type, int skip_idx) |
| 1063 | { |
| 1064 | uint32_t i; |
| 1065 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1066 | if (set->ht && node) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1067 | return set_dup_node_hash_check(set, (struct lyd_node *)node, node_type, skip_idx); |
| 1068 | } |
| 1069 | |
| 1070 | for (i = 0; i < set->used; ++i) { |
| 1071 | if ((skip_idx > -1) && (i == (unsigned)skip_idx)) { |
| 1072 | continue; |
| 1073 | } |
| 1074 | |
| 1075 | if ((set->val.nodes[i].node == node) && (set->val.nodes[i].type == node_type)) { |
| 1076 | return LY_EEXIST; |
| 1077 | } |
| 1078 | } |
| 1079 | |
| 1080 | return LY_SUCCESS; |
| 1081 | } |
| 1082 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 1083 | int |
| 1084 | 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] | 1085 | { |
| 1086 | uint32_t i; |
| 1087 | |
| 1088 | for (i = 0; i < set->used; ++i) { |
| 1089 | if ((skip_idx > -1) && (i == (unsigned)skip_idx)) { |
| 1090 | continue; |
| 1091 | } |
| 1092 | |
| 1093 | if ((set->val.scnodes[i].scnode == node) && (set->val.scnodes[i].type == node_type)) { |
| 1094 | return i; |
| 1095 | } |
| 1096 | } |
| 1097 | |
| 1098 | return -1; |
| 1099 | } |
| 1100 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 1101 | void |
| 1102 | lyxp_set_scnode_merge(struct lyxp_set *set1, struct lyxp_set *set2) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1103 | { |
| 1104 | uint32_t orig_used, i, j; |
| 1105 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1106 | 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] | 1107 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1108 | if (!set2->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1109 | return; |
| 1110 | } |
| 1111 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1112 | if (!set1->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1113 | memcpy(set1, set2, sizeof *set1); |
| 1114 | return; |
| 1115 | } |
| 1116 | |
| 1117 | if (set1->used + set2->used > set1->size) { |
| 1118 | set1->size = set1->used + set2->used; |
| 1119 | set1->val.scnodes = ly_realloc(set1->val.scnodes, set1->size * sizeof *set1->val.scnodes); |
| 1120 | LY_CHECK_ERR_RET(!set1->val.scnodes, LOGMEM(set1->ctx), ); |
| 1121 | } |
| 1122 | |
| 1123 | orig_used = set1->used; |
| 1124 | |
| 1125 | for (i = 0; i < set2->used; ++i) { |
| 1126 | for (j = 0; j < orig_used; ++j) { |
| 1127 | /* detect duplicities */ |
| 1128 | if (set1->val.scnodes[j].scnode == set2->val.scnodes[i].scnode) { |
| 1129 | break; |
| 1130 | } |
| 1131 | } |
| 1132 | |
| 1133 | if (j == orig_used) { |
| 1134 | memcpy(&set1->val.scnodes[set1->used], &set2->val.scnodes[i], sizeof *set2->val.scnodes); |
| 1135 | ++set1->used; |
| 1136 | } |
| 1137 | } |
| 1138 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1139 | lyxp_set_free_content(set2); |
| 1140 | set2->type = LYXP_SET_SCNODE_SET; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1141 | } |
| 1142 | |
| 1143 | /** |
| 1144 | * @brief Insert a node into a set. Context position aware. |
| 1145 | * |
| 1146 | * @param[in] set Set to use. |
| 1147 | * @param[in] node Node to insert to @p set. |
| 1148 | * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting. |
| 1149 | * @param[in] node_type Node type of @p node. |
| 1150 | * @param[in] idx Index in @p set to insert into. |
| 1151 | */ |
| 1152 | static void |
| 1153 | set_insert_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx) |
| 1154 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1155 | assert(set && (set->type == LYXP_SET_NODE_SET)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1156 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1157 | if (!set->size) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1158 | /* first item */ |
| 1159 | if (idx) { |
| 1160 | /* no real harm done, but it is a bug */ |
| 1161 | LOGINT(set->ctx); |
| 1162 | idx = 0; |
| 1163 | } |
| 1164 | set->val.nodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.nodes); |
| 1165 | LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), ); |
| 1166 | set->type = LYXP_SET_NODE_SET; |
| 1167 | set->used = 0; |
| 1168 | set->size = LYXP_SET_SIZE_START; |
| 1169 | set->ctx_pos = 1; |
| 1170 | set->ctx_size = 1; |
| 1171 | set->ht = NULL; |
| 1172 | } else { |
| 1173 | /* not an empty set */ |
| 1174 | if (set->used == set->size) { |
| 1175 | |
| 1176 | /* set is full */ |
| 1177 | set->val.nodes = ly_realloc(set->val.nodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.nodes); |
| 1178 | LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), ); |
| 1179 | set->size += LYXP_SET_SIZE_STEP; |
| 1180 | } |
| 1181 | |
| 1182 | if (idx > set->used) { |
| 1183 | LOGINT(set->ctx); |
| 1184 | idx = set->used; |
| 1185 | } |
| 1186 | |
| 1187 | /* make space for the new node */ |
| 1188 | if (idx < set->used) { |
| 1189 | memmove(&set->val.nodes[idx + 1], &set->val.nodes[idx], (set->used - idx) * sizeof *set->val.nodes); |
| 1190 | } |
| 1191 | } |
| 1192 | |
| 1193 | /* finally assign the value */ |
| 1194 | set->val.nodes[idx].node = (struct lyd_node *)node; |
| 1195 | set->val.nodes[idx].type = node_type; |
| 1196 | set->val.nodes[idx].pos = pos; |
| 1197 | ++set->used; |
| 1198 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1199 | if (set->val.nodes[idx].type == LYXP_NODE_ELEM) { |
| 1200 | set_insert_node_hash(set, (struct lyd_node *)node, node_type); |
| 1201 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1202 | } |
| 1203 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 1204 | int |
| 1205 | 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] | 1206 | { |
| 1207 | int ret; |
| 1208 | |
| 1209 | assert(set->type == LYXP_SET_SCNODE_SET); |
| 1210 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 1211 | ret = lyxp_set_scnode_dup_node_check(set, node, node_type, -1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1212 | if (ret > -1) { |
| 1213 | set->val.scnodes[ret].in_ctx = 1; |
| 1214 | } else { |
| 1215 | if (set->used == set->size) { |
| 1216 | set->val.scnodes = ly_realloc(set->val.scnodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.scnodes); |
| 1217 | LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), -1); |
| 1218 | set->size += LYXP_SET_SIZE_STEP; |
| 1219 | } |
| 1220 | |
| 1221 | ret = set->used; |
| 1222 | set->val.scnodes[ret].scnode = (struct lysc_node *)node; |
| 1223 | set->val.scnodes[ret].type = node_type; |
| 1224 | set->val.scnodes[ret].in_ctx = 1; |
| 1225 | ++set->used; |
| 1226 | } |
| 1227 | |
| 1228 | return ret; |
| 1229 | } |
| 1230 | |
| 1231 | /** |
| 1232 | * @brief Replace a node in a set with another. Context position aware. |
| 1233 | * |
| 1234 | * @param[in] set Set to use. |
| 1235 | * @param[in] node Node to insert to @p set. |
| 1236 | * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting. |
| 1237 | * @param[in] node_type Node type of @p node. |
| 1238 | * @param[in] idx Index in @p set of the node to replace. |
| 1239 | */ |
| 1240 | static void |
| 1241 | set_replace_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx) |
| 1242 | { |
| 1243 | assert(set && (idx < set->used)); |
| 1244 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1245 | if (set->val.nodes[idx].type == LYXP_NODE_ELEM) { |
| 1246 | set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
| 1247 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1248 | set->val.nodes[idx].node = (struct lyd_node *)node; |
| 1249 | set->val.nodes[idx].type = node_type; |
| 1250 | set->val.nodes[idx].pos = pos; |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1251 | if (set->val.nodes[idx].type == LYXP_NODE_ELEM) { |
| 1252 | set_insert_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
| 1253 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1254 | } |
| 1255 | |
| 1256 | /** |
| 1257 | * @brief Set all nodes with ctx 1 to a new unique context value. |
| 1258 | * |
| 1259 | * @param[in] set Set to modify. |
| 1260 | * @return New context value. |
| 1261 | */ |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 1262 | static int32_t |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1263 | set_scnode_new_in_ctx(struct lyxp_set *set) |
| 1264 | { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 1265 | uint32_t i; |
| 1266 | int32_t ret_ctx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1267 | |
| 1268 | assert(set->type == LYXP_SET_SCNODE_SET); |
| 1269 | |
| 1270 | ret_ctx = 3; |
| 1271 | retry: |
| 1272 | for (i = 0; i < set->used; ++i) { |
| 1273 | if (set->val.scnodes[i].in_ctx >= ret_ctx) { |
| 1274 | ret_ctx = set->val.scnodes[i].in_ctx + 1; |
| 1275 | goto retry; |
| 1276 | } |
| 1277 | } |
| 1278 | for (i = 0; i < set->used; ++i) { |
| 1279 | if (set->val.scnodes[i].in_ctx == 1) { |
| 1280 | set->val.scnodes[i].in_ctx = ret_ctx; |
| 1281 | } |
| 1282 | } |
| 1283 | |
| 1284 | return ret_ctx; |
| 1285 | } |
| 1286 | |
| 1287 | /** |
| 1288 | * @brief Get unique @p node position in the data. |
| 1289 | * |
| 1290 | * @param[in] node Node to find. |
| 1291 | * @param[in] node_type Node type of @p node. |
| 1292 | * @param[in] root Root node. |
| 1293 | * @param[in] root_type Type of the XPath @p root node. |
| 1294 | * @param[in] prev Node that we think is before @p node in DFS from @p root. Can optionally |
| 1295 | * be used to increase efficiency and start the DFS from this node. |
| 1296 | * @param[in] prev_pos Node @p prev position. Optional, but must be set if @p prev is set. |
| 1297 | * @return Node position. |
| 1298 | */ |
| 1299 | static uint32_t |
| 1300 | get_node_pos(const struct lyd_node *node, enum lyxp_node_type node_type, const struct lyd_node *root, |
| 1301 | enum lyxp_node_type root_type, const struct lyd_node **prev, uint32_t *prev_pos) |
| 1302 | { |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 1303 | const struct lyd_node *next, *elem = NULL, *top_sibling; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1304 | uint32_t pos = 1; |
| 1305 | |
| 1306 | assert(prev && prev_pos && !root->prev->next); |
| 1307 | |
| 1308 | if ((node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ROOT_CONFIG)) { |
| 1309 | return 0; |
| 1310 | } |
| 1311 | |
| 1312 | if (*prev) { |
| 1313 | /* start from the previous element instead from the root */ |
| 1314 | elem = next = *prev; |
| 1315 | pos = *prev_pos; |
| 1316 | for (top_sibling = elem; top_sibling->parent; top_sibling = (struct lyd_node *)top_sibling->parent); |
| 1317 | goto dfs_search; |
| 1318 | } |
| 1319 | |
| 1320 | for (top_sibling = root; top_sibling; top_sibling = top_sibling->next) { |
| 1321 | /* TREE DFS */ |
| 1322 | LYD_TREE_DFS_BEGIN(top_sibling, next, elem) { |
| 1323 | dfs_search: |
| 1324 | if ((root_type == LYXP_NODE_ROOT_CONFIG) && (elem->schema->flags & LYS_CONFIG_R)) { |
| 1325 | goto skip_children; |
| 1326 | } |
| 1327 | |
| 1328 | if (elem == node) { |
| 1329 | break; |
| 1330 | } |
| 1331 | ++pos; |
| 1332 | |
| 1333 | /* TREE DFS END */ |
| 1334 | /* select element for the next run - children first, |
| 1335 | * child exception for lyd_node_leaf and lyd_node_leaflist, but not the root */ |
| 1336 | if (elem->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
| 1337 | next = NULL; |
| 1338 | } else { |
Michal Vasko | 5bfd4be | 2020-06-23 13:26:19 +0200 | [diff] [blame] | 1339 | next = lyd_node_children(elem, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1340 | } |
| 1341 | if (!next) { |
| 1342 | skip_children: |
| 1343 | /* no children */ |
| 1344 | if (elem == top_sibling) { |
| 1345 | /* we are done, root has no children */ |
| 1346 | elem = NULL; |
| 1347 | break; |
| 1348 | } |
| 1349 | /* try siblings */ |
| 1350 | next = elem->next; |
| 1351 | } |
| 1352 | while (!next) { |
| 1353 | /* no siblings, go back through parents */ |
| 1354 | if (elem->parent == top_sibling->parent) { |
| 1355 | /* we are done, no next element to process */ |
| 1356 | elem = NULL; |
| 1357 | break; |
| 1358 | } |
| 1359 | /* parent is already processed, go to its sibling */ |
| 1360 | elem = (struct lyd_node *)elem->parent; |
| 1361 | next = elem->next; |
| 1362 | } |
| 1363 | } |
| 1364 | |
| 1365 | /* node found */ |
| 1366 | if (elem) { |
| 1367 | break; |
| 1368 | } |
| 1369 | } |
| 1370 | |
| 1371 | if (!elem) { |
| 1372 | if (!(*prev)) { |
| 1373 | /* we went from root and failed to find it, cannot be */ |
| 1374 | LOGINT(node->schema->module->ctx); |
| 1375 | return 0; |
| 1376 | } else { |
| 1377 | *prev = NULL; |
| 1378 | *prev_pos = 0; |
| 1379 | |
| 1380 | elem = next = top_sibling = root; |
| 1381 | pos = 1; |
| 1382 | goto dfs_search; |
| 1383 | } |
| 1384 | } |
| 1385 | |
| 1386 | /* remember the last found node for next time */ |
| 1387 | *prev = node; |
| 1388 | *prev_pos = pos; |
| 1389 | |
| 1390 | return pos; |
| 1391 | } |
| 1392 | |
| 1393 | /** |
| 1394 | * @brief Assign (fill) missing node positions. |
| 1395 | * |
| 1396 | * @param[in] set Set to fill positions in. |
| 1397 | * @param[in] root Context root node. |
| 1398 | * @param[in] root_type Context root type. |
| 1399 | * @return LY_ERR |
| 1400 | */ |
| 1401 | static LY_ERR |
| 1402 | set_assign_pos(struct lyxp_set *set, const struct lyd_node *root, enum lyxp_node_type root_type) |
| 1403 | { |
| 1404 | const struct lyd_node *prev = NULL, *tmp_node; |
| 1405 | uint32_t i, tmp_pos = 0; |
| 1406 | |
| 1407 | for (i = 0; i < set->used; ++i) { |
| 1408 | if (!set->val.nodes[i].pos) { |
| 1409 | tmp_node = NULL; |
| 1410 | switch (set->val.nodes[i].type) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1411 | case LYXP_NODE_META: |
| 1412 | tmp_node = set->val.meta[i].meta->parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1413 | if (!tmp_node) { |
| 1414 | LOGINT_RET(root->schema->module->ctx); |
| 1415 | } |
| 1416 | /* fallthrough */ |
| 1417 | case LYXP_NODE_ELEM: |
| 1418 | case LYXP_NODE_TEXT: |
| 1419 | if (!tmp_node) { |
| 1420 | tmp_node = set->val.nodes[i].node; |
| 1421 | } |
| 1422 | set->val.nodes[i].pos = get_node_pos(tmp_node, set->val.nodes[i].type, root, root_type, &prev, &tmp_pos); |
| 1423 | break; |
| 1424 | default: |
| 1425 | /* all roots have position 0 */ |
| 1426 | break; |
| 1427 | } |
| 1428 | } |
| 1429 | } |
| 1430 | |
| 1431 | return LY_SUCCESS; |
| 1432 | } |
| 1433 | |
| 1434 | /** |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1435 | * @brief Get unique @p meta position in the parent metadata. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1436 | * |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1437 | * @param[in] meta Metadata to use. |
| 1438 | * @return Metadata position. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1439 | */ |
| 1440 | static uint16_t |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1441 | get_meta_pos(struct lyd_meta *meta) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1442 | { |
| 1443 | uint16_t pos = 0; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1444 | struct lyd_meta *meta2; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1445 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1446 | for (meta2 = meta->parent->meta; meta2 && (meta2 != meta); meta2 = meta2->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1447 | ++pos; |
| 1448 | } |
| 1449 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1450 | assert(meta2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1451 | return pos; |
| 1452 | } |
| 1453 | |
| 1454 | /** |
| 1455 | * @brief Compare 2 nodes in respect to XPath document order. |
| 1456 | * |
| 1457 | * @param[in] item1 1st node. |
| 1458 | * @param[in] item2 2nd node. |
| 1459 | * @return If 1st > 2nd returns 1, 1st == 2nd returns 0, and 1st < 2nd returns -1. |
| 1460 | */ |
| 1461 | static int |
| 1462 | set_sort_compare(struct lyxp_set_node *item1, struct lyxp_set_node *item2) |
| 1463 | { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1464 | uint32_t meta_pos1 = 0, meta_pos2 = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1465 | |
| 1466 | if (item1->pos < item2->pos) { |
| 1467 | return -1; |
| 1468 | } |
| 1469 | |
| 1470 | if (item1->pos > item2->pos) { |
| 1471 | return 1; |
| 1472 | } |
| 1473 | |
| 1474 | /* node positions are equal, the fun case */ |
| 1475 | |
| 1476 | /* 1st ELEM - == - 2nd TEXT, 1st TEXT - == - 2nd ELEM */ |
| 1477 | /* special case since text nodes are actually saved as their parents */ |
| 1478 | if ((item1->node == item2->node) && (item1->type != item2->type)) { |
| 1479 | if (item1->type == LYXP_NODE_ELEM) { |
| 1480 | assert(item2->type == LYXP_NODE_TEXT); |
| 1481 | return -1; |
| 1482 | } else { |
| 1483 | assert((item1->type == LYXP_NODE_TEXT) && (item2->type == LYXP_NODE_ELEM)); |
| 1484 | return 1; |
| 1485 | } |
| 1486 | } |
| 1487 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1488 | /* we need meta positions now */ |
| 1489 | if (item1->type == LYXP_NODE_META) { |
| 1490 | meta_pos1 = get_meta_pos((struct lyd_meta *)item1->node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1491 | } |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1492 | if (item2->type == LYXP_NODE_META) { |
| 1493 | meta_pos2 = get_meta_pos((struct lyd_meta *)item2->node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1494 | } |
| 1495 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1496 | /* 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] | 1497 | /* check for duplicates */ |
| 1498 | if (item1->node == item2->node) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1499 | 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] | 1500 | return 0; |
| 1501 | } |
| 1502 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1503 | /* 1st ELEM - 2nd TEXT, 1st ELEM - any pos - 2nd META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1504 | /* elem is always first, 2nd node is after it */ |
| 1505 | if (item1->type == LYXP_NODE_ELEM) { |
| 1506 | assert(item2->type != LYXP_NODE_ELEM); |
| 1507 | return -1; |
| 1508 | } |
| 1509 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1510 | /* 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] | 1511 | /* 2nd is before 1st */ |
| 1512 | if (((item1->type == LYXP_NODE_TEXT) |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1513 | && ((item2->type == LYXP_NODE_ELEM) || (item2->type == LYXP_NODE_META))) |
| 1514 | || ((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_ELEM)) |
| 1515 | || (((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_META)) |
| 1516 | && (meta_pos1 > meta_pos2))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1517 | return 1; |
| 1518 | } |
| 1519 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1520 | /* 1st META - any pos - 2nd TEXT, 1st META <pos< - 2nd META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1521 | /* 2nd is after 1st */ |
| 1522 | return -1; |
| 1523 | } |
| 1524 | |
| 1525 | /** |
| 1526 | * @brief Set cast for comparisons. |
| 1527 | * |
| 1528 | * @param[in] trg Target set to cast source into. |
| 1529 | * @param[in] src Source set. |
| 1530 | * @param[in] type Target set type. |
| 1531 | * @param[in] src_idx Source set node index. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1532 | * @return LY_ERR |
| 1533 | */ |
| 1534 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1535 | 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] | 1536 | { |
| 1537 | assert(src->type == LYXP_SET_NODE_SET); |
| 1538 | |
| 1539 | set_init(trg, src); |
| 1540 | |
| 1541 | /* insert node into target set */ |
| 1542 | set_insert_node(trg, src->val.nodes[src_idx].node, src->val.nodes[src_idx].pos, src->val.nodes[src_idx].type, 0); |
| 1543 | |
| 1544 | /* cast target set appropriately */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1545 | return lyxp_set_cast(trg, type); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1546 | } |
| 1547 | |
| 1548 | #ifndef NDEBUG |
| 1549 | |
| 1550 | /** |
| 1551 | * @brief Bubble sort @p set into XPath document order. |
| 1552 | * Context position aware. Unused in the 'Release' build target. |
| 1553 | * |
| 1554 | * @param[in] set Set to sort. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1555 | * @return How many times the whole set was traversed - 1 (if set was sorted, returns 0). |
| 1556 | */ |
| 1557 | static int |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1558 | set_sort(struct lyxp_set *set) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1559 | { |
| 1560 | uint32_t i, j; |
| 1561 | int ret = 0, cmp, inverted, change; |
| 1562 | const struct lyd_node *root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1563 | struct lyxp_set_node item; |
| 1564 | struct lyxp_set_hash_node hnode; |
| 1565 | uint64_t hash; |
| 1566 | |
Michal Vasko | 3cf8fbf | 2020-05-27 15:21:21 +0200 | [diff] [blame] | 1567 | if ((set->type != LYXP_SET_NODE_SET) || (set->used < 2)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1568 | return 0; |
| 1569 | } |
| 1570 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1571 | /* find first top-level node to be used as anchor for positions */ |
| 1572 | for (root = set->ctx_node; root->parent; root = (const struct lyd_node *)root->parent); |
| 1573 | for (; root->prev->next; root = root->prev); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1574 | |
| 1575 | /* fill positions */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1576 | if (set_assign_pos(set, root, set->root_type)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1577 | return -1; |
| 1578 | } |
| 1579 | |
| 1580 | LOGDBG(LY_LDGXPATH, "SORT BEGIN"); |
| 1581 | print_set_debug(set); |
| 1582 | |
| 1583 | for (i = 0; i < set->used; ++i) { |
| 1584 | inverted = 0; |
| 1585 | change = 0; |
| 1586 | |
| 1587 | for (j = 1; j < set->used - i; ++j) { |
| 1588 | /* compare node positions */ |
| 1589 | if (inverted) { |
| 1590 | cmp = set_sort_compare(&set->val.nodes[j], &set->val.nodes[j - 1]); |
| 1591 | } else { |
| 1592 | cmp = set_sort_compare(&set->val.nodes[j - 1], &set->val.nodes[j]); |
| 1593 | } |
| 1594 | |
| 1595 | /* swap if needed */ |
| 1596 | if ((inverted && (cmp < 0)) || (!inverted && (cmp > 0))) { |
| 1597 | change = 1; |
| 1598 | |
| 1599 | item = set->val.nodes[j - 1]; |
| 1600 | set->val.nodes[j - 1] = set->val.nodes[j]; |
| 1601 | set->val.nodes[j] = item; |
| 1602 | } else { |
| 1603 | /* whether node_pos1 should be smaller than node_pos2 or the other way around */ |
| 1604 | inverted = !inverted; |
| 1605 | } |
| 1606 | } |
| 1607 | |
| 1608 | ++ret; |
| 1609 | |
| 1610 | if (!change) { |
| 1611 | break; |
| 1612 | } |
| 1613 | } |
| 1614 | |
| 1615 | LOGDBG(LY_LDGXPATH, "SORT END %d", ret); |
| 1616 | print_set_debug(set); |
| 1617 | |
| 1618 | /* check node hashes */ |
| 1619 | if (set->used >= LYD_HT_MIN_ITEMS) { |
| 1620 | assert(set->ht); |
| 1621 | for (i = 0; i < set->used; ++i) { |
| 1622 | hnode.node = set->val.nodes[i].node; |
| 1623 | hnode.type = set->val.nodes[i].type; |
| 1624 | |
| 1625 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 1626 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 1627 | hash = dict_hash_multi(hash, NULL, 0); |
| 1628 | |
| 1629 | assert(!lyht_find(set->ht, &hnode, hash, NULL)); |
| 1630 | } |
| 1631 | } |
| 1632 | |
| 1633 | return ret - 1; |
| 1634 | } |
| 1635 | |
| 1636 | /** |
| 1637 | * @brief Remove duplicate entries in a sorted node set. |
| 1638 | * |
| 1639 | * @param[in] set Sorted set to check. |
| 1640 | * @return LY_ERR (LY_EEXIST if some duplicates are found) |
| 1641 | */ |
| 1642 | static LY_ERR |
| 1643 | set_sorted_dup_node_clean(struct lyxp_set *set) |
| 1644 | { |
| 1645 | uint32_t i = 0; |
| 1646 | LY_ERR ret = LY_SUCCESS; |
| 1647 | |
| 1648 | if (set->used > 1) { |
| 1649 | while (i < set->used - 1) { |
| 1650 | if ((set->val.nodes[i].node == set->val.nodes[i + 1].node) |
| 1651 | && (set->val.nodes[i].type == set->val.nodes[i + 1].type)) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1652 | set_remove_node_none(set, i + 1); |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1653 | ret = LY_EEXIST; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1654 | } |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1655 | ++i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1656 | } |
| 1657 | } |
| 1658 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1659 | set_remove_nodes_none(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1660 | return ret; |
| 1661 | } |
| 1662 | |
| 1663 | #endif |
| 1664 | |
| 1665 | /** |
| 1666 | * @brief Merge 2 sorted sets into one. |
| 1667 | * |
| 1668 | * @param[in,out] trg Set to merge into. Duplicates are removed. |
| 1669 | * @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] | 1670 | * @return LY_ERR |
| 1671 | */ |
| 1672 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1673 | set_sorted_merge(struct lyxp_set *trg, struct lyxp_set *src) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1674 | { |
| 1675 | uint32_t i, j, k, count, dup_count; |
| 1676 | int cmp; |
| 1677 | const struct lyd_node *root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1678 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1679 | 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] | 1680 | return LY_EINVAL; |
| 1681 | } |
| 1682 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1683 | if (!src->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1684 | return LY_SUCCESS; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1685 | } else if (!trg->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1686 | set_fill_set(trg, src); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1687 | lyxp_set_free_content(src); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1688 | return LY_SUCCESS; |
| 1689 | } |
| 1690 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1691 | /* find first top-level node to be used as anchor for positions */ |
| 1692 | for (root = trg->ctx_node; root->parent; root = (const struct lyd_node *)root->parent); |
| 1693 | for (; root->prev->next; root = root->prev); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1694 | |
| 1695 | /* fill positions */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1696 | 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] | 1697 | return LY_EINT; |
| 1698 | } |
| 1699 | |
| 1700 | #ifndef NDEBUG |
| 1701 | LOGDBG(LY_LDGXPATH, "MERGE target"); |
| 1702 | print_set_debug(trg); |
| 1703 | LOGDBG(LY_LDGXPATH, "MERGE source"); |
| 1704 | print_set_debug(src); |
| 1705 | #endif |
| 1706 | |
| 1707 | /* make memory for the merge (duplicates are not detected yet, so space |
| 1708 | * will likely be wasted on them, too bad) */ |
| 1709 | if (trg->size - trg->used < src->used) { |
| 1710 | trg->size = trg->used + src->used; |
| 1711 | |
| 1712 | trg->val.nodes = ly_realloc(trg->val.nodes, trg->size * sizeof *trg->val.nodes); |
| 1713 | LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx), LY_EMEM); |
| 1714 | } |
| 1715 | |
| 1716 | i = 0; |
| 1717 | j = 0; |
| 1718 | count = 0; |
| 1719 | dup_count = 0; |
| 1720 | do { |
| 1721 | cmp = set_sort_compare(&src->val.nodes[i], &trg->val.nodes[j]); |
| 1722 | if (!cmp) { |
| 1723 | if (!count) { |
| 1724 | /* duplicate, just skip it */ |
| 1725 | ++i; |
| 1726 | ++j; |
| 1727 | } else { |
| 1728 | /* we are copying something already, so let's copy the duplicate too, |
| 1729 | * we are hoping that afterwards there are some more nodes to |
| 1730 | * copy and this way we can copy them all together */ |
| 1731 | ++count; |
| 1732 | ++dup_count; |
| 1733 | ++i; |
| 1734 | ++j; |
| 1735 | } |
| 1736 | } else if (cmp < 0) { |
| 1737 | /* inserting src node into trg, just remember it for now */ |
| 1738 | ++count; |
| 1739 | ++i; |
| 1740 | |
| 1741 | /* insert the hash now */ |
| 1742 | set_insert_node_hash(trg, src->val.nodes[i - 1].node, src->val.nodes[i - 1].type); |
| 1743 | } else if (count) { |
| 1744 | copy_nodes: |
| 1745 | /* time to actually copy the nodes, we have found the largest block of nodes */ |
| 1746 | memmove(&trg->val.nodes[j + (count - dup_count)], |
| 1747 | &trg->val.nodes[j], |
| 1748 | (trg->used - j) * sizeof *trg->val.nodes); |
| 1749 | memcpy(&trg->val.nodes[j - dup_count], &src->val.nodes[i - count], count * sizeof *src->val.nodes); |
| 1750 | |
| 1751 | trg->used += count - dup_count; |
| 1752 | /* do not change i, except the copying above, we are basically doing exactly what is in the else branch below */ |
| 1753 | j += count - dup_count; |
| 1754 | |
| 1755 | count = 0; |
| 1756 | dup_count = 0; |
| 1757 | } else { |
| 1758 | ++j; |
| 1759 | } |
| 1760 | } while ((i < src->used) && (j < trg->used)); |
| 1761 | |
| 1762 | if ((i < src->used) || count) { |
| 1763 | /* insert all the hashes first */ |
| 1764 | for (k = i; k < src->used; ++k) { |
| 1765 | set_insert_node_hash(trg, src->val.nodes[k].node, src->val.nodes[k].type); |
| 1766 | } |
| 1767 | |
| 1768 | /* loop ended, but we need to copy something at trg end */ |
| 1769 | count += src->used - i; |
| 1770 | i = src->used; |
| 1771 | goto copy_nodes; |
| 1772 | } |
| 1773 | |
| 1774 | /* we are inserting hashes before the actual node insert, which causes |
| 1775 | * situations when there were initially not enough items for a hash table, |
| 1776 | * but even after some were inserted, hash table was not created (during |
| 1777 | * insertion the number of items is not updated yet) */ |
| 1778 | if (!trg->ht && (trg->used >= LYD_HT_MIN_ITEMS)) { |
| 1779 | set_insert_node_hash(trg, NULL, 0); |
| 1780 | } |
| 1781 | |
| 1782 | #ifndef NDEBUG |
| 1783 | LOGDBG(LY_LDGXPATH, "MERGE result"); |
| 1784 | print_set_debug(trg); |
| 1785 | #endif |
| 1786 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1787 | lyxp_set_free_content(src); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1788 | return LY_SUCCESS; |
| 1789 | } |
| 1790 | |
| 1791 | /* |
| 1792 | * (re)parse functions |
| 1793 | * |
| 1794 | * Parse functions parse the expression into |
| 1795 | * tokens (syntactic analysis). |
| 1796 | * |
| 1797 | * Reparse functions perform semantic analysis |
| 1798 | * (do not save the result, just a check) of |
| 1799 | * the expression and fill repeat indices. |
| 1800 | */ |
| 1801 | |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1802 | LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1803 | 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] | 1804 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1805 | if (exp->used == tok_idx) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1806 | if (ctx) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1807 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF); |
| 1808 | } |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1809 | return LY_EINCOMPLETE; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1810 | } |
| 1811 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1812 | if (want_tok && (exp->tokens[tok_idx] != want_tok)) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1813 | if (ctx) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1814 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1815 | 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] | 1816 | } |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1817 | return LY_ENOT; |
| 1818 | } |
| 1819 | |
| 1820 | return LY_SUCCESS; |
| 1821 | } |
| 1822 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1823 | LY_ERR |
| 1824 | lyxp_next_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok) |
| 1825 | { |
| 1826 | LY_CHECK_RET(lyxp_check_token(ctx, exp, *tok_idx, want_tok)); |
| 1827 | |
| 1828 | /* skip the token */ |
| 1829 | ++(*tok_idx); |
| 1830 | |
| 1831 | return LY_SUCCESS; |
| 1832 | } |
| 1833 | |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1834 | /* just like lyxp_check_token() but tests for 2 tokens */ |
| 1835 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1836 | 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] | 1837 | enum lyxp_token want_tok2) |
| 1838 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1839 | if (exp->used == tok_idx) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1840 | if (ctx) { |
| 1841 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF); |
| 1842 | } |
| 1843 | return LY_EINCOMPLETE; |
| 1844 | } |
| 1845 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1846 | 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] | 1847 | if (ctx) { |
| 1848 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1849 | 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] | 1850 | } |
| 1851 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1852 | } |
| 1853 | |
| 1854 | return LY_SUCCESS; |
| 1855 | } |
| 1856 | |
| 1857 | /** |
| 1858 | * @brief Stack operation push on the repeat array. |
| 1859 | * |
| 1860 | * @param[in] exp Expression to use. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1861 | * @param[in] tok_idx Position in the expresion \p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1862 | * @param[in] repeat_op_idx Index from \p exp of the operator token. This value is pushed. |
| 1863 | */ |
| 1864 | static void |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1865 | 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] | 1866 | { |
| 1867 | uint16_t i; |
| 1868 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1869 | if (exp->repeat[tok_idx]) { |
| 1870 | for (i = 0; exp->repeat[tok_idx][i]; ++i); |
| 1871 | exp->repeat[tok_idx] = realloc(exp->repeat[tok_idx], (i + 2) * sizeof *exp->repeat[tok_idx]); |
| 1872 | LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), ); |
| 1873 | exp->repeat[tok_idx][i] = repeat_op_idx; |
| 1874 | exp->repeat[tok_idx][i + 1] = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1875 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1876 | exp->repeat[tok_idx] = calloc(2, sizeof *exp->repeat[tok_idx]); |
| 1877 | LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), ); |
| 1878 | exp->repeat[tok_idx][0] = repeat_op_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1879 | } |
| 1880 | } |
| 1881 | |
| 1882 | /** |
| 1883 | * @brief Reparse Predicate. Logs directly on error. |
| 1884 | * |
| 1885 | * [7] Predicate ::= '[' Expr ']' |
| 1886 | * |
| 1887 | * @param[in] ctx Context for logging. |
| 1888 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1889 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1890 | * @return LY_ERR |
| 1891 | */ |
| 1892 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1893 | 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] | 1894 | { |
| 1895 | LY_ERR rc; |
| 1896 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1897 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1898 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1899 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1900 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1901 | rc = reparse_or_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1902 | LY_CHECK_RET(rc); |
| 1903 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1904 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1905 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1906 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1907 | |
| 1908 | return LY_SUCCESS; |
| 1909 | } |
| 1910 | |
| 1911 | /** |
| 1912 | * @brief Reparse RelativeLocationPath. Logs directly on error. |
| 1913 | * |
| 1914 | * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step |
| 1915 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 1916 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 1917 | * |
| 1918 | * @param[in] ctx Context for logging. |
| 1919 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1920 | * @param[in] tok_idx Position in the expression \p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1921 | * @return LY_ERR (LY_EINCOMPLETE on forward reference) |
| 1922 | */ |
| 1923 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1924 | 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] | 1925 | { |
| 1926 | LY_ERR rc; |
| 1927 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1928 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1929 | LY_CHECK_RET(rc); |
| 1930 | |
| 1931 | goto step; |
| 1932 | do { |
| 1933 | /* '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1934 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1935 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1936 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1937 | LY_CHECK_RET(rc); |
| 1938 | step: |
| 1939 | /* Step */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1940 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1941 | case LYXP_TOKEN_DOT: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1942 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1943 | break; |
| 1944 | |
| 1945 | case LYXP_TOKEN_DDOT: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1946 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1947 | break; |
| 1948 | |
| 1949 | case LYXP_TOKEN_AT: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1950 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1951 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1952 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1953 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1954 | 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] | 1955 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1956 | 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] | 1957 | return LY_EVALID; |
| 1958 | } |
| 1959 | /* fall through */ |
| 1960 | case LYXP_TOKEN_NAMETEST: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1961 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1962 | goto reparse_predicate; |
| 1963 | break; |
| 1964 | |
| 1965 | case LYXP_TOKEN_NODETYPE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1966 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1967 | |
| 1968 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1969 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1970 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1971 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1972 | |
| 1973 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1974 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1975 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1976 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1977 | |
| 1978 | reparse_predicate: |
| 1979 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1980 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 1981 | rc = reparse_predicate(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1982 | LY_CHECK_RET(rc); |
| 1983 | } |
| 1984 | break; |
| 1985 | default: |
| 1986 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1987 | 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] | 1988 | return LY_EVALID; |
| 1989 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1990 | } 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] | 1991 | |
| 1992 | return LY_SUCCESS; |
| 1993 | } |
| 1994 | |
| 1995 | /** |
| 1996 | * @brief Reparse AbsoluteLocationPath. Logs directly on error. |
| 1997 | * |
| 1998 | * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath |
| 1999 | * |
| 2000 | * @param[in] ctx Context for logging. |
| 2001 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2002 | * @param[in] tok_idx Position in the expression \p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2003 | * @return LY_ERR |
| 2004 | */ |
| 2005 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2006 | 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] | 2007 | { |
| 2008 | LY_ERR rc; |
| 2009 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2010 | 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] | 2011 | |
| 2012 | /* '/' RelativeLocationPath? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2013 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2014 | /* '/' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2015 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2016 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2017 | if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2018 | return LY_SUCCESS; |
| 2019 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2020 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2021 | case LYXP_TOKEN_DOT: |
| 2022 | case LYXP_TOKEN_DDOT: |
| 2023 | case LYXP_TOKEN_AT: |
| 2024 | case LYXP_TOKEN_NAMETEST: |
| 2025 | case LYXP_TOKEN_NODETYPE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2026 | rc = reparse_relative_location_path(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2027 | LY_CHECK_RET(rc); |
| 2028 | /* fall through */ |
| 2029 | default: |
| 2030 | break; |
| 2031 | } |
| 2032 | |
| 2033 | /* '//' RelativeLocationPath */ |
| 2034 | } else { |
| 2035 | /* '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2036 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2037 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2038 | rc = reparse_relative_location_path(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2039 | LY_CHECK_RET(rc); |
| 2040 | } |
| 2041 | |
| 2042 | return LY_SUCCESS; |
| 2043 | } |
| 2044 | |
| 2045 | /** |
| 2046 | * @brief Reparse FunctionCall. Logs directly on error. |
| 2047 | * |
| 2048 | * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')' |
| 2049 | * |
| 2050 | * @param[in] ctx Context for logging. |
| 2051 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2052 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2053 | * @return LY_ERR |
| 2054 | */ |
| 2055 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2056 | 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] | 2057 | { |
| 2058 | int min_arg_count = -1, max_arg_count, arg_count; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2059 | uint16_t func_tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2060 | LY_ERR rc; |
| 2061 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2062 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2063 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2064 | func_tok_idx = *tok_idx; |
| 2065 | switch (exp->tok_len[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2066 | case 3: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2067 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2068 | min_arg_count = 1; |
| 2069 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2070 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2071 | min_arg_count = 1; |
| 2072 | max_arg_count = 1; |
| 2073 | } |
| 2074 | break; |
| 2075 | case 4: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2076 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2077 | min_arg_count = 1; |
| 2078 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2079 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2080 | min_arg_count = 0; |
| 2081 | max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2082 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2083 | min_arg_count = 0; |
| 2084 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2085 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2086 | min_arg_count = 0; |
| 2087 | max_arg_count = 0; |
| 2088 | } |
| 2089 | break; |
| 2090 | case 5: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2091 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2092 | min_arg_count = 1; |
| 2093 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2094 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2095 | min_arg_count = 0; |
| 2096 | max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2097 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2098 | min_arg_count = 1; |
| 2099 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2100 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2101 | min_arg_count = 1; |
| 2102 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2103 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2104 | min_arg_count = 1; |
| 2105 | max_arg_count = 1; |
| 2106 | } |
| 2107 | break; |
| 2108 | case 6: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2109 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2110 | min_arg_count = 2; |
Michal Vasko | be2e356 | 2019-10-15 15:35:35 +0200 | [diff] [blame] | 2111 | max_arg_count = INT_MAX; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2112 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2113 | min_arg_count = 0; |
| 2114 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2115 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2116 | min_arg_count = 0; |
| 2117 | max_arg_count = 1; |
| 2118 | } |
| 2119 | break; |
| 2120 | case 7: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2121 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2122 | min_arg_count = 1; |
| 2123 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2124 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2125 | min_arg_count = 1; |
| 2126 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2127 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2128 | min_arg_count = 0; |
| 2129 | max_arg_count = 0; |
| 2130 | } |
| 2131 | break; |
| 2132 | case 8: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2133 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2134 | min_arg_count = 2; |
| 2135 | max_arg_count = 2; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2136 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2137 | min_arg_count = 0; |
| 2138 | max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2139 | } 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] | 2140 | min_arg_count = 2; |
| 2141 | max_arg_count = 2; |
| 2142 | } |
| 2143 | break; |
| 2144 | case 9: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2145 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2146 | min_arg_count = 2; |
| 2147 | max_arg_count = 3; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2148 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2149 | min_arg_count = 3; |
| 2150 | max_arg_count = 3; |
| 2151 | } |
| 2152 | break; |
| 2153 | case 10: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2154 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2155 | min_arg_count = 0; |
| 2156 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2157 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2158 | min_arg_count = 1; |
| 2159 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2160 | } 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] | 2161 | min_arg_count = 2; |
| 2162 | max_arg_count = 2; |
| 2163 | } |
| 2164 | break; |
| 2165 | case 11: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2166 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2167 | min_arg_count = 2; |
| 2168 | max_arg_count = 2; |
| 2169 | } |
| 2170 | break; |
| 2171 | case 12: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2172 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2173 | min_arg_count = 2; |
| 2174 | max_arg_count = 2; |
| 2175 | } |
| 2176 | break; |
| 2177 | case 13: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2178 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2179 | min_arg_count = 0; |
| 2180 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2181 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2182 | min_arg_count = 0; |
| 2183 | max_arg_count = 1; |
| 2184 | } |
| 2185 | break; |
| 2186 | case 15: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2187 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2188 | min_arg_count = 0; |
| 2189 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2190 | } 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] | 2191 | min_arg_count = 2; |
| 2192 | max_arg_count = 2; |
| 2193 | } |
| 2194 | break; |
| 2195 | case 16: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2196 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2197 | min_arg_count = 2; |
| 2198 | max_arg_count = 2; |
| 2199 | } |
| 2200 | break; |
| 2201 | case 20: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2202 | 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] | 2203 | min_arg_count = 2; |
| 2204 | max_arg_count = 2; |
| 2205 | } |
| 2206 | break; |
| 2207 | } |
| 2208 | if (min_arg_count == -1) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2209 | 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] | 2210 | return LY_EINVAL; |
| 2211 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2212 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2213 | |
| 2214 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2215 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2216 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2217 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2218 | |
| 2219 | /* ( Expr ( ',' Expr )* )? */ |
| 2220 | arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2221 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2222 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2223 | if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2224 | ++arg_count; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2225 | rc = reparse_or_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2226 | LY_CHECK_RET(rc); |
| 2227 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2228 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) { |
| 2229 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2230 | |
| 2231 | ++arg_count; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2232 | rc = reparse_or_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2233 | LY_CHECK_RET(rc); |
| 2234 | } |
| 2235 | |
| 2236 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2237 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2238 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2239 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2240 | |
| 2241 | if ((arg_count < min_arg_count) || (arg_count > max_arg_count)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2242 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INARGCOUNT, arg_count, exp->tok_len[func_tok_idx], |
| 2243 | &exp->expr[exp->tok_pos[func_tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2244 | return LY_EVALID; |
| 2245 | } |
| 2246 | |
| 2247 | return LY_SUCCESS; |
| 2248 | } |
| 2249 | |
| 2250 | /** |
| 2251 | * @brief Reparse PathExpr. Logs directly on error. |
| 2252 | * |
| 2253 | * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate* |
| 2254 | * | PrimaryExpr Predicate* '/' RelativeLocationPath |
| 2255 | * | PrimaryExpr Predicate* '//' RelativeLocationPath |
| 2256 | * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath |
| 2257 | * [8] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall |
| 2258 | * |
| 2259 | * @param[in] ctx Context for logging. |
| 2260 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2261 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2262 | * @return LY_ERR |
| 2263 | */ |
| 2264 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2265 | 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] | 2266 | { |
| 2267 | LY_ERR rc; |
| 2268 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2269 | if (lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 2270 | return LY_EVALID; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2271 | } |
| 2272 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2273 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2274 | case LYXP_TOKEN_PAR1: |
| 2275 | /* '(' Expr ')' Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2276 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2277 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2278 | rc = reparse_or_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2279 | LY_CHECK_RET(rc); |
| 2280 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2281 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2282 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2283 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2284 | goto predicate; |
| 2285 | break; |
| 2286 | case LYXP_TOKEN_DOT: |
| 2287 | case LYXP_TOKEN_DDOT: |
| 2288 | case LYXP_TOKEN_AT: |
| 2289 | case LYXP_TOKEN_NAMETEST: |
| 2290 | case LYXP_TOKEN_NODETYPE: |
| 2291 | /* RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2292 | rc = reparse_relative_location_path(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2293 | LY_CHECK_RET(rc); |
| 2294 | break; |
| 2295 | case LYXP_TOKEN_FUNCNAME: |
| 2296 | /* FunctionCall */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2297 | rc = reparse_function_call(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2298 | LY_CHECK_RET(rc); |
| 2299 | goto predicate; |
| 2300 | break; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2301 | case LYXP_TOKEN_OPER_PATH: |
| 2302 | case LYXP_TOKEN_OPER_RPATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2303 | /* AbsoluteLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2304 | rc = reparse_absolute_location_path(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2305 | LY_CHECK_RET(rc); |
| 2306 | break; |
| 2307 | case LYXP_TOKEN_LITERAL: |
| 2308 | /* Literal */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2309 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2310 | goto predicate; |
| 2311 | break; |
| 2312 | case LYXP_TOKEN_NUMBER: |
| 2313 | /* Number */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2314 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2315 | goto predicate; |
| 2316 | break; |
| 2317 | default: |
| 2318 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2319 | 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] | 2320 | return LY_EVALID; |
| 2321 | } |
| 2322 | |
| 2323 | return LY_SUCCESS; |
| 2324 | |
| 2325 | predicate: |
| 2326 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2327 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 2328 | rc = reparse_predicate(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2329 | LY_CHECK_RET(rc); |
| 2330 | } |
| 2331 | |
| 2332 | /* ('/' or '//') RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2333 | 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] | 2334 | |
| 2335 | /* '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2336 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2337 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2338 | rc = reparse_relative_location_path(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2339 | LY_CHECK_RET(rc); |
| 2340 | } |
| 2341 | |
| 2342 | return LY_SUCCESS; |
| 2343 | } |
| 2344 | |
| 2345 | /** |
| 2346 | * @brief Reparse UnaryExpr. Logs directly on error. |
| 2347 | * |
| 2348 | * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr |
| 2349 | * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr |
| 2350 | * |
| 2351 | * @param[in] ctx Context for logging. |
| 2352 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2353 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2354 | * @return LY_ERR |
| 2355 | */ |
| 2356 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2357 | 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] | 2358 | { |
| 2359 | uint16_t prev_exp; |
| 2360 | LY_ERR rc; |
| 2361 | |
| 2362 | /* ('-')* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2363 | prev_exp = *tok_idx; |
| 2364 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) |
| 2365 | && (exp->expr[exp->tok_pos[*tok_idx]] == '-')) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2366 | exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2367 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2368 | } |
| 2369 | |
| 2370 | /* PathExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2371 | prev_exp = *tok_idx; |
| 2372 | rc = reparse_path_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2373 | LY_CHECK_RET(rc); |
| 2374 | |
| 2375 | /* ('|' PathExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2376 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_UNI)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2377 | exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2378 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2379 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2380 | rc = reparse_path_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2381 | LY_CHECK_RET(rc); |
| 2382 | } |
| 2383 | |
| 2384 | return LY_SUCCESS; |
| 2385 | } |
| 2386 | |
| 2387 | /** |
| 2388 | * @brief Reparse AdditiveExpr. Logs directly on error. |
| 2389 | * |
| 2390 | * [15] AdditiveExpr ::= MultiplicativeExpr |
| 2391 | * | AdditiveExpr '+' MultiplicativeExpr |
| 2392 | * | AdditiveExpr '-' MultiplicativeExpr |
| 2393 | * [16] MultiplicativeExpr ::= UnaryExpr |
| 2394 | * | MultiplicativeExpr '*' UnaryExpr |
| 2395 | * | MultiplicativeExpr 'div' UnaryExpr |
| 2396 | * | MultiplicativeExpr 'mod' UnaryExpr |
| 2397 | * |
| 2398 | * @param[in] ctx Context for logging. |
| 2399 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2400 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2401 | * @return LY_ERR |
| 2402 | */ |
| 2403 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2404 | 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] | 2405 | { |
| 2406 | uint16_t prev_add_exp, prev_mul_exp; |
| 2407 | LY_ERR rc; |
| 2408 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2409 | prev_add_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2410 | goto reparse_multiplicative_expr; |
| 2411 | |
| 2412 | /* ('+' / '-' MultiplicativeExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2413 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) |
| 2414 | && ((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] | 2415 | exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2416 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2417 | |
| 2418 | reparse_multiplicative_expr: |
| 2419 | /* UnaryExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2420 | prev_mul_exp = *tok_idx; |
| 2421 | rc = reparse_unary_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2422 | LY_CHECK_RET(rc); |
| 2423 | |
| 2424 | /* ('*' / 'div' / 'mod' UnaryExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2425 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) |
| 2426 | && ((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] | 2427 | exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2428 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2429 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2430 | rc = reparse_unary_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2431 | LY_CHECK_RET(rc); |
| 2432 | } |
| 2433 | } |
| 2434 | |
| 2435 | return LY_SUCCESS; |
| 2436 | } |
| 2437 | |
| 2438 | /** |
| 2439 | * @brief Reparse EqualityExpr. Logs directly on error. |
| 2440 | * |
| 2441 | * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr |
| 2442 | * | EqualityExpr '!=' RelationalExpr |
| 2443 | * [14] RelationalExpr ::= AdditiveExpr |
| 2444 | * | RelationalExpr '<' AdditiveExpr |
| 2445 | * | RelationalExpr '>' AdditiveExpr |
| 2446 | * | RelationalExpr '<=' AdditiveExpr |
| 2447 | * | RelationalExpr '>=' AdditiveExpr |
| 2448 | * |
| 2449 | * @param[in] ctx Context for logging. |
| 2450 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2451 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2452 | * @return LY_ERR |
| 2453 | */ |
| 2454 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2455 | 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] | 2456 | { |
| 2457 | uint16_t prev_eq_exp, prev_rel_exp; |
| 2458 | LY_ERR rc; |
| 2459 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2460 | prev_eq_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2461 | goto reparse_additive_expr; |
| 2462 | |
| 2463 | /* ('=' / '!=' RelationalExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2464 | 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] | 2465 | exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2466 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2467 | |
| 2468 | reparse_additive_expr: |
| 2469 | /* AdditiveExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2470 | prev_rel_exp = *tok_idx; |
| 2471 | rc = reparse_additive_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2472 | LY_CHECK_RET(rc); |
| 2473 | |
| 2474 | /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2475 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_COMP)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2476 | exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2477 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2478 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2479 | rc = reparse_additive_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2480 | LY_CHECK_RET(rc); |
| 2481 | } |
| 2482 | } |
| 2483 | |
| 2484 | return LY_SUCCESS; |
| 2485 | } |
| 2486 | |
| 2487 | /** |
| 2488 | * @brief Reparse OrExpr. Logs directly on error. |
| 2489 | * |
| 2490 | * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr |
| 2491 | * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr |
| 2492 | * |
| 2493 | * @param[in] ctx Context for logging. |
| 2494 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2495 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2496 | * @return LY_ERR |
| 2497 | */ |
| 2498 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2499 | 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] | 2500 | { |
| 2501 | uint16_t prev_or_exp, prev_and_exp; |
| 2502 | LY_ERR rc; |
| 2503 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2504 | prev_or_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2505 | goto reparse_equality_expr; |
| 2506 | |
| 2507 | /* ('or' AndExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2508 | 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] | 2509 | exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2510 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2511 | |
| 2512 | reparse_equality_expr: |
| 2513 | /* EqualityExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2514 | prev_and_exp = *tok_idx; |
| 2515 | rc = reparse_equality_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2516 | LY_CHECK_RET(rc); |
| 2517 | |
| 2518 | /* ('and' EqualityExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2519 | 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] | 2520 | exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2521 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2522 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2523 | rc = reparse_equality_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2524 | LY_CHECK_RET(rc); |
| 2525 | } |
| 2526 | } |
| 2527 | |
| 2528 | return LY_SUCCESS; |
| 2529 | } |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2530 | |
| 2531 | /** |
| 2532 | * @brief Parse NCName. |
| 2533 | * |
| 2534 | * @param[in] ncname Name to parse. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2535 | * @return Length of @p ncname valid bytes. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2536 | */ |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2537 | static long int |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2538 | parse_ncname(const char *ncname) |
| 2539 | { |
| 2540 | unsigned int uc; |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2541 | size_t size; |
| 2542 | long int len = 0; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2543 | |
| 2544 | LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0); |
| 2545 | if (!is_xmlqnamestartchar(uc) || (uc == ':')) { |
| 2546 | return len; |
| 2547 | } |
| 2548 | |
| 2549 | do { |
| 2550 | len += size; |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 2551 | if (!*ncname) { |
| 2552 | break; |
| 2553 | } |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2554 | LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2555 | } while (is_xmlqnamechar(uc) && (uc != ':')); |
| 2556 | |
| 2557 | return len; |
| 2558 | } |
| 2559 | |
| 2560 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2561 | * @brief Add @p token into the expression @p exp. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2562 | * |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2563 | * @param[in] ctx Context for logging. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2564 | * @param[in] exp Expression to use. |
| 2565 | * @param[in] token Token to add. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2566 | * @param[in] tok_pos Token position in the XPath expression. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2567 | * @param[in] tok_len Token length in the XPath expression. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2568 | * @return LY_ERR |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2569 | */ |
| 2570 | static LY_ERR |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 2571 | 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] | 2572 | { |
| 2573 | uint32_t prev; |
| 2574 | |
| 2575 | if (exp->used == exp->size) { |
| 2576 | prev = exp->size; |
| 2577 | exp->size += LYXP_EXPR_SIZE_STEP; |
| 2578 | if (prev > exp->size) { |
| 2579 | LOGINT(ctx); |
| 2580 | return LY_EINT; |
| 2581 | } |
| 2582 | |
| 2583 | exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens); |
| 2584 | LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM); |
| 2585 | exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos); |
| 2586 | LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM); |
| 2587 | exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len); |
| 2588 | LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM); |
| 2589 | } |
| 2590 | |
| 2591 | exp->tokens[exp->used] = token; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2592 | exp->tok_pos[exp->used] = tok_pos; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2593 | exp->tok_len[exp->used] = tok_len; |
| 2594 | ++exp->used; |
| 2595 | return LY_SUCCESS; |
| 2596 | } |
| 2597 | |
| 2598 | void |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 2599 | lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr) |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2600 | { |
| 2601 | uint16_t i; |
| 2602 | |
| 2603 | if (!expr) { |
| 2604 | return; |
| 2605 | } |
| 2606 | |
| 2607 | lydict_remove(ctx, expr->expr); |
| 2608 | free(expr->tokens); |
| 2609 | free(expr->tok_pos); |
| 2610 | free(expr->tok_len); |
| 2611 | if (expr->repeat) { |
| 2612 | for (i = 0; i < expr->used; ++i) { |
| 2613 | free(expr->repeat[i]); |
| 2614 | } |
| 2615 | } |
| 2616 | free(expr->repeat); |
| 2617 | free(expr); |
| 2618 | } |
| 2619 | |
| 2620 | struct lyxp_expr * |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2621 | 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] | 2622 | { |
| 2623 | struct lyxp_expr *ret; |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2624 | size_t parsed = 0, tok_len; |
| 2625 | long int ncname_len; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2626 | enum lyxp_token tok_type; |
| 2627 | int prev_function_check = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2628 | uint16_t tok_idx = 0; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2629 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2630 | if (!expr[0]) { |
| 2631 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF); |
| 2632 | return NULL; |
| 2633 | } |
| 2634 | |
| 2635 | if (!expr_len) { |
| 2636 | expr_len = strlen(expr); |
| 2637 | } |
| 2638 | if (expr_len > UINT16_MAX) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2639 | LOGERR(ctx, LY_EINVAL, "XPath expression cannot be longer than %ud characters.", UINT16_MAX); |
| 2640 | return NULL; |
| 2641 | } |
| 2642 | |
| 2643 | /* init lyxp_expr structure */ |
| 2644 | ret = calloc(1, sizeof *ret); |
| 2645 | LY_CHECK_ERR_GOTO(!ret, LOGMEM(ctx), error); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2646 | ret->expr = lydict_insert(ctx, expr, expr_len); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2647 | LY_CHECK_ERR_GOTO(!ret->expr, LOGMEM(ctx), error); |
| 2648 | ret->used = 0; |
| 2649 | ret->size = LYXP_EXPR_SIZE_START; |
| 2650 | ret->tokens = malloc(ret->size * sizeof *ret->tokens); |
| 2651 | LY_CHECK_ERR_GOTO(!ret->tokens, LOGMEM(ctx), error); |
| 2652 | |
| 2653 | ret->tok_pos = malloc(ret->size * sizeof *ret->tok_pos); |
| 2654 | LY_CHECK_ERR_GOTO(!ret->tok_pos, LOGMEM(ctx), error); |
| 2655 | |
| 2656 | ret->tok_len = malloc(ret->size * sizeof *ret->tok_len); |
| 2657 | LY_CHECK_ERR_GOTO(!ret->tok_len, LOGMEM(ctx), error); |
| 2658 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2659 | /* make expr 0-terminated */ |
| 2660 | expr = ret->expr; |
| 2661 | |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2662 | while (is_xmlws(expr[parsed])) { |
| 2663 | ++parsed; |
| 2664 | } |
| 2665 | |
| 2666 | do { |
| 2667 | if (expr[parsed] == '(') { |
| 2668 | |
| 2669 | /* '(' */ |
| 2670 | tok_len = 1; |
| 2671 | tok_type = LYXP_TOKEN_PAR1; |
| 2672 | |
| 2673 | if (prev_function_check && ret->used && (ret->tokens[ret->used - 1] == LYXP_TOKEN_NAMETEST)) { |
| 2674 | /* it is a NodeType/FunctionName after all */ |
| 2675 | if (((ret->tok_len[ret->used - 1] == 4) |
| 2676 | && (!strncmp(&expr[ret->tok_pos[ret->used - 1]], "node", 4) |
| 2677 | || !strncmp(&expr[ret->tok_pos[ret->used - 1]], "text", 4))) || |
| 2678 | ((ret->tok_len[ret->used - 1] == 7) |
| 2679 | && !strncmp(&expr[ret->tok_pos[ret->used - 1]], "comment", 7))) { |
| 2680 | ret->tokens[ret->used - 1] = LYXP_TOKEN_NODETYPE; |
| 2681 | } else { |
| 2682 | ret->tokens[ret->used - 1] = LYXP_TOKEN_FUNCNAME; |
| 2683 | } |
| 2684 | prev_function_check = 0; |
| 2685 | } |
| 2686 | |
| 2687 | } else if (expr[parsed] == ')') { |
| 2688 | |
| 2689 | /* ')' */ |
| 2690 | tok_len = 1; |
| 2691 | tok_type = LYXP_TOKEN_PAR2; |
| 2692 | |
| 2693 | } else if (expr[parsed] == '[') { |
| 2694 | |
| 2695 | /* '[' */ |
| 2696 | tok_len = 1; |
| 2697 | tok_type = LYXP_TOKEN_BRACK1; |
| 2698 | |
| 2699 | } else if (expr[parsed] == ']') { |
| 2700 | |
| 2701 | /* ']' */ |
| 2702 | tok_len = 1; |
| 2703 | tok_type = LYXP_TOKEN_BRACK2; |
| 2704 | |
| 2705 | } else if (!strncmp(&expr[parsed], "..", 2)) { |
| 2706 | |
| 2707 | /* '..' */ |
| 2708 | tok_len = 2; |
| 2709 | tok_type = LYXP_TOKEN_DDOT; |
| 2710 | |
| 2711 | } else if ((expr[parsed] == '.') && (!isdigit(expr[parsed + 1]))) { |
| 2712 | |
| 2713 | /* '.' */ |
| 2714 | tok_len = 1; |
| 2715 | tok_type = LYXP_TOKEN_DOT; |
| 2716 | |
| 2717 | } else if (expr[parsed] == '@') { |
| 2718 | |
| 2719 | /* '@' */ |
| 2720 | tok_len = 1; |
| 2721 | tok_type = LYXP_TOKEN_AT; |
| 2722 | |
| 2723 | } else if (expr[parsed] == ',') { |
| 2724 | |
| 2725 | /* ',' */ |
| 2726 | tok_len = 1; |
| 2727 | tok_type = LYXP_TOKEN_COMMA; |
| 2728 | |
| 2729 | } else if (expr[parsed] == '\'') { |
| 2730 | |
| 2731 | /* Literal with ' */ |
| 2732 | for (tok_len = 1; (expr[parsed + tok_len] != '\0') && (expr[parsed + tok_len] != '\''); ++tok_len); |
| 2733 | LY_CHECK_ERR_GOTO(expr[parsed + tok_len] == '\0', |
| 2734 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOE, expr[parsed], &expr[parsed]), error); |
| 2735 | ++tok_len; |
| 2736 | tok_type = LYXP_TOKEN_LITERAL; |
| 2737 | |
| 2738 | } else if (expr[parsed] == '\"') { |
| 2739 | |
| 2740 | /* Literal with " */ |
| 2741 | for (tok_len = 1; (expr[parsed + tok_len] != '\0') && (expr[parsed + tok_len] != '\"'); ++tok_len); |
| 2742 | LY_CHECK_ERR_GOTO(expr[parsed + tok_len] == '\0', |
| 2743 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOE, expr[parsed], &expr[parsed]), error); |
| 2744 | ++tok_len; |
| 2745 | tok_type = LYXP_TOKEN_LITERAL; |
| 2746 | |
| 2747 | } else if ((expr[parsed] == '.') || (isdigit(expr[parsed]))) { |
| 2748 | |
| 2749 | /* Number */ |
| 2750 | for (tok_len = 0; isdigit(expr[parsed + tok_len]); ++tok_len); |
| 2751 | if (expr[parsed + tok_len] == '.') { |
| 2752 | ++tok_len; |
| 2753 | for (; isdigit(expr[parsed + tok_len]); ++tok_len); |
| 2754 | } |
| 2755 | tok_type = LYXP_TOKEN_NUMBER; |
| 2756 | |
| 2757 | } else if (expr[parsed] == '/') { |
| 2758 | |
| 2759 | /* Operator '/', '//' */ |
| 2760 | if (!strncmp(&expr[parsed], "//", 2)) { |
| 2761 | tok_len = 2; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2762 | tok_type = LYXP_TOKEN_OPER_RPATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2763 | } else { |
| 2764 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2765 | tok_type = LYXP_TOKEN_OPER_PATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2766 | } |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2767 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2768 | } else if (!strncmp(&expr[parsed], "!=", 2)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2769 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2770 | /* Operator '!=' */ |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2771 | tok_len = 2; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2772 | tok_type = LYXP_TOKEN_OPER_NEQUAL; |
| 2773 | |
| 2774 | } else if (!strncmp(&expr[parsed], "<=", 2) || !strncmp(&expr[parsed], ">=", 2)) { |
| 2775 | |
| 2776 | /* Operator '<=', '>=' */ |
| 2777 | tok_len = 2; |
| 2778 | tok_type = LYXP_TOKEN_OPER_COMP; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2779 | |
| 2780 | } else if (expr[parsed] == '|') { |
| 2781 | |
| 2782 | /* Operator '|' */ |
| 2783 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2784 | tok_type = LYXP_TOKEN_OPER_UNI; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2785 | |
| 2786 | } else if ((expr[parsed] == '+') || (expr[parsed] == '-')) { |
| 2787 | |
| 2788 | /* Operator '+', '-' */ |
| 2789 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2790 | tok_type = LYXP_TOKEN_OPER_MATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2791 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2792 | } else if (expr[parsed] == '=') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2793 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2794 | /* Operator '=' */ |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2795 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2796 | tok_type = LYXP_TOKEN_OPER_EQUAL; |
| 2797 | |
| 2798 | } else if ((expr[parsed] == '<') || (expr[parsed] == '>')) { |
| 2799 | |
| 2800 | /* Operator '<', '>' */ |
| 2801 | tok_len = 1; |
| 2802 | tok_type = LYXP_TOKEN_OPER_COMP; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2803 | |
| 2804 | } else if (ret->used && (ret->tokens[ret->used - 1] != LYXP_TOKEN_AT) |
| 2805 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_PAR1) |
| 2806 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_BRACK1) |
| 2807 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_COMMA) |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2808 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_LOG) |
| 2809 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_EQUAL) |
| 2810 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_NEQUAL) |
| 2811 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_COMP) |
| 2812 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_MATH) |
| 2813 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_UNI) |
| 2814 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_PATH) |
| 2815 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_RPATH)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2816 | |
| 2817 | /* Operator '*', 'or', 'and', 'mod', or 'div' */ |
| 2818 | if (expr[parsed] == '*') { |
| 2819 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2820 | tok_type = LYXP_TOKEN_OPER_MATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2821 | |
| 2822 | } else if (!strncmp(&expr[parsed], "or", 2)) { |
| 2823 | tok_len = 2; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2824 | tok_type = LYXP_TOKEN_OPER_LOG; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2825 | |
| 2826 | } else if (!strncmp(&expr[parsed], "and", 3)) { |
| 2827 | tok_len = 3; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2828 | tok_type = LYXP_TOKEN_OPER_LOG; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2829 | |
| 2830 | } else if (!strncmp(&expr[parsed], "mod", 3) || !strncmp(&expr[parsed], "div", 3)) { |
| 2831 | tok_len = 3; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2832 | tok_type = LYXP_TOKEN_OPER_MATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2833 | |
| 2834 | } else if (prev_function_check) { |
Michal Vasko | 5307857 | 2019-05-24 08:50:15 +0200 | [diff] [blame] | 2835 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Invalid character 0x%x ('%c'), perhaps \"%.*s\" is supposed to be a function call.", |
| 2836 | 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] | 2837 | goto error; |
| 2838 | } else { |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2839 | 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] | 2840 | goto error; |
| 2841 | } |
| 2842 | } else if (expr[parsed] == '*') { |
| 2843 | |
| 2844 | /* NameTest '*' */ |
| 2845 | tok_len = 1; |
| 2846 | tok_type = LYXP_TOKEN_NAMETEST; |
| 2847 | |
| 2848 | } else { |
| 2849 | |
| 2850 | /* NameTest (NCName ':' '*' | QName) or NodeType/FunctionName */ |
| 2851 | ncname_len = parse_ncname(&expr[parsed]); |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2852 | 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] | 2853 | tok_len = ncname_len; |
| 2854 | |
| 2855 | if (expr[parsed + tok_len] == ':') { |
| 2856 | ++tok_len; |
| 2857 | if (expr[parsed + tok_len] == '*') { |
| 2858 | ++tok_len; |
| 2859 | } else { |
| 2860 | ncname_len = parse_ncname(&expr[parsed + tok_len]); |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2861 | 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] | 2862 | tok_len += ncname_len; |
| 2863 | } |
| 2864 | /* remove old flag to prevent ambiguities */ |
| 2865 | prev_function_check = 0; |
| 2866 | tok_type = LYXP_TOKEN_NAMETEST; |
| 2867 | } else { |
| 2868 | /* there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */ |
| 2869 | prev_function_check = 1; |
| 2870 | tok_type = LYXP_TOKEN_NAMETEST; |
| 2871 | } |
| 2872 | } |
| 2873 | |
| 2874 | /* store the token, move on to the next one */ |
| 2875 | LY_CHECK_GOTO(exp_add_token(ctx, ret, tok_type, parsed, tok_len), error); |
| 2876 | parsed += tok_len; |
| 2877 | while (is_xmlws(expr[parsed])) { |
| 2878 | ++parsed; |
| 2879 | } |
| 2880 | |
| 2881 | } while (expr[parsed]); |
| 2882 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2883 | if (reparse) { |
| 2884 | /* prealloc repeat */ |
| 2885 | ret->repeat = calloc(ret->size, sizeof *ret->repeat); |
| 2886 | LY_CHECK_ERR_GOTO(!ret->repeat, LOGMEM(ctx), error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2887 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2888 | /* fill repeat */ |
| 2889 | LY_CHECK_GOTO(reparse_or_expr(ctx, ret, &tok_idx), error); |
| 2890 | if (ret->used > tok_idx) { |
| 2891 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Unparsed characters \"%s\" left at the end of an XPath expression.", |
| 2892 | &ret->expr[ret->tok_pos[tok_idx]]); |
| 2893 | goto error; |
| 2894 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2895 | } |
| 2896 | |
| 2897 | print_expr_struct_debug(ret); |
| 2898 | |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2899 | return ret; |
| 2900 | |
| 2901 | error: |
| 2902 | lyxp_expr_free(ctx, ret); |
| 2903 | return NULL; |
| 2904 | } |
| 2905 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2906 | struct lyxp_expr * |
| 2907 | lyxp_expr_dup(const struct ly_ctx *ctx, const struct lyxp_expr *exp) |
| 2908 | { |
| 2909 | struct lyxp_expr *dup; |
| 2910 | uint32_t i, j; |
| 2911 | |
| 2912 | dup = calloc(1, sizeof *dup); |
| 2913 | LY_CHECK_ERR_GOTO(!dup, LOGMEM(ctx), error); |
| 2914 | |
| 2915 | dup->tokens = malloc(exp->used * sizeof *dup->tokens); |
| 2916 | LY_CHECK_ERR_GOTO(!dup->tokens, LOGMEM(ctx), error); |
| 2917 | memcpy(dup->tokens, exp->tokens, exp->used * sizeof *dup->tokens); |
| 2918 | |
| 2919 | dup->tok_pos = malloc(exp->used * sizeof *dup->tok_pos); |
| 2920 | LY_CHECK_ERR_GOTO(!dup->tok_pos, LOGMEM(ctx), error); |
| 2921 | memcpy(dup->tok_pos, exp->tok_pos, exp->used * sizeof *dup->tok_pos); |
| 2922 | |
| 2923 | dup->tok_len = malloc(exp->used * sizeof *dup->tok_len); |
| 2924 | LY_CHECK_ERR_GOTO(!dup->tok_len, LOGMEM(ctx), error); |
| 2925 | memcpy(dup->tok_len, exp->tok_len, exp->used * sizeof *dup->tok_len); |
| 2926 | |
| 2927 | dup->repeat = malloc(exp->used * sizeof *dup->repeat); |
| 2928 | LY_CHECK_ERR_GOTO(!dup->repeat, LOGMEM(ctx), error); |
| 2929 | for (i = 0; i < exp->used; ++i) { |
| 2930 | if (!exp->repeat[i]) { |
| 2931 | dup->repeat[i] = NULL; |
| 2932 | } else { |
| 2933 | for (j = 0; exp->repeat[i][j]; ++j); |
| 2934 | /* the ending 0 as well */ |
| 2935 | ++j; |
| 2936 | |
Michal Vasko | 99c7164 | 2020-07-03 13:33:36 +0200 | [diff] [blame] | 2937 | dup->repeat[i] = malloc(j * sizeof **dup->repeat); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2938 | LY_CHECK_ERR_GOTO(!dup->repeat[i], LOGMEM(ctx), error); |
| 2939 | memcpy(dup->repeat[i], exp->repeat[i], j * sizeof **dup->repeat); |
| 2940 | dup->repeat[i][j - 1] = 0; |
| 2941 | } |
| 2942 | } |
| 2943 | |
| 2944 | dup->used = exp->used; |
| 2945 | dup->size = exp->used; |
| 2946 | dup->expr = lydict_insert(ctx, exp->expr, 0); |
| 2947 | |
| 2948 | return dup; |
| 2949 | |
| 2950 | error: |
| 2951 | lyxp_expr_free(ctx, dup); |
| 2952 | return NULL; |
| 2953 | } |
| 2954 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2955 | /* |
| 2956 | * warn functions |
| 2957 | * |
| 2958 | * Warn functions check specific reasonable conditions for schema XPath |
| 2959 | * and print a warning if they are not satisfied. |
| 2960 | */ |
| 2961 | |
| 2962 | /** |
| 2963 | * @brief Get the last-added schema node that is currently in the context. |
| 2964 | * |
| 2965 | * @param[in] set Set to search in. |
| 2966 | * @return Last-added schema context node, NULL if no node is in context. |
| 2967 | */ |
| 2968 | static struct lysc_node * |
| 2969 | warn_get_scnode_in_ctx(struct lyxp_set *set) |
| 2970 | { |
| 2971 | uint32_t i; |
| 2972 | |
| 2973 | if (!set || (set->type != LYXP_SET_SCNODE_SET)) { |
| 2974 | return NULL; |
| 2975 | } |
| 2976 | |
| 2977 | i = set->used; |
| 2978 | do { |
| 2979 | --i; |
| 2980 | if (set->val.scnodes[i].in_ctx == 1) { |
| 2981 | /* if there are more, simply return the first found (last added) */ |
| 2982 | return set->val.scnodes[i].scnode; |
| 2983 | } |
| 2984 | } while (i); |
| 2985 | |
| 2986 | return NULL; |
| 2987 | } |
| 2988 | |
| 2989 | /** |
| 2990 | * @brief Test whether a type is numeric - integer type or decimal64. |
| 2991 | * |
| 2992 | * @param[in] type Type to test. |
| 2993 | * @return 1 if numeric, 0 otherwise. |
| 2994 | */ |
| 2995 | static int |
| 2996 | warn_is_numeric_type(struct lysc_type *type) |
| 2997 | { |
| 2998 | struct lysc_type_union *uni; |
| 2999 | int ret; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3000 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3001 | |
| 3002 | switch (type->basetype) { |
| 3003 | case LY_TYPE_DEC64: |
| 3004 | case LY_TYPE_INT8: |
| 3005 | case LY_TYPE_UINT8: |
| 3006 | case LY_TYPE_INT16: |
| 3007 | case LY_TYPE_UINT16: |
| 3008 | case LY_TYPE_INT32: |
| 3009 | case LY_TYPE_UINT32: |
| 3010 | case LY_TYPE_INT64: |
| 3011 | case LY_TYPE_UINT64: |
| 3012 | return 1; |
| 3013 | case LY_TYPE_UNION: |
| 3014 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3015 | LY_ARRAY_FOR(uni->types, u) { |
| 3016 | ret = warn_is_numeric_type(uni->types[u]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3017 | if (ret) { |
| 3018 | /* found a suitable type */ |
| 3019 | return 1; |
| 3020 | } |
| 3021 | } |
| 3022 | /* did not find any suitable type */ |
| 3023 | return 0; |
| 3024 | case LY_TYPE_LEAFREF: |
| 3025 | return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype); |
| 3026 | default: |
| 3027 | return 0; |
| 3028 | } |
| 3029 | } |
| 3030 | |
| 3031 | /** |
| 3032 | * @brief Test whether a type is string-like - no integers, decimal64 or binary. |
| 3033 | * |
| 3034 | * @param[in] type Type to test. |
| 3035 | * @return 1 if string, 0 otherwise. |
| 3036 | */ |
| 3037 | static int |
| 3038 | warn_is_string_type(struct lysc_type *type) |
| 3039 | { |
| 3040 | struct lysc_type_union *uni; |
| 3041 | int ret; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3042 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3043 | |
| 3044 | switch (type->basetype) { |
| 3045 | case LY_TYPE_BITS: |
| 3046 | case LY_TYPE_ENUM: |
| 3047 | case LY_TYPE_IDENT: |
| 3048 | case LY_TYPE_INST: |
| 3049 | case LY_TYPE_STRING: |
| 3050 | return 1; |
| 3051 | case LY_TYPE_UNION: |
| 3052 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3053 | LY_ARRAY_FOR(uni->types, u) { |
| 3054 | ret = warn_is_string_type(uni->types[u]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3055 | if (ret) { |
| 3056 | /* found a suitable type */ |
| 3057 | return 1; |
| 3058 | } |
| 3059 | } |
| 3060 | /* did not find any suitable type */ |
| 3061 | return 0; |
| 3062 | case LY_TYPE_LEAFREF: |
| 3063 | return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype); |
| 3064 | default: |
| 3065 | return 0; |
| 3066 | } |
| 3067 | } |
| 3068 | |
| 3069 | /** |
| 3070 | * @brief Test whether a type is one specific type. |
| 3071 | * |
| 3072 | * @param[in] type Type to test. |
| 3073 | * @param[in] base Expected type. |
| 3074 | * @return 1 if it is, 0 otherwise. |
| 3075 | */ |
| 3076 | static int |
| 3077 | warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base) |
| 3078 | { |
| 3079 | struct lysc_type_union *uni; |
| 3080 | int ret; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3081 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3082 | |
| 3083 | if (type->basetype == base) { |
| 3084 | return 1; |
| 3085 | } else if (type->basetype == LY_TYPE_UNION) { |
| 3086 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3087 | LY_ARRAY_FOR(uni->types, u) { |
| 3088 | ret = warn_is_specific_type(uni->types[u], base); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3089 | if (ret) { |
| 3090 | /* found a suitable type */ |
| 3091 | return 1; |
| 3092 | } |
| 3093 | } |
| 3094 | /* did not find any suitable type */ |
| 3095 | return 0; |
| 3096 | } else if (type->basetype == LY_TYPE_LEAFREF) { |
| 3097 | return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base); |
| 3098 | } |
| 3099 | |
| 3100 | return 0; |
| 3101 | } |
| 3102 | |
| 3103 | /** |
| 3104 | * @brief Get next type of a (union) type. |
| 3105 | * |
| 3106 | * @param[in] type Base type. |
| 3107 | * @param[in] prev_type Previously returned type. |
| 3108 | * @return Next type or NULL. |
| 3109 | */ |
| 3110 | static struct lysc_type * |
| 3111 | warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type) |
| 3112 | { |
| 3113 | struct lysc_type_union *uni; |
| 3114 | int found = 0; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3115 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3116 | |
| 3117 | switch (type->basetype) { |
| 3118 | case LY_TYPE_UNION: |
| 3119 | uni = (struct lysc_type_union *)type; |
| 3120 | if (!prev_type) { |
| 3121 | return uni->types[0]; |
| 3122 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3123 | LY_ARRAY_FOR(uni->types, u) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3124 | if (found) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3125 | return uni->types[u]; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3126 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3127 | if (prev_type == uni->types[u]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3128 | found = 1; |
| 3129 | } |
| 3130 | } |
| 3131 | return NULL; |
| 3132 | default: |
| 3133 | if (prev_type) { |
| 3134 | assert(type == prev_type); |
| 3135 | return NULL; |
| 3136 | } else { |
| 3137 | return type; |
| 3138 | } |
| 3139 | } |
| 3140 | } |
| 3141 | |
| 3142 | /** |
| 3143 | * @brief Test whether 2 types have a common type. |
| 3144 | * |
| 3145 | * @param[in] type1 First type. |
| 3146 | * @param[in] type2 Second type. |
| 3147 | * @return 1 if they do, 0 otherwise. |
| 3148 | */ |
| 3149 | static int |
| 3150 | warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2) |
| 3151 | { |
| 3152 | struct lysc_type *t1, *rt1, *t2, *rt2; |
| 3153 | |
| 3154 | t1 = NULL; |
| 3155 | while ((t1 = warn_is_equal_type_next_type(type1, t1))) { |
| 3156 | if (t1->basetype == LY_TYPE_LEAFREF) { |
| 3157 | rt1 = ((struct lysc_type_leafref *)t1)->realtype; |
| 3158 | } else { |
| 3159 | rt1 = t1; |
| 3160 | } |
| 3161 | |
| 3162 | t2 = NULL; |
| 3163 | while ((t2 = warn_is_equal_type_next_type(type2, t2))) { |
| 3164 | if (t2->basetype == LY_TYPE_LEAFREF) { |
| 3165 | rt2 = ((struct lysc_type_leafref *)t2)->realtype; |
| 3166 | } else { |
| 3167 | rt2 = t2; |
| 3168 | } |
| 3169 | |
| 3170 | if (rt2->basetype == rt1->basetype) { |
| 3171 | /* match found */ |
| 3172 | return 1; |
| 3173 | } |
| 3174 | } |
| 3175 | } |
| 3176 | |
| 3177 | return 0; |
| 3178 | } |
| 3179 | |
| 3180 | /** |
| 3181 | * @brief Check both operands of comparison operators. |
| 3182 | * |
| 3183 | * @param[in] ctx Context for errors. |
| 3184 | * @param[in] set1 First operand set. |
| 3185 | * @param[in] set2 Second operand set. |
| 3186 | * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!='). |
| 3187 | * @param[in] expr Start of the expression to print with the warning. |
| 3188 | * @param[in] tok_pos Token position. |
| 3189 | */ |
| 3190 | static void |
| 3191 | warn_operands(struct ly_ctx *ctx, struct lyxp_set *set1, struct lyxp_set *set2, int numbers_only, const char *expr, uint16_t tok_pos) |
| 3192 | { |
| 3193 | struct lysc_node_leaf *node1, *node2; |
| 3194 | int leaves = 1, warning = 0; |
| 3195 | |
| 3196 | node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1); |
| 3197 | node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2); |
| 3198 | |
| 3199 | if (!node1 && !node2) { |
| 3200 | /* no node-sets involved, nothing to do */ |
| 3201 | return; |
| 3202 | } |
| 3203 | |
| 3204 | if (node1) { |
| 3205 | if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3206 | LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name); |
| 3207 | warning = 1; |
| 3208 | leaves = 0; |
| 3209 | } else if (numbers_only && !warn_is_numeric_type(node1->type)) { |
| 3210 | LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name); |
| 3211 | warning = 1; |
| 3212 | } |
| 3213 | } |
| 3214 | |
| 3215 | if (node2) { |
| 3216 | if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3217 | LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name); |
| 3218 | warning = 1; |
| 3219 | leaves = 0; |
| 3220 | } else if (numbers_only && !warn_is_numeric_type(node2->type)) { |
| 3221 | LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name); |
| 3222 | warning = 1; |
| 3223 | } |
| 3224 | } |
| 3225 | |
| 3226 | if (node1 && node2 && leaves && !numbers_only) { |
| 3227 | if ((warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type)) |
| 3228 | || (!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_equal_type(node1->type, node2->type))) { |
| 3231 | LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name); |
| 3232 | warning = 1; |
| 3233 | } |
| 3234 | } |
| 3235 | |
| 3236 | if (warning) { |
| 3237 | LOGWRN(ctx, "Previous warning generated by XPath subexpression[%u] \"%.20s\".", tok_pos, expr + tok_pos); |
| 3238 | } |
| 3239 | } |
| 3240 | |
| 3241 | /** |
| 3242 | * @brief Check that a value is valid for a leaf. If not applicable, does nothing. |
| 3243 | * |
| 3244 | * @param[in] exp Parsed XPath expression. |
| 3245 | * @param[in] set Set with the leaf/leaf-list. |
| 3246 | * @param[in] val_exp Index of the value (literal/number) in @p exp. |
| 3247 | * @param[in] equal_exp Index of the start of the equality expression in @p exp. |
| 3248 | * @param[in] last_equal_exp Index of the end of the equality expression in @p exp. |
| 3249 | */ |
| 3250 | static void |
| 3251 | warn_equality_value(struct lyxp_expr *exp, struct lyxp_set *set, uint16_t val_exp, uint16_t equal_exp, uint16_t last_equal_exp) |
| 3252 | { |
| 3253 | struct lysc_node *scnode; |
| 3254 | struct lysc_type *type; |
| 3255 | char *value; |
| 3256 | LY_ERR rc; |
| 3257 | struct ly_err_item *err = NULL; |
| 3258 | |
| 3259 | if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST)) |
| 3260 | && ((exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) || (exp->tokens[val_exp] == LYXP_TOKEN_NUMBER))) { |
| 3261 | /* check that the node can have the specified value */ |
| 3262 | if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) { |
| 3263 | value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2); |
| 3264 | } else { |
| 3265 | value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]); |
| 3266 | } |
| 3267 | if (!value) { |
| 3268 | LOGMEM(set->ctx); |
| 3269 | return; |
| 3270 | } |
| 3271 | |
| 3272 | if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) { |
| 3273 | LOGWRN(set->ctx, "Identityref \"%s\" comparison with identity \"%s\" without prefix, consider adding" |
| 3274 | " a prefix or best using \"derived-from(-or-self)()\" functions.", scnode->name, value); |
| 3275 | LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp], |
| 3276 | (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp], |
| 3277 | exp->expr + exp->tok_pos[equal_exp]); |
| 3278 | } |
| 3279 | |
| 3280 | type = ((struct lysc_node_leaf *)scnode)->type; |
| 3281 | if (type->basetype != LY_TYPE_IDENT) { |
| 3282 | rc = type->plugin->store(set->ctx, type, value, strlen(value), LY_TYPE_OPTS_SCHEMA, |
| 3283 | lys_resolve_prefix, (void *)type->dflt_mod, LYD_XML, NULL, NULL, NULL, NULL, &err); |
| 3284 | |
| 3285 | if (err) { |
| 3286 | LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg); |
| 3287 | ly_err_free(err); |
| 3288 | } else if (rc != LY_SUCCESS) { |
| 3289 | LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value); |
| 3290 | } |
| 3291 | if (rc != LY_SUCCESS) { |
| 3292 | LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp], |
| 3293 | (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp], |
| 3294 | exp->expr + exp->tok_pos[equal_exp]); |
| 3295 | } |
| 3296 | } |
| 3297 | free(value); |
| 3298 | } |
| 3299 | } |
| 3300 | |
| 3301 | /* |
| 3302 | * XPath functions |
| 3303 | */ |
| 3304 | |
| 3305 | /** |
| 3306 | * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN |
| 3307 | * depending on whether the first node bit value from the second argument is set. |
| 3308 | * |
| 3309 | * @param[in] args Array of arguments. |
| 3310 | * @param[in] arg_count Count of elements in @p args. |
| 3311 | * @param[in,out] set Context and result set at the same time. |
| 3312 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3313 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3314 | */ |
| 3315 | static LY_ERR |
| 3316 | xpath_bit_is_set(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3317 | { |
| 3318 | struct lyd_node_term *leaf; |
| 3319 | struct lysc_node_leaf *sleaf; |
| 3320 | struct lysc_type_bits *bits; |
| 3321 | LY_ERR rc = LY_SUCCESS; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3322 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3323 | |
| 3324 | if (options & LYXP_SCNODE_ALL) { |
| 3325 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3326 | 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] | 3327 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3328 | 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] | 3329 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) { |
| 3330 | 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] | 3331 | } |
| 3332 | |
| 3333 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 3334 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3335 | 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] | 3336 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3337 | 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] | 3338 | } |
| 3339 | } |
| 3340 | set_scnode_clear_ctx(set); |
| 3341 | return rc; |
| 3342 | } |
| 3343 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3344 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3345 | 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)"); |
| 3346 | return LY_EVALID; |
| 3347 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3348 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3349 | LY_CHECK_RET(rc); |
| 3350 | |
| 3351 | set_fill_boolean(set, 0); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3352 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3353 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
| 3354 | if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) |
| 3355 | && (((struct lysc_node_leaf *)leaf->schema)->type->basetype == LY_TYPE_BITS)) { |
| 3356 | bits = (struct lysc_type_bits *)((struct lysc_node_leaf *)leaf->schema)->type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3357 | LY_ARRAY_FOR(bits->bits, u) { |
| 3358 | if (!strcmp(bits->bits[u].name, args[1]->val.str)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3359 | set_fill_boolean(set, 1); |
| 3360 | break; |
| 3361 | } |
| 3362 | } |
| 3363 | } |
| 3364 | } |
| 3365 | |
| 3366 | return LY_SUCCESS; |
| 3367 | } |
| 3368 | |
| 3369 | /** |
| 3370 | * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN |
| 3371 | * with the argument converted to boolean. |
| 3372 | * |
| 3373 | * @param[in] args Array of arguments. |
| 3374 | * @param[in] arg_count Count of elements in @p args. |
| 3375 | * @param[in,out] set Context and result set at the same time. |
| 3376 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3377 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3378 | */ |
| 3379 | static LY_ERR |
| 3380 | xpath_boolean(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3381 | { |
| 3382 | LY_ERR rc; |
| 3383 | |
| 3384 | if (options & LYXP_SCNODE_ALL) { |
| 3385 | set_scnode_clear_ctx(set); |
| 3386 | return LY_SUCCESS; |
| 3387 | } |
| 3388 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3389 | rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3390 | LY_CHECK_RET(rc); |
| 3391 | set_fill_set(set, args[0]); |
| 3392 | |
| 3393 | return LY_SUCCESS; |
| 3394 | } |
| 3395 | |
| 3396 | /** |
| 3397 | * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER |
| 3398 | * with the first argument rounded up to the nearest integer. |
| 3399 | * |
| 3400 | * @param[in] args Array of arguments. |
| 3401 | * @param[in] arg_count Count of elements in @p args. |
| 3402 | * @param[in,out] set Context and result set at the same time. |
| 3403 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3404 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3405 | */ |
| 3406 | static LY_ERR |
| 3407 | xpath_ceiling(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3408 | { |
| 3409 | struct lysc_node_leaf *sleaf; |
| 3410 | LY_ERR rc = LY_SUCCESS; |
| 3411 | |
| 3412 | if (options & LYXP_SCNODE_ALL) { |
| 3413 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3414 | 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] | 3415 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3416 | 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] | 3417 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) { |
| 3418 | 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] | 3419 | } |
| 3420 | set_scnode_clear_ctx(set); |
| 3421 | return rc; |
| 3422 | } |
| 3423 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3424 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3425 | LY_CHECK_RET(rc); |
| 3426 | if ((long long)args[0]->val.num != args[0]->val.num) { |
| 3427 | set_fill_number(set, ((long long)args[0]->val.num) + 1); |
| 3428 | } else { |
| 3429 | set_fill_number(set, args[0]->val.num); |
| 3430 | } |
| 3431 | |
| 3432 | return LY_SUCCESS; |
| 3433 | } |
| 3434 | |
| 3435 | /** |
| 3436 | * @brief Execute the XPath concat(string, string, string*) function. |
| 3437 | * Returns LYXP_SET_STRING with the concatenation of all the arguments. |
| 3438 | * |
| 3439 | * @param[in] args Array of arguments. |
| 3440 | * @param[in] arg_count Count of elements in @p args. |
| 3441 | * @param[in,out] set Context and result set at the same time. |
| 3442 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3443 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3444 | */ |
| 3445 | static LY_ERR |
| 3446 | xpath_concat(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options) |
| 3447 | { |
| 3448 | uint16_t i; |
| 3449 | char *str = NULL; |
| 3450 | size_t used = 1; |
| 3451 | LY_ERR rc = LY_SUCCESS; |
| 3452 | struct lysc_node_leaf *sleaf; |
| 3453 | |
| 3454 | if (options & LYXP_SCNODE_ALL) { |
| 3455 | for (i = 0; i < arg_count; ++i) { |
| 3456 | if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) { |
| 3457 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3458 | LOGWRN(set->ctx, "Argument #%u of %s is a %s node \"%s\".", |
| 3459 | i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3460 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3461 | 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] | 3462 | } |
| 3463 | } |
| 3464 | } |
| 3465 | set_scnode_clear_ctx(set); |
| 3466 | return rc; |
| 3467 | } |
| 3468 | |
| 3469 | for (i = 0; i < arg_count; ++i) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3470 | rc = lyxp_set_cast(args[i], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3471 | if (rc != LY_SUCCESS) { |
| 3472 | free(str); |
| 3473 | return rc; |
| 3474 | } |
| 3475 | |
| 3476 | str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char)); |
| 3477 | LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM); |
| 3478 | strcpy(str + used - 1, args[i]->val.str); |
| 3479 | used += strlen(args[i]->val.str); |
| 3480 | } |
| 3481 | |
| 3482 | /* free, kind of */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3483 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3484 | set->type = LYXP_SET_STRING; |
| 3485 | set->val.str = str; |
| 3486 | |
| 3487 | return LY_SUCCESS; |
| 3488 | } |
| 3489 | |
| 3490 | /** |
| 3491 | * @brief Execute the XPath contains(string, string) function. |
| 3492 | * Returns LYXP_SET_BOOLEAN whether the second argument can |
| 3493 | * be found in the first or not. |
| 3494 | * |
| 3495 | * @param[in] args Array of arguments. |
| 3496 | * @param[in] arg_count Count of elements in @p args. |
| 3497 | * @param[in,out] set Context and result set at the same time. |
| 3498 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3499 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3500 | */ |
| 3501 | static LY_ERR |
| 3502 | xpath_contains(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3503 | { |
| 3504 | struct lysc_node_leaf *sleaf; |
| 3505 | LY_ERR rc = LY_SUCCESS; |
| 3506 | |
| 3507 | if (options & LYXP_SCNODE_ALL) { |
| 3508 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3509 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3510 | 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] | 3511 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3512 | 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] | 3513 | } |
| 3514 | } |
| 3515 | |
| 3516 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 3517 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3518 | 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] | 3519 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3520 | 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] | 3521 | } |
| 3522 | } |
| 3523 | set_scnode_clear_ctx(set); |
| 3524 | return rc; |
| 3525 | } |
| 3526 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3527 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3528 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3529 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3530 | LY_CHECK_RET(rc); |
| 3531 | |
| 3532 | if (strstr(args[0]->val.str, args[1]->val.str)) { |
| 3533 | set_fill_boolean(set, 1); |
| 3534 | } else { |
| 3535 | set_fill_boolean(set, 0); |
| 3536 | } |
| 3537 | |
| 3538 | return LY_SUCCESS; |
| 3539 | } |
| 3540 | |
| 3541 | /** |
| 3542 | * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER |
| 3543 | * with the size of the node-set from the argument. |
| 3544 | * |
| 3545 | * @param[in] args Array of arguments. |
| 3546 | * @param[in] arg_count Count of elements in @p args. |
| 3547 | * @param[in,out] set Context and result set at the same time. |
| 3548 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3549 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3550 | */ |
| 3551 | static LY_ERR |
| 3552 | xpath_count(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3553 | { |
| 3554 | struct lysc_node *scnode = NULL; |
| 3555 | LY_ERR rc = LY_SUCCESS; |
| 3556 | |
| 3557 | if (options & LYXP_SCNODE_ALL) { |
| 3558 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(scnode = warn_get_scnode_in_ctx(args[0]))) { |
| 3559 | 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] | 3560 | } |
| 3561 | set_scnode_clear_ctx(set); |
| 3562 | return rc; |
| 3563 | } |
| 3564 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3565 | if (args[0]->type != LYXP_SET_NODE_SET) { |
| 3566 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "count(node-set)"); |
| 3567 | return LY_EVALID; |
| 3568 | } |
| 3569 | |
| 3570 | set_fill_number(set, args[0]->used); |
| 3571 | return LY_SUCCESS; |
| 3572 | } |
| 3573 | |
| 3574 | /** |
| 3575 | * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET |
| 3576 | * with the context with the intial node. |
| 3577 | * |
| 3578 | * @param[in] args Array of arguments. |
| 3579 | * @param[in] arg_count Count of elements in @p args. |
| 3580 | * @param[in,out] set Context and result set at the same time. |
| 3581 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3582 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3583 | */ |
| 3584 | static LY_ERR |
| 3585 | xpath_current(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options) |
| 3586 | { |
| 3587 | if (arg_count || args) { |
| 3588 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGCOUNT, arg_count, "current()"); |
| 3589 | return LY_EVALID; |
| 3590 | } |
| 3591 | |
| 3592 | if (options & LYXP_SCNODE_ALL) { |
| 3593 | set_scnode_clear_ctx(set); |
| 3594 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 3595 | lyxp_set_scnode_insert_node(set, set->ctx_scnode, LYXP_NODE_ELEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3596 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3597 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3598 | |
| 3599 | /* position is filled later */ |
| 3600 | set_insert_node(set, set->ctx_node, 0, LYXP_NODE_ELEM, 0); |
| 3601 | } |
| 3602 | |
| 3603 | return LY_SUCCESS; |
| 3604 | } |
| 3605 | |
| 3606 | /** |
| 3607 | * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either |
| 3608 | * leafref or instance-identifier target node(s). |
| 3609 | * |
| 3610 | * @param[in] args Array of arguments. |
| 3611 | * @param[in] arg_count Count of elements in @p args. |
| 3612 | * @param[in,out] set Context and result set at the same time. |
| 3613 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3614 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3615 | */ |
| 3616 | static LY_ERR |
| 3617 | xpath_deref(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3618 | { |
| 3619 | struct lyd_node_term *leaf; |
Michal Vasko | 42e497c | 2020-01-06 08:38:25 +0100 | [diff] [blame] | 3620 | struct lysc_node_leaf *sleaf = NULL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3621 | struct lysc_type_leafref *lref; |
Michal Vasko | ae9e4cb | 2019-09-25 08:43:05 +0200 | [diff] [blame] | 3622 | const struct lysc_node *target; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3623 | struct ly_path *p; |
| 3624 | struct lyd_node *node; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3625 | char *errmsg = NULL; |
| 3626 | const char *val; |
| 3627 | int dynamic; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 3628 | uint8_t oper; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3629 | LY_ERR rc = LY_SUCCESS; |
| 3630 | |
| 3631 | if (options & LYXP_SCNODE_ALL) { |
| 3632 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3633 | 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] | 3634 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3635 | 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] | 3636 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) && !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) { |
| 3637 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"leafref\" nor \"instance-identifier\".", |
| 3638 | __func__, sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3639 | } |
| 3640 | set_scnode_clear_ctx(set); |
Michal Vasko | 42e497c | 2020-01-06 08:38:25 +0100 | [diff] [blame] | 3641 | if (sleaf && (sleaf->type->basetype == LY_TYPE_LEAFREF)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3642 | lref = (struct lysc_type_leafref *)sleaf->type; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 3643 | 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] | 3644 | |
| 3645 | /* it was already evaluated on schema, it must succeed */ |
| 3646 | if (set->format == LYD_JSON) { |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 3647 | rc = ly_path_compile(set->ctx, sleaf->module, (struct lysc_node *)sleaf, lref->path, LY_PATH_LREF_TRUE, |
| 3648 | oper, LY_PATH_TARGET_MANY, lydjson_resolve_prefix, NULL, LYD_JSON, &p); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3649 | } else { |
| 3650 | assert(set->format == LYD_SCHEMA); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 3651 | rc = ly_path_compile(set->ctx, sleaf->module, (struct lysc_node *)sleaf, lref->path, LY_PATH_LREF_TRUE, |
| 3652 | 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] | 3653 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3654 | assert(!rc); |
| 3655 | |
| 3656 | /* get the target node */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3657 | target = p[LY_ARRAY_COUNT(p) - 1].node; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3658 | ly_path_free(set->ctx, p); |
| 3659 | |
| 3660 | lyxp_set_scnode_insert_node(set, target, LYXP_NODE_ELEM); |
Michal Vasko | ae9e4cb | 2019-09-25 08:43:05 +0200 | [diff] [blame] | 3661 | } |
| 3662 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3663 | return rc; |
| 3664 | } |
| 3665 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3666 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3667 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "deref(node-set)"); |
| 3668 | return LY_EVALID; |
| 3669 | } |
| 3670 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3671 | lyxp_set_free_content(set); |
| 3672 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3673 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
| 3674 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 3675 | if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 3676 | if (sleaf->type->basetype == LY_TYPE_LEAFREF) { |
| 3677 | /* find leafref target */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3678 | if (ly_type_find_leafref((struct lysc_type_leafref *)sleaf->type, (struct lyd_node *)leaf, |
| 3679 | &leaf->value, set->tree, &node, &errmsg)) { |
| 3680 | LOGERR(set->ctx, LY_EVALID, errmsg); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3681 | free(errmsg); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3682 | return LY_EVALID; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3683 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3684 | } else { |
| 3685 | assert(sleaf->type->basetype == LY_TYPE_INST); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3686 | if (ly_path_eval(leaf->value.target, set->tree, &node)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3687 | val = lyd_value2str(leaf, &dynamic); |
| 3688 | LOGERR(set->ctx, LY_EVALID, "Invalid instance-identifier \"%s\" value - required instance not found.", val); |
| 3689 | if (dynamic) { |
| 3690 | free((char *)val); |
| 3691 | } |
| 3692 | return LY_EVALID; |
| 3693 | } |
| 3694 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3695 | |
| 3696 | /* insert it */ |
| 3697 | set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3698 | } |
| 3699 | } |
| 3700 | |
| 3701 | return LY_SUCCESS; |
| 3702 | } |
| 3703 | |
| 3704 | /** |
| 3705 | * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending |
| 3706 | * on whether the first argument nodes contain a node of an identity derived from the second |
| 3707 | * argument identity. |
| 3708 | * |
| 3709 | * @param[in] args Array of arguments. |
| 3710 | * @param[in] arg_count Count of elements in @p args. |
| 3711 | * @param[in,out] set Context and result set at the same time. |
| 3712 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3713 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3714 | */ |
| 3715 | static LY_ERR |
| 3716 | xpath_derived_from(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3717 | { |
| 3718 | uint16_t i; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3719 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3720 | struct lyd_node_term *leaf; |
| 3721 | struct lysc_node_leaf *sleaf; |
| 3722 | struct lyd_value data = {0}; |
| 3723 | struct ly_err_item *err = NULL; |
| 3724 | LY_ERR rc = LY_SUCCESS; |
| 3725 | int found; |
| 3726 | |
| 3727 | if (options & LYXP_SCNODE_ALL) { |
| 3728 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3729 | 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] | 3730 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3731 | 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] | 3732 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) { |
| 3733 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", __func__, sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3734 | } |
| 3735 | |
| 3736 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 3737 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3738 | 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] | 3739 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3740 | 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] | 3741 | } |
| 3742 | } |
| 3743 | set_scnode_clear_ctx(set); |
| 3744 | return rc; |
| 3745 | } |
| 3746 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3747 | if (args[0]->type != LYXP_SET_NODE_SET) { |
| 3748 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), |
| 3749 | "derived-from(node-set, string)"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3750 | return LY_EVALID; |
| 3751 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3752 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3753 | LY_CHECK_RET(rc); |
| 3754 | |
| 3755 | set_fill_boolean(set, 0); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3756 | found = 0; |
| 3757 | for (i = 0; i < args[0]->used; ++i) { |
| 3758 | leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node; |
| 3759 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 3760 | if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_IDENT)) { |
| 3761 | /* store args[1] into ident */ |
| 3762 | rc = sleaf->type->plugin->store(set->ctx, sleaf->type, args[1]->val.str, strlen(args[1]->val.str), |
| 3763 | LY_TYPE_OPTS_STORE, lys_resolve_prefix, (void *)sleaf->dflt_mod, set->format, |
| 3764 | (struct lyd_node *)leaf, set->tree, &data, NULL, &err); |
| 3765 | if (err) { |
| 3766 | ly_err_print(err); |
| 3767 | ly_err_free(err); |
| 3768 | } |
| 3769 | LY_CHECK_RET(rc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3770 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3771 | LY_ARRAY_FOR(data.ident->derived, u) { |
| 3772 | if (data.ident->derived[u] == leaf->value.ident) { |
| 3773 | set_fill_boolean(set, 1); |
| 3774 | found = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3775 | break; |
| 3776 | } |
| 3777 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3778 | if (found) { |
| 3779 | break; |
| 3780 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3781 | } |
| 3782 | } |
| 3783 | |
| 3784 | return LY_SUCCESS; |
| 3785 | } |
| 3786 | |
| 3787 | /** |
| 3788 | * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending |
| 3789 | * on whether the first argument nodes contain a node of an identity that either is or is derived from |
| 3790 | * the second argument identity. |
| 3791 | * |
| 3792 | * @param[in] args Array of arguments. |
| 3793 | * @param[in] arg_count Count of elements in @p args. |
| 3794 | * @param[in,out] set Context and result set at the same time. |
| 3795 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3796 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3797 | */ |
| 3798 | static LY_ERR |
| 3799 | xpath_derived_from_or_self(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3800 | { |
| 3801 | uint16_t i; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3802 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3803 | struct lyd_node_term *leaf; |
| 3804 | struct lysc_node_leaf *sleaf; |
| 3805 | struct lyd_value data = {0}; |
| 3806 | struct ly_err_item *err = NULL; |
| 3807 | LY_ERR rc = LY_SUCCESS; |
| 3808 | int found; |
| 3809 | |
| 3810 | if (options & LYXP_SCNODE_ALL) { |
| 3811 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3812 | 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] | 3813 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3814 | 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] | 3815 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) { |
| 3816 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", __func__, sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3817 | } |
| 3818 | |
| 3819 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 3820 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3821 | 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] | 3822 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3823 | 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] | 3824 | } |
| 3825 | } |
| 3826 | set_scnode_clear_ctx(set); |
| 3827 | return rc; |
| 3828 | } |
| 3829 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3830 | if (args[0]->type != LYXP_SET_NODE_SET) { |
| 3831 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), |
| 3832 | "derived-from-or-self(node-set, string)"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3833 | return LY_EVALID; |
| 3834 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3835 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3836 | LY_CHECK_RET(rc); |
| 3837 | |
| 3838 | set_fill_boolean(set, 0); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3839 | found = 0; |
| 3840 | for (i = 0; i < args[0]->used; ++i) { |
| 3841 | leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node; |
| 3842 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 3843 | if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_IDENT)) { |
| 3844 | /* store args[1] into ident */ |
| 3845 | rc = sleaf->type->plugin->store(set->ctx, sleaf->type, args[1]->val.str, strlen(args[1]->val.str), |
| 3846 | LY_TYPE_OPTS_STORE, lys_resolve_prefix, (void *)sleaf->dflt_mod, set->format, |
| 3847 | (struct lyd_node *)leaf, set->tree, &data, NULL, &err); |
| 3848 | if (err) { |
| 3849 | ly_err_print(err); |
| 3850 | ly_err_free(err); |
| 3851 | } |
| 3852 | LY_CHECK_RET(rc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3853 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3854 | if (data.ident == leaf->value.ident) { |
| 3855 | set_fill_boolean(set, 1); |
| 3856 | break; |
| 3857 | } |
| 3858 | LY_ARRAY_FOR(data.ident->derived, u) { |
| 3859 | if (data.ident->derived[u] == leaf->value.ident) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3860 | set_fill_boolean(set, 1); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3861 | found = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3862 | break; |
| 3863 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3864 | } |
| 3865 | if (found) { |
| 3866 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3867 | } |
| 3868 | } |
| 3869 | } |
| 3870 | |
| 3871 | return LY_SUCCESS; |
| 3872 | } |
| 3873 | |
| 3874 | /** |
| 3875 | * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER |
| 3876 | * with the integer value of the first node's enum value, otherwise NaN. |
| 3877 | * |
| 3878 | * @param[in] args Array of arguments. |
| 3879 | * @param[in] arg_count Count of elements in @p args. |
| 3880 | * @param[in,out] set Context and result set at the same time. |
| 3881 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3882 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3883 | */ |
| 3884 | static LY_ERR |
| 3885 | xpath_enum_value(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3886 | { |
| 3887 | struct lyd_node_term *leaf; |
| 3888 | struct lysc_node_leaf *sleaf; |
| 3889 | LY_ERR rc = LY_SUCCESS; |
| 3890 | |
| 3891 | if (options & LYXP_SCNODE_ALL) { |
| 3892 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3893 | 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] | 3894 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3895 | 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] | 3896 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) { |
| 3897 | 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] | 3898 | } |
| 3899 | set_scnode_clear_ctx(set); |
| 3900 | return rc; |
| 3901 | } |
| 3902 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3903 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3904 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "enum-value(node-set)"); |
| 3905 | return LY_EVALID; |
| 3906 | } |
| 3907 | |
| 3908 | set_fill_number(set, NAN); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3909 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3910 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
| 3911 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 3912 | if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) { |
| 3913 | set_fill_number(set, leaf->value.enum_item->value); |
| 3914 | } |
| 3915 | } |
| 3916 | |
| 3917 | return LY_SUCCESS; |
| 3918 | } |
| 3919 | |
| 3920 | /** |
| 3921 | * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN |
| 3922 | * with false value. |
| 3923 | * |
| 3924 | * @param[in] args Array of arguments. |
| 3925 | * @param[in] arg_count Count of elements in @p args. |
| 3926 | * @param[in,out] set Context and result set at the same time. |
| 3927 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3928 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3929 | */ |
| 3930 | static LY_ERR |
| 3931 | xpath_false(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3932 | { |
| 3933 | if (options & LYXP_SCNODE_ALL) { |
| 3934 | set_scnode_clear_ctx(set); |
| 3935 | return LY_SUCCESS; |
| 3936 | } |
| 3937 | |
| 3938 | set_fill_boolean(set, 0); |
| 3939 | return LY_SUCCESS; |
| 3940 | } |
| 3941 | |
| 3942 | /** |
| 3943 | * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER |
| 3944 | * with the first argument floored (truncated). |
| 3945 | * |
| 3946 | * @param[in] args Array of arguments. |
| 3947 | * @param[in] arg_count Count of elements in @p args. |
| 3948 | * @param[in,out] set Context and result set at the same time. |
| 3949 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3950 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3951 | */ |
| 3952 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3953 | 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] | 3954 | { |
| 3955 | LY_ERR rc; |
| 3956 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3957 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3958 | LY_CHECK_RET(rc); |
| 3959 | if (isfinite(args[0]->val.num)) { |
| 3960 | set_fill_number(set, (long long)args[0]->val.num); |
| 3961 | } |
| 3962 | |
| 3963 | return LY_SUCCESS; |
| 3964 | } |
| 3965 | |
| 3966 | /** |
| 3967 | * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN |
| 3968 | * whether the language of the text matches the one from the argument. |
| 3969 | * |
| 3970 | * @param[in] args Array of arguments. |
| 3971 | * @param[in] arg_count Count of elements in @p args. |
| 3972 | * @param[in,out] set Context and result set at the same time. |
| 3973 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3974 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3975 | */ |
| 3976 | static LY_ERR |
| 3977 | xpath_lang(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3978 | { |
| 3979 | const struct lyd_node *node; |
| 3980 | struct lysc_node_leaf *sleaf; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 3981 | struct lyd_meta *meta = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3982 | const char *val; |
| 3983 | int i, dynamic; |
| 3984 | LY_ERR rc = LY_SUCCESS; |
| 3985 | |
| 3986 | if (options & LYXP_SCNODE_ALL) { |
| 3987 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3988 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3989 | 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] | 3990 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3991 | 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] | 3992 | } |
| 3993 | } |
| 3994 | set_scnode_clear_ctx(set); |
| 3995 | return rc; |
| 3996 | } |
| 3997 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3998 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3999 | LY_CHECK_RET(rc); |
| 4000 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4001 | if (set->type != LYXP_SET_NODE_SET) { |
| 4002 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "lang(string)"); |
| 4003 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4004 | } else if (!set->used) { |
| 4005 | set_fill_boolean(set, 0); |
| 4006 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4007 | } |
| 4008 | |
| 4009 | switch (set->val.nodes[0].type) { |
| 4010 | case LYXP_NODE_ELEM: |
| 4011 | case LYXP_NODE_TEXT: |
| 4012 | node = set->val.nodes[0].node; |
| 4013 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4014 | case LYXP_NODE_META: |
| 4015 | node = set->val.meta[0].meta->parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4016 | break; |
| 4017 | default: |
| 4018 | /* nothing to do with roots */ |
| 4019 | set_fill_boolean(set, 0); |
| 4020 | return LY_SUCCESS; |
| 4021 | } |
| 4022 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4023 | /* find lang metadata */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4024 | for (; node; node = (struct lyd_node *)node->parent) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4025 | for (meta = node->meta; meta; meta = meta->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4026 | /* annotations */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4027 | 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] | 4028 | break; |
| 4029 | } |
| 4030 | } |
| 4031 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4032 | if (meta) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4033 | break; |
| 4034 | } |
| 4035 | } |
| 4036 | |
| 4037 | /* compare languages */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4038 | if (!meta) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4039 | set_fill_boolean(set, 0); |
| 4040 | } else { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4041 | val = lyd_meta2str(meta, &dynamic); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4042 | for (i = 0; args[0]->val.str[i]; ++i) { |
| 4043 | if (tolower(args[0]->val.str[i]) != tolower(val[i])) { |
| 4044 | set_fill_boolean(set, 0); |
| 4045 | break; |
| 4046 | } |
| 4047 | } |
| 4048 | if (!args[0]->val.str[i]) { |
| 4049 | if (!val[i] || (val[i] == '-')) { |
| 4050 | set_fill_boolean(set, 1); |
| 4051 | } else { |
| 4052 | set_fill_boolean(set, 0); |
| 4053 | } |
| 4054 | } |
| 4055 | if (dynamic) { |
| 4056 | free((char *)val); |
| 4057 | } |
| 4058 | } |
| 4059 | |
| 4060 | return LY_SUCCESS; |
| 4061 | } |
| 4062 | |
| 4063 | /** |
| 4064 | * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER |
| 4065 | * with the context size. |
| 4066 | * |
| 4067 | * @param[in] args Array of arguments. |
| 4068 | * @param[in] arg_count Count of elements in @p args. |
| 4069 | * @param[in,out] set Context and result set at the same time. |
| 4070 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4071 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4072 | */ |
| 4073 | static LY_ERR |
| 4074 | xpath_last(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 4075 | { |
| 4076 | if (options & LYXP_SCNODE_ALL) { |
| 4077 | set_scnode_clear_ctx(set); |
| 4078 | return LY_SUCCESS; |
| 4079 | } |
| 4080 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4081 | if (set->type != LYXP_SET_NODE_SET) { |
| 4082 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "last()"); |
| 4083 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4084 | } else if (!set->used) { |
| 4085 | set_fill_number(set, 0); |
| 4086 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4087 | } |
| 4088 | |
| 4089 | set_fill_number(set, set->ctx_size); |
| 4090 | return LY_SUCCESS; |
| 4091 | } |
| 4092 | |
| 4093 | /** |
| 4094 | * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING |
| 4095 | * with the node name without namespace from the argument or the context. |
| 4096 | * |
| 4097 | * @param[in] args Array of arguments. |
| 4098 | * @param[in] arg_count Count of elements in @p args. |
| 4099 | * @param[in,out] set Context and result set at the same time. |
| 4100 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4101 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4102 | */ |
| 4103 | static LY_ERR |
| 4104 | xpath_local_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options) |
| 4105 | { |
| 4106 | struct lyxp_set_node *item; |
| 4107 | /* suppress unused variable warning */ |
| 4108 | (void)options; |
| 4109 | |
| 4110 | if (options & LYXP_SCNODE_ALL) { |
| 4111 | set_scnode_clear_ctx(set); |
| 4112 | return LY_SUCCESS; |
| 4113 | } |
| 4114 | |
| 4115 | if (arg_count) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4116 | if (args[0]->type != LYXP_SET_NODE_SET) { |
| 4117 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "local-name(node-set?)"); |
| 4118 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4119 | } else if (!args[0]->used) { |
| 4120 | set_fill_string(set, "", 0); |
| 4121 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4122 | } |
| 4123 | |
| 4124 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4125 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4126 | |
| 4127 | item = &args[0]->val.nodes[0]; |
| 4128 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4129 | if (set->type != LYXP_SET_NODE_SET) { |
| 4130 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "local-name(node-set?)"); |
| 4131 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4132 | } else if (!set->used) { |
| 4133 | set_fill_string(set, "", 0); |
| 4134 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4135 | } |
| 4136 | |
| 4137 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4138 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4139 | |
| 4140 | item = &set->val.nodes[0]; |
| 4141 | } |
| 4142 | |
| 4143 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4144 | case LYXP_NODE_NONE: |
| 4145 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4146 | case LYXP_NODE_ROOT: |
| 4147 | case LYXP_NODE_ROOT_CONFIG: |
| 4148 | case LYXP_NODE_TEXT: |
| 4149 | set_fill_string(set, "", 0); |
| 4150 | break; |
| 4151 | case LYXP_NODE_ELEM: |
| 4152 | set_fill_string(set, item->node->schema->name, strlen(item->node->schema->name)); |
| 4153 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4154 | case LYXP_NODE_META: |
| 4155 | 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] | 4156 | break; |
| 4157 | } |
| 4158 | |
| 4159 | return LY_SUCCESS; |
| 4160 | } |
| 4161 | |
| 4162 | /** |
| 4163 | * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING |
| 4164 | * with the node name fully qualified (with namespace) from the argument or the context. |
| 4165 | * |
| 4166 | * @param[in] args Array of arguments. |
| 4167 | * @param[in] arg_count Count of elements in @p args. |
| 4168 | * @param[in,out] set Context and result set at the same time. |
| 4169 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4170 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4171 | */ |
| 4172 | static LY_ERR |
| 4173 | xpath_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options) |
| 4174 | { |
| 4175 | struct lyxp_set_node *item; |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 4176 | struct lys_module *mod = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4177 | char *str; |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 4178 | const char *name = NULL; |
| 4179 | int rc = -1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4180 | |
| 4181 | if (options & LYXP_SCNODE_ALL) { |
| 4182 | set_scnode_clear_ctx(set); |
| 4183 | return LY_SUCCESS; |
| 4184 | } |
| 4185 | |
| 4186 | if (arg_count) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4187 | if (args[0]->type != LYXP_SET_NODE_SET) { |
| 4188 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "name(node-set?)"); |
| 4189 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4190 | } else if (!args[0]->used) { |
| 4191 | set_fill_string(set, "", 0); |
| 4192 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4193 | } |
| 4194 | |
| 4195 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4196 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4197 | |
| 4198 | item = &args[0]->val.nodes[0]; |
| 4199 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4200 | if (set->type != LYXP_SET_NODE_SET) { |
| 4201 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "name(node-set?)"); |
| 4202 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4203 | } else if (!set->used) { |
| 4204 | set_fill_string(set, "", 0); |
| 4205 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4206 | } |
| 4207 | |
| 4208 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4209 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4210 | |
| 4211 | item = &set->val.nodes[0]; |
| 4212 | } |
| 4213 | |
| 4214 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4215 | case LYXP_NODE_NONE: |
| 4216 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4217 | case LYXP_NODE_ROOT: |
| 4218 | case LYXP_NODE_ROOT_CONFIG: |
| 4219 | case LYXP_NODE_TEXT: |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 4220 | /* keep NULL */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4221 | break; |
| 4222 | case LYXP_NODE_ELEM: |
| 4223 | mod = item->node->schema->module; |
| 4224 | name = item->node->schema->name; |
| 4225 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4226 | case LYXP_NODE_META: |
| 4227 | mod = ((struct lyd_meta *)item->node)->annotation->module; |
| 4228 | name = ((struct lyd_meta *)item->node)->name; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4229 | break; |
| 4230 | } |
| 4231 | |
| 4232 | if (mod && name) { |
| 4233 | switch (set->format) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 4234 | case LYD_SCHEMA: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4235 | rc = asprintf(&str, "%s:%s", lys_prefix_find_module(set->local_mod, mod), name); |
| 4236 | break; |
| 4237 | case LYD_JSON: |
| 4238 | rc = asprintf(&str, "%s:%s", mod->name, name); |
| 4239 | break; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 4240 | case LYD_XML: |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 4241 | case LYD_LYB: |
Michal Vasko | 9409ef6 | 2019-09-12 11:47:17 +0200 | [diff] [blame] | 4242 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4243 | } |
| 4244 | LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM); |
| 4245 | set_fill_string(set, str, strlen(str)); |
| 4246 | free(str); |
| 4247 | } else { |
| 4248 | set_fill_string(set, "", 0); |
| 4249 | } |
| 4250 | |
| 4251 | return LY_SUCCESS; |
| 4252 | } |
| 4253 | |
| 4254 | /** |
| 4255 | * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING |
| 4256 | * with the namespace of the node from the argument or the context. |
| 4257 | * |
| 4258 | * @param[in] args Array of arguments. |
| 4259 | * @param[in] arg_count Count of elements in @p args. |
| 4260 | * @param[in,out] set Context and result set at the same time. |
| 4261 | * @param[in] options XPath options. |
| 4262 | * @return LY_ERR (LY_EINVAL for wrong arguments on schema) |
| 4263 | */ |
| 4264 | static LY_ERR |
| 4265 | xpath_namespace_uri(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options) |
| 4266 | { |
| 4267 | struct lyxp_set_node *item; |
| 4268 | struct lys_module *mod; |
| 4269 | /* suppress unused variable warning */ |
| 4270 | (void)options; |
| 4271 | |
| 4272 | if (options & LYXP_SCNODE_ALL) { |
| 4273 | set_scnode_clear_ctx(set); |
| 4274 | return LY_SUCCESS; |
| 4275 | } |
| 4276 | |
| 4277 | if (arg_count) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4278 | if (args[0]->type != LYXP_SET_NODE_SET) { |
| 4279 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), |
| 4280 | "namespace-uri(node-set?)"); |
| 4281 | return LY_EVALID; |
| 4282 | } else if (!args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4283 | set_fill_string(set, "", 0); |
| 4284 | return LY_SUCCESS; |
| 4285 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4286 | |
| 4287 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4288 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4289 | |
| 4290 | item = &args[0]->val.nodes[0]; |
| 4291 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4292 | if (set->type != LYXP_SET_NODE_SET) { |
| 4293 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "namespace-uri(node-set?)"); |
| 4294 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4295 | } else if (!set->used) { |
| 4296 | set_fill_string(set, "", 0); |
| 4297 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4298 | } |
| 4299 | |
| 4300 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4301 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4302 | |
| 4303 | item = &set->val.nodes[0]; |
| 4304 | } |
| 4305 | |
| 4306 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4307 | case LYXP_NODE_NONE: |
| 4308 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4309 | case LYXP_NODE_ROOT: |
| 4310 | case LYXP_NODE_ROOT_CONFIG: |
| 4311 | case LYXP_NODE_TEXT: |
| 4312 | set_fill_string(set, "", 0); |
| 4313 | break; |
| 4314 | case LYXP_NODE_ELEM: |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4315 | case LYXP_NODE_META: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4316 | if (item->type == LYXP_NODE_ELEM) { |
| 4317 | mod = item->node->schema->module; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4318 | } else { /* LYXP_NODE_META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4319 | /* annotations */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4320 | mod = ((struct lyd_meta *)item->node)->annotation->module; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4321 | } |
| 4322 | |
| 4323 | set_fill_string(set, mod->ns, strlen(mod->ns)); |
| 4324 | break; |
| 4325 | } |
| 4326 | |
| 4327 | return LY_SUCCESS; |
| 4328 | } |
| 4329 | |
| 4330 | /** |
| 4331 | * @brief Execute the XPath node() function (node type). Returns LYXP_SET_NODE_SET |
| 4332 | * with only nodes from the context. In practice it either leaves the context |
| 4333 | * as it is or returns an empty node set. |
| 4334 | * |
| 4335 | * @param[in] args Array of arguments. |
| 4336 | * @param[in] arg_count Count of elements in @p args. |
| 4337 | * @param[in,out] set Context and result set at the same time. |
| 4338 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4339 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4340 | */ |
| 4341 | static LY_ERR |
| 4342 | xpath_node(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 4343 | { |
| 4344 | if (options & LYXP_SCNODE_ALL) { |
| 4345 | set_scnode_clear_ctx(set); |
| 4346 | return LY_SUCCESS; |
| 4347 | } |
| 4348 | |
| 4349 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4350 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4351 | } |
| 4352 | return LY_SUCCESS; |
| 4353 | } |
| 4354 | |
| 4355 | /** |
| 4356 | * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING |
| 4357 | * with normalized value (no leading, trailing, double white spaces) of the node |
| 4358 | * from the argument or the context. |
| 4359 | * |
| 4360 | * @param[in] args Array of arguments. |
| 4361 | * @param[in] arg_count Count of elements in @p args. |
| 4362 | * @param[in,out] set Context and result set at the same time. |
| 4363 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4364 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4365 | */ |
| 4366 | static LY_ERR |
| 4367 | xpath_normalize_space(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options) |
| 4368 | { |
| 4369 | uint16_t i, new_used; |
| 4370 | char *new; |
| 4371 | int have_spaces = 0, space_before = 0; |
| 4372 | struct lysc_node_leaf *sleaf; |
| 4373 | LY_ERR rc = LY_SUCCESS; |
| 4374 | |
| 4375 | if (options & LYXP_SCNODE_ALL) { |
| 4376 | if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4377 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4378 | 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] | 4379 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4380 | 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] | 4381 | } |
| 4382 | } |
| 4383 | set_scnode_clear_ctx(set); |
| 4384 | return rc; |
| 4385 | } |
| 4386 | |
| 4387 | if (arg_count) { |
| 4388 | set_fill_set(set, args[0]); |
| 4389 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4390 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4391 | LY_CHECK_RET(rc); |
| 4392 | |
| 4393 | /* is there any normalization necessary? */ |
| 4394 | for (i = 0; set->val.str[i]; ++i) { |
| 4395 | if (is_xmlws(set->val.str[i])) { |
| 4396 | if ((i == 0) || space_before || (!set->val.str[i + 1])) { |
| 4397 | have_spaces = 1; |
| 4398 | break; |
| 4399 | } |
| 4400 | space_before = 1; |
| 4401 | } else { |
| 4402 | space_before = 0; |
| 4403 | } |
| 4404 | } |
| 4405 | |
| 4406 | /* yep, there is */ |
| 4407 | if (have_spaces) { |
| 4408 | /* it's enough, at least one character will go, makes space for ending '\0' */ |
| 4409 | new = malloc(strlen(set->val.str) * sizeof(char)); |
| 4410 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 4411 | new_used = 0; |
| 4412 | |
| 4413 | space_before = 0; |
| 4414 | for (i = 0; set->val.str[i]; ++i) { |
| 4415 | if (is_xmlws(set->val.str[i])) { |
| 4416 | if ((i == 0) || space_before) { |
| 4417 | space_before = 1; |
| 4418 | continue; |
| 4419 | } else { |
| 4420 | space_before = 1; |
| 4421 | } |
| 4422 | } else { |
| 4423 | space_before = 0; |
| 4424 | } |
| 4425 | |
| 4426 | new[new_used] = (space_before ? ' ' : set->val.str[i]); |
| 4427 | ++new_used; |
| 4428 | } |
| 4429 | |
| 4430 | /* at worst there is one trailing space now */ |
| 4431 | if (new_used && is_xmlws(new[new_used - 1])) { |
| 4432 | --new_used; |
| 4433 | } |
| 4434 | |
| 4435 | new = ly_realloc(new, (new_used + 1) * sizeof(char)); |
| 4436 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 4437 | new[new_used] = '\0'; |
| 4438 | |
| 4439 | free(set->val.str); |
| 4440 | set->val.str = new; |
| 4441 | } |
| 4442 | |
| 4443 | return LY_SUCCESS; |
| 4444 | } |
| 4445 | |
| 4446 | /** |
| 4447 | * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN |
| 4448 | * with the argument converted to boolean and logically inverted. |
| 4449 | * |
| 4450 | * @param[in] args Array of arguments. |
| 4451 | * @param[in] arg_count Count of elements in @p args. |
| 4452 | * @param[in,out] set Context and result set at the same time. |
| 4453 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4454 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4455 | */ |
| 4456 | static LY_ERR |
| 4457 | xpath_not(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 4458 | { |
| 4459 | if (options & LYXP_SCNODE_ALL) { |
| 4460 | set_scnode_clear_ctx(set); |
| 4461 | return LY_SUCCESS; |
| 4462 | } |
| 4463 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4464 | lyxp_set_cast(args[0], LYXP_SET_BOOLEAN); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4465 | if (args[0]->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4466 | set_fill_boolean(set, 0); |
| 4467 | } else { |
| 4468 | set_fill_boolean(set, 1); |
| 4469 | } |
| 4470 | |
| 4471 | return LY_SUCCESS; |
| 4472 | } |
| 4473 | |
| 4474 | /** |
| 4475 | * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER |
| 4476 | * with the number representation of either the argument or the context. |
| 4477 | * |
| 4478 | * @param[in] args Array of arguments. |
| 4479 | * @param[in] arg_count Count of elements in @p args. |
| 4480 | * @param[in,out] set Context and result set at the same time. |
| 4481 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4482 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4483 | */ |
| 4484 | static LY_ERR |
| 4485 | xpath_number(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options) |
| 4486 | { |
| 4487 | LY_ERR rc; |
| 4488 | |
| 4489 | if (options & LYXP_SCNODE_ALL) { |
| 4490 | set_scnode_clear_ctx(set); |
| 4491 | return LY_SUCCESS; |
| 4492 | } |
| 4493 | |
| 4494 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4495 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4496 | LY_CHECK_RET(rc); |
| 4497 | set_fill_set(set, args[0]); |
| 4498 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4499 | rc = lyxp_set_cast(set, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4500 | LY_CHECK_RET(rc); |
| 4501 | } |
| 4502 | |
| 4503 | return LY_SUCCESS; |
| 4504 | } |
| 4505 | |
| 4506 | /** |
| 4507 | * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER |
| 4508 | * with the context position. |
| 4509 | * |
| 4510 | * @param[in] args Array of arguments. |
| 4511 | * @param[in] arg_count Count of elements in @p args. |
| 4512 | * @param[in,out] set Context and result set at the same time. |
| 4513 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4514 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4515 | */ |
| 4516 | static LY_ERR |
| 4517 | xpath_position(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 4518 | { |
| 4519 | if (options & LYXP_SCNODE_ALL) { |
| 4520 | set_scnode_clear_ctx(set); |
| 4521 | return LY_SUCCESS; |
| 4522 | } |
| 4523 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4524 | if (set->type != LYXP_SET_NODE_SET) { |
| 4525 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "position()"); |
| 4526 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4527 | } else if (!set->used) { |
| 4528 | set_fill_number(set, 0); |
| 4529 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4530 | } |
| 4531 | |
| 4532 | set_fill_number(set, set->ctx_pos); |
| 4533 | |
| 4534 | /* UNUSED in 'Release' build type */ |
| 4535 | (void)options; |
| 4536 | return LY_SUCCESS; |
| 4537 | } |
| 4538 | |
| 4539 | /** |
| 4540 | * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN |
| 4541 | * depending on whether the second argument regex matches the first argument string. For details refer to |
| 4542 | * YANG 1.1 RFC section 10.2.1. |
| 4543 | * |
| 4544 | * @param[in] args Array of arguments. |
| 4545 | * @param[in] arg_count Count of elements in @p args. |
| 4546 | * @param[in,out] set Context and result set at the same time. |
| 4547 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4548 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4549 | */ |
| 4550 | static LY_ERR |
| 4551 | xpath_re_match(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 4552 | { |
| 4553 | struct lysc_pattern **patterns = NULL, **pattern; |
| 4554 | struct lysc_node_leaf *sleaf; |
| 4555 | char *path; |
| 4556 | LY_ERR rc = LY_SUCCESS; |
| 4557 | struct ly_err_item *err; |
| 4558 | |
| 4559 | if (options & LYXP_SCNODE_ALL) { |
| 4560 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4561 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4562 | 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] | 4563 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4564 | 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] | 4565 | } |
| 4566 | } |
| 4567 | |
| 4568 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4569 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4570 | 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] | 4571 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4572 | 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] | 4573 | } |
| 4574 | } |
| 4575 | set_scnode_clear_ctx(set); |
| 4576 | return rc; |
| 4577 | } |
| 4578 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4579 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4580 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4581 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4582 | LY_CHECK_RET(rc); |
| 4583 | |
| 4584 | LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM); |
| 4585 | *pattern = malloc(sizeof **pattern); |
| 4586 | path = lyd_path(set->ctx_node, LYD_PATH_LOG, NULL, 0); |
| 4587 | rc = lys_compile_type_pattern_check(set->ctx, path, args[1]->val.str, &(*pattern)->code); |
| 4588 | free(path); |
| 4589 | if (rc != LY_SUCCESS) { |
| 4590 | LY_ARRAY_FREE(patterns); |
| 4591 | return rc; |
| 4592 | } |
| 4593 | |
| 4594 | rc = ly_type_validate_patterns(patterns, args[0]->val.str, strlen(args[0]->val.str), &err); |
| 4595 | pcre2_code_free((*pattern)->code); |
| 4596 | free(*pattern); |
| 4597 | LY_ARRAY_FREE(patterns); |
| 4598 | if (rc && (rc != LY_EVALID)) { |
| 4599 | ly_err_print(err); |
| 4600 | ly_err_free(err); |
| 4601 | return rc; |
| 4602 | } |
| 4603 | |
| 4604 | if (rc == LY_EVALID) { |
| 4605 | ly_err_free(err); |
| 4606 | set_fill_boolean(set, 0); |
| 4607 | } else { |
| 4608 | set_fill_boolean(set, 1); |
| 4609 | } |
| 4610 | |
| 4611 | return LY_SUCCESS; |
| 4612 | } |
| 4613 | |
| 4614 | /** |
| 4615 | * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER |
| 4616 | * with the rounded first argument. For details refer to |
| 4617 | * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round. |
| 4618 | * |
| 4619 | * @param[in] args Array of arguments. |
| 4620 | * @param[in] arg_count Count of elements in @p args. |
| 4621 | * @param[in,out] set Context and result set at the same time. |
| 4622 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4623 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4624 | */ |
| 4625 | static LY_ERR |
| 4626 | xpath_round(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 4627 | { |
| 4628 | struct lysc_node_leaf *sleaf; |
| 4629 | LY_ERR rc = LY_SUCCESS; |
| 4630 | |
| 4631 | if (options & LYXP_SCNODE_ALL) { |
| 4632 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4633 | 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] | 4634 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4635 | 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] | 4636 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) { |
| 4637 | 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] | 4638 | } |
| 4639 | set_scnode_clear_ctx(set); |
| 4640 | return rc; |
| 4641 | } |
| 4642 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4643 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4644 | LY_CHECK_RET(rc); |
| 4645 | |
| 4646 | /* cover only the cases where floor can't be used */ |
| 4647 | if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) { |
| 4648 | set_fill_number(set, -0.0f); |
| 4649 | } else { |
| 4650 | args[0]->val.num += 0.5; |
| 4651 | rc = xpath_floor(args, 1, args[0], options); |
| 4652 | LY_CHECK_RET(rc); |
| 4653 | set_fill_number(set, args[0]->val.num); |
| 4654 | } |
| 4655 | |
| 4656 | return LY_SUCCESS; |
| 4657 | } |
| 4658 | |
| 4659 | /** |
| 4660 | * @brief Execute the XPath starts-with(string, string) function. |
| 4661 | * Returns LYXP_SET_BOOLEAN whether the second argument is |
| 4662 | * the prefix of the first or not. |
| 4663 | * |
| 4664 | * @param[in] args Array of arguments. |
| 4665 | * @param[in] arg_count Count of elements in @p args. |
| 4666 | * @param[in,out] set Context and result set at the same time. |
| 4667 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4668 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4669 | */ |
| 4670 | static LY_ERR |
| 4671 | xpath_starts_with(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 4672 | { |
| 4673 | struct lysc_node_leaf *sleaf; |
| 4674 | LY_ERR rc = LY_SUCCESS; |
| 4675 | |
| 4676 | if (options & LYXP_SCNODE_ALL) { |
| 4677 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4678 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4679 | 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] | 4680 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4681 | 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] | 4682 | } |
| 4683 | } |
| 4684 | |
| 4685 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4686 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4687 | 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] | 4688 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4689 | 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] | 4690 | } |
| 4691 | } |
| 4692 | set_scnode_clear_ctx(set); |
| 4693 | return rc; |
| 4694 | } |
| 4695 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4696 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4697 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4698 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4699 | LY_CHECK_RET(rc); |
| 4700 | |
| 4701 | if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) { |
| 4702 | set_fill_boolean(set, 0); |
| 4703 | } else { |
| 4704 | set_fill_boolean(set, 1); |
| 4705 | } |
| 4706 | |
| 4707 | return LY_SUCCESS; |
| 4708 | } |
| 4709 | |
| 4710 | /** |
| 4711 | * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING |
| 4712 | * with the string representation of either the argument or the context. |
| 4713 | * |
| 4714 | * @param[in] args Array of arguments. |
| 4715 | * @param[in] arg_count Count of elements in @p args. |
| 4716 | * @param[in,out] set Context and result set at the same time. |
| 4717 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4718 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4719 | */ |
| 4720 | static LY_ERR |
| 4721 | xpath_string(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options) |
| 4722 | { |
| 4723 | LY_ERR rc; |
| 4724 | |
| 4725 | if (options & LYXP_SCNODE_ALL) { |
| 4726 | set_scnode_clear_ctx(set); |
| 4727 | return LY_SUCCESS; |
| 4728 | } |
| 4729 | |
| 4730 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4731 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4732 | LY_CHECK_RET(rc); |
| 4733 | set_fill_set(set, args[0]); |
| 4734 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4735 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4736 | LY_CHECK_RET(rc); |
| 4737 | } |
| 4738 | |
| 4739 | return LY_SUCCESS; |
| 4740 | } |
| 4741 | |
| 4742 | /** |
| 4743 | * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER |
| 4744 | * with the length of the string in either the argument or the context. |
| 4745 | * |
| 4746 | * @param[in] args Array of arguments. |
| 4747 | * @param[in] arg_count Count of elements in @p args. |
| 4748 | * @param[in,out] set Context and result set at the same time. |
| 4749 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4750 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4751 | */ |
| 4752 | static LY_ERR |
| 4753 | xpath_string_length(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options) |
| 4754 | { |
| 4755 | struct lysc_node_leaf *sleaf; |
| 4756 | LY_ERR rc = LY_SUCCESS; |
| 4757 | |
| 4758 | if (options & LYXP_SCNODE_ALL) { |
| 4759 | if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4760 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4761 | 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] | 4762 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4763 | 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] | 4764 | } |
| 4765 | } |
| 4766 | if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) { |
| 4767 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4768 | 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] | 4769 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4770 | 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] | 4771 | } |
| 4772 | } |
| 4773 | set_scnode_clear_ctx(set); |
| 4774 | return rc; |
| 4775 | } |
| 4776 | |
| 4777 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4778 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4779 | LY_CHECK_RET(rc); |
| 4780 | set_fill_number(set, strlen(args[0]->val.str)); |
| 4781 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4782 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4783 | LY_CHECK_RET(rc); |
| 4784 | set_fill_number(set, strlen(set->val.str)); |
| 4785 | } |
| 4786 | |
| 4787 | return LY_SUCCESS; |
| 4788 | } |
| 4789 | |
| 4790 | /** |
| 4791 | * @brief Execute the XPath substring(string, number, number?) function. |
| 4792 | * Returns LYXP_SET_STRING substring of the first argument starting |
| 4793 | * on the second argument index ending on the third argument index, |
| 4794 | * indexed from 1. For exact definition refer to |
| 4795 | * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring. |
| 4796 | * |
| 4797 | * @param[in] args Array of arguments. |
| 4798 | * @param[in] arg_count Count of elements in @p args. |
| 4799 | * @param[in,out] set Context and result set at the same time. |
| 4800 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4801 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4802 | */ |
| 4803 | static LY_ERR |
| 4804 | xpath_substring(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options) |
| 4805 | { |
| 4806 | int start, len; |
| 4807 | uint16_t str_start, str_len, pos; |
| 4808 | struct lysc_node_leaf *sleaf; |
| 4809 | LY_ERR rc = LY_SUCCESS; |
| 4810 | |
| 4811 | if (options & LYXP_SCNODE_ALL) { |
| 4812 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4813 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4814 | 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] | 4815 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4816 | 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] | 4817 | } |
| 4818 | } |
| 4819 | |
| 4820 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4821 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4822 | 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] | 4823 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 4824 | 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] | 4825 | } |
| 4826 | } |
| 4827 | |
| 4828 | if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET) |
| 4829 | && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) { |
| 4830 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4831 | 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] | 4832 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 4833 | 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] | 4834 | } |
| 4835 | } |
| 4836 | set_scnode_clear_ctx(set); |
| 4837 | return rc; |
| 4838 | } |
| 4839 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4840 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4841 | LY_CHECK_RET(rc); |
| 4842 | |
| 4843 | /* start */ |
| 4844 | if (xpath_round(&args[1], 1, args[1], options)) { |
| 4845 | return -1; |
| 4846 | } |
| 4847 | if (isfinite(args[1]->val.num)) { |
| 4848 | start = args[1]->val.num - 1; |
| 4849 | } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) { |
| 4850 | start = INT_MIN; |
| 4851 | } else { |
| 4852 | start = INT_MAX; |
| 4853 | } |
| 4854 | |
| 4855 | /* len */ |
| 4856 | if (arg_count == 3) { |
| 4857 | rc = xpath_round(&args[2], 1, args[2], options); |
| 4858 | LY_CHECK_RET(rc); |
| 4859 | if (isfinite(args[2]->val.num)) { |
| 4860 | len = args[2]->val.num; |
| 4861 | } else if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) { |
| 4862 | len = 0; |
| 4863 | } else { |
| 4864 | len = INT_MAX; |
| 4865 | } |
| 4866 | } else { |
| 4867 | len = INT_MAX; |
| 4868 | } |
| 4869 | |
| 4870 | /* find matching character positions */ |
| 4871 | str_start = 0; |
| 4872 | str_len = 0; |
| 4873 | for (pos = 0; args[0]->val.str[pos]; ++pos) { |
| 4874 | if (pos < start) { |
| 4875 | ++str_start; |
| 4876 | } else if (pos < start + len) { |
| 4877 | ++str_len; |
| 4878 | } else { |
| 4879 | break; |
| 4880 | } |
| 4881 | } |
| 4882 | |
| 4883 | set_fill_string(set, args[0]->val.str + str_start, str_len); |
| 4884 | return LY_SUCCESS; |
| 4885 | } |
| 4886 | |
| 4887 | /** |
| 4888 | * @brief Execute the XPath substring-after(string, string) function. |
| 4889 | * Returns LYXP_SET_STRING with the string succeeding the occurance |
| 4890 | * of the second argument in the first or an empty string. |
| 4891 | * |
| 4892 | * @param[in] args Array of arguments. |
| 4893 | * @param[in] arg_count Count of elements in @p args. |
| 4894 | * @param[in,out] set Context and result set at the same time. |
| 4895 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4896 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4897 | */ |
| 4898 | static LY_ERR |
| 4899 | xpath_substring_after(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 4900 | { |
| 4901 | char *ptr; |
| 4902 | struct lysc_node_leaf *sleaf; |
| 4903 | LY_ERR rc = LY_SUCCESS; |
| 4904 | |
| 4905 | if (options & LYXP_SCNODE_ALL) { |
| 4906 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4907 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4908 | 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] | 4909 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4910 | 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] | 4911 | } |
| 4912 | } |
| 4913 | |
| 4914 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4915 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4916 | 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] | 4917 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4918 | 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] | 4919 | } |
| 4920 | } |
| 4921 | set_scnode_clear_ctx(set); |
| 4922 | return rc; |
| 4923 | } |
| 4924 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4925 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4926 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4927 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4928 | LY_CHECK_RET(rc); |
| 4929 | |
| 4930 | ptr = strstr(args[0]->val.str, args[1]->val.str); |
| 4931 | if (ptr) { |
| 4932 | set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str))); |
| 4933 | } else { |
| 4934 | set_fill_string(set, "", 0); |
| 4935 | } |
| 4936 | |
| 4937 | return LY_SUCCESS; |
| 4938 | } |
| 4939 | |
| 4940 | /** |
| 4941 | * @brief Execute the XPath substring-before(string, string) function. |
| 4942 | * Returns LYXP_SET_STRING with the string preceding the occurance |
| 4943 | * of the second argument in the first or an empty string. |
| 4944 | * |
| 4945 | * @param[in] args Array of arguments. |
| 4946 | * @param[in] arg_count Count of elements in @p args. |
| 4947 | * @param[in,out] set Context and result set at the same time. |
| 4948 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4949 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4950 | */ |
| 4951 | static LY_ERR |
| 4952 | xpath_substring_before(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 4953 | { |
| 4954 | char *ptr; |
| 4955 | struct lysc_node_leaf *sleaf; |
| 4956 | LY_ERR rc = LY_SUCCESS; |
| 4957 | |
| 4958 | if (options & LYXP_SCNODE_ALL) { |
| 4959 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4960 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4961 | 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] | 4962 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4963 | 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] | 4964 | } |
| 4965 | } |
| 4966 | |
| 4967 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4968 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4969 | 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] | 4970 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4971 | 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] | 4972 | } |
| 4973 | } |
| 4974 | set_scnode_clear_ctx(set); |
| 4975 | return rc; |
| 4976 | } |
| 4977 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4978 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4979 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4980 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4981 | LY_CHECK_RET(rc); |
| 4982 | |
| 4983 | ptr = strstr(args[0]->val.str, args[1]->val.str); |
| 4984 | if (ptr) { |
| 4985 | set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str); |
| 4986 | } else { |
| 4987 | set_fill_string(set, "", 0); |
| 4988 | } |
| 4989 | |
| 4990 | return LY_SUCCESS; |
| 4991 | } |
| 4992 | |
| 4993 | /** |
| 4994 | * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER |
| 4995 | * with the sum of all the nodes in the context. |
| 4996 | * |
| 4997 | * @param[in] args Array of arguments. |
| 4998 | * @param[in] arg_count Count of elements in @p args. |
| 4999 | * @param[in,out] set Context and result set at the same time. |
| 5000 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5001 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5002 | */ |
| 5003 | static LY_ERR |
| 5004 | xpath_sum(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 5005 | { |
| 5006 | long double num; |
| 5007 | char *str; |
| 5008 | uint16_t i; |
| 5009 | struct lyxp_set set_item; |
| 5010 | struct lysc_node_leaf *sleaf; |
| 5011 | LY_ERR rc = LY_SUCCESS; |
| 5012 | |
| 5013 | if (options & LYXP_SCNODE_ALL) { |
| 5014 | if (args[0]->type == LYXP_SET_SCNODE_SET) { |
| 5015 | for (i = 0; i < args[0]->used; ++i) { |
| 5016 | if (args[0]->val.scnodes[i].in_ctx == 1) { |
| 5017 | sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode; |
| 5018 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5019 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, |
| 5020 | lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5021 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 5022 | 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] | 5023 | } |
| 5024 | } |
| 5025 | } |
| 5026 | } |
| 5027 | set_scnode_clear_ctx(set); |
| 5028 | return rc; |
| 5029 | } |
| 5030 | |
| 5031 | set_fill_number(set, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5032 | |
| 5033 | if (args[0]->type != LYXP_SET_NODE_SET) { |
| 5034 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "sum(node-set)"); |
| 5035 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5036 | } else if (!args[0]->used) { |
| 5037 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5038 | } |
| 5039 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5040 | set_init(&set_item, set); |
| 5041 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5042 | set_item.type = LYXP_SET_NODE_SET; |
| 5043 | set_item.val.nodes = malloc(sizeof *set_item.val.nodes); |
| 5044 | LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM); |
| 5045 | |
| 5046 | set_item.used = 1; |
| 5047 | set_item.size = 1; |
| 5048 | |
| 5049 | for (i = 0; i < args[0]->used; ++i) { |
| 5050 | set_item.val.nodes[0] = args[0]->val.nodes[i]; |
| 5051 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5052 | rc = cast_node_set_to_string(&set_item, &str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5053 | LY_CHECK_RET(rc); |
| 5054 | num = cast_string_to_number(str); |
| 5055 | free(str); |
| 5056 | set->val.num += num; |
| 5057 | } |
| 5058 | |
| 5059 | free(set_item.val.nodes); |
| 5060 | |
| 5061 | return LY_SUCCESS; |
| 5062 | } |
| 5063 | |
| 5064 | /** |
| 5065 | * @brief Execute the XPath text() function (node type). Returns LYXP_SET_NODE_SET |
| 5066 | * with the text content of the nodes in the context. |
| 5067 | * |
| 5068 | * @param[in] args Array of arguments. |
| 5069 | * @param[in] arg_count Count of elements in @p args. |
| 5070 | * @param[in,out] set Context and result set at the same time. |
| 5071 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5072 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5073 | */ |
| 5074 | static LY_ERR |
| 5075 | xpath_text(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 5076 | { |
| 5077 | uint32_t i; |
| 5078 | |
| 5079 | if (options & LYXP_SCNODE_ALL) { |
| 5080 | set_scnode_clear_ctx(set); |
| 5081 | return LY_SUCCESS; |
| 5082 | } |
| 5083 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5084 | if (set->type != LYXP_SET_NODE_SET) { |
| 5085 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "text()"); |
| 5086 | return LY_EVALID; |
| 5087 | } |
| 5088 | |
| 5089 | for (i = 0; i < set->used;) { |
| 5090 | switch (set->val.nodes[i].type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 5091 | case LYXP_NODE_NONE: |
| 5092 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5093 | case LYXP_NODE_ELEM: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5094 | if (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 5095 | set->val.nodes[i].type = LYXP_NODE_TEXT; |
| 5096 | ++i; |
| 5097 | break; |
| 5098 | } |
| 5099 | /* fall through */ |
| 5100 | case LYXP_NODE_ROOT: |
| 5101 | case LYXP_NODE_ROOT_CONFIG: |
| 5102 | case LYXP_NODE_TEXT: |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5103 | case LYXP_NODE_META: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5104 | set_remove_node(set, i); |
| 5105 | break; |
| 5106 | } |
| 5107 | } |
| 5108 | |
| 5109 | return LY_SUCCESS; |
| 5110 | } |
| 5111 | |
| 5112 | /** |
| 5113 | * @brief Execute the XPath translate(string, string, string) function. |
| 5114 | * Returns LYXP_SET_STRING with the first argument with the characters |
| 5115 | * from the second argument replaced by those on the corresponding |
| 5116 | * positions in the third argument. |
| 5117 | * |
| 5118 | * @param[in] args Array of arguments. |
| 5119 | * @param[in] arg_count Count of elements in @p args. |
| 5120 | * @param[in,out] set Context and result set at the same time. |
| 5121 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5122 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5123 | */ |
| 5124 | static LY_ERR |
| 5125 | xpath_translate(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 5126 | { |
| 5127 | uint16_t i, j, new_used; |
| 5128 | char *new; |
| 5129 | int found, have_removed; |
| 5130 | struct lysc_node_leaf *sleaf; |
| 5131 | LY_ERR rc = LY_SUCCESS; |
| 5132 | |
| 5133 | if (options & LYXP_SCNODE_ALL) { |
| 5134 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 5135 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5136 | 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] | 5137 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5138 | 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] | 5139 | } |
| 5140 | } |
| 5141 | |
| 5142 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 5143 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5144 | 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] | 5145 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5146 | 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] | 5147 | } |
| 5148 | } |
| 5149 | |
| 5150 | if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) { |
| 5151 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5152 | 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] | 5153 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5154 | 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] | 5155 | } |
| 5156 | } |
| 5157 | set_scnode_clear_ctx(set); |
| 5158 | return rc; |
| 5159 | } |
| 5160 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5161 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5162 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5163 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5164 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5165 | rc = lyxp_set_cast(args[2], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5166 | LY_CHECK_RET(rc); |
| 5167 | |
| 5168 | new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char)); |
| 5169 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 5170 | new_used = 0; |
| 5171 | |
| 5172 | have_removed = 0; |
| 5173 | for (i = 0; args[0]->val.str[i]; ++i) { |
| 5174 | found = 0; |
| 5175 | |
| 5176 | for (j = 0; args[1]->val.str[j]; ++j) { |
| 5177 | if (args[0]->val.str[i] == args[1]->val.str[j]) { |
| 5178 | /* removing this char */ |
| 5179 | if (j >= strlen(args[2]->val.str)) { |
| 5180 | have_removed = 1; |
| 5181 | found = 1; |
| 5182 | break; |
| 5183 | } |
| 5184 | /* replacing this char */ |
| 5185 | new[new_used] = args[2]->val.str[j]; |
| 5186 | ++new_used; |
| 5187 | found = 1; |
| 5188 | break; |
| 5189 | } |
| 5190 | } |
| 5191 | |
| 5192 | /* copying this char */ |
| 5193 | if (!found) { |
| 5194 | new[new_used] = args[0]->val.str[i]; |
| 5195 | ++new_used; |
| 5196 | } |
| 5197 | } |
| 5198 | |
| 5199 | if (have_removed) { |
| 5200 | new = ly_realloc(new, (new_used + 1) * sizeof(char)); |
| 5201 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 5202 | } |
| 5203 | new[new_used] = '\0'; |
| 5204 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5205 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5206 | set->type = LYXP_SET_STRING; |
| 5207 | set->val.str = new; |
| 5208 | |
| 5209 | return LY_SUCCESS; |
| 5210 | } |
| 5211 | |
| 5212 | /** |
| 5213 | * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN |
| 5214 | * with true value. |
| 5215 | * |
| 5216 | * @param[in] args Array of arguments. |
| 5217 | * @param[in] arg_count Count of elements in @p args. |
| 5218 | * @param[in,out] set Context and result set at the same time. |
| 5219 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5220 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5221 | */ |
| 5222 | static LY_ERR |
| 5223 | xpath_true(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 5224 | { |
| 5225 | if (options & LYXP_SCNODE_ALL) { |
| 5226 | set_scnode_clear_ctx(set); |
| 5227 | return LY_SUCCESS; |
| 5228 | } |
| 5229 | |
| 5230 | set_fill_boolean(set, 1); |
| 5231 | return LY_SUCCESS; |
| 5232 | } |
| 5233 | |
| 5234 | /* |
| 5235 | * moveto functions |
| 5236 | * |
| 5237 | * They and only they actually change the context (set). |
| 5238 | */ |
| 5239 | |
| 5240 | /** |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5241 | * @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] | 5242 | * |
Michal Vasko | 2104e9f | 2020-03-06 08:23:25 +0100 | [diff] [blame] | 5243 | * XPath @p set is expected to be a (sc)node set! |
| 5244 | * |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5245 | * @param[in,out] qname Qualified node name. If includes prefix, it is skipped. |
| 5246 | * @param[in,out] qname_len Length of @p qname, is updated accordingly. |
| 5247 | * @param[in] set Set with XPath context. |
| 5248 | * @param[out] moveto_mod Expected module of a matching node. |
| 5249 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5250 | */ |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5251 | static LY_ERR |
| 5252 | 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] | 5253 | { |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 5254 | const struct lys_module *mod = NULL; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5255 | const char *ptr; |
| 5256 | int pref_len; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5257 | char *str; |
| 5258 | |
Michal Vasko | 2104e9f | 2020-03-06 08:23:25 +0100 | [diff] [blame] | 5259 | assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET)); |
| 5260 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5261 | if ((ptr = ly_strnchr(*qname, ':', *qname_len))) { |
| 5262 | /* specific module */ |
| 5263 | pref_len = ptr - *qname; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5264 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5265 | switch (set->format) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 5266 | case LYD_SCHEMA: |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5267 | /* schema, search all local module imports */ |
| 5268 | mod = lys_module_find_prefix(set->local_mod, *qname, pref_len); |
| 5269 | break; |
| 5270 | case LYD_JSON: |
| 5271 | /* JSON data, search in context */ |
| 5272 | str = strndup(*qname, pref_len); |
Michal Vasko | 97e76fd | 2020-05-27 15:22:01 +0200 | [diff] [blame] | 5273 | mod = ly_ctx_get_module_implemented(set->ctx, str); |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5274 | free(str); |
| 5275 | break; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 5276 | case LYD_XML: |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 5277 | case LYD_LYB: |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5278 | LOGINT_RET(set->ctx); |
| 5279 | } |
| 5280 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5281 | /* check for errors and non-implemented modules, as they are not valid */ |
Juraj Vijtiuk | d75faa6 | 2019-11-26 14:10:10 +0100 | [diff] [blame] | 5282 | if (!mod || !mod->implemented) { |
Michal Vasko | 2104e9f | 2020-03-06 08:23:25 +0100 | [diff] [blame] | 5283 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 5284 | LOGVAL(set->ctx, LY_VLOG_LYSC, set->ctx_scnode, LY_VCODE_XP_INMOD, pref_len, *qname); |
| 5285 | } else { |
| 5286 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INMOD, pref_len, *qname); |
| 5287 | } |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5288 | return LY_EVALID; |
| 5289 | } |
Juraj Vijtiuk | d75faa6 | 2019-11-26 14:10:10 +0100 | [diff] [blame] | 5290 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5291 | *qname += pref_len + 1; |
| 5292 | *qname_len -= pref_len + 1; |
| 5293 | } else if (((*qname)[0] == '*') && (*qname_len == 1)) { |
| 5294 | /* all modules - special case */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5295 | mod = NULL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5296 | } else if (set->type == LYXP_SET_SCNODE_SET) { |
| 5297 | /* current node module */ |
| 5298 | mod = set->ctx_scnode->module; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5299 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5300 | /* current node module */ |
| 5301 | mod = set->ctx_node->schema->module; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5302 | } |
| 5303 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5304 | *moveto_mod = mod; |
| 5305 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5306 | } |
| 5307 | |
| 5308 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5309 | * @brief Move context @p set to the root. Handles absolute path. |
| 5310 | * Result is LYXP_SET_NODE_SET. |
| 5311 | * |
| 5312 | * @param[in,out] set Set to use. |
| 5313 | * @param[in] options Xpath options. |
| 5314 | */ |
| 5315 | static void |
| 5316 | moveto_root(struct lyxp_set *set, int options) |
| 5317 | { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5318 | if (!set) { |
| 5319 | return; |
| 5320 | } |
| 5321 | |
| 5322 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5323 | set_scnode_clear_ctx(set); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 5324 | lyxp_set_scnode_insert_node(set, NULL, set->root_type); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5325 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5326 | set->type = LYXP_SET_NODE_SET; |
| 5327 | set->used = 0; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5328 | set_insert_node(set, NULL, 0, set->root_type, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5329 | } |
| 5330 | } |
| 5331 | |
| 5332 | /** |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 5333 | * @brief Check whether a node has some unresolved "when". |
| 5334 | * |
| 5335 | * @param[in] node Node to check. |
| 5336 | * @return LY_ERR value (LY_EINCOMPLETE if there are some unresolved "when") |
| 5337 | */ |
| 5338 | static LY_ERR |
| 5339 | moveto_when_check(const struct lyd_node *node) |
| 5340 | { |
| 5341 | const struct lysc_node *schema; |
| 5342 | |
| 5343 | if (!node) { |
| 5344 | return LY_SUCCESS; |
| 5345 | } |
| 5346 | |
| 5347 | schema = node->schema; |
| 5348 | do { |
| 5349 | if (schema->when && !(node->flags & LYD_WHEN_TRUE)) { |
| 5350 | return LY_EINCOMPLETE; |
| 5351 | } |
| 5352 | schema = schema->parent; |
| 5353 | } while (schema && (schema->nodetype & (LYS_CASE | LYS_CHOICE))); |
| 5354 | |
| 5355 | return LY_SUCCESS; |
| 5356 | } |
| 5357 | |
| 5358 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5359 | * @brief Check @p node as a part of NameTest processing. |
| 5360 | * |
| 5361 | * @param[in] node Node to check. |
| 5362 | * @param[in] root_type XPath root node type. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5363 | * @param[in] node_name Node name in the dictionary to move to, NULL for any node. |
| 5364 | * @param[in] moveto_mod Expected module of the node, NULL for any. |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5365 | * @return LY_ERR (LY_ENOT if node does not match, LY_EINCOMPLETE on unresolved when, |
| 5366 | * LY_EINVAL if netither node nor any children match) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5367 | */ |
| 5368 | static LY_ERR |
| 5369 | moveto_node_check(const struct lyd_node *node, enum lyxp_node_type root_type, const char *node_name, |
| 5370 | const struct lys_module *moveto_mod) |
| 5371 | { |
| 5372 | /* module check */ |
| 5373 | if (moveto_mod && (node->schema->module != moveto_mod)) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5374 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5375 | } |
| 5376 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5377 | /* context check */ |
| 5378 | 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] | 5379 | return LY_EINVAL; |
| 5380 | } |
| 5381 | |
| 5382 | /* name check */ |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5383 | if (node_name && strcmp(node_name, "*") && (node->schema->name != node_name)) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5384 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5385 | } |
| 5386 | |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 5387 | /* when check */ |
| 5388 | if (moveto_when_check(node)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5389 | return LY_EINCOMPLETE; |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 5390 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5391 | |
| 5392 | /* match */ |
| 5393 | return LY_SUCCESS; |
| 5394 | } |
| 5395 | |
| 5396 | /** |
| 5397 | * @brief Check @p node as a part of schema NameTest processing. |
| 5398 | * |
| 5399 | * @param[in] node Schema node to check. |
| 5400 | * @param[in] root_type XPath root node type. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5401 | * @param[in] node_name Node name in the dictionary to move to, NULL for any nodes. |
| 5402 | * @param[in] moveto_mod Expected module of the node, NULL for any. |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5403 | * @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] | 5404 | */ |
| 5405 | static LY_ERR |
| 5406 | moveto_scnode_check(const struct lysc_node *node, enum lyxp_node_type root_type, const char *node_name, |
Michal Vasko | cafad9d | 2019-11-07 15:20:03 +0100 | [diff] [blame] | 5407 | const struct lys_module *moveto_mod) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5408 | { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5409 | /* module check */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5410 | if (moveto_mod && (node->module != moveto_mod)) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5411 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5412 | } |
| 5413 | |
| 5414 | /* context check */ |
| 5415 | if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) { |
| 5416 | return LY_EINVAL; |
| 5417 | } |
| 5418 | |
| 5419 | /* name check */ |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5420 | if (node_name && strcmp(node_name, "*") && (node->name != node_name)) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5421 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5422 | } |
| 5423 | |
| 5424 | /* match */ |
| 5425 | return LY_SUCCESS; |
| 5426 | } |
| 5427 | |
| 5428 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5429 | * @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] | 5430 | * |
| 5431 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5432 | * @param[in] mod Matching node module, NULL for any. |
| 5433 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5434 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 5435 | */ |
| 5436 | static LY_ERR |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5437 | 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] | 5438 | { |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 5439 | uint32_t i; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5440 | int replaced; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5441 | const struct lyd_node *siblings, *sub; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5442 | LY_ERR rc; |
| 5443 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5444 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5445 | return LY_SUCCESS; |
| 5446 | } |
| 5447 | |
| 5448 | if (set->type != LYXP_SET_NODE_SET) { |
| 5449 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 5450 | return LY_EVALID; |
| 5451 | } |
| 5452 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5453 | for (i = 0; i < set->used; ) { |
| 5454 | replaced = 0; |
| 5455 | |
| 5456 | 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] | 5457 | assert(!set->val.nodes[i].node); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5458 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5459 | /* search in all the trees */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5460 | siblings = set->tree; |
Michal Vasko | 5bfd4be | 2020-06-23 13:26:19 +0200 | [diff] [blame] | 5461 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5462 | /* search in children */ |
Michal Vasko | 5bfd4be | 2020-06-23 13:26:19 +0200 | [diff] [blame] | 5463 | siblings = lyd_node_children(set->val.nodes[i].node, 0); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5464 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5465 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5466 | for (sub = siblings; sub; sub = sub->next) { |
| 5467 | rc = moveto_node_check(sub, set->root_type, ncname, mod); |
| 5468 | if (rc == LY_SUCCESS) { |
| 5469 | if (!replaced) { |
| 5470 | set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i); |
| 5471 | replaced = 1; |
| 5472 | } else { |
| 5473 | set_insert_node(set, sub, 0, LYXP_NODE_ELEM, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5474 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5475 | ++i; |
| 5476 | } else if (rc == LY_EINCOMPLETE) { |
| 5477 | return rc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5478 | } |
| 5479 | } |
| 5480 | |
| 5481 | if (!replaced) { |
| 5482 | /* no match */ |
| 5483 | set_remove_node(set, i); |
| 5484 | } |
| 5485 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5486 | |
| 5487 | return LY_SUCCESS; |
| 5488 | } |
| 5489 | |
| 5490 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5491 | * @brief Move context @p set to a node using hashes. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY). |
| 5492 | * Context position aware. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5493 | * |
| 5494 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5495 | * @param[in] scnode Matching node schema. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5496 | * @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] | 5497 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 5498 | */ |
| 5499 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5500 | 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] | 5501 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5502 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5503 | uint32_t i; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5504 | const struct lyd_node *siblings; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5505 | struct lyd_node *sub, *inst = NULL; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5506 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5507 | assert(scnode && (!(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) || predicates)); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5508 | |
| 5509 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5510 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5511 | } |
| 5512 | |
| 5513 | if (set->type != LYXP_SET_NODE_SET) { |
| 5514 | 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] | 5515 | ret = LY_EVALID; |
| 5516 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5517 | } |
| 5518 | |
| 5519 | /* context check for all the nodes since we have the schema node */ |
| 5520 | if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) { |
| 5521 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5522 | goto cleanup; |
| 5523 | } |
| 5524 | |
| 5525 | /* create specific data instance if needed */ |
| 5526 | if (scnode->nodetype == LYS_LIST) { |
| 5527 | LY_CHECK_GOTO(ret = lyd_create_list(scnode, predicates, &inst), cleanup); |
| 5528 | } else if (scnode->nodetype == LYS_LEAFLIST) { |
| 5529 | 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] | 5530 | } |
| 5531 | |
| 5532 | for (i = 0; i < set->used; ) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5533 | siblings = NULL; |
| 5534 | |
| 5535 | if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) { |
| 5536 | assert(!set->val.nodes[i].node); |
| 5537 | |
| 5538 | /* search in all the trees */ |
| 5539 | siblings = set->tree; |
| 5540 | } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
| 5541 | /* search in children */ |
Michal Vasko | 5bfd4be | 2020-06-23 13:26:19 +0200 | [diff] [blame] | 5542 | siblings = lyd_node_children(set->val.nodes[i].node, 0); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5543 | } |
| 5544 | |
| 5545 | /* find the node using hashes */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5546 | if (inst) { |
| 5547 | lyd_find_sibling_first(siblings, inst, &sub); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5548 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5549 | lyd_find_sibling_val(siblings, scnode, NULL, 0, &sub); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5550 | } |
| 5551 | |
| 5552 | /* when check */ |
| 5553 | if (sub && moveto_when_check(sub)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5554 | ret = LY_EINCOMPLETE; |
| 5555 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5556 | } |
| 5557 | |
| 5558 | if (sub) { |
| 5559 | /* pos filled later */ |
Michal Vasko | cb1b7c0 | 2020-07-03 13:38:12 +0200 | [diff] [blame] | 5560 | set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5561 | ++i; |
Michal Vasko | cb1b7c0 | 2020-07-03 13:38:12 +0200 | [diff] [blame] | 5562 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5563 | /* no match */ |
| 5564 | set_remove_node(set, i); |
| 5565 | } |
| 5566 | } |
| 5567 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5568 | cleanup: |
| 5569 | lyd_free_tree(inst); |
| 5570 | return ret; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5571 | } |
| 5572 | |
| 5573 | /** |
| 5574 | * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY). |
| 5575 | * |
| 5576 | * @param[in,out] set Set to use. |
| 5577 | * @param[in] mod Matching node module, NULL for any. |
| 5578 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5579 | * @param[in] options XPath options. |
| 5580 | * @return LY_ERR |
| 5581 | */ |
| 5582 | static LY_ERR |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5583 | 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] | 5584 | { |
Michal Vasko | cafad9d | 2019-11-07 15:20:03 +0100 | [diff] [blame] | 5585 | int i, orig_used, idx, temp_ctx = 0, getnext_opts; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5586 | uint32_t mod_idx; |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 5587 | const struct lysc_node *iter, *start_parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5588 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5589 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5590 | return LY_SUCCESS; |
| 5591 | } |
| 5592 | |
| 5593 | if (set->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 5594 | 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] | 5595 | return LY_EVALID; |
| 5596 | } |
| 5597 | |
Michal Vasko | cafad9d | 2019-11-07 15:20:03 +0100 | [diff] [blame] | 5598 | /* getnext opts */ |
| 5599 | getnext_opts = LYS_GETNEXT_NOSTATECHECK; |
| 5600 | if (options & LYXP_SCNODE_OUTPUT) { |
| 5601 | getnext_opts |= LYS_GETNEXT_OUTPUT; |
| 5602 | } |
| 5603 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5604 | orig_used = set->used; |
| 5605 | for (i = 0; i < orig_used; ++i) { |
| 5606 | if (set->val.scnodes[i].in_ctx != 1) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5607 | if (set->val.scnodes[i].in_ctx != -2) { |
| 5608 | continue; |
| 5609 | } |
| 5610 | |
| 5611 | /* remember context node */ |
| 5612 | set->val.scnodes[i].in_ctx = -1; |
Michal Vasko | ec4df48 | 2019-12-16 10:02:18 +0100 | [diff] [blame] | 5613 | } else { |
| 5614 | set->val.scnodes[i].in_ctx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5615 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5616 | |
| 5617 | start_parent = set->val.scnodes[i].scnode; |
| 5618 | |
| 5619 | 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] | 5620 | /* 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] | 5621 | * so use it directly (root node itself is useless in this case) */ |
| 5622 | mod_idx = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5623 | 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] | 5624 | iter = NULL; |
Michal Vasko | 509de4d | 2019-12-10 14:51:30 +0100 | [diff] [blame] | 5625 | /* module may not be implemented */ |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 5626 | while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) { |
| 5627 | if (!moveto_scnode_check(iter, set->root_type, ncname, mod)) { |
| 5628 | idx = lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5629 | /* we need to prevent these nodes from being considered in this moveto */ |
| 5630 | if ((idx < orig_used) && (idx > i)) { |
| 5631 | set->val.scnodes[idx].in_ctx = 2; |
| 5632 | temp_ctx = 1; |
| 5633 | } |
| 5634 | } |
| 5635 | } |
| 5636 | |
| 5637 | if (!mod_idx) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5638 | /* mod was specified, we are not going through the whole context */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5639 | break; |
| 5640 | } |
| 5641 | /* next iteration */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5642 | mod = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5643 | } |
| 5644 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 5645 | } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) { |
| 5646 | iter = NULL; |
| 5647 | while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) { |
| 5648 | if (!moveto_scnode_check(iter, set->root_type, ncname, (mod ? mod : set->local_mod))) { |
| 5649 | idx = lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5650 | if ((idx < orig_used) && (idx > i)) { |
| 5651 | set->val.scnodes[idx].in_ctx = 2; |
| 5652 | temp_ctx = 1; |
| 5653 | } |
| 5654 | } |
| 5655 | } |
| 5656 | } |
| 5657 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5658 | |
| 5659 | /* correct temporary in_ctx values */ |
| 5660 | if (temp_ctx) { |
| 5661 | for (i = 0; i < orig_used; ++i) { |
| 5662 | if (set->val.scnodes[i].in_ctx == 2) { |
| 5663 | set->val.scnodes[i].in_ctx = 1; |
| 5664 | } |
| 5665 | } |
| 5666 | } |
| 5667 | |
| 5668 | return LY_SUCCESS; |
| 5669 | } |
| 5670 | |
| 5671 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5672 | * @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] | 5673 | * Context position aware. |
| 5674 | * |
| 5675 | * @param[in] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5676 | * @param[in] mod Matching node module, NULL for any. |
| 5677 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5678 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 5679 | */ |
| 5680 | static LY_ERR |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5681 | 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] | 5682 | { |
| 5683 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5684 | const struct lyd_node *next, *elem, *start; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5685 | struct lyxp_set ret_set; |
| 5686 | LY_ERR rc; |
| 5687 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5688 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5689 | return LY_SUCCESS; |
| 5690 | } |
| 5691 | |
| 5692 | if (set->type != LYXP_SET_NODE_SET) { |
| 5693 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 5694 | return LY_EVALID; |
| 5695 | } |
| 5696 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5697 | /* 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] | 5698 | rc = moveto_node(set, NULL, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5699 | LY_CHECK_RET(rc); |
| 5700 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5701 | /* 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] | 5702 | set_init(&ret_set, set); |
| 5703 | for (i = 0; i < set->used; ++i) { |
| 5704 | |
| 5705 | /* TREE DFS */ |
| 5706 | start = set->val.nodes[i].node; |
| 5707 | for (elem = next = start; elem; elem = next) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5708 | rc = moveto_node_check(elem, set->root_type, ncname, mod); |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5709 | if (!rc) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5710 | /* add matching node into result set */ |
| 5711 | set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used); |
| 5712 | if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) { |
| 5713 | /* the node is a duplicate, we'll process it later in the set */ |
| 5714 | goto skip_children; |
| 5715 | } |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5716 | } else if (rc == LY_EINCOMPLETE) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5717 | return rc; |
| 5718 | } else if (rc == LY_EINVAL) { |
| 5719 | goto skip_children; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5720 | } |
| 5721 | |
| 5722 | /* TREE DFS NEXT ELEM */ |
| 5723 | /* select element for the next run - children first */ |
Michal Vasko | 5bfd4be | 2020-06-23 13:26:19 +0200 | [diff] [blame] | 5724 | next = lyd_node_children(elem, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5725 | if (!next) { |
| 5726 | skip_children: |
| 5727 | /* no children, so try siblings, but only if it's not the start, |
| 5728 | * that is considered to be the root and it's siblings are not traversed */ |
| 5729 | if (elem != start) { |
| 5730 | next = elem->next; |
| 5731 | } else { |
| 5732 | break; |
| 5733 | } |
| 5734 | } |
| 5735 | while (!next) { |
| 5736 | /* no siblings, go back through the parents */ |
| 5737 | if ((struct lyd_node *)elem->parent == start) { |
| 5738 | /* we are done, no next element to process */ |
| 5739 | break; |
| 5740 | } |
| 5741 | /* parent is already processed, go to its sibling */ |
| 5742 | elem = (struct lyd_node *)elem->parent; |
| 5743 | next = elem->next; |
| 5744 | } |
| 5745 | } |
| 5746 | } |
| 5747 | |
| 5748 | /* make the temporary set the current one */ |
| 5749 | ret_set.ctx_pos = set->ctx_pos; |
| 5750 | ret_set.ctx_size = set->ctx_size; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5751 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5752 | memcpy(set, &ret_set, sizeof *set); |
| 5753 | |
| 5754 | return LY_SUCCESS; |
| 5755 | } |
| 5756 | |
| 5757 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5758 | * @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] | 5759 | * |
| 5760 | * @param[in] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5761 | * @param[in] mod Matching node module, NULL for any. |
| 5762 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5763 | * @param[in] options XPath options. |
| 5764 | * @return LY_ERR |
| 5765 | */ |
| 5766 | static LY_ERR |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5767 | 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] | 5768 | { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5769 | int i, orig_used, idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5770 | const struct lysc_node *next, *elem, *start; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5771 | LY_ERR rc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5772 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5773 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5774 | return LY_SUCCESS; |
| 5775 | } |
| 5776 | |
| 5777 | if (set->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 5778 | 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] | 5779 | return LY_EVALID; |
| 5780 | } |
| 5781 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5782 | orig_used = set->used; |
| 5783 | for (i = 0; i < orig_used; ++i) { |
| 5784 | if (set->val.scnodes[i].in_ctx != 1) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5785 | if (set->val.scnodes[i].in_ctx != -2) { |
| 5786 | continue; |
| 5787 | } |
| 5788 | |
| 5789 | /* remember context node */ |
| 5790 | set->val.scnodes[i].in_ctx = -1; |
Michal Vasko | ec4df48 | 2019-12-16 10:02:18 +0100 | [diff] [blame] | 5791 | } else { |
| 5792 | set->val.scnodes[i].in_ctx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5793 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5794 | |
| 5795 | /* TREE DFS */ |
| 5796 | start = set->val.scnodes[i].scnode; |
| 5797 | for (elem = next = start; elem; elem = next) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5798 | if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) { |
| 5799 | /* schema-only nodes, skip root */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5800 | goto next_iter; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5801 | } |
| 5802 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5803 | rc = moveto_scnode_check(elem, set->root_type, ncname, mod); |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5804 | if (!rc) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 5805 | 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] | 5806 | set->val.scnodes[idx].in_ctx = 1; |
| 5807 | if (idx > i) { |
| 5808 | /* we will process it later in the set */ |
| 5809 | goto skip_children; |
| 5810 | } |
| 5811 | } else { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 5812 | lyxp_set_scnode_insert_node(set, elem, LYXP_NODE_ELEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5813 | } |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5814 | } else if (rc == LY_EINVAL) { |
| 5815 | goto skip_children; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5816 | } |
| 5817 | |
| 5818 | next_iter: |
| 5819 | /* TREE DFS NEXT ELEM */ |
| 5820 | /* select element for the next run - children first */ |
| 5821 | 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] | 5822 | if (!next) { |
| 5823 | skip_children: |
| 5824 | /* no children, so try siblings, but only if it's not the start, |
| 5825 | * that is considered to be the root and it's siblings are not traversed */ |
| 5826 | if (elem != start) { |
| 5827 | next = elem->next; |
| 5828 | } else { |
| 5829 | break; |
| 5830 | } |
| 5831 | } |
| 5832 | while (!next) { |
| 5833 | /* no siblings, go back through the parents */ |
| 5834 | if (elem->parent == start) { |
| 5835 | /* we are done, no next element to process */ |
| 5836 | break; |
| 5837 | } |
| 5838 | /* parent is already processed, go to its sibling */ |
| 5839 | elem = elem->parent; |
| 5840 | next = elem->next; |
| 5841 | } |
| 5842 | } |
| 5843 | } |
| 5844 | |
| 5845 | return LY_SUCCESS; |
| 5846 | } |
| 5847 | |
| 5848 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5849 | * @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] | 5850 | * Indirectly context position aware. |
| 5851 | * |
| 5852 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5853 | * @param[in] mod Matching metadata module, NULL for any. |
| 5854 | * @param[in] ncname Matching metadata name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5855 | * @return LY_ERR |
| 5856 | */ |
| 5857 | static LY_ERR |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5858 | 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] | 5859 | { |
| 5860 | uint32_t i; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5861 | int replaced; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5862 | struct lyd_meta *sub; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5863 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5864 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5865 | return LY_SUCCESS; |
| 5866 | } |
| 5867 | |
| 5868 | if (set->type != LYXP_SET_NODE_SET) { |
| 5869 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 5870 | return LY_EVALID; |
| 5871 | } |
| 5872 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5873 | for (i = 0; i < set->used; ) { |
| 5874 | replaced = 0; |
| 5875 | |
| 5876 | /* only attributes of an elem (not dummy) can be in the result, skip all the rest; |
| 5877 | * our attributes are always qualified */ |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5878 | if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5879 | for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5880 | |
| 5881 | /* check "namespace" */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5882 | if (mod && (sub->annotation->module != mod)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5883 | continue; |
| 5884 | } |
| 5885 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5886 | if (!ncname || (sub->name == ncname)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5887 | /* match */ |
| 5888 | if (!replaced) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5889 | set->val.meta[i].meta = sub; |
| 5890 | set->val.meta[i].type = LYXP_NODE_META; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5891 | /* pos does not change */ |
| 5892 | replaced = 1; |
| 5893 | } else { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5894 | 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] | 5895 | } |
| 5896 | ++i; |
| 5897 | } |
| 5898 | } |
| 5899 | } |
| 5900 | |
| 5901 | if (!replaced) { |
| 5902 | /* no match */ |
| 5903 | set_remove_node(set, i); |
| 5904 | } |
| 5905 | } |
| 5906 | |
| 5907 | return LY_SUCCESS; |
| 5908 | } |
| 5909 | |
| 5910 | /** |
| 5911 | * @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] | 5912 | * Result is LYXP_SET_NODE_SET. Context position aware. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5913 | * |
| 5914 | * @param[in,out] set1 Set to use for the result. |
| 5915 | * @param[in] set2 Set that is copied to @p set1. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5916 | * @return LY_ERR |
| 5917 | */ |
| 5918 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5919 | moveto_union(struct lyxp_set *set1, struct lyxp_set *set2) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5920 | { |
| 5921 | LY_ERR rc; |
| 5922 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5923 | 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] | 5924 | LOGVAL(set1->ctx, LY_VLOG_LYD, set1->ctx_node, LY_VCODE_XP_INOP_2, "union", print_set_type(set1), print_set_type(set2)); |
| 5925 | return LY_EVALID; |
| 5926 | } |
| 5927 | |
| 5928 | /* set2 is empty or both set1 and set2 */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5929 | if (!set2->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5930 | return LY_SUCCESS; |
| 5931 | } |
| 5932 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5933 | if (!set1->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5934 | memcpy(set1, set2, sizeof *set1); |
| 5935 | /* dynamic memory belongs to set1 now, do not free */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5936 | memset(set2, 0, sizeof *set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5937 | return LY_SUCCESS; |
| 5938 | } |
| 5939 | |
| 5940 | /* we assume sets are sorted */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5941 | assert(!set_sort(set1) && !set_sort(set2)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5942 | |
| 5943 | /* sort, remove duplicates */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5944 | rc = set_sorted_merge(set1, set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5945 | LY_CHECK_RET(rc); |
| 5946 | |
| 5947 | /* final set must be sorted */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5948 | assert(!set_sort(set1)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5949 | |
| 5950 | return LY_SUCCESS; |
| 5951 | } |
| 5952 | |
| 5953 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5954 | * @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] | 5955 | * Context position aware. |
| 5956 | * |
| 5957 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5958 | * @param[in] mod Matching metadata module, NULL for any. |
| 5959 | * @param[in] ncname Matching metadata name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5960 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 5961 | */ |
| 5962 | static int |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5963 | 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] | 5964 | { |
| 5965 | uint32_t i; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5966 | int replaced; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5967 | struct lyd_meta *sub; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5968 | struct lyxp_set *set_all_desc = NULL; |
| 5969 | LY_ERR rc; |
| 5970 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5971 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5972 | return LY_SUCCESS; |
| 5973 | } |
| 5974 | |
| 5975 | if (set->type != LYXP_SET_NODE_SET) { |
| 5976 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 5977 | return LY_EVALID; |
| 5978 | } |
| 5979 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5980 | /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory, |
| 5981 | * but it likely won't be used much, so it's a waste of time */ |
| 5982 | /* copy the context */ |
| 5983 | set_all_desc = set_copy(set); |
| 5984 | /* get all descendant nodes (the original context nodes are removed) */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5985 | rc = moveto_node_alldesc(set_all_desc, NULL, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5986 | if (rc != LY_SUCCESS) { |
| 5987 | lyxp_set_free(set_all_desc); |
| 5988 | return rc; |
| 5989 | } |
| 5990 | /* prepend the original context nodes */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5991 | rc = moveto_union(set, set_all_desc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5992 | if (rc != LY_SUCCESS) { |
| 5993 | lyxp_set_free(set_all_desc); |
| 5994 | return rc; |
| 5995 | } |
| 5996 | lyxp_set_free(set_all_desc); |
| 5997 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5998 | for (i = 0; i < set->used; ) { |
| 5999 | replaced = 0; |
| 6000 | |
| 6001 | /* only attributes of an elem can be in the result, skip all the rest, |
| 6002 | * we have all attributes qualified in lyd tree */ |
| 6003 | if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6004 | for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6005 | /* check "namespace" */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6006 | if (mod && (sub->annotation->module != mod)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6007 | continue; |
| 6008 | } |
| 6009 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6010 | if (!ncname || (sub->name == ncname)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6011 | /* match */ |
| 6012 | if (!replaced) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6013 | set->val.meta[i].meta = sub; |
| 6014 | set->val.meta[i].type = LYXP_NODE_META; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6015 | /* pos does not change */ |
| 6016 | replaced = 1; |
| 6017 | } else { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6018 | 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] | 6019 | } |
| 6020 | ++i; |
| 6021 | } |
| 6022 | } |
| 6023 | } |
| 6024 | |
| 6025 | if (!replaced) { |
| 6026 | /* no match */ |
| 6027 | set_remove_node(set, i); |
| 6028 | } |
| 6029 | } |
| 6030 | |
| 6031 | return LY_SUCCESS; |
| 6032 | } |
| 6033 | |
| 6034 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6035 | * @brief Move context @p set to self and al chilren, recursively. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET. |
| 6036 | * Context position aware. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6037 | * |
| 6038 | * @param[in] parent Current parent. |
| 6039 | * @param[in] parent_pos Position of @p parent. |
| 6040 | * @param[in] parent_type Node type of @p parent. |
| 6041 | * @param[in,out] to_set Set to use. |
| 6042 | * @param[in] dup_check_set Set for checking duplicities. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6043 | * @param[in] options XPath options. |
| 6044 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6045 | */ |
| 6046 | static LY_ERR |
| 6047 | 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] | 6048 | 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] | 6049 | { |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6050 | const struct lyd_node *iter, *first; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6051 | LY_ERR rc; |
| 6052 | |
| 6053 | switch (parent_type) { |
| 6054 | case LYXP_NODE_ROOT: |
| 6055 | case LYXP_NODE_ROOT_CONFIG: |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6056 | assert(!parent); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6057 | |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6058 | /* add all top-level nodes as elements */ |
| 6059 | first = to_set->tree; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6060 | break; |
| 6061 | case LYXP_NODE_ELEM: |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6062 | /* add just the text node of this term element node */ |
| 6063 | if (parent->schema->nodetype & (LYD_NODE_TERM | LYD_NODE_ANY)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6064 | if (!set_dup_node_check(dup_check_set, parent, LYXP_NODE_TEXT, -1)) { |
| 6065 | set_insert_node(to_set, parent, parent_pos, LYXP_NODE_TEXT, to_set->used); |
| 6066 | } |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6067 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6068 | } |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6069 | |
| 6070 | /* add all the children of this node */ |
Michal Vasko | 5bfd4be | 2020-06-23 13:26:19 +0200 | [diff] [blame] | 6071 | first = lyd_node_children(parent, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6072 | break; |
| 6073 | default: |
| 6074 | LOGINT_RET(parent->schema->module->ctx); |
| 6075 | } |
| 6076 | |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6077 | /* add all top-level nodes as elements */ |
| 6078 | LY_LIST_FOR(first, iter) { |
| 6079 | /* context check */ |
| 6080 | if ((parent_type == LYXP_NODE_ROOT_CONFIG) && (iter->schema->flags & LYS_CONFIG_R)) { |
| 6081 | continue; |
| 6082 | } |
| 6083 | |
| 6084 | /* when check */ |
| 6085 | if (moveto_when_check(iter)) { |
| 6086 | return LY_EINCOMPLETE; |
| 6087 | } |
| 6088 | |
| 6089 | if (!set_dup_node_check(dup_check_set, iter, LYXP_NODE_ELEM, -1)) { |
| 6090 | set_insert_node(to_set, iter, 0, LYXP_NODE_ELEM, to_set->used); |
| 6091 | |
| 6092 | /* also add all the children of this node, recursively */ |
| 6093 | rc = moveto_self_add_children_r(iter, 0, LYXP_NODE_ELEM, to_set, dup_check_set, options); |
| 6094 | LY_CHECK_RET(rc); |
| 6095 | } |
| 6096 | } |
| 6097 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6098 | return LY_SUCCESS; |
| 6099 | } |
| 6100 | |
| 6101 | /** |
| 6102 | * @brief Move context @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET |
| 6103 | * (or LYXP_SET_EMPTY). Context position aware. |
| 6104 | * |
| 6105 | * @param[in,out] set Set to use. |
| 6106 | * @param[in] all_desc Whether to go to all descendants ('//') or not ('/'). |
| 6107 | * @param[in] options XPath options. |
| 6108 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6109 | */ |
| 6110 | static LY_ERR |
| 6111 | moveto_self(struct lyxp_set *set, int all_desc, int options) |
| 6112 | { |
| 6113 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6114 | struct lyxp_set ret_set; |
| 6115 | LY_ERR rc; |
| 6116 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6117 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6118 | return LY_SUCCESS; |
| 6119 | } |
| 6120 | |
| 6121 | if (set->type != LYXP_SET_NODE_SET) { |
| 6122 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 6123 | return LY_EVALID; |
| 6124 | } |
| 6125 | |
| 6126 | /* nothing to do */ |
| 6127 | if (!all_desc) { |
| 6128 | return LY_SUCCESS; |
| 6129 | } |
| 6130 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6131 | /* add all the children, they get added recursively */ |
| 6132 | set_init(&ret_set, set); |
| 6133 | for (i = 0; i < set->used; ++i) { |
| 6134 | /* copy the current node to tmp */ |
| 6135 | set_insert_node(&ret_set, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, ret_set.used); |
| 6136 | |
| 6137 | /* do not touch attributes and text nodes */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6138 | 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] | 6139 | continue; |
| 6140 | } |
| 6141 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6142 | /* add all the children */ |
| 6143 | 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] | 6144 | set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6145 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6146 | lyxp_set_free_content(&ret_set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6147 | return rc; |
| 6148 | } |
| 6149 | } |
| 6150 | |
| 6151 | /* use the temporary set as the current one */ |
| 6152 | ret_set.ctx_pos = set->ctx_pos; |
| 6153 | ret_set.ctx_size = set->ctx_size; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6154 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6155 | memcpy(set, &ret_set, sizeof *set); |
| 6156 | |
| 6157 | return LY_SUCCESS; |
| 6158 | } |
| 6159 | |
| 6160 | /** |
| 6161 | * @brief Move context schema @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_SCNODE_SET |
| 6162 | * (or LYXP_SET_EMPTY). |
| 6163 | * |
| 6164 | * @param[in,out] set Set to use. |
| 6165 | * @param[in] all_desc Whether to go to all descendants ('//') or not ('/'). |
| 6166 | * @param[in] options XPath options. |
| 6167 | * @return LY_ERR |
| 6168 | */ |
| 6169 | static LY_ERR |
| 6170 | moveto_scnode_self(struct lyxp_set *set, int all_desc, int options) |
| 6171 | { |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6172 | int getnext_opts; |
| 6173 | uint32_t i, mod_idx; |
| 6174 | const struct lysc_node *iter, *start_parent; |
| 6175 | const struct lys_module *mod; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6176 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6177 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6178 | return LY_SUCCESS; |
| 6179 | } |
| 6180 | |
| 6181 | if (set->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 6182 | 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] | 6183 | return LY_EVALID; |
| 6184 | } |
| 6185 | |
| 6186 | /* nothing to do */ |
| 6187 | if (!all_desc) { |
| 6188 | return LY_SUCCESS; |
| 6189 | } |
| 6190 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6191 | /* getnext opts */ |
| 6192 | getnext_opts = LYS_GETNEXT_NOSTATECHECK; |
| 6193 | if (options & LYXP_SCNODE_OUTPUT) { |
| 6194 | getnext_opts |= LYS_GETNEXT_OUTPUT; |
| 6195 | } |
| 6196 | |
| 6197 | /* 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] | 6198 | for (i = 0; i < set->used; ++i) { |
| 6199 | if (set->val.scnodes[i].in_ctx != 1) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6200 | if (set->val.scnodes[i].in_ctx != -2) { |
| 6201 | continue; |
| 6202 | } |
| 6203 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6204 | /* remember context node */ |
| 6205 | set->val.scnodes[i].in_ctx = -1; |
| 6206 | } else { |
| 6207 | set->val.scnodes[i].in_ctx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6208 | } |
| 6209 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6210 | start_parent = set->val.scnodes[i].scnode; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6211 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6212 | if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) { |
| 6213 | /* it can actually be in any module, it's all <running> */ |
| 6214 | mod_idx = 0; |
| 6215 | while ((mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) { |
| 6216 | iter = NULL; |
| 6217 | /* module may not be implemented */ |
| 6218 | while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) { |
| 6219 | /* context check */ |
| 6220 | if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) { |
| 6221 | continue; |
| 6222 | } |
| 6223 | |
| 6224 | lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM); |
| 6225 | /* throw away the insert index, we want to consider that node again, recursively */ |
| 6226 | } |
| 6227 | } |
| 6228 | |
| 6229 | } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) { |
| 6230 | iter = NULL; |
| 6231 | while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6232 | /* context check */ |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6233 | 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] | 6234 | continue; |
| 6235 | } |
| 6236 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6237 | lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6238 | } |
| 6239 | } |
| 6240 | } |
| 6241 | |
| 6242 | return LY_SUCCESS; |
| 6243 | } |
| 6244 | |
| 6245 | /** |
| 6246 | * @brief Move context @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_NODE_SET |
| 6247 | * (or LYXP_SET_EMPTY). Context position aware. |
| 6248 | * |
| 6249 | * @param[in] set Set to use. |
| 6250 | * @param[in] all_desc Whether to go to all descendants ('//') or not ('/'). |
| 6251 | * @param[in] options XPath options. |
| 6252 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6253 | */ |
| 6254 | static LY_ERR |
| 6255 | moveto_parent(struct lyxp_set *set, int all_desc, int options) |
| 6256 | { |
| 6257 | LY_ERR rc; |
| 6258 | uint32_t i; |
| 6259 | struct lyd_node *node, *new_node; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6260 | enum lyxp_node_type new_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6261 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6262 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6263 | return LY_SUCCESS; |
| 6264 | } |
| 6265 | |
| 6266 | if (set->type != LYXP_SET_NODE_SET) { |
| 6267 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 6268 | return LY_EVALID; |
| 6269 | } |
| 6270 | |
| 6271 | if (all_desc) { |
| 6272 | /* <path>//.. == <path>//./.. */ |
| 6273 | rc = moveto_self(set, 1, options); |
| 6274 | LY_CHECK_RET(rc); |
| 6275 | } |
| 6276 | |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 6277 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6278 | node = set->val.nodes[i].node; |
| 6279 | |
| 6280 | if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
| 6281 | new_node = (struct lyd_node *)node->parent; |
| 6282 | } else if (set->val.nodes[i].type == LYXP_NODE_TEXT) { |
| 6283 | new_node = node; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6284 | } else if (set->val.nodes[i].type == LYXP_NODE_META) { |
| 6285 | new_node = set->val.meta[i].meta->parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6286 | if (!new_node) { |
| 6287 | LOGINT_RET(set->ctx); |
| 6288 | } |
| 6289 | } else { |
| 6290 | /* root does not have a parent */ |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6291 | set_remove_node_none(set, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6292 | continue; |
| 6293 | } |
| 6294 | |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 6295 | /* when check */ |
| 6296 | if (moveto_when_check(new_node)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6297 | return LY_EINCOMPLETE; |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 6298 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6299 | |
| 6300 | /* node already there can also be the root */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6301 | if (!new_node) { |
| 6302 | new_type = set->root_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6303 | |
| 6304 | /* node has a standard parent (it can equal the root, it's not the root yet since they are fake) */ |
| 6305 | } else { |
| 6306 | new_type = LYXP_NODE_ELEM; |
| 6307 | } |
| 6308 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6309 | if (set_dup_node_check(set, new_node, new_type, -1)) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6310 | set_remove_node_none(set, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6311 | } else { |
| 6312 | set_replace_node(set, new_node, 0, new_type, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6313 | } |
| 6314 | } |
| 6315 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6316 | set_remove_nodes_none(set); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6317 | assert(!set_sort(set) && !set_sorted_dup_node_clean(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6318 | |
| 6319 | return LY_SUCCESS; |
| 6320 | } |
| 6321 | |
| 6322 | /** |
| 6323 | * @brief Move context schema @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_SCNODE_SET |
| 6324 | * (or LYXP_SET_EMPTY). |
| 6325 | * |
| 6326 | * @param[in] set Set to use. |
| 6327 | * @param[in] all_desc Whether to go to all descendants ('//') or not ('/'). |
| 6328 | * @param[in] options XPath options. |
| 6329 | * @return LY_ERR |
| 6330 | */ |
| 6331 | static LY_ERR |
| 6332 | moveto_scnode_parent(struct lyxp_set *set, int all_desc, int options) |
| 6333 | { |
| 6334 | int idx, i, orig_used, temp_ctx = 0; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6335 | const struct lysc_node *node, *new_node; |
| 6336 | enum lyxp_node_type new_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6337 | LY_ERR rc; |
| 6338 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6339 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6340 | return LY_SUCCESS; |
| 6341 | } |
| 6342 | |
| 6343 | if (set->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 6344 | 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] | 6345 | return LY_EVALID; |
| 6346 | } |
| 6347 | |
| 6348 | if (all_desc) { |
| 6349 | /* <path>//.. == <path>//./.. */ |
| 6350 | rc = moveto_scnode_self(set, 1, options); |
| 6351 | LY_CHECK_RET(rc); |
| 6352 | } |
| 6353 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6354 | orig_used = set->used; |
| 6355 | for (i = 0; i < orig_used; ++i) { |
| 6356 | if (set->val.scnodes[i].in_ctx != 1) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6357 | if (set->val.scnodes[i].in_ctx != -2) { |
| 6358 | continue; |
| 6359 | } |
| 6360 | |
| 6361 | /* remember context node */ |
| 6362 | set->val.scnodes[i].in_ctx = -1; |
Michal Vasko | ec4df48 | 2019-12-16 10:02:18 +0100 | [diff] [blame] | 6363 | } else { |
| 6364 | set->val.scnodes[i].in_ctx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6365 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6366 | |
| 6367 | node = set->val.scnodes[i].scnode; |
| 6368 | |
| 6369 | if (set->val.scnodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6370 | new_node = lysc_data_parent(node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6371 | } else { |
| 6372 | /* root does not have a parent */ |
| 6373 | continue; |
| 6374 | } |
| 6375 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6376 | /* node has no parent */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6377 | if (!new_node) { |
| 6378 | new_type = set->root_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6379 | |
| 6380 | /* node has a standard parent (it can equal the root, it's not the root yet since they are fake) */ |
| 6381 | } else { |
| 6382 | new_type = LYXP_NODE_ELEM; |
| 6383 | } |
| 6384 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6385 | idx = lyxp_set_scnode_insert_node(set, new_node, new_type); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6386 | if ((idx < orig_used) && (idx > i)) { |
| 6387 | set->val.scnodes[idx].in_ctx = 2; |
| 6388 | temp_ctx = 1; |
| 6389 | } |
| 6390 | } |
| 6391 | |
| 6392 | if (temp_ctx) { |
| 6393 | for (i = 0; i < orig_used; ++i) { |
| 6394 | if (set->val.scnodes[i].in_ctx == 2) { |
| 6395 | set->val.scnodes[i].in_ctx = 1; |
| 6396 | } |
| 6397 | } |
| 6398 | } |
| 6399 | |
| 6400 | return LY_SUCCESS; |
| 6401 | } |
| 6402 | |
| 6403 | /** |
| 6404 | * @brief Move context @p set to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'. |
| 6405 | * Result is LYXP_SET_BOOLEAN. Indirectly context position aware. |
| 6406 | * |
| 6407 | * @param[in,out] set1 Set to use for the result. |
| 6408 | * @param[in] set2 Set acting as the second operand for @p op. |
| 6409 | * @param[in] op Comparison operator to process. |
| 6410 | * @param[in] options XPath options. |
| 6411 | * @return LY_ERR |
| 6412 | */ |
| 6413 | static LY_ERR |
| 6414 | moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op, int options) |
| 6415 | { |
| 6416 | /* |
| 6417 | * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING |
| 6418 | * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING) |
| 6419 | * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 6420 | * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 6421 | * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING |
| 6422 | * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6423 | * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 6424 | * |
| 6425 | * '=' or '!=' |
| 6426 | * BOOLEAN + BOOLEAN |
| 6427 | * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 6428 | * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 6429 | * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 6430 | * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 6431 | * NUMBER + NUMBER |
| 6432 | * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6433 | * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 6434 | * STRING + STRING |
| 6435 | * |
| 6436 | * '<=', '<', '>=', '>' |
| 6437 | * NUMBER + NUMBER |
| 6438 | * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 6439 | * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 6440 | * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 6441 | * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6442 | * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 6443 | * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 6444 | * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6445 | * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6446 | */ |
| 6447 | struct lyxp_set iter1, iter2; |
| 6448 | int result; |
| 6449 | int64_t i; |
| 6450 | LY_ERR rc; |
| 6451 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6452 | memset(&iter1, 0, sizeof iter1); |
| 6453 | memset(&iter2, 0, sizeof iter2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6454 | |
| 6455 | /* iterative evaluation with node-sets */ |
| 6456 | if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) { |
| 6457 | if (set1->type == LYXP_SET_NODE_SET) { |
| 6458 | for (i = 0; i < set1->used; ++i) { |
| 6459 | switch (set2->type) { |
| 6460 | case LYXP_SET_NUMBER: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6461 | rc = set_comp_cast(&iter1, set1, LYXP_SET_NUMBER, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6462 | break; |
| 6463 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6464 | rc = set_comp_cast(&iter1, set1, LYXP_SET_BOOLEAN, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6465 | break; |
| 6466 | default: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6467 | rc = set_comp_cast(&iter1, set1, LYXP_SET_STRING, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6468 | break; |
| 6469 | } |
| 6470 | LY_CHECK_RET(rc); |
| 6471 | |
| 6472 | rc = moveto_op_comp(&iter1, set2, op, options); |
| 6473 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6474 | lyxp_set_free_content(&iter1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6475 | return rc; |
| 6476 | } |
| 6477 | |
| 6478 | /* lazy evaluation until true */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6479 | if (iter1.val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6480 | set_fill_boolean(set1, 1); |
| 6481 | return LY_SUCCESS; |
| 6482 | } |
| 6483 | } |
| 6484 | } else { |
| 6485 | for (i = 0; i < set2->used; ++i) { |
| 6486 | switch (set1->type) { |
| 6487 | case LYXP_SET_NUMBER: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6488 | rc = set_comp_cast(&iter2, set2, LYXP_SET_NUMBER, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6489 | break; |
| 6490 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6491 | rc = set_comp_cast(&iter2, set2, LYXP_SET_BOOLEAN, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6492 | break; |
| 6493 | default: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6494 | rc = set_comp_cast(&iter2, set2, LYXP_SET_STRING, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6495 | break; |
| 6496 | } |
| 6497 | LY_CHECK_RET(rc); |
| 6498 | |
| 6499 | set_fill_set(&iter1, set1); |
| 6500 | |
| 6501 | rc = moveto_op_comp(&iter1, &iter2, op, options); |
| 6502 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6503 | lyxp_set_free_content(&iter1); |
| 6504 | lyxp_set_free_content(&iter2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6505 | return rc; |
| 6506 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6507 | lyxp_set_free_content(&iter2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6508 | |
| 6509 | /* lazy evaluation until true */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6510 | if (iter1.val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6511 | set_fill_boolean(set1, 1); |
| 6512 | return LY_SUCCESS; |
| 6513 | } |
| 6514 | } |
| 6515 | } |
| 6516 | |
| 6517 | /* false for all nodes */ |
| 6518 | set_fill_boolean(set1, 0); |
| 6519 | return LY_SUCCESS; |
| 6520 | } |
| 6521 | |
| 6522 | /* first convert properly */ |
| 6523 | if ((op[0] == '=') || (op[0] == '!')) { |
| 6524 | if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6525 | lyxp_set_cast(set1, LYXP_SET_BOOLEAN); |
| 6526 | lyxp_set_cast(set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6527 | } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6528 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6529 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6530 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6531 | LY_CHECK_RET(rc); |
| 6532 | } /* else we have 2 strings */ |
| 6533 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6534 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6535 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6536 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6537 | LY_CHECK_RET(rc); |
| 6538 | } |
| 6539 | |
| 6540 | assert(set1->type == set2->type); |
| 6541 | |
| 6542 | /* compute result */ |
| 6543 | if (op[0] == '=') { |
| 6544 | if (set1->type == LYXP_SET_BOOLEAN) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6545 | result = (set1->val.bln == set2->val.bln); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6546 | } else if (set1->type == LYXP_SET_NUMBER) { |
| 6547 | result = (set1->val.num == set2->val.num); |
| 6548 | } else { |
| 6549 | assert(set1->type == LYXP_SET_STRING); |
Michal Vasko | ac6c72f | 2019-11-14 16:09:34 +0100 | [diff] [blame] | 6550 | result = !strcmp(set1->val.str, set2->val.str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6551 | } |
| 6552 | } else if (op[0] == '!') { |
| 6553 | if (set1->type == LYXP_SET_BOOLEAN) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6554 | result = (set1->val.bln != set2->val.bln); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6555 | } else if (set1->type == LYXP_SET_NUMBER) { |
| 6556 | result = (set1->val.num != set2->val.num); |
| 6557 | } else { |
| 6558 | assert(set1->type == LYXP_SET_STRING); |
Michal Vasko | ac6c72f | 2019-11-14 16:09:34 +0100 | [diff] [blame] | 6559 | result = !strcmp(set1->val.str, set2->val.str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6560 | } |
| 6561 | } else { |
| 6562 | assert(set1->type == LYXP_SET_NUMBER); |
| 6563 | if (op[0] == '<') { |
| 6564 | if (op[1] == '=') { |
| 6565 | result = (set1->val.num <= set2->val.num); |
| 6566 | } else { |
| 6567 | result = (set1->val.num < set2->val.num); |
| 6568 | } |
| 6569 | } else { |
| 6570 | if (op[1] == '=') { |
| 6571 | result = (set1->val.num >= set2->val.num); |
| 6572 | } else { |
| 6573 | result = (set1->val.num > set2->val.num); |
| 6574 | } |
| 6575 | } |
| 6576 | } |
| 6577 | |
| 6578 | /* assign result */ |
| 6579 | if (result) { |
| 6580 | set_fill_boolean(set1, 1); |
| 6581 | } else { |
| 6582 | set_fill_boolean(set1, 0); |
| 6583 | } |
| 6584 | |
| 6585 | return LY_SUCCESS; |
| 6586 | } |
| 6587 | |
| 6588 | /** |
| 6589 | * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div', |
| 6590 | * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware. |
| 6591 | * |
| 6592 | * @param[in,out] set1 Set to use for the result. |
| 6593 | * @param[in] set2 Set acting as the second operand for @p op. |
| 6594 | * @param[in] op Operator to process. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6595 | * @return LY_ERR |
| 6596 | */ |
| 6597 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6598 | 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] | 6599 | { |
| 6600 | LY_ERR rc; |
| 6601 | |
| 6602 | /* unary '-' */ |
| 6603 | if (!set2 && (op[0] == '-')) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6604 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6605 | LY_CHECK_RET(rc); |
| 6606 | set1->val.num *= -1; |
| 6607 | lyxp_set_free(set2); |
| 6608 | return LY_SUCCESS; |
| 6609 | } |
| 6610 | |
| 6611 | assert(set1 && set2); |
| 6612 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6613 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6614 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6615 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6616 | LY_CHECK_RET(rc); |
| 6617 | |
| 6618 | switch (op[0]) { |
| 6619 | /* '+' */ |
| 6620 | case '+': |
| 6621 | set1->val.num += set2->val.num; |
| 6622 | break; |
| 6623 | |
| 6624 | /* '-' */ |
| 6625 | case '-': |
| 6626 | set1->val.num -= set2->val.num; |
| 6627 | break; |
| 6628 | |
| 6629 | /* '*' */ |
| 6630 | case '*': |
| 6631 | set1->val.num *= set2->val.num; |
| 6632 | break; |
| 6633 | |
| 6634 | /* 'div' */ |
| 6635 | case 'd': |
| 6636 | set1->val.num /= set2->val.num; |
| 6637 | break; |
| 6638 | |
| 6639 | /* 'mod' */ |
| 6640 | case 'm': |
| 6641 | set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num); |
| 6642 | break; |
| 6643 | |
| 6644 | default: |
| 6645 | LOGINT_RET(set1->ctx); |
| 6646 | } |
| 6647 | |
| 6648 | return LY_SUCCESS; |
| 6649 | } |
| 6650 | |
| 6651 | /* |
| 6652 | * eval functions |
| 6653 | * |
| 6654 | * They execute a parsed XPath expression on some data subtree. |
| 6655 | */ |
| 6656 | |
| 6657 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6658 | * @brief Evaluate Predicate. Logs directly on error. |
| 6659 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6660 | * [9] Predicate ::= '[' Expr ']' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6661 | * |
| 6662 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6663 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6664 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 6665 | * @param[in] options XPath options. |
| 6666 | * @param[in] parent_pos_pred Whether parent predicate was a positional one. |
| 6667 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6668 | */ |
| 6669 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6670 | 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] | 6671 | { |
| 6672 | LY_ERR rc; |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 6673 | uint16_t i, orig_exp; |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6674 | uint32_t orig_pos, orig_size; |
| 6675 | int32_t pred_in_ctx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6676 | struct lyxp_set set2; |
| 6677 | struct lyd_node *orig_parent; |
| 6678 | |
| 6679 | /* '[' */ |
| 6680 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6681 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 6682 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6683 | |
| 6684 | if (!set) { |
| 6685 | only_parse: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6686 | rc = eval_expr_select(exp, tok_idx, 0, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6687 | LY_CHECK_RET(rc); |
| 6688 | } else if (set->type == LYXP_SET_NODE_SET) { |
| 6689 | /* 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] | 6690 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6691 | |
| 6692 | /* empty set, nothing to evaluate */ |
| 6693 | if (!set->used) { |
| 6694 | goto only_parse; |
| 6695 | } |
| 6696 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6697 | orig_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6698 | orig_pos = 0; |
| 6699 | orig_size = set->used; |
| 6700 | orig_parent = NULL; |
Michal Vasko | 39dbf35 | 2020-05-21 10:08:59 +0200 | [diff] [blame] | 6701 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6702 | set_init(&set2, set); |
| 6703 | set_insert_node(&set2, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, 0); |
| 6704 | /* remember the node context position for position() and context size for last(), |
| 6705 | * predicates should always be evaluated with respect to the child axis (since we do |
| 6706 | * not support explicit axes) so we assign positions based on their parents */ |
| 6707 | if (parent_pos_pred && ((struct lyd_node *)set->val.nodes[i].node->parent != orig_parent)) { |
| 6708 | orig_parent = (struct lyd_node *)set->val.nodes[i].node->parent; |
| 6709 | orig_pos = 1; |
| 6710 | } else { |
| 6711 | ++orig_pos; |
| 6712 | } |
| 6713 | |
| 6714 | set2.ctx_pos = orig_pos; |
| 6715 | set2.ctx_size = orig_size; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6716 | *tok_idx = orig_exp; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6717 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6718 | rc = eval_expr_select(exp, tok_idx, 0, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6719 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6720 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6721 | return rc; |
| 6722 | } |
| 6723 | |
| 6724 | /* number is a position */ |
| 6725 | if (set2.type == LYXP_SET_NUMBER) { |
| 6726 | if ((long long)set2.val.num == orig_pos) { |
| 6727 | set2.val.num = 1; |
| 6728 | } else { |
| 6729 | set2.val.num = 0; |
| 6730 | } |
| 6731 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6732 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6733 | |
| 6734 | /* predicate satisfied or not? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6735 | if (!set2.val.bln) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6736 | set_remove_node_none(set, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6737 | } |
| 6738 | } |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6739 | set_remove_nodes_none(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6740 | |
| 6741 | } else if (set->type == LYXP_SET_SCNODE_SET) { |
| 6742 | for (i = 0; i < set->used; ++i) { |
| 6743 | if (set->val.scnodes[i].in_ctx == 1) { |
| 6744 | /* there is a currently-valid node */ |
| 6745 | break; |
| 6746 | } |
| 6747 | } |
| 6748 | /* empty set, nothing to evaluate */ |
| 6749 | if (i == set->used) { |
| 6750 | goto only_parse; |
| 6751 | } |
| 6752 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6753 | orig_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6754 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6755 | /* set special in_ctx to all the valid snodes */ |
| 6756 | pred_in_ctx = set_scnode_new_in_ctx(set); |
| 6757 | |
| 6758 | /* use the valid snodes one-by-one */ |
| 6759 | for (i = 0; i < set->used; ++i) { |
| 6760 | if (set->val.scnodes[i].in_ctx != pred_in_ctx) { |
| 6761 | continue; |
| 6762 | } |
| 6763 | set->val.scnodes[i].in_ctx = 1; |
| 6764 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6765 | *tok_idx = orig_exp; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6766 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6767 | rc = eval_expr_select(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6768 | LY_CHECK_RET(rc); |
| 6769 | |
| 6770 | set->val.scnodes[i].in_ctx = pred_in_ctx; |
| 6771 | } |
| 6772 | |
| 6773 | /* restore the state as it was before the predicate */ |
| 6774 | for (i = 0; i < set->used; ++i) { |
| 6775 | if (set->val.scnodes[i].in_ctx == 1) { |
| 6776 | set->val.scnodes[i].in_ctx = 0; |
| 6777 | } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) { |
| 6778 | set->val.scnodes[i].in_ctx = 1; |
| 6779 | } |
| 6780 | } |
| 6781 | |
| 6782 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6783 | set2.type = LYXP_SET_NODE_SET; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6784 | set_fill_set(&set2, set); |
| 6785 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6786 | rc = eval_expr_select(exp, tok_idx, 0, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6787 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6788 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6789 | return rc; |
| 6790 | } |
| 6791 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6792 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6793 | if (!set2.val.bln) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6794 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6795 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6796 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6797 | } |
| 6798 | |
| 6799 | /* ']' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6800 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6801 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6802 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 6803 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6804 | |
| 6805 | return LY_SUCCESS; |
| 6806 | } |
| 6807 | |
| 6808 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6809 | * @brief Evaluate Literal. Logs directly on error. |
| 6810 | * |
| 6811 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6812 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6813 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 6814 | */ |
| 6815 | static void |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6816 | 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] | 6817 | { |
| 6818 | if (set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6819 | if (exp->tok_len[*tok_idx] == 2) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6820 | set_fill_string(set, "", 0); |
| 6821 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6822 | 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] | 6823 | } |
| 6824 | } |
| 6825 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6826 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 6827 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6828 | } |
| 6829 | |
| 6830 | /** |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6831 | * @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] | 6832 | * |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6833 | * @param[in] exp Full parsed XPath expression. |
| 6834 | * @param[in,out] tok_idx Index in @p exp at the beginning of the predicate, is updated on success. |
| 6835 | * @param[in] scnode Found schema node as context for the predicate. |
| 6836 | * @param[in] format Format of any prefixes in key names/values. |
| 6837 | * @param[out] predicates Parsed predicates. |
| 6838 | * @param[out] pred_type Type of @p predicates. |
| 6839 | * @return LY_SUCCESS on success, |
| 6840 | * @return LY_ERR on any error. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6841 | */ |
| 6842 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6843 | eval_name_test_try_compile_predicates(struct lyxp_expr *exp, uint16_t *tok_idx, const struct lysc_node *scnode, |
| 6844 | 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] | 6845 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6846 | LY_ERR ret = LY_SUCCESS; |
| 6847 | uint16_t key_count, e_idx, pred_idx = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6848 | const struct lysc_node *key; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6849 | size_t pred_len; |
| 6850 | int prev_lo; |
| 6851 | struct lyxp_expr *exp2 = NULL; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6852 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6853 | assert(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6854 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6855 | if (scnode->nodetype == LYS_LIST) { |
| 6856 | /* get key count */ |
| 6857 | if (scnode->flags & LYS_KEYLESS) { |
| 6858 | return LY_EINVAL; |
| 6859 | } |
| 6860 | for (key_count = 0, key = lysc_node_children(scnode, 0); key && (key->flags & LYS_KEY); key = key->next, ++key_count); |
| 6861 | assert(key_count); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6862 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6863 | /* learn where the predicates end */ |
| 6864 | e_idx = *tok_idx; |
| 6865 | while (key_count) { |
| 6866 | /* '[' */ |
| 6867 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) { |
| 6868 | return LY_EINVAL; |
| 6869 | } |
| 6870 | ++e_idx; |
| 6871 | |
| 6872 | /* ']' */ |
| 6873 | while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) { |
| 6874 | ++e_idx; |
| 6875 | } |
| 6876 | ++e_idx; |
| 6877 | |
| 6878 | /* another presumably key predicate parsed */ |
| 6879 | --key_count; |
| 6880 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6881 | } else { |
| 6882 | /* learn just where this single predicate ends */ |
| 6883 | e_idx = *tok_idx; |
| 6884 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6885 | /* '[' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6886 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) { |
| 6887 | return LY_EINVAL; |
| 6888 | } |
| 6889 | ++e_idx; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6890 | |
| 6891 | /* ']' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6892 | while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) { |
| 6893 | ++e_idx; |
| 6894 | } |
| 6895 | ++e_idx; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6896 | } |
| 6897 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6898 | /* get the joined length of all the keys predicates/of the single leaf-list predicate */ |
| 6899 | pred_len = (exp->tok_pos[e_idx - 1] + exp->tok_len[e_idx - 1]) - exp->tok_pos[*tok_idx]; |
| 6900 | |
| 6901 | /* turn logging off */ |
| 6902 | prev_lo = ly_log_options(0); |
| 6903 | |
| 6904 | /* parse the predicate(s) */ |
| 6905 | LY_CHECK_GOTO(ret = ly_path_parse_predicate(scnode->module->ctx, exp->expr + exp->tok_pos[*tok_idx], pred_len, |
| 6906 | LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp2), cleanup); |
| 6907 | |
| 6908 | /* compile */ |
| 6909 | switch (format) { |
| 6910 | case LYD_SCHEMA: |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 6911 | ret = ly_path_compile_predicate(scnode->module->ctx, scnode->module, scnode, exp2, &pred_idx, lys_resolve_prefix, |
| 6912 | scnode->module, LYD_SCHEMA, predicates, pred_type); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6913 | break; |
| 6914 | case LYD_JSON: |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 6915 | ret = ly_path_compile_predicate(scnode->module->ctx, scnode->module, scnode, exp2, &pred_idx, |
| 6916 | lydjson_resolve_prefix, NULL, LYD_JSON, predicates, pred_type); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6917 | break; |
| 6918 | case LYD_XML: |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 6919 | case LYD_LYB: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6920 | ret = LY_EINT; |
| 6921 | goto cleanup; |
| 6922 | } |
| 6923 | LY_CHECK_GOTO(ret, cleanup); |
| 6924 | |
| 6925 | /* success, the predicate must include all the needed information for hash-based search */ |
| 6926 | *tok_idx = e_idx; |
| 6927 | |
| 6928 | cleanup: |
| 6929 | ly_log_options(prev_lo); |
| 6930 | lyxp_expr_free(scnode->module->ctx, exp2); |
| 6931 | return ret; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6932 | } |
| 6933 | |
| 6934 | /** |
| 6935 | * @brief Evaluate NameTest and any following Predicates. Logs directly on error. |
| 6936 | * |
| 6937 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 6938 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 6939 | * [7] NameTest ::= '*' | NCName ':' '*' | QName |
| 6940 | * |
| 6941 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6942 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6943 | * @param[in] attr_axis Whether to search attributes or standard nodes. |
| 6944 | * @param[in] all_desc Whether to search all the descendants or children only. |
| 6945 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 6946 | * @param[in] options XPath options. |
| 6947 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6948 | */ |
| 6949 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6950 | 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] | 6951 | int options) |
| 6952 | { |
| 6953 | int i; |
| 6954 | char *path; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6955 | const char *ncname, *ncname_dict = NULL; |
| 6956 | uint16_t ncname_len; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6957 | const struct lys_module *moveto_mod; |
| 6958 | const struct lysc_node *scnode = NULL, *tmp; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6959 | struct ly_path_predicate *predicates = NULL; |
| 6960 | enum ly_path_pred_type pred_type = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6961 | LY_ERR rc = LY_SUCCESS; |
| 6962 | |
| 6963 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6964 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 6965 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6966 | |
| 6967 | if (!set) { |
| 6968 | goto moveto; |
| 6969 | } |
| 6970 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6971 | ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]]; |
| 6972 | ncname_len = exp->tok_len[*tok_idx - 1]; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6973 | |
| 6974 | /* parse (and skip) module name */ |
| 6975 | rc = moveto_resolve_model(&ncname, &ncname_len, set, &moveto_mod); |
| 6976 | LY_CHECK_GOTO(rc, cleanup); |
| 6977 | |
| 6978 | if (moveto_mod && !attr_axis && !all_desc && (set->type == LYXP_SET_NODE_SET)) { |
| 6979 | /* find the matching schema node in some parent in the context */ |
| 6980 | for (i = 0; i < (signed)set->used; ++i) { |
| 6981 | if (set->val.nodes[i].type == set->root_type) { |
| 6982 | tmp = lys_find_child(NULL, moveto_mod, ncname, ncname_len, 0, 0); |
| 6983 | } else if ((set->val.nodes[i].type == LYXP_NODE_ELEM) |
| 6984 | && (!scnode || (lysc_data_parent(scnode) != set->val.nodes[i].node->schema))) { |
| 6985 | /* do not repeat the same search */ |
| 6986 | 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] | 6987 | } else { |
| 6988 | tmp = NULL; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6989 | } |
| 6990 | |
| 6991 | /* additional context check */ |
| 6992 | if (tmp && (set->root_type == LYXP_NODE_ROOT_CONFIG) && (tmp->flags & LYS_CONFIG_R)) { |
| 6993 | tmp = NULL; |
| 6994 | } |
| 6995 | |
| 6996 | if (tmp) { |
| 6997 | if (scnode) { |
| 6998 | /* we found a schema node with the same name but at different level, give up, too complicated */ |
| 6999 | scnode = NULL; |
| 7000 | break; |
| 7001 | } else { |
| 7002 | /* remember the found schema node and continue to make sure it can be used */ |
| 7003 | scnode = tmp; |
| 7004 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7005 | } |
| 7006 | } |
| 7007 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7008 | if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) { |
| 7009 | /* try to create the predicates */ |
| 7010 | if (eval_name_test_try_compile_predicates(exp, tok_idx, scnode, set->format, &predicates, &pred_type)) { |
| 7011 | /* hashes cannot be used */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7012 | scnode = NULL; |
| 7013 | } |
| 7014 | } |
| 7015 | } |
| 7016 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7017 | if (!scnode && moveto_mod) { |
| 7018 | /* we are not able to match based on a schema node and not all the modules match, |
| 7019 | * use dictionary for efficient comparison */ |
| 7020 | ncname_dict = lydict_insert(set->ctx, ncname, ncname_len); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7021 | } |
| 7022 | |
| 7023 | moveto: |
| 7024 | /* move to the attribute(s), data node(s), or schema node(s) */ |
| 7025 | if (attr_axis) { |
| 7026 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 7027 | set_scnode_clear_ctx(set); |
| 7028 | } else { |
| 7029 | if (all_desc) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7030 | rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7031 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7032 | rc = moveto_attr(set, moveto_mod, ncname_dict); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7033 | } |
| 7034 | LY_CHECK_GOTO(rc, cleanup); |
| 7035 | } |
| 7036 | } else { |
| 7037 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 7038 | if (all_desc) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7039 | rc = moveto_scnode_alldesc(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7040 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7041 | rc = moveto_scnode(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7042 | } |
| 7043 | LY_CHECK_GOTO(rc, cleanup); |
| 7044 | |
| 7045 | for (i = set->used - 1; i > -1; --i) { |
| 7046 | if (set->val.scnodes[i].in_ctx > 0) { |
| 7047 | break; |
| 7048 | } |
| 7049 | } |
| 7050 | if (i == -1) { |
| 7051 | path = lysc_path(set->ctx_scnode, LYSC_PATH_LOG, NULL, 0); |
| 7052 | LOGWRN(set->ctx, "Schema node \"%.*s\" not found (%.*s) with context node \"%s\".", |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7053 | exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]], |
| 7054 | exp->tok_pos[*tok_idx] + exp->tok_len[*tok_idx], exp->expr, path); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7055 | free(path); |
| 7056 | } |
| 7057 | } else { |
| 7058 | if (all_desc) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7059 | rc = moveto_node_alldesc(set, moveto_mod, ncname_dict); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7060 | } else { |
| 7061 | if (scnode) { |
| 7062 | /* we can find the nodes using hashes */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7063 | rc = moveto_node_hash(set, scnode, predicates); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7064 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7065 | rc = moveto_node(set, moveto_mod, ncname_dict); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7066 | } |
| 7067 | } |
| 7068 | LY_CHECK_GOTO(rc, cleanup); |
| 7069 | } |
| 7070 | } |
| 7071 | |
| 7072 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7073 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 7074 | rc = eval_predicate(exp, tok_idx, set, options, 1); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7075 | LY_CHECK_RET(rc); |
| 7076 | } |
| 7077 | |
| 7078 | cleanup: |
Michal Vasko | db51a8d | 2020-05-27 15:22:29 +0200 | [diff] [blame] | 7079 | if (set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7080 | lydict_remove(set->ctx, ncname_dict); |
| 7081 | ly_path_predicates_free(set->ctx, pred_type, scnode, predicates); |
Michal Vasko | db51a8d | 2020-05-27 15:22:29 +0200 | [diff] [blame] | 7082 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7083 | return rc; |
| 7084 | } |
| 7085 | |
| 7086 | /** |
| 7087 | * @brief Evaluate NodeType and any following Predicates. Logs directly on error. |
| 7088 | * |
| 7089 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 7090 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 7091 | * [8] NodeType ::= 'text' | 'node' |
| 7092 | * |
| 7093 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7094 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7095 | * @param[in] attr_axis Whether to search attributes or standard nodes. |
| 7096 | * @param[in] all_desc Whether to search all the descendants or children only. |
| 7097 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7098 | * @param[in] options XPath options. |
| 7099 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7100 | */ |
| 7101 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7102 | 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] | 7103 | struct lyxp_set *set, int options) |
| 7104 | { |
| 7105 | LY_ERR rc; |
| 7106 | |
| 7107 | /* TODO */ |
| 7108 | (void)attr_axis; |
| 7109 | (void)all_desc; |
| 7110 | |
| 7111 | if (set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7112 | assert(exp->tok_len[*tok_idx] == 4); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7113 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 7114 | set_scnode_clear_ctx(set); |
| 7115 | /* just for the debug message below */ |
| 7116 | set = NULL; |
| 7117 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7118 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7119 | rc = xpath_node(NULL, 0, set, options); |
| 7120 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7121 | assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4)); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7122 | rc = xpath_text(NULL, 0, set, options); |
| 7123 | } |
| 7124 | LY_CHECK_RET(rc); |
| 7125 | } |
| 7126 | } |
| 7127 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7128 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7129 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7130 | |
| 7131 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7132 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7133 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7134 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7135 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7136 | |
| 7137 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7138 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7139 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7140 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7141 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7142 | |
| 7143 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7144 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 7145 | rc = eval_predicate(exp, tok_idx, set, options, 1); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7146 | LY_CHECK_RET(rc); |
| 7147 | } |
| 7148 | |
| 7149 | return LY_SUCCESS; |
| 7150 | } |
| 7151 | |
| 7152 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7153 | * @brief Evaluate RelativeLocationPath. Logs directly on error. |
| 7154 | * |
| 7155 | * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step |
| 7156 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7157 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7158 | * |
| 7159 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7160 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7161 | * @param[in] all_desc Whether to search all the descendants or children only. |
| 7162 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7163 | * @param[in] options XPath options. |
| 7164 | * @return LY_ERR (YL_EINCOMPLETE on unresolved when) |
| 7165 | */ |
| 7166 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7167 | 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] | 7168 | { |
| 7169 | int attr_axis; |
| 7170 | LY_ERR rc; |
| 7171 | |
| 7172 | goto step; |
| 7173 | do { |
| 7174 | /* evaluate '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7175 | if (exp->tok_len[*tok_idx] == 1) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7176 | all_desc = 0; |
| 7177 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7178 | assert(exp->tok_len[*tok_idx] == 2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7179 | all_desc = 1; |
| 7180 | } |
| 7181 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7182 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7183 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7184 | |
| 7185 | step: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7186 | /* evaluate abbreviated axis '@'? if any */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7187 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7188 | attr_axis = 1; |
| 7189 | |
| 7190 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7191 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7192 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7193 | } else { |
| 7194 | attr_axis = 0; |
| 7195 | } |
| 7196 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7197 | /* Step */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7198 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7199 | case LYXP_TOKEN_DOT: |
| 7200 | /* evaluate '.' */ |
| 7201 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 7202 | rc = moveto_scnode_self(set, all_desc, options); |
| 7203 | } else { |
| 7204 | rc = moveto_self(set, all_desc, options); |
| 7205 | } |
| 7206 | LY_CHECK_RET(rc); |
| 7207 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7208 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7209 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7210 | break; |
| 7211 | |
| 7212 | case LYXP_TOKEN_DDOT: |
| 7213 | /* evaluate '..' */ |
| 7214 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 7215 | rc = moveto_scnode_parent(set, all_desc, options); |
| 7216 | } else { |
| 7217 | rc = moveto_parent(set, all_desc, options); |
| 7218 | } |
| 7219 | LY_CHECK_RET(rc); |
| 7220 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7221 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7222 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7223 | break; |
| 7224 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7225 | case LYXP_TOKEN_NAMETEST: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7226 | /* evaluate NameTest Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7227 | 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] | 7228 | LY_CHECK_RET(rc); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7229 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7230 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7231 | case LYXP_TOKEN_NODETYPE: |
| 7232 | /* evaluate NodeType Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7233 | 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] | 7234 | LY_CHECK_RET(rc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7235 | break; |
| 7236 | |
| 7237 | default: |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 7238 | LOGINT_RET(set ? set->ctx : NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7239 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7240 | } 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] | 7241 | |
| 7242 | return LY_SUCCESS; |
| 7243 | } |
| 7244 | |
| 7245 | /** |
| 7246 | * @brief Evaluate AbsoluteLocationPath. Logs directly on error. |
| 7247 | * |
| 7248 | * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath |
| 7249 | * |
| 7250 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7251 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7252 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7253 | * @param[in] options XPath options. |
| 7254 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7255 | */ |
| 7256 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7257 | 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] | 7258 | { |
| 7259 | int all_desc; |
| 7260 | LY_ERR rc; |
| 7261 | |
| 7262 | if (set) { |
| 7263 | /* no matter what tokens follow, we need to be at the root */ |
| 7264 | moveto_root(set, options); |
| 7265 | } |
| 7266 | |
| 7267 | /* '/' RelativeLocationPath? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7268 | if (exp->tok_len[*tok_idx] == 1) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7269 | /* evaluate '/' - deferred */ |
| 7270 | all_desc = 0; |
| 7271 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7272 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7273 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7274 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7275 | if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7276 | return LY_SUCCESS; |
| 7277 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7278 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7279 | case LYXP_TOKEN_DOT: |
| 7280 | case LYXP_TOKEN_DDOT: |
| 7281 | case LYXP_TOKEN_AT: |
| 7282 | case LYXP_TOKEN_NAMETEST: |
| 7283 | case LYXP_TOKEN_NODETYPE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7284 | rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7285 | LY_CHECK_RET(rc); |
| 7286 | break; |
| 7287 | default: |
| 7288 | break; |
| 7289 | } |
| 7290 | |
| 7291 | /* '//' RelativeLocationPath */ |
| 7292 | } else { |
| 7293 | /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */ |
| 7294 | all_desc = 1; |
| 7295 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7296 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7297 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7298 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7299 | rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7300 | LY_CHECK_RET(rc); |
| 7301 | } |
| 7302 | |
| 7303 | return LY_SUCCESS; |
| 7304 | } |
| 7305 | |
| 7306 | /** |
| 7307 | * @brief Evaluate FunctionCall. Logs directly on error. |
| 7308 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7309 | * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7310 | * |
| 7311 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7312 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7313 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7314 | * @param[in] options XPath options. |
| 7315 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7316 | */ |
| 7317 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7318 | eval_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] | 7319 | { |
| 7320 | LY_ERR rc; |
| 7321 | 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] | 7322 | uint16_t arg_count = 0, i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7323 | struct lyxp_set **args = NULL, **args_aux; |
| 7324 | |
| 7325 | if (set) { |
| 7326 | /* FunctionName */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7327 | switch (exp->tok_len[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7328 | case 3: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7329 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7330 | xpath_func = &xpath_not; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7331 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7332 | xpath_func = &xpath_sum; |
| 7333 | } |
| 7334 | break; |
| 7335 | case 4: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7336 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7337 | xpath_func = &xpath_lang; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7338 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7339 | xpath_func = &xpath_last; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7340 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7341 | xpath_func = &xpath_name; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7342 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7343 | xpath_func = &xpath_true; |
| 7344 | } |
| 7345 | break; |
| 7346 | case 5: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7347 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7348 | xpath_func = &xpath_count; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7349 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7350 | xpath_func = &xpath_false; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7351 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7352 | xpath_func = &xpath_floor; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7353 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7354 | xpath_func = &xpath_round; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7355 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7356 | xpath_func = &xpath_deref; |
| 7357 | } |
| 7358 | break; |
| 7359 | case 6: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7360 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7361 | xpath_func = &xpath_concat; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7362 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7363 | xpath_func = &xpath_number; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7364 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7365 | xpath_func = &xpath_string; |
| 7366 | } |
| 7367 | break; |
| 7368 | case 7: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7369 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7370 | xpath_func = &xpath_boolean; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7371 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7372 | xpath_func = &xpath_ceiling; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7373 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7374 | xpath_func = &xpath_current; |
| 7375 | } |
| 7376 | break; |
| 7377 | case 8: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7378 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7379 | xpath_func = &xpath_contains; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7380 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7381 | xpath_func = &xpath_position; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7382 | } 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] | 7383 | xpath_func = &xpath_re_match; |
| 7384 | } |
| 7385 | break; |
| 7386 | case 9: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7387 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7388 | xpath_func = &xpath_substring; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7389 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7390 | xpath_func = &xpath_translate; |
| 7391 | } |
| 7392 | break; |
| 7393 | case 10: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7394 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7395 | xpath_func = &xpath_local_name; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7396 | } 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] | 7397 | xpath_func = &xpath_enum_value; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7398 | } 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] | 7399 | xpath_func = &xpath_bit_is_set; |
| 7400 | } |
| 7401 | break; |
| 7402 | case 11: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7403 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7404 | xpath_func = &xpath_starts_with; |
| 7405 | } |
| 7406 | break; |
| 7407 | case 12: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7408 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7409 | xpath_func = &xpath_derived_from; |
| 7410 | } |
| 7411 | break; |
| 7412 | case 13: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7413 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7414 | xpath_func = &xpath_namespace_uri; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7415 | } 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] | 7416 | xpath_func = &xpath_string_length; |
| 7417 | } |
| 7418 | break; |
| 7419 | case 15: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7420 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7421 | xpath_func = &xpath_normalize_space; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7422 | } 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] | 7423 | xpath_func = &xpath_substring_after; |
| 7424 | } |
| 7425 | break; |
| 7426 | case 16: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7427 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7428 | xpath_func = &xpath_substring_before; |
| 7429 | } |
| 7430 | break; |
| 7431 | case 20: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7432 | 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] | 7433 | xpath_func = &xpath_derived_from_or_self; |
| 7434 | } |
| 7435 | break; |
| 7436 | } |
| 7437 | |
| 7438 | if (!xpath_func) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7439 | 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] | 7440 | return LY_EVALID; |
| 7441 | } |
| 7442 | } |
| 7443 | |
| 7444 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7445 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7446 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7447 | |
| 7448 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7449 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7450 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7451 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7452 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7453 | |
| 7454 | /* ( Expr ( ',' Expr )* )? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7455 | if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7456 | if (set) { |
| 7457 | args = malloc(sizeof *args); |
| 7458 | LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup); |
| 7459 | arg_count = 1; |
| 7460 | args[0] = set_copy(set); |
| 7461 | if (!args[0]) { |
| 7462 | rc = LY_EMEM; |
| 7463 | goto cleanup; |
| 7464 | } |
| 7465 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7466 | rc = eval_expr_select(exp, tok_idx, 0, args[0], options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7467 | LY_CHECK_GOTO(rc, cleanup); |
| 7468 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7469 | rc = eval_expr_select(exp, tok_idx, 0, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7470 | LY_CHECK_GOTO(rc, cleanup); |
| 7471 | } |
| 7472 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7473 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7474 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7475 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7476 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7477 | |
| 7478 | if (set) { |
| 7479 | ++arg_count; |
| 7480 | args_aux = realloc(args, arg_count * sizeof *args); |
| 7481 | LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup); |
| 7482 | args = args_aux; |
| 7483 | args[arg_count - 1] = set_copy(set); |
| 7484 | if (!args[arg_count - 1]) { |
| 7485 | rc = LY_EMEM; |
| 7486 | goto cleanup; |
| 7487 | } |
| 7488 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7489 | 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] | 7490 | LY_CHECK_GOTO(rc, cleanup); |
| 7491 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7492 | rc = eval_expr_select(exp, tok_idx, 0, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7493 | LY_CHECK_GOTO(rc, cleanup); |
| 7494 | } |
| 7495 | } |
| 7496 | |
| 7497 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7498 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7499 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7500 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7501 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7502 | |
| 7503 | if (set) { |
| 7504 | /* evaluate function */ |
| 7505 | rc = xpath_func(args, arg_count, set, options); |
| 7506 | |
| 7507 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7508 | /* merge all nodes from arg evaluations */ |
| 7509 | for (i = 0; i < arg_count; ++i) { |
| 7510 | set_scnode_clear_ctx(args[i]); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7511 | lyxp_set_scnode_merge(set, args[i]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7512 | } |
| 7513 | } |
| 7514 | } else { |
| 7515 | rc = LY_SUCCESS; |
| 7516 | } |
| 7517 | |
| 7518 | cleanup: |
| 7519 | for (i = 0; i < arg_count; ++i) { |
| 7520 | lyxp_set_free(args[i]); |
| 7521 | } |
| 7522 | free(args); |
| 7523 | |
| 7524 | return rc; |
| 7525 | } |
| 7526 | |
| 7527 | /** |
| 7528 | * @brief Evaluate Number. Logs directly on error. |
| 7529 | * |
| 7530 | * @param[in] ctx Context for errors. |
| 7531 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7532 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7533 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7534 | * @return LY_ERR |
| 7535 | */ |
| 7536 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7537 | 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] | 7538 | { |
| 7539 | long double num; |
| 7540 | char *endptr; |
| 7541 | |
| 7542 | if (set) { |
| 7543 | errno = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7544 | num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7545 | if (errno) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7546 | 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] | 7547 | 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] | 7548 | 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] | 7549 | return LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7550 | } else if (endptr - &exp->expr[exp->tok_pos[*tok_idx]] != exp->tok_len[*tok_idx]) { |
| 7551 | 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] | 7552 | 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] | 7553 | exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7554 | return LY_EVALID; |
| 7555 | } |
| 7556 | |
| 7557 | set_fill_number(set, num); |
| 7558 | } |
| 7559 | |
| 7560 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7561 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7562 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7563 | return LY_SUCCESS; |
| 7564 | } |
| 7565 | |
| 7566 | /** |
| 7567 | * @brief Evaluate PathExpr. Logs directly on error. |
| 7568 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7569 | * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate* |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7570 | * | PrimaryExpr Predicate* '/' RelativeLocationPath |
| 7571 | * | PrimaryExpr Predicate* '//' RelativeLocationPath |
| 7572 | * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7573 | * [10] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7574 | * |
| 7575 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7576 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7577 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7578 | * @param[in] options XPath options. |
| 7579 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7580 | */ |
| 7581 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7582 | 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] | 7583 | { |
| 7584 | int all_desc, parent_pos_pred; |
| 7585 | LY_ERR rc; |
| 7586 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7587 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7588 | case LYXP_TOKEN_PAR1: |
| 7589 | /* '(' Expr ')' */ |
| 7590 | |
| 7591 | /* '(' */ |
| 7592 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7593 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7594 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7595 | |
| 7596 | /* Expr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7597 | rc = eval_expr_select(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7598 | LY_CHECK_RET(rc); |
| 7599 | |
| 7600 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7601 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7602 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7603 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7604 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7605 | |
| 7606 | parent_pos_pred = 0; |
| 7607 | goto predicate; |
| 7608 | |
| 7609 | case LYXP_TOKEN_DOT: |
| 7610 | case LYXP_TOKEN_DDOT: |
| 7611 | case LYXP_TOKEN_AT: |
| 7612 | case LYXP_TOKEN_NAMETEST: |
| 7613 | case LYXP_TOKEN_NODETYPE: |
| 7614 | /* RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7615 | rc = eval_relative_location_path(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7616 | LY_CHECK_RET(rc); |
| 7617 | break; |
| 7618 | |
| 7619 | case LYXP_TOKEN_FUNCNAME: |
| 7620 | /* FunctionCall */ |
| 7621 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7622 | rc = eval_function_call(exp, tok_idx, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7623 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7624 | rc = eval_function_call(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7625 | } |
| 7626 | LY_CHECK_RET(rc); |
| 7627 | |
| 7628 | parent_pos_pred = 1; |
| 7629 | goto predicate; |
| 7630 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 7631 | case LYXP_TOKEN_OPER_PATH: |
| 7632 | case LYXP_TOKEN_OPER_RPATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7633 | /* AbsoluteLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7634 | rc = eval_absolute_location_path(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7635 | LY_CHECK_RET(rc); |
| 7636 | break; |
| 7637 | |
| 7638 | case LYXP_TOKEN_LITERAL: |
| 7639 | /* Literal */ |
| 7640 | if (!set || (options & LYXP_SCNODE_ALL)) { |
| 7641 | if (set) { |
| 7642 | set_scnode_clear_ctx(set); |
| 7643 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7644 | eval_literal(exp, tok_idx, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7645 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7646 | eval_literal(exp, tok_idx, set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7647 | } |
| 7648 | |
| 7649 | parent_pos_pred = 1; |
| 7650 | goto predicate; |
| 7651 | |
| 7652 | case LYXP_TOKEN_NUMBER: |
| 7653 | /* Number */ |
| 7654 | if (!set || (options & LYXP_SCNODE_ALL)) { |
| 7655 | if (set) { |
| 7656 | set_scnode_clear_ctx(set); |
| 7657 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7658 | rc = eval_number(NULL, exp, tok_idx, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7659 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7660 | rc = eval_number(set->ctx, exp, tok_idx, set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7661 | } |
| 7662 | LY_CHECK_RET(rc); |
| 7663 | |
| 7664 | parent_pos_pred = 1; |
| 7665 | goto predicate; |
| 7666 | |
| 7667 | default: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7668 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[*tok_idx]), |
| 7669 | &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7670 | return LY_EVALID; |
| 7671 | } |
| 7672 | |
| 7673 | return LY_SUCCESS; |
| 7674 | |
| 7675 | predicate: |
| 7676 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7677 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 7678 | rc = eval_predicate(exp, tok_idx, set, options, parent_pos_pred); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7679 | LY_CHECK_RET(rc); |
| 7680 | } |
| 7681 | |
| 7682 | /* ('/' or '//') RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7683 | 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] | 7684 | |
| 7685 | /* evaluate '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7686 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7687 | all_desc = 0; |
| 7688 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7689 | all_desc = 1; |
| 7690 | } |
| 7691 | |
| 7692 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7693 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7694 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7695 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7696 | rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7697 | LY_CHECK_RET(rc); |
| 7698 | } |
| 7699 | |
| 7700 | return LY_SUCCESS; |
| 7701 | } |
| 7702 | |
| 7703 | /** |
| 7704 | * @brief Evaluate UnionExpr. Logs directly on error. |
| 7705 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7706 | * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7707 | * |
| 7708 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7709 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7710 | * @param[in] repeat How many times this expression is repeated. |
| 7711 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7712 | * @param[in] options XPath options. |
| 7713 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7714 | */ |
| 7715 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7716 | 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] | 7717 | { |
| 7718 | LY_ERR rc = LY_SUCCESS; |
| 7719 | struct lyxp_set orig_set, set2; |
| 7720 | uint16_t i; |
| 7721 | |
| 7722 | assert(repeat); |
| 7723 | |
| 7724 | set_init(&orig_set, set); |
| 7725 | set_init(&set2, set); |
| 7726 | |
| 7727 | set_fill_set(&orig_set, set); |
| 7728 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7729 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7730 | LY_CHECK_GOTO(rc, cleanup); |
| 7731 | |
| 7732 | /* ('|' PathExpr)* */ |
| 7733 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7734 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7735 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7736 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7737 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7738 | |
| 7739 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7740 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7741 | LY_CHECK_GOTO(rc, cleanup); |
| 7742 | continue; |
| 7743 | } |
| 7744 | |
| 7745 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7746 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7747 | LY_CHECK_GOTO(rc, cleanup); |
| 7748 | |
| 7749 | /* eval */ |
| 7750 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7751 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7752 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7753 | rc = moveto_union(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7754 | LY_CHECK_GOTO(rc, cleanup); |
| 7755 | } |
| 7756 | } |
| 7757 | |
| 7758 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7759 | lyxp_set_free_content(&orig_set); |
| 7760 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7761 | return rc; |
| 7762 | } |
| 7763 | |
| 7764 | /** |
| 7765 | * @brief Evaluate UnaryExpr. Logs directly on error. |
| 7766 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7767 | * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7768 | * |
| 7769 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7770 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7771 | * @param[in] repeat How many times this expression is repeated. |
| 7772 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7773 | * @param[in] options XPath options. |
| 7774 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7775 | */ |
| 7776 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7777 | eval_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] | 7778 | { |
| 7779 | LY_ERR rc; |
| 7780 | uint16_t this_op, i; |
| 7781 | |
| 7782 | assert(repeat); |
| 7783 | |
| 7784 | /* ('-')+ */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7785 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7786 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7787 | 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] | 7788 | |
| 7789 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7790 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7791 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7792 | } |
| 7793 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7794 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7795 | LY_CHECK_RET(rc); |
| 7796 | |
| 7797 | if (set && (repeat % 2)) { |
| 7798 | if (options & LYXP_SCNODE_ALL) { |
| 7799 | warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]); |
| 7800 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7801 | 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] | 7802 | LY_CHECK_RET(rc); |
| 7803 | } |
| 7804 | } |
| 7805 | |
| 7806 | return LY_SUCCESS; |
| 7807 | } |
| 7808 | |
| 7809 | /** |
| 7810 | * @brief Evaluate MultiplicativeExpr. Logs directly on error. |
| 7811 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7812 | * [18] MultiplicativeExpr ::= UnaryExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7813 | * | MultiplicativeExpr '*' UnaryExpr |
| 7814 | * | MultiplicativeExpr 'div' UnaryExpr |
| 7815 | * | MultiplicativeExpr 'mod' UnaryExpr |
| 7816 | * |
| 7817 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7818 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7819 | * @param[in] repeat How many times this expression is repeated. |
| 7820 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7821 | * @param[in] options XPath options. |
| 7822 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7823 | */ |
| 7824 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7825 | 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] | 7826 | { |
| 7827 | LY_ERR rc; |
| 7828 | uint16_t this_op; |
| 7829 | struct lyxp_set orig_set, set2; |
| 7830 | uint16_t i; |
| 7831 | |
| 7832 | assert(repeat); |
| 7833 | |
| 7834 | set_init(&orig_set, set); |
| 7835 | set_init(&set2, set); |
| 7836 | |
| 7837 | set_fill_set(&orig_set, set); |
| 7838 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7839 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7840 | LY_CHECK_GOTO(rc, cleanup); |
| 7841 | |
| 7842 | /* ('*' / 'div' / 'mod' UnaryExpr)* */ |
| 7843 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7844 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7845 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7846 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7847 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7848 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7849 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7850 | |
| 7851 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7852 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7853 | LY_CHECK_GOTO(rc, cleanup); |
| 7854 | continue; |
| 7855 | } |
| 7856 | |
| 7857 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7858 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7859 | LY_CHECK_GOTO(rc, cleanup); |
| 7860 | |
| 7861 | /* eval */ |
| 7862 | if (options & LYXP_SCNODE_ALL) { |
| 7863 | 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] | 7864 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7865 | set_scnode_clear_ctx(set); |
| 7866 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7867 | 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] | 7868 | LY_CHECK_GOTO(rc, cleanup); |
| 7869 | } |
| 7870 | } |
| 7871 | |
| 7872 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7873 | lyxp_set_free_content(&orig_set); |
| 7874 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7875 | return rc; |
| 7876 | } |
| 7877 | |
| 7878 | /** |
| 7879 | * @brief Evaluate AdditiveExpr. Logs directly on error. |
| 7880 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7881 | * [17] AdditiveExpr ::= MultiplicativeExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7882 | * | AdditiveExpr '+' MultiplicativeExpr |
| 7883 | * | AdditiveExpr '-' MultiplicativeExpr |
| 7884 | * |
| 7885 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7886 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7887 | * @param[in] repeat How many times this expression is repeated. |
| 7888 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7889 | * @param[in] options XPath options. |
| 7890 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7891 | */ |
| 7892 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7893 | 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] | 7894 | { |
| 7895 | LY_ERR rc; |
| 7896 | uint16_t this_op; |
| 7897 | struct lyxp_set orig_set, set2; |
| 7898 | uint16_t i; |
| 7899 | |
| 7900 | assert(repeat); |
| 7901 | |
| 7902 | set_init(&orig_set, set); |
| 7903 | set_init(&set2, set); |
| 7904 | |
| 7905 | set_fill_set(&orig_set, set); |
| 7906 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7907 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7908 | LY_CHECK_GOTO(rc, cleanup); |
| 7909 | |
| 7910 | /* ('+' / '-' MultiplicativeExpr)* */ |
| 7911 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7912 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7913 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7914 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7915 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7916 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7917 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7918 | |
| 7919 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7920 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7921 | LY_CHECK_GOTO(rc, cleanup); |
| 7922 | continue; |
| 7923 | } |
| 7924 | |
| 7925 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7926 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7927 | LY_CHECK_GOTO(rc, cleanup); |
| 7928 | |
| 7929 | /* eval */ |
| 7930 | if (options & LYXP_SCNODE_ALL) { |
| 7931 | 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] | 7932 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7933 | set_scnode_clear_ctx(set); |
| 7934 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7935 | 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] | 7936 | LY_CHECK_GOTO(rc, cleanup); |
| 7937 | } |
| 7938 | } |
| 7939 | |
| 7940 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7941 | lyxp_set_free_content(&orig_set); |
| 7942 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7943 | return rc; |
| 7944 | } |
| 7945 | |
| 7946 | /** |
| 7947 | * @brief Evaluate RelationalExpr. Logs directly on error. |
| 7948 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7949 | * [16] RelationalExpr ::= AdditiveExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7950 | * | RelationalExpr '<' AdditiveExpr |
| 7951 | * | RelationalExpr '>' AdditiveExpr |
| 7952 | * | RelationalExpr '<=' AdditiveExpr |
| 7953 | * | RelationalExpr '>=' AdditiveExpr |
| 7954 | * |
| 7955 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7956 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7957 | * @param[in] repeat How many times this expression is repeated. |
| 7958 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7959 | * @param[in] options XPath options. |
| 7960 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7961 | */ |
| 7962 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7963 | 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] | 7964 | { |
| 7965 | LY_ERR rc; |
| 7966 | uint16_t this_op; |
| 7967 | struct lyxp_set orig_set, set2; |
| 7968 | uint16_t i; |
| 7969 | |
| 7970 | assert(repeat); |
| 7971 | |
| 7972 | set_init(&orig_set, set); |
| 7973 | set_init(&set2, set); |
| 7974 | |
| 7975 | set_fill_set(&orig_set, set); |
| 7976 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7977 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7978 | LY_CHECK_GOTO(rc, cleanup); |
| 7979 | |
| 7980 | /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */ |
| 7981 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7982 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7983 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7984 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7985 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7986 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7987 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7988 | |
| 7989 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7990 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7991 | LY_CHECK_GOTO(rc, cleanup); |
| 7992 | continue; |
| 7993 | } |
| 7994 | |
| 7995 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7996 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7997 | LY_CHECK_GOTO(rc, cleanup); |
| 7998 | |
| 7999 | /* eval */ |
| 8000 | if (options & LYXP_SCNODE_ALL) { |
| 8001 | 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] | 8002 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8003 | set_scnode_clear_ctx(set); |
| 8004 | } else { |
| 8005 | rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options); |
| 8006 | LY_CHECK_GOTO(rc, cleanup); |
| 8007 | } |
| 8008 | } |
| 8009 | |
| 8010 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8011 | lyxp_set_free_content(&orig_set); |
| 8012 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8013 | return rc; |
| 8014 | } |
| 8015 | |
| 8016 | /** |
| 8017 | * @brief Evaluate EqualityExpr. Logs directly on error. |
| 8018 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8019 | * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8020 | * | EqualityExpr '!=' RelationalExpr |
| 8021 | * |
| 8022 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8023 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8024 | * @param[in] repeat How many times this expression is repeated. |
| 8025 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8026 | * @param[in] options XPath options. |
| 8027 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8028 | */ |
| 8029 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8030 | 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] | 8031 | { |
| 8032 | LY_ERR rc; |
| 8033 | uint16_t this_op; |
| 8034 | struct lyxp_set orig_set, set2; |
| 8035 | uint16_t i; |
| 8036 | |
| 8037 | assert(repeat); |
| 8038 | |
| 8039 | set_init(&orig_set, set); |
| 8040 | set_init(&set2, set); |
| 8041 | |
| 8042 | set_fill_set(&orig_set, set); |
| 8043 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8044 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8045 | LY_CHECK_GOTO(rc, cleanup); |
| 8046 | |
| 8047 | /* ('=' / '!=' RelationalExpr)* */ |
| 8048 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8049 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8050 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8051 | 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] | 8052 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8053 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 8054 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8055 | |
| 8056 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8057 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8058 | LY_CHECK_GOTO(rc, cleanup); |
| 8059 | continue; |
| 8060 | } |
| 8061 | |
| 8062 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8063 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8064 | LY_CHECK_GOTO(rc, cleanup); |
| 8065 | |
| 8066 | /* eval */ |
| 8067 | if (options & LYXP_SCNODE_ALL) { |
| 8068 | 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] | 8069 | warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1); |
| 8070 | 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] | 8071 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8072 | set_scnode_clear_ctx(set); |
| 8073 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8074 | rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options); |
| 8075 | LY_CHECK_GOTO(rc, cleanup); |
| 8076 | } |
| 8077 | } |
| 8078 | |
| 8079 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8080 | lyxp_set_free_content(&orig_set); |
| 8081 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8082 | return rc; |
| 8083 | } |
| 8084 | |
| 8085 | /** |
| 8086 | * @brief Evaluate AndExpr. Logs directly on error. |
| 8087 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8088 | * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8089 | * |
| 8090 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8091 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8092 | * @param[in] repeat How many times this expression is repeated. |
| 8093 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8094 | * @param[in] options XPath options. |
| 8095 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8096 | */ |
| 8097 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8098 | 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] | 8099 | { |
| 8100 | LY_ERR rc; |
| 8101 | struct lyxp_set orig_set, set2; |
| 8102 | uint16_t i; |
| 8103 | |
| 8104 | assert(repeat); |
| 8105 | |
| 8106 | set_init(&orig_set, set); |
| 8107 | set_init(&set2, set); |
| 8108 | |
| 8109 | set_fill_set(&orig_set, set); |
| 8110 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8111 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8112 | LY_CHECK_GOTO(rc, cleanup); |
| 8113 | |
| 8114 | /* cast to boolean, we know that will be the final result */ |
| 8115 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 8116 | set_scnode_clear_ctx(set); |
| 8117 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8118 | lyxp_set_cast(set, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8119 | } |
| 8120 | |
| 8121 | /* ('and' EqualityExpr)* */ |
| 8122 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8123 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG); |
| 8124 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || !set->val.bln ? "skipped" : "parsed"), |
| 8125 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 8126 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8127 | |
| 8128 | /* lazy evaluation */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8129 | if (!set || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) { |
| 8130 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8131 | LY_CHECK_GOTO(rc, cleanup); |
| 8132 | continue; |
| 8133 | } |
| 8134 | |
| 8135 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8136 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8137 | LY_CHECK_GOTO(rc, cleanup); |
| 8138 | |
| 8139 | /* eval - just get boolean value actually */ |
| 8140 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 8141 | set_scnode_clear_ctx(&set2); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 8142 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8143 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8144 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8145 | set_fill_set(set, &set2); |
| 8146 | } |
| 8147 | } |
| 8148 | |
| 8149 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8150 | lyxp_set_free_content(&orig_set); |
| 8151 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8152 | return rc; |
| 8153 | } |
| 8154 | |
| 8155 | /** |
| 8156 | * @brief Evaluate OrExpr. Logs directly on error. |
| 8157 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8158 | * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8159 | * |
| 8160 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8161 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8162 | * @param[in] repeat How many times this expression is repeated. |
| 8163 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8164 | * @param[in] options XPath options. |
| 8165 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8166 | */ |
| 8167 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8168 | 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] | 8169 | { |
| 8170 | LY_ERR rc; |
| 8171 | struct lyxp_set orig_set, set2; |
| 8172 | uint16_t i; |
| 8173 | |
| 8174 | assert(repeat); |
| 8175 | |
| 8176 | set_init(&orig_set, set); |
| 8177 | set_init(&set2, set); |
| 8178 | |
| 8179 | set_fill_set(&orig_set, set); |
| 8180 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8181 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8182 | LY_CHECK_GOTO(rc, cleanup); |
| 8183 | |
| 8184 | /* cast to boolean, we know that will be the final result */ |
| 8185 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 8186 | set_scnode_clear_ctx(set); |
| 8187 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8188 | lyxp_set_cast(set, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8189 | } |
| 8190 | |
| 8191 | /* ('or' AndExpr)* */ |
| 8192 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8193 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG); |
| 8194 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || set->val.bln ? "skipped" : "parsed"), |
| 8195 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 8196 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8197 | |
| 8198 | /* lazy evaluation */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8199 | if (!set || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) { |
| 8200 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8201 | LY_CHECK_GOTO(rc, cleanup); |
| 8202 | continue; |
| 8203 | } |
| 8204 | |
| 8205 | set_fill_set(&set2, &orig_set); |
| 8206 | /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one), |
| 8207 | * but it does not matter */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8208 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8209 | LY_CHECK_GOTO(rc, cleanup); |
| 8210 | |
| 8211 | /* eval - just get boolean value actually */ |
| 8212 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 8213 | set_scnode_clear_ctx(&set2); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 8214 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8215 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8216 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8217 | set_fill_set(set, &set2); |
| 8218 | } |
| 8219 | } |
| 8220 | |
| 8221 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8222 | lyxp_set_free_content(&orig_set); |
| 8223 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8224 | return rc; |
| 8225 | } |
| 8226 | |
| 8227 | /** |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8228 | * @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] | 8229 | * |
| 8230 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8231 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8232 | * @param[in] etype Expression type to evaluate. |
| 8233 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8234 | * @param[in] options XPath options. |
| 8235 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8236 | */ |
| 8237 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8238 | 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] | 8239 | { |
| 8240 | uint16_t i, count; |
| 8241 | enum lyxp_expr_type next_etype; |
| 8242 | LY_ERR rc; |
| 8243 | |
| 8244 | /* process operator repeats */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8245 | if (!exp->repeat[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8246 | next_etype = LYXP_EXPR_NONE; |
| 8247 | } else { |
| 8248 | /* find etype repeat */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8249 | for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8250 | |
| 8251 | /* select one-priority lower because etype expression called us */ |
| 8252 | if (i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8253 | next_etype = exp->repeat[*tok_idx][i - 1]; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8254 | /* count repeats for that expression */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8255 | 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] | 8256 | } else { |
| 8257 | next_etype = LYXP_EXPR_NONE; |
| 8258 | } |
| 8259 | } |
| 8260 | |
| 8261 | /* decide what expression are we parsing based on the repeat */ |
| 8262 | switch (next_etype) { |
| 8263 | case LYXP_EXPR_OR: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8264 | rc = eval_or_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_AND: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8267 | rc = eval_and_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_EQUALITY: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8270 | rc = eval_equality_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8271 | break; |
| 8272 | case LYXP_EXPR_RELATIONAL: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8273 | rc = eval_relational_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8274 | break; |
| 8275 | case LYXP_EXPR_ADDITIVE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8276 | rc = eval_additive_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8277 | break; |
| 8278 | case LYXP_EXPR_MULTIPLICATIVE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8279 | rc = eval_multiplicative_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8280 | break; |
| 8281 | case LYXP_EXPR_UNARY: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8282 | rc = eval_unary_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8283 | break; |
| 8284 | case LYXP_EXPR_UNION: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8285 | rc = eval_union_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8286 | break; |
| 8287 | case LYXP_EXPR_NONE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8288 | rc = eval_path_expr(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8289 | break; |
| 8290 | default: |
| 8291 | LOGINT_RET(set->ctx); |
| 8292 | } |
| 8293 | |
| 8294 | return rc; |
| 8295 | } |
| 8296 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8297 | /** |
| 8298 | * @brief Get root type. |
| 8299 | * |
| 8300 | * @param[in] ctx_node Context node. |
| 8301 | * @param[in] ctx_scnode Schema context node. |
| 8302 | * @param[in] options XPath options. |
| 8303 | * @return Root type. |
| 8304 | */ |
| 8305 | static enum lyxp_node_type |
| 8306 | lyxp_get_root_type(const struct lyd_node *ctx_node, const struct lysc_node *ctx_scnode, int options) |
| 8307 | { |
| 8308 | if (options & LYXP_SCNODE_ALL) { |
| 8309 | if (options & LYXP_SCNODE) { |
| 8310 | /* general root that can access everything */ |
| 8311 | return LYXP_NODE_ROOT; |
| 8312 | } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) { |
| 8313 | /* root context node can access only config data (because we said so, it is unspecified) */ |
| 8314 | return LYXP_NODE_ROOT_CONFIG; |
| 8315 | } else { |
| 8316 | return LYXP_NODE_ROOT; |
| 8317 | } |
| 8318 | } |
| 8319 | |
| 8320 | if (!ctx_node || (ctx_node->schema->flags & LYS_CONFIG_W)) { |
| 8321 | /* root context node can access only config data (because we said so, it is unspecified) */ |
| 8322 | return LYXP_NODE_ROOT_CONFIG; |
| 8323 | } |
| 8324 | |
| 8325 | return LYXP_NODE_ROOT; |
| 8326 | } |
| 8327 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8328 | LY_ERR |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 8329 | 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] | 8330 | 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] | 8331 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8332 | uint16_t tok_idx = 0; |
| 8333 | const struct lyd_node *real_ctx_node; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8334 | LY_ERR rc; |
| 8335 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8336 | LY_CHECK_ARG_RET(NULL, exp, local_mod, ctx_node, set, LY_EINVAL); |
| 8337 | |
| 8338 | if ((ctx_node_type == LYXP_NODE_ROOT) || (ctx_node_type == LYXP_NODE_ROOT_CONFIG)) { |
| 8339 | /* we always need some context node because it is used for resolving unqualified names */ |
| 8340 | real_ctx_node = NULL; |
| 8341 | } else { |
| 8342 | real_ctx_node = ctx_node; |
| 8343 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8344 | |
| 8345 | /* prepare set for evaluation */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8346 | tok_idx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8347 | memset(set, 0, sizeof *set); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8348 | set->type = LYXP_SET_NODE_SET; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8349 | 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] | 8350 | set->ctx = local_mod->ctx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8351 | set->ctx_node = ctx_node; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8352 | set->root_type = lyxp_get_root_type(real_ctx_node, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8353 | set->local_mod = local_mod; |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 8354 | set->tree = tree; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8355 | set->format = format; |
| 8356 | |
| 8357 | /* evaluate */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8358 | rc = eval_expr_select(exp, &tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8359 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8360 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8361 | } |
| 8362 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8363 | return rc; |
| 8364 | } |
| 8365 | |
| 8366 | #if 0 |
| 8367 | |
| 8368 | /* full xml printing of set elements, not used currently */ |
| 8369 | |
| 8370 | void |
| 8371 | lyxp_set_print_xml(FILE *f, struct lyxp_set *set) |
| 8372 | { |
| 8373 | uint32_t i; |
| 8374 | char *str_num; |
| 8375 | struct lyout out; |
| 8376 | |
| 8377 | memset(&out, 0, sizeof out); |
| 8378 | |
| 8379 | out.type = LYOUT_STREAM; |
| 8380 | out.method.f = f; |
| 8381 | |
| 8382 | switch (set->type) { |
| 8383 | case LYXP_SET_EMPTY: |
| 8384 | ly_print(&out, "Empty XPath set\n\n"); |
| 8385 | break; |
| 8386 | case LYXP_SET_BOOLEAN: |
| 8387 | ly_print(&out, "Boolean XPath set:\n"); |
| 8388 | ly_print(&out, "%s\n\n", set->value.bool ? "true" : "false"); |
| 8389 | break; |
| 8390 | case LYXP_SET_STRING: |
| 8391 | ly_print(&out, "String XPath set:\n"); |
| 8392 | ly_print(&out, "\"%s\"\n\n", set->value.str); |
| 8393 | break; |
| 8394 | case LYXP_SET_NUMBER: |
| 8395 | ly_print(&out, "Number XPath set:\n"); |
| 8396 | |
| 8397 | if (isnan(set->value.num)) { |
| 8398 | str_num = strdup("NaN"); |
| 8399 | } else if ((set->value.num == 0) || (set->value.num == -0.0f)) { |
| 8400 | str_num = strdup("0"); |
| 8401 | } else if (isinf(set->value.num) && !signbit(set->value.num)) { |
| 8402 | str_num = strdup("Infinity"); |
| 8403 | } else if (isinf(set->value.num) && signbit(set->value.num)) { |
| 8404 | str_num = strdup("-Infinity"); |
| 8405 | } else if ((long long)set->value.num == set->value.num) { |
| 8406 | if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) { |
| 8407 | str_num = NULL; |
| 8408 | } |
| 8409 | } else { |
| 8410 | if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) { |
| 8411 | str_num = NULL; |
| 8412 | } |
| 8413 | } |
| 8414 | if (!str_num) { |
| 8415 | LOGMEM; |
| 8416 | return; |
| 8417 | } |
| 8418 | ly_print(&out, "%s\n\n", str_num); |
| 8419 | free(str_num); |
| 8420 | break; |
| 8421 | case LYXP_SET_NODE_SET: |
| 8422 | ly_print(&out, "Node XPath set:\n"); |
| 8423 | |
| 8424 | for (i = 0; i < set->used; ++i) { |
| 8425 | ly_print(&out, "%d. ", i + 1); |
| 8426 | switch (set->node_type[i]) { |
| 8427 | case LYXP_NODE_ROOT_ALL: |
| 8428 | ly_print(&out, "ROOT all\n\n"); |
| 8429 | break; |
| 8430 | case LYXP_NODE_ROOT_CONFIG: |
| 8431 | ly_print(&out, "ROOT config\n\n"); |
| 8432 | break; |
| 8433 | case LYXP_NODE_ROOT_STATE: |
| 8434 | ly_print(&out, "ROOT state\n\n"); |
| 8435 | break; |
| 8436 | case LYXP_NODE_ROOT_NOTIF: |
| 8437 | ly_print(&out, "ROOT notification \"%s\"\n\n", set->value.nodes[i]->schema->name); |
| 8438 | break; |
| 8439 | case LYXP_NODE_ROOT_RPC: |
| 8440 | ly_print(&out, "ROOT rpc \"%s\"\n\n", set->value.nodes[i]->schema->name); |
| 8441 | break; |
| 8442 | case LYXP_NODE_ROOT_OUTPUT: |
| 8443 | ly_print(&out, "ROOT output \"%s\"\n\n", set->value.nodes[i]->schema->name); |
| 8444 | break; |
| 8445 | case LYXP_NODE_ELEM: |
| 8446 | ly_print(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name); |
| 8447 | xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT); |
| 8448 | ly_print(&out, "\n"); |
| 8449 | break; |
| 8450 | case LYXP_NODE_TEXT: |
| 8451 | ly_print(&out, "TEXT \"%s\"\n\n", ((struct lyd_node_leaf_list *)set->value.nodes[i])->value_str); |
| 8452 | break; |
| 8453 | case LYXP_NODE_ATTR: |
| 8454 | ly_print(&out, "ATTR \"%s\" = \"%s\"\n\n", set->value.attrs[i]->name, set->value.attrs[i]->value); |
| 8455 | break; |
| 8456 | } |
| 8457 | } |
| 8458 | break; |
| 8459 | } |
| 8460 | } |
| 8461 | |
| 8462 | #endif |
| 8463 | |
| 8464 | LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8465 | lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8466 | { |
| 8467 | long double num; |
| 8468 | char *str; |
| 8469 | LY_ERR rc; |
| 8470 | |
| 8471 | if (!set || (set->type == target)) { |
| 8472 | return LY_SUCCESS; |
| 8473 | } |
| 8474 | |
| 8475 | /* it's not possible to convert anything into a node set */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8476 | assert(target != LYXP_SET_NODE_SET); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8477 | |
| 8478 | if (set->type == LYXP_SET_SCNODE_SET) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8479 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8480 | return LY_EINVAL; |
| 8481 | } |
| 8482 | |
| 8483 | /* to STRING */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8484 | 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] | 8485 | switch (set->type) { |
| 8486 | case LYXP_SET_NUMBER: |
| 8487 | if (isnan(set->val.num)) { |
| 8488 | set->val.str = strdup("NaN"); |
| 8489 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 8490 | } else if ((set->val.num == 0) || (set->val.num == -0.0f)) { |
| 8491 | set->val.str = strdup("0"); |
| 8492 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 8493 | } else if (isinf(set->val.num) && !signbit(set->val.num)) { |
| 8494 | set->val.str = strdup("Infinity"); |
| 8495 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 8496 | } else if (isinf(set->val.num) && signbit(set->val.num)) { |
| 8497 | set->val.str = strdup("-Infinity"); |
| 8498 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 8499 | } else if ((long long)set->val.num == set->val.num) { |
| 8500 | if (asprintf(&str, "%lld", (long long)set->val.num) == -1) { |
| 8501 | LOGMEM_RET(set->ctx); |
| 8502 | } |
| 8503 | set->val.str = str; |
| 8504 | } else { |
| 8505 | if (asprintf(&str, "%03.1Lf", set->val.num) == -1) { |
| 8506 | LOGMEM_RET(set->ctx); |
| 8507 | } |
| 8508 | set->val.str = str; |
| 8509 | } |
| 8510 | break; |
| 8511 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8512 | if (set->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8513 | set->val.str = strdup("true"); |
| 8514 | } else { |
| 8515 | set->val.str = strdup("false"); |
| 8516 | } |
| 8517 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM); |
| 8518 | break; |
| 8519 | case LYXP_SET_NODE_SET: |
| 8520 | assert(set->used); |
| 8521 | |
| 8522 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8523 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8524 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8525 | rc = cast_node_set_to_string(set, &str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8526 | LY_CHECK_RET(rc); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8527 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8528 | set->val.str = str; |
| 8529 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8530 | default: |
| 8531 | LOGINT_RET(set->ctx); |
| 8532 | } |
| 8533 | set->type = LYXP_SET_STRING; |
| 8534 | } |
| 8535 | |
| 8536 | /* to NUMBER */ |
| 8537 | if (target == LYXP_SET_NUMBER) { |
| 8538 | switch (set->type) { |
| 8539 | case LYXP_SET_STRING: |
| 8540 | num = cast_string_to_number(set->val.str); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8541 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8542 | set->val.num = num; |
| 8543 | break; |
| 8544 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8545 | if (set->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8546 | set->val.num = 1; |
| 8547 | } else { |
| 8548 | set->val.num = 0; |
| 8549 | } |
| 8550 | break; |
| 8551 | default: |
| 8552 | LOGINT_RET(set->ctx); |
| 8553 | } |
| 8554 | set->type = LYXP_SET_NUMBER; |
| 8555 | } |
| 8556 | |
| 8557 | /* to BOOLEAN */ |
| 8558 | if (target == LYXP_SET_BOOLEAN) { |
| 8559 | switch (set->type) { |
| 8560 | case LYXP_SET_NUMBER: |
| 8561 | 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] | 8562 | set->val.bln = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8563 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8564 | set->val.bln = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8565 | } |
| 8566 | break; |
| 8567 | case LYXP_SET_STRING: |
| 8568 | if (set->val.str[0]) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 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 | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8571 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 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 | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8574 | } |
| 8575 | break; |
| 8576 | case LYXP_SET_NODE_SET: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8577 | if (set->used) { |
| 8578 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8579 | set->val.bln = 1; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8580 | } else { |
| 8581 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8582 | set->val.bln = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8583 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8584 | break; |
| 8585 | default: |
| 8586 | LOGINT_RET(set->ctx); |
| 8587 | } |
| 8588 | set->type = LYXP_SET_BOOLEAN; |
| 8589 | } |
| 8590 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8591 | return LY_SUCCESS; |
| 8592 | } |
| 8593 | |
| 8594 | LY_ERR |
| 8595 | lyxp_atomize(struct lyxp_expr *exp, LYD_FORMAT format, const struct lys_module *local_mod, const struct lysc_node *ctx_scnode, |
| 8596 | enum lyxp_node_type ctx_scnode_type, struct lyxp_set *set, int options) |
| 8597 | { |
| 8598 | struct ly_ctx *ctx; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8599 | const struct lysc_node *real_ctx_scnode; |
| 8600 | uint16_t tok_idx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8601 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8602 | 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] | 8603 | |
| 8604 | ctx = local_mod->ctx; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8605 | if ((ctx_scnode_type == LYXP_NODE_ROOT) || (ctx_scnode_type == LYXP_NODE_ROOT_CONFIG)) { |
| 8606 | /* we always need some context node because it is used for resolving unqualified names */ |
| 8607 | real_ctx_scnode = NULL; |
| 8608 | } else { |
| 8609 | real_ctx_scnode = ctx_scnode; |
| 8610 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8611 | |
| 8612 | /* prepare set for evaluation */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8613 | tok_idx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8614 | memset(set, 0, sizeof *set); |
| 8615 | set->type = LYXP_SET_SCNODE_SET; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8616 | lyxp_set_scnode_insert_node(set, real_ctx_scnode, ctx_scnode_type); |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 8617 | set->val.scnodes[0].in_ctx = -2; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8618 | set->ctx = ctx; |
| 8619 | set->ctx_scnode = ctx_scnode; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8620 | set->root_type = lyxp_get_root_type(NULL, real_ctx_scnode, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8621 | set->local_mod = local_mod; |
| 8622 | set->format = format; |
| 8623 | |
| 8624 | /* evaluate */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8625 | return eval_expr_select(exp, &tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8626 | } |