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" |
| 32 | #include "context.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 33 | #include "dict.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 34 | #include "hash_table.h" |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 35 | #include "path.h" |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 36 | #include "plugins_types.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 37 | #include "printer.h" |
| 38 | #include "printer_data.h" |
| 39 | #include "tree.h" |
| 40 | #include "tree_data_internal.h" |
| 41 | #include "tree_schema_internal.h" |
| 42 | #include "xml.h" |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 43 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 44 | static LY_ERR reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx); |
| 45 | 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] | 46 | |
| 47 | /** |
| 48 | * @brief Print the type of an XPath \p set. |
| 49 | * |
| 50 | * @param[in] set Set to use. |
| 51 | * @return Set type string. |
| 52 | */ |
| 53 | static const char * |
| 54 | print_set_type(struct lyxp_set *set) |
| 55 | { |
| 56 | switch (set->type) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 57 | case LYXP_SET_NODE_SET: |
| 58 | return "node set"; |
| 59 | case LYXP_SET_SCNODE_SET: |
| 60 | return "schema node set"; |
| 61 | case LYXP_SET_BOOLEAN: |
| 62 | return "boolean"; |
| 63 | case LYXP_SET_NUMBER: |
| 64 | return "number"; |
| 65 | case LYXP_SET_STRING: |
| 66 | return "string"; |
| 67 | } |
| 68 | |
| 69 | return NULL; |
| 70 | } |
| 71 | |
Michal Vasko | 24cddf8 | 2020-06-01 08:17:01 +0200 | [diff] [blame] | 72 | const char * |
| 73 | lyxp_print_token(enum lyxp_token tok) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 74 | { |
| 75 | switch (tok) { |
| 76 | case LYXP_TOKEN_PAR1: |
| 77 | return "("; |
| 78 | case LYXP_TOKEN_PAR2: |
| 79 | return ")"; |
| 80 | case LYXP_TOKEN_BRACK1: |
| 81 | return "["; |
| 82 | case LYXP_TOKEN_BRACK2: |
| 83 | return "]"; |
| 84 | case LYXP_TOKEN_DOT: |
| 85 | return "."; |
| 86 | case LYXP_TOKEN_DDOT: |
| 87 | return ".."; |
| 88 | case LYXP_TOKEN_AT: |
| 89 | return "@"; |
| 90 | case LYXP_TOKEN_COMMA: |
| 91 | return ","; |
| 92 | case LYXP_TOKEN_NAMETEST: |
| 93 | return "NameTest"; |
| 94 | case LYXP_TOKEN_NODETYPE: |
| 95 | return "NodeType"; |
| 96 | case LYXP_TOKEN_FUNCNAME: |
| 97 | return "FunctionName"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 98 | case LYXP_TOKEN_OPER_LOG: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 99 | return "Operator(Logic)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 100 | case LYXP_TOKEN_OPER_EQUAL: |
| 101 | return "Operator(Equal)"; |
| 102 | case LYXP_TOKEN_OPER_NEQUAL: |
| 103 | return "Operator(Non-equal)"; |
| 104 | case LYXP_TOKEN_OPER_COMP: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 105 | return "Operator(Comparison)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 106 | case LYXP_TOKEN_OPER_MATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 107 | return "Operator(Math)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 108 | case LYXP_TOKEN_OPER_UNI: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 109 | return "Operator(Union)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 110 | case LYXP_TOKEN_OPER_PATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 111 | return "Operator(Path)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 112 | case LYXP_TOKEN_OPER_RPATH: |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 113 | return "Operator(Recursive Path)"; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 114 | case LYXP_TOKEN_LITERAL: |
| 115 | return "Literal"; |
| 116 | case LYXP_TOKEN_NUMBER: |
| 117 | return "Number"; |
| 118 | default: |
| 119 | LOGINT(NULL); |
| 120 | return ""; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * @brief Print the whole expression \p exp to debug output. |
| 126 | * |
| 127 | * @param[in] exp Expression to use. |
| 128 | */ |
| 129 | static void |
| 130 | print_expr_struct_debug(struct lyxp_expr *exp) |
| 131 | { |
| 132 | uint16_t i, j; |
| 133 | char tmp[128]; |
| 134 | |
| 135 | if (!exp || (ly_log_level < LY_LLDBG)) { |
| 136 | return; |
| 137 | } |
| 138 | |
| 139 | LOGDBG(LY_LDGXPATH, "expression \"%s\":", exp->expr); |
| 140 | for (i = 0; i < exp->used; ++i) { |
Michal Vasko | 24cddf8 | 2020-06-01 08:17:01 +0200 | [diff] [blame] | 141 | 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] | 142 | &exp->expr[exp->tok_pos[i]]); |
| 143 | if (exp->repeat[i]) { |
| 144 | sprintf(tmp + strlen(tmp), " (repeat %d", exp->repeat[i][0]); |
| 145 | for (j = 1; exp->repeat[i][j]; ++j) { |
| 146 | sprintf(tmp + strlen(tmp), ", %d", exp->repeat[i][j]); |
| 147 | } |
| 148 | strcat(tmp, ")"); |
| 149 | } |
| 150 | LOGDBG(LY_LDGXPATH, tmp); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | #ifndef NDEBUG |
| 155 | |
| 156 | /** |
| 157 | * @brief Print XPath set content to debug output. |
| 158 | * |
| 159 | * @param[in] set Set to print. |
| 160 | */ |
| 161 | static void |
| 162 | print_set_debug(struct lyxp_set *set) |
| 163 | { |
| 164 | uint32_t i; |
| 165 | char *str; |
| 166 | int dynamic; |
| 167 | struct lyxp_set_node *item; |
| 168 | struct lyxp_set_scnode *sitem; |
| 169 | |
| 170 | if (ly_log_level < LY_LLDBG) { |
| 171 | return; |
| 172 | } |
| 173 | |
| 174 | switch (set->type) { |
| 175 | case LYXP_SET_NODE_SET: |
| 176 | LOGDBG(LY_LDGXPATH, "set NODE SET:"); |
| 177 | for (i = 0; i < set->used; ++i) { |
| 178 | item = &set->val.nodes[i]; |
| 179 | |
| 180 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 181 | case LYXP_NODE_NONE: |
| 182 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): NONE", i + 1, item->pos); |
| 183 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 184 | case LYXP_NODE_ROOT: |
| 185 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT", i + 1, item->pos); |
| 186 | break; |
| 187 | case LYXP_NODE_ROOT_CONFIG: |
| 188 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT CONFIG", i + 1, item->pos); |
| 189 | break; |
| 190 | case LYXP_NODE_ELEM: |
| 191 | if ((item->node->schema->nodetype == LYS_LIST) |
| 192 | && (((struct lyd_node_inner *)item->node)->child->schema->nodetype == LYS_LEAF)) { |
| 193 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (1st child val: %s)", i + 1, item->pos, |
| 194 | item->node->schema->name, |
| 195 | (str = (char *)lyd_value2str((struct lyd_node_term *)lyd_node_children(item->node), &dynamic))); |
| 196 | if (dynamic) { |
| 197 | free(str); |
| 198 | } |
| 199 | } else if (((struct lyd_node_inner *)item->node)->schema->nodetype == LYS_LEAFLIST) { |
| 200 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (val: %s)", i + 1, item->pos, |
| 201 | item->node->schema->name, |
| 202 | (str = (char *)lyd_value2str((struct lyd_node_term *)item->node, &dynamic))); |
| 203 | if (dynamic) { |
| 204 | free(str); |
| 205 | } |
| 206 | } else { |
| 207 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s", i + 1, item->pos, item->node->schema->name); |
| 208 | } |
| 209 | break; |
| 210 | case LYXP_NODE_TEXT: |
| 211 | if (item->node->schema->nodetype & LYS_ANYDATA) { |
| 212 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT <%s>", i + 1, item->pos, |
| 213 | item->node->schema->nodetype == LYS_ANYXML ? "anyxml" : "anydata"); |
| 214 | } else { |
| 215 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT %s", i + 1, item->pos, |
| 216 | (str = (char *)lyd_value2str((struct lyd_node_term *)item->node, &dynamic))); |
| 217 | if (dynamic) { |
| 218 | free(str); |
| 219 | } |
| 220 | } |
| 221 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 222 | case LYXP_NODE_META: |
| 223 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): META %s = %s", i + 1, item->pos, set->val.meta[i].meta->name, |
| 224 | set->val.meta[i].meta->value); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 225 | break; |
| 226 | } |
| 227 | } |
| 228 | break; |
| 229 | |
| 230 | case LYXP_SET_SCNODE_SET: |
| 231 | LOGDBG(LY_LDGXPATH, "set SCNODE SET:"); |
| 232 | for (i = 0; i < set->used; ++i) { |
| 233 | sitem = &set->val.scnodes[i]; |
| 234 | |
| 235 | switch (sitem->type) { |
| 236 | case LYXP_NODE_ROOT: |
| 237 | LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT", i + 1, sitem->in_ctx); |
| 238 | break; |
| 239 | case LYXP_NODE_ROOT_CONFIG: |
| 240 | LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT CONFIG", i + 1, sitem->in_ctx); |
| 241 | break; |
| 242 | case LYXP_NODE_ELEM: |
| 243 | LOGDBG(LY_LDGXPATH, "\t%d (%u): ELEM %s", i + 1, sitem->in_ctx, sitem->scnode->name); |
| 244 | break; |
| 245 | default: |
| 246 | LOGINT(NULL); |
| 247 | break; |
| 248 | } |
| 249 | } |
| 250 | break; |
| 251 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 252 | case LYXP_SET_BOOLEAN: |
| 253 | LOGDBG(LY_LDGXPATH, "set BOOLEAN"); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 254 | LOGDBG(LY_LDGXPATH, "\t%s", (set->val.bln ? "true" : "false")); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 255 | break; |
| 256 | |
| 257 | case LYXP_SET_STRING: |
| 258 | LOGDBG(LY_LDGXPATH, "set STRING"); |
| 259 | LOGDBG(LY_LDGXPATH, "\t%s", set->val.str); |
| 260 | break; |
| 261 | |
| 262 | case LYXP_SET_NUMBER: |
| 263 | LOGDBG(LY_LDGXPATH, "set NUMBER"); |
| 264 | |
| 265 | if (isnan(set->val.num)) { |
| 266 | str = strdup("NaN"); |
| 267 | } else if ((set->val.num == 0) || (set->val.num == -0.0f)) { |
| 268 | str = strdup("0"); |
| 269 | } else if (isinf(set->val.num) && !signbit(set->val.num)) { |
| 270 | str = strdup("Infinity"); |
| 271 | } else if (isinf(set->val.num) && signbit(set->val.num)) { |
| 272 | str = strdup("-Infinity"); |
| 273 | } else if ((long long)set->val.num == set->val.num) { |
| 274 | if (asprintf(&str, "%lld", (long long)set->val.num) == -1) { |
| 275 | str = NULL; |
| 276 | } |
| 277 | } else { |
| 278 | if (asprintf(&str, "%03.1Lf", set->val.num) == -1) { |
| 279 | str = NULL; |
| 280 | } |
| 281 | } |
| 282 | LY_CHECK_ERR_RET(!str, LOGMEM(NULL), ); |
| 283 | |
| 284 | LOGDBG(LY_LDGXPATH, "\t%s", str); |
| 285 | free(str); |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | #endif |
| 290 | |
| 291 | /** |
| 292 | * @brief Realloc the string \p str. |
| 293 | * |
| 294 | * @param[in] ctx libyang context for logging. |
| 295 | * @param[in] needed How much free space is required. |
| 296 | * @param[in,out] str Pointer to the string to use. |
| 297 | * @param[in,out] used Used bytes in \p str. |
| 298 | * @param[in,out] size Allocated bytes in \p str. |
| 299 | * @return LY_ERR |
| 300 | */ |
| 301 | static LY_ERR |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 302 | 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] | 303 | { |
| 304 | if (*size - *used < needed) { |
| 305 | do { |
| 306 | if ((UINT16_MAX - *size) < LYXP_STRING_CAST_SIZE_STEP) { |
| 307 | LOGERR(ctx, LY_EINVAL, "XPath string length limit (%u) reached.", UINT16_MAX); |
| 308 | return LY_EINVAL; |
| 309 | } |
| 310 | *size += LYXP_STRING_CAST_SIZE_STEP; |
| 311 | } while (*size - *used < needed); |
| 312 | *str = ly_realloc(*str, *size * sizeof(char)); |
| 313 | LY_CHECK_ERR_RET(!(*str), LOGMEM(ctx), LY_EMEM); |
| 314 | } |
| 315 | |
| 316 | return LY_SUCCESS; |
| 317 | } |
| 318 | |
| 319 | /** |
| 320 | * @brief Cast nodes recursively to one string @p str. |
| 321 | * |
| 322 | * @param[in] node Node to cast. |
| 323 | * @param[in] fake_cont Whether to put the data into a "fake" container. |
| 324 | * @param[in] root_type Type of the XPath root. |
| 325 | * @param[in] indent Current indent. |
| 326 | * @param[in,out] str Resulting string. |
| 327 | * @param[in,out] used Used bytes in @p str. |
| 328 | * @param[in,out] size Allocated bytes in @p str. |
| 329 | * @return LY_ERR |
| 330 | */ |
| 331 | static LY_ERR |
| 332 | cast_string_recursive(const struct lyd_node *node, int fake_cont, enum lyxp_node_type root_type, uint16_t indent, char **str, |
| 333 | uint16_t *used, uint16_t *size) |
| 334 | { |
| 335 | char *buf, *line, *ptr; |
| 336 | const char *value_str; |
| 337 | int dynamic; |
| 338 | const struct lyd_node *child; |
| 339 | struct lyd_node_any *any; |
| 340 | LY_ERR rc; |
| 341 | |
| 342 | if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) { |
| 343 | return LY_SUCCESS; |
| 344 | } |
| 345 | |
| 346 | if (fake_cont) { |
| 347 | rc = cast_string_realloc(LYD_NODE_CTX(node), 1, str, used, size); |
| 348 | LY_CHECK_RET(rc); |
| 349 | strcpy(*str + (*used - 1), "\n"); |
| 350 | ++(*used); |
| 351 | |
| 352 | ++indent; |
| 353 | } |
| 354 | |
| 355 | switch (node->schema->nodetype) { |
| 356 | case LYS_CONTAINER: |
| 357 | case LYS_LIST: |
| 358 | case LYS_RPC: |
| 359 | case LYS_NOTIF: |
| 360 | rc = cast_string_realloc(LYD_NODE_CTX(node), 1, str, used, size); |
| 361 | LY_CHECK_RET(rc); |
| 362 | strcpy(*str + (*used - 1), "\n"); |
| 363 | ++(*used); |
| 364 | |
| 365 | for (child = lyd_node_children(node); child; child = child->next) { |
| 366 | rc = cast_string_recursive(child, 0, root_type, indent + 1, str, used, size); |
| 367 | LY_CHECK_RET(rc); |
| 368 | } |
| 369 | |
| 370 | break; |
| 371 | |
| 372 | case LYS_LEAF: |
| 373 | case LYS_LEAFLIST: |
| 374 | value_str = lyd_value2str(((struct lyd_node_term *)node), &dynamic); |
| 375 | |
| 376 | /* print indent */ |
| 377 | rc = cast_string_realloc(LYD_NODE_CTX(node), indent * 2 + strlen(value_str) + 1, str, used, size); |
| 378 | if (rc != LY_SUCCESS) { |
| 379 | if (dynamic) { |
| 380 | free((char *)value_str); |
| 381 | } |
| 382 | return rc; |
| 383 | } |
| 384 | memset(*str + (*used - 1), ' ', indent * 2); |
| 385 | *used += indent * 2; |
| 386 | |
| 387 | /* print value */ |
| 388 | if (*used == 1) { |
| 389 | sprintf(*str + (*used - 1), "%s", value_str); |
| 390 | *used += strlen(value_str); |
| 391 | } else { |
| 392 | sprintf(*str + (*used - 1), "%s\n", value_str); |
| 393 | *used += strlen(value_str) + 1; |
| 394 | } |
| 395 | if (dynamic) { |
| 396 | free((char *)value_str); |
| 397 | } |
| 398 | |
| 399 | break; |
| 400 | |
| 401 | case LYS_ANYXML: |
| 402 | case LYS_ANYDATA: |
| 403 | any = (struct lyd_node_any *)node; |
| 404 | if (!(void *)any->value.tree) { |
| 405 | /* no content */ |
| 406 | buf = strdup(""); |
| 407 | LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_NODE_CTX(node)), LY_EMEM); |
| 408 | } else { |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 409 | struct ly_out *out; |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 410 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 411 | switch (any->value_type) { |
| 412 | case LYD_ANYDATA_STRING: |
| 413 | case LYD_ANYDATA_XML: |
| 414 | case LYD_ANYDATA_JSON: |
| 415 | buf = strdup(any->value.json); |
| 416 | LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_NODE_CTX(node)), LY_EMEM); |
| 417 | break; |
| 418 | case LYD_ANYDATA_DATATREE: |
Radek Krejci | 84ce7b1 | 2020-06-11 17:28:25 +0200 | [diff] [blame^] | 419 | LY_CHECK_RET(ly_out_new_memory(&buf, 0, &out)); |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 420 | rc = lyd_print(out, any->value.tree, LYD_XML, LYDP_WITHSIBLINGS); |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 421 | ly_out_free(out, NULL, 0); |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 422 | LY_CHECK_RET(rc < 0, -rc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 423 | break; |
| 424 | /* TODO case LYD_ANYDATA_LYB: |
| 425 | LOGERR(LYD_NODE_CTX(node), LY_EINVAL, "Cannot convert LYB anydata into string."); |
| 426 | return -1;*/ |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | line = strtok_r(buf, "\n", &ptr); |
| 431 | do { |
| 432 | rc = cast_string_realloc(LYD_NODE_CTX(node), indent * 2 + strlen(line) + 1, str, used, size); |
| 433 | if (rc != LY_SUCCESS) { |
| 434 | free(buf); |
| 435 | return rc; |
| 436 | } |
| 437 | memset(*str + (*used - 1), ' ', indent * 2); |
| 438 | *used += indent * 2; |
| 439 | |
| 440 | strcpy(*str + (*used - 1), line); |
| 441 | *used += strlen(line); |
| 442 | |
| 443 | strcpy(*str + (*used - 1), "\n"); |
| 444 | *used += 1; |
| 445 | } while ((line = strtok_r(NULL, "\n", &ptr))); |
| 446 | |
| 447 | free(buf); |
| 448 | break; |
| 449 | |
| 450 | default: |
| 451 | LOGINT_RET(LYD_NODE_CTX(node)); |
| 452 | } |
| 453 | |
| 454 | if (fake_cont) { |
| 455 | rc = cast_string_realloc(LYD_NODE_CTX(node), 1, str, used, size); |
| 456 | LY_CHECK_RET(rc); |
| 457 | strcpy(*str + (*used - 1), "\n"); |
| 458 | ++(*used); |
| 459 | |
| 460 | --indent; |
| 461 | } |
| 462 | |
| 463 | return LY_SUCCESS; |
| 464 | } |
| 465 | |
| 466 | /** |
| 467 | * @brief Cast an element into a string. |
| 468 | * |
| 469 | * @param[in] node Node to cast. |
| 470 | * @param[in] fake_cont Whether to put the data into a "fake" container. |
| 471 | * @param[in] root_type Type of the XPath root. |
| 472 | * @param[out] str Element cast to dynamically-allocated string. |
| 473 | * @return LY_ERR |
| 474 | */ |
| 475 | static LY_ERR |
| 476 | cast_string_elem(struct lyd_node *node, int fake_cont, enum lyxp_node_type root_type, char **str) |
| 477 | { |
| 478 | uint16_t used, size; |
| 479 | LY_ERR rc; |
| 480 | |
| 481 | *str = malloc(LYXP_STRING_CAST_SIZE_START * sizeof(char)); |
| 482 | LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_NODE_CTX(node)), LY_EMEM); |
| 483 | (*str)[0] = '\0'; |
| 484 | used = 1; |
| 485 | size = LYXP_STRING_CAST_SIZE_START; |
| 486 | |
| 487 | rc = cast_string_recursive(node, fake_cont, root_type, 0, str, &used, &size); |
| 488 | if (rc != LY_SUCCESS) { |
| 489 | free(*str); |
| 490 | return rc; |
| 491 | } |
| 492 | |
| 493 | if (size > used) { |
| 494 | *str = ly_realloc(*str, used * sizeof(char)); |
| 495 | LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_NODE_CTX(node)), LY_EMEM); |
| 496 | } |
| 497 | return LY_SUCCESS; |
| 498 | } |
| 499 | |
| 500 | /** |
| 501 | * @brief Cast a LYXP_SET_NODE_SET set into a string. |
| 502 | * Context position aware. |
| 503 | * |
| 504 | * @param[in] set Set to cast. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 505 | * @param[out] str Cast dynamically-allocated string. |
| 506 | * @return LY_ERR |
| 507 | */ |
| 508 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 509 | cast_node_set_to_string(struct lyxp_set *set, char **str) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 510 | { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 511 | int dynamic; |
| 512 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 513 | switch (set->val.nodes[0].type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 514 | case LYXP_NODE_NONE: |
| 515 | /* invalid */ |
| 516 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 517 | case LYXP_NODE_ROOT: |
| 518 | case LYXP_NODE_ROOT_CONFIG: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 519 | 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] | 520 | case LYXP_NODE_ELEM: |
| 521 | case LYXP_NODE_TEXT: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 522 | 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] | 523 | case LYXP_NODE_META: |
| 524 | *str = (char *)lyd_meta2str(set->val.meta[0].meta, &dynamic); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 525 | if (!dynamic) { |
| 526 | *str = strdup(*str); |
| 527 | if (!*str) { |
| 528 | LOGMEM_RET(set->ctx); |
| 529 | } |
| 530 | } |
| 531 | return LY_SUCCESS; |
| 532 | } |
| 533 | |
| 534 | LOGINT_RET(set->ctx); |
| 535 | } |
| 536 | |
| 537 | /** |
| 538 | * @brief Cast a string into an XPath number. |
| 539 | * |
| 540 | * @param[in] str String to use. |
| 541 | * @return Cast number. |
| 542 | */ |
| 543 | static long double |
| 544 | cast_string_to_number(const char *str) |
| 545 | { |
| 546 | long double num; |
| 547 | char *ptr; |
| 548 | |
| 549 | errno = 0; |
| 550 | num = strtold(str, &ptr); |
| 551 | if (errno || *ptr) { |
| 552 | num = NAN; |
| 553 | } |
| 554 | return num; |
| 555 | } |
| 556 | |
| 557 | /** |
| 558 | * @brief Callback for checking value equality. |
| 559 | * |
| 560 | * @param[in] val1_p First value. |
| 561 | * @param[in] val2_p Second value. |
| 562 | * @param[in] mod Whether hash table is being modified. |
| 563 | * @param[in] cb_data Callback data. |
| 564 | * @return 0 if not equal, non-zero if equal. |
| 565 | */ |
| 566 | static int |
| 567 | set_values_equal_cb(void *val1_p, void *val2_p, int UNUSED(mod), void *UNUSED(cb_data)) |
| 568 | { |
| 569 | struct lyxp_set_hash_node *val1, *val2; |
| 570 | |
| 571 | val1 = (struct lyxp_set_hash_node *)val1_p; |
| 572 | val2 = (struct lyxp_set_hash_node *)val2_p; |
| 573 | |
| 574 | if ((val1->node == val2->node) && (val1->type == val2->type)) { |
| 575 | return 1; |
| 576 | } |
| 577 | |
| 578 | return 0; |
| 579 | } |
| 580 | |
| 581 | /** |
| 582 | * @brief Insert node and its hash into set. |
| 583 | * |
| 584 | * @param[in] set et to insert to. |
| 585 | * @param[in] node Node with hash. |
| 586 | * @param[in] type Node type. |
| 587 | */ |
| 588 | static void |
| 589 | set_insert_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type) |
| 590 | { |
| 591 | int r; |
| 592 | uint32_t i, hash; |
| 593 | struct lyxp_set_hash_node hnode; |
| 594 | |
| 595 | if (!set->ht && (set->used >= LYD_HT_MIN_ITEMS)) { |
| 596 | /* create hash table and add all the nodes */ |
| 597 | set->ht = lyht_new(1, sizeof(struct lyxp_set_hash_node), set_values_equal_cb, NULL, 1); |
| 598 | for (i = 0; i < set->used; ++i) { |
| 599 | hnode.node = set->val.nodes[i].node; |
| 600 | hnode.type = set->val.nodes[i].type; |
| 601 | |
| 602 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 603 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 604 | hash = dict_hash_multi(hash, NULL, 0); |
| 605 | |
| 606 | r = lyht_insert(set->ht, &hnode, hash, NULL); |
| 607 | assert(!r); |
| 608 | (void)r; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 609 | |
Michal Vasko | 9d6befd | 2019-12-11 14:56:56 +0100 | [diff] [blame] | 610 | if (hnode.node == node) { |
| 611 | /* it was just added, do not add it twice */ |
| 612 | node = NULL; |
| 613 | } |
| 614 | } |
| 615 | } |
| 616 | |
| 617 | if (set->ht && node) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 618 | /* add the new node into hash table */ |
| 619 | hnode.node = node; |
| 620 | hnode.type = type; |
| 621 | |
| 622 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 623 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 624 | hash = dict_hash_multi(hash, NULL, 0); |
| 625 | |
| 626 | r = lyht_insert(set->ht, &hnode, hash, NULL); |
| 627 | assert(!r); |
| 628 | (void)r; |
| 629 | } |
| 630 | } |
| 631 | |
| 632 | /** |
| 633 | * @brief Remove node and its hash from set. |
| 634 | * |
| 635 | * @param[in] set Set to remove from. |
| 636 | * @param[in] node Node to remove. |
| 637 | * @param[in] type Node type. |
| 638 | */ |
| 639 | static void |
| 640 | set_remove_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type) |
| 641 | { |
| 642 | int r; |
| 643 | struct lyxp_set_hash_node hnode; |
| 644 | uint32_t hash; |
| 645 | |
| 646 | if (set->ht) { |
| 647 | hnode.node = node; |
| 648 | hnode.type = type; |
| 649 | |
| 650 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 651 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 652 | hash = dict_hash_multi(hash, NULL, 0); |
| 653 | |
| 654 | r = lyht_remove(set->ht, &hnode, hash); |
| 655 | assert(!r); |
| 656 | (void)r; |
| 657 | |
| 658 | if (!set->ht->used) { |
| 659 | lyht_free(set->ht); |
| 660 | set->ht = NULL; |
| 661 | } |
| 662 | } |
| 663 | } |
| 664 | |
| 665 | /** |
| 666 | * @brief Check whether node is in set based on its hash. |
| 667 | * |
| 668 | * @param[in] set Set to search in. |
| 669 | * @param[in] node Node to search for. |
| 670 | * @param[in] type Node type. |
| 671 | * @param[in] skip_idx Index in @p set to skip. |
| 672 | * @return LY_ERR |
| 673 | */ |
| 674 | static LY_ERR |
| 675 | set_dup_node_hash_check(const struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type, int skip_idx) |
| 676 | { |
| 677 | struct lyxp_set_hash_node hnode, *match_p; |
| 678 | uint32_t hash; |
| 679 | |
| 680 | hnode.node = node; |
| 681 | hnode.type = type; |
| 682 | |
| 683 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 684 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 685 | hash = dict_hash_multi(hash, NULL, 0); |
| 686 | |
| 687 | if (!lyht_find(set->ht, &hnode, hash, (void **)&match_p)) { |
| 688 | if ((skip_idx > -1) && (set->val.nodes[skip_idx].node == match_p->node) && (set->val.nodes[skip_idx].type == match_p->type)) { |
| 689 | /* we found it on the index that should be skipped, find another */ |
| 690 | hnode = *match_p; |
| 691 | if (lyht_find_next(set->ht, &hnode, hash, (void **)&match_p)) { |
| 692 | /* none other found */ |
| 693 | return LY_SUCCESS; |
| 694 | } |
| 695 | } |
| 696 | |
| 697 | return LY_EEXIST; |
| 698 | } |
| 699 | |
| 700 | /* not found */ |
| 701 | return LY_SUCCESS; |
| 702 | } |
| 703 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 704 | void |
| 705 | lyxp_set_free_content(struct lyxp_set *set) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 706 | { |
| 707 | if (!set) { |
| 708 | return; |
| 709 | } |
| 710 | |
| 711 | if (set->type == LYXP_SET_NODE_SET) { |
| 712 | free(set->val.nodes); |
| 713 | lyht_free(set->ht); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 714 | } else if (set->type == LYXP_SET_SCNODE_SET) { |
| 715 | free(set->val.scnodes); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 716 | lyht_free(set->ht); |
| 717 | } else { |
| 718 | if (set->type == LYXP_SET_STRING) { |
| 719 | free(set->val.str); |
| 720 | } |
| 721 | set->type = LYXP_SET_NODE_SET; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 722 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 723 | |
| 724 | set->val.nodes = NULL; |
| 725 | set->used = 0; |
| 726 | set->size = 0; |
| 727 | set->ht = NULL; |
| 728 | set->ctx_pos = 0; |
| 729 | set->ctx_pos = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 730 | } |
| 731 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 732 | /** |
| 733 | * @brief Free dynamically-allocated set. |
| 734 | * |
| 735 | * @param[in] set Set to free. |
| 736 | */ |
| 737 | static void |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 738 | lyxp_set_free(struct lyxp_set *set) |
| 739 | { |
| 740 | if (!set) { |
| 741 | return; |
| 742 | } |
| 743 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 744 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 745 | free(set); |
| 746 | } |
| 747 | |
| 748 | /** |
| 749 | * @brief Initialize set context. |
| 750 | * |
| 751 | * @param[in] new Set to initialize. |
| 752 | * @param[in] set Arbitrary initialized set. |
| 753 | */ |
| 754 | static void |
| 755 | set_init(struct lyxp_set *new, struct lyxp_set *set) |
| 756 | { |
| 757 | memset(new, 0, sizeof *new); |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 758 | if (set) { |
| 759 | new->ctx = set->ctx; |
| 760 | new->ctx_node = set->ctx_node; |
Michal Vasko | 588112f | 2019-12-10 14:51:53 +0100 | [diff] [blame] | 761 | new->root_type = set->root_type; |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 762 | new->local_mod = set->local_mod; |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 763 | new->tree = set->tree; |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 764 | new->format = set->format; |
| 765 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 766 | } |
| 767 | |
| 768 | /** |
| 769 | * @brief Create a deep copy of a set. |
| 770 | * |
| 771 | * @param[in] set Set to copy. |
| 772 | * @return Copy of @p set. |
| 773 | */ |
| 774 | static struct lyxp_set * |
| 775 | set_copy(struct lyxp_set *set) |
| 776 | { |
| 777 | struct lyxp_set *ret; |
| 778 | uint16_t i; |
Michal Vasko | ba71654 | 2019-12-16 10:01:58 +0100 | [diff] [blame] | 779 | int idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 780 | |
| 781 | if (!set) { |
| 782 | return NULL; |
| 783 | } |
| 784 | |
| 785 | ret = malloc(sizeof *ret); |
| 786 | LY_CHECK_ERR_RET(!ret, LOGMEM(set->ctx), NULL); |
| 787 | set_init(ret, set); |
| 788 | |
| 789 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 790 | ret->type = set->type; |
| 791 | |
| 792 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | ba71654 | 2019-12-16 10:01:58 +0100 | [diff] [blame] | 793 | if ((set->val.scnodes[i].in_ctx == 1) || (set->val.scnodes[i].in_ctx == -2)) { |
| 794 | 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] | 795 | /* coverity seems to think scnodes can be NULL */ |
| 796 | if ((idx == -1) || !ret->val.scnodes) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 797 | lyxp_set_free(ret); |
| 798 | return NULL; |
| 799 | } |
Michal Vasko | ba71654 | 2019-12-16 10:01:58 +0100 | [diff] [blame] | 800 | ret->val.scnodes[idx].in_ctx = set->val.scnodes[i].in_ctx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 801 | } |
| 802 | } |
| 803 | } else if (set->type == LYXP_SET_NODE_SET) { |
| 804 | ret->type = set->type; |
| 805 | ret->val.nodes = malloc(set->used * sizeof *ret->val.nodes); |
| 806 | LY_CHECK_ERR_RET(!ret->val.nodes, LOGMEM(set->ctx); free(ret), NULL); |
| 807 | memcpy(ret->val.nodes, set->val.nodes, set->used * sizeof *ret->val.nodes); |
| 808 | |
| 809 | ret->used = ret->size = set->used; |
| 810 | ret->ctx_pos = set->ctx_pos; |
| 811 | ret->ctx_size = set->ctx_size; |
| 812 | ret->ht = lyht_dup(set->ht); |
| 813 | } else { |
| 814 | memcpy(ret, set, sizeof *ret); |
| 815 | if (set->type == LYXP_SET_STRING) { |
| 816 | ret->val.str = strdup(set->val.str); |
| 817 | LY_CHECK_ERR_RET(!ret->val.str, LOGMEM(set->ctx); free(ret), NULL); |
| 818 | } |
| 819 | } |
| 820 | |
| 821 | return ret; |
| 822 | } |
| 823 | |
| 824 | /** |
| 825 | * @brief Fill XPath set with a string. Any current data are disposed of. |
| 826 | * |
| 827 | * @param[in] set Set to fill. |
| 828 | * @param[in] string String to fill into \p set. |
| 829 | * @param[in] str_len Length of \p string. 0 is a valid value! |
| 830 | */ |
| 831 | static void |
| 832 | set_fill_string(struct lyxp_set *set, const char *string, uint16_t str_len) |
| 833 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 834 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 835 | |
| 836 | set->type = LYXP_SET_STRING; |
| 837 | if ((str_len == 0) && (string[0] != '\0')) { |
| 838 | string = ""; |
| 839 | } |
| 840 | set->val.str = strndup(string, str_len); |
| 841 | } |
| 842 | |
| 843 | /** |
| 844 | * @brief Fill XPath set with a number. Any current data are disposed of. |
| 845 | * |
| 846 | * @param[in] set Set to fill. |
| 847 | * @param[in] number Number to fill into \p set. |
| 848 | */ |
| 849 | static void |
| 850 | set_fill_number(struct lyxp_set *set, long double number) |
| 851 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 852 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 853 | |
| 854 | set->type = LYXP_SET_NUMBER; |
| 855 | set->val.num = number; |
| 856 | } |
| 857 | |
| 858 | /** |
| 859 | * @brief Fill XPath set with a boolean. Any current data are disposed of. |
| 860 | * |
| 861 | * @param[in] set Set to fill. |
| 862 | * @param[in] boolean Boolean to fill into \p set. |
| 863 | */ |
| 864 | static void |
| 865 | set_fill_boolean(struct lyxp_set *set, int boolean) |
| 866 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 867 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 868 | |
| 869 | set->type = LYXP_SET_BOOLEAN; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 870 | set->val.bln = boolean; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 871 | } |
| 872 | |
| 873 | /** |
| 874 | * @brief Fill XPath set with the value from another set (deep assign). |
| 875 | * Any current data are disposed of. |
| 876 | * |
| 877 | * @param[in] trg Set to fill. |
| 878 | * @param[in] src Source set to copy into \p trg. |
| 879 | */ |
| 880 | static void |
| 881 | set_fill_set(struct lyxp_set *trg, struct lyxp_set *src) |
| 882 | { |
| 883 | if (!trg || !src) { |
| 884 | return; |
| 885 | } |
| 886 | |
| 887 | if (trg->type == LYXP_SET_NODE_SET) { |
| 888 | free(trg->val.nodes); |
| 889 | } else if (trg->type == LYXP_SET_STRING) { |
| 890 | free(trg->val.str); |
| 891 | } |
| 892 | set_init(trg, src); |
| 893 | |
| 894 | if (src->type == LYXP_SET_SCNODE_SET) { |
| 895 | trg->type = LYXP_SET_SCNODE_SET; |
| 896 | trg->used = src->used; |
| 897 | trg->size = src->used; |
| 898 | |
| 899 | trg->val.scnodes = ly_realloc(trg->val.scnodes, trg->size * sizeof *trg->val.scnodes); |
| 900 | LY_CHECK_ERR_RET(!trg->val.scnodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), ); |
| 901 | memcpy(trg->val.scnodes, src->val.scnodes, src->used * sizeof *src->val.scnodes); |
| 902 | } else if (src->type == LYXP_SET_BOOLEAN) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 903 | set_fill_boolean(trg, src->val.bln); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 904 | } else if (src->type == LYXP_SET_NUMBER) { |
| 905 | set_fill_number(trg, src->val.num); |
| 906 | } else if (src->type == LYXP_SET_STRING) { |
| 907 | set_fill_string(trg, src->val.str, strlen(src->val.str)); |
| 908 | } else { |
| 909 | if (trg->type == LYXP_SET_NODE_SET) { |
| 910 | free(trg->val.nodes); |
| 911 | } else if (trg->type == LYXP_SET_STRING) { |
| 912 | free(trg->val.str); |
| 913 | } |
| 914 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 915 | assert(src->type == LYXP_SET_NODE_SET); |
| 916 | |
| 917 | trg->type = LYXP_SET_NODE_SET; |
| 918 | trg->used = src->used; |
| 919 | trg->size = src->used; |
| 920 | trg->ctx_pos = src->ctx_pos; |
| 921 | trg->ctx_size = src->ctx_size; |
| 922 | |
| 923 | trg->val.nodes = malloc(trg->used * sizeof *trg->val.nodes); |
| 924 | LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), ); |
| 925 | memcpy(trg->val.nodes, src->val.nodes, src->used * sizeof *src->val.nodes); |
| 926 | if (src->ht) { |
| 927 | trg->ht = lyht_dup(src->ht); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 928 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 929 | trg->ht = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 930 | } |
| 931 | } |
| 932 | } |
| 933 | |
| 934 | /** |
| 935 | * @brief Clear context of all schema nodes. |
| 936 | * |
| 937 | * @param[in] set Set to clear. |
| 938 | */ |
| 939 | static void |
| 940 | set_scnode_clear_ctx(struct lyxp_set *set) |
| 941 | { |
| 942 | uint32_t i; |
| 943 | |
| 944 | for (i = 0; i < set->used; ++i) { |
| 945 | if (set->val.scnodes[i].in_ctx == 1) { |
| 946 | set->val.scnodes[i].in_ctx = 0; |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 947 | } else if (set->val.scnodes[i].in_ctx == -2) { |
| 948 | set->val.scnodes[i].in_ctx = -1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 949 | } |
| 950 | } |
| 951 | } |
| 952 | |
| 953 | /** |
| 954 | * @brief Remove a node from a set. Removing last node changes |
| 955 | * set into LYXP_SET_EMPTY. Context position aware. |
| 956 | * |
| 957 | * @param[in] set Set to use. |
| 958 | * @param[in] idx Index from @p set of the node to be removed. |
| 959 | */ |
| 960 | static void |
| 961 | set_remove_node(struct lyxp_set *set, uint32_t idx) |
| 962 | { |
| 963 | assert(set && (set->type == LYXP_SET_NODE_SET)); |
| 964 | assert(idx < set->used); |
| 965 | |
| 966 | set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
| 967 | |
| 968 | --set->used; |
| 969 | if (set->used) { |
| 970 | memmove(&set->val.nodes[idx], &set->val.nodes[idx + 1], |
| 971 | (set->used - idx) * sizeof *set->val.nodes); |
| 972 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 973 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 974 | } |
| 975 | } |
| 976 | |
| 977 | /** |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 978 | * @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] | 979 | * |
| 980 | * @param[in] set Set to use. |
| 981 | * @param[in] idx Index from @p set of the node to be removed. |
| 982 | */ |
| 983 | static void |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 984 | set_remove_node_none(struct lyxp_set *set, uint32_t idx) |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 985 | { |
| 986 | assert(set && (set->type == LYXP_SET_NODE_SET)); |
| 987 | assert(idx < set->used); |
| 988 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 989 | if (set->val.nodes[idx].type == LYXP_NODE_ELEM) { |
| 990 | set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
| 991 | } |
| 992 | set->val.nodes[idx].type = LYXP_NODE_NONE; |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 993 | } |
| 994 | |
| 995 | /** |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 996 | * @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] | 997 | * set into LYXP_SET_EMPTY. Context position aware. |
| 998 | * |
| 999 | * @param[in] set Set to consolidate. |
| 1000 | */ |
| 1001 | static void |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1002 | set_remove_nodes_none(struct lyxp_set *set) |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1003 | { |
| 1004 | uint16_t i, orig_used, end; |
| 1005 | int32_t start; |
| 1006 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1007 | assert(set); |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1008 | |
| 1009 | orig_used = set->used; |
| 1010 | set->used = 0; |
| 1011 | for (i = 0; i < orig_used;) { |
| 1012 | start = -1; |
| 1013 | do { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1014 | if ((set->val.nodes[i].type != LYXP_NODE_NONE) && (start == -1)) { |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1015 | start = i; |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1016 | } else if ((start > -1) && (set->val.nodes[i].type == LYXP_NODE_NONE)) { |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1017 | end = i; |
| 1018 | ++i; |
| 1019 | break; |
| 1020 | } |
| 1021 | |
| 1022 | ++i; |
| 1023 | if (i == orig_used) { |
| 1024 | end = i; |
| 1025 | } |
| 1026 | } while (i < orig_used); |
| 1027 | |
| 1028 | if (start > -1) { |
| 1029 | /* move the whole chunk of valid nodes together */ |
| 1030 | if (set->used != (unsigned)start) { |
| 1031 | memmove(&set->val.nodes[set->used], &set->val.nodes[start], (end - start) * sizeof *set->val.nodes); |
| 1032 | } |
| 1033 | set->used += end - start; |
| 1034 | } |
| 1035 | } |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1036 | } |
| 1037 | |
| 1038 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1039 | * @brief Check for duplicates in a node set. |
| 1040 | * |
| 1041 | * @param[in] set Set to check. |
| 1042 | * @param[in] node Node to look for in @p set. |
| 1043 | * @param[in] node_type Type of @p node. |
| 1044 | * @param[in] skip_idx Index from @p set to skip. |
| 1045 | * @return LY_ERR |
| 1046 | */ |
| 1047 | static LY_ERR |
| 1048 | set_dup_node_check(const struct lyxp_set *set, const struct lyd_node *node, enum lyxp_node_type node_type, int skip_idx) |
| 1049 | { |
| 1050 | uint32_t i; |
| 1051 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1052 | if (set->ht && node) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1053 | return set_dup_node_hash_check(set, (struct lyd_node *)node, node_type, skip_idx); |
| 1054 | } |
| 1055 | |
| 1056 | for (i = 0; i < set->used; ++i) { |
| 1057 | if ((skip_idx > -1) && (i == (unsigned)skip_idx)) { |
| 1058 | continue; |
| 1059 | } |
| 1060 | |
| 1061 | if ((set->val.nodes[i].node == node) && (set->val.nodes[i].type == node_type)) { |
| 1062 | return LY_EEXIST; |
| 1063 | } |
| 1064 | } |
| 1065 | |
| 1066 | return LY_SUCCESS; |
| 1067 | } |
| 1068 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 1069 | int |
| 1070 | 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] | 1071 | { |
| 1072 | uint32_t i; |
| 1073 | |
| 1074 | for (i = 0; i < set->used; ++i) { |
| 1075 | if ((skip_idx > -1) && (i == (unsigned)skip_idx)) { |
| 1076 | continue; |
| 1077 | } |
| 1078 | |
| 1079 | if ((set->val.scnodes[i].scnode == node) && (set->val.scnodes[i].type == node_type)) { |
| 1080 | return i; |
| 1081 | } |
| 1082 | } |
| 1083 | |
| 1084 | return -1; |
| 1085 | } |
| 1086 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 1087 | void |
| 1088 | lyxp_set_scnode_merge(struct lyxp_set *set1, struct lyxp_set *set2) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1089 | { |
| 1090 | uint32_t orig_used, i, j; |
| 1091 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1092 | 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] | 1093 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1094 | if (!set2->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1095 | return; |
| 1096 | } |
| 1097 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1098 | if (!set1->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1099 | memcpy(set1, set2, sizeof *set1); |
| 1100 | return; |
| 1101 | } |
| 1102 | |
| 1103 | if (set1->used + set2->used > set1->size) { |
| 1104 | set1->size = set1->used + set2->used; |
| 1105 | set1->val.scnodes = ly_realloc(set1->val.scnodes, set1->size * sizeof *set1->val.scnodes); |
| 1106 | LY_CHECK_ERR_RET(!set1->val.scnodes, LOGMEM(set1->ctx), ); |
| 1107 | } |
| 1108 | |
| 1109 | orig_used = set1->used; |
| 1110 | |
| 1111 | for (i = 0; i < set2->used; ++i) { |
| 1112 | for (j = 0; j < orig_used; ++j) { |
| 1113 | /* detect duplicities */ |
| 1114 | if (set1->val.scnodes[j].scnode == set2->val.scnodes[i].scnode) { |
| 1115 | break; |
| 1116 | } |
| 1117 | } |
| 1118 | |
| 1119 | if (j == orig_used) { |
| 1120 | memcpy(&set1->val.scnodes[set1->used], &set2->val.scnodes[i], sizeof *set2->val.scnodes); |
| 1121 | ++set1->used; |
| 1122 | } |
| 1123 | } |
| 1124 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1125 | lyxp_set_free_content(set2); |
| 1126 | set2->type = LYXP_SET_SCNODE_SET; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1127 | } |
| 1128 | |
| 1129 | /** |
| 1130 | * @brief Insert a node into a set. Context position aware. |
| 1131 | * |
| 1132 | * @param[in] set Set to use. |
| 1133 | * @param[in] node Node to insert to @p set. |
| 1134 | * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting. |
| 1135 | * @param[in] node_type Node type of @p node. |
| 1136 | * @param[in] idx Index in @p set to insert into. |
| 1137 | */ |
| 1138 | static void |
| 1139 | set_insert_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx) |
| 1140 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1141 | assert(set && (set->type == LYXP_SET_NODE_SET)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1142 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1143 | if (!set->size) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1144 | /* first item */ |
| 1145 | if (idx) { |
| 1146 | /* no real harm done, but it is a bug */ |
| 1147 | LOGINT(set->ctx); |
| 1148 | idx = 0; |
| 1149 | } |
| 1150 | set->val.nodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.nodes); |
| 1151 | LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), ); |
| 1152 | set->type = LYXP_SET_NODE_SET; |
| 1153 | set->used = 0; |
| 1154 | set->size = LYXP_SET_SIZE_START; |
| 1155 | set->ctx_pos = 1; |
| 1156 | set->ctx_size = 1; |
| 1157 | set->ht = NULL; |
| 1158 | } else { |
| 1159 | /* not an empty set */ |
| 1160 | if (set->used == set->size) { |
| 1161 | |
| 1162 | /* set is full */ |
| 1163 | set->val.nodes = ly_realloc(set->val.nodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.nodes); |
| 1164 | LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), ); |
| 1165 | set->size += LYXP_SET_SIZE_STEP; |
| 1166 | } |
| 1167 | |
| 1168 | if (idx > set->used) { |
| 1169 | LOGINT(set->ctx); |
| 1170 | idx = set->used; |
| 1171 | } |
| 1172 | |
| 1173 | /* make space for the new node */ |
| 1174 | if (idx < set->used) { |
| 1175 | memmove(&set->val.nodes[idx + 1], &set->val.nodes[idx], (set->used - idx) * sizeof *set->val.nodes); |
| 1176 | } |
| 1177 | } |
| 1178 | |
| 1179 | /* finally assign the value */ |
| 1180 | set->val.nodes[idx].node = (struct lyd_node *)node; |
| 1181 | set->val.nodes[idx].type = node_type; |
| 1182 | set->val.nodes[idx].pos = pos; |
| 1183 | ++set->used; |
| 1184 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1185 | if (set->val.nodes[idx].type == LYXP_NODE_ELEM) { |
| 1186 | set_insert_node_hash(set, (struct lyd_node *)node, node_type); |
| 1187 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1188 | } |
| 1189 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 1190 | int |
| 1191 | 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] | 1192 | { |
| 1193 | int ret; |
| 1194 | |
| 1195 | assert(set->type == LYXP_SET_SCNODE_SET); |
| 1196 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 1197 | ret = lyxp_set_scnode_dup_node_check(set, node, node_type, -1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1198 | if (ret > -1) { |
| 1199 | set->val.scnodes[ret].in_ctx = 1; |
| 1200 | } else { |
| 1201 | if (set->used == set->size) { |
| 1202 | set->val.scnodes = ly_realloc(set->val.scnodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.scnodes); |
| 1203 | LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), -1); |
| 1204 | set->size += LYXP_SET_SIZE_STEP; |
| 1205 | } |
| 1206 | |
| 1207 | ret = set->used; |
| 1208 | set->val.scnodes[ret].scnode = (struct lysc_node *)node; |
| 1209 | set->val.scnodes[ret].type = node_type; |
| 1210 | set->val.scnodes[ret].in_ctx = 1; |
| 1211 | ++set->used; |
| 1212 | } |
| 1213 | |
| 1214 | return ret; |
| 1215 | } |
| 1216 | |
| 1217 | /** |
| 1218 | * @brief Replace a node in a set with another. Context position aware. |
| 1219 | * |
| 1220 | * @param[in] set Set to use. |
| 1221 | * @param[in] node Node to insert to @p set. |
| 1222 | * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting. |
| 1223 | * @param[in] node_type Node type of @p node. |
| 1224 | * @param[in] idx Index in @p set of the node to replace. |
| 1225 | */ |
| 1226 | static void |
| 1227 | set_replace_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx) |
| 1228 | { |
| 1229 | assert(set && (idx < set->used)); |
| 1230 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1231 | if (set->val.nodes[idx].type == LYXP_NODE_ELEM) { |
| 1232 | set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
| 1233 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1234 | set->val.nodes[idx].node = (struct lyd_node *)node; |
| 1235 | set->val.nodes[idx].type = node_type; |
| 1236 | set->val.nodes[idx].pos = pos; |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1237 | if (set->val.nodes[idx].type == LYXP_NODE_ELEM) { |
| 1238 | set_insert_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
| 1239 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1240 | } |
| 1241 | |
| 1242 | /** |
| 1243 | * @brief Set all nodes with ctx 1 to a new unique context value. |
| 1244 | * |
| 1245 | * @param[in] set Set to modify. |
| 1246 | * @return New context value. |
| 1247 | */ |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 1248 | static int32_t |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1249 | set_scnode_new_in_ctx(struct lyxp_set *set) |
| 1250 | { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 1251 | uint32_t i; |
| 1252 | int32_t ret_ctx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1253 | |
| 1254 | assert(set->type == LYXP_SET_SCNODE_SET); |
| 1255 | |
| 1256 | ret_ctx = 3; |
| 1257 | retry: |
| 1258 | for (i = 0; i < set->used; ++i) { |
| 1259 | if (set->val.scnodes[i].in_ctx >= ret_ctx) { |
| 1260 | ret_ctx = set->val.scnodes[i].in_ctx + 1; |
| 1261 | goto retry; |
| 1262 | } |
| 1263 | } |
| 1264 | for (i = 0; i < set->used; ++i) { |
| 1265 | if (set->val.scnodes[i].in_ctx == 1) { |
| 1266 | set->val.scnodes[i].in_ctx = ret_ctx; |
| 1267 | } |
| 1268 | } |
| 1269 | |
| 1270 | return ret_ctx; |
| 1271 | } |
| 1272 | |
| 1273 | /** |
| 1274 | * @brief Get unique @p node position in the data. |
| 1275 | * |
| 1276 | * @param[in] node Node to find. |
| 1277 | * @param[in] node_type Node type of @p node. |
| 1278 | * @param[in] root Root node. |
| 1279 | * @param[in] root_type Type of the XPath @p root node. |
| 1280 | * @param[in] prev Node that we think is before @p node in DFS from @p root. Can optionally |
| 1281 | * be used to increase efficiency and start the DFS from this node. |
| 1282 | * @param[in] prev_pos Node @p prev position. Optional, but must be set if @p prev is set. |
| 1283 | * @return Node position. |
| 1284 | */ |
| 1285 | static uint32_t |
| 1286 | get_node_pos(const struct lyd_node *node, enum lyxp_node_type node_type, const struct lyd_node *root, |
| 1287 | enum lyxp_node_type root_type, const struct lyd_node **prev, uint32_t *prev_pos) |
| 1288 | { |
| 1289 | const struct lyd_node *next, *elem, *top_sibling; |
| 1290 | uint32_t pos = 1; |
| 1291 | |
| 1292 | assert(prev && prev_pos && !root->prev->next); |
| 1293 | |
| 1294 | if ((node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ROOT_CONFIG)) { |
| 1295 | return 0; |
| 1296 | } |
| 1297 | |
| 1298 | if (*prev) { |
| 1299 | /* start from the previous element instead from the root */ |
| 1300 | elem = next = *prev; |
| 1301 | pos = *prev_pos; |
| 1302 | for (top_sibling = elem; top_sibling->parent; top_sibling = (struct lyd_node *)top_sibling->parent); |
| 1303 | goto dfs_search; |
| 1304 | } |
| 1305 | |
| 1306 | for (top_sibling = root; top_sibling; top_sibling = top_sibling->next) { |
| 1307 | /* TREE DFS */ |
| 1308 | LYD_TREE_DFS_BEGIN(top_sibling, next, elem) { |
| 1309 | dfs_search: |
| 1310 | if ((root_type == LYXP_NODE_ROOT_CONFIG) && (elem->schema->flags & LYS_CONFIG_R)) { |
| 1311 | goto skip_children; |
| 1312 | } |
| 1313 | |
| 1314 | if (elem == node) { |
| 1315 | break; |
| 1316 | } |
| 1317 | ++pos; |
| 1318 | |
| 1319 | /* TREE DFS END */ |
| 1320 | /* select element for the next run - children first, |
| 1321 | * child exception for lyd_node_leaf and lyd_node_leaflist, but not the root */ |
| 1322 | if (elem->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
| 1323 | next = NULL; |
| 1324 | } else { |
| 1325 | next = lyd_node_children(elem); |
| 1326 | } |
| 1327 | if (!next) { |
| 1328 | skip_children: |
| 1329 | /* no children */ |
| 1330 | if (elem == top_sibling) { |
| 1331 | /* we are done, root has no children */ |
| 1332 | elem = NULL; |
| 1333 | break; |
| 1334 | } |
| 1335 | /* try siblings */ |
| 1336 | next = elem->next; |
| 1337 | } |
| 1338 | while (!next) { |
| 1339 | /* no siblings, go back through parents */ |
| 1340 | if (elem->parent == top_sibling->parent) { |
| 1341 | /* we are done, no next element to process */ |
| 1342 | elem = NULL; |
| 1343 | break; |
| 1344 | } |
| 1345 | /* parent is already processed, go to its sibling */ |
| 1346 | elem = (struct lyd_node *)elem->parent; |
| 1347 | next = elem->next; |
| 1348 | } |
| 1349 | } |
| 1350 | |
| 1351 | /* node found */ |
| 1352 | if (elem) { |
| 1353 | break; |
| 1354 | } |
| 1355 | } |
| 1356 | |
| 1357 | if (!elem) { |
| 1358 | if (!(*prev)) { |
| 1359 | /* we went from root and failed to find it, cannot be */ |
| 1360 | LOGINT(node->schema->module->ctx); |
| 1361 | return 0; |
| 1362 | } else { |
| 1363 | *prev = NULL; |
| 1364 | *prev_pos = 0; |
| 1365 | |
| 1366 | elem = next = top_sibling = root; |
| 1367 | pos = 1; |
| 1368 | goto dfs_search; |
| 1369 | } |
| 1370 | } |
| 1371 | |
| 1372 | /* remember the last found node for next time */ |
| 1373 | *prev = node; |
| 1374 | *prev_pos = pos; |
| 1375 | |
| 1376 | return pos; |
| 1377 | } |
| 1378 | |
| 1379 | /** |
| 1380 | * @brief Assign (fill) missing node positions. |
| 1381 | * |
| 1382 | * @param[in] set Set to fill positions in. |
| 1383 | * @param[in] root Context root node. |
| 1384 | * @param[in] root_type Context root type. |
| 1385 | * @return LY_ERR |
| 1386 | */ |
| 1387 | static LY_ERR |
| 1388 | set_assign_pos(struct lyxp_set *set, const struct lyd_node *root, enum lyxp_node_type root_type) |
| 1389 | { |
| 1390 | const struct lyd_node *prev = NULL, *tmp_node; |
| 1391 | uint32_t i, tmp_pos = 0; |
| 1392 | |
| 1393 | for (i = 0; i < set->used; ++i) { |
| 1394 | if (!set->val.nodes[i].pos) { |
| 1395 | tmp_node = NULL; |
| 1396 | switch (set->val.nodes[i].type) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1397 | case LYXP_NODE_META: |
| 1398 | tmp_node = set->val.meta[i].meta->parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1399 | if (!tmp_node) { |
| 1400 | LOGINT_RET(root->schema->module->ctx); |
| 1401 | } |
| 1402 | /* fallthrough */ |
| 1403 | case LYXP_NODE_ELEM: |
| 1404 | case LYXP_NODE_TEXT: |
| 1405 | if (!tmp_node) { |
| 1406 | tmp_node = set->val.nodes[i].node; |
| 1407 | } |
| 1408 | set->val.nodes[i].pos = get_node_pos(tmp_node, set->val.nodes[i].type, root, root_type, &prev, &tmp_pos); |
| 1409 | break; |
| 1410 | default: |
| 1411 | /* all roots have position 0 */ |
| 1412 | break; |
| 1413 | } |
| 1414 | } |
| 1415 | } |
| 1416 | |
| 1417 | return LY_SUCCESS; |
| 1418 | } |
| 1419 | |
| 1420 | /** |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1421 | * @brief Get unique @p meta position in the parent metadata. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1422 | * |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1423 | * @param[in] meta Metadata to use. |
| 1424 | * @return Metadata position. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1425 | */ |
| 1426 | static uint16_t |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1427 | get_meta_pos(struct lyd_meta *meta) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1428 | { |
| 1429 | uint16_t pos = 0; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1430 | struct lyd_meta *meta2; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1431 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1432 | for (meta2 = meta->parent->meta; meta2 && (meta2 != meta); meta2 = meta2->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1433 | ++pos; |
| 1434 | } |
| 1435 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1436 | assert(meta2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1437 | return pos; |
| 1438 | } |
| 1439 | |
| 1440 | /** |
| 1441 | * @brief Compare 2 nodes in respect to XPath document order. |
| 1442 | * |
| 1443 | * @param[in] item1 1st node. |
| 1444 | * @param[in] item2 2nd node. |
| 1445 | * @return If 1st > 2nd returns 1, 1st == 2nd returns 0, and 1st < 2nd returns -1. |
| 1446 | */ |
| 1447 | static int |
| 1448 | set_sort_compare(struct lyxp_set_node *item1, struct lyxp_set_node *item2) |
| 1449 | { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1450 | uint32_t meta_pos1 = 0, meta_pos2 = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1451 | |
| 1452 | if (item1->pos < item2->pos) { |
| 1453 | return -1; |
| 1454 | } |
| 1455 | |
| 1456 | if (item1->pos > item2->pos) { |
| 1457 | return 1; |
| 1458 | } |
| 1459 | |
| 1460 | /* node positions are equal, the fun case */ |
| 1461 | |
| 1462 | /* 1st ELEM - == - 2nd TEXT, 1st TEXT - == - 2nd ELEM */ |
| 1463 | /* special case since text nodes are actually saved as their parents */ |
| 1464 | if ((item1->node == item2->node) && (item1->type != item2->type)) { |
| 1465 | if (item1->type == LYXP_NODE_ELEM) { |
| 1466 | assert(item2->type == LYXP_NODE_TEXT); |
| 1467 | return -1; |
| 1468 | } else { |
| 1469 | assert((item1->type == LYXP_NODE_TEXT) && (item2->type == LYXP_NODE_ELEM)); |
| 1470 | return 1; |
| 1471 | } |
| 1472 | } |
| 1473 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1474 | /* we need meta positions now */ |
| 1475 | if (item1->type == LYXP_NODE_META) { |
| 1476 | meta_pos1 = get_meta_pos((struct lyd_meta *)item1->node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1477 | } |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1478 | if (item2->type == LYXP_NODE_META) { |
| 1479 | meta_pos2 = get_meta_pos((struct lyd_meta *)item2->node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1480 | } |
| 1481 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1482 | /* 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] | 1483 | /* check for duplicates */ |
| 1484 | if (item1->node == item2->node) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1485 | 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] | 1486 | return 0; |
| 1487 | } |
| 1488 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1489 | /* 1st ELEM - 2nd TEXT, 1st ELEM - any pos - 2nd META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1490 | /* elem is always first, 2nd node is after it */ |
| 1491 | if (item1->type == LYXP_NODE_ELEM) { |
| 1492 | assert(item2->type != LYXP_NODE_ELEM); |
| 1493 | return -1; |
| 1494 | } |
| 1495 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1496 | /* 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] | 1497 | /* 2nd is before 1st */ |
| 1498 | if (((item1->type == LYXP_NODE_TEXT) |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1499 | && ((item2->type == LYXP_NODE_ELEM) || (item2->type == LYXP_NODE_META))) |
| 1500 | || ((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_ELEM)) |
| 1501 | || (((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_META)) |
| 1502 | && (meta_pos1 > meta_pos2))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1503 | return 1; |
| 1504 | } |
| 1505 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1506 | /* 1st META - any pos - 2nd TEXT, 1st META <pos< - 2nd META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1507 | /* 2nd is after 1st */ |
| 1508 | return -1; |
| 1509 | } |
| 1510 | |
| 1511 | /** |
| 1512 | * @brief Set cast for comparisons. |
| 1513 | * |
| 1514 | * @param[in] trg Target set to cast source into. |
| 1515 | * @param[in] src Source set. |
| 1516 | * @param[in] type Target set type. |
| 1517 | * @param[in] src_idx Source set node index. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1518 | * @return LY_ERR |
| 1519 | */ |
| 1520 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1521 | 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] | 1522 | { |
| 1523 | assert(src->type == LYXP_SET_NODE_SET); |
| 1524 | |
| 1525 | set_init(trg, src); |
| 1526 | |
| 1527 | /* insert node into target set */ |
| 1528 | set_insert_node(trg, src->val.nodes[src_idx].node, src->val.nodes[src_idx].pos, src->val.nodes[src_idx].type, 0); |
| 1529 | |
| 1530 | /* cast target set appropriately */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1531 | return lyxp_set_cast(trg, type); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1532 | } |
| 1533 | |
| 1534 | #ifndef NDEBUG |
| 1535 | |
| 1536 | /** |
| 1537 | * @brief Bubble sort @p set into XPath document order. |
| 1538 | * Context position aware. Unused in the 'Release' build target. |
| 1539 | * |
| 1540 | * @param[in] set Set to sort. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1541 | * @return How many times the whole set was traversed - 1 (if set was sorted, returns 0). |
| 1542 | */ |
| 1543 | static int |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1544 | set_sort(struct lyxp_set *set) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1545 | { |
| 1546 | uint32_t i, j; |
| 1547 | int ret = 0, cmp, inverted, change; |
| 1548 | const struct lyd_node *root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1549 | struct lyxp_set_node item; |
| 1550 | struct lyxp_set_hash_node hnode; |
| 1551 | uint64_t hash; |
| 1552 | |
Michal Vasko | 3cf8fbf | 2020-05-27 15:21:21 +0200 | [diff] [blame] | 1553 | if ((set->type != LYXP_SET_NODE_SET) || (set->used < 2)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1554 | return 0; |
| 1555 | } |
| 1556 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1557 | /* find first top-level node to be used as anchor for positions */ |
| 1558 | for (root = set->ctx_node; root->parent; root = (const struct lyd_node *)root->parent); |
| 1559 | for (; root->prev->next; root = root->prev); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1560 | |
| 1561 | /* fill positions */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1562 | if (set_assign_pos(set, root, set->root_type)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1563 | return -1; |
| 1564 | } |
| 1565 | |
| 1566 | LOGDBG(LY_LDGXPATH, "SORT BEGIN"); |
| 1567 | print_set_debug(set); |
| 1568 | |
| 1569 | for (i = 0; i < set->used; ++i) { |
| 1570 | inverted = 0; |
| 1571 | change = 0; |
| 1572 | |
| 1573 | for (j = 1; j < set->used - i; ++j) { |
| 1574 | /* compare node positions */ |
| 1575 | if (inverted) { |
| 1576 | cmp = set_sort_compare(&set->val.nodes[j], &set->val.nodes[j - 1]); |
| 1577 | } else { |
| 1578 | cmp = set_sort_compare(&set->val.nodes[j - 1], &set->val.nodes[j]); |
| 1579 | } |
| 1580 | |
| 1581 | /* swap if needed */ |
| 1582 | if ((inverted && (cmp < 0)) || (!inverted && (cmp > 0))) { |
| 1583 | change = 1; |
| 1584 | |
| 1585 | item = set->val.nodes[j - 1]; |
| 1586 | set->val.nodes[j - 1] = set->val.nodes[j]; |
| 1587 | set->val.nodes[j] = item; |
| 1588 | } else { |
| 1589 | /* whether node_pos1 should be smaller than node_pos2 or the other way around */ |
| 1590 | inverted = !inverted; |
| 1591 | } |
| 1592 | } |
| 1593 | |
| 1594 | ++ret; |
| 1595 | |
| 1596 | if (!change) { |
| 1597 | break; |
| 1598 | } |
| 1599 | } |
| 1600 | |
| 1601 | LOGDBG(LY_LDGXPATH, "SORT END %d", ret); |
| 1602 | print_set_debug(set); |
| 1603 | |
| 1604 | /* check node hashes */ |
| 1605 | if (set->used >= LYD_HT_MIN_ITEMS) { |
| 1606 | assert(set->ht); |
| 1607 | for (i = 0; i < set->used; ++i) { |
| 1608 | hnode.node = set->val.nodes[i].node; |
| 1609 | hnode.type = set->val.nodes[i].type; |
| 1610 | |
| 1611 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 1612 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 1613 | hash = dict_hash_multi(hash, NULL, 0); |
| 1614 | |
| 1615 | assert(!lyht_find(set->ht, &hnode, hash, NULL)); |
| 1616 | } |
| 1617 | } |
| 1618 | |
| 1619 | return ret - 1; |
| 1620 | } |
| 1621 | |
| 1622 | /** |
| 1623 | * @brief Remove duplicate entries in a sorted node set. |
| 1624 | * |
| 1625 | * @param[in] set Sorted set to check. |
| 1626 | * @return LY_ERR (LY_EEXIST if some duplicates are found) |
| 1627 | */ |
| 1628 | static LY_ERR |
| 1629 | set_sorted_dup_node_clean(struct lyxp_set *set) |
| 1630 | { |
| 1631 | uint32_t i = 0; |
| 1632 | LY_ERR ret = LY_SUCCESS; |
| 1633 | |
| 1634 | if (set->used > 1) { |
| 1635 | while (i < set->used - 1) { |
| 1636 | if ((set->val.nodes[i].node == set->val.nodes[i + 1].node) |
| 1637 | && (set->val.nodes[i].type == set->val.nodes[i + 1].type)) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1638 | set_remove_node_none(set, i + 1); |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1639 | ret = LY_EEXIST; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1640 | } |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1641 | ++i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1642 | } |
| 1643 | } |
| 1644 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1645 | set_remove_nodes_none(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1646 | return ret; |
| 1647 | } |
| 1648 | |
| 1649 | #endif |
| 1650 | |
| 1651 | /** |
| 1652 | * @brief Merge 2 sorted sets into one. |
| 1653 | * |
| 1654 | * @param[in,out] trg Set to merge into. Duplicates are removed. |
| 1655 | * @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] | 1656 | * @return LY_ERR |
| 1657 | */ |
| 1658 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1659 | set_sorted_merge(struct lyxp_set *trg, struct lyxp_set *src) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1660 | { |
| 1661 | uint32_t i, j, k, count, dup_count; |
| 1662 | int cmp; |
| 1663 | const struct lyd_node *root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1664 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1665 | 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] | 1666 | return LY_EINVAL; |
| 1667 | } |
| 1668 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1669 | if (!src->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1670 | return LY_SUCCESS; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1671 | } else if (!trg->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1672 | set_fill_set(trg, src); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1673 | lyxp_set_free_content(src); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1674 | return LY_SUCCESS; |
| 1675 | } |
| 1676 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1677 | /* find first top-level node to be used as anchor for positions */ |
| 1678 | for (root = trg->ctx_node; root->parent; root = (const struct lyd_node *)root->parent); |
| 1679 | for (; root->prev->next; root = root->prev); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1680 | |
| 1681 | /* fill positions */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1682 | 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] | 1683 | return LY_EINT; |
| 1684 | } |
| 1685 | |
| 1686 | #ifndef NDEBUG |
| 1687 | LOGDBG(LY_LDGXPATH, "MERGE target"); |
| 1688 | print_set_debug(trg); |
| 1689 | LOGDBG(LY_LDGXPATH, "MERGE source"); |
| 1690 | print_set_debug(src); |
| 1691 | #endif |
| 1692 | |
| 1693 | /* make memory for the merge (duplicates are not detected yet, so space |
| 1694 | * will likely be wasted on them, too bad) */ |
| 1695 | if (trg->size - trg->used < src->used) { |
| 1696 | trg->size = trg->used + src->used; |
| 1697 | |
| 1698 | trg->val.nodes = ly_realloc(trg->val.nodes, trg->size * sizeof *trg->val.nodes); |
| 1699 | LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx), LY_EMEM); |
| 1700 | } |
| 1701 | |
| 1702 | i = 0; |
| 1703 | j = 0; |
| 1704 | count = 0; |
| 1705 | dup_count = 0; |
| 1706 | do { |
| 1707 | cmp = set_sort_compare(&src->val.nodes[i], &trg->val.nodes[j]); |
| 1708 | if (!cmp) { |
| 1709 | if (!count) { |
| 1710 | /* duplicate, just skip it */ |
| 1711 | ++i; |
| 1712 | ++j; |
| 1713 | } else { |
| 1714 | /* we are copying something already, so let's copy the duplicate too, |
| 1715 | * we are hoping that afterwards there are some more nodes to |
| 1716 | * copy and this way we can copy them all together */ |
| 1717 | ++count; |
| 1718 | ++dup_count; |
| 1719 | ++i; |
| 1720 | ++j; |
| 1721 | } |
| 1722 | } else if (cmp < 0) { |
| 1723 | /* inserting src node into trg, just remember it for now */ |
| 1724 | ++count; |
| 1725 | ++i; |
| 1726 | |
| 1727 | /* insert the hash now */ |
| 1728 | set_insert_node_hash(trg, src->val.nodes[i - 1].node, src->val.nodes[i - 1].type); |
| 1729 | } else if (count) { |
| 1730 | copy_nodes: |
| 1731 | /* time to actually copy the nodes, we have found the largest block of nodes */ |
| 1732 | memmove(&trg->val.nodes[j + (count - dup_count)], |
| 1733 | &trg->val.nodes[j], |
| 1734 | (trg->used - j) * sizeof *trg->val.nodes); |
| 1735 | memcpy(&trg->val.nodes[j - dup_count], &src->val.nodes[i - count], count * sizeof *src->val.nodes); |
| 1736 | |
| 1737 | trg->used += count - dup_count; |
| 1738 | /* do not change i, except the copying above, we are basically doing exactly what is in the else branch below */ |
| 1739 | j += count - dup_count; |
| 1740 | |
| 1741 | count = 0; |
| 1742 | dup_count = 0; |
| 1743 | } else { |
| 1744 | ++j; |
| 1745 | } |
| 1746 | } while ((i < src->used) && (j < trg->used)); |
| 1747 | |
| 1748 | if ((i < src->used) || count) { |
| 1749 | /* insert all the hashes first */ |
| 1750 | for (k = i; k < src->used; ++k) { |
| 1751 | set_insert_node_hash(trg, src->val.nodes[k].node, src->val.nodes[k].type); |
| 1752 | } |
| 1753 | |
| 1754 | /* loop ended, but we need to copy something at trg end */ |
| 1755 | count += src->used - i; |
| 1756 | i = src->used; |
| 1757 | goto copy_nodes; |
| 1758 | } |
| 1759 | |
| 1760 | /* we are inserting hashes before the actual node insert, which causes |
| 1761 | * situations when there were initially not enough items for a hash table, |
| 1762 | * but even after some were inserted, hash table was not created (during |
| 1763 | * insertion the number of items is not updated yet) */ |
| 1764 | if (!trg->ht && (trg->used >= LYD_HT_MIN_ITEMS)) { |
| 1765 | set_insert_node_hash(trg, NULL, 0); |
| 1766 | } |
| 1767 | |
| 1768 | #ifndef NDEBUG |
| 1769 | LOGDBG(LY_LDGXPATH, "MERGE result"); |
| 1770 | print_set_debug(trg); |
| 1771 | #endif |
| 1772 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1773 | lyxp_set_free_content(src); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1774 | return LY_SUCCESS; |
| 1775 | } |
| 1776 | |
| 1777 | /* |
| 1778 | * (re)parse functions |
| 1779 | * |
| 1780 | * Parse functions parse the expression into |
| 1781 | * tokens (syntactic analysis). |
| 1782 | * |
| 1783 | * Reparse functions perform semantic analysis |
| 1784 | * (do not save the result, just a check) of |
| 1785 | * the expression and fill repeat indices. |
| 1786 | */ |
| 1787 | |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1788 | LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1789 | 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] | 1790 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1791 | if (exp->used == tok_idx) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1792 | if (ctx) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1793 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF); |
| 1794 | } |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1795 | return LY_EINCOMPLETE; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1796 | } |
| 1797 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1798 | if (want_tok && (exp->tokens[tok_idx] != want_tok)) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1799 | if (ctx) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1800 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1801 | 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] | 1802 | } |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1803 | return LY_ENOT; |
| 1804 | } |
| 1805 | |
| 1806 | return LY_SUCCESS; |
| 1807 | } |
| 1808 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1809 | LY_ERR |
| 1810 | lyxp_next_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok) |
| 1811 | { |
| 1812 | LY_CHECK_RET(lyxp_check_token(ctx, exp, *tok_idx, want_tok)); |
| 1813 | |
| 1814 | /* skip the token */ |
| 1815 | ++(*tok_idx); |
| 1816 | |
| 1817 | return LY_SUCCESS; |
| 1818 | } |
| 1819 | |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1820 | /* just like lyxp_check_token() but tests for 2 tokens */ |
| 1821 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1822 | 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] | 1823 | enum lyxp_token want_tok2) |
| 1824 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1825 | if (exp->used == tok_idx) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1826 | if (ctx) { |
| 1827 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF); |
| 1828 | } |
| 1829 | return LY_EINCOMPLETE; |
| 1830 | } |
| 1831 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1832 | 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] | 1833 | if (ctx) { |
| 1834 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1835 | 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] | 1836 | } |
| 1837 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1838 | } |
| 1839 | |
| 1840 | return LY_SUCCESS; |
| 1841 | } |
| 1842 | |
| 1843 | /** |
| 1844 | * @brief Stack operation push on the repeat array. |
| 1845 | * |
| 1846 | * @param[in] exp Expression to use. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1847 | * @param[in] tok_idx Position in the expresion \p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1848 | * @param[in] repeat_op_idx Index from \p exp of the operator token. This value is pushed. |
| 1849 | */ |
| 1850 | static void |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1851 | 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] | 1852 | { |
| 1853 | uint16_t i; |
| 1854 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1855 | if (exp->repeat[tok_idx]) { |
| 1856 | for (i = 0; exp->repeat[tok_idx][i]; ++i); |
| 1857 | exp->repeat[tok_idx] = realloc(exp->repeat[tok_idx], (i + 2) * sizeof *exp->repeat[tok_idx]); |
| 1858 | LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), ); |
| 1859 | exp->repeat[tok_idx][i] = repeat_op_idx; |
| 1860 | exp->repeat[tok_idx][i + 1] = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1861 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1862 | exp->repeat[tok_idx] = calloc(2, sizeof *exp->repeat[tok_idx]); |
| 1863 | LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), ); |
| 1864 | exp->repeat[tok_idx][0] = repeat_op_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1865 | } |
| 1866 | } |
| 1867 | |
| 1868 | /** |
| 1869 | * @brief Reparse Predicate. Logs directly on error. |
| 1870 | * |
| 1871 | * [7] Predicate ::= '[' Expr ']' |
| 1872 | * |
| 1873 | * @param[in] ctx Context for logging. |
| 1874 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1875 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1876 | * @return LY_ERR |
| 1877 | */ |
| 1878 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1879 | 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] | 1880 | { |
| 1881 | LY_ERR rc; |
| 1882 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1883 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1884 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1885 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1886 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1887 | rc = reparse_or_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1888 | LY_CHECK_RET(rc); |
| 1889 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1890 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1891 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1892 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1893 | |
| 1894 | return LY_SUCCESS; |
| 1895 | } |
| 1896 | |
| 1897 | /** |
| 1898 | * @brief Reparse RelativeLocationPath. Logs directly on error. |
| 1899 | * |
| 1900 | * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step |
| 1901 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 1902 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 1903 | * |
| 1904 | * @param[in] ctx Context for logging. |
| 1905 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1906 | * @param[in] tok_idx Position in the expression \p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1907 | * @return LY_ERR (LY_EINCOMPLETE on forward reference) |
| 1908 | */ |
| 1909 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1910 | 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] | 1911 | { |
| 1912 | LY_ERR rc; |
| 1913 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1914 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1915 | LY_CHECK_RET(rc); |
| 1916 | |
| 1917 | goto step; |
| 1918 | do { |
| 1919 | /* '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1920 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1921 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1922 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1923 | LY_CHECK_RET(rc); |
| 1924 | step: |
| 1925 | /* Step */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1926 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1927 | case LYXP_TOKEN_DOT: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1928 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1929 | break; |
| 1930 | |
| 1931 | case LYXP_TOKEN_DDOT: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1932 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1933 | break; |
| 1934 | |
| 1935 | case LYXP_TOKEN_AT: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1936 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1937 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1938 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1939 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1940 | 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] | 1941 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1942 | 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] | 1943 | return LY_EVALID; |
| 1944 | } |
| 1945 | /* fall through */ |
| 1946 | case LYXP_TOKEN_NAMETEST: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1947 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1948 | goto reparse_predicate; |
| 1949 | break; |
| 1950 | |
| 1951 | case LYXP_TOKEN_NODETYPE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1952 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1953 | |
| 1954 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1955 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1956 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1957 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1958 | |
| 1959 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1960 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1961 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1962 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1963 | |
| 1964 | reparse_predicate: |
| 1965 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1966 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 1967 | rc = reparse_predicate(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1968 | LY_CHECK_RET(rc); |
| 1969 | } |
| 1970 | break; |
| 1971 | default: |
| 1972 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1973 | 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] | 1974 | return LY_EVALID; |
| 1975 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1976 | } 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] | 1977 | |
| 1978 | return LY_SUCCESS; |
| 1979 | } |
| 1980 | |
| 1981 | /** |
| 1982 | * @brief Reparse AbsoluteLocationPath. Logs directly on error. |
| 1983 | * |
| 1984 | * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath |
| 1985 | * |
| 1986 | * @param[in] ctx Context for logging. |
| 1987 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1988 | * @param[in] tok_idx Position in the expression \p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1989 | * @return LY_ERR |
| 1990 | */ |
| 1991 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1992 | 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] | 1993 | { |
| 1994 | LY_ERR rc; |
| 1995 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1996 | 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] | 1997 | |
| 1998 | /* '/' RelativeLocationPath? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1999 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2000 | /* '/' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2001 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2002 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2003 | if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2004 | return LY_SUCCESS; |
| 2005 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2006 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2007 | case LYXP_TOKEN_DOT: |
| 2008 | case LYXP_TOKEN_DDOT: |
| 2009 | case LYXP_TOKEN_AT: |
| 2010 | case LYXP_TOKEN_NAMETEST: |
| 2011 | case LYXP_TOKEN_NODETYPE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2012 | rc = reparse_relative_location_path(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2013 | LY_CHECK_RET(rc); |
| 2014 | /* fall through */ |
| 2015 | default: |
| 2016 | break; |
| 2017 | } |
| 2018 | |
| 2019 | /* '//' RelativeLocationPath */ |
| 2020 | } else { |
| 2021 | /* '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2022 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2023 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2024 | rc = reparse_relative_location_path(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2025 | LY_CHECK_RET(rc); |
| 2026 | } |
| 2027 | |
| 2028 | return LY_SUCCESS; |
| 2029 | } |
| 2030 | |
| 2031 | /** |
| 2032 | * @brief Reparse FunctionCall. Logs directly on error. |
| 2033 | * |
| 2034 | * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')' |
| 2035 | * |
| 2036 | * @param[in] ctx Context for logging. |
| 2037 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2038 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2039 | * @return LY_ERR |
| 2040 | */ |
| 2041 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2042 | 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] | 2043 | { |
| 2044 | int min_arg_count = -1, max_arg_count, arg_count; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2045 | uint16_t func_tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2046 | LY_ERR rc; |
| 2047 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2048 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2049 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2050 | func_tok_idx = *tok_idx; |
| 2051 | switch (exp->tok_len[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2052 | case 3: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2053 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2054 | min_arg_count = 1; |
| 2055 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2056 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2057 | min_arg_count = 1; |
| 2058 | max_arg_count = 1; |
| 2059 | } |
| 2060 | break; |
| 2061 | case 4: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2062 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2063 | min_arg_count = 1; |
| 2064 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2065 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2066 | min_arg_count = 0; |
| 2067 | max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2068 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2069 | min_arg_count = 0; |
| 2070 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2071 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2072 | min_arg_count = 0; |
| 2073 | max_arg_count = 0; |
| 2074 | } |
| 2075 | break; |
| 2076 | case 5: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2077 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2078 | min_arg_count = 1; |
| 2079 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2080 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2081 | min_arg_count = 0; |
| 2082 | max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2083 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2084 | min_arg_count = 1; |
| 2085 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2086 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2087 | min_arg_count = 1; |
| 2088 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2089 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2090 | min_arg_count = 1; |
| 2091 | max_arg_count = 1; |
| 2092 | } |
| 2093 | break; |
| 2094 | case 6: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2095 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2096 | min_arg_count = 2; |
Michal Vasko | be2e356 | 2019-10-15 15:35:35 +0200 | [diff] [blame] | 2097 | max_arg_count = INT_MAX; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2098 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2099 | min_arg_count = 0; |
| 2100 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2101 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2102 | min_arg_count = 0; |
| 2103 | max_arg_count = 1; |
| 2104 | } |
| 2105 | break; |
| 2106 | case 7: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2107 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2108 | min_arg_count = 1; |
| 2109 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2110 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2111 | min_arg_count = 1; |
| 2112 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2113 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2114 | min_arg_count = 0; |
| 2115 | max_arg_count = 0; |
| 2116 | } |
| 2117 | break; |
| 2118 | case 8: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2119 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2120 | min_arg_count = 2; |
| 2121 | max_arg_count = 2; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2122 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2123 | min_arg_count = 0; |
| 2124 | max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2125 | } 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] | 2126 | min_arg_count = 2; |
| 2127 | max_arg_count = 2; |
| 2128 | } |
| 2129 | break; |
| 2130 | case 9: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2131 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2132 | min_arg_count = 2; |
| 2133 | max_arg_count = 3; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2134 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2135 | min_arg_count = 3; |
| 2136 | max_arg_count = 3; |
| 2137 | } |
| 2138 | break; |
| 2139 | case 10: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2140 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2141 | min_arg_count = 0; |
| 2142 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2143 | } 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] | 2144 | min_arg_count = 1; |
| 2145 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2146 | } 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] | 2147 | min_arg_count = 2; |
| 2148 | max_arg_count = 2; |
| 2149 | } |
| 2150 | break; |
| 2151 | case 11: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2152 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2153 | min_arg_count = 2; |
| 2154 | max_arg_count = 2; |
| 2155 | } |
| 2156 | break; |
| 2157 | case 12: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2158 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2159 | min_arg_count = 2; |
| 2160 | max_arg_count = 2; |
| 2161 | } |
| 2162 | break; |
| 2163 | case 13: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2164 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2165 | min_arg_count = 0; |
| 2166 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2167 | } 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] | 2168 | min_arg_count = 0; |
| 2169 | max_arg_count = 1; |
| 2170 | } |
| 2171 | break; |
| 2172 | case 15: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2173 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2174 | min_arg_count = 0; |
| 2175 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2176 | } 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] | 2177 | min_arg_count = 2; |
| 2178 | max_arg_count = 2; |
| 2179 | } |
| 2180 | break; |
| 2181 | case 16: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2182 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2183 | min_arg_count = 2; |
| 2184 | max_arg_count = 2; |
| 2185 | } |
| 2186 | break; |
| 2187 | case 20: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2188 | 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] | 2189 | min_arg_count = 2; |
| 2190 | max_arg_count = 2; |
| 2191 | } |
| 2192 | break; |
| 2193 | } |
| 2194 | if (min_arg_count == -1) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2195 | 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] | 2196 | return LY_EINVAL; |
| 2197 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2198 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2199 | |
| 2200 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2201 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2202 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2203 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2204 | |
| 2205 | /* ( Expr ( ',' Expr )* )? */ |
| 2206 | arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2207 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2208 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2209 | if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2210 | ++arg_count; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2211 | rc = reparse_or_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2212 | LY_CHECK_RET(rc); |
| 2213 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2214 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) { |
| 2215 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2216 | |
| 2217 | ++arg_count; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2218 | rc = reparse_or_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2219 | LY_CHECK_RET(rc); |
| 2220 | } |
| 2221 | |
| 2222 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2223 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2224 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2225 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2226 | |
| 2227 | if ((arg_count < min_arg_count) || (arg_count > max_arg_count)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2228 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INARGCOUNT, arg_count, exp->tok_len[func_tok_idx], |
| 2229 | &exp->expr[exp->tok_pos[func_tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2230 | return LY_EVALID; |
| 2231 | } |
| 2232 | |
| 2233 | return LY_SUCCESS; |
| 2234 | } |
| 2235 | |
| 2236 | /** |
| 2237 | * @brief Reparse PathExpr. Logs directly on error. |
| 2238 | * |
| 2239 | * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate* |
| 2240 | * | PrimaryExpr Predicate* '/' RelativeLocationPath |
| 2241 | * | PrimaryExpr Predicate* '//' RelativeLocationPath |
| 2242 | * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath |
| 2243 | * [8] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall |
| 2244 | * |
| 2245 | * @param[in] ctx Context for logging. |
| 2246 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2247 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2248 | * @return LY_ERR |
| 2249 | */ |
| 2250 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2251 | 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] | 2252 | { |
| 2253 | LY_ERR rc; |
| 2254 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2255 | if (lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 2256 | return LY_EVALID; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2257 | } |
| 2258 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2259 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2260 | case LYXP_TOKEN_PAR1: |
| 2261 | /* '(' Expr ')' Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2262 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2263 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2264 | rc = reparse_or_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2265 | LY_CHECK_RET(rc); |
| 2266 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2267 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2268 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2269 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2270 | goto predicate; |
| 2271 | break; |
| 2272 | case LYXP_TOKEN_DOT: |
| 2273 | case LYXP_TOKEN_DDOT: |
| 2274 | case LYXP_TOKEN_AT: |
| 2275 | case LYXP_TOKEN_NAMETEST: |
| 2276 | case LYXP_TOKEN_NODETYPE: |
| 2277 | /* RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2278 | rc = reparse_relative_location_path(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2279 | LY_CHECK_RET(rc); |
| 2280 | break; |
| 2281 | case LYXP_TOKEN_FUNCNAME: |
| 2282 | /* FunctionCall */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2283 | rc = reparse_function_call(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2284 | LY_CHECK_RET(rc); |
| 2285 | goto predicate; |
| 2286 | break; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2287 | case LYXP_TOKEN_OPER_PATH: |
| 2288 | case LYXP_TOKEN_OPER_RPATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2289 | /* AbsoluteLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2290 | rc = reparse_absolute_location_path(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2291 | LY_CHECK_RET(rc); |
| 2292 | break; |
| 2293 | case LYXP_TOKEN_LITERAL: |
| 2294 | /* Literal */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2295 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2296 | goto predicate; |
| 2297 | break; |
| 2298 | case LYXP_TOKEN_NUMBER: |
| 2299 | /* Number */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2300 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2301 | goto predicate; |
| 2302 | break; |
| 2303 | default: |
| 2304 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2305 | 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] | 2306 | return LY_EVALID; |
| 2307 | } |
| 2308 | |
| 2309 | return LY_SUCCESS; |
| 2310 | |
| 2311 | predicate: |
| 2312 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2313 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 2314 | rc = reparse_predicate(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2315 | LY_CHECK_RET(rc); |
| 2316 | } |
| 2317 | |
| 2318 | /* ('/' or '//') RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2319 | 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] | 2320 | |
| 2321 | /* '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2322 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2323 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2324 | rc = reparse_relative_location_path(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2325 | LY_CHECK_RET(rc); |
| 2326 | } |
| 2327 | |
| 2328 | return LY_SUCCESS; |
| 2329 | } |
| 2330 | |
| 2331 | /** |
| 2332 | * @brief Reparse UnaryExpr. Logs directly on error. |
| 2333 | * |
| 2334 | * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr |
| 2335 | * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr |
| 2336 | * |
| 2337 | * @param[in] ctx Context for logging. |
| 2338 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2339 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2340 | * @return LY_ERR |
| 2341 | */ |
| 2342 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2343 | reparse_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] | 2344 | { |
| 2345 | uint16_t prev_exp; |
| 2346 | LY_ERR rc; |
| 2347 | |
| 2348 | /* ('-')* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2349 | prev_exp = *tok_idx; |
| 2350 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) |
| 2351 | && (exp->expr[exp->tok_pos[*tok_idx]] == '-')) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2352 | exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2353 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2354 | } |
| 2355 | |
| 2356 | /* PathExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2357 | prev_exp = *tok_idx; |
| 2358 | rc = reparse_path_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2359 | LY_CHECK_RET(rc); |
| 2360 | |
| 2361 | /* ('|' PathExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2362 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_UNI)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2363 | exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2364 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2365 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2366 | rc = reparse_path_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2367 | LY_CHECK_RET(rc); |
| 2368 | } |
| 2369 | |
| 2370 | return LY_SUCCESS; |
| 2371 | } |
| 2372 | |
| 2373 | /** |
| 2374 | * @brief Reparse AdditiveExpr. Logs directly on error. |
| 2375 | * |
| 2376 | * [15] AdditiveExpr ::= MultiplicativeExpr |
| 2377 | * | AdditiveExpr '+' MultiplicativeExpr |
| 2378 | * | AdditiveExpr '-' MultiplicativeExpr |
| 2379 | * [16] MultiplicativeExpr ::= UnaryExpr |
| 2380 | * | MultiplicativeExpr '*' UnaryExpr |
| 2381 | * | MultiplicativeExpr 'div' UnaryExpr |
| 2382 | * | MultiplicativeExpr 'mod' UnaryExpr |
| 2383 | * |
| 2384 | * @param[in] ctx Context for logging. |
| 2385 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2386 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2387 | * @return LY_ERR |
| 2388 | */ |
| 2389 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2390 | 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] | 2391 | { |
| 2392 | uint16_t prev_add_exp, prev_mul_exp; |
| 2393 | LY_ERR rc; |
| 2394 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2395 | prev_add_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2396 | goto reparse_multiplicative_expr; |
| 2397 | |
| 2398 | /* ('+' / '-' MultiplicativeExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2399 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) |
| 2400 | && ((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] | 2401 | exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2402 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2403 | |
| 2404 | reparse_multiplicative_expr: |
| 2405 | /* UnaryExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2406 | prev_mul_exp = *tok_idx; |
| 2407 | rc = reparse_unary_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2408 | LY_CHECK_RET(rc); |
| 2409 | |
| 2410 | /* ('*' / 'div' / 'mod' UnaryExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2411 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) |
| 2412 | && ((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] | 2413 | exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2414 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2415 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2416 | rc = reparse_unary_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2417 | LY_CHECK_RET(rc); |
| 2418 | } |
| 2419 | } |
| 2420 | |
| 2421 | return LY_SUCCESS; |
| 2422 | } |
| 2423 | |
| 2424 | /** |
| 2425 | * @brief Reparse EqualityExpr. Logs directly on error. |
| 2426 | * |
| 2427 | * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr |
| 2428 | * | EqualityExpr '!=' RelationalExpr |
| 2429 | * [14] RelationalExpr ::= AdditiveExpr |
| 2430 | * | RelationalExpr '<' AdditiveExpr |
| 2431 | * | RelationalExpr '>' AdditiveExpr |
| 2432 | * | RelationalExpr '<=' AdditiveExpr |
| 2433 | * | RelationalExpr '>=' AdditiveExpr |
| 2434 | * |
| 2435 | * @param[in] ctx Context for logging. |
| 2436 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2437 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2438 | * @return LY_ERR |
| 2439 | */ |
| 2440 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2441 | 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] | 2442 | { |
| 2443 | uint16_t prev_eq_exp, prev_rel_exp; |
| 2444 | LY_ERR rc; |
| 2445 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2446 | prev_eq_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2447 | goto reparse_additive_expr; |
| 2448 | |
| 2449 | /* ('=' / '!=' RelationalExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2450 | 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] | 2451 | exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2452 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2453 | |
| 2454 | reparse_additive_expr: |
| 2455 | /* AdditiveExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2456 | prev_rel_exp = *tok_idx; |
| 2457 | rc = reparse_additive_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2458 | LY_CHECK_RET(rc); |
| 2459 | |
| 2460 | /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2461 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_COMP)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2462 | exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2463 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2464 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2465 | rc = reparse_additive_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2466 | LY_CHECK_RET(rc); |
| 2467 | } |
| 2468 | } |
| 2469 | |
| 2470 | return LY_SUCCESS; |
| 2471 | } |
| 2472 | |
| 2473 | /** |
| 2474 | * @brief Reparse OrExpr. Logs directly on error. |
| 2475 | * |
| 2476 | * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr |
| 2477 | * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr |
| 2478 | * |
| 2479 | * @param[in] ctx Context for logging. |
| 2480 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2481 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2482 | * @return LY_ERR |
| 2483 | */ |
| 2484 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2485 | 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] | 2486 | { |
| 2487 | uint16_t prev_or_exp, prev_and_exp; |
| 2488 | LY_ERR rc; |
| 2489 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2490 | prev_or_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2491 | goto reparse_equality_expr; |
| 2492 | |
| 2493 | /* ('or' AndExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2494 | 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] | 2495 | exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2496 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2497 | |
| 2498 | reparse_equality_expr: |
| 2499 | /* EqualityExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2500 | prev_and_exp = *tok_idx; |
| 2501 | rc = reparse_equality_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2502 | LY_CHECK_RET(rc); |
| 2503 | |
| 2504 | /* ('and' EqualityExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2505 | 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] | 2506 | exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2507 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2508 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2509 | rc = reparse_equality_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2510 | LY_CHECK_RET(rc); |
| 2511 | } |
| 2512 | } |
| 2513 | |
| 2514 | return LY_SUCCESS; |
| 2515 | } |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2516 | |
| 2517 | /** |
| 2518 | * @brief Parse NCName. |
| 2519 | * |
| 2520 | * @param[in] ncname Name to parse. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2521 | * @return Length of @p ncname valid bytes. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2522 | */ |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2523 | static long int |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2524 | parse_ncname(const char *ncname) |
| 2525 | { |
| 2526 | unsigned int uc; |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2527 | size_t size; |
| 2528 | long int len = 0; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2529 | |
| 2530 | LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0); |
| 2531 | if (!is_xmlqnamestartchar(uc) || (uc == ':')) { |
| 2532 | return len; |
| 2533 | } |
| 2534 | |
| 2535 | do { |
| 2536 | len += size; |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 2537 | if (!*ncname) { |
| 2538 | break; |
| 2539 | } |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2540 | LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2541 | } while (is_xmlqnamechar(uc) && (uc != ':')); |
| 2542 | |
| 2543 | return len; |
| 2544 | } |
| 2545 | |
| 2546 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2547 | * @brief Add @p token into the expression @p exp. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2548 | * |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2549 | * @param[in] ctx Context for logging. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2550 | * @param[in] exp Expression to use. |
| 2551 | * @param[in] token Token to add. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2552 | * @param[in] tok_pos Token position in the XPath expression. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2553 | * @param[in] tok_len Token length in the XPath expression. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2554 | * @return LY_ERR |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2555 | */ |
| 2556 | static LY_ERR |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 2557 | 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] | 2558 | { |
| 2559 | uint32_t prev; |
| 2560 | |
| 2561 | if (exp->used == exp->size) { |
| 2562 | prev = exp->size; |
| 2563 | exp->size += LYXP_EXPR_SIZE_STEP; |
| 2564 | if (prev > exp->size) { |
| 2565 | LOGINT(ctx); |
| 2566 | return LY_EINT; |
| 2567 | } |
| 2568 | |
| 2569 | exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens); |
| 2570 | LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM); |
| 2571 | exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos); |
| 2572 | LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM); |
| 2573 | exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len); |
| 2574 | LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM); |
| 2575 | } |
| 2576 | |
| 2577 | exp->tokens[exp->used] = token; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2578 | exp->tok_pos[exp->used] = tok_pos; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2579 | exp->tok_len[exp->used] = tok_len; |
| 2580 | ++exp->used; |
| 2581 | return LY_SUCCESS; |
| 2582 | } |
| 2583 | |
| 2584 | void |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 2585 | lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr) |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2586 | { |
| 2587 | uint16_t i; |
| 2588 | |
| 2589 | if (!expr) { |
| 2590 | return; |
| 2591 | } |
| 2592 | |
| 2593 | lydict_remove(ctx, expr->expr); |
| 2594 | free(expr->tokens); |
| 2595 | free(expr->tok_pos); |
| 2596 | free(expr->tok_len); |
| 2597 | if (expr->repeat) { |
| 2598 | for (i = 0; i < expr->used; ++i) { |
| 2599 | free(expr->repeat[i]); |
| 2600 | } |
| 2601 | } |
| 2602 | free(expr->repeat); |
| 2603 | free(expr); |
| 2604 | } |
| 2605 | |
| 2606 | struct lyxp_expr * |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2607 | 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] | 2608 | { |
| 2609 | struct lyxp_expr *ret; |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2610 | size_t parsed = 0, tok_len; |
| 2611 | long int ncname_len; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2612 | enum lyxp_token tok_type; |
| 2613 | int prev_function_check = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2614 | uint16_t tok_idx = 0; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2615 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2616 | if (!expr[0]) { |
| 2617 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF); |
| 2618 | return NULL; |
| 2619 | } |
| 2620 | |
| 2621 | if (!expr_len) { |
| 2622 | expr_len = strlen(expr); |
| 2623 | } |
| 2624 | if (expr_len > UINT16_MAX) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2625 | LOGERR(ctx, LY_EINVAL, "XPath expression cannot be longer than %ud characters.", UINT16_MAX); |
| 2626 | return NULL; |
| 2627 | } |
| 2628 | |
| 2629 | /* init lyxp_expr structure */ |
| 2630 | ret = calloc(1, sizeof *ret); |
| 2631 | LY_CHECK_ERR_GOTO(!ret, LOGMEM(ctx), error); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2632 | ret->expr = lydict_insert(ctx, expr, expr_len); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2633 | LY_CHECK_ERR_GOTO(!ret->expr, LOGMEM(ctx), error); |
| 2634 | ret->used = 0; |
| 2635 | ret->size = LYXP_EXPR_SIZE_START; |
| 2636 | ret->tokens = malloc(ret->size * sizeof *ret->tokens); |
| 2637 | LY_CHECK_ERR_GOTO(!ret->tokens, LOGMEM(ctx), error); |
| 2638 | |
| 2639 | ret->tok_pos = malloc(ret->size * sizeof *ret->tok_pos); |
| 2640 | LY_CHECK_ERR_GOTO(!ret->tok_pos, LOGMEM(ctx), error); |
| 2641 | |
| 2642 | ret->tok_len = malloc(ret->size * sizeof *ret->tok_len); |
| 2643 | LY_CHECK_ERR_GOTO(!ret->tok_len, LOGMEM(ctx), error); |
| 2644 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2645 | /* make expr 0-terminated */ |
| 2646 | expr = ret->expr; |
| 2647 | |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2648 | while (is_xmlws(expr[parsed])) { |
| 2649 | ++parsed; |
| 2650 | } |
| 2651 | |
| 2652 | do { |
| 2653 | if (expr[parsed] == '(') { |
| 2654 | |
| 2655 | /* '(' */ |
| 2656 | tok_len = 1; |
| 2657 | tok_type = LYXP_TOKEN_PAR1; |
| 2658 | |
| 2659 | if (prev_function_check && ret->used && (ret->tokens[ret->used - 1] == LYXP_TOKEN_NAMETEST)) { |
| 2660 | /* it is a NodeType/FunctionName after all */ |
| 2661 | if (((ret->tok_len[ret->used - 1] == 4) |
| 2662 | && (!strncmp(&expr[ret->tok_pos[ret->used - 1]], "node", 4) |
| 2663 | || !strncmp(&expr[ret->tok_pos[ret->used - 1]], "text", 4))) || |
| 2664 | ((ret->tok_len[ret->used - 1] == 7) |
| 2665 | && !strncmp(&expr[ret->tok_pos[ret->used - 1]], "comment", 7))) { |
| 2666 | ret->tokens[ret->used - 1] = LYXP_TOKEN_NODETYPE; |
| 2667 | } else { |
| 2668 | ret->tokens[ret->used - 1] = LYXP_TOKEN_FUNCNAME; |
| 2669 | } |
| 2670 | prev_function_check = 0; |
| 2671 | } |
| 2672 | |
| 2673 | } else if (expr[parsed] == ')') { |
| 2674 | |
| 2675 | /* ')' */ |
| 2676 | tok_len = 1; |
| 2677 | tok_type = LYXP_TOKEN_PAR2; |
| 2678 | |
| 2679 | } else if (expr[parsed] == '[') { |
| 2680 | |
| 2681 | /* '[' */ |
| 2682 | tok_len = 1; |
| 2683 | tok_type = LYXP_TOKEN_BRACK1; |
| 2684 | |
| 2685 | } else if (expr[parsed] == ']') { |
| 2686 | |
| 2687 | /* ']' */ |
| 2688 | tok_len = 1; |
| 2689 | tok_type = LYXP_TOKEN_BRACK2; |
| 2690 | |
| 2691 | } else if (!strncmp(&expr[parsed], "..", 2)) { |
| 2692 | |
| 2693 | /* '..' */ |
| 2694 | tok_len = 2; |
| 2695 | tok_type = LYXP_TOKEN_DDOT; |
| 2696 | |
| 2697 | } else if ((expr[parsed] == '.') && (!isdigit(expr[parsed + 1]))) { |
| 2698 | |
| 2699 | /* '.' */ |
| 2700 | tok_len = 1; |
| 2701 | tok_type = LYXP_TOKEN_DOT; |
| 2702 | |
| 2703 | } else if (expr[parsed] == '@') { |
| 2704 | |
| 2705 | /* '@' */ |
| 2706 | tok_len = 1; |
| 2707 | tok_type = LYXP_TOKEN_AT; |
| 2708 | |
| 2709 | } else if (expr[parsed] == ',') { |
| 2710 | |
| 2711 | /* ',' */ |
| 2712 | tok_len = 1; |
| 2713 | tok_type = LYXP_TOKEN_COMMA; |
| 2714 | |
| 2715 | } else if (expr[parsed] == '\'') { |
| 2716 | |
| 2717 | /* Literal with ' */ |
| 2718 | for (tok_len = 1; (expr[parsed + tok_len] != '\0') && (expr[parsed + tok_len] != '\''); ++tok_len); |
| 2719 | LY_CHECK_ERR_GOTO(expr[parsed + tok_len] == '\0', |
| 2720 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOE, expr[parsed], &expr[parsed]), error); |
| 2721 | ++tok_len; |
| 2722 | tok_type = LYXP_TOKEN_LITERAL; |
| 2723 | |
| 2724 | } else if (expr[parsed] == '\"') { |
| 2725 | |
| 2726 | /* Literal with " */ |
| 2727 | for (tok_len = 1; (expr[parsed + tok_len] != '\0') && (expr[parsed + tok_len] != '\"'); ++tok_len); |
| 2728 | LY_CHECK_ERR_GOTO(expr[parsed + tok_len] == '\0', |
| 2729 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOE, expr[parsed], &expr[parsed]), error); |
| 2730 | ++tok_len; |
| 2731 | tok_type = LYXP_TOKEN_LITERAL; |
| 2732 | |
| 2733 | } else if ((expr[parsed] == '.') || (isdigit(expr[parsed]))) { |
| 2734 | |
| 2735 | /* Number */ |
| 2736 | for (tok_len = 0; isdigit(expr[parsed + tok_len]); ++tok_len); |
| 2737 | if (expr[parsed + tok_len] == '.') { |
| 2738 | ++tok_len; |
| 2739 | for (; isdigit(expr[parsed + tok_len]); ++tok_len); |
| 2740 | } |
| 2741 | tok_type = LYXP_TOKEN_NUMBER; |
| 2742 | |
| 2743 | } else if (expr[parsed] == '/') { |
| 2744 | |
| 2745 | /* Operator '/', '//' */ |
| 2746 | if (!strncmp(&expr[parsed], "//", 2)) { |
| 2747 | tok_len = 2; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2748 | tok_type = LYXP_TOKEN_OPER_RPATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2749 | } else { |
| 2750 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2751 | tok_type = LYXP_TOKEN_OPER_PATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2752 | } |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2753 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2754 | } else if (!strncmp(&expr[parsed], "!=", 2)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2755 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2756 | /* Operator '!=' */ |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2757 | tok_len = 2; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2758 | tok_type = LYXP_TOKEN_OPER_NEQUAL; |
| 2759 | |
| 2760 | } else if (!strncmp(&expr[parsed], "<=", 2) || !strncmp(&expr[parsed], ">=", 2)) { |
| 2761 | |
| 2762 | /* Operator '<=', '>=' */ |
| 2763 | tok_len = 2; |
| 2764 | tok_type = LYXP_TOKEN_OPER_COMP; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2765 | |
| 2766 | } else if (expr[parsed] == '|') { |
| 2767 | |
| 2768 | /* Operator '|' */ |
| 2769 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2770 | tok_type = LYXP_TOKEN_OPER_UNI; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2771 | |
| 2772 | } else if ((expr[parsed] == '+') || (expr[parsed] == '-')) { |
| 2773 | |
| 2774 | /* Operator '+', '-' */ |
| 2775 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2776 | tok_type = LYXP_TOKEN_OPER_MATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2777 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2778 | } else if (expr[parsed] == '=') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2779 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2780 | /* Operator '=' */ |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2781 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2782 | tok_type = LYXP_TOKEN_OPER_EQUAL; |
| 2783 | |
| 2784 | } else if ((expr[parsed] == '<') || (expr[parsed] == '>')) { |
| 2785 | |
| 2786 | /* Operator '<', '>' */ |
| 2787 | tok_len = 1; |
| 2788 | tok_type = LYXP_TOKEN_OPER_COMP; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2789 | |
| 2790 | } else if (ret->used && (ret->tokens[ret->used - 1] != LYXP_TOKEN_AT) |
| 2791 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_PAR1) |
| 2792 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_BRACK1) |
| 2793 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_COMMA) |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2794 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_LOG) |
| 2795 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_EQUAL) |
| 2796 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_NEQUAL) |
| 2797 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_COMP) |
| 2798 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_MATH) |
| 2799 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_UNI) |
| 2800 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_PATH) |
| 2801 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_RPATH)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2802 | |
| 2803 | /* Operator '*', 'or', 'and', 'mod', or 'div' */ |
| 2804 | if (expr[parsed] == '*') { |
| 2805 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2806 | tok_type = LYXP_TOKEN_OPER_MATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2807 | |
| 2808 | } else if (!strncmp(&expr[parsed], "or", 2)) { |
| 2809 | tok_len = 2; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2810 | tok_type = LYXP_TOKEN_OPER_LOG; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2811 | |
| 2812 | } else if (!strncmp(&expr[parsed], "and", 3)) { |
| 2813 | tok_len = 3; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2814 | tok_type = LYXP_TOKEN_OPER_LOG; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2815 | |
| 2816 | } else if (!strncmp(&expr[parsed], "mod", 3) || !strncmp(&expr[parsed], "div", 3)) { |
| 2817 | tok_len = 3; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2818 | tok_type = LYXP_TOKEN_OPER_MATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2819 | |
| 2820 | } else if (prev_function_check) { |
Michal Vasko | 5307857 | 2019-05-24 08:50:15 +0200 | [diff] [blame] | 2821 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Invalid character 0x%x ('%c'), perhaps \"%.*s\" is supposed to be a function call.", |
| 2822 | 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] | 2823 | goto error; |
| 2824 | } else { |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2825 | 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] | 2826 | goto error; |
| 2827 | } |
| 2828 | } else if (expr[parsed] == '*') { |
| 2829 | |
| 2830 | /* NameTest '*' */ |
| 2831 | tok_len = 1; |
| 2832 | tok_type = LYXP_TOKEN_NAMETEST; |
| 2833 | |
| 2834 | } else { |
| 2835 | |
| 2836 | /* NameTest (NCName ':' '*' | QName) or NodeType/FunctionName */ |
| 2837 | ncname_len = parse_ncname(&expr[parsed]); |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2838 | 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] | 2839 | tok_len = ncname_len; |
| 2840 | |
| 2841 | if (expr[parsed + tok_len] == ':') { |
| 2842 | ++tok_len; |
| 2843 | if (expr[parsed + tok_len] == '*') { |
| 2844 | ++tok_len; |
| 2845 | } else { |
| 2846 | ncname_len = parse_ncname(&expr[parsed + tok_len]); |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2847 | 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] | 2848 | tok_len += ncname_len; |
| 2849 | } |
| 2850 | /* remove old flag to prevent ambiguities */ |
| 2851 | prev_function_check = 0; |
| 2852 | tok_type = LYXP_TOKEN_NAMETEST; |
| 2853 | } else { |
| 2854 | /* there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */ |
| 2855 | prev_function_check = 1; |
| 2856 | tok_type = LYXP_TOKEN_NAMETEST; |
| 2857 | } |
| 2858 | } |
| 2859 | |
| 2860 | /* store the token, move on to the next one */ |
| 2861 | LY_CHECK_GOTO(exp_add_token(ctx, ret, tok_type, parsed, tok_len), error); |
| 2862 | parsed += tok_len; |
| 2863 | while (is_xmlws(expr[parsed])) { |
| 2864 | ++parsed; |
| 2865 | } |
| 2866 | |
| 2867 | } while (expr[parsed]); |
| 2868 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2869 | if (reparse) { |
| 2870 | /* prealloc repeat */ |
| 2871 | ret->repeat = calloc(ret->size, sizeof *ret->repeat); |
| 2872 | LY_CHECK_ERR_GOTO(!ret->repeat, LOGMEM(ctx), error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2873 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2874 | /* fill repeat */ |
| 2875 | LY_CHECK_GOTO(reparse_or_expr(ctx, ret, &tok_idx), error); |
| 2876 | if (ret->used > tok_idx) { |
| 2877 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Unparsed characters \"%s\" left at the end of an XPath expression.", |
| 2878 | &ret->expr[ret->tok_pos[tok_idx]]); |
| 2879 | goto error; |
| 2880 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2881 | } |
| 2882 | |
| 2883 | print_expr_struct_debug(ret); |
| 2884 | |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2885 | return ret; |
| 2886 | |
| 2887 | error: |
| 2888 | lyxp_expr_free(ctx, ret); |
| 2889 | return NULL; |
| 2890 | } |
| 2891 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2892 | struct lyxp_expr * |
| 2893 | lyxp_expr_dup(const struct ly_ctx *ctx, const struct lyxp_expr *exp) |
| 2894 | { |
| 2895 | struct lyxp_expr *dup; |
| 2896 | uint32_t i, j; |
| 2897 | |
| 2898 | dup = calloc(1, sizeof *dup); |
| 2899 | LY_CHECK_ERR_GOTO(!dup, LOGMEM(ctx), error); |
| 2900 | |
| 2901 | dup->tokens = malloc(exp->used * sizeof *dup->tokens); |
| 2902 | LY_CHECK_ERR_GOTO(!dup->tokens, LOGMEM(ctx), error); |
| 2903 | memcpy(dup->tokens, exp->tokens, exp->used * sizeof *dup->tokens); |
| 2904 | |
| 2905 | dup->tok_pos = malloc(exp->used * sizeof *dup->tok_pos); |
| 2906 | LY_CHECK_ERR_GOTO(!dup->tok_pos, LOGMEM(ctx), error); |
| 2907 | memcpy(dup->tok_pos, exp->tok_pos, exp->used * sizeof *dup->tok_pos); |
| 2908 | |
| 2909 | dup->tok_len = malloc(exp->used * sizeof *dup->tok_len); |
| 2910 | LY_CHECK_ERR_GOTO(!dup->tok_len, LOGMEM(ctx), error); |
| 2911 | memcpy(dup->tok_len, exp->tok_len, exp->used * sizeof *dup->tok_len); |
| 2912 | |
| 2913 | dup->repeat = malloc(exp->used * sizeof *dup->repeat); |
| 2914 | LY_CHECK_ERR_GOTO(!dup->repeat, LOGMEM(ctx), error); |
| 2915 | for (i = 0; i < exp->used; ++i) { |
| 2916 | if (!exp->repeat[i]) { |
| 2917 | dup->repeat[i] = NULL; |
| 2918 | } else { |
| 2919 | for (j = 0; exp->repeat[i][j]; ++j); |
| 2920 | /* the ending 0 as well */ |
| 2921 | ++j; |
| 2922 | |
| 2923 | dup->repeat[i] = malloc(j * sizeof *dup->repeat); |
| 2924 | LY_CHECK_ERR_GOTO(!dup->repeat[i], LOGMEM(ctx), error); |
| 2925 | memcpy(dup->repeat[i], exp->repeat[i], j * sizeof **dup->repeat); |
| 2926 | dup->repeat[i][j - 1] = 0; |
| 2927 | } |
| 2928 | } |
| 2929 | |
| 2930 | dup->used = exp->used; |
| 2931 | dup->size = exp->used; |
| 2932 | dup->expr = lydict_insert(ctx, exp->expr, 0); |
| 2933 | |
| 2934 | return dup; |
| 2935 | |
| 2936 | error: |
| 2937 | lyxp_expr_free(ctx, dup); |
| 2938 | return NULL; |
| 2939 | } |
| 2940 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2941 | /* |
| 2942 | * warn functions |
| 2943 | * |
| 2944 | * Warn functions check specific reasonable conditions for schema XPath |
| 2945 | * and print a warning if they are not satisfied. |
| 2946 | */ |
| 2947 | |
| 2948 | /** |
| 2949 | * @brief Get the last-added schema node that is currently in the context. |
| 2950 | * |
| 2951 | * @param[in] set Set to search in. |
| 2952 | * @return Last-added schema context node, NULL if no node is in context. |
| 2953 | */ |
| 2954 | static struct lysc_node * |
| 2955 | warn_get_scnode_in_ctx(struct lyxp_set *set) |
| 2956 | { |
| 2957 | uint32_t i; |
| 2958 | |
| 2959 | if (!set || (set->type != LYXP_SET_SCNODE_SET)) { |
| 2960 | return NULL; |
| 2961 | } |
| 2962 | |
| 2963 | i = set->used; |
| 2964 | do { |
| 2965 | --i; |
| 2966 | if (set->val.scnodes[i].in_ctx == 1) { |
| 2967 | /* if there are more, simply return the first found (last added) */ |
| 2968 | return set->val.scnodes[i].scnode; |
| 2969 | } |
| 2970 | } while (i); |
| 2971 | |
| 2972 | return NULL; |
| 2973 | } |
| 2974 | |
| 2975 | /** |
| 2976 | * @brief Test whether a type is numeric - integer type or decimal64. |
| 2977 | * |
| 2978 | * @param[in] type Type to test. |
| 2979 | * @return 1 if numeric, 0 otherwise. |
| 2980 | */ |
| 2981 | static int |
| 2982 | warn_is_numeric_type(struct lysc_type *type) |
| 2983 | { |
| 2984 | struct lysc_type_union *uni; |
| 2985 | int ret; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 2986 | LY_ARRAY_SIZE_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2987 | |
| 2988 | switch (type->basetype) { |
| 2989 | case LY_TYPE_DEC64: |
| 2990 | case LY_TYPE_INT8: |
| 2991 | case LY_TYPE_UINT8: |
| 2992 | case LY_TYPE_INT16: |
| 2993 | case LY_TYPE_UINT16: |
| 2994 | case LY_TYPE_INT32: |
| 2995 | case LY_TYPE_UINT32: |
| 2996 | case LY_TYPE_INT64: |
| 2997 | case LY_TYPE_UINT64: |
| 2998 | return 1; |
| 2999 | case LY_TYPE_UNION: |
| 3000 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3001 | LY_ARRAY_FOR(uni->types, u) { |
| 3002 | ret = warn_is_numeric_type(uni->types[u]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3003 | if (ret) { |
| 3004 | /* found a suitable type */ |
| 3005 | return 1; |
| 3006 | } |
| 3007 | } |
| 3008 | /* did not find any suitable type */ |
| 3009 | return 0; |
| 3010 | case LY_TYPE_LEAFREF: |
| 3011 | return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype); |
| 3012 | default: |
| 3013 | return 0; |
| 3014 | } |
| 3015 | } |
| 3016 | |
| 3017 | /** |
| 3018 | * @brief Test whether a type is string-like - no integers, decimal64 or binary. |
| 3019 | * |
| 3020 | * @param[in] type Type to test. |
| 3021 | * @return 1 if string, 0 otherwise. |
| 3022 | */ |
| 3023 | static int |
| 3024 | warn_is_string_type(struct lysc_type *type) |
| 3025 | { |
| 3026 | struct lysc_type_union *uni; |
| 3027 | int ret; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3028 | LY_ARRAY_SIZE_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3029 | |
| 3030 | switch (type->basetype) { |
| 3031 | case LY_TYPE_BITS: |
| 3032 | case LY_TYPE_ENUM: |
| 3033 | case LY_TYPE_IDENT: |
| 3034 | case LY_TYPE_INST: |
| 3035 | case LY_TYPE_STRING: |
| 3036 | return 1; |
| 3037 | case LY_TYPE_UNION: |
| 3038 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3039 | LY_ARRAY_FOR(uni->types, u) { |
| 3040 | ret = warn_is_string_type(uni->types[u]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3041 | if (ret) { |
| 3042 | /* found a suitable type */ |
| 3043 | return 1; |
| 3044 | } |
| 3045 | } |
| 3046 | /* did not find any suitable type */ |
| 3047 | return 0; |
| 3048 | case LY_TYPE_LEAFREF: |
| 3049 | return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype); |
| 3050 | default: |
| 3051 | return 0; |
| 3052 | } |
| 3053 | } |
| 3054 | |
| 3055 | /** |
| 3056 | * @brief Test whether a type is one specific type. |
| 3057 | * |
| 3058 | * @param[in] type Type to test. |
| 3059 | * @param[in] base Expected type. |
| 3060 | * @return 1 if it is, 0 otherwise. |
| 3061 | */ |
| 3062 | static int |
| 3063 | warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base) |
| 3064 | { |
| 3065 | struct lysc_type_union *uni; |
| 3066 | int ret; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3067 | LY_ARRAY_SIZE_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3068 | |
| 3069 | if (type->basetype == base) { |
| 3070 | return 1; |
| 3071 | } else if (type->basetype == LY_TYPE_UNION) { |
| 3072 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3073 | LY_ARRAY_FOR(uni->types, u) { |
| 3074 | ret = warn_is_specific_type(uni->types[u], base); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3075 | if (ret) { |
| 3076 | /* found a suitable type */ |
| 3077 | return 1; |
| 3078 | } |
| 3079 | } |
| 3080 | /* did not find any suitable type */ |
| 3081 | return 0; |
| 3082 | } else if (type->basetype == LY_TYPE_LEAFREF) { |
| 3083 | return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base); |
| 3084 | } |
| 3085 | |
| 3086 | return 0; |
| 3087 | } |
| 3088 | |
| 3089 | /** |
| 3090 | * @brief Get next type of a (union) type. |
| 3091 | * |
| 3092 | * @param[in] type Base type. |
| 3093 | * @param[in] prev_type Previously returned type. |
| 3094 | * @return Next type or NULL. |
| 3095 | */ |
| 3096 | static struct lysc_type * |
| 3097 | warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type) |
| 3098 | { |
| 3099 | struct lysc_type_union *uni; |
| 3100 | int found = 0; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3101 | LY_ARRAY_SIZE_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3102 | |
| 3103 | switch (type->basetype) { |
| 3104 | case LY_TYPE_UNION: |
| 3105 | uni = (struct lysc_type_union *)type; |
| 3106 | if (!prev_type) { |
| 3107 | return uni->types[0]; |
| 3108 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3109 | LY_ARRAY_FOR(uni->types, u) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3110 | if (found) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3111 | return uni->types[u]; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3112 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3113 | if (prev_type == uni->types[u]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3114 | found = 1; |
| 3115 | } |
| 3116 | } |
| 3117 | return NULL; |
| 3118 | default: |
| 3119 | if (prev_type) { |
| 3120 | assert(type == prev_type); |
| 3121 | return NULL; |
| 3122 | } else { |
| 3123 | return type; |
| 3124 | } |
| 3125 | } |
| 3126 | } |
| 3127 | |
| 3128 | /** |
| 3129 | * @brief Test whether 2 types have a common type. |
| 3130 | * |
| 3131 | * @param[in] type1 First type. |
| 3132 | * @param[in] type2 Second type. |
| 3133 | * @return 1 if they do, 0 otherwise. |
| 3134 | */ |
| 3135 | static int |
| 3136 | warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2) |
| 3137 | { |
| 3138 | struct lysc_type *t1, *rt1, *t2, *rt2; |
| 3139 | |
| 3140 | t1 = NULL; |
| 3141 | while ((t1 = warn_is_equal_type_next_type(type1, t1))) { |
| 3142 | if (t1->basetype == LY_TYPE_LEAFREF) { |
| 3143 | rt1 = ((struct lysc_type_leafref *)t1)->realtype; |
| 3144 | } else { |
| 3145 | rt1 = t1; |
| 3146 | } |
| 3147 | |
| 3148 | t2 = NULL; |
| 3149 | while ((t2 = warn_is_equal_type_next_type(type2, t2))) { |
| 3150 | if (t2->basetype == LY_TYPE_LEAFREF) { |
| 3151 | rt2 = ((struct lysc_type_leafref *)t2)->realtype; |
| 3152 | } else { |
| 3153 | rt2 = t2; |
| 3154 | } |
| 3155 | |
| 3156 | if (rt2->basetype == rt1->basetype) { |
| 3157 | /* match found */ |
| 3158 | return 1; |
| 3159 | } |
| 3160 | } |
| 3161 | } |
| 3162 | |
| 3163 | return 0; |
| 3164 | } |
| 3165 | |
| 3166 | /** |
| 3167 | * @brief Check both operands of comparison operators. |
| 3168 | * |
| 3169 | * @param[in] ctx Context for errors. |
| 3170 | * @param[in] set1 First operand set. |
| 3171 | * @param[in] set2 Second operand set. |
| 3172 | * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!='). |
| 3173 | * @param[in] expr Start of the expression to print with the warning. |
| 3174 | * @param[in] tok_pos Token position. |
| 3175 | */ |
| 3176 | static void |
| 3177 | warn_operands(struct ly_ctx *ctx, struct lyxp_set *set1, struct lyxp_set *set2, int numbers_only, const char *expr, uint16_t tok_pos) |
| 3178 | { |
| 3179 | struct lysc_node_leaf *node1, *node2; |
| 3180 | int leaves = 1, warning = 0; |
| 3181 | |
| 3182 | node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1); |
| 3183 | node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2); |
| 3184 | |
| 3185 | if (!node1 && !node2) { |
| 3186 | /* no node-sets involved, nothing to do */ |
| 3187 | return; |
| 3188 | } |
| 3189 | |
| 3190 | if (node1) { |
| 3191 | if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3192 | LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name); |
| 3193 | warning = 1; |
| 3194 | leaves = 0; |
| 3195 | } else if (numbers_only && !warn_is_numeric_type(node1->type)) { |
| 3196 | LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name); |
| 3197 | warning = 1; |
| 3198 | } |
| 3199 | } |
| 3200 | |
| 3201 | if (node2) { |
| 3202 | if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3203 | LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name); |
| 3204 | warning = 1; |
| 3205 | leaves = 0; |
| 3206 | } else if (numbers_only && !warn_is_numeric_type(node2->type)) { |
| 3207 | LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name); |
| 3208 | warning = 1; |
| 3209 | } |
| 3210 | } |
| 3211 | |
| 3212 | if (node1 && node2 && leaves && !numbers_only) { |
| 3213 | if ((warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type)) |
| 3214 | || (!warn_is_numeric_type(node1->type) && warn_is_numeric_type(node2->type)) |
| 3215 | || (!warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type) |
| 3216 | && !warn_is_equal_type(node1->type, node2->type))) { |
| 3217 | LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name); |
| 3218 | warning = 1; |
| 3219 | } |
| 3220 | } |
| 3221 | |
| 3222 | if (warning) { |
| 3223 | LOGWRN(ctx, "Previous warning generated by XPath subexpression[%u] \"%.20s\".", tok_pos, expr + tok_pos); |
| 3224 | } |
| 3225 | } |
| 3226 | |
| 3227 | /** |
| 3228 | * @brief Check that a value is valid for a leaf. If not applicable, does nothing. |
| 3229 | * |
| 3230 | * @param[in] exp Parsed XPath expression. |
| 3231 | * @param[in] set Set with the leaf/leaf-list. |
| 3232 | * @param[in] val_exp Index of the value (literal/number) in @p exp. |
| 3233 | * @param[in] equal_exp Index of the start of the equality expression in @p exp. |
| 3234 | * @param[in] last_equal_exp Index of the end of the equality expression in @p exp. |
| 3235 | */ |
| 3236 | static void |
| 3237 | warn_equality_value(struct lyxp_expr *exp, struct lyxp_set *set, uint16_t val_exp, uint16_t equal_exp, uint16_t last_equal_exp) |
| 3238 | { |
| 3239 | struct lysc_node *scnode; |
| 3240 | struct lysc_type *type; |
| 3241 | char *value; |
| 3242 | LY_ERR rc; |
| 3243 | struct ly_err_item *err = NULL; |
| 3244 | |
| 3245 | if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST)) |
| 3246 | && ((exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) || (exp->tokens[val_exp] == LYXP_TOKEN_NUMBER))) { |
| 3247 | /* check that the node can have the specified value */ |
| 3248 | if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) { |
| 3249 | value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2); |
| 3250 | } else { |
| 3251 | value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]); |
| 3252 | } |
| 3253 | if (!value) { |
| 3254 | LOGMEM(set->ctx); |
| 3255 | return; |
| 3256 | } |
| 3257 | |
| 3258 | if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) { |
| 3259 | LOGWRN(set->ctx, "Identityref \"%s\" comparison with identity \"%s\" without prefix, consider adding" |
| 3260 | " a prefix or best using \"derived-from(-or-self)()\" functions.", scnode->name, value); |
| 3261 | LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp], |
| 3262 | (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp], |
| 3263 | exp->expr + exp->tok_pos[equal_exp]); |
| 3264 | } |
| 3265 | |
| 3266 | type = ((struct lysc_node_leaf *)scnode)->type; |
| 3267 | if (type->basetype != LY_TYPE_IDENT) { |
| 3268 | rc = type->plugin->store(set->ctx, type, value, strlen(value), LY_TYPE_OPTS_SCHEMA, |
| 3269 | lys_resolve_prefix, (void *)type->dflt_mod, LYD_XML, NULL, NULL, NULL, NULL, &err); |
| 3270 | |
| 3271 | if (err) { |
| 3272 | LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg); |
| 3273 | ly_err_free(err); |
| 3274 | } else if (rc != LY_SUCCESS) { |
| 3275 | LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value); |
| 3276 | } |
| 3277 | if (rc != LY_SUCCESS) { |
| 3278 | LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp], |
| 3279 | (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp], |
| 3280 | exp->expr + exp->tok_pos[equal_exp]); |
| 3281 | } |
| 3282 | } |
| 3283 | free(value); |
| 3284 | } |
| 3285 | } |
| 3286 | |
| 3287 | /* |
| 3288 | * XPath functions |
| 3289 | */ |
| 3290 | |
| 3291 | /** |
| 3292 | * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN |
| 3293 | * depending on whether the first node bit value from the second argument is set. |
| 3294 | * |
| 3295 | * @param[in] args Array of arguments. |
| 3296 | * @param[in] arg_count Count of elements in @p args. |
| 3297 | * @param[in,out] set Context and result set at the same time. |
| 3298 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3299 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3300 | */ |
| 3301 | static LY_ERR |
| 3302 | xpath_bit_is_set(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3303 | { |
| 3304 | struct lyd_node_term *leaf; |
| 3305 | struct lysc_node_leaf *sleaf; |
| 3306 | struct lysc_type_bits *bits; |
| 3307 | LY_ERR rc = LY_SUCCESS; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3308 | LY_ARRAY_SIZE_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3309 | |
| 3310 | if (options & LYXP_SCNODE_ALL) { |
| 3311 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3312 | 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] | 3313 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3314 | 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] | 3315 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) { |
| 3316 | 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] | 3317 | } |
| 3318 | |
| 3319 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 3320 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3321 | 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] | 3322 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3323 | 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] | 3324 | } |
| 3325 | } |
| 3326 | set_scnode_clear_ctx(set); |
| 3327 | return rc; |
| 3328 | } |
| 3329 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3330 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3331 | 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)"); |
| 3332 | return LY_EVALID; |
| 3333 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3334 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3335 | LY_CHECK_RET(rc); |
| 3336 | |
| 3337 | set_fill_boolean(set, 0); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3338 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3339 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
| 3340 | if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) |
| 3341 | && (((struct lysc_node_leaf *)leaf->schema)->type->basetype == LY_TYPE_BITS)) { |
| 3342 | bits = (struct lysc_type_bits *)((struct lysc_node_leaf *)leaf->schema)->type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3343 | LY_ARRAY_FOR(bits->bits, u) { |
| 3344 | if (!strcmp(bits->bits[u].name, args[1]->val.str)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3345 | set_fill_boolean(set, 1); |
| 3346 | break; |
| 3347 | } |
| 3348 | } |
| 3349 | } |
| 3350 | } |
| 3351 | |
| 3352 | return LY_SUCCESS; |
| 3353 | } |
| 3354 | |
| 3355 | /** |
| 3356 | * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN |
| 3357 | * with the argument converted to boolean. |
| 3358 | * |
| 3359 | * @param[in] args Array of arguments. |
| 3360 | * @param[in] arg_count Count of elements in @p args. |
| 3361 | * @param[in,out] set Context and result set at the same time. |
| 3362 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3363 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3364 | */ |
| 3365 | static LY_ERR |
| 3366 | xpath_boolean(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3367 | { |
| 3368 | LY_ERR rc; |
| 3369 | |
| 3370 | if (options & LYXP_SCNODE_ALL) { |
| 3371 | set_scnode_clear_ctx(set); |
| 3372 | return LY_SUCCESS; |
| 3373 | } |
| 3374 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3375 | rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3376 | LY_CHECK_RET(rc); |
| 3377 | set_fill_set(set, args[0]); |
| 3378 | |
| 3379 | return LY_SUCCESS; |
| 3380 | } |
| 3381 | |
| 3382 | /** |
| 3383 | * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER |
| 3384 | * with the first argument rounded up to the nearest integer. |
| 3385 | * |
| 3386 | * @param[in] args Array of arguments. |
| 3387 | * @param[in] arg_count Count of elements in @p args. |
| 3388 | * @param[in,out] set Context and result set at the same time. |
| 3389 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3390 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3391 | */ |
| 3392 | static LY_ERR |
| 3393 | xpath_ceiling(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3394 | { |
| 3395 | struct lysc_node_leaf *sleaf; |
| 3396 | LY_ERR rc = LY_SUCCESS; |
| 3397 | |
| 3398 | if (options & LYXP_SCNODE_ALL) { |
| 3399 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3400 | 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] | 3401 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3402 | 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] | 3403 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) { |
| 3404 | 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] | 3405 | } |
| 3406 | set_scnode_clear_ctx(set); |
| 3407 | return rc; |
| 3408 | } |
| 3409 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3410 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3411 | LY_CHECK_RET(rc); |
| 3412 | if ((long long)args[0]->val.num != args[0]->val.num) { |
| 3413 | set_fill_number(set, ((long long)args[0]->val.num) + 1); |
| 3414 | } else { |
| 3415 | set_fill_number(set, args[0]->val.num); |
| 3416 | } |
| 3417 | |
| 3418 | return LY_SUCCESS; |
| 3419 | } |
| 3420 | |
| 3421 | /** |
| 3422 | * @brief Execute the XPath concat(string, string, string*) function. |
| 3423 | * Returns LYXP_SET_STRING with the concatenation of all the arguments. |
| 3424 | * |
| 3425 | * @param[in] args Array of arguments. |
| 3426 | * @param[in] arg_count Count of elements in @p args. |
| 3427 | * @param[in,out] set Context and result set at the same time. |
| 3428 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3429 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3430 | */ |
| 3431 | static LY_ERR |
| 3432 | xpath_concat(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options) |
| 3433 | { |
| 3434 | uint16_t i; |
| 3435 | char *str = NULL; |
| 3436 | size_t used = 1; |
| 3437 | LY_ERR rc = LY_SUCCESS; |
| 3438 | struct lysc_node_leaf *sleaf; |
| 3439 | |
| 3440 | if (options & LYXP_SCNODE_ALL) { |
| 3441 | for (i = 0; i < arg_count; ++i) { |
| 3442 | if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) { |
| 3443 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3444 | LOGWRN(set->ctx, "Argument #%u of %s is a %s node \"%s\".", |
| 3445 | i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3446 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3447 | 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] | 3448 | } |
| 3449 | } |
| 3450 | } |
| 3451 | set_scnode_clear_ctx(set); |
| 3452 | return rc; |
| 3453 | } |
| 3454 | |
| 3455 | for (i = 0; i < arg_count; ++i) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3456 | rc = lyxp_set_cast(args[i], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3457 | if (rc != LY_SUCCESS) { |
| 3458 | free(str); |
| 3459 | return rc; |
| 3460 | } |
| 3461 | |
| 3462 | str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char)); |
| 3463 | LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM); |
| 3464 | strcpy(str + used - 1, args[i]->val.str); |
| 3465 | used += strlen(args[i]->val.str); |
| 3466 | } |
| 3467 | |
| 3468 | /* free, kind of */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3469 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3470 | set->type = LYXP_SET_STRING; |
| 3471 | set->val.str = str; |
| 3472 | |
| 3473 | return LY_SUCCESS; |
| 3474 | } |
| 3475 | |
| 3476 | /** |
| 3477 | * @brief Execute the XPath contains(string, string) function. |
| 3478 | * Returns LYXP_SET_BOOLEAN whether the second argument can |
| 3479 | * be found in the first or not. |
| 3480 | * |
| 3481 | * @param[in] args Array of arguments. |
| 3482 | * @param[in] arg_count Count of elements in @p args. |
| 3483 | * @param[in,out] set Context and result set at the same time. |
| 3484 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3485 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3486 | */ |
| 3487 | static LY_ERR |
| 3488 | xpath_contains(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3489 | { |
| 3490 | struct lysc_node_leaf *sleaf; |
| 3491 | LY_ERR rc = LY_SUCCESS; |
| 3492 | |
| 3493 | if (options & LYXP_SCNODE_ALL) { |
| 3494 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3495 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3496 | 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] | 3497 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3498 | 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] | 3499 | } |
| 3500 | } |
| 3501 | |
| 3502 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 3503 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3504 | 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] | 3505 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3506 | 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] | 3507 | } |
| 3508 | } |
| 3509 | set_scnode_clear_ctx(set); |
| 3510 | return rc; |
| 3511 | } |
| 3512 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3513 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3514 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3515 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3516 | LY_CHECK_RET(rc); |
| 3517 | |
| 3518 | if (strstr(args[0]->val.str, args[1]->val.str)) { |
| 3519 | set_fill_boolean(set, 1); |
| 3520 | } else { |
| 3521 | set_fill_boolean(set, 0); |
| 3522 | } |
| 3523 | |
| 3524 | return LY_SUCCESS; |
| 3525 | } |
| 3526 | |
| 3527 | /** |
| 3528 | * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER |
| 3529 | * with the size of the node-set from the argument. |
| 3530 | * |
| 3531 | * @param[in] args Array of arguments. |
| 3532 | * @param[in] arg_count Count of elements in @p args. |
| 3533 | * @param[in,out] set Context and result set at the same time. |
| 3534 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3535 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3536 | */ |
| 3537 | static LY_ERR |
| 3538 | xpath_count(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3539 | { |
| 3540 | struct lysc_node *scnode = NULL; |
| 3541 | LY_ERR rc = LY_SUCCESS; |
| 3542 | |
| 3543 | if (options & LYXP_SCNODE_ALL) { |
| 3544 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(scnode = warn_get_scnode_in_ctx(args[0]))) { |
| 3545 | 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] | 3546 | } |
| 3547 | set_scnode_clear_ctx(set); |
| 3548 | return rc; |
| 3549 | } |
| 3550 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3551 | if (args[0]->type != LYXP_SET_NODE_SET) { |
| 3552 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "count(node-set)"); |
| 3553 | return LY_EVALID; |
| 3554 | } |
| 3555 | |
| 3556 | set_fill_number(set, args[0]->used); |
| 3557 | return LY_SUCCESS; |
| 3558 | } |
| 3559 | |
| 3560 | /** |
| 3561 | * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET |
| 3562 | * with the context with the intial node. |
| 3563 | * |
| 3564 | * @param[in] args Array of arguments. |
| 3565 | * @param[in] arg_count Count of elements in @p args. |
| 3566 | * @param[in,out] set Context and result set at the same time. |
| 3567 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3568 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3569 | */ |
| 3570 | static LY_ERR |
| 3571 | xpath_current(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options) |
| 3572 | { |
| 3573 | if (arg_count || args) { |
| 3574 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGCOUNT, arg_count, "current()"); |
| 3575 | return LY_EVALID; |
| 3576 | } |
| 3577 | |
| 3578 | if (options & LYXP_SCNODE_ALL) { |
| 3579 | set_scnode_clear_ctx(set); |
| 3580 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 3581 | lyxp_set_scnode_insert_node(set, set->ctx_scnode, LYXP_NODE_ELEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3582 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3583 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3584 | |
| 3585 | /* position is filled later */ |
| 3586 | set_insert_node(set, set->ctx_node, 0, LYXP_NODE_ELEM, 0); |
| 3587 | } |
| 3588 | |
| 3589 | return LY_SUCCESS; |
| 3590 | } |
| 3591 | |
| 3592 | /** |
| 3593 | * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either |
| 3594 | * leafref or instance-identifier target node(s). |
| 3595 | * |
| 3596 | * @param[in] args Array of arguments. |
| 3597 | * @param[in] arg_count Count of elements in @p args. |
| 3598 | * @param[in,out] set Context and result set at the same time. |
| 3599 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3600 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3601 | */ |
| 3602 | static LY_ERR |
| 3603 | xpath_deref(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3604 | { |
| 3605 | struct lyd_node_term *leaf; |
Michal Vasko | 42e497c | 2020-01-06 08:38:25 +0100 | [diff] [blame] | 3606 | struct lysc_node_leaf *sleaf = NULL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3607 | struct lysc_type_leafref *lref; |
Michal Vasko | ae9e4cb | 2019-09-25 08:43:05 +0200 | [diff] [blame] | 3608 | const struct lysc_node *target; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3609 | struct ly_path *p; |
| 3610 | struct lyd_node *node; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3611 | char *errmsg = NULL; |
| 3612 | const char *val; |
| 3613 | int dynamic; |
| 3614 | LY_ERR rc = LY_SUCCESS; |
| 3615 | |
| 3616 | if (options & LYXP_SCNODE_ALL) { |
| 3617 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3618 | 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] | 3619 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3620 | 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] | 3621 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) && !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) { |
| 3622 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"leafref\" nor \"instance-identifier\".", |
| 3623 | __func__, sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3624 | } |
| 3625 | set_scnode_clear_ctx(set); |
Michal Vasko | 42e497c | 2020-01-06 08:38:25 +0100 | [diff] [blame] | 3626 | if (sleaf && (sleaf->type->basetype == LY_TYPE_LEAFREF)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3627 | lref = (struct lysc_type_leafref *)sleaf->type; |
| 3628 | |
| 3629 | /* it was already evaluated on schema, it must succeed */ |
| 3630 | if (set->format == LYD_JSON) { |
| 3631 | rc = ly_path_compile(sleaf->module, (struct lysc_node *)sleaf, lref->path, LY_PATH_LREF_TRUE, |
| 3632 | lydjson_resolve_prefix, NULL, LYD_JSON, &p); |
| 3633 | } else { |
| 3634 | assert(set->format == LYD_SCHEMA); |
| 3635 | rc = ly_path_compile(sleaf->module, (struct lysc_node *)sleaf, lref->path, LY_PATH_LREF_TRUE, |
| 3636 | lys_resolve_prefix, lref->path_context, LYD_SCHEMA, &p); |
Michal Vasko | ae9e4cb | 2019-09-25 08:43:05 +0200 | [diff] [blame] | 3637 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3638 | assert(!rc); |
| 3639 | |
| 3640 | /* get the target node */ |
| 3641 | target = p[LY_ARRAY_SIZE(p) - 1].node; |
| 3642 | ly_path_free(set->ctx, p); |
| 3643 | |
| 3644 | lyxp_set_scnode_insert_node(set, target, LYXP_NODE_ELEM); |
Michal Vasko | ae9e4cb | 2019-09-25 08:43:05 +0200 | [diff] [blame] | 3645 | } |
| 3646 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3647 | return rc; |
| 3648 | } |
| 3649 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3650 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3651 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "deref(node-set)"); |
| 3652 | return LY_EVALID; |
| 3653 | } |
| 3654 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3655 | lyxp_set_free_content(set); |
| 3656 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3657 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
| 3658 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 3659 | if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 3660 | if (sleaf->type->basetype == LY_TYPE_LEAFREF) { |
| 3661 | /* find leafref target */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3662 | if (ly_type_find_leafref((struct lysc_type_leafref *)sleaf->type, (struct lyd_node *)leaf, |
| 3663 | &leaf->value, set->tree, &node, &errmsg)) { |
| 3664 | LOGERR(set->ctx, LY_EVALID, errmsg); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3665 | free(errmsg); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3666 | return LY_EVALID; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3667 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3668 | } else { |
| 3669 | assert(sleaf->type->basetype == LY_TYPE_INST); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3670 | if (ly_path_eval(leaf->value.target, set->tree, &node)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3671 | val = lyd_value2str(leaf, &dynamic); |
| 3672 | LOGERR(set->ctx, LY_EVALID, "Invalid instance-identifier \"%s\" value - required instance not found.", val); |
| 3673 | if (dynamic) { |
| 3674 | free((char *)val); |
| 3675 | } |
| 3676 | return LY_EVALID; |
| 3677 | } |
| 3678 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3679 | |
| 3680 | /* insert it */ |
| 3681 | set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3682 | } |
| 3683 | } |
| 3684 | |
| 3685 | return LY_SUCCESS; |
| 3686 | } |
| 3687 | |
| 3688 | /** |
| 3689 | * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending |
| 3690 | * on whether the first argument nodes contain a node of an identity derived from the second |
| 3691 | * argument identity. |
| 3692 | * |
| 3693 | * @param[in] args Array of arguments. |
| 3694 | * @param[in] arg_count Count of elements in @p args. |
| 3695 | * @param[in,out] set Context and result set at the same time. |
| 3696 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3697 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3698 | */ |
| 3699 | static LY_ERR |
| 3700 | xpath_derived_from(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3701 | { |
| 3702 | uint16_t i; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3703 | LY_ARRAY_SIZE_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3704 | struct lyd_node_term *leaf; |
| 3705 | struct lysc_node_leaf *sleaf; |
| 3706 | struct lyd_value data = {0}; |
| 3707 | struct ly_err_item *err = NULL; |
| 3708 | LY_ERR rc = LY_SUCCESS; |
| 3709 | int found; |
| 3710 | |
| 3711 | if (options & LYXP_SCNODE_ALL) { |
| 3712 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3713 | 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] | 3714 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3715 | 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] | 3716 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) { |
| 3717 | 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] | 3718 | } |
| 3719 | |
| 3720 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 3721 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3722 | 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] | 3723 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3724 | 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] | 3725 | } |
| 3726 | } |
| 3727 | set_scnode_clear_ctx(set); |
| 3728 | return rc; |
| 3729 | } |
| 3730 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3731 | if (args[0]->type != LYXP_SET_NODE_SET) { |
| 3732 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), |
| 3733 | "derived-from(node-set, string)"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3734 | return LY_EVALID; |
| 3735 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3736 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3737 | LY_CHECK_RET(rc); |
| 3738 | |
| 3739 | set_fill_boolean(set, 0); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3740 | found = 0; |
| 3741 | for (i = 0; i < args[0]->used; ++i) { |
| 3742 | leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node; |
| 3743 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 3744 | if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_IDENT)) { |
| 3745 | /* store args[1] into ident */ |
| 3746 | rc = sleaf->type->plugin->store(set->ctx, sleaf->type, args[1]->val.str, strlen(args[1]->val.str), |
| 3747 | LY_TYPE_OPTS_STORE, lys_resolve_prefix, (void *)sleaf->dflt_mod, set->format, |
| 3748 | (struct lyd_node *)leaf, set->tree, &data, NULL, &err); |
| 3749 | if (err) { |
| 3750 | ly_err_print(err); |
| 3751 | ly_err_free(err); |
| 3752 | } |
| 3753 | LY_CHECK_RET(rc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3754 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3755 | LY_ARRAY_FOR(data.ident->derived, u) { |
| 3756 | if (data.ident->derived[u] == leaf->value.ident) { |
| 3757 | set_fill_boolean(set, 1); |
| 3758 | found = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3759 | break; |
| 3760 | } |
| 3761 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3762 | if (found) { |
| 3763 | break; |
| 3764 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3765 | } |
| 3766 | } |
| 3767 | |
| 3768 | return LY_SUCCESS; |
| 3769 | } |
| 3770 | |
| 3771 | /** |
| 3772 | * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending |
| 3773 | * on whether the first argument nodes contain a node of an identity that either is or is derived from |
| 3774 | * the second argument identity. |
| 3775 | * |
| 3776 | * @param[in] args Array of arguments. |
| 3777 | * @param[in] arg_count Count of elements in @p args. |
| 3778 | * @param[in,out] set Context and result set at the same time. |
| 3779 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3780 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3781 | */ |
| 3782 | static LY_ERR |
| 3783 | xpath_derived_from_or_self(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3784 | { |
| 3785 | uint16_t i; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3786 | LY_ARRAY_SIZE_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3787 | struct lyd_node_term *leaf; |
| 3788 | struct lysc_node_leaf *sleaf; |
| 3789 | struct lyd_value data = {0}; |
| 3790 | struct ly_err_item *err = NULL; |
| 3791 | LY_ERR rc = LY_SUCCESS; |
| 3792 | int found; |
| 3793 | |
| 3794 | if (options & LYXP_SCNODE_ALL) { |
| 3795 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3796 | 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] | 3797 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3798 | 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] | 3799 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) { |
| 3800 | 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] | 3801 | } |
| 3802 | |
| 3803 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 3804 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3805 | 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] | 3806 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3807 | 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] | 3808 | } |
| 3809 | } |
| 3810 | set_scnode_clear_ctx(set); |
| 3811 | return rc; |
| 3812 | } |
| 3813 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3814 | if (args[0]->type != LYXP_SET_NODE_SET) { |
| 3815 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), |
| 3816 | "derived-from-or-self(node-set, string)"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3817 | return LY_EVALID; |
| 3818 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3819 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3820 | LY_CHECK_RET(rc); |
| 3821 | |
| 3822 | set_fill_boolean(set, 0); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3823 | found = 0; |
| 3824 | for (i = 0; i < args[0]->used; ++i) { |
| 3825 | leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node; |
| 3826 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 3827 | if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_IDENT)) { |
| 3828 | /* store args[1] into ident */ |
| 3829 | rc = sleaf->type->plugin->store(set->ctx, sleaf->type, args[1]->val.str, strlen(args[1]->val.str), |
| 3830 | LY_TYPE_OPTS_STORE, lys_resolve_prefix, (void *)sleaf->dflt_mod, set->format, |
| 3831 | (struct lyd_node *)leaf, set->tree, &data, NULL, &err); |
| 3832 | if (err) { |
| 3833 | ly_err_print(err); |
| 3834 | ly_err_free(err); |
| 3835 | } |
| 3836 | LY_CHECK_RET(rc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3837 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3838 | if (data.ident == leaf->value.ident) { |
| 3839 | set_fill_boolean(set, 1); |
| 3840 | break; |
| 3841 | } |
| 3842 | LY_ARRAY_FOR(data.ident->derived, u) { |
| 3843 | if (data.ident->derived[u] == leaf->value.ident) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3844 | set_fill_boolean(set, 1); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3845 | found = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3846 | break; |
| 3847 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3848 | } |
| 3849 | if (found) { |
| 3850 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3851 | } |
| 3852 | } |
| 3853 | } |
| 3854 | |
| 3855 | return LY_SUCCESS; |
| 3856 | } |
| 3857 | |
| 3858 | /** |
| 3859 | * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER |
| 3860 | * with the integer value of the first node's enum value, otherwise NaN. |
| 3861 | * |
| 3862 | * @param[in] args Array of arguments. |
| 3863 | * @param[in] arg_count Count of elements in @p args. |
| 3864 | * @param[in,out] set Context and result set at the same time. |
| 3865 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3866 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3867 | */ |
| 3868 | static LY_ERR |
| 3869 | xpath_enum_value(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3870 | { |
| 3871 | struct lyd_node_term *leaf; |
| 3872 | struct lysc_node_leaf *sleaf; |
| 3873 | LY_ERR rc = LY_SUCCESS; |
| 3874 | |
| 3875 | if (options & LYXP_SCNODE_ALL) { |
| 3876 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3877 | 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] | 3878 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3879 | 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] | 3880 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) { |
| 3881 | 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] | 3882 | } |
| 3883 | set_scnode_clear_ctx(set); |
| 3884 | return rc; |
| 3885 | } |
| 3886 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3887 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3888 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "enum-value(node-set)"); |
| 3889 | return LY_EVALID; |
| 3890 | } |
| 3891 | |
| 3892 | set_fill_number(set, NAN); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3893 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3894 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
| 3895 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 3896 | if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) { |
| 3897 | set_fill_number(set, leaf->value.enum_item->value); |
| 3898 | } |
| 3899 | } |
| 3900 | |
| 3901 | return LY_SUCCESS; |
| 3902 | } |
| 3903 | |
| 3904 | /** |
| 3905 | * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN |
| 3906 | * with false value. |
| 3907 | * |
| 3908 | * @param[in] args Array of arguments. |
| 3909 | * @param[in] arg_count Count of elements in @p args. |
| 3910 | * @param[in,out] set Context and result set at the same time. |
| 3911 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3912 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3913 | */ |
| 3914 | static LY_ERR |
| 3915 | xpath_false(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3916 | { |
| 3917 | if (options & LYXP_SCNODE_ALL) { |
| 3918 | set_scnode_clear_ctx(set); |
| 3919 | return LY_SUCCESS; |
| 3920 | } |
| 3921 | |
| 3922 | set_fill_boolean(set, 0); |
| 3923 | return LY_SUCCESS; |
| 3924 | } |
| 3925 | |
| 3926 | /** |
| 3927 | * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER |
| 3928 | * with the first argument floored (truncated). |
| 3929 | * |
| 3930 | * @param[in] args Array of arguments. |
| 3931 | * @param[in] arg_count Count of elements in @p args. |
| 3932 | * @param[in,out] set Context and result set at the same time. |
| 3933 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3934 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3935 | */ |
| 3936 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3937 | 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] | 3938 | { |
| 3939 | LY_ERR rc; |
| 3940 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3941 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3942 | LY_CHECK_RET(rc); |
| 3943 | if (isfinite(args[0]->val.num)) { |
| 3944 | set_fill_number(set, (long long)args[0]->val.num); |
| 3945 | } |
| 3946 | |
| 3947 | return LY_SUCCESS; |
| 3948 | } |
| 3949 | |
| 3950 | /** |
| 3951 | * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN |
| 3952 | * whether the language of the text matches the one from the argument. |
| 3953 | * |
| 3954 | * @param[in] args Array of arguments. |
| 3955 | * @param[in] arg_count Count of elements in @p args. |
| 3956 | * @param[in,out] set Context and result set at the same time. |
| 3957 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3958 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3959 | */ |
| 3960 | static LY_ERR |
| 3961 | xpath_lang(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3962 | { |
| 3963 | const struct lyd_node *node; |
| 3964 | struct lysc_node_leaf *sleaf; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 3965 | struct lyd_meta *meta = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3966 | const char *val; |
| 3967 | int i, dynamic; |
| 3968 | LY_ERR rc = LY_SUCCESS; |
| 3969 | |
| 3970 | if (options & LYXP_SCNODE_ALL) { |
| 3971 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3972 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3973 | 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] | 3974 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3975 | 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] | 3976 | } |
| 3977 | } |
| 3978 | set_scnode_clear_ctx(set); |
| 3979 | return rc; |
| 3980 | } |
| 3981 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3982 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3983 | LY_CHECK_RET(rc); |
| 3984 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3985 | if (set->type != LYXP_SET_NODE_SET) { |
| 3986 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "lang(string)"); |
| 3987 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3988 | } else if (!set->used) { |
| 3989 | set_fill_boolean(set, 0); |
| 3990 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3991 | } |
| 3992 | |
| 3993 | switch (set->val.nodes[0].type) { |
| 3994 | case LYXP_NODE_ELEM: |
| 3995 | case LYXP_NODE_TEXT: |
| 3996 | node = set->val.nodes[0].node; |
| 3997 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 3998 | case LYXP_NODE_META: |
| 3999 | node = set->val.meta[0].meta->parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4000 | break; |
| 4001 | default: |
| 4002 | /* nothing to do with roots */ |
| 4003 | set_fill_boolean(set, 0); |
| 4004 | return LY_SUCCESS; |
| 4005 | } |
| 4006 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4007 | /* find lang metadata */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4008 | for (; node; node = (struct lyd_node *)node->parent) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4009 | for (meta = node->meta; meta; meta = meta->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4010 | /* annotations */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4011 | 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] | 4012 | break; |
| 4013 | } |
| 4014 | } |
| 4015 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4016 | if (meta) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4017 | break; |
| 4018 | } |
| 4019 | } |
| 4020 | |
| 4021 | /* compare languages */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4022 | if (!meta) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4023 | set_fill_boolean(set, 0); |
| 4024 | } else { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4025 | val = lyd_meta2str(meta, &dynamic); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4026 | for (i = 0; args[0]->val.str[i]; ++i) { |
| 4027 | if (tolower(args[0]->val.str[i]) != tolower(val[i])) { |
| 4028 | set_fill_boolean(set, 0); |
| 4029 | break; |
| 4030 | } |
| 4031 | } |
| 4032 | if (!args[0]->val.str[i]) { |
| 4033 | if (!val[i] || (val[i] == '-')) { |
| 4034 | set_fill_boolean(set, 1); |
| 4035 | } else { |
| 4036 | set_fill_boolean(set, 0); |
| 4037 | } |
| 4038 | } |
| 4039 | if (dynamic) { |
| 4040 | free((char *)val); |
| 4041 | } |
| 4042 | } |
| 4043 | |
| 4044 | return LY_SUCCESS; |
| 4045 | } |
| 4046 | |
| 4047 | /** |
| 4048 | * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER |
| 4049 | * with the context size. |
| 4050 | * |
| 4051 | * @param[in] args Array of arguments. |
| 4052 | * @param[in] arg_count Count of elements in @p args. |
| 4053 | * @param[in,out] set Context and result set at the same time. |
| 4054 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4055 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4056 | */ |
| 4057 | static LY_ERR |
| 4058 | xpath_last(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 4059 | { |
| 4060 | if (options & LYXP_SCNODE_ALL) { |
| 4061 | set_scnode_clear_ctx(set); |
| 4062 | return LY_SUCCESS; |
| 4063 | } |
| 4064 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4065 | if (set->type != LYXP_SET_NODE_SET) { |
| 4066 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "last()"); |
| 4067 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4068 | } else if (!set->used) { |
| 4069 | set_fill_number(set, 0); |
| 4070 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4071 | } |
| 4072 | |
| 4073 | set_fill_number(set, set->ctx_size); |
| 4074 | return LY_SUCCESS; |
| 4075 | } |
| 4076 | |
| 4077 | /** |
| 4078 | * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING |
| 4079 | * with the node name without namespace from the argument or the context. |
| 4080 | * |
| 4081 | * @param[in] args Array of arguments. |
| 4082 | * @param[in] arg_count Count of elements in @p args. |
| 4083 | * @param[in,out] set Context and result set at the same time. |
| 4084 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4085 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4086 | */ |
| 4087 | static LY_ERR |
| 4088 | xpath_local_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options) |
| 4089 | { |
| 4090 | struct lyxp_set_node *item; |
| 4091 | /* suppress unused variable warning */ |
| 4092 | (void)options; |
| 4093 | |
| 4094 | if (options & LYXP_SCNODE_ALL) { |
| 4095 | set_scnode_clear_ctx(set); |
| 4096 | return LY_SUCCESS; |
| 4097 | } |
| 4098 | |
| 4099 | if (arg_count) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4100 | if (args[0]->type != LYXP_SET_NODE_SET) { |
| 4101 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "local-name(node-set?)"); |
| 4102 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4103 | } else if (!args[0]->used) { |
| 4104 | set_fill_string(set, "", 0); |
| 4105 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4106 | } |
| 4107 | |
| 4108 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4109 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4110 | |
| 4111 | item = &args[0]->val.nodes[0]; |
| 4112 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4113 | if (set->type != LYXP_SET_NODE_SET) { |
| 4114 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "local-name(node-set?)"); |
| 4115 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4116 | } else if (!set->used) { |
| 4117 | set_fill_string(set, "", 0); |
| 4118 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4119 | } |
| 4120 | |
| 4121 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4122 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4123 | |
| 4124 | item = &set->val.nodes[0]; |
| 4125 | } |
| 4126 | |
| 4127 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4128 | case LYXP_NODE_NONE: |
| 4129 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4130 | case LYXP_NODE_ROOT: |
| 4131 | case LYXP_NODE_ROOT_CONFIG: |
| 4132 | case LYXP_NODE_TEXT: |
| 4133 | set_fill_string(set, "", 0); |
| 4134 | break; |
| 4135 | case LYXP_NODE_ELEM: |
| 4136 | set_fill_string(set, item->node->schema->name, strlen(item->node->schema->name)); |
| 4137 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4138 | case LYXP_NODE_META: |
| 4139 | 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] | 4140 | break; |
| 4141 | } |
| 4142 | |
| 4143 | return LY_SUCCESS; |
| 4144 | } |
| 4145 | |
| 4146 | /** |
| 4147 | * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING |
| 4148 | * with the node name fully qualified (with namespace) from the argument or the context. |
| 4149 | * |
| 4150 | * @param[in] args Array of arguments. |
| 4151 | * @param[in] arg_count Count of elements in @p args. |
| 4152 | * @param[in,out] set Context and result set at the same time. |
| 4153 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4154 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4155 | */ |
| 4156 | static LY_ERR |
| 4157 | xpath_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options) |
| 4158 | { |
| 4159 | struct lyxp_set_node *item; |
| 4160 | struct lys_module *mod; |
| 4161 | char *str; |
| 4162 | const char *name; |
| 4163 | int rc; |
| 4164 | |
| 4165 | if (options & LYXP_SCNODE_ALL) { |
| 4166 | set_scnode_clear_ctx(set); |
| 4167 | return LY_SUCCESS; |
| 4168 | } |
| 4169 | |
| 4170 | if (arg_count) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4171 | if (args[0]->type != LYXP_SET_NODE_SET) { |
| 4172 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "name(node-set?)"); |
| 4173 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4174 | } else if (!args[0]->used) { |
| 4175 | set_fill_string(set, "", 0); |
| 4176 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4177 | } |
| 4178 | |
| 4179 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4180 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4181 | |
| 4182 | item = &args[0]->val.nodes[0]; |
| 4183 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4184 | if (set->type != LYXP_SET_NODE_SET) { |
| 4185 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "name(node-set?)"); |
| 4186 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4187 | } else if (!set->used) { |
| 4188 | set_fill_string(set, "", 0); |
| 4189 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4190 | } |
| 4191 | |
| 4192 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4193 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4194 | |
| 4195 | item = &set->val.nodes[0]; |
| 4196 | } |
| 4197 | |
| 4198 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4199 | case LYXP_NODE_NONE: |
| 4200 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4201 | case LYXP_NODE_ROOT: |
| 4202 | case LYXP_NODE_ROOT_CONFIG: |
| 4203 | case LYXP_NODE_TEXT: |
| 4204 | mod = NULL; |
| 4205 | name = NULL; |
| 4206 | break; |
| 4207 | case LYXP_NODE_ELEM: |
| 4208 | mod = item->node->schema->module; |
| 4209 | name = item->node->schema->name; |
| 4210 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4211 | case LYXP_NODE_META: |
| 4212 | mod = ((struct lyd_meta *)item->node)->annotation->module; |
| 4213 | name = ((struct lyd_meta *)item->node)->name; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4214 | break; |
| 4215 | } |
| 4216 | |
| 4217 | if (mod && name) { |
| 4218 | switch (set->format) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 4219 | case LYD_SCHEMA: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4220 | rc = asprintf(&str, "%s:%s", lys_prefix_find_module(set->local_mod, mod), name); |
| 4221 | break; |
| 4222 | case LYD_JSON: |
| 4223 | rc = asprintf(&str, "%s:%s", mod->name, name); |
| 4224 | break; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 4225 | case LYD_XML: |
Michal Vasko | 9409ef6 | 2019-09-12 11:47:17 +0200 | [diff] [blame] | 4226 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4227 | } |
| 4228 | LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM); |
| 4229 | set_fill_string(set, str, strlen(str)); |
| 4230 | free(str); |
| 4231 | } else { |
| 4232 | set_fill_string(set, "", 0); |
| 4233 | } |
| 4234 | |
| 4235 | return LY_SUCCESS; |
| 4236 | } |
| 4237 | |
| 4238 | /** |
| 4239 | * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING |
| 4240 | * with the namespace of the node from the argument or the context. |
| 4241 | * |
| 4242 | * @param[in] args Array of arguments. |
| 4243 | * @param[in] arg_count Count of elements in @p args. |
| 4244 | * @param[in,out] set Context and result set at the same time. |
| 4245 | * @param[in] options XPath options. |
| 4246 | * @return LY_ERR (LY_EINVAL for wrong arguments on schema) |
| 4247 | */ |
| 4248 | static LY_ERR |
| 4249 | xpath_namespace_uri(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options) |
| 4250 | { |
| 4251 | struct lyxp_set_node *item; |
| 4252 | struct lys_module *mod; |
| 4253 | /* suppress unused variable warning */ |
| 4254 | (void)options; |
| 4255 | |
| 4256 | if (options & LYXP_SCNODE_ALL) { |
| 4257 | set_scnode_clear_ctx(set); |
| 4258 | return LY_SUCCESS; |
| 4259 | } |
| 4260 | |
| 4261 | if (arg_count) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4262 | if (args[0]->type != LYXP_SET_NODE_SET) { |
| 4263 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), |
| 4264 | "namespace-uri(node-set?)"); |
| 4265 | return LY_EVALID; |
| 4266 | } else if (!args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4267 | set_fill_string(set, "", 0); |
| 4268 | return LY_SUCCESS; |
| 4269 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4270 | |
| 4271 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4272 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4273 | |
| 4274 | item = &args[0]->val.nodes[0]; |
| 4275 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4276 | if (set->type != LYXP_SET_NODE_SET) { |
| 4277 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "namespace-uri(node-set?)"); |
| 4278 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4279 | } else if (!set->used) { |
| 4280 | set_fill_string(set, "", 0); |
| 4281 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4282 | } |
| 4283 | |
| 4284 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4285 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4286 | |
| 4287 | item = &set->val.nodes[0]; |
| 4288 | } |
| 4289 | |
| 4290 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4291 | case LYXP_NODE_NONE: |
| 4292 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4293 | case LYXP_NODE_ROOT: |
| 4294 | case LYXP_NODE_ROOT_CONFIG: |
| 4295 | case LYXP_NODE_TEXT: |
| 4296 | set_fill_string(set, "", 0); |
| 4297 | break; |
| 4298 | case LYXP_NODE_ELEM: |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4299 | case LYXP_NODE_META: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4300 | if (item->type == LYXP_NODE_ELEM) { |
| 4301 | mod = item->node->schema->module; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4302 | } else { /* LYXP_NODE_META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4303 | /* annotations */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4304 | mod = ((struct lyd_meta *)item->node)->annotation->module; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4305 | } |
| 4306 | |
| 4307 | set_fill_string(set, mod->ns, strlen(mod->ns)); |
| 4308 | break; |
| 4309 | } |
| 4310 | |
| 4311 | return LY_SUCCESS; |
| 4312 | } |
| 4313 | |
| 4314 | /** |
| 4315 | * @brief Execute the XPath node() function (node type). Returns LYXP_SET_NODE_SET |
| 4316 | * with only nodes from the context. In practice it either leaves the context |
| 4317 | * as it is or returns an empty node set. |
| 4318 | * |
| 4319 | * @param[in] args Array of arguments. |
| 4320 | * @param[in] arg_count Count of elements in @p args. |
| 4321 | * @param[in,out] set Context and result set at the same time. |
| 4322 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4323 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4324 | */ |
| 4325 | static LY_ERR |
| 4326 | xpath_node(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 4327 | { |
| 4328 | if (options & LYXP_SCNODE_ALL) { |
| 4329 | set_scnode_clear_ctx(set); |
| 4330 | return LY_SUCCESS; |
| 4331 | } |
| 4332 | |
| 4333 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4334 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4335 | } |
| 4336 | return LY_SUCCESS; |
| 4337 | } |
| 4338 | |
| 4339 | /** |
| 4340 | * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING |
| 4341 | * with normalized value (no leading, trailing, double white spaces) of the node |
| 4342 | * from the argument or the context. |
| 4343 | * |
| 4344 | * @param[in] args Array of arguments. |
| 4345 | * @param[in] arg_count Count of elements in @p args. |
| 4346 | * @param[in,out] set Context and result set at the same time. |
| 4347 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4348 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4349 | */ |
| 4350 | static LY_ERR |
| 4351 | xpath_normalize_space(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options) |
| 4352 | { |
| 4353 | uint16_t i, new_used; |
| 4354 | char *new; |
| 4355 | int have_spaces = 0, space_before = 0; |
| 4356 | struct lysc_node_leaf *sleaf; |
| 4357 | LY_ERR rc = LY_SUCCESS; |
| 4358 | |
| 4359 | if (options & LYXP_SCNODE_ALL) { |
| 4360 | if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4361 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4362 | 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] | 4363 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4364 | 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] | 4365 | } |
| 4366 | } |
| 4367 | set_scnode_clear_ctx(set); |
| 4368 | return rc; |
| 4369 | } |
| 4370 | |
| 4371 | if (arg_count) { |
| 4372 | set_fill_set(set, args[0]); |
| 4373 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4374 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4375 | LY_CHECK_RET(rc); |
| 4376 | |
| 4377 | /* is there any normalization necessary? */ |
| 4378 | for (i = 0; set->val.str[i]; ++i) { |
| 4379 | if (is_xmlws(set->val.str[i])) { |
| 4380 | if ((i == 0) || space_before || (!set->val.str[i + 1])) { |
| 4381 | have_spaces = 1; |
| 4382 | break; |
| 4383 | } |
| 4384 | space_before = 1; |
| 4385 | } else { |
| 4386 | space_before = 0; |
| 4387 | } |
| 4388 | } |
| 4389 | |
| 4390 | /* yep, there is */ |
| 4391 | if (have_spaces) { |
| 4392 | /* it's enough, at least one character will go, makes space for ending '\0' */ |
| 4393 | new = malloc(strlen(set->val.str) * sizeof(char)); |
| 4394 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 4395 | new_used = 0; |
| 4396 | |
| 4397 | space_before = 0; |
| 4398 | for (i = 0; set->val.str[i]; ++i) { |
| 4399 | if (is_xmlws(set->val.str[i])) { |
| 4400 | if ((i == 0) || space_before) { |
| 4401 | space_before = 1; |
| 4402 | continue; |
| 4403 | } else { |
| 4404 | space_before = 1; |
| 4405 | } |
| 4406 | } else { |
| 4407 | space_before = 0; |
| 4408 | } |
| 4409 | |
| 4410 | new[new_used] = (space_before ? ' ' : set->val.str[i]); |
| 4411 | ++new_used; |
| 4412 | } |
| 4413 | |
| 4414 | /* at worst there is one trailing space now */ |
| 4415 | if (new_used && is_xmlws(new[new_used - 1])) { |
| 4416 | --new_used; |
| 4417 | } |
| 4418 | |
| 4419 | new = ly_realloc(new, (new_used + 1) * sizeof(char)); |
| 4420 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 4421 | new[new_used] = '\0'; |
| 4422 | |
| 4423 | free(set->val.str); |
| 4424 | set->val.str = new; |
| 4425 | } |
| 4426 | |
| 4427 | return LY_SUCCESS; |
| 4428 | } |
| 4429 | |
| 4430 | /** |
| 4431 | * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN |
| 4432 | * with the argument converted to boolean and logically inverted. |
| 4433 | * |
| 4434 | * @param[in] args Array of arguments. |
| 4435 | * @param[in] arg_count Count of elements in @p args. |
| 4436 | * @param[in,out] set Context and result set at the same time. |
| 4437 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4438 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4439 | */ |
| 4440 | static LY_ERR |
| 4441 | xpath_not(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 4442 | { |
| 4443 | if (options & LYXP_SCNODE_ALL) { |
| 4444 | set_scnode_clear_ctx(set); |
| 4445 | return LY_SUCCESS; |
| 4446 | } |
| 4447 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4448 | lyxp_set_cast(args[0], LYXP_SET_BOOLEAN); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4449 | if (args[0]->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4450 | set_fill_boolean(set, 0); |
| 4451 | } else { |
| 4452 | set_fill_boolean(set, 1); |
| 4453 | } |
| 4454 | |
| 4455 | return LY_SUCCESS; |
| 4456 | } |
| 4457 | |
| 4458 | /** |
| 4459 | * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER |
| 4460 | * with the number representation of either the argument or the context. |
| 4461 | * |
| 4462 | * @param[in] args Array of arguments. |
| 4463 | * @param[in] arg_count Count of elements in @p args. |
| 4464 | * @param[in,out] set Context and result set at the same time. |
| 4465 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4466 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4467 | */ |
| 4468 | static LY_ERR |
| 4469 | xpath_number(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options) |
| 4470 | { |
| 4471 | LY_ERR rc; |
| 4472 | |
| 4473 | if (options & LYXP_SCNODE_ALL) { |
| 4474 | set_scnode_clear_ctx(set); |
| 4475 | return LY_SUCCESS; |
| 4476 | } |
| 4477 | |
| 4478 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4479 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4480 | LY_CHECK_RET(rc); |
| 4481 | set_fill_set(set, args[0]); |
| 4482 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4483 | rc = lyxp_set_cast(set, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4484 | LY_CHECK_RET(rc); |
| 4485 | } |
| 4486 | |
| 4487 | return LY_SUCCESS; |
| 4488 | } |
| 4489 | |
| 4490 | /** |
| 4491 | * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER |
| 4492 | * with the context position. |
| 4493 | * |
| 4494 | * @param[in] args Array of arguments. |
| 4495 | * @param[in] arg_count Count of elements in @p args. |
| 4496 | * @param[in,out] set Context and result set at the same time. |
| 4497 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4498 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4499 | */ |
| 4500 | static LY_ERR |
| 4501 | xpath_position(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 4502 | { |
| 4503 | if (options & LYXP_SCNODE_ALL) { |
| 4504 | set_scnode_clear_ctx(set); |
| 4505 | return LY_SUCCESS; |
| 4506 | } |
| 4507 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4508 | if (set->type != LYXP_SET_NODE_SET) { |
| 4509 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "position()"); |
| 4510 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4511 | } else if (!set->used) { |
| 4512 | set_fill_number(set, 0); |
| 4513 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4514 | } |
| 4515 | |
| 4516 | set_fill_number(set, set->ctx_pos); |
| 4517 | |
| 4518 | /* UNUSED in 'Release' build type */ |
| 4519 | (void)options; |
| 4520 | return LY_SUCCESS; |
| 4521 | } |
| 4522 | |
| 4523 | /** |
| 4524 | * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN |
| 4525 | * depending on whether the second argument regex matches the first argument string. For details refer to |
| 4526 | * YANG 1.1 RFC section 10.2.1. |
| 4527 | * |
| 4528 | * @param[in] args Array of arguments. |
| 4529 | * @param[in] arg_count Count of elements in @p args. |
| 4530 | * @param[in,out] set Context and result set at the same time. |
| 4531 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4532 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4533 | */ |
| 4534 | static LY_ERR |
| 4535 | xpath_re_match(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 4536 | { |
| 4537 | struct lysc_pattern **patterns = NULL, **pattern; |
| 4538 | struct lysc_node_leaf *sleaf; |
| 4539 | char *path; |
| 4540 | LY_ERR rc = LY_SUCCESS; |
| 4541 | struct ly_err_item *err; |
| 4542 | |
| 4543 | if (options & LYXP_SCNODE_ALL) { |
| 4544 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4545 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4546 | 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] | 4547 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4548 | 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] | 4549 | } |
| 4550 | } |
| 4551 | |
| 4552 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4553 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4554 | 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] | 4555 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4556 | 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] | 4557 | } |
| 4558 | } |
| 4559 | set_scnode_clear_ctx(set); |
| 4560 | return rc; |
| 4561 | } |
| 4562 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4563 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4564 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4565 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4566 | LY_CHECK_RET(rc); |
| 4567 | |
| 4568 | LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM); |
| 4569 | *pattern = malloc(sizeof **pattern); |
| 4570 | path = lyd_path(set->ctx_node, LYD_PATH_LOG, NULL, 0); |
| 4571 | rc = lys_compile_type_pattern_check(set->ctx, path, args[1]->val.str, &(*pattern)->code); |
| 4572 | free(path); |
| 4573 | if (rc != LY_SUCCESS) { |
| 4574 | LY_ARRAY_FREE(patterns); |
| 4575 | return rc; |
| 4576 | } |
| 4577 | |
| 4578 | rc = ly_type_validate_patterns(patterns, args[0]->val.str, strlen(args[0]->val.str), &err); |
| 4579 | pcre2_code_free((*pattern)->code); |
| 4580 | free(*pattern); |
| 4581 | LY_ARRAY_FREE(patterns); |
| 4582 | if (rc && (rc != LY_EVALID)) { |
| 4583 | ly_err_print(err); |
| 4584 | ly_err_free(err); |
| 4585 | return rc; |
| 4586 | } |
| 4587 | |
| 4588 | if (rc == LY_EVALID) { |
| 4589 | ly_err_free(err); |
| 4590 | set_fill_boolean(set, 0); |
| 4591 | } else { |
| 4592 | set_fill_boolean(set, 1); |
| 4593 | } |
| 4594 | |
| 4595 | return LY_SUCCESS; |
| 4596 | } |
| 4597 | |
| 4598 | /** |
| 4599 | * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER |
| 4600 | * with the rounded first argument. For details refer to |
| 4601 | * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round. |
| 4602 | * |
| 4603 | * @param[in] args Array of arguments. |
| 4604 | * @param[in] arg_count Count of elements in @p args. |
| 4605 | * @param[in,out] set Context and result set at the same time. |
| 4606 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4607 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4608 | */ |
| 4609 | static LY_ERR |
| 4610 | xpath_round(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 4611 | { |
| 4612 | struct lysc_node_leaf *sleaf; |
| 4613 | LY_ERR rc = LY_SUCCESS; |
| 4614 | |
| 4615 | if (options & LYXP_SCNODE_ALL) { |
| 4616 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4617 | 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] | 4618 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4619 | 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] | 4620 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) { |
| 4621 | 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] | 4622 | } |
| 4623 | set_scnode_clear_ctx(set); |
| 4624 | return rc; |
| 4625 | } |
| 4626 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4627 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4628 | LY_CHECK_RET(rc); |
| 4629 | |
| 4630 | /* cover only the cases where floor can't be used */ |
| 4631 | if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) { |
| 4632 | set_fill_number(set, -0.0f); |
| 4633 | } else { |
| 4634 | args[0]->val.num += 0.5; |
| 4635 | rc = xpath_floor(args, 1, args[0], options); |
| 4636 | LY_CHECK_RET(rc); |
| 4637 | set_fill_number(set, args[0]->val.num); |
| 4638 | } |
| 4639 | |
| 4640 | return LY_SUCCESS; |
| 4641 | } |
| 4642 | |
| 4643 | /** |
| 4644 | * @brief Execute the XPath starts-with(string, string) function. |
| 4645 | * Returns LYXP_SET_BOOLEAN whether the second argument is |
| 4646 | * the prefix of the first or not. |
| 4647 | * |
| 4648 | * @param[in] args Array of arguments. |
| 4649 | * @param[in] arg_count Count of elements in @p args. |
| 4650 | * @param[in,out] set Context and result set at the same time. |
| 4651 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4652 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4653 | */ |
| 4654 | static LY_ERR |
| 4655 | xpath_starts_with(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 4656 | { |
| 4657 | struct lysc_node_leaf *sleaf; |
| 4658 | LY_ERR rc = LY_SUCCESS; |
| 4659 | |
| 4660 | if (options & LYXP_SCNODE_ALL) { |
| 4661 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4662 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4663 | 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] | 4664 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4665 | 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] | 4666 | } |
| 4667 | } |
| 4668 | |
| 4669 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4670 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4671 | 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] | 4672 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4673 | 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] | 4674 | } |
| 4675 | } |
| 4676 | set_scnode_clear_ctx(set); |
| 4677 | return rc; |
| 4678 | } |
| 4679 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4680 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4681 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4682 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4683 | LY_CHECK_RET(rc); |
| 4684 | |
| 4685 | if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) { |
| 4686 | set_fill_boolean(set, 0); |
| 4687 | } else { |
| 4688 | set_fill_boolean(set, 1); |
| 4689 | } |
| 4690 | |
| 4691 | return LY_SUCCESS; |
| 4692 | } |
| 4693 | |
| 4694 | /** |
| 4695 | * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING |
| 4696 | * with the string representation of either the argument or the context. |
| 4697 | * |
| 4698 | * @param[in] args Array of arguments. |
| 4699 | * @param[in] arg_count Count of elements in @p args. |
| 4700 | * @param[in,out] set Context and result set at the same time. |
| 4701 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4702 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4703 | */ |
| 4704 | static LY_ERR |
| 4705 | xpath_string(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options) |
| 4706 | { |
| 4707 | LY_ERR rc; |
| 4708 | |
| 4709 | if (options & LYXP_SCNODE_ALL) { |
| 4710 | set_scnode_clear_ctx(set); |
| 4711 | return LY_SUCCESS; |
| 4712 | } |
| 4713 | |
| 4714 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4715 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4716 | LY_CHECK_RET(rc); |
| 4717 | set_fill_set(set, args[0]); |
| 4718 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4719 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4720 | LY_CHECK_RET(rc); |
| 4721 | } |
| 4722 | |
| 4723 | return LY_SUCCESS; |
| 4724 | } |
| 4725 | |
| 4726 | /** |
| 4727 | * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER |
| 4728 | * with the length of the string in either the argument or the context. |
| 4729 | * |
| 4730 | * @param[in] args Array of arguments. |
| 4731 | * @param[in] arg_count Count of elements in @p args. |
| 4732 | * @param[in,out] set Context and result set at the same time. |
| 4733 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4734 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4735 | */ |
| 4736 | static LY_ERR |
| 4737 | xpath_string_length(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options) |
| 4738 | { |
| 4739 | struct lysc_node_leaf *sleaf; |
| 4740 | LY_ERR rc = LY_SUCCESS; |
| 4741 | |
| 4742 | if (options & LYXP_SCNODE_ALL) { |
| 4743 | if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4744 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4745 | 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] | 4746 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4747 | 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] | 4748 | } |
| 4749 | } |
| 4750 | if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) { |
| 4751 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4752 | 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] | 4753 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4754 | 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] | 4755 | } |
| 4756 | } |
| 4757 | set_scnode_clear_ctx(set); |
| 4758 | return rc; |
| 4759 | } |
| 4760 | |
| 4761 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4762 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4763 | LY_CHECK_RET(rc); |
| 4764 | set_fill_number(set, strlen(args[0]->val.str)); |
| 4765 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4766 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4767 | LY_CHECK_RET(rc); |
| 4768 | set_fill_number(set, strlen(set->val.str)); |
| 4769 | } |
| 4770 | |
| 4771 | return LY_SUCCESS; |
| 4772 | } |
| 4773 | |
| 4774 | /** |
| 4775 | * @brief Execute the XPath substring(string, number, number?) function. |
| 4776 | * Returns LYXP_SET_STRING substring of the first argument starting |
| 4777 | * on the second argument index ending on the third argument index, |
| 4778 | * indexed from 1. For exact definition refer to |
| 4779 | * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring. |
| 4780 | * |
| 4781 | * @param[in] args Array of arguments. |
| 4782 | * @param[in] arg_count Count of elements in @p args. |
| 4783 | * @param[in,out] set Context and result set at the same time. |
| 4784 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4785 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4786 | */ |
| 4787 | static LY_ERR |
| 4788 | xpath_substring(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options) |
| 4789 | { |
| 4790 | int start, len; |
| 4791 | uint16_t str_start, str_len, pos; |
| 4792 | struct lysc_node_leaf *sleaf; |
| 4793 | LY_ERR rc = LY_SUCCESS; |
| 4794 | |
| 4795 | if (options & LYXP_SCNODE_ALL) { |
| 4796 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4797 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4798 | 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] | 4799 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4800 | 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] | 4801 | } |
| 4802 | } |
| 4803 | |
| 4804 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4805 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4806 | 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] | 4807 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 4808 | 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] | 4809 | } |
| 4810 | } |
| 4811 | |
| 4812 | if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET) |
| 4813 | && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) { |
| 4814 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4815 | 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] | 4816 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 4817 | 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] | 4818 | } |
| 4819 | } |
| 4820 | set_scnode_clear_ctx(set); |
| 4821 | return rc; |
| 4822 | } |
| 4823 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4824 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4825 | LY_CHECK_RET(rc); |
| 4826 | |
| 4827 | /* start */ |
| 4828 | if (xpath_round(&args[1], 1, args[1], options)) { |
| 4829 | return -1; |
| 4830 | } |
| 4831 | if (isfinite(args[1]->val.num)) { |
| 4832 | start = args[1]->val.num - 1; |
| 4833 | } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) { |
| 4834 | start = INT_MIN; |
| 4835 | } else { |
| 4836 | start = INT_MAX; |
| 4837 | } |
| 4838 | |
| 4839 | /* len */ |
| 4840 | if (arg_count == 3) { |
| 4841 | rc = xpath_round(&args[2], 1, args[2], options); |
| 4842 | LY_CHECK_RET(rc); |
| 4843 | if (isfinite(args[2]->val.num)) { |
| 4844 | len = args[2]->val.num; |
| 4845 | } else if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) { |
| 4846 | len = 0; |
| 4847 | } else { |
| 4848 | len = INT_MAX; |
| 4849 | } |
| 4850 | } else { |
| 4851 | len = INT_MAX; |
| 4852 | } |
| 4853 | |
| 4854 | /* find matching character positions */ |
| 4855 | str_start = 0; |
| 4856 | str_len = 0; |
| 4857 | for (pos = 0; args[0]->val.str[pos]; ++pos) { |
| 4858 | if (pos < start) { |
| 4859 | ++str_start; |
| 4860 | } else if (pos < start + len) { |
| 4861 | ++str_len; |
| 4862 | } else { |
| 4863 | break; |
| 4864 | } |
| 4865 | } |
| 4866 | |
| 4867 | set_fill_string(set, args[0]->val.str + str_start, str_len); |
| 4868 | return LY_SUCCESS; |
| 4869 | } |
| 4870 | |
| 4871 | /** |
| 4872 | * @brief Execute the XPath substring-after(string, string) function. |
| 4873 | * Returns LYXP_SET_STRING with the string succeeding the occurance |
| 4874 | * of the second argument in the first or an empty string. |
| 4875 | * |
| 4876 | * @param[in] args Array of arguments. |
| 4877 | * @param[in] arg_count Count of elements in @p args. |
| 4878 | * @param[in,out] set Context and result set at the same time. |
| 4879 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4880 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4881 | */ |
| 4882 | static LY_ERR |
| 4883 | xpath_substring_after(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 4884 | { |
| 4885 | char *ptr; |
| 4886 | struct lysc_node_leaf *sleaf; |
| 4887 | LY_ERR rc = LY_SUCCESS; |
| 4888 | |
| 4889 | if (options & LYXP_SCNODE_ALL) { |
| 4890 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4891 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4892 | 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] | 4893 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4894 | 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] | 4895 | } |
| 4896 | } |
| 4897 | |
| 4898 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4899 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4900 | 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] | 4901 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4902 | 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] | 4903 | } |
| 4904 | } |
| 4905 | set_scnode_clear_ctx(set); |
| 4906 | return rc; |
| 4907 | } |
| 4908 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4909 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4910 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4911 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4912 | LY_CHECK_RET(rc); |
| 4913 | |
| 4914 | ptr = strstr(args[0]->val.str, args[1]->val.str); |
| 4915 | if (ptr) { |
| 4916 | set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str))); |
| 4917 | } else { |
| 4918 | set_fill_string(set, "", 0); |
| 4919 | } |
| 4920 | |
| 4921 | return LY_SUCCESS; |
| 4922 | } |
| 4923 | |
| 4924 | /** |
| 4925 | * @brief Execute the XPath substring-before(string, string) function. |
| 4926 | * Returns LYXP_SET_STRING with the string preceding the occurance |
| 4927 | * of the second argument in the first or an empty string. |
| 4928 | * |
| 4929 | * @param[in] args Array of arguments. |
| 4930 | * @param[in] arg_count Count of elements in @p args. |
| 4931 | * @param[in,out] set Context and result set at the same time. |
| 4932 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4933 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4934 | */ |
| 4935 | static LY_ERR |
| 4936 | xpath_substring_before(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 4937 | { |
| 4938 | char *ptr; |
| 4939 | struct lysc_node_leaf *sleaf; |
| 4940 | LY_ERR rc = LY_SUCCESS; |
| 4941 | |
| 4942 | if (options & LYXP_SCNODE_ALL) { |
| 4943 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4944 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4945 | 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] | 4946 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4947 | 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] | 4948 | } |
| 4949 | } |
| 4950 | |
| 4951 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4952 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4953 | 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] | 4954 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4955 | 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] | 4956 | } |
| 4957 | } |
| 4958 | set_scnode_clear_ctx(set); |
| 4959 | return rc; |
| 4960 | } |
| 4961 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4962 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4963 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4964 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4965 | LY_CHECK_RET(rc); |
| 4966 | |
| 4967 | ptr = strstr(args[0]->val.str, args[1]->val.str); |
| 4968 | if (ptr) { |
| 4969 | set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str); |
| 4970 | } else { |
| 4971 | set_fill_string(set, "", 0); |
| 4972 | } |
| 4973 | |
| 4974 | return LY_SUCCESS; |
| 4975 | } |
| 4976 | |
| 4977 | /** |
| 4978 | * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER |
| 4979 | * with the sum of all the nodes in the context. |
| 4980 | * |
| 4981 | * @param[in] args Array of arguments. |
| 4982 | * @param[in] arg_count Count of elements in @p args. |
| 4983 | * @param[in,out] set Context and result set at the same time. |
| 4984 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4985 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4986 | */ |
| 4987 | static LY_ERR |
| 4988 | xpath_sum(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 4989 | { |
| 4990 | long double num; |
| 4991 | char *str; |
| 4992 | uint16_t i; |
| 4993 | struct lyxp_set set_item; |
| 4994 | struct lysc_node_leaf *sleaf; |
| 4995 | LY_ERR rc = LY_SUCCESS; |
| 4996 | |
| 4997 | if (options & LYXP_SCNODE_ALL) { |
| 4998 | if (args[0]->type == LYXP_SET_SCNODE_SET) { |
| 4999 | for (i = 0; i < args[0]->used; ++i) { |
| 5000 | if (args[0]->val.scnodes[i].in_ctx == 1) { |
| 5001 | sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode; |
| 5002 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5003 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, |
| 5004 | lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5005 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 5006 | 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] | 5007 | } |
| 5008 | } |
| 5009 | } |
| 5010 | } |
| 5011 | set_scnode_clear_ctx(set); |
| 5012 | return rc; |
| 5013 | } |
| 5014 | |
| 5015 | set_fill_number(set, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5016 | |
| 5017 | if (args[0]->type != LYXP_SET_NODE_SET) { |
| 5018 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "sum(node-set)"); |
| 5019 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5020 | } else if (!args[0]->used) { |
| 5021 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5022 | } |
| 5023 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5024 | set_init(&set_item, set); |
| 5025 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5026 | set_item.type = LYXP_SET_NODE_SET; |
| 5027 | set_item.val.nodes = malloc(sizeof *set_item.val.nodes); |
| 5028 | LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM); |
| 5029 | |
| 5030 | set_item.used = 1; |
| 5031 | set_item.size = 1; |
| 5032 | |
| 5033 | for (i = 0; i < args[0]->used; ++i) { |
| 5034 | set_item.val.nodes[0] = args[0]->val.nodes[i]; |
| 5035 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5036 | rc = cast_node_set_to_string(&set_item, &str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5037 | LY_CHECK_RET(rc); |
| 5038 | num = cast_string_to_number(str); |
| 5039 | free(str); |
| 5040 | set->val.num += num; |
| 5041 | } |
| 5042 | |
| 5043 | free(set_item.val.nodes); |
| 5044 | |
| 5045 | return LY_SUCCESS; |
| 5046 | } |
| 5047 | |
| 5048 | /** |
| 5049 | * @brief Execute the XPath text() function (node type). Returns LYXP_SET_NODE_SET |
| 5050 | * with the text content of the nodes in the context. |
| 5051 | * |
| 5052 | * @param[in] args Array of arguments. |
| 5053 | * @param[in] arg_count Count of elements in @p args. |
| 5054 | * @param[in,out] set Context and result set at the same time. |
| 5055 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5056 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5057 | */ |
| 5058 | static LY_ERR |
| 5059 | xpath_text(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 5060 | { |
| 5061 | uint32_t i; |
| 5062 | |
| 5063 | if (options & LYXP_SCNODE_ALL) { |
| 5064 | set_scnode_clear_ctx(set); |
| 5065 | return LY_SUCCESS; |
| 5066 | } |
| 5067 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5068 | if (set->type != LYXP_SET_NODE_SET) { |
| 5069 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "text()"); |
| 5070 | return LY_EVALID; |
| 5071 | } |
| 5072 | |
| 5073 | for (i = 0; i < set->used;) { |
| 5074 | switch (set->val.nodes[i].type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 5075 | case LYXP_NODE_NONE: |
| 5076 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5077 | case LYXP_NODE_ELEM: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5078 | if (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 5079 | set->val.nodes[i].type = LYXP_NODE_TEXT; |
| 5080 | ++i; |
| 5081 | break; |
| 5082 | } |
| 5083 | /* fall through */ |
| 5084 | case LYXP_NODE_ROOT: |
| 5085 | case LYXP_NODE_ROOT_CONFIG: |
| 5086 | case LYXP_NODE_TEXT: |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5087 | case LYXP_NODE_META: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5088 | set_remove_node(set, i); |
| 5089 | break; |
| 5090 | } |
| 5091 | } |
| 5092 | |
| 5093 | return LY_SUCCESS; |
| 5094 | } |
| 5095 | |
| 5096 | /** |
| 5097 | * @brief Execute the XPath translate(string, string, string) function. |
| 5098 | * Returns LYXP_SET_STRING with the first argument with the characters |
| 5099 | * from the second argument replaced by those on the corresponding |
| 5100 | * positions in the third argument. |
| 5101 | * |
| 5102 | * @param[in] args Array of arguments. |
| 5103 | * @param[in] arg_count Count of elements in @p args. |
| 5104 | * @param[in,out] set Context and result set at the same time. |
| 5105 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5106 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5107 | */ |
| 5108 | static LY_ERR |
| 5109 | xpath_translate(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 5110 | { |
| 5111 | uint16_t i, j, new_used; |
| 5112 | char *new; |
| 5113 | int found, have_removed; |
| 5114 | struct lysc_node_leaf *sleaf; |
| 5115 | LY_ERR rc = LY_SUCCESS; |
| 5116 | |
| 5117 | if (options & LYXP_SCNODE_ALL) { |
| 5118 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 5119 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5120 | 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] | 5121 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5122 | 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] | 5123 | } |
| 5124 | } |
| 5125 | |
| 5126 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 5127 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5128 | 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] | 5129 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5130 | 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] | 5131 | } |
| 5132 | } |
| 5133 | |
| 5134 | if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) { |
| 5135 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5136 | 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] | 5137 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5138 | 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] | 5139 | } |
| 5140 | } |
| 5141 | set_scnode_clear_ctx(set); |
| 5142 | return rc; |
| 5143 | } |
| 5144 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5145 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5146 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5147 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5148 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5149 | rc = lyxp_set_cast(args[2], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5150 | LY_CHECK_RET(rc); |
| 5151 | |
| 5152 | new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char)); |
| 5153 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 5154 | new_used = 0; |
| 5155 | |
| 5156 | have_removed = 0; |
| 5157 | for (i = 0; args[0]->val.str[i]; ++i) { |
| 5158 | found = 0; |
| 5159 | |
| 5160 | for (j = 0; args[1]->val.str[j]; ++j) { |
| 5161 | if (args[0]->val.str[i] == args[1]->val.str[j]) { |
| 5162 | /* removing this char */ |
| 5163 | if (j >= strlen(args[2]->val.str)) { |
| 5164 | have_removed = 1; |
| 5165 | found = 1; |
| 5166 | break; |
| 5167 | } |
| 5168 | /* replacing this char */ |
| 5169 | new[new_used] = args[2]->val.str[j]; |
| 5170 | ++new_used; |
| 5171 | found = 1; |
| 5172 | break; |
| 5173 | } |
| 5174 | } |
| 5175 | |
| 5176 | /* copying this char */ |
| 5177 | if (!found) { |
| 5178 | new[new_used] = args[0]->val.str[i]; |
| 5179 | ++new_used; |
| 5180 | } |
| 5181 | } |
| 5182 | |
| 5183 | if (have_removed) { |
| 5184 | new = ly_realloc(new, (new_used + 1) * sizeof(char)); |
| 5185 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 5186 | } |
| 5187 | new[new_used] = '\0'; |
| 5188 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5189 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5190 | set->type = LYXP_SET_STRING; |
| 5191 | set->val.str = new; |
| 5192 | |
| 5193 | return LY_SUCCESS; |
| 5194 | } |
| 5195 | |
| 5196 | /** |
| 5197 | * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN |
| 5198 | * with true value. |
| 5199 | * |
| 5200 | * @param[in] args Array of arguments. |
| 5201 | * @param[in] arg_count Count of elements in @p args. |
| 5202 | * @param[in,out] set Context and result set at the same time. |
| 5203 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5204 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5205 | */ |
| 5206 | static LY_ERR |
| 5207 | xpath_true(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 5208 | { |
| 5209 | if (options & LYXP_SCNODE_ALL) { |
| 5210 | set_scnode_clear_ctx(set); |
| 5211 | return LY_SUCCESS; |
| 5212 | } |
| 5213 | |
| 5214 | set_fill_boolean(set, 1); |
| 5215 | return LY_SUCCESS; |
| 5216 | } |
| 5217 | |
| 5218 | /* |
| 5219 | * moveto functions |
| 5220 | * |
| 5221 | * They and only they actually change the context (set). |
| 5222 | */ |
| 5223 | |
| 5224 | /** |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5225 | * @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] | 5226 | * |
Michal Vasko | 2104e9f | 2020-03-06 08:23:25 +0100 | [diff] [blame] | 5227 | * XPath @p set is expected to be a (sc)node set! |
| 5228 | * |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5229 | * @param[in,out] qname Qualified node name. If includes prefix, it is skipped. |
| 5230 | * @param[in,out] qname_len Length of @p qname, is updated accordingly. |
| 5231 | * @param[in] set Set with XPath context. |
| 5232 | * @param[out] moveto_mod Expected module of a matching node. |
| 5233 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5234 | */ |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5235 | static LY_ERR |
| 5236 | 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] | 5237 | { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5238 | const struct lys_module *mod; |
| 5239 | const char *ptr; |
| 5240 | int pref_len; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5241 | char *str; |
| 5242 | |
Michal Vasko | 2104e9f | 2020-03-06 08:23:25 +0100 | [diff] [blame] | 5243 | assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET)); |
| 5244 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5245 | if ((ptr = ly_strnchr(*qname, ':', *qname_len))) { |
| 5246 | /* specific module */ |
| 5247 | pref_len = ptr - *qname; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5248 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5249 | switch (set->format) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 5250 | case LYD_SCHEMA: |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5251 | /* schema, search all local module imports */ |
| 5252 | mod = lys_module_find_prefix(set->local_mod, *qname, pref_len); |
| 5253 | break; |
| 5254 | case LYD_JSON: |
| 5255 | /* JSON data, search in context */ |
| 5256 | str = strndup(*qname, pref_len); |
Michal Vasko | 97e76fd | 2020-05-27 15:22:01 +0200 | [diff] [blame] | 5257 | mod = ly_ctx_get_module_implemented(set->ctx, str); |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5258 | free(str); |
| 5259 | break; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 5260 | case LYD_XML: |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5261 | LOGINT_RET(set->ctx); |
| 5262 | } |
| 5263 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5264 | /* check for errors and non-implemented modules, as they are not valid */ |
Juraj Vijtiuk | d75faa6 | 2019-11-26 14:10:10 +0100 | [diff] [blame] | 5265 | if (!mod || !mod->implemented) { |
Michal Vasko | 2104e9f | 2020-03-06 08:23:25 +0100 | [diff] [blame] | 5266 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 5267 | LOGVAL(set->ctx, LY_VLOG_LYSC, set->ctx_scnode, LY_VCODE_XP_INMOD, pref_len, *qname); |
| 5268 | } else { |
| 5269 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INMOD, pref_len, *qname); |
| 5270 | } |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5271 | return LY_EVALID; |
| 5272 | } |
Juraj Vijtiuk | d75faa6 | 2019-11-26 14:10:10 +0100 | [diff] [blame] | 5273 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5274 | *qname += pref_len + 1; |
| 5275 | *qname_len -= pref_len + 1; |
| 5276 | } else if (((*qname)[0] == '*') && (*qname_len == 1)) { |
| 5277 | /* all modules - special case */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5278 | mod = NULL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5279 | } else if (set->type == LYXP_SET_SCNODE_SET) { |
| 5280 | /* current node module */ |
| 5281 | mod = set->ctx_scnode->module; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5282 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5283 | /* current node module */ |
| 5284 | mod = set->ctx_node->schema->module; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5285 | } |
| 5286 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5287 | *moveto_mod = mod; |
| 5288 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5289 | } |
| 5290 | |
| 5291 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5292 | * @brief Move context @p set to the root. Handles absolute path. |
| 5293 | * Result is LYXP_SET_NODE_SET. |
| 5294 | * |
| 5295 | * @param[in,out] set Set to use. |
| 5296 | * @param[in] options Xpath options. |
| 5297 | */ |
| 5298 | static void |
| 5299 | moveto_root(struct lyxp_set *set, int options) |
| 5300 | { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5301 | if (!set) { |
| 5302 | return; |
| 5303 | } |
| 5304 | |
| 5305 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5306 | set_scnode_clear_ctx(set); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 5307 | lyxp_set_scnode_insert_node(set, NULL, set->root_type); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5308 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5309 | set->type = LYXP_SET_NODE_SET; |
| 5310 | set->used = 0; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5311 | set_insert_node(set, NULL, 0, set->root_type, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5312 | } |
| 5313 | } |
| 5314 | |
| 5315 | /** |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 5316 | * @brief Check whether a node has some unresolved "when". |
| 5317 | * |
| 5318 | * @param[in] node Node to check. |
| 5319 | * @return LY_ERR value (LY_EINCOMPLETE if there are some unresolved "when") |
| 5320 | */ |
| 5321 | static LY_ERR |
| 5322 | moveto_when_check(const struct lyd_node *node) |
| 5323 | { |
| 5324 | const struct lysc_node *schema; |
| 5325 | |
| 5326 | if (!node) { |
| 5327 | return LY_SUCCESS; |
| 5328 | } |
| 5329 | |
| 5330 | schema = node->schema; |
| 5331 | do { |
| 5332 | if (schema->when && !(node->flags & LYD_WHEN_TRUE)) { |
| 5333 | return LY_EINCOMPLETE; |
| 5334 | } |
| 5335 | schema = schema->parent; |
| 5336 | } while (schema && (schema->nodetype & (LYS_CASE | LYS_CHOICE))); |
| 5337 | |
| 5338 | return LY_SUCCESS; |
| 5339 | } |
| 5340 | |
| 5341 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5342 | * @brief Check @p node as a part of NameTest processing. |
| 5343 | * |
| 5344 | * @param[in] node Node to check. |
| 5345 | * @param[in] root_type XPath root node type. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5346 | * @param[in] node_name Node name in the dictionary to move to, NULL for any node. |
| 5347 | * @param[in] moveto_mod Expected module of the node, NULL for any. |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5348 | * @return LY_ERR (LY_ENOT if node does not match, LY_EINCOMPLETE on unresolved when, |
| 5349 | * LY_EINVAL if netither node nor any children match) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5350 | */ |
| 5351 | static LY_ERR |
| 5352 | moveto_node_check(const struct lyd_node *node, enum lyxp_node_type root_type, const char *node_name, |
| 5353 | const struct lys_module *moveto_mod) |
| 5354 | { |
| 5355 | /* module check */ |
| 5356 | if (moveto_mod && (node->schema->module != moveto_mod)) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5357 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5358 | } |
| 5359 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5360 | /* context check */ |
| 5361 | 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] | 5362 | return LY_EINVAL; |
| 5363 | } |
| 5364 | |
| 5365 | /* name check */ |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5366 | if (node_name && strcmp(node_name, "*") && (node->schema->name != node_name)) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5367 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5368 | } |
| 5369 | |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 5370 | /* when check */ |
| 5371 | if (moveto_when_check(node)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5372 | return LY_EINCOMPLETE; |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 5373 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5374 | |
| 5375 | /* match */ |
| 5376 | return LY_SUCCESS; |
| 5377 | } |
| 5378 | |
| 5379 | /** |
| 5380 | * @brief Check @p node as a part of schema NameTest processing. |
| 5381 | * |
| 5382 | * @param[in] node Schema node to check. |
| 5383 | * @param[in] root_type XPath root node type. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5384 | * @param[in] node_name Node name in the dictionary to move to, NULL for any nodes. |
| 5385 | * @param[in] moveto_mod Expected module of the node, NULL for any. |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5386 | * @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] | 5387 | */ |
| 5388 | static LY_ERR |
| 5389 | 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] | 5390 | const struct lys_module *moveto_mod) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5391 | { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5392 | /* module check */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5393 | if (moveto_mod && (node->module != moveto_mod)) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5394 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5395 | } |
| 5396 | |
| 5397 | /* context check */ |
| 5398 | if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) { |
| 5399 | return LY_EINVAL; |
| 5400 | } |
| 5401 | |
| 5402 | /* name check */ |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5403 | if (node_name && strcmp(node_name, "*") && (node->name != node_name)) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5404 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5405 | } |
| 5406 | |
| 5407 | /* match */ |
| 5408 | return LY_SUCCESS; |
| 5409 | } |
| 5410 | |
| 5411 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5412 | * @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] | 5413 | * |
| 5414 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5415 | * @param[in] mod Matching node module, NULL for any. |
| 5416 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5417 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 5418 | */ |
| 5419 | static LY_ERR |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5420 | 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] | 5421 | { |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 5422 | uint32_t i; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5423 | int replaced; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5424 | const struct lyd_node *siblings, *sub; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5425 | LY_ERR rc; |
| 5426 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5427 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5428 | return LY_SUCCESS; |
| 5429 | } |
| 5430 | |
| 5431 | if (set->type != LYXP_SET_NODE_SET) { |
| 5432 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 5433 | return LY_EVALID; |
| 5434 | } |
| 5435 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5436 | for (i = 0; i < set->used; ) { |
| 5437 | replaced = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5438 | siblings = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5439 | |
| 5440 | 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] | 5441 | assert(!set->val.nodes[i].node); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5442 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5443 | /* search in all the trees */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5444 | siblings = set->tree; |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5445 | } else if (!(set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5446 | /* search in children */ |
| 5447 | siblings = lyd_node_children(set->val.nodes[i].node); |
| 5448 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5449 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5450 | for (sub = siblings; sub; sub = sub->next) { |
| 5451 | rc = moveto_node_check(sub, set->root_type, ncname, mod); |
| 5452 | if (rc == LY_SUCCESS) { |
| 5453 | if (!replaced) { |
| 5454 | set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i); |
| 5455 | replaced = 1; |
| 5456 | } else { |
| 5457 | set_insert_node(set, sub, 0, LYXP_NODE_ELEM, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5458 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5459 | ++i; |
| 5460 | } else if (rc == LY_EINCOMPLETE) { |
| 5461 | return rc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5462 | } |
| 5463 | } |
| 5464 | |
| 5465 | if (!replaced) { |
| 5466 | /* no match */ |
| 5467 | set_remove_node(set, i); |
| 5468 | } |
| 5469 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5470 | |
| 5471 | return LY_SUCCESS; |
| 5472 | } |
| 5473 | |
| 5474 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5475 | * @brief Move context @p set to a node using hashes. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY). |
| 5476 | * Context position aware. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5477 | * |
| 5478 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5479 | * @param[in] scnode Matching node schema. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5480 | * @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] | 5481 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 5482 | */ |
| 5483 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5484 | 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] | 5485 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5486 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5487 | uint32_t i; |
| 5488 | int replaced; |
| 5489 | const struct lyd_node *siblings; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5490 | struct lyd_node *sub, *inst = NULL; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5491 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5492 | assert(scnode && (!(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) || predicates)); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5493 | |
| 5494 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5495 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5496 | } |
| 5497 | |
| 5498 | if (set->type != LYXP_SET_NODE_SET) { |
| 5499 | 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] | 5500 | ret = LY_EVALID; |
| 5501 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5502 | } |
| 5503 | |
| 5504 | /* context check for all the nodes since we have the schema node */ |
| 5505 | if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) { |
| 5506 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5507 | goto cleanup; |
| 5508 | } |
| 5509 | |
| 5510 | /* create specific data instance if needed */ |
| 5511 | if (scnode->nodetype == LYS_LIST) { |
| 5512 | LY_CHECK_GOTO(ret = lyd_create_list(scnode, predicates, &inst), cleanup); |
| 5513 | } else if (scnode->nodetype == LYS_LEAFLIST) { |
| 5514 | 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] | 5515 | } |
| 5516 | |
| 5517 | for (i = 0; i < set->used; ) { |
| 5518 | replaced = 0; |
| 5519 | siblings = NULL; |
| 5520 | |
| 5521 | if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) { |
| 5522 | assert(!set->val.nodes[i].node); |
| 5523 | |
| 5524 | /* search in all the trees */ |
| 5525 | siblings = set->tree; |
| 5526 | } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
| 5527 | /* search in children */ |
| 5528 | siblings = lyd_node_children(set->val.nodes[i].node); |
| 5529 | } |
| 5530 | |
| 5531 | /* find the node using hashes */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5532 | if (inst) { |
| 5533 | lyd_find_sibling_first(siblings, inst, &sub); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5534 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5535 | lyd_find_sibling_val(siblings, scnode, NULL, 0, &sub); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5536 | } |
| 5537 | |
| 5538 | /* when check */ |
| 5539 | if (sub && moveto_when_check(sub)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5540 | ret = LY_EINCOMPLETE; |
| 5541 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5542 | } |
| 5543 | |
| 5544 | if (sub) { |
| 5545 | /* pos filled later */ |
| 5546 | if (!replaced) { |
| 5547 | set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i); |
| 5548 | replaced = 1; |
| 5549 | } else { |
| 5550 | set_insert_node(set, sub, 0, LYXP_NODE_ELEM, i); |
| 5551 | } |
| 5552 | ++i; |
| 5553 | } |
| 5554 | |
| 5555 | if (!replaced) { |
| 5556 | /* no match */ |
| 5557 | set_remove_node(set, i); |
| 5558 | } |
| 5559 | } |
| 5560 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5561 | cleanup: |
| 5562 | lyd_free_tree(inst); |
| 5563 | return ret; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5564 | } |
| 5565 | |
| 5566 | /** |
| 5567 | * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY). |
| 5568 | * |
| 5569 | * @param[in,out] set Set to use. |
| 5570 | * @param[in] mod Matching node module, NULL for any. |
| 5571 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5572 | * @param[in] options XPath options. |
| 5573 | * @return LY_ERR |
| 5574 | */ |
| 5575 | static LY_ERR |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5576 | 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] | 5577 | { |
Michal Vasko | cafad9d | 2019-11-07 15:20:03 +0100 | [diff] [blame] | 5578 | int i, orig_used, idx, temp_ctx = 0, getnext_opts; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5579 | uint32_t mod_idx; |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 5580 | const struct lysc_node *iter, *start_parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5581 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5582 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5583 | return LY_SUCCESS; |
| 5584 | } |
| 5585 | |
| 5586 | if (set->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 5587 | 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] | 5588 | return LY_EVALID; |
| 5589 | } |
| 5590 | |
Michal Vasko | cafad9d | 2019-11-07 15:20:03 +0100 | [diff] [blame] | 5591 | /* getnext opts */ |
| 5592 | getnext_opts = LYS_GETNEXT_NOSTATECHECK; |
| 5593 | if (options & LYXP_SCNODE_OUTPUT) { |
| 5594 | getnext_opts |= LYS_GETNEXT_OUTPUT; |
| 5595 | } |
| 5596 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5597 | orig_used = set->used; |
| 5598 | for (i = 0; i < orig_used; ++i) { |
| 5599 | if (set->val.scnodes[i].in_ctx != 1) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5600 | if (set->val.scnodes[i].in_ctx != -2) { |
| 5601 | continue; |
| 5602 | } |
| 5603 | |
| 5604 | /* remember context node */ |
| 5605 | set->val.scnodes[i].in_ctx = -1; |
Michal Vasko | ec4df48 | 2019-12-16 10:02:18 +0100 | [diff] [blame] | 5606 | } else { |
| 5607 | set->val.scnodes[i].in_ctx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5608 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5609 | |
| 5610 | start_parent = set->val.scnodes[i].scnode; |
| 5611 | |
| 5612 | 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] | 5613 | /* 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] | 5614 | * so use it directly (root node itself is useless in this case) */ |
| 5615 | mod_idx = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5616 | 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] | 5617 | iter = NULL; |
Michal Vasko | 509de4d | 2019-12-10 14:51:30 +0100 | [diff] [blame] | 5618 | /* module may not be implemented */ |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 5619 | while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) { |
| 5620 | if (!moveto_scnode_check(iter, set->root_type, ncname, mod)) { |
| 5621 | idx = lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5622 | /* we need to prevent these nodes from being considered in this moveto */ |
| 5623 | if ((idx < orig_used) && (idx > i)) { |
| 5624 | set->val.scnodes[idx].in_ctx = 2; |
| 5625 | temp_ctx = 1; |
| 5626 | } |
| 5627 | } |
| 5628 | } |
| 5629 | |
| 5630 | if (!mod_idx) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5631 | /* mod was specified, we are not going through the whole context */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5632 | break; |
| 5633 | } |
| 5634 | /* next iteration */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5635 | mod = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5636 | } |
| 5637 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 5638 | } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) { |
| 5639 | iter = NULL; |
| 5640 | while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) { |
| 5641 | if (!moveto_scnode_check(iter, set->root_type, ncname, (mod ? mod : set->local_mod))) { |
| 5642 | idx = lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5643 | if ((idx < orig_used) && (idx > i)) { |
| 5644 | set->val.scnodes[idx].in_ctx = 2; |
| 5645 | temp_ctx = 1; |
| 5646 | } |
| 5647 | } |
| 5648 | } |
| 5649 | } |
| 5650 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5651 | |
| 5652 | /* correct temporary in_ctx values */ |
| 5653 | if (temp_ctx) { |
| 5654 | for (i = 0; i < orig_used; ++i) { |
| 5655 | if (set->val.scnodes[i].in_ctx == 2) { |
| 5656 | set->val.scnodes[i].in_ctx = 1; |
| 5657 | } |
| 5658 | } |
| 5659 | } |
| 5660 | |
| 5661 | return LY_SUCCESS; |
| 5662 | } |
| 5663 | |
| 5664 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5665 | * @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] | 5666 | * Context position aware. |
| 5667 | * |
| 5668 | * @param[in] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5669 | * @param[in] mod Matching node module, NULL for any. |
| 5670 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5671 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 5672 | */ |
| 5673 | static LY_ERR |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5674 | 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] | 5675 | { |
| 5676 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5677 | const struct lyd_node *next, *elem, *start; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5678 | struct lyxp_set ret_set; |
| 5679 | LY_ERR rc; |
| 5680 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5681 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5682 | return LY_SUCCESS; |
| 5683 | } |
| 5684 | |
| 5685 | if (set->type != LYXP_SET_NODE_SET) { |
| 5686 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 5687 | return LY_EVALID; |
| 5688 | } |
| 5689 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5690 | /* 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] | 5691 | rc = moveto_node(set, NULL, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5692 | LY_CHECK_RET(rc); |
| 5693 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5694 | /* 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] | 5695 | set_init(&ret_set, set); |
| 5696 | for (i = 0; i < set->used; ++i) { |
| 5697 | |
| 5698 | /* TREE DFS */ |
| 5699 | start = set->val.nodes[i].node; |
| 5700 | for (elem = next = start; elem; elem = next) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5701 | rc = moveto_node_check(elem, set->root_type, ncname, mod); |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5702 | if (!rc) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5703 | /* add matching node into result set */ |
| 5704 | set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used); |
| 5705 | if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) { |
| 5706 | /* the node is a duplicate, we'll process it later in the set */ |
| 5707 | goto skip_children; |
| 5708 | } |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5709 | } else if (rc == LY_EINCOMPLETE) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5710 | return rc; |
| 5711 | } else if (rc == LY_EINVAL) { |
| 5712 | goto skip_children; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5713 | } |
| 5714 | |
| 5715 | /* TREE DFS NEXT ELEM */ |
| 5716 | /* select element for the next run - children first */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5717 | next = lyd_node_children(elem); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5718 | if (!next) { |
| 5719 | skip_children: |
| 5720 | /* no children, so try siblings, but only if it's not the start, |
| 5721 | * that is considered to be the root and it's siblings are not traversed */ |
| 5722 | if (elem != start) { |
| 5723 | next = elem->next; |
| 5724 | } else { |
| 5725 | break; |
| 5726 | } |
| 5727 | } |
| 5728 | while (!next) { |
| 5729 | /* no siblings, go back through the parents */ |
| 5730 | if ((struct lyd_node *)elem->parent == start) { |
| 5731 | /* we are done, no next element to process */ |
| 5732 | break; |
| 5733 | } |
| 5734 | /* parent is already processed, go to its sibling */ |
| 5735 | elem = (struct lyd_node *)elem->parent; |
| 5736 | next = elem->next; |
| 5737 | } |
| 5738 | } |
| 5739 | } |
| 5740 | |
| 5741 | /* make the temporary set the current one */ |
| 5742 | ret_set.ctx_pos = set->ctx_pos; |
| 5743 | ret_set.ctx_size = set->ctx_size; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5744 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5745 | memcpy(set, &ret_set, sizeof *set); |
| 5746 | |
| 5747 | return LY_SUCCESS; |
| 5748 | } |
| 5749 | |
| 5750 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5751 | * @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] | 5752 | * |
| 5753 | * @param[in] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5754 | * @param[in] mod Matching node module, NULL for any. |
| 5755 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5756 | * @param[in] options XPath options. |
| 5757 | * @return LY_ERR |
| 5758 | */ |
| 5759 | static LY_ERR |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5760 | 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] | 5761 | { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5762 | int i, orig_used, idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5763 | const struct lysc_node *next, *elem, *start; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5764 | LY_ERR rc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5765 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5766 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5767 | return LY_SUCCESS; |
| 5768 | } |
| 5769 | |
| 5770 | if (set->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 5771 | 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] | 5772 | return LY_EVALID; |
| 5773 | } |
| 5774 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5775 | orig_used = set->used; |
| 5776 | for (i = 0; i < orig_used; ++i) { |
| 5777 | if (set->val.scnodes[i].in_ctx != 1) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5778 | if (set->val.scnodes[i].in_ctx != -2) { |
| 5779 | continue; |
| 5780 | } |
| 5781 | |
| 5782 | /* remember context node */ |
| 5783 | set->val.scnodes[i].in_ctx = -1; |
Michal Vasko | ec4df48 | 2019-12-16 10:02:18 +0100 | [diff] [blame] | 5784 | } else { |
| 5785 | set->val.scnodes[i].in_ctx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5786 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5787 | |
| 5788 | /* TREE DFS */ |
| 5789 | start = set->val.scnodes[i].scnode; |
| 5790 | for (elem = next = start; elem; elem = next) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5791 | if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) { |
| 5792 | /* schema-only nodes, skip root */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5793 | goto next_iter; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5794 | } |
| 5795 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5796 | rc = moveto_scnode_check(elem, set->root_type, ncname, mod); |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5797 | if (!rc) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 5798 | 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] | 5799 | set->val.scnodes[idx].in_ctx = 1; |
| 5800 | if (idx > i) { |
| 5801 | /* we will process it later in the set */ |
| 5802 | goto skip_children; |
| 5803 | } |
| 5804 | } else { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 5805 | lyxp_set_scnode_insert_node(set, elem, LYXP_NODE_ELEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5806 | } |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5807 | } else if (rc == LY_EINVAL) { |
| 5808 | goto skip_children; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5809 | } |
| 5810 | |
| 5811 | next_iter: |
| 5812 | /* TREE DFS NEXT ELEM */ |
| 5813 | /* select element for the next run - children first */ |
| 5814 | 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] | 5815 | if (!next) { |
| 5816 | skip_children: |
| 5817 | /* no children, so try siblings, but only if it's not the start, |
| 5818 | * that is considered to be the root and it's siblings are not traversed */ |
| 5819 | if (elem != start) { |
| 5820 | next = elem->next; |
| 5821 | } else { |
| 5822 | break; |
| 5823 | } |
| 5824 | } |
| 5825 | while (!next) { |
| 5826 | /* no siblings, go back through the parents */ |
| 5827 | if (elem->parent == start) { |
| 5828 | /* we are done, no next element to process */ |
| 5829 | break; |
| 5830 | } |
| 5831 | /* parent is already processed, go to its sibling */ |
| 5832 | elem = elem->parent; |
| 5833 | next = elem->next; |
| 5834 | } |
| 5835 | } |
| 5836 | } |
| 5837 | |
| 5838 | return LY_SUCCESS; |
| 5839 | } |
| 5840 | |
| 5841 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5842 | * @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] | 5843 | * Indirectly context position aware. |
| 5844 | * |
| 5845 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5846 | * @param[in] mod Matching metadata module, NULL for any. |
| 5847 | * @param[in] ncname Matching metadata name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5848 | * @return LY_ERR |
| 5849 | */ |
| 5850 | static LY_ERR |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5851 | 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] | 5852 | { |
| 5853 | uint32_t i; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5854 | int replaced; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5855 | struct lyd_meta *sub; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5856 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5857 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5858 | return LY_SUCCESS; |
| 5859 | } |
| 5860 | |
| 5861 | if (set->type != LYXP_SET_NODE_SET) { |
| 5862 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 5863 | return LY_EVALID; |
| 5864 | } |
| 5865 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5866 | for (i = 0; i < set->used; ) { |
| 5867 | replaced = 0; |
| 5868 | |
| 5869 | /* only attributes of an elem (not dummy) can be in the result, skip all the rest; |
| 5870 | * our attributes are always qualified */ |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5871 | if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5872 | for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5873 | |
| 5874 | /* check "namespace" */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5875 | if (mod && (sub->annotation->module != mod)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5876 | continue; |
| 5877 | } |
| 5878 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5879 | if (!ncname || (sub->name == ncname)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5880 | /* match */ |
| 5881 | if (!replaced) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5882 | set->val.meta[i].meta = sub; |
| 5883 | set->val.meta[i].type = LYXP_NODE_META; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5884 | /* pos does not change */ |
| 5885 | replaced = 1; |
| 5886 | } else { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5887 | 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] | 5888 | } |
| 5889 | ++i; |
| 5890 | } |
| 5891 | } |
| 5892 | } |
| 5893 | |
| 5894 | if (!replaced) { |
| 5895 | /* no match */ |
| 5896 | set_remove_node(set, i); |
| 5897 | } |
| 5898 | } |
| 5899 | |
| 5900 | return LY_SUCCESS; |
| 5901 | } |
| 5902 | |
| 5903 | /** |
| 5904 | * @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] | 5905 | * Result is LYXP_SET_NODE_SET. Context position aware. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5906 | * |
| 5907 | * @param[in,out] set1 Set to use for the result. |
| 5908 | * @param[in] set2 Set that is copied to @p set1. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5909 | * @return LY_ERR |
| 5910 | */ |
| 5911 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5912 | moveto_union(struct lyxp_set *set1, struct lyxp_set *set2) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5913 | { |
| 5914 | LY_ERR rc; |
| 5915 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5916 | 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] | 5917 | LOGVAL(set1->ctx, LY_VLOG_LYD, set1->ctx_node, LY_VCODE_XP_INOP_2, "union", print_set_type(set1), print_set_type(set2)); |
| 5918 | return LY_EVALID; |
| 5919 | } |
| 5920 | |
| 5921 | /* set2 is empty or both set1 and set2 */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5922 | if (!set2->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5923 | return LY_SUCCESS; |
| 5924 | } |
| 5925 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5926 | if (!set1->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5927 | memcpy(set1, set2, sizeof *set1); |
| 5928 | /* dynamic memory belongs to set1 now, do not free */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5929 | memset(set2, 0, sizeof *set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5930 | return LY_SUCCESS; |
| 5931 | } |
| 5932 | |
| 5933 | /* we assume sets are sorted */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5934 | assert(!set_sort(set1) && !set_sort(set2)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5935 | |
| 5936 | /* sort, remove duplicates */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5937 | rc = set_sorted_merge(set1, set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5938 | LY_CHECK_RET(rc); |
| 5939 | |
| 5940 | /* final set must be sorted */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5941 | assert(!set_sort(set1)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5942 | |
| 5943 | return LY_SUCCESS; |
| 5944 | } |
| 5945 | |
| 5946 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5947 | * @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] | 5948 | * Context position aware. |
| 5949 | * |
| 5950 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5951 | * @param[in] mod Matching metadata module, NULL for any. |
| 5952 | * @param[in] ncname Matching metadata name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5953 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 5954 | */ |
| 5955 | static int |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5956 | 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] | 5957 | { |
| 5958 | uint32_t i; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5959 | int replaced; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5960 | struct lyd_meta *sub; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5961 | struct lyxp_set *set_all_desc = NULL; |
| 5962 | LY_ERR rc; |
| 5963 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5964 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5965 | return LY_SUCCESS; |
| 5966 | } |
| 5967 | |
| 5968 | if (set->type != LYXP_SET_NODE_SET) { |
| 5969 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 5970 | return LY_EVALID; |
| 5971 | } |
| 5972 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5973 | /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory, |
| 5974 | * but it likely won't be used much, so it's a waste of time */ |
| 5975 | /* copy the context */ |
| 5976 | set_all_desc = set_copy(set); |
| 5977 | /* get all descendant nodes (the original context nodes are removed) */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5978 | rc = moveto_node_alldesc(set_all_desc, NULL, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5979 | if (rc != LY_SUCCESS) { |
| 5980 | lyxp_set_free(set_all_desc); |
| 5981 | return rc; |
| 5982 | } |
| 5983 | /* prepend the original context nodes */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5984 | rc = moveto_union(set, set_all_desc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5985 | if (rc != LY_SUCCESS) { |
| 5986 | lyxp_set_free(set_all_desc); |
| 5987 | return rc; |
| 5988 | } |
| 5989 | lyxp_set_free(set_all_desc); |
| 5990 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5991 | for (i = 0; i < set->used; ) { |
| 5992 | replaced = 0; |
| 5993 | |
| 5994 | /* only attributes of an elem can be in the result, skip all the rest, |
| 5995 | * we have all attributes qualified in lyd tree */ |
| 5996 | if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5997 | for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5998 | /* check "namespace" */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5999 | if (mod && (sub->annotation->module != mod)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6000 | continue; |
| 6001 | } |
| 6002 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6003 | if (!ncname || (sub->name == ncname)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6004 | /* match */ |
| 6005 | if (!replaced) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6006 | set->val.meta[i].meta = sub; |
| 6007 | set->val.meta[i].type = LYXP_NODE_META; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6008 | /* pos does not change */ |
| 6009 | replaced = 1; |
| 6010 | } else { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6011 | 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] | 6012 | } |
| 6013 | ++i; |
| 6014 | } |
| 6015 | } |
| 6016 | } |
| 6017 | |
| 6018 | if (!replaced) { |
| 6019 | /* no match */ |
| 6020 | set_remove_node(set, i); |
| 6021 | } |
| 6022 | } |
| 6023 | |
| 6024 | return LY_SUCCESS; |
| 6025 | } |
| 6026 | |
| 6027 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6028 | * @brief Move context @p set to self and al chilren, recursively. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET. |
| 6029 | * Context position aware. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6030 | * |
| 6031 | * @param[in] parent Current parent. |
| 6032 | * @param[in] parent_pos Position of @p parent. |
| 6033 | * @param[in] parent_type Node type of @p parent. |
| 6034 | * @param[in,out] to_set Set to use. |
| 6035 | * @param[in] dup_check_set Set for checking duplicities. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6036 | * @param[in] options XPath options. |
| 6037 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6038 | */ |
| 6039 | static LY_ERR |
| 6040 | 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] | 6041 | 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] | 6042 | { |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6043 | const struct lyd_node *iter, *first; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6044 | LY_ERR rc; |
| 6045 | |
| 6046 | switch (parent_type) { |
| 6047 | case LYXP_NODE_ROOT: |
| 6048 | case LYXP_NODE_ROOT_CONFIG: |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6049 | assert(!parent); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6050 | |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6051 | /* add all top-level nodes as elements */ |
| 6052 | first = to_set->tree; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6053 | break; |
| 6054 | case LYXP_NODE_ELEM: |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6055 | /* add just the text node of this term element node */ |
| 6056 | if (parent->schema->nodetype & (LYD_NODE_TERM | LYD_NODE_ANY)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6057 | if (!set_dup_node_check(dup_check_set, parent, LYXP_NODE_TEXT, -1)) { |
| 6058 | set_insert_node(to_set, parent, parent_pos, LYXP_NODE_TEXT, to_set->used); |
| 6059 | } |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6060 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6061 | } |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6062 | |
| 6063 | /* add all the children of this node */ |
| 6064 | first = lyd_node_children(parent); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6065 | break; |
| 6066 | default: |
| 6067 | LOGINT_RET(parent->schema->module->ctx); |
| 6068 | } |
| 6069 | |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6070 | /* add all top-level nodes as elements */ |
| 6071 | LY_LIST_FOR(first, iter) { |
| 6072 | /* context check */ |
| 6073 | if ((parent_type == LYXP_NODE_ROOT_CONFIG) && (iter->schema->flags & LYS_CONFIG_R)) { |
| 6074 | continue; |
| 6075 | } |
| 6076 | |
| 6077 | /* when check */ |
| 6078 | if (moveto_when_check(iter)) { |
| 6079 | return LY_EINCOMPLETE; |
| 6080 | } |
| 6081 | |
| 6082 | if (!set_dup_node_check(dup_check_set, iter, LYXP_NODE_ELEM, -1)) { |
| 6083 | set_insert_node(to_set, iter, 0, LYXP_NODE_ELEM, to_set->used); |
| 6084 | |
| 6085 | /* also add all the children of this node, recursively */ |
| 6086 | rc = moveto_self_add_children_r(iter, 0, LYXP_NODE_ELEM, to_set, dup_check_set, options); |
| 6087 | LY_CHECK_RET(rc); |
| 6088 | } |
| 6089 | } |
| 6090 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6091 | return LY_SUCCESS; |
| 6092 | } |
| 6093 | |
| 6094 | /** |
| 6095 | * @brief Move context @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET |
| 6096 | * (or LYXP_SET_EMPTY). Context position aware. |
| 6097 | * |
| 6098 | * @param[in,out] set Set to use. |
| 6099 | * @param[in] all_desc Whether to go to all descendants ('//') or not ('/'). |
| 6100 | * @param[in] options XPath options. |
| 6101 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6102 | */ |
| 6103 | static LY_ERR |
| 6104 | moveto_self(struct lyxp_set *set, int all_desc, int options) |
| 6105 | { |
| 6106 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6107 | struct lyxp_set ret_set; |
| 6108 | LY_ERR rc; |
| 6109 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6110 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6111 | return LY_SUCCESS; |
| 6112 | } |
| 6113 | |
| 6114 | if (set->type != LYXP_SET_NODE_SET) { |
| 6115 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 6116 | return LY_EVALID; |
| 6117 | } |
| 6118 | |
| 6119 | /* nothing to do */ |
| 6120 | if (!all_desc) { |
| 6121 | return LY_SUCCESS; |
| 6122 | } |
| 6123 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6124 | /* add all the children, they get added recursively */ |
| 6125 | set_init(&ret_set, set); |
| 6126 | for (i = 0; i < set->used; ++i) { |
| 6127 | /* copy the current node to tmp */ |
| 6128 | set_insert_node(&ret_set, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, ret_set.used); |
| 6129 | |
| 6130 | /* do not touch attributes and text nodes */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6131 | 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] | 6132 | continue; |
| 6133 | } |
| 6134 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6135 | /* add all the children */ |
| 6136 | 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] | 6137 | set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6138 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6139 | lyxp_set_free_content(&ret_set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6140 | return rc; |
| 6141 | } |
| 6142 | } |
| 6143 | |
| 6144 | /* use the temporary set as the current one */ |
| 6145 | ret_set.ctx_pos = set->ctx_pos; |
| 6146 | ret_set.ctx_size = set->ctx_size; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6147 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6148 | memcpy(set, &ret_set, sizeof *set); |
| 6149 | |
| 6150 | return LY_SUCCESS; |
| 6151 | } |
| 6152 | |
| 6153 | /** |
| 6154 | * @brief Move context schema @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_SCNODE_SET |
| 6155 | * (or LYXP_SET_EMPTY). |
| 6156 | * |
| 6157 | * @param[in,out] set Set to use. |
| 6158 | * @param[in] all_desc Whether to go to all descendants ('//') or not ('/'). |
| 6159 | * @param[in] options XPath options. |
| 6160 | * @return LY_ERR |
| 6161 | */ |
| 6162 | static LY_ERR |
| 6163 | moveto_scnode_self(struct lyxp_set *set, int all_desc, int options) |
| 6164 | { |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6165 | int getnext_opts; |
| 6166 | uint32_t i, mod_idx; |
| 6167 | const struct lysc_node *iter, *start_parent; |
| 6168 | const struct lys_module *mod; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6169 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6170 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6171 | return LY_SUCCESS; |
| 6172 | } |
| 6173 | |
| 6174 | if (set->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 6175 | 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] | 6176 | return LY_EVALID; |
| 6177 | } |
| 6178 | |
| 6179 | /* nothing to do */ |
| 6180 | if (!all_desc) { |
| 6181 | return LY_SUCCESS; |
| 6182 | } |
| 6183 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6184 | /* getnext opts */ |
| 6185 | getnext_opts = LYS_GETNEXT_NOSTATECHECK; |
| 6186 | if (options & LYXP_SCNODE_OUTPUT) { |
| 6187 | getnext_opts |= LYS_GETNEXT_OUTPUT; |
| 6188 | } |
| 6189 | |
| 6190 | /* 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] | 6191 | for (i = 0; i < set->used; ++i) { |
| 6192 | if (set->val.scnodes[i].in_ctx != 1) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6193 | if (set->val.scnodes[i].in_ctx != -2) { |
| 6194 | continue; |
| 6195 | } |
| 6196 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6197 | /* remember context node */ |
| 6198 | set->val.scnodes[i].in_ctx = -1; |
| 6199 | } else { |
| 6200 | set->val.scnodes[i].in_ctx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6201 | } |
| 6202 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6203 | start_parent = set->val.scnodes[i].scnode; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6204 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6205 | if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) { |
| 6206 | /* it can actually be in any module, it's all <running> */ |
| 6207 | mod_idx = 0; |
| 6208 | while ((mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) { |
| 6209 | iter = NULL; |
| 6210 | /* module may not be implemented */ |
| 6211 | while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) { |
| 6212 | /* context check */ |
| 6213 | if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) { |
| 6214 | continue; |
| 6215 | } |
| 6216 | |
| 6217 | lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM); |
| 6218 | /* throw away the insert index, we want to consider that node again, recursively */ |
| 6219 | } |
| 6220 | } |
| 6221 | |
| 6222 | } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) { |
| 6223 | iter = NULL; |
| 6224 | while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6225 | /* context check */ |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6226 | 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] | 6227 | continue; |
| 6228 | } |
| 6229 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6230 | lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6231 | } |
| 6232 | } |
| 6233 | } |
| 6234 | |
| 6235 | return LY_SUCCESS; |
| 6236 | } |
| 6237 | |
| 6238 | /** |
| 6239 | * @brief Move context @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_NODE_SET |
| 6240 | * (or LYXP_SET_EMPTY). Context position aware. |
| 6241 | * |
| 6242 | * @param[in] set Set to use. |
| 6243 | * @param[in] all_desc Whether to go to all descendants ('//') or not ('/'). |
| 6244 | * @param[in] options XPath options. |
| 6245 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6246 | */ |
| 6247 | static LY_ERR |
| 6248 | moveto_parent(struct lyxp_set *set, int all_desc, int options) |
| 6249 | { |
| 6250 | LY_ERR rc; |
| 6251 | uint32_t i; |
| 6252 | struct lyd_node *node, *new_node; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6253 | enum lyxp_node_type new_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6254 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6255 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6256 | return LY_SUCCESS; |
| 6257 | } |
| 6258 | |
| 6259 | if (set->type != LYXP_SET_NODE_SET) { |
| 6260 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 6261 | return LY_EVALID; |
| 6262 | } |
| 6263 | |
| 6264 | if (all_desc) { |
| 6265 | /* <path>//.. == <path>//./.. */ |
| 6266 | rc = moveto_self(set, 1, options); |
| 6267 | LY_CHECK_RET(rc); |
| 6268 | } |
| 6269 | |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 6270 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6271 | node = set->val.nodes[i].node; |
| 6272 | |
| 6273 | if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
| 6274 | new_node = (struct lyd_node *)node->parent; |
| 6275 | } else if (set->val.nodes[i].type == LYXP_NODE_TEXT) { |
| 6276 | new_node = node; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6277 | } else if (set->val.nodes[i].type == LYXP_NODE_META) { |
| 6278 | new_node = set->val.meta[i].meta->parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6279 | if (!new_node) { |
| 6280 | LOGINT_RET(set->ctx); |
| 6281 | } |
| 6282 | } else { |
| 6283 | /* root does not have a parent */ |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6284 | set_remove_node_none(set, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6285 | continue; |
| 6286 | } |
| 6287 | |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 6288 | /* when check */ |
| 6289 | if (moveto_when_check(new_node)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6290 | return LY_EINCOMPLETE; |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 6291 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6292 | |
| 6293 | /* node already there can also be the root */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6294 | if (!new_node) { |
| 6295 | new_type = set->root_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6296 | |
| 6297 | /* node has a standard parent (it can equal the root, it's not the root yet since they are fake) */ |
| 6298 | } else { |
| 6299 | new_type = LYXP_NODE_ELEM; |
| 6300 | } |
| 6301 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6302 | if (set_dup_node_check(set, new_node, new_type, -1)) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6303 | set_remove_node_none(set, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6304 | } else { |
| 6305 | set_replace_node(set, new_node, 0, new_type, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6306 | } |
| 6307 | } |
| 6308 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6309 | set_remove_nodes_none(set); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6310 | assert(!set_sort(set) && !set_sorted_dup_node_clean(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6311 | |
| 6312 | return LY_SUCCESS; |
| 6313 | } |
| 6314 | |
| 6315 | /** |
| 6316 | * @brief Move context schema @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_SCNODE_SET |
| 6317 | * (or LYXP_SET_EMPTY). |
| 6318 | * |
| 6319 | * @param[in] set Set to use. |
| 6320 | * @param[in] all_desc Whether to go to all descendants ('//') or not ('/'). |
| 6321 | * @param[in] options XPath options. |
| 6322 | * @return LY_ERR |
| 6323 | */ |
| 6324 | static LY_ERR |
| 6325 | moveto_scnode_parent(struct lyxp_set *set, int all_desc, int options) |
| 6326 | { |
| 6327 | int idx, i, orig_used, temp_ctx = 0; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6328 | const struct lysc_node *node, *new_node; |
| 6329 | enum lyxp_node_type new_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6330 | LY_ERR rc; |
| 6331 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6332 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6333 | return LY_SUCCESS; |
| 6334 | } |
| 6335 | |
| 6336 | if (set->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 6337 | 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] | 6338 | return LY_EVALID; |
| 6339 | } |
| 6340 | |
| 6341 | if (all_desc) { |
| 6342 | /* <path>//.. == <path>//./.. */ |
| 6343 | rc = moveto_scnode_self(set, 1, options); |
| 6344 | LY_CHECK_RET(rc); |
| 6345 | } |
| 6346 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6347 | orig_used = set->used; |
| 6348 | for (i = 0; i < orig_used; ++i) { |
| 6349 | if (set->val.scnodes[i].in_ctx != 1) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6350 | if (set->val.scnodes[i].in_ctx != -2) { |
| 6351 | continue; |
| 6352 | } |
| 6353 | |
| 6354 | /* remember context node */ |
| 6355 | set->val.scnodes[i].in_ctx = -1; |
Michal Vasko | ec4df48 | 2019-12-16 10:02:18 +0100 | [diff] [blame] | 6356 | } else { |
| 6357 | set->val.scnodes[i].in_ctx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6358 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6359 | |
| 6360 | node = set->val.scnodes[i].scnode; |
| 6361 | |
| 6362 | if (set->val.scnodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6363 | new_node = lysc_data_parent(node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6364 | } else { |
| 6365 | /* root does not have a parent */ |
| 6366 | continue; |
| 6367 | } |
| 6368 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6369 | /* node has no parent */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6370 | if (!new_node) { |
| 6371 | new_type = set->root_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6372 | |
| 6373 | /* node has a standard parent (it can equal the root, it's not the root yet since they are fake) */ |
| 6374 | } else { |
| 6375 | new_type = LYXP_NODE_ELEM; |
| 6376 | } |
| 6377 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6378 | idx = lyxp_set_scnode_insert_node(set, new_node, new_type); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6379 | if ((idx < orig_used) && (idx > i)) { |
| 6380 | set->val.scnodes[idx].in_ctx = 2; |
| 6381 | temp_ctx = 1; |
| 6382 | } |
| 6383 | } |
| 6384 | |
| 6385 | if (temp_ctx) { |
| 6386 | for (i = 0; i < orig_used; ++i) { |
| 6387 | if (set->val.scnodes[i].in_ctx == 2) { |
| 6388 | set->val.scnodes[i].in_ctx = 1; |
| 6389 | } |
| 6390 | } |
| 6391 | } |
| 6392 | |
| 6393 | return LY_SUCCESS; |
| 6394 | } |
| 6395 | |
| 6396 | /** |
| 6397 | * @brief Move context @p set to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'. |
| 6398 | * Result is LYXP_SET_BOOLEAN. Indirectly context position aware. |
| 6399 | * |
| 6400 | * @param[in,out] set1 Set to use for the result. |
| 6401 | * @param[in] set2 Set acting as the second operand for @p op. |
| 6402 | * @param[in] op Comparison operator to process. |
| 6403 | * @param[in] options XPath options. |
| 6404 | * @return LY_ERR |
| 6405 | */ |
| 6406 | static LY_ERR |
| 6407 | moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op, int options) |
| 6408 | { |
| 6409 | /* |
| 6410 | * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING |
| 6411 | * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING) |
| 6412 | * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 6413 | * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 6414 | * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING |
| 6415 | * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6416 | * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 6417 | * |
| 6418 | * '=' or '!=' |
| 6419 | * BOOLEAN + BOOLEAN |
| 6420 | * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 6421 | * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 6422 | * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 6423 | * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 6424 | * NUMBER + NUMBER |
| 6425 | * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6426 | * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 6427 | * STRING + STRING |
| 6428 | * |
| 6429 | * '<=', '<', '>=', '>' |
| 6430 | * NUMBER + NUMBER |
| 6431 | * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 6432 | * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 6433 | * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 6434 | * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6435 | * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 6436 | * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 6437 | * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6438 | * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6439 | */ |
| 6440 | struct lyxp_set iter1, iter2; |
| 6441 | int result; |
| 6442 | int64_t i; |
| 6443 | LY_ERR rc; |
| 6444 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6445 | memset(&iter1, 0, sizeof iter1); |
| 6446 | memset(&iter2, 0, sizeof iter2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6447 | |
| 6448 | /* iterative evaluation with node-sets */ |
| 6449 | if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) { |
| 6450 | if (set1->type == LYXP_SET_NODE_SET) { |
| 6451 | for (i = 0; i < set1->used; ++i) { |
| 6452 | switch (set2->type) { |
| 6453 | case LYXP_SET_NUMBER: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6454 | rc = set_comp_cast(&iter1, set1, LYXP_SET_NUMBER, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6455 | break; |
| 6456 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6457 | rc = set_comp_cast(&iter1, set1, LYXP_SET_BOOLEAN, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6458 | break; |
| 6459 | default: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6460 | rc = set_comp_cast(&iter1, set1, LYXP_SET_STRING, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6461 | break; |
| 6462 | } |
| 6463 | LY_CHECK_RET(rc); |
| 6464 | |
| 6465 | rc = moveto_op_comp(&iter1, set2, op, options); |
| 6466 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6467 | lyxp_set_free_content(&iter1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6468 | return rc; |
| 6469 | } |
| 6470 | |
| 6471 | /* lazy evaluation until true */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6472 | if (iter1.val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6473 | set_fill_boolean(set1, 1); |
| 6474 | return LY_SUCCESS; |
| 6475 | } |
| 6476 | } |
| 6477 | } else { |
| 6478 | for (i = 0; i < set2->used; ++i) { |
| 6479 | switch (set1->type) { |
| 6480 | case LYXP_SET_NUMBER: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6481 | rc = set_comp_cast(&iter2, set2, LYXP_SET_NUMBER, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6482 | break; |
| 6483 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6484 | rc = set_comp_cast(&iter2, set2, LYXP_SET_BOOLEAN, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6485 | break; |
| 6486 | default: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6487 | rc = set_comp_cast(&iter2, set2, LYXP_SET_STRING, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6488 | break; |
| 6489 | } |
| 6490 | LY_CHECK_RET(rc); |
| 6491 | |
| 6492 | set_fill_set(&iter1, set1); |
| 6493 | |
| 6494 | rc = moveto_op_comp(&iter1, &iter2, op, options); |
| 6495 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6496 | lyxp_set_free_content(&iter1); |
| 6497 | lyxp_set_free_content(&iter2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6498 | return rc; |
| 6499 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6500 | lyxp_set_free_content(&iter2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6501 | |
| 6502 | /* lazy evaluation until true */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6503 | if (iter1.val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6504 | set_fill_boolean(set1, 1); |
| 6505 | return LY_SUCCESS; |
| 6506 | } |
| 6507 | } |
| 6508 | } |
| 6509 | |
| 6510 | /* false for all nodes */ |
| 6511 | set_fill_boolean(set1, 0); |
| 6512 | return LY_SUCCESS; |
| 6513 | } |
| 6514 | |
| 6515 | /* first convert properly */ |
| 6516 | if ((op[0] == '=') || (op[0] == '!')) { |
| 6517 | if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6518 | lyxp_set_cast(set1, LYXP_SET_BOOLEAN); |
| 6519 | lyxp_set_cast(set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6520 | } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6521 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6522 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6523 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6524 | LY_CHECK_RET(rc); |
| 6525 | } /* else we have 2 strings */ |
| 6526 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6527 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6528 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6529 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6530 | LY_CHECK_RET(rc); |
| 6531 | } |
| 6532 | |
| 6533 | assert(set1->type == set2->type); |
| 6534 | |
| 6535 | /* compute result */ |
| 6536 | if (op[0] == '=') { |
| 6537 | if (set1->type == LYXP_SET_BOOLEAN) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6538 | result = (set1->val.bln == set2->val.bln); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6539 | } else if (set1->type == LYXP_SET_NUMBER) { |
| 6540 | result = (set1->val.num == set2->val.num); |
| 6541 | } else { |
| 6542 | assert(set1->type == LYXP_SET_STRING); |
Michal Vasko | ac6c72f | 2019-11-14 16:09:34 +0100 | [diff] [blame] | 6543 | result = !strcmp(set1->val.str, set2->val.str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6544 | } |
| 6545 | } else if (op[0] == '!') { |
| 6546 | if (set1->type == LYXP_SET_BOOLEAN) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6547 | result = (set1->val.bln != set2->val.bln); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6548 | } else if (set1->type == LYXP_SET_NUMBER) { |
| 6549 | result = (set1->val.num != set2->val.num); |
| 6550 | } else { |
| 6551 | assert(set1->type == LYXP_SET_STRING); |
Michal Vasko | ac6c72f | 2019-11-14 16:09:34 +0100 | [diff] [blame] | 6552 | result = !strcmp(set1->val.str, set2->val.str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6553 | } |
| 6554 | } else { |
| 6555 | assert(set1->type == LYXP_SET_NUMBER); |
| 6556 | if (op[0] == '<') { |
| 6557 | if (op[1] == '=') { |
| 6558 | result = (set1->val.num <= set2->val.num); |
| 6559 | } else { |
| 6560 | result = (set1->val.num < set2->val.num); |
| 6561 | } |
| 6562 | } else { |
| 6563 | if (op[1] == '=') { |
| 6564 | result = (set1->val.num >= set2->val.num); |
| 6565 | } else { |
| 6566 | result = (set1->val.num > set2->val.num); |
| 6567 | } |
| 6568 | } |
| 6569 | } |
| 6570 | |
| 6571 | /* assign result */ |
| 6572 | if (result) { |
| 6573 | set_fill_boolean(set1, 1); |
| 6574 | } else { |
| 6575 | set_fill_boolean(set1, 0); |
| 6576 | } |
| 6577 | |
| 6578 | return LY_SUCCESS; |
| 6579 | } |
| 6580 | |
| 6581 | /** |
| 6582 | * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div', |
| 6583 | * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware. |
| 6584 | * |
| 6585 | * @param[in,out] set1 Set to use for the result. |
| 6586 | * @param[in] set2 Set acting as the second operand for @p op. |
| 6587 | * @param[in] op Operator to process. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6588 | * @return LY_ERR |
| 6589 | */ |
| 6590 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6591 | 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] | 6592 | { |
| 6593 | LY_ERR rc; |
| 6594 | |
| 6595 | /* unary '-' */ |
| 6596 | if (!set2 && (op[0] == '-')) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6597 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6598 | LY_CHECK_RET(rc); |
| 6599 | set1->val.num *= -1; |
| 6600 | lyxp_set_free(set2); |
| 6601 | return LY_SUCCESS; |
| 6602 | } |
| 6603 | |
| 6604 | assert(set1 && set2); |
| 6605 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6606 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6607 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6608 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6609 | LY_CHECK_RET(rc); |
| 6610 | |
| 6611 | switch (op[0]) { |
| 6612 | /* '+' */ |
| 6613 | case '+': |
| 6614 | set1->val.num += set2->val.num; |
| 6615 | break; |
| 6616 | |
| 6617 | /* '-' */ |
| 6618 | case '-': |
| 6619 | set1->val.num -= set2->val.num; |
| 6620 | break; |
| 6621 | |
| 6622 | /* '*' */ |
| 6623 | case '*': |
| 6624 | set1->val.num *= set2->val.num; |
| 6625 | break; |
| 6626 | |
| 6627 | /* 'div' */ |
| 6628 | case 'd': |
| 6629 | set1->val.num /= set2->val.num; |
| 6630 | break; |
| 6631 | |
| 6632 | /* 'mod' */ |
| 6633 | case 'm': |
| 6634 | set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num); |
| 6635 | break; |
| 6636 | |
| 6637 | default: |
| 6638 | LOGINT_RET(set1->ctx); |
| 6639 | } |
| 6640 | |
| 6641 | return LY_SUCCESS; |
| 6642 | } |
| 6643 | |
| 6644 | /* |
| 6645 | * eval functions |
| 6646 | * |
| 6647 | * They execute a parsed XPath expression on some data subtree. |
| 6648 | */ |
| 6649 | |
| 6650 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6651 | * @brief Evaluate Predicate. Logs directly on error. |
| 6652 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6653 | * [9] Predicate ::= '[' Expr ']' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6654 | * |
| 6655 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6656 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6657 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 6658 | * @param[in] options XPath options. |
| 6659 | * @param[in] parent_pos_pred Whether parent predicate was a positional one. |
| 6660 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6661 | */ |
| 6662 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6663 | 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] | 6664 | { |
| 6665 | LY_ERR rc; |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 6666 | uint16_t i, orig_exp; |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6667 | uint32_t orig_pos, orig_size; |
| 6668 | int32_t pred_in_ctx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6669 | struct lyxp_set set2; |
| 6670 | struct lyd_node *orig_parent; |
| 6671 | |
| 6672 | /* '[' */ |
| 6673 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6674 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 6675 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6676 | |
| 6677 | if (!set) { |
| 6678 | only_parse: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6679 | rc = eval_expr_select(exp, tok_idx, 0, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6680 | LY_CHECK_RET(rc); |
| 6681 | } else if (set->type == LYXP_SET_NODE_SET) { |
| 6682 | /* 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] | 6683 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6684 | |
| 6685 | /* empty set, nothing to evaluate */ |
| 6686 | if (!set->used) { |
| 6687 | goto only_parse; |
| 6688 | } |
| 6689 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6690 | orig_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6691 | orig_pos = 0; |
| 6692 | orig_size = set->used; |
| 6693 | orig_parent = NULL; |
Michal Vasko | 39dbf35 | 2020-05-21 10:08:59 +0200 | [diff] [blame] | 6694 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6695 | set_init(&set2, set); |
| 6696 | set_insert_node(&set2, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, 0); |
| 6697 | /* remember the node context position for position() and context size for last(), |
| 6698 | * predicates should always be evaluated with respect to the child axis (since we do |
| 6699 | * not support explicit axes) so we assign positions based on their parents */ |
| 6700 | if (parent_pos_pred && ((struct lyd_node *)set->val.nodes[i].node->parent != orig_parent)) { |
| 6701 | orig_parent = (struct lyd_node *)set->val.nodes[i].node->parent; |
| 6702 | orig_pos = 1; |
| 6703 | } else { |
| 6704 | ++orig_pos; |
| 6705 | } |
| 6706 | |
| 6707 | set2.ctx_pos = orig_pos; |
| 6708 | set2.ctx_size = orig_size; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6709 | *tok_idx = orig_exp; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6710 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6711 | rc = eval_expr_select(exp, tok_idx, 0, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6712 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6713 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6714 | return rc; |
| 6715 | } |
| 6716 | |
| 6717 | /* number is a position */ |
| 6718 | if (set2.type == LYXP_SET_NUMBER) { |
| 6719 | if ((long long)set2.val.num == orig_pos) { |
| 6720 | set2.val.num = 1; |
| 6721 | } else { |
| 6722 | set2.val.num = 0; |
| 6723 | } |
| 6724 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6725 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6726 | |
| 6727 | /* predicate satisfied or not? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6728 | if (!set2.val.bln) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6729 | set_remove_node_none(set, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6730 | } |
| 6731 | } |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6732 | set_remove_nodes_none(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6733 | |
| 6734 | } else if (set->type == LYXP_SET_SCNODE_SET) { |
| 6735 | for (i = 0; i < set->used; ++i) { |
| 6736 | if (set->val.scnodes[i].in_ctx == 1) { |
| 6737 | /* there is a currently-valid node */ |
| 6738 | break; |
| 6739 | } |
| 6740 | } |
| 6741 | /* empty set, nothing to evaluate */ |
| 6742 | if (i == set->used) { |
| 6743 | goto only_parse; |
| 6744 | } |
| 6745 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6746 | orig_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6747 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6748 | /* set special in_ctx to all the valid snodes */ |
| 6749 | pred_in_ctx = set_scnode_new_in_ctx(set); |
| 6750 | |
| 6751 | /* use the valid snodes one-by-one */ |
| 6752 | for (i = 0; i < set->used; ++i) { |
| 6753 | if (set->val.scnodes[i].in_ctx != pred_in_ctx) { |
| 6754 | continue; |
| 6755 | } |
| 6756 | set->val.scnodes[i].in_ctx = 1; |
| 6757 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6758 | *tok_idx = orig_exp; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6759 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6760 | rc = eval_expr_select(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6761 | LY_CHECK_RET(rc); |
| 6762 | |
| 6763 | set->val.scnodes[i].in_ctx = pred_in_ctx; |
| 6764 | } |
| 6765 | |
| 6766 | /* restore the state as it was before the predicate */ |
| 6767 | for (i = 0; i < set->used; ++i) { |
| 6768 | if (set->val.scnodes[i].in_ctx == 1) { |
| 6769 | set->val.scnodes[i].in_ctx = 0; |
| 6770 | } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) { |
| 6771 | set->val.scnodes[i].in_ctx = 1; |
| 6772 | } |
| 6773 | } |
| 6774 | |
| 6775 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6776 | set2.type = LYXP_SET_NODE_SET; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6777 | set_fill_set(&set2, set); |
| 6778 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6779 | rc = eval_expr_select(exp, tok_idx, 0, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6780 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6781 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6782 | return rc; |
| 6783 | } |
| 6784 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6785 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6786 | if (!set2.val.bln) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6787 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6788 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6789 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6790 | } |
| 6791 | |
| 6792 | /* ']' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6793 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6794 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6795 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 6796 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6797 | |
| 6798 | return LY_SUCCESS; |
| 6799 | } |
| 6800 | |
| 6801 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6802 | * @brief Evaluate Literal. Logs directly on error. |
| 6803 | * |
| 6804 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6805 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6806 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 6807 | */ |
| 6808 | static void |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6809 | 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] | 6810 | { |
| 6811 | if (set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6812 | if (exp->tok_len[*tok_idx] == 2) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6813 | set_fill_string(set, "", 0); |
| 6814 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6815 | 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] | 6816 | } |
| 6817 | } |
| 6818 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6819 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 6820 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6821 | } |
| 6822 | |
| 6823 | /** |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6824 | * @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] | 6825 | * |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6826 | * @param[in] exp Full parsed XPath expression. |
| 6827 | * @param[in,out] tok_idx Index in @p exp at the beginning of the predicate, is updated on success. |
| 6828 | * @param[in] scnode Found schema node as context for the predicate. |
| 6829 | * @param[in] format Format of any prefixes in key names/values. |
| 6830 | * @param[out] predicates Parsed predicates. |
| 6831 | * @param[out] pred_type Type of @p predicates. |
| 6832 | * @return LY_SUCCESS on success, |
| 6833 | * @return LY_ERR on any error. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6834 | */ |
| 6835 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6836 | eval_name_test_try_compile_predicates(struct lyxp_expr *exp, uint16_t *tok_idx, const struct lysc_node *scnode, |
| 6837 | 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] | 6838 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6839 | LY_ERR ret = LY_SUCCESS; |
| 6840 | uint16_t key_count, e_idx, pred_idx = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6841 | const struct lysc_node *key; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6842 | size_t pred_len; |
| 6843 | int prev_lo; |
| 6844 | struct lyxp_expr *exp2 = NULL; |
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 | assert(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6847 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6848 | if (scnode->nodetype == LYS_LIST) { |
| 6849 | /* get key count */ |
| 6850 | if (scnode->flags & LYS_KEYLESS) { |
| 6851 | return LY_EINVAL; |
| 6852 | } |
| 6853 | for (key_count = 0, key = lysc_node_children(scnode, 0); key && (key->flags & LYS_KEY); key = key->next, ++key_count); |
| 6854 | assert(key_count); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6855 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6856 | /* learn where the predicates end */ |
| 6857 | e_idx = *tok_idx; |
| 6858 | while (key_count) { |
| 6859 | /* '[' */ |
| 6860 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) { |
| 6861 | return LY_EINVAL; |
| 6862 | } |
| 6863 | ++e_idx; |
| 6864 | |
| 6865 | /* ']' */ |
| 6866 | while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) { |
| 6867 | ++e_idx; |
| 6868 | } |
| 6869 | ++e_idx; |
| 6870 | |
| 6871 | /* another presumably key predicate parsed */ |
| 6872 | --key_count; |
| 6873 | } |
| 6874 | |
| 6875 | if (key_count) { |
| 6876 | /* some keys missing for sure */ |
| 6877 | return LY_EINVAL; |
| 6878 | } |
| 6879 | } else { |
| 6880 | /* learn just where this single predicate ends */ |
| 6881 | e_idx = *tok_idx; |
| 6882 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6883 | /* '[' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6884 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) { |
| 6885 | return LY_EINVAL; |
| 6886 | } |
| 6887 | ++e_idx; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6888 | |
| 6889 | /* ']' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6890 | while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) { |
| 6891 | ++e_idx; |
| 6892 | } |
| 6893 | ++e_idx; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6894 | } |
| 6895 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6896 | /* get the joined length of all the keys predicates/of the single leaf-list predicate */ |
| 6897 | pred_len = (exp->tok_pos[e_idx - 1] + exp->tok_len[e_idx - 1]) - exp->tok_pos[*tok_idx]; |
| 6898 | |
| 6899 | /* turn logging off */ |
| 6900 | prev_lo = ly_log_options(0); |
| 6901 | |
| 6902 | /* parse the predicate(s) */ |
| 6903 | LY_CHECK_GOTO(ret = ly_path_parse_predicate(scnode->module->ctx, exp->expr + exp->tok_pos[*tok_idx], pred_len, |
| 6904 | LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp2), cleanup); |
| 6905 | |
| 6906 | /* compile */ |
| 6907 | switch (format) { |
| 6908 | case LYD_SCHEMA: |
| 6909 | ret = ly_path_compile_predicate(scnode->module, scnode, exp2, &pred_idx, lys_resolve_prefix, scnode->module, |
| 6910 | LYD_SCHEMA, predicates, pred_type); |
| 6911 | break; |
| 6912 | case LYD_JSON: |
| 6913 | ret = ly_path_compile_predicate(scnode->module, scnode, exp2, &pred_idx, lydjson_resolve_prefix, NULL, LYD_JSON, |
| 6914 | predicates, pred_type); |
| 6915 | break; |
| 6916 | case LYD_XML: |
| 6917 | ret = LY_EINT; |
| 6918 | goto cleanup; |
| 6919 | } |
| 6920 | LY_CHECK_GOTO(ret, cleanup); |
| 6921 | |
| 6922 | /* success, the predicate must include all the needed information for hash-based search */ |
| 6923 | *tok_idx = e_idx; |
| 6924 | |
| 6925 | cleanup: |
| 6926 | ly_log_options(prev_lo); |
| 6927 | lyxp_expr_free(scnode->module->ctx, exp2); |
| 6928 | return ret; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6929 | } |
| 6930 | |
| 6931 | /** |
| 6932 | * @brief Evaluate NameTest and any following Predicates. Logs directly on error. |
| 6933 | * |
| 6934 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 6935 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 6936 | * [7] NameTest ::= '*' | NCName ':' '*' | QName |
| 6937 | * |
| 6938 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6939 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6940 | * @param[in] attr_axis Whether to search attributes or standard nodes. |
| 6941 | * @param[in] all_desc Whether to search all the descendants or children only. |
| 6942 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 6943 | * @param[in] options XPath options. |
| 6944 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6945 | */ |
| 6946 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6947 | 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] | 6948 | int options) |
| 6949 | { |
| 6950 | int i; |
| 6951 | char *path; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6952 | const char *ncname, *ncname_dict = NULL; |
| 6953 | uint16_t ncname_len; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6954 | const struct lys_module *moveto_mod; |
| 6955 | const struct lysc_node *scnode = NULL, *tmp; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6956 | struct ly_path_predicate *predicates = NULL; |
| 6957 | enum ly_path_pred_type pred_type = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6958 | LY_ERR rc = LY_SUCCESS; |
| 6959 | |
| 6960 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6961 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 6962 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6963 | |
| 6964 | if (!set) { |
| 6965 | goto moveto; |
| 6966 | } |
| 6967 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6968 | ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]]; |
| 6969 | ncname_len = exp->tok_len[*tok_idx - 1]; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6970 | |
| 6971 | /* parse (and skip) module name */ |
| 6972 | rc = moveto_resolve_model(&ncname, &ncname_len, set, &moveto_mod); |
| 6973 | LY_CHECK_GOTO(rc, cleanup); |
| 6974 | |
| 6975 | if (moveto_mod && !attr_axis && !all_desc && (set->type == LYXP_SET_NODE_SET)) { |
| 6976 | /* find the matching schema node in some parent in the context */ |
| 6977 | for (i = 0; i < (signed)set->used; ++i) { |
| 6978 | if (set->val.nodes[i].type == set->root_type) { |
| 6979 | tmp = lys_find_child(NULL, moveto_mod, ncname, ncname_len, 0, 0); |
| 6980 | } else if ((set->val.nodes[i].type == LYXP_NODE_ELEM) |
| 6981 | && (!scnode || (lysc_data_parent(scnode) != set->val.nodes[i].node->schema))) { |
| 6982 | /* do not repeat the same search */ |
| 6983 | tmp = lys_find_child(set->val.nodes[i].node->schema, moveto_mod, ncname, ncname_len, 0, 0); |
| 6984 | } |
| 6985 | |
| 6986 | /* additional context check */ |
| 6987 | if (tmp && (set->root_type == LYXP_NODE_ROOT_CONFIG) && (tmp->flags & LYS_CONFIG_R)) { |
| 6988 | tmp = NULL; |
| 6989 | } |
| 6990 | |
| 6991 | if (tmp) { |
| 6992 | if (scnode) { |
| 6993 | /* we found a schema node with the same name but at different level, give up, too complicated */ |
| 6994 | scnode = NULL; |
| 6995 | break; |
| 6996 | } else { |
| 6997 | /* remember the found schema node and continue to make sure it can be used */ |
| 6998 | scnode = tmp; |
| 6999 | } |
| 7000 | tmp = NULL; |
| 7001 | } |
| 7002 | } |
| 7003 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7004 | if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) { |
| 7005 | /* try to create the predicates */ |
| 7006 | if (eval_name_test_try_compile_predicates(exp, tok_idx, scnode, set->format, &predicates, &pred_type)) { |
| 7007 | /* hashes cannot be used */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7008 | scnode = NULL; |
| 7009 | } |
| 7010 | } |
| 7011 | } |
| 7012 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7013 | if (!scnode && moveto_mod) { |
| 7014 | /* we are not able to match based on a schema node and not all the modules match, |
| 7015 | * use dictionary for efficient comparison */ |
| 7016 | ncname_dict = lydict_insert(set->ctx, ncname, ncname_len); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7017 | } |
| 7018 | |
| 7019 | moveto: |
| 7020 | /* move to the attribute(s), data node(s), or schema node(s) */ |
| 7021 | if (attr_axis) { |
| 7022 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 7023 | set_scnode_clear_ctx(set); |
| 7024 | } else { |
| 7025 | if (all_desc) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7026 | rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7027 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7028 | rc = moveto_attr(set, moveto_mod, ncname_dict); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7029 | } |
| 7030 | LY_CHECK_GOTO(rc, cleanup); |
| 7031 | } |
| 7032 | } else { |
| 7033 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 7034 | if (all_desc) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7035 | rc = moveto_scnode_alldesc(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7036 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7037 | rc = moveto_scnode(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7038 | } |
| 7039 | LY_CHECK_GOTO(rc, cleanup); |
| 7040 | |
| 7041 | for (i = set->used - 1; i > -1; --i) { |
| 7042 | if (set->val.scnodes[i].in_ctx > 0) { |
| 7043 | break; |
| 7044 | } |
| 7045 | } |
| 7046 | if (i == -1) { |
| 7047 | path = lysc_path(set->ctx_scnode, LYSC_PATH_LOG, NULL, 0); |
| 7048 | 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] | 7049 | exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]], |
| 7050 | 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] | 7051 | free(path); |
| 7052 | } |
| 7053 | } else { |
| 7054 | if (all_desc) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7055 | rc = moveto_node_alldesc(set, moveto_mod, ncname_dict); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7056 | } else { |
| 7057 | if (scnode) { |
| 7058 | /* we can find the nodes using hashes */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7059 | rc = moveto_node_hash(set, scnode, predicates); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7060 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7061 | rc = moveto_node(set, moveto_mod, ncname_dict); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7062 | } |
| 7063 | } |
| 7064 | LY_CHECK_GOTO(rc, cleanup); |
| 7065 | } |
| 7066 | } |
| 7067 | |
| 7068 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7069 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 7070 | rc = eval_predicate(exp, tok_idx, set, options, 1); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7071 | LY_CHECK_RET(rc); |
| 7072 | } |
| 7073 | |
| 7074 | cleanup: |
Michal Vasko | db51a8d | 2020-05-27 15:22:29 +0200 | [diff] [blame] | 7075 | if (set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7076 | lydict_remove(set->ctx, ncname_dict); |
| 7077 | ly_path_predicates_free(set->ctx, pred_type, scnode, predicates); |
Michal Vasko | db51a8d | 2020-05-27 15:22:29 +0200 | [diff] [blame] | 7078 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7079 | return rc; |
| 7080 | } |
| 7081 | |
| 7082 | /** |
| 7083 | * @brief Evaluate NodeType and any following Predicates. Logs directly on error. |
| 7084 | * |
| 7085 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 7086 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 7087 | * [8] NodeType ::= 'text' | 'node' |
| 7088 | * |
| 7089 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7090 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7091 | * @param[in] attr_axis Whether to search attributes or standard nodes. |
| 7092 | * @param[in] all_desc Whether to search all the descendants or children only. |
| 7093 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7094 | * @param[in] options XPath options. |
| 7095 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7096 | */ |
| 7097 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7098 | 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] | 7099 | struct lyxp_set *set, int options) |
| 7100 | { |
| 7101 | LY_ERR rc; |
| 7102 | |
| 7103 | /* TODO */ |
| 7104 | (void)attr_axis; |
| 7105 | (void)all_desc; |
| 7106 | |
| 7107 | if (set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7108 | assert(exp->tok_len[*tok_idx] == 4); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7109 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 7110 | set_scnode_clear_ctx(set); |
| 7111 | /* just for the debug message below */ |
| 7112 | set = NULL; |
| 7113 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7114 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7115 | rc = xpath_node(NULL, 0, set, options); |
| 7116 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7117 | assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4)); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7118 | rc = xpath_text(NULL, 0, set, options); |
| 7119 | } |
| 7120 | LY_CHECK_RET(rc); |
| 7121 | } |
| 7122 | } |
| 7123 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7124 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7125 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7126 | |
| 7127 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7128 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7129 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7130 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7131 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7132 | |
| 7133 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7134 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7135 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7136 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7137 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7138 | |
| 7139 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7140 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 7141 | rc = eval_predicate(exp, tok_idx, set, options, 1); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7142 | LY_CHECK_RET(rc); |
| 7143 | } |
| 7144 | |
| 7145 | return LY_SUCCESS; |
| 7146 | } |
| 7147 | |
| 7148 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7149 | * @brief Evaluate RelativeLocationPath. Logs directly on error. |
| 7150 | * |
| 7151 | * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step |
| 7152 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7153 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7154 | * |
| 7155 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7156 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7157 | * @param[in] all_desc Whether to search all the descendants or children only. |
| 7158 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7159 | * @param[in] options XPath options. |
| 7160 | * @return LY_ERR (YL_EINCOMPLETE on unresolved when) |
| 7161 | */ |
| 7162 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7163 | 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] | 7164 | { |
| 7165 | int attr_axis; |
| 7166 | LY_ERR rc; |
| 7167 | |
| 7168 | goto step; |
| 7169 | do { |
| 7170 | /* evaluate '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7171 | if (exp->tok_len[*tok_idx] == 1) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7172 | all_desc = 0; |
| 7173 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7174 | assert(exp->tok_len[*tok_idx] == 2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7175 | all_desc = 1; |
| 7176 | } |
| 7177 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7178 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7179 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7180 | |
| 7181 | step: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7182 | /* evaluate abbreviated axis '@'? if any */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7183 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7184 | attr_axis = 1; |
| 7185 | |
| 7186 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7187 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7188 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7189 | } else { |
| 7190 | attr_axis = 0; |
| 7191 | } |
| 7192 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7193 | /* Step */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7194 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7195 | case LYXP_TOKEN_DOT: |
| 7196 | /* evaluate '.' */ |
| 7197 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 7198 | rc = moveto_scnode_self(set, all_desc, options); |
| 7199 | } else { |
| 7200 | rc = moveto_self(set, all_desc, options); |
| 7201 | } |
| 7202 | LY_CHECK_RET(rc); |
| 7203 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7204 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7205 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7206 | break; |
| 7207 | |
| 7208 | case LYXP_TOKEN_DDOT: |
| 7209 | /* evaluate '..' */ |
| 7210 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 7211 | rc = moveto_scnode_parent(set, all_desc, options); |
| 7212 | } else { |
| 7213 | rc = moveto_parent(set, all_desc, options); |
| 7214 | } |
| 7215 | LY_CHECK_RET(rc); |
| 7216 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7217 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7218 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7219 | break; |
| 7220 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7221 | case LYXP_TOKEN_NAMETEST: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7222 | /* evaluate NameTest Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7223 | 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] | 7224 | LY_CHECK_RET(rc); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7225 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7226 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7227 | case LYXP_TOKEN_NODETYPE: |
| 7228 | /* evaluate NodeType Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7229 | 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] | 7230 | LY_CHECK_RET(rc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7231 | break; |
| 7232 | |
| 7233 | default: |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 7234 | LOGINT_RET(set ? set->ctx : NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7235 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7236 | } 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] | 7237 | |
| 7238 | return LY_SUCCESS; |
| 7239 | } |
| 7240 | |
| 7241 | /** |
| 7242 | * @brief Evaluate AbsoluteLocationPath. Logs directly on error. |
| 7243 | * |
| 7244 | * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath |
| 7245 | * |
| 7246 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7247 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7248 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7249 | * @param[in] options XPath options. |
| 7250 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7251 | */ |
| 7252 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7253 | 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] | 7254 | { |
| 7255 | int all_desc; |
| 7256 | LY_ERR rc; |
| 7257 | |
| 7258 | if (set) { |
| 7259 | /* no matter what tokens follow, we need to be at the root */ |
| 7260 | moveto_root(set, options); |
| 7261 | } |
| 7262 | |
| 7263 | /* '/' RelativeLocationPath? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7264 | if (exp->tok_len[*tok_idx] == 1) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7265 | /* evaluate '/' - deferred */ |
| 7266 | all_desc = 0; |
| 7267 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7268 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7269 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7270 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7271 | if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7272 | return LY_SUCCESS; |
| 7273 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7274 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7275 | case LYXP_TOKEN_DOT: |
| 7276 | case LYXP_TOKEN_DDOT: |
| 7277 | case LYXP_TOKEN_AT: |
| 7278 | case LYXP_TOKEN_NAMETEST: |
| 7279 | case LYXP_TOKEN_NODETYPE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7280 | rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7281 | LY_CHECK_RET(rc); |
| 7282 | break; |
| 7283 | default: |
| 7284 | break; |
| 7285 | } |
| 7286 | |
| 7287 | /* '//' RelativeLocationPath */ |
| 7288 | } else { |
| 7289 | /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */ |
| 7290 | all_desc = 1; |
| 7291 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7292 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7293 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7294 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7295 | rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7296 | LY_CHECK_RET(rc); |
| 7297 | } |
| 7298 | |
| 7299 | return LY_SUCCESS; |
| 7300 | } |
| 7301 | |
| 7302 | /** |
| 7303 | * @brief Evaluate FunctionCall. Logs directly on error. |
| 7304 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7305 | * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7306 | * |
| 7307 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7308 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7309 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7310 | * @param[in] options XPath options. |
| 7311 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7312 | */ |
| 7313 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7314 | 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] | 7315 | { |
| 7316 | LY_ERR rc; |
| 7317 | 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] | 7318 | uint16_t arg_count = 0, i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7319 | struct lyxp_set **args = NULL, **args_aux; |
| 7320 | |
| 7321 | if (set) { |
| 7322 | /* FunctionName */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7323 | switch (exp->tok_len[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7324 | case 3: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7325 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7326 | xpath_func = &xpath_not; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7327 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7328 | xpath_func = &xpath_sum; |
| 7329 | } |
| 7330 | break; |
| 7331 | case 4: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7332 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7333 | xpath_func = &xpath_lang; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7334 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7335 | xpath_func = &xpath_last; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7336 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7337 | xpath_func = &xpath_name; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7338 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7339 | xpath_func = &xpath_true; |
| 7340 | } |
| 7341 | break; |
| 7342 | case 5: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7343 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7344 | xpath_func = &xpath_count; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7345 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7346 | xpath_func = &xpath_false; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7347 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7348 | xpath_func = &xpath_floor; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7349 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7350 | xpath_func = &xpath_round; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7351 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7352 | xpath_func = &xpath_deref; |
| 7353 | } |
| 7354 | break; |
| 7355 | case 6: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7356 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7357 | xpath_func = &xpath_concat; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7358 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7359 | xpath_func = &xpath_number; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7360 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7361 | xpath_func = &xpath_string; |
| 7362 | } |
| 7363 | break; |
| 7364 | case 7: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7365 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7366 | xpath_func = &xpath_boolean; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7367 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7368 | xpath_func = &xpath_ceiling; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7369 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7370 | xpath_func = &xpath_current; |
| 7371 | } |
| 7372 | break; |
| 7373 | case 8: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7374 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7375 | xpath_func = &xpath_contains; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7376 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7377 | xpath_func = &xpath_position; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7378 | } 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] | 7379 | xpath_func = &xpath_re_match; |
| 7380 | } |
| 7381 | break; |
| 7382 | case 9: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7383 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7384 | xpath_func = &xpath_substring; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7385 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7386 | xpath_func = &xpath_translate; |
| 7387 | } |
| 7388 | break; |
| 7389 | case 10: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7390 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7391 | xpath_func = &xpath_local_name; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7392 | } 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] | 7393 | xpath_func = &xpath_enum_value; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7394 | } 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] | 7395 | xpath_func = &xpath_bit_is_set; |
| 7396 | } |
| 7397 | break; |
| 7398 | case 11: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7399 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7400 | xpath_func = &xpath_starts_with; |
| 7401 | } |
| 7402 | break; |
| 7403 | case 12: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7404 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7405 | xpath_func = &xpath_derived_from; |
| 7406 | } |
| 7407 | break; |
| 7408 | case 13: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7409 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7410 | xpath_func = &xpath_namespace_uri; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7411 | } 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] | 7412 | xpath_func = &xpath_string_length; |
| 7413 | } |
| 7414 | break; |
| 7415 | case 15: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7416 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7417 | xpath_func = &xpath_normalize_space; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7418 | } 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] | 7419 | xpath_func = &xpath_substring_after; |
| 7420 | } |
| 7421 | break; |
| 7422 | case 16: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7423 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7424 | xpath_func = &xpath_substring_before; |
| 7425 | } |
| 7426 | break; |
| 7427 | case 20: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7428 | 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] | 7429 | xpath_func = &xpath_derived_from_or_self; |
| 7430 | } |
| 7431 | break; |
| 7432 | } |
| 7433 | |
| 7434 | if (!xpath_func) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7435 | 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] | 7436 | return LY_EVALID; |
| 7437 | } |
| 7438 | } |
| 7439 | |
| 7440 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7441 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7442 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7443 | |
| 7444 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7445 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7446 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7447 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7448 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7449 | |
| 7450 | /* ( Expr ( ',' Expr )* )? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7451 | if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7452 | if (set) { |
| 7453 | args = malloc(sizeof *args); |
| 7454 | LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup); |
| 7455 | arg_count = 1; |
| 7456 | args[0] = set_copy(set); |
| 7457 | if (!args[0]) { |
| 7458 | rc = LY_EMEM; |
| 7459 | goto cleanup; |
| 7460 | } |
| 7461 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7462 | rc = eval_expr_select(exp, tok_idx, 0, args[0], options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7463 | LY_CHECK_GOTO(rc, cleanup); |
| 7464 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7465 | rc = eval_expr_select(exp, tok_idx, 0, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7466 | LY_CHECK_GOTO(rc, cleanup); |
| 7467 | } |
| 7468 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7469 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7470 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7471 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7472 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7473 | |
| 7474 | if (set) { |
| 7475 | ++arg_count; |
| 7476 | args_aux = realloc(args, arg_count * sizeof *args); |
| 7477 | LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup); |
| 7478 | args = args_aux; |
| 7479 | args[arg_count - 1] = set_copy(set); |
| 7480 | if (!args[arg_count - 1]) { |
| 7481 | rc = LY_EMEM; |
| 7482 | goto cleanup; |
| 7483 | } |
| 7484 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7485 | 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] | 7486 | LY_CHECK_GOTO(rc, cleanup); |
| 7487 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7488 | rc = eval_expr_select(exp, tok_idx, 0, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7489 | LY_CHECK_GOTO(rc, cleanup); |
| 7490 | } |
| 7491 | } |
| 7492 | |
| 7493 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7494 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7495 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7496 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7497 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7498 | |
| 7499 | if (set) { |
| 7500 | /* evaluate function */ |
| 7501 | rc = xpath_func(args, arg_count, set, options); |
| 7502 | |
| 7503 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7504 | /* merge all nodes from arg evaluations */ |
| 7505 | for (i = 0; i < arg_count; ++i) { |
| 7506 | set_scnode_clear_ctx(args[i]); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7507 | lyxp_set_scnode_merge(set, args[i]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7508 | } |
| 7509 | } |
| 7510 | } else { |
| 7511 | rc = LY_SUCCESS; |
| 7512 | } |
| 7513 | |
| 7514 | cleanup: |
| 7515 | for (i = 0; i < arg_count; ++i) { |
| 7516 | lyxp_set_free(args[i]); |
| 7517 | } |
| 7518 | free(args); |
| 7519 | |
| 7520 | return rc; |
| 7521 | } |
| 7522 | |
| 7523 | /** |
| 7524 | * @brief Evaluate Number. Logs directly on error. |
| 7525 | * |
| 7526 | * @param[in] ctx Context for errors. |
| 7527 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7528 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7529 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7530 | * @return LY_ERR |
| 7531 | */ |
| 7532 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7533 | 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] | 7534 | { |
| 7535 | long double num; |
| 7536 | char *endptr; |
| 7537 | |
| 7538 | if (set) { |
| 7539 | errno = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7540 | num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7541 | if (errno) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7542 | 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] | 7543 | 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] | 7544 | 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] | 7545 | return LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7546 | } else if (endptr - &exp->expr[exp->tok_pos[*tok_idx]] != exp->tok_len[*tok_idx]) { |
| 7547 | 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] | 7548 | 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] | 7549 | exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7550 | return LY_EVALID; |
| 7551 | } |
| 7552 | |
| 7553 | set_fill_number(set, num); |
| 7554 | } |
| 7555 | |
| 7556 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7557 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7558 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7559 | return LY_SUCCESS; |
| 7560 | } |
| 7561 | |
| 7562 | /** |
| 7563 | * @brief Evaluate PathExpr. Logs directly on error. |
| 7564 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7565 | * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate* |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7566 | * | PrimaryExpr Predicate* '/' RelativeLocationPath |
| 7567 | * | PrimaryExpr Predicate* '//' RelativeLocationPath |
| 7568 | * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7569 | * [10] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7570 | * |
| 7571 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7572 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7573 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7574 | * @param[in] options XPath options. |
| 7575 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7576 | */ |
| 7577 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7578 | 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] | 7579 | { |
| 7580 | int all_desc, parent_pos_pred; |
| 7581 | LY_ERR rc; |
| 7582 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7583 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7584 | case LYXP_TOKEN_PAR1: |
| 7585 | /* '(' Expr ')' */ |
| 7586 | |
| 7587 | /* '(' */ |
| 7588 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7589 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7590 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7591 | |
| 7592 | /* Expr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7593 | rc = eval_expr_select(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7594 | LY_CHECK_RET(rc); |
| 7595 | |
| 7596 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7597 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7598 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7599 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7600 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7601 | |
| 7602 | parent_pos_pred = 0; |
| 7603 | goto predicate; |
| 7604 | |
| 7605 | case LYXP_TOKEN_DOT: |
| 7606 | case LYXP_TOKEN_DDOT: |
| 7607 | case LYXP_TOKEN_AT: |
| 7608 | case LYXP_TOKEN_NAMETEST: |
| 7609 | case LYXP_TOKEN_NODETYPE: |
| 7610 | /* RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7611 | rc = eval_relative_location_path(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7612 | LY_CHECK_RET(rc); |
| 7613 | break; |
| 7614 | |
| 7615 | case LYXP_TOKEN_FUNCNAME: |
| 7616 | /* FunctionCall */ |
| 7617 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7618 | rc = eval_function_call(exp, tok_idx, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7619 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7620 | rc = eval_function_call(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7621 | } |
| 7622 | LY_CHECK_RET(rc); |
| 7623 | |
| 7624 | parent_pos_pred = 1; |
| 7625 | goto predicate; |
| 7626 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 7627 | case LYXP_TOKEN_OPER_PATH: |
| 7628 | case LYXP_TOKEN_OPER_RPATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7629 | /* AbsoluteLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7630 | rc = eval_absolute_location_path(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7631 | LY_CHECK_RET(rc); |
| 7632 | break; |
| 7633 | |
| 7634 | case LYXP_TOKEN_LITERAL: |
| 7635 | /* Literal */ |
| 7636 | if (!set || (options & LYXP_SCNODE_ALL)) { |
| 7637 | if (set) { |
| 7638 | set_scnode_clear_ctx(set); |
| 7639 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7640 | eval_literal(exp, tok_idx, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7641 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7642 | eval_literal(exp, tok_idx, set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7643 | } |
| 7644 | |
| 7645 | parent_pos_pred = 1; |
| 7646 | goto predicate; |
| 7647 | |
| 7648 | case LYXP_TOKEN_NUMBER: |
| 7649 | /* Number */ |
| 7650 | if (!set || (options & LYXP_SCNODE_ALL)) { |
| 7651 | if (set) { |
| 7652 | set_scnode_clear_ctx(set); |
| 7653 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7654 | rc = eval_number(NULL, exp, tok_idx, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7655 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7656 | rc = eval_number(set->ctx, exp, tok_idx, set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7657 | } |
| 7658 | LY_CHECK_RET(rc); |
| 7659 | |
| 7660 | parent_pos_pred = 1; |
| 7661 | goto predicate; |
| 7662 | |
| 7663 | default: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7664 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[*tok_idx]), |
| 7665 | &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7666 | return LY_EVALID; |
| 7667 | } |
| 7668 | |
| 7669 | return LY_SUCCESS; |
| 7670 | |
| 7671 | predicate: |
| 7672 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7673 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 7674 | rc = eval_predicate(exp, tok_idx, set, options, parent_pos_pred); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7675 | LY_CHECK_RET(rc); |
| 7676 | } |
| 7677 | |
| 7678 | /* ('/' or '//') RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7679 | 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] | 7680 | |
| 7681 | /* evaluate '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7682 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7683 | all_desc = 0; |
| 7684 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7685 | all_desc = 1; |
| 7686 | } |
| 7687 | |
| 7688 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7689 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7690 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7691 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7692 | rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7693 | LY_CHECK_RET(rc); |
| 7694 | } |
| 7695 | |
| 7696 | return LY_SUCCESS; |
| 7697 | } |
| 7698 | |
| 7699 | /** |
| 7700 | * @brief Evaluate UnionExpr. Logs directly on error. |
| 7701 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7702 | * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7703 | * |
| 7704 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7705 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7706 | * @param[in] repeat How many times this expression is repeated. |
| 7707 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7708 | * @param[in] options XPath options. |
| 7709 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7710 | */ |
| 7711 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7712 | 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] | 7713 | { |
| 7714 | LY_ERR rc = LY_SUCCESS; |
| 7715 | struct lyxp_set orig_set, set2; |
| 7716 | uint16_t i; |
| 7717 | |
| 7718 | assert(repeat); |
| 7719 | |
| 7720 | set_init(&orig_set, set); |
| 7721 | set_init(&set2, set); |
| 7722 | |
| 7723 | set_fill_set(&orig_set, set); |
| 7724 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7725 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7726 | LY_CHECK_GOTO(rc, cleanup); |
| 7727 | |
| 7728 | /* ('|' PathExpr)* */ |
| 7729 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7730 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7731 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7732 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7733 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7734 | |
| 7735 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7736 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7737 | LY_CHECK_GOTO(rc, cleanup); |
| 7738 | continue; |
| 7739 | } |
| 7740 | |
| 7741 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7742 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7743 | LY_CHECK_GOTO(rc, cleanup); |
| 7744 | |
| 7745 | /* eval */ |
| 7746 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7747 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7748 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7749 | rc = moveto_union(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7750 | LY_CHECK_GOTO(rc, cleanup); |
| 7751 | } |
| 7752 | } |
| 7753 | |
| 7754 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7755 | lyxp_set_free_content(&orig_set); |
| 7756 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7757 | return rc; |
| 7758 | } |
| 7759 | |
| 7760 | /** |
| 7761 | * @brief Evaluate UnaryExpr. Logs directly on error. |
| 7762 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7763 | * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7764 | * |
| 7765 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7766 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7767 | * @param[in] repeat How many times this expression is repeated. |
| 7768 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7769 | * @param[in] options XPath options. |
| 7770 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7771 | */ |
| 7772 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7773 | 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] | 7774 | { |
| 7775 | LY_ERR rc; |
| 7776 | uint16_t this_op, i; |
| 7777 | |
| 7778 | assert(repeat); |
| 7779 | |
| 7780 | /* ('-')+ */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7781 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7782 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7783 | 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] | 7784 | |
| 7785 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7786 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7787 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7788 | } |
| 7789 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7790 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7791 | LY_CHECK_RET(rc); |
| 7792 | |
| 7793 | if (set && (repeat % 2)) { |
| 7794 | if (options & LYXP_SCNODE_ALL) { |
| 7795 | warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]); |
| 7796 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7797 | 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] | 7798 | LY_CHECK_RET(rc); |
| 7799 | } |
| 7800 | } |
| 7801 | |
| 7802 | return LY_SUCCESS; |
| 7803 | } |
| 7804 | |
| 7805 | /** |
| 7806 | * @brief Evaluate MultiplicativeExpr. Logs directly on error. |
| 7807 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7808 | * [18] MultiplicativeExpr ::= UnaryExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7809 | * | MultiplicativeExpr '*' UnaryExpr |
| 7810 | * | MultiplicativeExpr 'div' UnaryExpr |
| 7811 | * | MultiplicativeExpr 'mod' UnaryExpr |
| 7812 | * |
| 7813 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7814 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7815 | * @param[in] repeat How many times this expression is repeated. |
| 7816 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7817 | * @param[in] options XPath options. |
| 7818 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7819 | */ |
| 7820 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7821 | 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] | 7822 | { |
| 7823 | LY_ERR rc; |
| 7824 | uint16_t this_op; |
| 7825 | struct lyxp_set orig_set, set2; |
| 7826 | uint16_t i; |
| 7827 | |
| 7828 | assert(repeat); |
| 7829 | |
| 7830 | set_init(&orig_set, set); |
| 7831 | set_init(&set2, set); |
| 7832 | |
| 7833 | set_fill_set(&orig_set, set); |
| 7834 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7835 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7836 | LY_CHECK_GOTO(rc, cleanup); |
| 7837 | |
| 7838 | /* ('*' / 'div' / 'mod' UnaryExpr)* */ |
| 7839 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7840 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7841 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7842 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7843 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7844 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7845 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7846 | |
| 7847 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7848 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7849 | LY_CHECK_GOTO(rc, cleanup); |
| 7850 | continue; |
| 7851 | } |
| 7852 | |
| 7853 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7854 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7855 | LY_CHECK_GOTO(rc, cleanup); |
| 7856 | |
| 7857 | /* eval */ |
| 7858 | if (options & LYXP_SCNODE_ALL) { |
| 7859 | 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] | 7860 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7861 | set_scnode_clear_ctx(set); |
| 7862 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7863 | 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] | 7864 | LY_CHECK_GOTO(rc, cleanup); |
| 7865 | } |
| 7866 | } |
| 7867 | |
| 7868 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7869 | lyxp_set_free_content(&orig_set); |
| 7870 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7871 | return rc; |
| 7872 | } |
| 7873 | |
| 7874 | /** |
| 7875 | * @brief Evaluate AdditiveExpr. Logs directly on error. |
| 7876 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7877 | * [17] AdditiveExpr ::= MultiplicativeExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7878 | * | AdditiveExpr '+' MultiplicativeExpr |
| 7879 | * | AdditiveExpr '-' MultiplicativeExpr |
| 7880 | * |
| 7881 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7882 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7883 | * @param[in] repeat How many times this expression is repeated. |
| 7884 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7885 | * @param[in] options XPath options. |
| 7886 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7887 | */ |
| 7888 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7889 | 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] | 7890 | { |
| 7891 | LY_ERR rc; |
| 7892 | uint16_t this_op; |
| 7893 | struct lyxp_set orig_set, set2; |
| 7894 | uint16_t i; |
| 7895 | |
| 7896 | assert(repeat); |
| 7897 | |
| 7898 | set_init(&orig_set, set); |
| 7899 | set_init(&set2, set); |
| 7900 | |
| 7901 | set_fill_set(&orig_set, set); |
| 7902 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7903 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7904 | LY_CHECK_GOTO(rc, cleanup); |
| 7905 | |
| 7906 | /* ('+' / '-' MultiplicativeExpr)* */ |
| 7907 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7908 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7909 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7910 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7911 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7912 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7913 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7914 | |
| 7915 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7916 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7917 | LY_CHECK_GOTO(rc, cleanup); |
| 7918 | continue; |
| 7919 | } |
| 7920 | |
| 7921 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7922 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7923 | LY_CHECK_GOTO(rc, cleanup); |
| 7924 | |
| 7925 | /* eval */ |
| 7926 | if (options & LYXP_SCNODE_ALL) { |
| 7927 | 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] | 7928 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7929 | set_scnode_clear_ctx(set); |
| 7930 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7931 | 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] | 7932 | LY_CHECK_GOTO(rc, cleanup); |
| 7933 | } |
| 7934 | } |
| 7935 | |
| 7936 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7937 | lyxp_set_free_content(&orig_set); |
| 7938 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7939 | return rc; |
| 7940 | } |
| 7941 | |
| 7942 | /** |
| 7943 | * @brief Evaluate RelationalExpr. Logs directly on error. |
| 7944 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7945 | * [16] RelationalExpr ::= AdditiveExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7946 | * | RelationalExpr '<' AdditiveExpr |
| 7947 | * | RelationalExpr '>' AdditiveExpr |
| 7948 | * | RelationalExpr '<=' AdditiveExpr |
| 7949 | * | RelationalExpr '>=' AdditiveExpr |
| 7950 | * |
| 7951 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7952 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7953 | * @param[in] repeat How many times this expression is repeated. |
| 7954 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7955 | * @param[in] options XPath options. |
| 7956 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7957 | */ |
| 7958 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7959 | 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] | 7960 | { |
| 7961 | LY_ERR rc; |
| 7962 | uint16_t this_op; |
| 7963 | struct lyxp_set orig_set, set2; |
| 7964 | uint16_t i; |
| 7965 | |
| 7966 | assert(repeat); |
| 7967 | |
| 7968 | set_init(&orig_set, set); |
| 7969 | set_init(&set2, set); |
| 7970 | |
| 7971 | set_fill_set(&orig_set, set); |
| 7972 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7973 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7974 | LY_CHECK_GOTO(rc, cleanup); |
| 7975 | |
| 7976 | /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */ |
| 7977 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7978 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7979 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7980 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7981 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7982 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7983 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7984 | |
| 7985 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7986 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7987 | LY_CHECK_GOTO(rc, cleanup); |
| 7988 | continue; |
| 7989 | } |
| 7990 | |
| 7991 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7992 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7993 | LY_CHECK_GOTO(rc, cleanup); |
| 7994 | |
| 7995 | /* eval */ |
| 7996 | if (options & LYXP_SCNODE_ALL) { |
| 7997 | 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] | 7998 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7999 | set_scnode_clear_ctx(set); |
| 8000 | } else { |
| 8001 | rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options); |
| 8002 | LY_CHECK_GOTO(rc, cleanup); |
| 8003 | } |
| 8004 | } |
| 8005 | |
| 8006 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8007 | lyxp_set_free_content(&orig_set); |
| 8008 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8009 | return rc; |
| 8010 | } |
| 8011 | |
| 8012 | /** |
| 8013 | * @brief Evaluate EqualityExpr. Logs directly on error. |
| 8014 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8015 | * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8016 | * | EqualityExpr '!=' RelationalExpr |
| 8017 | * |
| 8018 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8019 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8020 | * @param[in] repeat How many times this expression is repeated. |
| 8021 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8022 | * @param[in] options XPath options. |
| 8023 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8024 | */ |
| 8025 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8026 | 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] | 8027 | { |
| 8028 | LY_ERR rc; |
| 8029 | uint16_t this_op; |
| 8030 | struct lyxp_set orig_set, set2; |
| 8031 | uint16_t i; |
| 8032 | |
| 8033 | assert(repeat); |
| 8034 | |
| 8035 | set_init(&orig_set, set); |
| 8036 | set_init(&set2, set); |
| 8037 | |
| 8038 | set_fill_set(&orig_set, set); |
| 8039 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8040 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8041 | LY_CHECK_GOTO(rc, cleanup); |
| 8042 | |
| 8043 | /* ('=' / '!=' RelationalExpr)* */ |
| 8044 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8045 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8046 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8047 | 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] | 8048 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8049 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 8050 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8051 | |
| 8052 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8053 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8054 | LY_CHECK_GOTO(rc, cleanup); |
| 8055 | continue; |
| 8056 | } |
| 8057 | |
| 8058 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8059 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8060 | LY_CHECK_GOTO(rc, cleanup); |
| 8061 | |
| 8062 | /* eval */ |
| 8063 | if (options & LYXP_SCNODE_ALL) { |
| 8064 | 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] | 8065 | warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1); |
| 8066 | 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] | 8067 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8068 | set_scnode_clear_ctx(set); |
| 8069 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8070 | rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options); |
| 8071 | LY_CHECK_GOTO(rc, cleanup); |
| 8072 | } |
| 8073 | } |
| 8074 | |
| 8075 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8076 | lyxp_set_free_content(&orig_set); |
| 8077 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8078 | return rc; |
| 8079 | } |
| 8080 | |
| 8081 | /** |
| 8082 | * @brief Evaluate AndExpr. Logs directly on error. |
| 8083 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8084 | * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8085 | * |
| 8086 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8087 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8088 | * @param[in] repeat How many times this expression is repeated. |
| 8089 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8090 | * @param[in] options XPath options. |
| 8091 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8092 | */ |
| 8093 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8094 | 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] | 8095 | { |
| 8096 | LY_ERR rc; |
| 8097 | struct lyxp_set orig_set, set2; |
| 8098 | uint16_t i; |
| 8099 | |
| 8100 | assert(repeat); |
| 8101 | |
| 8102 | set_init(&orig_set, set); |
| 8103 | set_init(&set2, set); |
| 8104 | |
| 8105 | set_fill_set(&orig_set, set); |
| 8106 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8107 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8108 | LY_CHECK_GOTO(rc, cleanup); |
| 8109 | |
| 8110 | /* cast to boolean, we know that will be the final result */ |
| 8111 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 8112 | set_scnode_clear_ctx(set); |
| 8113 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8114 | lyxp_set_cast(set, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8115 | } |
| 8116 | |
| 8117 | /* ('and' EqualityExpr)* */ |
| 8118 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8119 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG); |
| 8120 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || !set->val.bln ? "skipped" : "parsed"), |
| 8121 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 8122 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8123 | |
| 8124 | /* lazy evaluation */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8125 | if (!set || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) { |
| 8126 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8127 | LY_CHECK_GOTO(rc, cleanup); |
| 8128 | continue; |
| 8129 | } |
| 8130 | |
| 8131 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8132 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8133 | LY_CHECK_GOTO(rc, cleanup); |
| 8134 | |
| 8135 | /* eval - just get boolean value actually */ |
| 8136 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 8137 | set_scnode_clear_ctx(&set2); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 8138 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8139 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8140 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8141 | set_fill_set(set, &set2); |
| 8142 | } |
| 8143 | } |
| 8144 | |
| 8145 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8146 | lyxp_set_free_content(&orig_set); |
| 8147 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8148 | return rc; |
| 8149 | } |
| 8150 | |
| 8151 | /** |
| 8152 | * @brief Evaluate OrExpr. Logs directly on error. |
| 8153 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8154 | * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8155 | * |
| 8156 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8157 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8158 | * @param[in] repeat How many times this expression is repeated. |
| 8159 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8160 | * @param[in] options XPath options. |
| 8161 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8162 | */ |
| 8163 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8164 | 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] | 8165 | { |
| 8166 | LY_ERR rc; |
| 8167 | struct lyxp_set orig_set, set2; |
| 8168 | uint16_t i; |
| 8169 | |
| 8170 | assert(repeat); |
| 8171 | |
| 8172 | set_init(&orig_set, set); |
| 8173 | set_init(&set2, set); |
| 8174 | |
| 8175 | set_fill_set(&orig_set, set); |
| 8176 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8177 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8178 | LY_CHECK_GOTO(rc, cleanup); |
| 8179 | |
| 8180 | /* cast to boolean, we know that will be the final result */ |
| 8181 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 8182 | set_scnode_clear_ctx(set); |
| 8183 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8184 | lyxp_set_cast(set, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8185 | } |
| 8186 | |
| 8187 | /* ('or' AndExpr)* */ |
| 8188 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8189 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG); |
| 8190 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || set->val.bln ? "skipped" : "parsed"), |
| 8191 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 8192 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8193 | |
| 8194 | /* lazy evaluation */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8195 | if (!set || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) { |
| 8196 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8197 | LY_CHECK_GOTO(rc, cleanup); |
| 8198 | continue; |
| 8199 | } |
| 8200 | |
| 8201 | set_fill_set(&set2, &orig_set); |
| 8202 | /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one), |
| 8203 | * but it does not matter */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8204 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8205 | LY_CHECK_GOTO(rc, cleanup); |
| 8206 | |
| 8207 | /* eval - just get boolean value actually */ |
| 8208 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 8209 | set_scnode_clear_ctx(&set2); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 8210 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8211 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8212 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8213 | set_fill_set(set, &set2); |
| 8214 | } |
| 8215 | } |
| 8216 | |
| 8217 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8218 | lyxp_set_free_content(&orig_set); |
| 8219 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8220 | return rc; |
| 8221 | } |
| 8222 | |
| 8223 | /** |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8224 | * @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] | 8225 | * |
| 8226 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8227 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8228 | * @param[in] etype Expression type to evaluate. |
| 8229 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8230 | * @param[in] options XPath options. |
| 8231 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8232 | */ |
| 8233 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8234 | 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] | 8235 | { |
| 8236 | uint16_t i, count; |
| 8237 | enum lyxp_expr_type next_etype; |
| 8238 | LY_ERR rc; |
| 8239 | |
| 8240 | /* process operator repeats */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8241 | if (!exp->repeat[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8242 | next_etype = LYXP_EXPR_NONE; |
| 8243 | } else { |
| 8244 | /* find etype repeat */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8245 | for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8246 | |
| 8247 | /* select one-priority lower because etype expression called us */ |
| 8248 | if (i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8249 | next_etype = exp->repeat[*tok_idx][i - 1]; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8250 | /* count repeats for that expression */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8251 | 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] | 8252 | } else { |
| 8253 | next_etype = LYXP_EXPR_NONE; |
| 8254 | } |
| 8255 | } |
| 8256 | |
| 8257 | /* decide what expression are we parsing based on the repeat */ |
| 8258 | switch (next_etype) { |
| 8259 | case LYXP_EXPR_OR: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8260 | rc = eval_or_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8261 | break; |
| 8262 | case LYXP_EXPR_AND: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8263 | rc = eval_and_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8264 | break; |
| 8265 | case LYXP_EXPR_EQUALITY: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8266 | rc = eval_equality_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8267 | break; |
| 8268 | case LYXP_EXPR_RELATIONAL: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8269 | rc = eval_relational_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8270 | break; |
| 8271 | case LYXP_EXPR_ADDITIVE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8272 | rc = eval_additive_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8273 | break; |
| 8274 | case LYXP_EXPR_MULTIPLICATIVE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8275 | rc = eval_multiplicative_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8276 | break; |
| 8277 | case LYXP_EXPR_UNARY: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8278 | rc = eval_unary_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8279 | break; |
| 8280 | case LYXP_EXPR_UNION: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8281 | rc = eval_union_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8282 | break; |
| 8283 | case LYXP_EXPR_NONE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8284 | rc = eval_path_expr(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8285 | break; |
| 8286 | default: |
| 8287 | LOGINT_RET(set->ctx); |
| 8288 | } |
| 8289 | |
| 8290 | return rc; |
| 8291 | } |
| 8292 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8293 | /** |
| 8294 | * @brief Get root type. |
| 8295 | * |
| 8296 | * @param[in] ctx_node Context node. |
| 8297 | * @param[in] ctx_scnode Schema context node. |
| 8298 | * @param[in] options XPath options. |
| 8299 | * @return Root type. |
| 8300 | */ |
| 8301 | static enum lyxp_node_type |
| 8302 | lyxp_get_root_type(const struct lyd_node *ctx_node, const struct lysc_node *ctx_scnode, int options) |
| 8303 | { |
| 8304 | if (options & LYXP_SCNODE_ALL) { |
| 8305 | if (options & LYXP_SCNODE) { |
| 8306 | /* general root that can access everything */ |
| 8307 | return LYXP_NODE_ROOT; |
| 8308 | } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) { |
| 8309 | /* root context node can access only config data (because we said so, it is unspecified) */ |
| 8310 | return LYXP_NODE_ROOT_CONFIG; |
| 8311 | } else { |
| 8312 | return LYXP_NODE_ROOT; |
| 8313 | } |
| 8314 | } |
| 8315 | |
| 8316 | if (!ctx_node || (ctx_node->schema->flags & LYS_CONFIG_W)) { |
| 8317 | /* root context node can access only config data (because we said so, it is unspecified) */ |
| 8318 | return LYXP_NODE_ROOT_CONFIG; |
| 8319 | } |
| 8320 | |
| 8321 | return LYXP_NODE_ROOT; |
| 8322 | } |
| 8323 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8324 | LY_ERR |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 8325 | 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] | 8326 | 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] | 8327 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8328 | uint16_t tok_idx = 0; |
| 8329 | const struct lyd_node *real_ctx_node; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8330 | LY_ERR rc; |
| 8331 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8332 | LY_CHECK_ARG_RET(NULL, exp, local_mod, ctx_node, set, LY_EINVAL); |
| 8333 | |
| 8334 | if ((ctx_node_type == LYXP_NODE_ROOT) || (ctx_node_type == LYXP_NODE_ROOT_CONFIG)) { |
| 8335 | /* we always need some context node because it is used for resolving unqualified names */ |
| 8336 | real_ctx_node = NULL; |
| 8337 | } else { |
| 8338 | real_ctx_node = ctx_node; |
| 8339 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8340 | |
| 8341 | /* prepare set for evaluation */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8342 | tok_idx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8343 | memset(set, 0, sizeof *set); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8344 | set->type = LYXP_SET_NODE_SET; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8345 | 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] | 8346 | set->ctx = local_mod->ctx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8347 | set->ctx_node = ctx_node; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8348 | set->root_type = lyxp_get_root_type(real_ctx_node, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8349 | set->local_mod = local_mod; |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 8350 | set->tree = tree; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8351 | set->format = format; |
| 8352 | |
| 8353 | /* evaluate */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8354 | rc = eval_expr_select(exp, &tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8355 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8356 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8357 | } |
| 8358 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8359 | return rc; |
| 8360 | } |
| 8361 | |
| 8362 | #if 0 |
| 8363 | |
| 8364 | /* full xml printing of set elements, not used currently */ |
| 8365 | |
| 8366 | void |
| 8367 | lyxp_set_print_xml(FILE *f, struct lyxp_set *set) |
| 8368 | { |
| 8369 | uint32_t i; |
| 8370 | char *str_num; |
| 8371 | struct lyout out; |
| 8372 | |
| 8373 | memset(&out, 0, sizeof out); |
| 8374 | |
| 8375 | out.type = LYOUT_STREAM; |
| 8376 | out.method.f = f; |
| 8377 | |
| 8378 | switch (set->type) { |
| 8379 | case LYXP_SET_EMPTY: |
| 8380 | ly_print(&out, "Empty XPath set\n\n"); |
| 8381 | break; |
| 8382 | case LYXP_SET_BOOLEAN: |
| 8383 | ly_print(&out, "Boolean XPath set:\n"); |
| 8384 | ly_print(&out, "%s\n\n", set->value.bool ? "true" : "false"); |
| 8385 | break; |
| 8386 | case LYXP_SET_STRING: |
| 8387 | ly_print(&out, "String XPath set:\n"); |
| 8388 | ly_print(&out, "\"%s\"\n\n", set->value.str); |
| 8389 | break; |
| 8390 | case LYXP_SET_NUMBER: |
| 8391 | ly_print(&out, "Number XPath set:\n"); |
| 8392 | |
| 8393 | if (isnan(set->value.num)) { |
| 8394 | str_num = strdup("NaN"); |
| 8395 | } else if ((set->value.num == 0) || (set->value.num == -0.0f)) { |
| 8396 | str_num = strdup("0"); |
| 8397 | } else if (isinf(set->value.num) && !signbit(set->value.num)) { |
| 8398 | str_num = strdup("Infinity"); |
| 8399 | } else if (isinf(set->value.num) && signbit(set->value.num)) { |
| 8400 | str_num = strdup("-Infinity"); |
| 8401 | } else if ((long long)set->value.num == set->value.num) { |
| 8402 | if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) { |
| 8403 | str_num = NULL; |
| 8404 | } |
| 8405 | } else { |
| 8406 | if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) { |
| 8407 | str_num = NULL; |
| 8408 | } |
| 8409 | } |
| 8410 | if (!str_num) { |
| 8411 | LOGMEM; |
| 8412 | return; |
| 8413 | } |
| 8414 | ly_print(&out, "%s\n\n", str_num); |
| 8415 | free(str_num); |
| 8416 | break; |
| 8417 | case LYXP_SET_NODE_SET: |
| 8418 | ly_print(&out, "Node XPath set:\n"); |
| 8419 | |
| 8420 | for (i = 0; i < set->used; ++i) { |
| 8421 | ly_print(&out, "%d. ", i + 1); |
| 8422 | switch (set->node_type[i]) { |
| 8423 | case LYXP_NODE_ROOT_ALL: |
| 8424 | ly_print(&out, "ROOT all\n\n"); |
| 8425 | break; |
| 8426 | case LYXP_NODE_ROOT_CONFIG: |
| 8427 | ly_print(&out, "ROOT config\n\n"); |
| 8428 | break; |
| 8429 | case LYXP_NODE_ROOT_STATE: |
| 8430 | ly_print(&out, "ROOT state\n\n"); |
| 8431 | break; |
| 8432 | case LYXP_NODE_ROOT_NOTIF: |
| 8433 | ly_print(&out, "ROOT notification \"%s\"\n\n", set->value.nodes[i]->schema->name); |
| 8434 | break; |
| 8435 | case LYXP_NODE_ROOT_RPC: |
| 8436 | ly_print(&out, "ROOT rpc \"%s\"\n\n", set->value.nodes[i]->schema->name); |
| 8437 | break; |
| 8438 | case LYXP_NODE_ROOT_OUTPUT: |
| 8439 | ly_print(&out, "ROOT output \"%s\"\n\n", set->value.nodes[i]->schema->name); |
| 8440 | break; |
| 8441 | case LYXP_NODE_ELEM: |
| 8442 | ly_print(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name); |
| 8443 | xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT); |
| 8444 | ly_print(&out, "\n"); |
| 8445 | break; |
| 8446 | case LYXP_NODE_TEXT: |
| 8447 | ly_print(&out, "TEXT \"%s\"\n\n", ((struct lyd_node_leaf_list *)set->value.nodes[i])->value_str); |
| 8448 | break; |
| 8449 | case LYXP_NODE_ATTR: |
| 8450 | ly_print(&out, "ATTR \"%s\" = \"%s\"\n\n", set->value.attrs[i]->name, set->value.attrs[i]->value); |
| 8451 | break; |
| 8452 | } |
| 8453 | } |
| 8454 | break; |
| 8455 | } |
| 8456 | } |
| 8457 | |
| 8458 | #endif |
| 8459 | |
| 8460 | LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8461 | lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8462 | { |
| 8463 | long double num; |
| 8464 | char *str; |
| 8465 | LY_ERR rc; |
| 8466 | |
| 8467 | if (!set || (set->type == target)) { |
| 8468 | return LY_SUCCESS; |
| 8469 | } |
| 8470 | |
| 8471 | /* it's not possible to convert anything into a node set */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8472 | assert(target != LYXP_SET_NODE_SET); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8473 | |
| 8474 | if (set->type == LYXP_SET_SCNODE_SET) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8475 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8476 | return LY_EINVAL; |
| 8477 | } |
| 8478 | |
| 8479 | /* to STRING */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8480 | 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] | 8481 | switch (set->type) { |
| 8482 | case LYXP_SET_NUMBER: |
| 8483 | if (isnan(set->val.num)) { |
| 8484 | set->val.str = strdup("NaN"); |
| 8485 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 8486 | } else if ((set->val.num == 0) || (set->val.num == -0.0f)) { |
| 8487 | set->val.str = strdup("0"); |
| 8488 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 8489 | } else if (isinf(set->val.num) && !signbit(set->val.num)) { |
| 8490 | set->val.str = strdup("Infinity"); |
| 8491 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 8492 | } else if (isinf(set->val.num) && signbit(set->val.num)) { |
| 8493 | set->val.str = strdup("-Infinity"); |
| 8494 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 8495 | } else if ((long long)set->val.num == set->val.num) { |
| 8496 | if (asprintf(&str, "%lld", (long long)set->val.num) == -1) { |
| 8497 | LOGMEM_RET(set->ctx); |
| 8498 | } |
| 8499 | set->val.str = str; |
| 8500 | } else { |
| 8501 | if (asprintf(&str, "%03.1Lf", set->val.num) == -1) { |
| 8502 | LOGMEM_RET(set->ctx); |
| 8503 | } |
| 8504 | set->val.str = str; |
| 8505 | } |
| 8506 | break; |
| 8507 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8508 | if (set->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8509 | set->val.str = strdup("true"); |
| 8510 | } else { |
| 8511 | set->val.str = strdup("false"); |
| 8512 | } |
| 8513 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM); |
| 8514 | break; |
| 8515 | case LYXP_SET_NODE_SET: |
| 8516 | assert(set->used); |
| 8517 | |
| 8518 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8519 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8520 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8521 | rc = cast_node_set_to_string(set, &str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8522 | LY_CHECK_RET(rc); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8523 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8524 | set->val.str = str; |
| 8525 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8526 | default: |
| 8527 | LOGINT_RET(set->ctx); |
| 8528 | } |
| 8529 | set->type = LYXP_SET_STRING; |
| 8530 | } |
| 8531 | |
| 8532 | /* to NUMBER */ |
| 8533 | if (target == LYXP_SET_NUMBER) { |
| 8534 | switch (set->type) { |
| 8535 | case LYXP_SET_STRING: |
| 8536 | num = cast_string_to_number(set->val.str); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8537 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8538 | set->val.num = num; |
| 8539 | break; |
| 8540 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8541 | if (set->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8542 | set->val.num = 1; |
| 8543 | } else { |
| 8544 | set->val.num = 0; |
| 8545 | } |
| 8546 | break; |
| 8547 | default: |
| 8548 | LOGINT_RET(set->ctx); |
| 8549 | } |
| 8550 | set->type = LYXP_SET_NUMBER; |
| 8551 | } |
| 8552 | |
| 8553 | /* to BOOLEAN */ |
| 8554 | if (target == LYXP_SET_BOOLEAN) { |
| 8555 | switch (set->type) { |
| 8556 | case LYXP_SET_NUMBER: |
| 8557 | 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] | 8558 | set->val.bln = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8559 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8560 | set->val.bln = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8561 | } |
| 8562 | break; |
| 8563 | case LYXP_SET_STRING: |
| 8564 | if (set->val.str[0]) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8565 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8566 | set->val.bln = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8567 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8568 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8569 | set->val.bln = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8570 | } |
| 8571 | break; |
| 8572 | case LYXP_SET_NODE_SET: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8573 | if (set->used) { |
| 8574 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8575 | set->val.bln = 1; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8576 | } else { |
| 8577 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8578 | set->val.bln = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8579 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8580 | break; |
| 8581 | default: |
| 8582 | LOGINT_RET(set->ctx); |
| 8583 | } |
| 8584 | set->type = LYXP_SET_BOOLEAN; |
| 8585 | } |
| 8586 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8587 | return LY_SUCCESS; |
| 8588 | } |
| 8589 | |
| 8590 | LY_ERR |
| 8591 | lyxp_atomize(struct lyxp_expr *exp, LYD_FORMAT format, const struct lys_module *local_mod, const struct lysc_node *ctx_scnode, |
| 8592 | enum lyxp_node_type ctx_scnode_type, struct lyxp_set *set, int options) |
| 8593 | { |
| 8594 | struct ly_ctx *ctx; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8595 | const struct lysc_node *real_ctx_scnode; |
| 8596 | uint16_t tok_idx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8597 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8598 | 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] | 8599 | |
| 8600 | ctx = local_mod->ctx; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8601 | if ((ctx_scnode_type == LYXP_NODE_ROOT) || (ctx_scnode_type == LYXP_NODE_ROOT_CONFIG)) { |
| 8602 | /* we always need some context node because it is used for resolving unqualified names */ |
| 8603 | real_ctx_scnode = NULL; |
| 8604 | } else { |
| 8605 | real_ctx_scnode = ctx_scnode; |
| 8606 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8607 | |
| 8608 | /* prepare set for evaluation */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8609 | tok_idx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8610 | memset(set, 0, sizeof *set); |
| 8611 | set->type = LYXP_SET_SCNODE_SET; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8612 | lyxp_set_scnode_insert_node(set, real_ctx_scnode, ctx_scnode_type); |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 8613 | set->val.scnodes[0].in_ctx = -2; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8614 | set->ctx = ctx; |
| 8615 | set->ctx_scnode = ctx_scnode; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8616 | set->root_type = lyxp_get_root_type(NULL, real_ctx_scnode, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8617 | set->local_mod = local_mod; |
| 8618 | set->format = format; |
| 8619 | |
| 8620 | /* evaluate */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8621 | return eval_expr_select(exp, &tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8622 | } |