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 | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6 | * Copyright (c) 2015 - 2019 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 | |
| 19 | #include "common.h" |
| 20 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 21 | #include <math.h> |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 22 | #include <ctype.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 23 | #include <stdint.h> |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 26 | #include <string.h> |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 27 | #include <errno.h> |
| 28 | #include <assert.h> |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 29 | |
| 30 | #include "xpath.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 31 | #include "dict.h" |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 32 | #include "xml.h" |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 33 | #include "printer_data.h" |
| 34 | #include "tree_schema_internal.h" |
| 35 | #include "plugins_types.h" |
| 36 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 37 | static LY_ERR reparse_or_expr(struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *exp_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 38 | static LY_ERR eval_expr_select(struct lyxp_expr *exp, uint16_t *exp_idx, enum lyxp_expr_type etype, struct lyxp_set *set, int options); |
| 39 | |
| 40 | /** |
| 41 | * @brief Print the type of an XPath \p set. |
| 42 | * |
| 43 | * @param[in] set Set to use. |
| 44 | * @return Set type string. |
| 45 | */ |
| 46 | static const char * |
| 47 | print_set_type(struct lyxp_set *set) |
| 48 | { |
| 49 | switch (set->type) { |
| 50 | case LYXP_SET_EMPTY: |
| 51 | return "empty"; |
| 52 | case LYXP_SET_NODE_SET: |
| 53 | return "node set"; |
| 54 | case LYXP_SET_SCNODE_SET: |
| 55 | return "schema node set"; |
| 56 | case LYXP_SET_BOOLEAN: |
| 57 | return "boolean"; |
| 58 | case LYXP_SET_NUMBER: |
| 59 | return "number"; |
| 60 | case LYXP_SET_STRING: |
| 61 | return "string"; |
| 62 | } |
| 63 | |
| 64 | return NULL; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * @brief Print an XPath token \p tok type. |
| 69 | * |
| 70 | * @param[in] tok Token to use. |
| 71 | * @return Token type string. |
| 72 | */ |
| 73 | static const char * |
| 74 | print_token(enum lyxp_token tok) |
| 75 | { |
| 76 | switch (tok) { |
| 77 | case LYXP_TOKEN_PAR1: |
| 78 | return "("; |
| 79 | case LYXP_TOKEN_PAR2: |
| 80 | return ")"; |
| 81 | case LYXP_TOKEN_BRACK1: |
| 82 | return "["; |
| 83 | case LYXP_TOKEN_BRACK2: |
| 84 | return "]"; |
| 85 | case LYXP_TOKEN_DOT: |
| 86 | return "."; |
| 87 | case LYXP_TOKEN_DDOT: |
| 88 | return ".."; |
| 89 | case LYXP_TOKEN_AT: |
| 90 | return "@"; |
| 91 | case LYXP_TOKEN_COMMA: |
| 92 | return ","; |
| 93 | case LYXP_TOKEN_NAMETEST: |
| 94 | return "NameTest"; |
| 95 | case LYXP_TOKEN_NODETYPE: |
| 96 | return "NodeType"; |
| 97 | case LYXP_TOKEN_FUNCNAME: |
| 98 | return "FunctionName"; |
| 99 | case LYXP_TOKEN_OPERATOR_LOG: |
| 100 | return "Operator(Logic)"; |
| 101 | case LYXP_TOKEN_OPERATOR_COMP: |
| 102 | return "Operator(Comparison)"; |
| 103 | case LYXP_TOKEN_OPERATOR_MATH: |
| 104 | return "Operator(Math)"; |
| 105 | case LYXP_TOKEN_OPERATOR_UNI: |
| 106 | return "Operator(Union)"; |
| 107 | case LYXP_TOKEN_OPERATOR_PATH: |
| 108 | return "Operator(Path)"; |
| 109 | case LYXP_TOKEN_LITERAL: |
| 110 | return "Literal"; |
| 111 | case LYXP_TOKEN_NUMBER: |
| 112 | return "Number"; |
| 113 | default: |
| 114 | LOGINT(NULL); |
| 115 | return ""; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * @brief Print the whole expression \p exp to debug output. |
| 121 | * |
| 122 | * @param[in] exp Expression to use. |
| 123 | */ |
| 124 | static void |
| 125 | print_expr_struct_debug(struct lyxp_expr *exp) |
| 126 | { |
| 127 | uint16_t i, j; |
| 128 | char tmp[128]; |
| 129 | |
| 130 | if (!exp || (ly_log_level < LY_LLDBG)) { |
| 131 | return; |
| 132 | } |
| 133 | |
| 134 | LOGDBG(LY_LDGXPATH, "expression \"%s\":", exp->expr); |
| 135 | for (i = 0; i < exp->used; ++i) { |
| 136 | sprintf(tmp, "\ttoken %s, in expression \"%.*s\"", print_token(exp->tokens[i]), exp->tok_len[i], |
| 137 | &exp->expr[exp->tok_pos[i]]); |
| 138 | if (exp->repeat[i]) { |
| 139 | sprintf(tmp + strlen(tmp), " (repeat %d", exp->repeat[i][0]); |
| 140 | for (j = 1; exp->repeat[i][j]; ++j) { |
| 141 | sprintf(tmp + strlen(tmp), ", %d", exp->repeat[i][j]); |
| 142 | } |
| 143 | strcat(tmp, ")"); |
| 144 | } |
| 145 | LOGDBG(LY_LDGXPATH, tmp); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | #ifndef NDEBUG |
| 150 | |
| 151 | /** |
| 152 | * @brief Print XPath set content to debug output. |
| 153 | * |
| 154 | * @param[in] set Set to print. |
| 155 | */ |
| 156 | static void |
| 157 | print_set_debug(struct lyxp_set *set) |
| 158 | { |
| 159 | uint32_t i; |
| 160 | char *str; |
| 161 | int dynamic; |
| 162 | struct lyxp_set_node *item; |
| 163 | struct lyxp_set_scnode *sitem; |
| 164 | |
| 165 | if (ly_log_level < LY_LLDBG) { |
| 166 | return; |
| 167 | } |
| 168 | |
| 169 | switch (set->type) { |
| 170 | case LYXP_SET_NODE_SET: |
| 171 | LOGDBG(LY_LDGXPATH, "set NODE SET:"); |
| 172 | for (i = 0; i < set->used; ++i) { |
| 173 | item = &set->val.nodes[i]; |
| 174 | |
| 175 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 176 | case LYXP_NODE_NONE: |
| 177 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): NONE", i + 1, item->pos); |
| 178 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 179 | case LYXP_NODE_ROOT: |
| 180 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT", i + 1, item->pos); |
| 181 | break; |
| 182 | case LYXP_NODE_ROOT_CONFIG: |
| 183 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT CONFIG", i + 1, item->pos); |
| 184 | break; |
| 185 | case LYXP_NODE_ELEM: |
| 186 | if ((item->node->schema->nodetype == LYS_LIST) |
| 187 | && (((struct lyd_node_inner *)item->node)->child->schema->nodetype == LYS_LEAF)) { |
| 188 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (1st child val: %s)", i + 1, item->pos, |
| 189 | item->node->schema->name, |
| 190 | (str = (char *)lyd_value2str((struct lyd_node_term *)lyd_node_children(item->node), &dynamic))); |
| 191 | if (dynamic) { |
| 192 | free(str); |
| 193 | } |
| 194 | } else if (((struct lyd_node_inner *)item->node)->schema->nodetype == LYS_LEAFLIST) { |
| 195 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (val: %s)", i + 1, item->pos, |
| 196 | item->node->schema->name, |
| 197 | (str = (char *)lyd_value2str((struct lyd_node_term *)item->node, &dynamic))); |
| 198 | if (dynamic) { |
| 199 | free(str); |
| 200 | } |
| 201 | } else { |
| 202 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s", i + 1, item->pos, item->node->schema->name); |
| 203 | } |
| 204 | break; |
| 205 | case LYXP_NODE_TEXT: |
| 206 | if (item->node->schema->nodetype & LYS_ANYDATA) { |
| 207 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT <%s>", i + 1, item->pos, |
| 208 | item->node->schema->nodetype == LYS_ANYXML ? "anyxml" : "anydata"); |
| 209 | } else { |
| 210 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT %s", i + 1, item->pos, |
| 211 | (str = (char *)lyd_value2str((struct lyd_node_term *)item->node, &dynamic))); |
| 212 | if (dynamic) { |
| 213 | free(str); |
| 214 | } |
| 215 | } |
| 216 | break; |
| 217 | case LYXP_NODE_ATTR: |
| 218 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ATTR %s = %s", i + 1, item->pos, set->val.attrs[i].attr->name, |
| 219 | set->val.attrs[i].attr->value); |
| 220 | break; |
| 221 | } |
| 222 | } |
| 223 | break; |
| 224 | |
| 225 | case LYXP_SET_SCNODE_SET: |
| 226 | LOGDBG(LY_LDGXPATH, "set SCNODE SET:"); |
| 227 | for (i = 0; i < set->used; ++i) { |
| 228 | sitem = &set->val.scnodes[i]; |
| 229 | |
| 230 | switch (sitem->type) { |
| 231 | case LYXP_NODE_ROOT: |
| 232 | LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT", i + 1, sitem->in_ctx); |
| 233 | break; |
| 234 | case LYXP_NODE_ROOT_CONFIG: |
| 235 | LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT CONFIG", i + 1, sitem->in_ctx); |
| 236 | break; |
| 237 | case LYXP_NODE_ELEM: |
| 238 | LOGDBG(LY_LDGXPATH, "\t%d (%u): ELEM %s", i + 1, sitem->in_ctx, sitem->scnode->name); |
| 239 | break; |
| 240 | default: |
| 241 | LOGINT(NULL); |
| 242 | break; |
| 243 | } |
| 244 | } |
| 245 | break; |
| 246 | |
| 247 | case LYXP_SET_EMPTY: |
| 248 | LOGDBG(LY_LDGXPATH, "set EMPTY"); |
| 249 | break; |
| 250 | |
| 251 | case LYXP_SET_BOOLEAN: |
| 252 | LOGDBG(LY_LDGXPATH, "set BOOLEAN"); |
| 253 | LOGDBG(LY_LDGXPATH, "\t%s", (set->val.bool ? "true" : "false")); |
| 254 | break; |
| 255 | |
| 256 | case LYXP_SET_STRING: |
| 257 | LOGDBG(LY_LDGXPATH, "set STRING"); |
| 258 | LOGDBG(LY_LDGXPATH, "\t%s", set->val.str); |
| 259 | break; |
| 260 | |
| 261 | case LYXP_SET_NUMBER: |
| 262 | LOGDBG(LY_LDGXPATH, "set NUMBER"); |
| 263 | |
| 264 | if (isnan(set->val.num)) { |
| 265 | str = strdup("NaN"); |
| 266 | } else if ((set->val.num == 0) || (set->val.num == -0.0f)) { |
| 267 | str = strdup("0"); |
| 268 | } else if (isinf(set->val.num) && !signbit(set->val.num)) { |
| 269 | str = strdup("Infinity"); |
| 270 | } else if (isinf(set->val.num) && signbit(set->val.num)) { |
| 271 | str = strdup("-Infinity"); |
| 272 | } else if ((long long)set->val.num == set->val.num) { |
| 273 | if (asprintf(&str, "%lld", (long long)set->val.num) == -1) { |
| 274 | str = NULL; |
| 275 | } |
| 276 | } else { |
| 277 | if (asprintf(&str, "%03.1Lf", set->val.num) == -1) { |
| 278 | str = NULL; |
| 279 | } |
| 280 | } |
| 281 | LY_CHECK_ERR_RET(!str, LOGMEM(NULL), ); |
| 282 | |
| 283 | LOGDBG(LY_LDGXPATH, "\t%s", str); |
| 284 | free(str); |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | #endif |
| 289 | |
| 290 | /** |
| 291 | * @brief Realloc the string \p str. |
| 292 | * |
| 293 | * @param[in] ctx libyang context for logging. |
| 294 | * @param[in] needed How much free space is required. |
| 295 | * @param[in,out] str Pointer to the string to use. |
| 296 | * @param[in,out] used Used bytes in \p str. |
| 297 | * @param[in,out] size Allocated bytes in \p str. |
| 298 | * @return LY_ERR |
| 299 | */ |
| 300 | static LY_ERR |
| 301 | cast_string_realloc(struct ly_ctx *ctx, uint16_t needed, char **str, uint16_t *used, uint16_t *size) |
| 302 | { |
| 303 | if (*size - *used < needed) { |
| 304 | do { |
| 305 | if ((UINT16_MAX - *size) < LYXP_STRING_CAST_SIZE_STEP) { |
| 306 | LOGERR(ctx, LY_EINVAL, "XPath string length limit (%u) reached.", UINT16_MAX); |
| 307 | return LY_EINVAL; |
| 308 | } |
| 309 | *size += LYXP_STRING_CAST_SIZE_STEP; |
| 310 | } while (*size - *used < needed); |
| 311 | *str = ly_realloc(*str, *size * sizeof(char)); |
| 312 | LY_CHECK_ERR_RET(!(*str), LOGMEM(ctx), LY_EMEM); |
| 313 | } |
| 314 | |
| 315 | return LY_SUCCESS; |
| 316 | } |
| 317 | |
| 318 | /** |
| 319 | * @brief Cast nodes recursively to one string @p str. |
| 320 | * |
| 321 | * @param[in] node Node to cast. |
| 322 | * @param[in] fake_cont Whether to put the data into a "fake" container. |
| 323 | * @param[in] root_type Type of the XPath root. |
| 324 | * @param[in] indent Current indent. |
| 325 | * @param[in,out] str Resulting string. |
| 326 | * @param[in,out] used Used bytes in @p str. |
| 327 | * @param[in,out] size Allocated bytes in @p str. |
| 328 | * @return LY_ERR |
| 329 | */ |
| 330 | static LY_ERR |
| 331 | cast_string_recursive(const struct lyd_node *node, int fake_cont, enum lyxp_node_type root_type, uint16_t indent, char **str, |
| 332 | uint16_t *used, uint16_t *size) |
| 333 | { |
| 334 | char *buf, *line, *ptr; |
| 335 | const char *value_str; |
| 336 | int dynamic; |
| 337 | const struct lyd_node *child; |
| 338 | struct lyd_node_any *any; |
| 339 | LY_ERR rc; |
| 340 | |
| 341 | if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) { |
| 342 | return LY_SUCCESS; |
| 343 | } |
| 344 | |
| 345 | if (fake_cont) { |
| 346 | rc = cast_string_realloc(LYD_NODE_CTX(node), 1, str, used, size); |
| 347 | LY_CHECK_RET(rc); |
| 348 | strcpy(*str + (*used - 1), "\n"); |
| 349 | ++(*used); |
| 350 | |
| 351 | ++indent; |
| 352 | } |
| 353 | |
| 354 | switch (node->schema->nodetype) { |
| 355 | case LYS_CONTAINER: |
| 356 | case LYS_LIST: |
| 357 | case LYS_RPC: |
| 358 | case LYS_NOTIF: |
| 359 | rc = cast_string_realloc(LYD_NODE_CTX(node), 1, str, used, size); |
| 360 | LY_CHECK_RET(rc); |
| 361 | strcpy(*str + (*used - 1), "\n"); |
| 362 | ++(*used); |
| 363 | |
| 364 | for (child = lyd_node_children(node); child; child = child->next) { |
| 365 | rc = cast_string_recursive(child, 0, root_type, indent + 1, str, used, size); |
| 366 | LY_CHECK_RET(rc); |
| 367 | } |
| 368 | |
| 369 | break; |
| 370 | |
| 371 | case LYS_LEAF: |
| 372 | case LYS_LEAFLIST: |
| 373 | value_str = lyd_value2str(((struct lyd_node_term *)node), &dynamic); |
| 374 | |
| 375 | /* print indent */ |
| 376 | rc = cast_string_realloc(LYD_NODE_CTX(node), indent * 2 + strlen(value_str) + 1, str, used, size); |
| 377 | if (rc != LY_SUCCESS) { |
| 378 | if (dynamic) { |
| 379 | free((char *)value_str); |
| 380 | } |
| 381 | return rc; |
| 382 | } |
| 383 | memset(*str + (*used - 1), ' ', indent * 2); |
| 384 | *used += indent * 2; |
| 385 | |
| 386 | /* print value */ |
| 387 | if (*used == 1) { |
| 388 | sprintf(*str + (*used - 1), "%s", value_str); |
| 389 | *used += strlen(value_str); |
| 390 | } else { |
| 391 | sprintf(*str + (*used - 1), "%s\n", value_str); |
| 392 | *used += strlen(value_str) + 1; |
| 393 | } |
| 394 | if (dynamic) { |
| 395 | free((char *)value_str); |
| 396 | } |
| 397 | |
| 398 | break; |
| 399 | |
| 400 | case LYS_ANYXML: |
| 401 | case LYS_ANYDATA: |
| 402 | any = (struct lyd_node_any *)node; |
| 403 | if (!(void *)any->value.tree) { |
| 404 | /* no content */ |
| 405 | buf = strdup(""); |
| 406 | LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_NODE_CTX(node)), LY_EMEM); |
| 407 | } else { |
| 408 | switch (any->value_type) { |
| 409 | case LYD_ANYDATA_STRING: |
| 410 | case LYD_ANYDATA_XML: |
| 411 | case LYD_ANYDATA_JSON: |
| 412 | buf = strdup(any->value.json); |
| 413 | LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_NODE_CTX(node)), LY_EMEM); |
| 414 | break; |
| 415 | case LYD_ANYDATA_DATATREE: |
| 416 | rc = lyd_print_mem(&buf, any->value.tree, LYD_XML, LYDP_WITHSIBLINGS); |
| 417 | LY_CHECK_RET(rc); |
| 418 | break; |
| 419 | /* TODO case LYD_ANYDATA_LYB: |
| 420 | LOGERR(LYD_NODE_CTX(node), LY_EINVAL, "Cannot convert LYB anydata into string."); |
| 421 | return -1;*/ |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | line = strtok_r(buf, "\n", &ptr); |
| 426 | do { |
| 427 | rc = cast_string_realloc(LYD_NODE_CTX(node), indent * 2 + strlen(line) + 1, str, used, size); |
| 428 | if (rc != LY_SUCCESS) { |
| 429 | free(buf); |
| 430 | return rc; |
| 431 | } |
| 432 | memset(*str + (*used - 1), ' ', indent * 2); |
| 433 | *used += indent * 2; |
| 434 | |
| 435 | strcpy(*str + (*used - 1), line); |
| 436 | *used += strlen(line); |
| 437 | |
| 438 | strcpy(*str + (*used - 1), "\n"); |
| 439 | *used += 1; |
| 440 | } while ((line = strtok_r(NULL, "\n", &ptr))); |
| 441 | |
| 442 | free(buf); |
| 443 | break; |
| 444 | |
| 445 | default: |
| 446 | LOGINT_RET(LYD_NODE_CTX(node)); |
| 447 | } |
| 448 | |
| 449 | if (fake_cont) { |
| 450 | rc = cast_string_realloc(LYD_NODE_CTX(node), 1, str, used, size); |
| 451 | LY_CHECK_RET(rc); |
| 452 | strcpy(*str + (*used - 1), "\n"); |
| 453 | ++(*used); |
| 454 | |
| 455 | --indent; |
| 456 | } |
| 457 | |
| 458 | return LY_SUCCESS; |
| 459 | } |
| 460 | |
| 461 | /** |
| 462 | * @brief Cast an element into a string. |
| 463 | * |
| 464 | * @param[in] node Node to cast. |
| 465 | * @param[in] fake_cont Whether to put the data into a "fake" container. |
| 466 | * @param[in] root_type Type of the XPath root. |
| 467 | * @param[out] str Element cast to dynamically-allocated string. |
| 468 | * @return LY_ERR |
| 469 | */ |
| 470 | static LY_ERR |
| 471 | cast_string_elem(struct lyd_node *node, int fake_cont, enum lyxp_node_type root_type, char **str) |
| 472 | { |
| 473 | uint16_t used, size; |
| 474 | LY_ERR rc; |
| 475 | |
| 476 | *str = malloc(LYXP_STRING_CAST_SIZE_START * sizeof(char)); |
| 477 | LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_NODE_CTX(node)), LY_EMEM); |
| 478 | (*str)[0] = '\0'; |
| 479 | used = 1; |
| 480 | size = LYXP_STRING_CAST_SIZE_START; |
| 481 | |
| 482 | rc = cast_string_recursive(node, fake_cont, root_type, 0, str, &used, &size); |
| 483 | if (rc != LY_SUCCESS) { |
| 484 | free(*str); |
| 485 | return rc; |
| 486 | } |
| 487 | |
| 488 | if (size > used) { |
| 489 | *str = ly_realloc(*str, used * sizeof(char)); |
| 490 | LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_NODE_CTX(node)), LY_EMEM); |
| 491 | } |
| 492 | return LY_SUCCESS; |
| 493 | } |
| 494 | |
| 495 | /** |
| 496 | * @brief Cast a LYXP_SET_NODE_SET set into a string. |
| 497 | * Context position aware. |
| 498 | * |
| 499 | * @param[in] set Set to cast. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 500 | * @param[out] str Cast dynamically-allocated string. |
| 501 | * @return LY_ERR |
| 502 | */ |
| 503 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 504 | cast_node_set_to_string(struct lyxp_set *set, char **str) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 505 | { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 506 | int dynamic; |
| 507 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 508 | switch (set->val.nodes[0].type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 509 | case LYXP_NODE_NONE: |
| 510 | /* invalid */ |
| 511 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 512 | case LYXP_NODE_ROOT: |
| 513 | case LYXP_NODE_ROOT_CONFIG: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 514 | 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] | 515 | case LYXP_NODE_ELEM: |
| 516 | case LYXP_NODE_TEXT: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 517 | return cast_string_elem(set->val.nodes[0].node, 0, set->root_type, str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 518 | case LYXP_NODE_ATTR: |
| 519 | *str = (char *)lyd_attr2str(set->val.attrs[0].attr, &dynamic); |
| 520 | if (!dynamic) { |
| 521 | *str = strdup(*str); |
| 522 | if (!*str) { |
| 523 | LOGMEM_RET(set->ctx); |
| 524 | } |
| 525 | } |
| 526 | return LY_SUCCESS; |
| 527 | } |
| 528 | |
| 529 | LOGINT_RET(set->ctx); |
| 530 | } |
| 531 | |
| 532 | /** |
| 533 | * @brief Cast a string into an XPath number. |
| 534 | * |
| 535 | * @param[in] str String to use. |
| 536 | * @return Cast number. |
| 537 | */ |
| 538 | static long double |
| 539 | cast_string_to_number(const char *str) |
| 540 | { |
| 541 | long double num; |
| 542 | char *ptr; |
| 543 | |
| 544 | errno = 0; |
| 545 | num = strtold(str, &ptr); |
| 546 | if (errno || *ptr) { |
| 547 | num = NAN; |
| 548 | } |
| 549 | return num; |
| 550 | } |
| 551 | |
| 552 | /** |
| 553 | * @brief Callback for checking value equality. |
| 554 | * |
| 555 | * @param[in] val1_p First value. |
| 556 | * @param[in] val2_p Second value. |
| 557 | * @param[in] mod Whether hash table is being modified. |
| 558 | * @param[in] cb_data Callback data. |
| 559 | * @return 0 if not equal, non-zero if equal. |
| 560 | */ |
| 561 | static int |
| 562 | set_values_equal_cb(void *val1_p, void *val2_p, int UNUSED(mod), void *UNUSED(cb_data)) |
| 563 | { |
| 564 | struct lyxp_set_hash_node *val1, *val2; |
| 565 | |
| 566 | val1 = (struct lyxp_set_hash_node *)val1_p; |
| 567 | val2 = (struct lyxp_set_hash_node *)val2_p; |
| 568 | |
| 569 | if ((val1->node == val2->node) && (val1->type == val2->type)) { |
| 570 | return 1; |
| 571 | } |
| 572 | |
| 573 | return 0; |
| 574 | } |
| 575 | |
| 576 | /** |
| 577 | * @brief Insert node and its hash into set. |
| 578 | * |
| 579 | * @param[in] set et to insert to. |
| 580 | * @param[in] node Node with hash. |
| 581 | * @param[in] type Node type. |
| 582 | */ |
| 583 | static void |
| 584 | set_insert_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type) |
| 585 | { |
| 586 | int r; |
| 587 | uint32_t i, hash; |
| 588 | struct lyxp_set_hash_node hnode; |
| 589 | |
| 590 | if (!set->ht && (set->used >= LYD_HT_MIN_ITEMS)) { |
| 591 | /* create hash table and add all the nodes */ |
| 592 | set->ht = lyht_new(1, sizeof(struct lyxp_set_hash_node), set_values_equal_cb, NULL, 1); |
| 593 | for (i = 0; i < set->used; ++i) { |
| 594 | hnode.node = set->val.nodes[i].node; |
| 595 | hnode.type = set->val.nodes[i].type; |
| 596 | |
| 597 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 598 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 599 | hash = dict_hash_multi(hash, NULL, 0); |
| 600 | |
| 601 | r = lyht_insert(set->ht, &hnode, hash, NULL); |
| 602 | assert(!r); |
| 603 | (void)r; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 604 | |
Michal Vasko | 9d6befd | 2019-12-11 14:56:56 +0100 | [diff] [blame^] | 605 | if (hnode.node == node) { |
| 606 | /* it was just added, do not add it twice */ |
| 607 | node = NULL; |
| 608 | } |
| 609 | } |
| 610 | } |
| 611 | |
| 612 | if (set->ht && node) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 613 | /* add the new node into hash table */ |
| 614 | hnode.node = node; |
| 615 | hnode.type = type; |
| 616 | |
| 617 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 618 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 619 | hash = dict_hash_multi(hash, NULL, 0); |
| 620 | |
| 621 | r = lyht_insert(set->ht, &hnode, hash, NULL); |
| 622 | assert(!r); |
| 623 | (void)r; |
| 624 | } |
| 625 | } |
| 626 | |
| 627 | /** |
| 628 | * @brief Remove node and its hash from set. |
| 629 | * |
| 630 | * @param[in] set Set to remove from. |
| 631 | * @param[in] node Node to remove. |
| 632 | * @param[in] type Node type. |
| 633 | */ |
| 634 | static void |
| 635 | set_remove_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type) |
| 636 | { |
| 637 | int r; |
| 638 | struct lyxp_set_hash_node hnode; |
| 639 | uint32_t hash; |
| 640 | |
| 641 | if (set->ht) { |
| 642 | hnode.node = node; |
| 643 | hnode.type = type; |
| 644 | |
| 645 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 646 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 647 | hash = dict_hash_multi(hash, NULL, 0); |
| 648 | |
| 649 | r = lyht_remove(set->ht, &hnode, hash); |
| 650 | assert(!r); |
| 651 | (void)r; |
| 652 | |
| 653 | if (!set->ht->used) { |
| 654 | lyht_free(set->ht); |
| 655 | set->ht = NULL; |
| 656 | } |
| 657 | } |
| 658 | } |
| 659 | |
| 660 | /** |
| 661 | * @brief Check whether node is in set based on its hash. |
| 662 | * |
| 663 | * @param[in] set Set to search in. |
| 664 | * @param[in] node Node to search for. |
| 665 | * @param[in] type Node type. |
| 666 | * @param[in] skip_idx Index in @p set to skip. |
| 667 | * @return LY_ERR |
| 668 | */ |
| 669 | static LY_ERR |
| 670 | set_dup_node_hash_check(const struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type, int skip_idx) |
| 671 | { |
| 672 | struct lyxp_set_hash_node hnode, *match_p; |
| 673 | uint32_t hash; |
| 674 | |
| 675 | hnode.node = node; |
| 676 | hnode.type = type; |
| 677 | |
| 678 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 679 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 680 | hash = dict_hash_multi(hash, NULL, 0); |
| 681 | |
| 682 | if (!lyht_find(set->ht, &hnode, hash, (void **)&match_p)) { |
| 683 | if ((skip_idx > -1) && (set->val.nodes[skip_idx].node == match_p->node) && (set->val.nodes[skip_idx].type == match_p->type)) { |
| 684 | /* we found it on the index that should be skipped, find another */ |
| 685 | hnode = *match_p; |
| 686 | if (lyht_find_next(set->ht, &hnode, hash, (void **)&match_p)) { |
| 687 | /* none other found */ |
| 688 | return LY_SUCCESS; |
| 689 | } |
| 690 | } |
| 691 | |
| 692 | return LY_EEXIST; |
| 693 | } |
| 694 | |
| 695 | /* not found */ |
| 696 | return LY_SUCCESS; |
| 697 | } |
| 698 | |
| 699 | /** |
| 700 | * @brief Free dynamic content of a set. |
| 701 | * |
| 702 | * @param[in] set Set to modify. |
| 703 | */ |
| 704 | static void |
| 705 | set_free_content(struct lyxp_set *set) |
| 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); |
| 714 | set->ht = NULL; |
| 715 | } else if (set->type == LYXP_SET_SCNODE_SET) { |
| 716 | free(set->val.scnodes); |
| 717 | } else if (set->type == LYXP_SET_STRING) { |
| 718 | free(set->val.str); |
| 719 | } |
| 720 | set->type = LYXP_SET_EMPTY; |
| 721 | } |
| 722 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 723 | /** |
| 724 | * @brief Free dynamically-allocated set. |
| 725 | * |
| 726 | * @param[in] set Set to free. |
| 727 | */ |
| 728 | static void |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 729 | lyxp_set_free(struct lyxp_set *set) |
| 730 | { |
| 731 | if (!set) { |
| 732 | return; |
| 733 | } |
| 734 | |
| 735 | set_free_content(set); |
| 736 | free(set); |
| 737 | } |
| 738 | |
| 739 | /** |
| 740 | * @brief Initialize set context. |
| 741 | * |
| 742 | * @param[in] new Set to initialize. |
| 743 | * @param[in] set Arbitrary initialized set. |
| 744 | */ |
| 745 | static void |
| 746 | set_init(struct lyxp_set *new, struct lyxp_set *set) |
| 747 | { |
| 748 | memset(new, 0, sizeof *new); |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 749 | if (set) { |
| 750 | new->ctx = set->ctx; |
| 751 | new->ctx_node = set->ctx_node; |
Michal Vasko | 588112f | 2019-12-10 14:51:53 +0100 | [diff] [blame] | 752 | new->root_type = set->root_type; |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 753 | new->local_mod = set->local_mod; |
| 754 | new->trees = set->trees; |
| 755 | new->format = set->format; |
| 756 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 757 | } |
| 758 | |
| 759 | /** |
| 760 | * @brief Create a deep copy of a set. |
| 761 | * |
| 762 | * @param[in] set Set to copy. |
| 763 | * @return Copy of @p set. |
| 764 | */ |
| 765 | static struct lyxp_set * |
| 766 | set_copy(struct lyxp_set *set) |
| 767 | { |
| 768 | struct lyxp_set *ret; |
| 769 | uint16_t i; |
| 770 | |
| 771 | if (!set) { |
| 772 | return NULL; |
| 773 | } |
| 774 | |
| 775 | ret = malloc(sizeof *ret); |
| 776 | LY_CHECK_ERR_RET(!ret, LOGMEM(set->ctx), NULL); |
| 777 | set_init(ret, set); |
| 778 | |
| 779 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 780 | ret->type = set->type; |
| 781 | |
| 782 | for (i = 0; i < set->used; ++i) { |
| 783 | if (set->val.scnodes[i].in_ctx == 1) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 784 | if (lyxp_set_scnode_insert_node(ret, set->val.scnodes[i].scnode, set->val.scnodes[i].type)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 785 | lyxp_set_free(ret); |
| 786 | return NULL; |
| 787 | } |
| 788 | } |
| 789 | } |
| 790 | } else if (set->type == LYXP_SET_NODE_SET) { |
| 791 | ret->type = set->type; |
| 792 | ret->val.nodes = malloc(set->used * sizeof *ret->val.nodes); |
| 793 | LY_CHECK_ERR_RET(!ret->val.nodes, LOGMEM(set->ctx); free(ret), NULL); |
| 794 | memcpy(ret->val.nodes, set->val.nodes, set->used * sizeof *ret->val.nodes); |
| 795 | |
| 796 | ret->used = ret->size = set->used; |
| 797 | ret->ctx_pos = set->ctx_pos; |
| 798 | ret->ctx_size = set->ctx_size; |
| 799 | ret->ht = lyht_dup(set->ht); |
| 800 | } else { |
| 801 | memcpy(ret, set, sizeof *ret); |
| 802 | if (set->type == LYXP_SET_STRING) { |
| 803 | ret->val.str = strdup(set->val.str); |
| 804 | LY_CHECK_ERR_RET(!ret->val.str, LOGMEM(set->ctx); free(ret), NULL); |
| 805 | } |
| 806 | } |
| 807 | |
| 808 | return ret; |
| 809 | } |
| 810 | |
| 811 | /** |
| 812 | * @brief Fill XPath set with a string. Any current data are disposed of. |
| 813 | * |
| 814 | * @param[in] set Set to fill. |
| 815 | * @param[in] string String to fill into \p set. |
| 816 | * @param[in] str_len Length of \p string. 0 is a valid value! |
| 817 | */ |
| 818 | static void |
| 819 | set_fill_string(struct lyxp_set *set, const char *string, uint16_t str_len) |
| 820 | { |
| 821 | set_free_content(set); |
| 822 | |
| 823 | set->type = LYXP_SET_STRING; |
| 824 | if ((str_len == 0) && (string[0] != '\0')) { |
| 825 | string = ""; |
| 826 | } |
| 827 | set->val.str = strndup(string, str_len); |
| 828 | } |
| 829 | |
| 830 | /** |
| 831 | * @brief Fill XPath set with a number. Any current data are disposed of. |
| 832 | * |
| 833 | * @param[in] set Set to fill. |
| 834 | * @param[in] number Number to fill into \p set. |
| 835 | */ |
| 836 | static void |
| 837 | set_fill_number(struct lyxp_set *set, long double number) |
| 838 | { |
| 839 | set_free_content(set); |
| 840 | |
| 841 | set->type = LYXP_SET_NUMBER; |
| 842 | set->val.num = number; |
| 843 | } |
| 844 | |
| 845 | /** |
| 846 | * @brief Fill XPath set with a boolean. Any current data are disposed of. |
| 847 | * |
| 848 | * @param[in] set Set to fill. |
| 849 | * @param[in] boolean Boolean to fill into \p set. |
| 850 | */ |
| 851 | static void |
| 852 | set_fill_boolean(struct lyxp_set *set, int boolean) |
| 853 | { |
| 854 | set_free_content(set); |
| 855 | |
| 856 | set->type = LYXP_SET_BOOLEAN; |
| 857 | set->val.bool = boolean; |
| 858 | } |
| 859 | |
| 860 | /** |
| 861 | * @brief Fill XPath set with the value from another set (deep assign). |
| 862 | * Any current data are disposed of. |
| 863 | * |
| 864 | * @param[in] trg Set to fill. |
| 865 | * @param[in] src Source set to copy into \p trg. |
| 866 | */ |
| 867 | static void |
| 868 | set_fill_set(struct lyxp_set *trg, struct lyxp_set *src) |
| 869 | { |
| 870 | if (!trg || !src) { |
| 871 | return; |
| 872 | } |
| 873 | |
| 874 | if (trg->type == LYXP_SET_NODE_SET) { |
| 875 | free(trg->val.nodes); |
| 876 | } else if (trg->type == LYXP_SET_STRING) { |
| 877 | free(trg->val.str); |
| 878 | } |
| 879 | set_init(trg, src); |
| 880 | |
| 881 | if (src->type == LYXP_SET_SCNODE_SET) { |
| 882 | trg->type = LYXP_SET_SCNODE_SET; |
| 883 | trg->used = src->used; |
| 884 | trg->size = src->used; |
| 885 | |
| 886 | trg->val.scnodes = ly_realloc(trg->val.scnodes, trg->size * sizeof *trg->val.scnodes); |
| 887 | LY_CHECK_ERR_RET(!trg->val.scnodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), ); |
| 888 | memcpy(trg->val.scnodes, src->val.scnodes, src->used * sizeof *src->val.scnodes); |
| 889 | } else if (src->type == LYXP_SET_BOOLEAN) { |
| 890 | set_fill_boolean(trg, src->val.bool); |
| 891 | } else if (src->type == LYXP_SET_NUMBER) { |
| 892 | set_fill_number(trg, src->val.num); |
| 893 | } else if (src->type == LYXP_SET_STRING) { |
| 894 | set_fill_string(trg, src->val.str, strlen(src->val.str)); |
| 895 | } else { |
| 896 | if (trg->type == LYXP_SET_NODE_SET) { |
| 897 | free(trg->val.nodes); |
| 898 | } else if (trg->type == LYXP_SET_STRING) { |
| 899 | free(trg->val.str); |
| 900 | } |
| 901 | |
| 902 | if (src->type == LYXP_SET_EMPTY) { |
| 903 | trg->type = LYXP_SET_EMPTY; |
| 904 | } else { |
| 905 | assert(src->type == LYXP_SET_NODE_SET); |
| 906 | |
| 907 | trg->type = LYXP_SET_NODE_SET; |
| 908 | trg->used = src->used; |
| 909 | trg->size = src->used; |
| 910 | trg->ctx_pos = src->ctx_pos; |
| 911 | trg->ctx_size = src->ctx_size; |
| 912 | |
| 913 | trg->val.nodes = malloc(trg->used * sizeof *trg->val.nodes); |
| 914 | LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), ); |
| 915 | memcpy(trg->val.nodes, src->val.nodes, src->used * sizeof *src->val.nodes); |
| 916 | trg->ht = lyht_dup(src->ht); |
| 917 | } |
| 918 | } |
| 919 | } |
| 920 | |
| 921 | /** |
| 922 | * @brief Clear context of all schema nodes. |
| 923 | * |
| 924 | * @param[in] set Set to clear. |
| 925 | */ |
| 926 | static void |
| 927 | set_scnode_clear_ctx(struct lyxp_set *set) |
| 928 | { |
| 929 | uint32_t i; |
| 930 | |
| 931 | for (i = 0; i < set->used; ++i) { |
| 932 | if (set->val.scnodes[i].in_ctx == 1) { |
| 933 | set->val.scnodes[i].in_ctx = 0; |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 934 | } else if (set->val.scnodes[i].in_ctx == -2) { |
| 935 | set->val.scnodes[i].in_ctx = -1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 936 | } |
| 937 | } |
| 938 | } |
| 939 | |
| 940 | /** |
| 941 | * @brief Remove a node from a set. Removing last node changes |
| 942 | * set into LYXP_SET_EMPTY. Context position aware. |
| 943 | * |
| 944 | * @param[in] set Set to use. |
| 945 | * @param[in] idx Index from @p set of the node to be removed. |
| 946 | */ |
| 947 | static void |
| 948 | set_remove_node(struct lyxp_set *set, uint32_t idx) |
| 949 | { |
| 950 | assert(set && (set->type == LYXP_SET_NODE_SET)); |
| 951 | assert(idx < set->used); |
| 952 | |
| 953 | set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
| 954 | |
| 955 | --set->used; |
| 956 | if (set->used) { |
| 957 | memmove(&set->val.nodes[idx], &set->val.nodes[idx + 1], |
| 958 | (set->used - idx) * sizeof *set->val.nodes); |
| 959 | } else { |
| 960 | set_free_content(set); |
| 961 | set->type = LYXP_SET_EMPTY; |
| 962 | } |
| 963 | } |
| 964 | |
| 965 | /** |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 966 | * @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] | 967 | * |
| 968 | * @param[in] set Set to use. |
| 969 | * @param[in] idx Index from @p set of the node to be removed. |
| 970 | */ |
| 971 | static void |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 972 | set_remove_node_none(struct lyxp_set *set, uint32_t idx) |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 973 | { |
| 974 | assert(set && (set->type == LYXP_SET_NODE_SET)); |
| 975 | assert(idx < set->used); |
| 976 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 977 | if (set->val.nodes[idx].type == LYXP_NODE_ELEM) { |
| 978 | set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
| 979 | } |
| 980 | set->val.nodes[idx].type = LYXP_NODE_NONE; |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 981 | } |
| 982 | |
| 983 | /** |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 984 | * @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] | 985 | * set into LYXP_SET_EMPTY. Context position aware. |
| 986 | * |
| 987 | * @param[in] set Set to consolidate. |
| 988 | */ |
| 989 | static void |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 990 | set_remove_nodes_none(struct lyxp_set *set) |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 991 | { |
| 992 | uint16_t i, orig_used, end; |
| 993 | int32_t start; |
| 994 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 995 | assert(set && (set->type != LYXP_SET_EMPTY)); |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 996 | |
| 997 | orig_used = set->used; |
| 998 | set->used = 0; |
| 999 | for (i = 0; i < orig_used;) { |
| 1000 | start = -1; |
| 1001 | do { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1002 | if ((set->val.nodes[i].type != LYXP_NODE_NONE) && (start == -1)) { |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1003 | start = i; |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1004 | } else if ((start > -1) && (set->val.nodes[i].type == LYXP_NODE_NONE)) { |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1005 | end = i; |
| 1006 | ++i; |
| 1007 | break; |
| 1008 | } |
| 1009 | |
| 1010 | ++i; |
| 1011 | if (i == orig_used) { |
| 1012 | end = i; |
| 1013 | } |
| 1014 | } while (i < orig_used); |
| 1015 | |
| 1016 | if (start > -1) { |
| 1017 | /* move the whole chunk of valid nodes together */ |
| 1018 | if (set->used != (unsigned)start) { |
| 1019 | memmove(&set->val.nodes[set->used], &set->val.nodes[start], (end - start) * sizeof *set->val.nodes); |
| 1020 | } |
| 1021 | set->used += end - start; |
| 1022 | } |
| 1023 | } |
| 1024 | |
| 1025 | if (!set->used) { |
| 1026 | set_free_content(set); |
| 1027 | /* this changes it to LYXP_SET_EMPTY */ |
| 1028 | memset(set, 0, sizeof *set); |
| 1029 | } |
| 1030 | } |
| 1031 | |
| 1032 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1033 | * @brief Check for duplicates in a node set. |
| 1034 | * |
| 1035 | * @param[in] set Set to check. |
| 1036 | * @param[in] node Node to look for in @p set. |
| 1037 | * @param[in] node_type Type of @p node. |
| 1038 | * @param[in] skip_idx Index from @p set to skip. |
| 1039 | * @return LY_ERR |
| 1040 | */ |
| 1041 | static LY_ERR |
| 1042 | set_dup_node_check(const struct lyxp_set *set, const struct lyd_node *node, enum lyxp_node_type node_type, int skip_idx) |
| 1043 | { |
| 1044 | uint32_t i; |
| 1045 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1046 | if (set->ht && node) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1047 | return set_dup_node_hash_check(set, (struct lyd_node *)node, node_type, skip_idx); |
| 1048 | } |
| 1049 | |
| 1050 | for (i = 0; i < set->used; ++i) { |
| 1051 | if ((skip_idx > -1) && (i == (unsigned)skip_idx)) { |
| 1052 | continue; |
| 1053 | } |
| 1054 | |
| 1055 | if ((set->val.nodes[i].node == node) && (set->val.nodes[i].type == node_type)) { |
| 1056 | return LY_EEXIST; |
| 1057 | } |
| 1058 | } |
| 1059 | |
| 1060 | return LY_SUCCESS; |
| 1061 | } |
| 1062 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 1063 | int |
| 1064 | 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] | 1065 | { |
| 1066 | uint32_t i; |
| 1067 | |
| 1068 | for (i = 0; i < set->used; ++i) { |
| 1069 | if ((skip_idx > -1) && (i == (unsigned)skip_idx)) { |
| 1070 | continue; |
| 1071 | } |
| 1072 | |
| 1073 | if ((set->val.scnodes[i].scnode == node) && (set->val.scnodes[i].type == node_type)) { |
| 1074 | return i; |
| 1075 | } |
| 1076 | } |
| 1077 | |
| 1078 | return -1; |
| 1079 | } |
| 1080 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 1081 | void |
| 1082 | lyxp_set_scnode_merge(struct lyxp_set *set1, struct lyxp_set *set2) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1083 | { |
| 1084 | uint32_t orig_used, i, j; |
| 1085 | |
| 1086 | assert(((set1->type == LYXP_SET_SCNODE_SET) || (set1->type == LYXP_SET_EMPTY)) |
| 1087 | && ((set2->type == LYXP_SET_SCNODE_SET) || (set2->type == LYXP_SET_EMPTY))); |
| 1088 | |
| 1089 | if (set2->type == LYXP_SET_EMPTY) { |
| 1090 | return; |
| 1091 | } |
| 1092 | |
| 1093 | if (set1->type == LYXP_SET_EMPTY) { |
| 1094 | memcpy(set1, set2, sizeof *set1); |
| 1095 | return; |
| 1096 | } |
| 1097 | |
| 1098 | if (set1->used + set2->used > set1->size) { |
| 1099 | set1->size = set1->used + set2->used; |
| 1100 | set1->val.scnodes = ly_realloc(set1->val.scnodes, set1->size * sizeof *set1->val.scnodes); |
| 1101 | LY_CHECK_ERR_RET(!set1->val.scnodes, LOGMEM(set1->ctx), ); |
| 1102 | } |
| 1103 | |
| 1104 | orig_used = set1->used; |
| 1105 | |
| 1106 | for (i = 0; i < set2->used; ++i) { |
| 1107 | for (j = 0; j < orig_used; ++j) { |
| 1108 | /* detect duplicities */ |
| 1109 | if (set1->val.scnodes[j].scnode == set2->val.scnodes[i].scnode) { |
| 1110 | break; |
| 1111 | } |
| 1112 | } |
| 1113 | |
| 1114 | if (j == orig_used) { |
| 1115 | memcpy(&set1->val.scnodes[set1->used], &set2->val.scnodes[i], sizeof *set2->val.scnodes); |
| 1116 | ++set1->used; |
| 1117 | } |
| 1118 | } |
| 1119 | |
| 1120 | set_free_content(set2); |
| 1121 | set2->type = LYXP_SET_EMPTY; |
| 1122 | } |
| 1123 | |
| 1124 | /** |
| 1125 | * @brief Insert a node into a set. Context position aware. |
| 1126 | * |
| 1127 | * @param[in] set Set to use. |
| 1128 | * @param[in] node Node to insert to @p set. |
| 1129 | * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting. |
| 1130 | * @param[in] node_type Node type of @p node. |
| 1131 | * @param[in] idx Index in @p set to insert into. |
| 1132 | */ |
| 1133 | static void |
| 1134 | set_insert_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx) |
| 1135 | { |
| 1136 | assert(set && ((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_EMPTY))); |
| 1137 | |
| 1138 | if (set->type == LYXP_SET_EMPTY) { |
| 1139 | /* first item */ |
| 1140 | if (idx) { |
| 1141 | /* no real harm done, but it is a bug */ |
| 1142 | LOGINT(set->ctx); |
| 1143 | idx = 0; |
| 1144 | } |
| 1145 | set->val.nodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.nodes); |
| 1146 | LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), ); |
| 1147 | set->type = LYXP_SET_NODE_SET; |
| 1148 | set->used = 0; |
| 1149 | set->size = LYXP_SET_SIZE_START; |
| 1150 | set->ctx_pos = 1; |
| 1151 | set->ctx_size = 1; |
| 1152 | set->ht = NULL; |
| 1153 | } else { |
| 1154 | /* not an empty set */ |
| 1155 | if (set->used == set->size) { |
| 1156 | |
| 1157 | /* set is full */ |
| 1158 | set->val.nodes = ly_realloc(set->val.nodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.nodes); |
| 1159 | LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), ); |
| 1160 | set->size += LYXP_SET_SIZE_STEP; |
| 1161 | } |
| 1162 | |
| 1163 | if (idx > set->used) { |
| 1164 | LOGINT(set->ctx); |
| 1165 | idx = set->used; |
| 1166 | } |
| 1167 | |
| 1168 | /* make space for the new node */ |
| 1169 | if (idx < set->used) { |
| 1170 | memmove(&set->val.nodes[idx + 1], &set->val.nodes[idx], (set->used - idx) * sizeof *set->val.nodes); |
| 1171 | } |
| 1172 | } |
| 1173 | |
| 1174 | /* finally assign the value */ |
| 1175 | set->val.nodes[idx].node = (struct lyd_node *)node; |
| 1176 | set->val.nodes[idx].type = node_type; |
| 1177 | set->val.nodes[idx].pos = pos; |
| 1178 | ++set->used; |
| 1179 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1180 | if (set->val.nodes[idx].type == LYXP_NODE_ELEM) { |
| 1181 | set_insert_node_hash(set, (struct lyd_node *)node, node_type); |
| 1182 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1183 | } |
| 1184 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 1185 | int |
| 1186 | 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] | 1187 | { |
| 1188 | int ret; |
| 1189 | |
| 1190 | assert(set->type == LYXP_SET_SCNODE_SET); |
| 1191 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 1192 | ret = lyxp_set_scnode_dup_node_check(set, node, node_type, -1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1193 | if (ret > -1) { |
| 1194 | set->val.scnodes[ret].in_ctx = 1; |
| 1195 | } else { |
| 1196 | if (set->used == set->size) { |
| 1197 | set->val.scnodes = ly_realloc(set->val.scnodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.scnodes); |
| 1198 | LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), -1); |
| 1199 | set->size += LYXP_SET_SIZE_STEP; |
| 1200 | } |
| 1201 | |
| 1202 | ret = set->used; |
| 1203 | set->val.scnodes[ret].scnode = (struct lysc_node *)node; |
| 1204 | set->val.scnodes[ret].type = node_type; |
| 1205 | set->val.scnodes[ret].in_ctx = 1; |
| 1206 | ++set->used; |
| 1207 | } |
| 1208 | |
| 1209 | return ret; |
| 1210 | } |
| 1211 | |
| 1212 | /** |
| 1213 | * @brief Replace a node in a set with another. Context position aware. |
| 1214 | * |
| 1215 | * @param[in] set Set to use. |
| 1216 | * @param[in] node Node to insert to @p set. |
| 1217 | * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting. |
| 1218 | * @param[in] node_type Node type of @p node. |
| 1219 | * @param[in] idx Index in @p set of the node to replace. |
| 1220 | */ |
| 1221 | static void |
| 1222 | set_replace_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx) |
| 1223 | { |
| 1224 | assert(set && (idx < set->used)); |
| 1225 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1226 | if (set->val.nodes[idx].type == LYXP_NODE_ELEM) { |
| 1227 | set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
| 1228 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1229 | set->val.nodes[idx].node = (struct lyd_node *)node; |
| 1230 | set->val.nodes[idx].type = node_type; |
| 1231 | set->val.nodes[idx].pos = pos; |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1232 | if (set->val.nodes[idx].type == LYXP_NODE_ELEM) { |
| 1233 | set_insert_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
| 1234 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1235 | } |
| 1236 | |
| 1237 | /** |
| 1238 | * @brief Set all nodes with ctx 1 to a new unique context value. |
| 1239 | * |
| 1240 | * @param[in] set Set to modify. |
| 1241 | * @return New context value. |
| 1242 | */ |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 1243 | static int32_t |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1244 | set_scnode_new_in_ctx(struct lyxp_set *set) |
| 1245 | { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 1246 | uint32_t i; |
| 1247 | int32_t ret_ctx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1248 | |
| 1249 | assert(set->type == LYXP_SET_SCNODE_SET); |
| 1250 | |
| 1251 | ret_ctx = 3; |
| 1252 | retry: |
| 1253 | for (i = 0; i < set->used; ++i) { |
| 1254 | if (set->val.scnodes[i].in_ctx >= ret_ctx) { |
| 1255 | ret_ctx = set->val.scnodes[i].in_ctx + 1; |
| 1256 | goto retry; |
| 1257 | } |
| 1258 | } |
| 1259 | for (i = 0; i < set->used; ++i) { |
| 1260 | if (set->val.scnodes[i].in_ctx == 1) { |
| 1261 | set->val.scnodes[i].in_ctx = ret_ctx; |
| 1262 | } |
| 1263 | } |
| 1264 | |
| 1265 | return ret_ctx; |
| 1266 | } |
| 1267 | |
| 1268 | /** |
| 1269 | * @brief Get unique @p node position in the data. |
| 1270 | * |
| 1271 | * @param[in] node Node to find. |
| 1272 | * @param[in] node_type Node type of @p node. |
| 1273 | * @param[in] root Root node. |
| 1274 | * @param[in] root_type Type of the XPath @p root node. |
| 1275 | * @param[in] prev Node that we think is before @p node in DFS from @p root. Can optionally |
| 1276 | * be used to increase efficiency and start the DFS from this node. |
| 1277 | * @param[in] prev_pos Node @p prev position. Optional, but must be set if @p prev is set. |
| 1278 | * @return Node position. |
| 1279 | */ |
| 1280 | static uint32_t |
| 1281 | get_node_pos(const struct lyd_node *node, enum lyxp_node_type node_type, const struct lyd_node *root, |
| 1282 | enum lyxp_node_type root_type, const struct lyd_node **prev, uint32_t *prev_pos) |
| 1283 | { |
| 1284 | const struct lyd_node *next, *elem, *top_sibling; |
| 1285 | uint32_t pos = 1; |
| 1286 | |
| 1287 | assert(prev && prev_pos && !root->prev->next); |
| 1288 | |
| 1289 | if ((node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ROOT_CONFIG)) { |
| 1290 | return 0; |
| 1291 | } |
| 1292 | |
| 1293 | if (*prev) { |
| 1294 | /* start from the previous element instead from the root */ |
| 1295 | elem = next = *prev; |
| 1296 | pos = *prev_pos; |
| 1297 | for (top_sibling = elem; top_sibling->parent; top_sibling = (struct lyd_node *)top_sibling->parent); |
| 1298 | goto dfs_search; |
| 1299 | } |
| 1300 | |
| 1301 | for (top_sibling = root; top_sibling; top_sibling = top_sibling->next) { |
| 1302 | /* TREE DFS */ |
| 1303 | LYD_TREE_DFS_BEGIN(top_sibling, next, elem) { |
| 1304 | dfs_search: |
| 1305 | if ((root_type == LYXP_NODE_ROOT_CONFIG) && (elem->schema->flags & LYS_CONFIG_R)) { |
| 1306 | goto skip_children; |
| 1307 | } |
| 1308 | |
| 1309 | if (elem == node) { |
| 1310 | break; |
| 1311 | } |
| 1312 | ++pos; |
| 1313 | |
| 1314 | /* TREE DFS END */ |
| 1315 | /* select element for the next run - children first, |
| 1316 | * child exception for lyd_node_leaf and lyd_node_leaflist, but not the root */ |
| 1317 | if (elem->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
| 1318 | next = NULL; |
| 1319 | } else { |
| 1320 | next = lyd_node_children(elem); |
| 1321 | } |
| 1322 | if (!next) { |
| 1323 | skip_children: |
| 1324 | /* no children */ |
| 1325 | if (elem == top_sibling) { |
| 1326 | /* we are done, root has no children */ |
| 1327 | elem = NULL; |
| 1328 | break; |
| 1329 | } |
| 1330 | /* try siblings */ |
| 1331 | next = elem->next; |
| 1332 | } |
| 1333 | while (!next) { |
| 1334 | /* no siblings, go back through parents */ |
| 1335 | if (elem->parent == top_sibling->parent) { |
| 1336 | /* we are done, no next element to process */ |
| 1337 | elem = NULL; |
| 1338 | break; |
| 1339 | } |
| 1340 | /* parent is already processed, go to its sibling */ |
| 1341 | elem = (struct lyd_node *)elem->parent; |
| 1342 | next = elem->next; |
| 1343 | } |
| 1344 | } |
| 1345 | |
| 1346 | /* node found */ |
| 1347 | if (elem) { |
| 1348 | break; |
| 1349 | } |
| 1350 | } |
| 1351 | |
| 1352 | if (!elem) { |
| 1353 | if (!(*prev)) { |
| 1354 | /* we went from root and failed to find it, cannot be */ |
| 1355 | LOGINT(node->schema->module->ctx); |
| 1356 | return 0; |
| 1357 | } else { |
| 1358 | *prev = NULL; |
| 1359 | *prev_pos = 0; |
| 1360 | |
| 1361 | elem = next = top_sibling = root; |
| 1362 | pos = 1; |
| 1363 | goto dfs_search; |
| 1364 | } |
| 1365 | } |
| 1366 | |
| 1367 | /* remember the last found node for next time */ |
| 1368 | *prev = node; |
| 1369 | *prev_pos = pos; |
| 1370 | |
| 1371 | return pos; |
| 1372 | } |
| 1373 | |
| 1374 | /** |
| 1375 | * @brief Assign (fill) missing node positions. |
| 1376 | * |
| 1377 | * @param[in] set Set to fill positions in. |
| 1378 | * @param[in] root Context root node. |
| 1379 | * @param[in] root_type Context root type. |
| 1380 | * @return LY_ERR |
| 1381 | */ |
| 1382 | static LY_ERR |
| 1383 | set_assign_pos(struct lyxp_set *set, const struct lyd_node *root, enum lyxp_node_type root_type) |
| 1384 | { |
| 1385 | const struct lyd_node *prev = NULL, *tmp_node; |
| 1386 | uint32_t i, tmp_pos = 0; |
| 1387 | |
| 1388 | for (i = 0; i < set->used; ++i) { |
| 1389 | if (!set->val.nodes[i].pos) { |
| 1390 | tmp_node = NULL; |
| 1391 | switch (set->val.nodes[i].type) { |
| 1392 | case LYXP_NODE_ATTR: |
| 1393 | tmp_node = set->val.attrs[i].attr->parent; |
| 1394 | if (!tmp_node) { |
| 1395 | LOGINT_RET(root->schema->module->ctx); |
| 1396 | } |
| 1397 | /* fallthrough */ |
| 1398 | case LYXP_NODE_ELEM: |
| 1399 | case LYXP_NODE_TEXT: |
| 1400 | if (!tmp_node) { |
| 1401 | tmp_node = set->val.nodes[i].node; |
| 1402 | } |
| 1403 | set->val.nodes[i].pos = get_node_pos(tmp_node, set->val.nodes[i].type, root, root_type, &prev, &tmp_pos); |
| 1404 | break; |
| 1405 | default: |
| 1406 | /* all roots have position 0 */ |
| 1407 | break; |
| 1408 | } |
| 1409 | } |
| 1410 | } |
| 1411 | |
| 1412 | return LY_SUCCESS; |
| 1413 | } |
| 1414 | |
| 1415 | /** |
| 1416 | * @brief Get unique @p attr position in the parent attributes. |
| 1417 | * |
| 1418 | * @param[in] attr Attr to use. |
| 1419 | * @return Attribute position. |
| 1420 | */ |
| 1421 | static uint16_t |
| 1422 | get_attr_pos(struct lyd_attr *attr) |
| 1423 | { |
| 1424 | uint16_t pos = 0; |
| 1425 | struct lyd_attr *attr2; |
| 1426 | |
| 1427 | for (attr2 = attr->parent->attr; attr2 && (attr2 != attr); attr2 = attr2->next) { |
| 1428 | ++pos; |
| 1429 | } |
| 1430 | |
| 1431 | assert(attr2); |
| 1432 | return pos; |
| 1433 | } |
| 1434 | |
| 1435 | /** |
| 1436 | * @brief Compare 2 nodes in respect to XPath document order. |
| 1437 | * |
| 1438 | * @param[in] item1 1st node. |
| 1439 | * @param[in] item2 2nd node. |
| 1440 | * @return If 1st > 2nd returns 1, 1st == 2nd returns 0, and 1st < 2nd returns -1. |
| 1441 | */ |
| 1442 | static int |
| 1443 | set_sort_compare(struct lyxp_set_node *item1, struct lyxp_set_node *item2) |
| 1444 | { |
| 1445 | uint32_t attr_pos1 = 0, attr_pos2 = 0; |
| 1446 | |
| 1447 | if (item1->pos < item2->pos) { |
| 1448 | return -1; |
| 1449 | } |
| 1450 | |
| 1451 | if (item1->pos > item2->pos) { |
| 1452 | return 1; |
| 1453 | } |
| 1454 | |
| 1455 | /* node positions are equal, the fun case */ |
| 1456 | |
| 1457 | /* 1st ELEM - == - 2nd TEXT, 1st TEXT - == - 2nd ELEM */ |
| 1458 | /* special case since text nodes are actually saved as their parents */ |
| 1459 | if ((item1->node == item2->node) && (item1->type != item2->type)) { |
| 1460 | if (item1->type == LYXP_NODE_ELEM) { |
| 1461 | assert(item2->type == LYXP_NODE_TEXT); |
| 1462 | return -1; |
| 1463 | } else { |
| 1464 | assert((item1->type == LYXP_NODE_TEXT) && (item2->type == LYXP_NODE_ELEM)); |
| 1465 | return 1; |
| 1466 | } |
| 1467 | } |
| 1468 | |
| 1469 | /* we need attr positions now */ |
| 1470 | if (item1->type == LYXP_NODE_ATTR) { |
| 1471 | attr_pos1 = get_attr_pos((struct lyd_attr *)item1->node); |
| 1472 | } |
| 1473 | if (item2->type == LYXP_NODE_ATTR) { |
| 1474 | attr_pos2 = get_attr_pos((struct lyd_attr *)item2->node); |
| 1475 | } |
| 1476 | |
| 1477 | /* 1st ROOT - 2nd ROOT, 1st ELEM - 2nd ELEM, 1st TEXT - 2nd TEXT, 1st ATTR - =pos= - 2nd ATTR */ |
| 1478 | /* check for duplicates */ |
| 1479 | if (item1->node == item2->node) { |
| 1480 | assert((item1->type == item2->type) && ((item1->type != LYXP_NODE_ATTR) || (attr_pos1 == attr_pos2))); |
| 1481 | return 0; |
| 1482 | } |
| 1483 | |
| 1484 | /* 1st ELEM - 2nd TEXT, 1st ELEM - any pos - 2nd ATTR */ |
| 1485 | /* elem is always first, 2nd node is after it */ |
| 1486 | if (item1->type == LYXP_NODE_ELEM) { |
| 1487 | assert(item2->type != LYXP_NODE_ELEM); |
| 1488 | return -1; |
| 1489 | } |
| 1490 | |
| 1491 | /* 1st TEXT - 2nd ELEM, 1st TEXT - any pos - 2nd ATTR, 1st ATTR - any pos - 2nd ELEM, 1st ATTR - >pos> - 2nd ATTR */ |
| 1492 | /* 2nd is before 1st */ |
| 1493 | if (((item1->type == LYXP_NODE_TEXT) |
| 1494 | && ((item2->type == LYXP_NODE_ELEM) || (item2->type == LYXP_NODE_ATTR))) |
| 1495 | || ((item1->type == LYXP_NODE_ATTR) && (item2->type == LYXP_NODE_ELEM)) |
| 1496 | || (((item1->type == LYXP_NODE_ATTR) && (item2->type == LYXP_NODE_ATTR)) |
| 1497 | && (attr_pos1 > attr_pos2))) { |
| 1498 | return 1; |
| 1499 | } |
| 1500 | |
| 1501 | /* 1st ATTR - any pos - 2nd TEXT, 1st ATTR <pos< - 2nd ATTR */ |
| 1502 | /* 2nd is after 1st */ |
| 1503 | return -1; |
| 1504 | } |
| 1505 | |
| 1506 | /** |
| 1507 | * @brief Set cast for comparisons. |
| 1508 | * |
| 1509 | * @param[in] trg Target set to cast source into. |
| 1510 | * @param[in] src Source set. |
| 1511 | * @param[in] type Target set type. |
| 1512 | * @param[in] src_idx Source set node index. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1513 | * @return LY_ERR |
| 1514 | */ |
| 1515 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1516 | 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] | 1517 | { |
| 1518 | assert(src->type == LYXP_SET_NODE_SET); |
| 1519 | |
| 1520 | set_init(trg, src); |
| 1521 | |
| 1522 | /* insert node into target set */ |
| 1523 | set_insert_node(trg, src->val.nodes[src_idx].node, src->val.nodes[src_idx].pos, src->val.nodes[src_idx].type, 0); |
| 1524 | |
| 1525 | /* cast target set appropriately */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1526 | return lyxp_set_cast(trg, type); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1527 | } |
| 1528 | |
| 1529 | #ifndef NDEBUG |
| 1530 | |
| 1531 | /** |
| 1532 | * @brief Bubble sort @p set into XPath document order. |
| 1533 | * Context position aware. Unused in the 'Release' build target. |
| 1534 | * |
| 1535 | * @param[in] set Set to sort. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1536 | * @return How many times the whole set was traversed - 1 (if set was sorted, returns 0). |
| 1537 | */ |
| 1538 | static int |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1539 | set_sort(struct lyxp_set *set) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1540 | { |
| 1541 | uint32_t i, j; |
| 1542 | int ret = 0, cmp, inverted, change; |
| 1543 | const struct lyd_node *root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1544 | struct lyxp_set_node item; |
| 1545 | struct lyxp_set_hash_node hnode; |
| 1546 | uint64_t hash; |
| 1547 | |
| 1548 | if ((set->type != LYXP_SET_NODE_SET) || (set->used == 1)) { |
| 1549 | return 0; |
| 1550 | } |
| 1551 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1552 | /* find first top-level node to be used as anchor for positions */ |
| 1553 | for (root = set->ctx_node; root->parent; root = (const struct lyd_node *)root->parent); |
| 1554 | for (; root->prev->next; root = root->prev); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1555 | |
| 1556 | /* fill positions */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1557 | if (set_assign_pos(set, root, set->root_type)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1558 | return -1; |
| 1559 | } |
| 1560 | |
| 1561 | LOGDBG(LY_LDGXPATH, "SORT BEGIN"); |
| 1562 | print_set_debug(set); |
| 1563 | |
| 1564 | for (i = 0; i < set->used; ++i) { |
| 1565 | inverted = 0; |
| 1566 | change = 0; |
| 1567 | |
| 1568 | for (j = 1; j < set->used - i; ++j) { |
| 1569 | /* compare node positions */ |
| 1570 | if (inverted) { |
| 1571 | cmp = set_sort_compare(&set->val.nodes[j], &set->val.nodes[j - 1]); |
| 1572 | } else { |
| 1573 | cmp = set_sort_compare(&set->val.nodes[j - 1], &set->val.nodes[j]); |
| 1574 | } |
| 1575 | |
| 1576 | /* swap if needed */ |
| 1577 | if ((inverted && (cmp < 0)) || (!inverted && (cmp > 0))) { |
| 1578 | change = 1; |
| 1579 | |
| 1580 | item = set->val.nodes[j - 1]; |
| 1581 | set->val.nodes[j - 1] = set->val.nodes[j]; |
| 1582 | set->val.nodes[j] = item; |
| 1583 | } else { |
| 1584 | /* whether node_pos1 should be smaller than node_pos2 or the other way around */ |
| 1585 | inverted = !inverted; |
| 1586 | } |
| 1587 | } |
| 1588 | |
| 1589 | ++ret; |
| 1590 | |
| 1591 | if (!change) { |
| 1592 | break; |
| 1593 | } |
| 1594 | } |
| 1595 | |
| 1596 | LOGDBG(LY_LDGXPATH, "SORT END %d", ret); |
| 1597 | print_set_debug(set); |
| 1598 | |
| 1599 | /* check node hashes */ |
| 1600 | if (set->used >= LYD_HT_MIN_ITEMS) { |
| 1601 | assert(set->ht); |
| 1602 | for (i = 0; i < set->used; ++i) { |
| 1603 | hnode.node = set->val.nodes[i].node; |
| 1604 | hnode.type = set->val.nodes[i].type; |
| 1605 | |
| 1606 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 1607 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 1608 | hash = dict_hash_multi(hash, NULL, 0); |
| 1609 | |
| 1610 | assert(!lyht_find(set->ht, &hnode, hash, NULL)); |
| 1611 | } |
| 1612 | } |
| 1613 | |
| 1614 | return ret - 1; |
| 1615 | } |
| 1616 | |
| 1617 | /** |
| 1618 | * @brief Remove duplicate entries in a sorted node set. |
| 1619 | * |
| 1620 | * @param[in] set Sorted set to check. |
| 1621 | * @return LY_ERR (LY_EEXIST if some duplicates are found) |
| 1622 | */ |
| 1623 | static LY_ERR |
| 1624 | set_sorted_dup_node_clean(struct lyxp_set *set) |
| 1625 | { |
| 1626 | uint32_t i = 0; |
| 1627 | LY_ERR ret = LY_SUCCESS; |
| 1628 | |
| 1629 | if (set->used > 1) { |
| 1630 | while (i < set->used - 1) { |
| 1631 | if ((set->val.nodes[i].node == set->val.nodes[i + 1].node) |
| 1632 | && (set->val.nodes[i].type == set->val.nodes[i + 1].type)) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1633 | set_remove_node_none(set, i + 1); |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1634 | ret = LY_EEXIST; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1635 | } |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1636 | ++i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1637 | } |
| 1638 | } |
| 1639 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1640 | set_remove_nodes_none(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1641 | return ret; |
| 1642 | } |
| 1643 | |
| 1644 | #endif |
| 1645 | |
| 1646 | /** |
| 1647 | * @brief Merge 2 sorted sets into one. |
| 1648 | * |
| 1649 | * @param[in,out] trg Set to merge into. Duplicates are removed. |
| 1650 | * @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] | 1651 | * @return LY_ERR |
| 1652 | */ |
| 1653 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1654 | set_sorted_merge(struct lyxp_set *trg, struct lyxp_set *src) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1655 | { |
| 1656 | uint32_t i, j, k, count, dup_count; |
| 1657 | int cmp; |
| 1658 | const struct lyd_node *root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1659 | |
| 1660 | if (((trg->type != LYXP_SET_NODE_SET) && (trg->type != LYXP_SET_EMPTY)) |
| 1661 | || ((src->type != LYXP_SET_NODE_SET) && (src->type != LYXP_SET_EMPTY))) { |
| 1662 | return LY_EINVAL; |
| 1663 | } |
| 1664 | |
| 1665 | if (src->type == LYXP_SET_EMPTY) { |
| 1666 | return LY_SUCCESS; |
| 1667 | } else if (trg->type == LYXP_SET_EMPTY) { |
| 1668 | set_fill_set(trg, src); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1669 | lyxp_set_cast(src, LYXP_SET_EMPTY); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1670 | return LY_SUCCESS; |
| 1671 | } |
| 1672 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1673 | /* find first top-level node to be used as anchor for positions */ |
| 1674 | for (root = trg->ctx_node; root->parent; root = (const struct lyd_node *)root->parent); |
| 1675 | for (; root->prev->next; root = root->prev); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1676 | |
| 1677 | /* fill positions */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1678 | 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] | 1679 | return LY_EINT; |
| 1680 | } |
| 1681 | |
| 1682 | #ifndef NDEBUG |
| 1683 | LOGDBG(LY_LDGXPATH, "MERGE target"); |
| 1684 | print_set_debug(trg); |
| 1685 | LOGDBG(LY_LDGXPATH, "MERGE source"); |
| 1686 | print_set_debug(src); |
| 1687 | #endif |
| 1688 | |
| 1689 | /* make memory for the merge (duplicates are not detected yet, so space |
| 1690 | * will likely be wasted on them, too bad) */ |
| 1691 | if (trg->size - trg->used < src->used) { |
| 1692 | trg->size = trg->used + src->used; |
| 1693 | |
| 1694 | trg->val.nodes = ly_realloc(trg->val.nodes, trg->size * sizeof *trg->val.nodes); |
| 1695 | LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx), LY_EMEM); |
| 1696 | } |
| 1697 | |
| 1698 | i = 0; |
| 1699 | j = 0; |
| 1700 | count = 0; |
| 1701 | dup_count = 0; |
| 1702 | do { |
| 1703 | cmp = set_sort_compare(&src->val.nodes[i], &trg->val.nodes[j]); |
| 1704 | if (!cmp) { |
| 1705 | if (!count) { |
| 1706 | /* duplicate, just skip it */ |
| 1707 | ++i; |
| 1708 | ++j; |
| 1709 | } else { |
| 1710 | /* we are copying something already, so let's copy the duplicate too, |
| 1711 | * we are hoping that afterwards there are some more nodes to |
| 1712 | * copy and this way we can copy them all together */ |
| 1713 | ++count; |
| 1714 | ++dup_count; |
| 1715 | ++i; |
| 1716 | ++j; |
| 1717 | } |
| 1718 | } else if (cmp < 0) { |
| 1719 | /* inserting src node into trg, just remember it for now */ |
| 1720 | ++count; |
| 1721 | ++i; |
| 1722 | |
| 1723 | /* insert the hash now */ |
| 1724 | set_insert_node_hash(trg, src->val.nodes[i - 1].node, src->val.nodes[i - 1].type); |
| 1725 | } else if (count) { |
| 1726 | copy_nodes: |
| 1727 | /* time to actually copy the nodes, we have found the largest block of nodes */ |
| 1728 | memmove(&trg->val.nodes[j + (count - dup_count)], |
| 1729 | &trg->val.nodes[j], |
| 1730 | (trg->used - j) * sizeof *trg->val.nodes); |
| 1731 | memcpy(&trg->val.nodes[j - dup_count], &src->val.nodes[i - count], count * sizeof *src->val.nodes); |
| 1732 | |
| 1733 | trg->used += count - dup_count; |
| 1734 | /* do not change i, except the copying above, we are basically doing exactly what is in the else branch below */ |
| 1735 | j += count - dup_count; |
| 1736 | |
| 1737 | count = 0; |
| 1738 | dup_count = 0; |
| 1739 | } else { |
| 1740 | ++j; |
| 1741 | } |
| 1742 | } while ((i < src->used) && (j < trg->used)); |
| 1743 | |
| 1744 | if ((i < src->used) || count) { |
| 1745 | /* insert all the hashes first */ |
| 1746 | for (k = i; k < src->used; ++k) { |
| 1747 | set_insert_node_hash(trg, src->val.nodes[k].node, src->val.nodes[k].type); |
| 1748 | } |
| 1749 | |
| 1750 | /* loop ended, but we need to copy something at trg end */ |
| 1751 | count += src->used - i; |
| 1752 | i = src->used; |
| 1753 | goto copy_nodes; |
| 1754 | } |
| 1755 | |
| 1756 | /* we are inserting hashes before the actual node insert, which causes |
| 1757 | * situations when there were initially not enough items for a hash table, |
| 1758 | * but even after some were inserted, hash table was not created (during |
| 1759 | * insertion the number of items is not updated yet) */ |
| 1760 | if (!trg->ht && (trg->used >= LYD_HT_MIN_ITEMS)) { |
| 1761 | set_insert_node_hash(trg, NULL, 0); |
| 1762 | } |
| 1763 | |
| 1764 | #ifndef NDEBUG |
| 1765 | LOGDBG(LY_LDGXPATH, "MERGE result"); |
| 1766 | print_set_debug(trg); |
| 1767 | #endif |
| 1768 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1769 | lyxp_set_cast(src, LYXP_SET_EMPTY); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1770 | return LY_SUCCESS; |
| 1771 | } |
| 1772 | |
| 1773 | /* |
| 1774 | * (re)parse functions |
| 1775 | * |
| 1776 | * Parse functions parse the expression into |
| 1777 | * tokens (syntactic analysis). |
| 1778 | * |
| 1779 | * Reparse functions perform semantic analysis |
| 1780 | * (do not save the result, just a check) of |
| 1781 | * the expression and fill repeat indices. |
| 1782 | */ |
| 1783 | |
| 1784 | /** |
| 1785 | * @brief Look at the next token and check its kind. |
| 1786 | * |
| 1787 | * @param[in] ctx Context for logging. |
| 1788 | * @param[in] exp Expression to use. |
| 1789 | * @param[in] exp_idx Position in the expression \p exp. |
| 1790 | * @param[in] want_tok Expected token. |
| 1791 | * @param[in] strict Whether the token is strictly required (print error if |
| 1792 | * not the next one) or we simply want to check whether it is the next or not. |
| 1793 | * @return LY_ERR |
| 1794 | */ |
| 1795 | static LY_ERR |
| 1796 | exp_check_token(struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t exp_idx, enum lyxp_token want_tok, int strict) |
| 1797 | { |
| 1798 | if (exp->used == exp_idx) { |
| 1799 | if (strict) { |
| 1800 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF); |
| 1801 | } |
| 1802 | return LY_EINVAL; |
| 1803 | } |
| 1804 | |
| 1805 | if (want_tok && (exp->tokens[exp_idx] != want_tok)) { |
| 1806 | if (strict) { |
| 1807 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, |
| 1808 | print_token(exp->tokens[exp_idx]), &exp->expr[exp->tok_pos[exp_idx]]); |
| 1809 | } |
| 1810 | return LY_EINVAL; |
| 1811 | } |
| 1812 | |
| 1813 | return LY_SUCCESS; |
| 1814 | } |
| 1815 | |
| 1816 | /** |
| 1817 | * @brief Stack operation push on the repeat array. |
| 1818 | * |
| 1819 | * @param[in] exp Expression to use. |
| 1820 | * @param[in] exp_idx Position in the expresion \p exp. |
| 1821 | * @param[in] repeat_op_idx Index from \p exp of the operator token. This value is pushed. |
| 1822 | */ |
| 1823 | static void |
| 1824 | exp_repeat_push(struct lyxp_expr *exp, uint16_t exp_idx, uint16_t repeat_op_idx) |
| 1825 | { |
| 1826 | uint16_t i; |
| 1827 | |
| 1828 | if (exp->repeat[exp_idx]) { |
| 1829 | for (i = 0; exp->repeat[exp_idx][i]; ++i); |
| 1830 | exp->repeat[exp_idx] = realloc(exp->repeat[exp_idx], (i + 2) * sizeof *exp->repeat[exp_idx]); |
| 1831 | LY_CHECK_ERR_RET(!exp->repeat[exp_idx], LOGMEM(NULL), ); |
| 1832 | exp->repeat[exp_idx][i] = repeat_op_idx; |
| 1833 | exp->repeat[exp_idx][i + 1] = 0; |
| 1834 | } else { |
| 1835 | exp->repeat[exp_idx] = calloc(2, sizeof *exp->repeat[exp_idx]); |
| 1836 | LY_CHECK_ERR_RET(!exp->repeat[exp_idx], LOGMEM(NULL), ); |
| 1837 | exp->repeat[exp_idx][0] = repeat_op_idx; |
| 1838 | } |
| 1839 | } |
| 1840 | |
| 1841 | /** |
| 1842 | * @brief Reparse Predicate. Logs directly on error. |
| 1843 | * |
| 1844 | * [7] Predicate ::= '[' Expr ']' |
| 1845 | * |
| 1846 | * @param[in] ctx Context for logging. |
| 1847 | * @param[in] exp Parsed XPath expression. |
| 1848 | * @param[in] exp_idx Position in the expression @p exp. |
| 1849 | * @return LY_ERR |
| 1850 | */ |
| 1851 | static LY_ERR |
| 1852 | reparse_predicate(struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *exp_idx) |
| 1853 | { |
| 1854 | LY_ERR rc; |
| 1855 | |
| 1856 | rc = exp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_BRACK1, 1); |
| 1857 | LY_CHECK_RET(rc); |
| 1858 | ++(*exp_idx); |
| 1859 | |
| 1860 | rc = reparse_or_expr(ctx, exp, exp_idx); |
| 1861 | LY_CHECK_RET(rc); |
| 1862 | |
| 1863 | rc = exp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_BRACK2, 1); |
| 1864 | LY_CHECK_RET(rc); |
| 1865 | ++(*exp_idx); |
| 1866 | |
| 1867 | return LY_SUCCESS; |
| 1868 | } |
| 1869 | |
| 1870 | /** |
| 1871 | * @brief Reparse RelativeLocationPath. Logs directly on error. |
| 1872 | * |
| 1873 | * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step |
| 1874 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 1875 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 1876 | * |
| 1877 | * @param[in] ctx Context for logging. |
| 1878 | * @param[in] exp Parsed XPath expression. |
| 1879 | * @param[in] exp_idx Position in the expression \p exp. |
| 1880 | * @return LY_ERR (LY_EINCOMPLETE on forward reference) |
| 1881 | */ |
| 1882 | static LY_ERR |
| 1883 | reparse_relative_location_path(struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *exp_idx) |
| 1884 | { |
| 1885 | LY_ERR rc; |
| 1886 | |
| 1887 | rc = exp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_NONE, 1); |
| 1888 | LY_CHECK_RET(rc); |
| 1889 | |
| 1890 | goto step; |
| 1891 | do { |
| 1892 | /* '/' or '//' */ |
| 1893 | ++(*exp_idx); |
| 1894 | |
| 1895 | rc = exp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_NONE, 1); |
| 1896 | LY_CHECK_RET(rc); |
| 1897 | step: |
| 1898 | /* Step */ |
| 1899 | switch (exp->tokens[*exp_idx]) { |
| 1900 | case LYXP_TOKEN_DOT: |
| 1901 | ++(*exp_idx); |
| 1902 | break; |
| 1903 | |
| 1904 | case LYXP_TOKEN_DDOT: |
| 1905 | ++(*exp_idx); |
| 1906 | break; |
| 1907 | |
| 1908 | case LYXP_TOKEN_AT: |
| 1909 | ++(*exp_idx); |
| 1910 | |
| 1911 | rc = exp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_NONE, 1); |
| 1912 | LY_CHECK_RET(rc); |
| 1913 | if ((exp->tokens[*exp_idx] != LYXP_TOKEN_NAMETEST) && (exp->tokens[*exp_idx] != LYXP_TOKEN_NODETYPE)) { |
| 1914 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, |
| 1915 | print_token(exp->tokens[*exp_idx]), &exp->expr[exp->tok_pos[*exp_idx]]); |
| 1916 | return LY_EVALID; |
| 1917 | } |
| 1918 | /* fall through */ |
| 1919 | case LYXP_TOKEN_NAMETEST: |
| 1920 | ++(*exp_idx); |
| 1921 | goto reparse_predicate; |
| 1922 | break; |
| 1923 | |
| 1924 | case LYXP_TOKEN_NODETYPE: |
| 1925 | ++(*exp_idx); |
| 1926 | |
| 1927 | /* '(' */ |
| 1928 | rc = exp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_PAR1, 1); |
| 1929 | LY_CHECK_RET(rc); |
| 1930 | ++(*exp_idx); |
| 1931 | |
| 1932 | /* ')' */ |
| 1933 | rc = exp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_PAR2, 1); |
| 1934 | LY_CHECK_RET(rc); |
| 1935 | ++(*exp_idx); |
| 1936 | |
| 1937 | reparse_predicate: |
| 1938 | /* Predicate* */ |
| 1939 | while ((exp->used > *exp_idx) && (exp->tokens[*exp_idx] == LYXP_TOKEN_BRACK1)) { |
| 1940 | rc = reparse_predicate(ctx, exp, exp_idx); |
| 1941 | LY_CHECK_RET(rc); |
| 1942 | } |
| 1943 | break; |
| 1944 | default: |
| 1945 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, |
| 1946 | print_token(exp->tokens[*exp_idx]), &exp->expr[exp->tok_pos[*exp_idx]]); |
| 1947 | return LY_EVALID; |
| 1948 | } |
| 1949 | } while ((exp->used > *exp_idx) && (exp->tokens[*exp_idx] == LYXP_TOKEN_OPERATOR_PATH)); |
| 1950 | |
| 1951 | return LY_SUCCESS; |
| 1952 | } |
| 1953 | |
| 1954 | /** |
| 1955 | * @brief Reparse AbsoluteLocationPath. Logs directly on error. |
| 1956 | * |
| 1957 | * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath |
| 1958 | * |
| 1959 | * @param[in] ctx Context for logging. |
| 1960 | * @param[in] exp Parsed XPath expression. |
| 1961 | * @param[in] exp_idx Position in the expression \p exp. |
| 1962 | * @return LY_ERR |
| 1963 | */ |
| 1964 | static LY_ERR |
| 1965 | reparse_absolute_location_path(struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *exp_idx) |
| 1966 | { |
| 1967 | LY_ERR rc; |
| 1968 | |
| 1969 | rc = exp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_OPERATOR_PATH, 1); |
| 1970 | LY_CHECK_RET(rc); |
| 1971 | |
| 1972 | /* '/' RelativeLocationPath? */ |
| 1973 | if (exp->tok_len[*exp_idx] == 1) { |
| 1974 | /* '/' */ |
| 1975 | ++(*exp_idx); |
| 1976 | |
| 1977 | if (exp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_NONE, 0)) { |
| 1978 | return LY_SUCCESS; |
| 1979 | } |
| 1980 | switch (exp->tokens[*exp_idx]) { |
| 1981 | case LYXP_TOKEN_DOT: |
| 1982 | case LYXP_TOKEN_DDOT: |
| 1983 | case LYXP_TOKEN_AT: |
| 1984 | case LYXP_TOKEN_NAMETEST: |
| 1985 | case LYXP_TOKEN_NODETYPE: |
| 1986 | rc = reparse_relative_location_path(ctx, exp, exp_idx); |
| 1987 | LY_CHECK_RET(rc); |
| 1988 | /* fall through */ |
| 1989 | default: |
| 1990 | break; |
| 1991 | } |
| 1992 | |
| 1993 | /* '//' RelativeLocationPath */ |
| 1994 | } else { |
| 1995 | /* '//' */ |
| 1996 | ++(*exp_idx); |
| 1997 | |
| 1998 | rc = reparse_relative_location_path(ctx, exp, exp_idx); |
| 1999 | LY_CHECK_RET(rc); |
| 2000 | } |
| 2001 | |
| 2002 | return LY_SUCCESS; |
| 2003 | } |
| 2004 | |
| 2005 | /** |
| 2006 | * @brief Reparse FunctionCall. Logs directly on error. |
| 2007 | * |
| 2008 | * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')' |
| 2009 | * |
| 2010 | * @param[in] ctx Context for logging. |
| 2011 | * @param[in] exp Parsed XPath expression. |
| 2012 | * @param[in] exp_idx Position in the expression @p exp. |
| 2013 | * @return LY_ERR |
| 2014 | */ |
| 2015 | static LY_ERR |
| 2016 | reparse_function_call(struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *exp_idx) |
| 2017 | { |
| 2018 | int min_arg_count = -1, max_arg_count, arg_count; |
| 2019 | uint16_t func_exp_idx; |
| 2020 | LY_ERR rc; |
| 2021 | |
| 2022 | rc = exp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_FUNCNAME, 1); |
| 2023 | LY_CHECK_RET(rc); |
| 2024 | func_exp_idx = *exp_idx; |
| 2025 | switch (exp->tok_len[*exp_idx]) { |
| 2026 | case 3: |
| 2027 | if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "not", 3)) { |
| 2028 | min_arg_count = 1; |
| 2029 | max_arg_count = 1; |
| 2030 | } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "sum", 3)) { |
| 2031 | min_arg_count = 1; |
| 2032 | max_arg_count = 1; |
| 2033 | } |
| 2034 | break; |
| 2035 | case 4: |
| 2036 | if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "lang", 4)) { |
| 2037 | min_arg_count = 1; |
| 2038 | max_arg_count = 1; |
| 2039 | } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "last", 4)) { |
| 2040 | min_arg_count = 0; |
| 2041 | max_arg_count = 0; |
| 2042 | } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "name", 4)) { |
| 2043 | min_arg_count = 0; |
| 2044 | max_arg_count = 1; |
| 2045 | } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "true", 4)) { |
| 2046 | min_arg_count = 0; |
| 2047 | max_arg_count = 0; |
| 2048 | } |
| 2049 | break; |
| 2050 | case 5: |
| 2051 | if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "count", 5)) { |
| 2052 | min_arg_count = 1; |
| 2053 | max_arg_count = 1; |
| 2054 | } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "false", 5)) { |
| 2055 | min_arg_count = 0; |
| 2056 | max_arg_count = 0; |
| 2057 | } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "floor", 5)) { |
| 2058 | min_arg_count = 1; |
| 2059 | max_arg_count = 1; |
| 2060 | } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "round", 5)) { |
| 2061 | min_arg_count = 1; |
| 2062 | max_arg_count = 1; |
| 2063 | } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "deref", 5)) { |
| 2064 | min_arg_count = 1; |
| 2065 | max_arg_count = 1; |
| 2066 | } |
| 2067 | break; |
| 2068 | case 6: |
| 2069 | if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "concat", 6)) { |
| 2070 | min_arg_count = 2; |
Michal Vasko | be2e356 | 2019-10-15 15:35:35 +0200 | [diff] [blame] | 2071 | max_arg_count = INT_MAX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2072 | } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "number", 6)) { |
| 2073 | min_arg_count = 0; |
| 2074 | max_arg_count = 1; |
| 2075 | } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "string", 6)) { |
| 2076 | min_arg_count = 0; |
| 2077 | max_arg_count = 1; |
| 2078 | } |
| 2079 | break; |
| 2080 | case 7: |
| 2081 | if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "boolean", 7)) { |
| 2082 | min_arg_count = 1; |
| 2083 | max_arg_count = 1; |
| 2084 | } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "ceiling", 7)) { |
| 2085 | min_arg_count = 1; |
| 2086 | max_arg_count = 1; |
| 2087 | } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "current", 7)) { |
| 2088 | min_arg_count = 0; |
| 2089 | max_arg_count = 0; |
| 2090 | } |
| 2091 | break; |
| 2092 | case 8: |
| 2093 | if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "contains", 8)) { |
| 2094 | min_arg_count = 2; |
| 2095 | max_arg_count = 2; |
| 2096 | } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "position", 8)) { |
| 2097 | min_arg_count = 0; |
| 2098 | max_arg_count = 0; |
| 2099 | } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "re-match", 8)) { |
| 2100 | min_arg_count = 2; |
| 2101 | max_arg_count = 2; |
| 2102 | } |
| 2103 | break; |
| 2104 | case 9: |
| 2105 | if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "substring", 9)) { |
| 2106 | min_arg_count = 2; |
| 2107 | max_arg_count = 3; |
| 2108 | } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "translate", 9)) { |
| 2109 | min_arg_count = 3; |
| 2110 | max_arg_count = 3; |
| 2111 | } |
| 2112 | break; |
| 2113 | case 10: |
| 2114 | if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "local-name", 10)) { |
| 2115 | min_arg_count = 0; |
| 2116 | max_arg_count = 1; |
| 2117 | } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "enum-value", 10)) { |
| 2118 | min_arg_count = 1; |
| 2119 | max_arg_count = 1; |
| 2120 | } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "bit-is-set", 10)) { |
| 2121 | min_arg_count = 2; |
| 2122 | max_arg_count = 2; |
| 2123 | } |
| 2124 | break; |
| 2125 | case 11: |
| 2126 | if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "starts-with", 11)) { |
| 2127 | min_arg_count = 2; |
| 2128 | max_arg_count = 2; |
| 2129 | } |
| 2130 | break; |
| 2131 | case 12: |
| 2132 | if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "derived-from", 12)) { |
| 2133 | min_arg_count = 2; |
| 2134 | max_arg_count = 2; |
| 2135 | } |
| 2136 | break; |
| 2137 | case 13: |
| 2138 | if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "namespace-uri", 13)) { |
| 2139 | min_arg_count = 0; |
| 2140 | max_arg_count = 1; |
| 2141 | } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "string-length", 13)) { |
| 2142 | min_arg_count = 0; |
| 2143 | max_arg_count = 1; |
| 2144 | } |
| 2145 | break; |
| 2146 | case 15: |
| 2147 | if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "normalize-space", 15)) { |
| 2148 | min_arg_count = 0; |
| 2149 | max_arg_count = 1; |
| 2150 | } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "substring-after", 15)) { |
| 2151 | min_arg_count = 2; |
| 2152 | max_arg_count = 2; |
| 2153 | } |
| 2154 | break; |
| 2155 | case 16: |
| 2156 | if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "substring-before", 16)) { |
| 2157 | min_arg_count = 2; |
| 2158 | max_arg_count = 2; |
| 2159 | } |
| 2160 | break; |
| 2161 | case 20: |
| 2162 | if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "derived-from-or-self", 20)) { |
| 2163 | min_arg_count = 2; |
| 2164 | max_arg_count = 2; |
| 2165 | } |
| 2166 | break; |
| 2167 | } |
| 2168 | if (min_arg_count == -1) { |
| 2169 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INFUNC, exp->tok_len[*exp_idx], &exp->expr[exp->tok_pos[*exp_idx]]); |
| 2170 | return LY_EINVAL; |
| 2171 | } |
| 2172 | ++(*exp_idx); |
| 2173 | |
| 2174 | /* '(' */ |
| 2175 | rc = exp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_PAR1, 1); |
| 2176 | LY_CHECK_RET(rc); |
| 2177 | ++(*exp_idx); |
| 2178 | |
| 2179 | /* ( Expr ( ',' Expr )* )? */ |
| 2180 | arg_count = 0; |
| 2181 | rc = exp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_NONE, 1); |
| 2182 | LY_CHECK_RET(rc); |
| 2183 | if (exp->tokens[*exp_idx] != LYXP_TOKEN_PAR2) { |
| 2184 | ++arg_count; |
| 2185 | rc = reparse_or_expr(ctx, exp, exp_idx); |
| 2186 | LY_CHECK_RET(rc); |
| 2187 | } |
| 2188 | while ((exp->used > *exp_idx) && (exp->tokens[*exp_idx] == LYXP_TOKEN_COMMA)) { |
| 2189 | ++(*exp_idx); |
| 2190 | |
| 2191 | ++arg_count; |
| 2192 | rc = reparse_or_expr(ctx, exp, exp_idx); |
| 2193 | LY_CHECK_RET(rc); |
| 2194 | } |
| 2195 | |
| 2196 | /* ')' */ |
| 2197 | rc = exp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_PAR2, 1); |
| 2198 | LY_CHECK_RET(rc); |
| 2199 | ++(*exp_idx); |
| 2200 | |
| 2201 | if ((arg_count < min_arg_count) || (arg_count > max_arg_count)) { |
| 2202 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INARGCOUNT, arg_count, exp->tok_len[func_exp_idx], |
| 2203 | &exp->expr[exp->tok_pos[func_exp_idx]]); |
| 2204 | return LY_EVALID; |
| 2205 | } |
| 2206 | |
| 2207 | return LY_SUCCESS; |
| 2208 | } |
| 2209 | |
| 2210 | /** |
| 2211 | * @brief Reparse PathExpr. Logs directly on error. |
| 2212 | * |
| 2213 | * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate* |
| 2214 | * | PrimaryExpr Predicate* '/' RelativeLocationPath |
| 2215 | * | PrimaryExpr Predicate* '//' RelativeLocationPath |
| 2216 | * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath |
| 2217 | * [8] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall |
| 2218 | * |
| 2219 | * @param[in] ctx Context for logging. |
| 2220 | * @param[in] exp Parsed XPath expression. |
| 2221 | * @param[in] exp_idx Position in the expression @p exp. |
| 2222 | * @return LY_ERR |
| 2223 | */ |
| 2224 | static LY_ERR |
| 2225 | reparse_path_expr(struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *exp_idx) |
| 2226 | { |
| 2227 | LY_ERR rc; |
| 2228 | |
| 2229 | if (exp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_NONE, 1)) { |
| 2230 | return -1; |
| 2231 | } |
| 2232 | |
| 2233 | switch (exp->tokens[*exp_idx]) { |
| 2234 | case LYXP_TOKEN_PAR1: |
| 2235 | /* '(' Expr ')' Predicate* */ |
| 2236 | ++(*exp_idx); |
| 2237 | |
| 2238 | rc = reparse_or_expr(ctx, exp, exp_idx); |
| 2239 | LY_CHECK_RET(rc); |
| 2240 | |
| 2241 | rc = exp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_PAR2, 1); |
| 2242 | LY_CHECK_RET(rc); |
| 2243 | ++(*exp_idx); |
| 2244 | goto predicate; |
| 2245 | break; |
| 2246 | case LYXP_TOKEN_DOT: |
| 2247 | case LYXP_TOKEN_DDOT: |
| 2248 | case LYXP_TOKEN_AT: |
| 2249 | case LYXP_TOKEN_NAMETEST: |
| 2250 | case LYXP_TOKEN_NODETYPE: |
| 2251 | /* RelativeLocationPath */ |
| 2252 | rc = reparse_relative_location_path(ctx, exp, exp_idx); |
| 2253 | LY_CHECK_RET(rc); |
| 2254 | break; |
| 2255 | case LYXP_TOKEN_FUNCNAME: |
| 2256 | /* FunctionCall */ |
| 2257 | rc = reparse_function_call(ctx, exp, exp_idx); |
| 2258 | LY_CHECK_RET(rc); |
| 2259 | goto predicate; |
| 2260 | break; |
| 2261 | case LYXP_TOKEN_OPERATOR_PATH: |
| 2262 | /* AbsoluteLocationPath */ |
| 2263 | rc = reparse_absolute_location_path(ctx, exp, exp_idx); |
| 2264 | LY_CHECK_RET(rc); |
| 2265 | break; |
| 2266 | case LYXP_TOKEN_LITERAL: |
| 2267 | /* Literal */ |
| 2268 | ++(*exp_idx); |
| 2269 | goto predicate; |
| 2270 | break; |
| 2271 | case LYXP_TOKEN_NUMBER: |
| 2272 | /* Number */ |
| 2273 | ++(*exp_idx); |
| 2274 | goto predicate; |
| 2275 | break; |
| 2276 | default: |
| 2277 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, |
| 2278 | print_token(exp->tokens[*exp_idx]), &exp->expr[exp->tok_pos[*exp_idx]]); |
| 2279 | return LY_EVALID; |
| 2280 | } |
| 2281 | |
| 2282 | return LY_SUCCESS; |
| 2283 | |
| 2284 | predicate: |
| 2285 | /* Predicate* */ |
| 2286 | while ((exp->used > *exp_idx) && (exp->tokens[*exp_idx] == LYXP_TOKEN_BRACK1)) { |
| 2287 | rc = reparse_predicate(ctx, exp, exp_idx); |
| 2288 | LY_CHECK_RET(rc); |
| 2289 | } |
| 2290 | |
| 2291 | /* ('/' or '//') RelativeLocationPath */ |
| 2292 | if ((exp->used > *exp_idx) && (exp->tokens[*exp_idx] == LYXP_TOKEN_OPERATOR_PATH)) { |
| 2293 | |
| 2294 | /* '/' or '//' */ |
| 2295 | ++(*exp_idx); |
| 2296 | |
| 2297 | rc = reparse_relative_location_path(ctx, exp, exp_idx); |
| 2298 | LY_CHECK_RET(rc); |
| 2299 | } |
| 2300 | |
| 2301 | return LY_SUCCESS; |
| 2302 | } |
| 2303 | |
| 2304 | /** |
| 2305 | * @brief Reparse UnaryExpr. Logs directly on error. |
| 2306 | * |
| 2307 | * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr |
| 2308 | * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr |
| 2309 | * |
| 2310 | * @param[in] ctx Context for logging. |
| 2311 | * @param[in] exp Parsed XPath expression. |
| 2312 | * @param[in] exp_idx Position in the expression @p exp. |
| 2313 | * @return LY_ERR |
| 2314 | */ |
| 2315 | static LY_ERR |
| 2316 | reparse_unary_expr(struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *exp_idx) |
| 2317 | { |
| 2318 | uint16_t prev_exp; |
| 2319 | LY_ERR rc; |
| 2320 | |
| 2321 | /* ('-')* */ |
| 2322 | prev_exp = *exp_idx; |
| 2323 | while (!exp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_OPERATOR_MATH, 0) |
| 2324 | && (exp->expr[exp->tok_pos[*exp_idx]] == '-')) { |
| 2325 | exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY); |
| 2326 | ++(*exp_idx); |
| 2327 | } |
| 2328 | |
| 2329 | /* PathExpr */ |
| 2330 | prev_exp = *exp_idx; |
| 2331 | rc = reparse_path_expr(ctx, exp, exp_idx); |
| 2332 | LY_CHECK_RET(rc); |
| 2333 | |
| 2334 | /* ('|' PathExpr)* */ |
| 2335 | while (!exp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_OPERATOR_UNI, 0)) { |
| 2336 | exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION); |
| 2337 | ++(*exp_idx); |
| 2338 | |
| 2339 | rc = reparse_path_expr(ctx, exp, exp_idx); |
| 2340 | LY_CHECK_RET(rc); |
| 2341 | } |
| 2342 | |
| 2343 | return LY_SUCCESS; |
| 2344 | } |
| 2345 | |
| 2346 | /** |
| 2347 | * @brief Reparse AdditiveExpr. Logs directly on error. |
| 2348 | * |
| 2349 | * [15] AdditiveExpr ::= MultiplicativeExpr |
| 2350 | * | AdditiveExpr '+' MultiplicativeExpr |
| 2351 | * | AdditiveExpr '-' MultiplicativeExpr |
| 2352 | * [16] MultiplicativeExpr ::= UnaryExpr |
| 2353 | * | MultiplicativeExpr '*' UnaryExpr |
| 2354 | * | MultiplicativeExpr 'div' UnaryExpr |
| 2355 | * | MultiplicativeExpr 'mod' UnaryExpr |
| 2356 | * |
| 2357 | * @param[in] ctx Context for logging. |
| 2358 | * @param[in] exp Parsed XPath expression. |
| 2359 | * @param[in] exp_idx Position in the expression @p exp. |
| 2360 | * @return LY_ERR |
| 2361 | */ |
| 2362 | static LY_ERR |
| 2363 | reparse_additive_expr(struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *exp_idx) |
| 2364 | { |
| 2365 | uint16_t prev_add_exp, prev_mul_exp; |
| 2366 | LY_ERR rc; |
| 2367 | |
| 2368 | prev_add_exp = *exp_idx; |
| 2369 | goto reparse_multiplicative_expr; |
| 2370 | |
| 2371 | /* ('+' / '-' MultiplicativeExpr)* */ |
| 2372 | while (!exp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_OPERATOR_MATH, 0) |
| 2373 | && ((exp->expr[exp->tok_pos[*exp_idx]] == '+') || (exp->expr[exp->tok_pos[*exp_idx]] == '-'))) { |
| 2374 | exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE); |
| 2375 | ++(*exp_idx); |
| 2376 | |
| 2377 | reparse_multiplicative_expr: |
| 2378 | /* UnaryExpr */ |
| 2379 | prev_mul_exp = *exp_idx; |
| 2380 | rc = reparse_unary_expr(ctx, exp, exp_idx); |
| 2381 | LY_CHECK_RET(rc); |
| 2382 | |
| 2383 | /* ('*' / 'div' / 'mod' UnaryExpr)* */ |
| 2384 | while (!exp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_OPERATOR_MATH, 0) |
| 2385 | && ((exp->expr[exp->tok_pos[*exp_idx]] == '*') || (exp->tok_len[*exp_idx] == 3))) { |
| 2386 | exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE); |
| 2387 | ++(*exp_idx); |
| 2388 | |
| 2389 | rc = reparse_unary_expr(ctx, exp, exp_idx); |
| 2390 | LY_CHECK_RET(rc); |
| 2391 | } |
| 2392 | } |
| 2393 | |
| 2394 | return LY_SUCCESS; |
| 2395 | } |
| 2396 | |
| 2397 | /** |
| 2398 | * @brief Reparse EqualityExpr. Logs directly on error. |
| 2399 | * |
| 2400 | * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr |
| 2401 | * | EqualityExpr '!=' RelationalExpr |
| 2402 | * [14] RelationalExpr ::= AdditiveExpr |
| 2403 | * | RelationalExpr '<' AdditiveExpr |
| 2404 | * | RelationalExpr '>' AdditiveExpr |
| 2405 | * | RelationalExpr '<=' AdditiveExpr |
| 2406 | * | RelationalExpr '>=' AdditiveExpr |
| 2407 | * |
| 2408 | * @param[in] ctx Context for logging. |
| 2409 | * @param[in] exp Parsed XPath expression. |
| 2410 | * @param[in] exp_idx Position in the expression @p exp. |
| 2411 | * @return LY_ERR |
| 2412 | */ |
| 2413 | static LY_ERR |
| 2414 | reparse_equality_expr(struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *exp_idx) |
| 2415 | { |
| 2416 | uint16_t prev_eq_exp, prev_rel_exp; |
| 2417 | LY_ERR rc; |
| 2418 | |
| 2419 | prev_eq_exp = *exp_idx; |
| 2420 | goto reparse_additive_expr; |
| 2421 | |
| 2422 | /* ('=' / '!=' RelationalExpr)* */ |
| 2423 | while (!exp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_OPERATOR_COMP, 0) |
| 2424 | && ((exp->expr[exp->tok_pos[*exp_idx]] == '=') || (exp->expr[exp->tok_pos[*exp_idx]] == '!'))) { |
| 2425 | exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY); |
| 2426 | ++(*exp_idx); |
| 2427 | |
| 2428 | reparse_additive_expr: |
| 2429 | /* AdditiveExpr */ |
| 2430 | prev_rel_exp = *exp_idx; |
| 2431 | rc = reparse_additive_expr(ctx, exp, exp_idx); |
| 2432 | LY_CHECK_RET(rc); |
| 2433 | |
| 2434 | /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */ |
| 2435 | while (!exp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_OPERATOR_COMP, 0) |
| 2436 | && ((exp->expr[exp->tok_pos[*exp_idx]] == '<') || (exp->expr[exp->tok_pos[*exp_idx]] == '>'))) { |
| 2437 | exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL); |
| 2438 | ++(*exp_idx); |
| 2439 | |
| 2440 | rc = reparse_additive_expr(ctx, exp, exp_idx); |
| 2441 | LY_CHECK_RET(rc); |
| 2442 | } |
| 2443 | } |
| 2444 | |
| 2445 | return LY_SUCCESS; |
| 2446 | } |
| 2447 | |
| 2448 | /** |
| 2449 | * @brief Reparse OrExpr. Logs directly on error. |
| 2450 | * |
| 2451 | * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr |
| 2452 | * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr |
| 2453 | * |
| 2454 | * @param[in] ctx Context for logging. |
| 2455 | * @param[in] exp Parsed XPath expression. |
| 2456 | * @param[in] exp_idx Position in the expression @p exp. |
| 2457 | * @return LY_ERR |
| 2458 | */ |
| 2459 | static LY_ERR |
| 2460 | reparse_or_expr(struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *exp_idx) |
| 2461 | { |
| 2462 | uint16_t prev_or_exp, prev_and_exp; |
| 2463 | LY_ERR rc; |
| 2464 | |
| 2465 | prev_or_exp = *exp_idx; |
| 2466 | goto reparse_equality_expr; |
| 2467 | |
| 2468 | /* ('or' AndExpr)* */ |
| 2469 | while (!exp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_OPERATOR_LOG, 0) && (exp->tok_len[*exp_idx] == 2)) { |
| 2470 | exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR); |
| 2471 | ++(*exp_idx); |
| 2472 | |
| 2473 | reparse_equality_expr: |
| 2474 | /* EqualityExpr */ |
| 2475 | prev_and_exp = *exp_idx; |
| 2476 | rc = reparse_equality_expr(ctx, exp, exp_idx); |
| 2477 | LY_CHECK_RET(rc); |
| 2478 | |
| 2479 | /* ('and' EqualityExpr)* */ |
| 2480 | while (!exp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_OPERATOR_LOG, 0) && (exp->tok_len[*exp_idx] == 3)) { |
| 2481 | exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND); |
| 2482 | ++(*exp_idx); |
| 2483 | |
| 2484 | rc = reparse_equality_expr(ctx, exp, exp_idx); |
| 2485 | LY_CHECK_RET(rc); |
| 2486 | } |
| 2487 | } |
| 2488 | |
| 2489 | return LY_SUCCESS; |
| 2490 | } |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2491 | |
| 2492 | /** |
| 2493 | * @brief Parse NCName. |
| 2494 | * |
| 2495 | * @param[in] ncname Name to parse. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2496 | * @return Length of @p ncname valid bytes. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2497 | */ |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2498 | static long int |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2499 | parse_ncname(const char *ncname) |
| 2500 | { |
| 2501 | unsigned int uc; |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2502 | size_t size; |
| 2503 | long int len = 0; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2504 | |
| 2505 | LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0); |
| 2506 | if (!is_xmlqnamestartchar(uc) || (uc == ':')) { |
| 2507 | return len; |
| 2508 | } |
| 2509 | |
| 2510 | do { |
| 2511 | len += size; |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 2512 | if (!*ncname) { |
| 2513 | break; |
| 2514 | } |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2515 | LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2516 | } while (is_xmlqnamechar(uc) && (uc != ':')); |
| 2517 | |
| 2518 | return len; |
| 2519 | } |
| 2520 | |
| 2521 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2522 | * @brief Add @p token into the expression @p exp. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2523 | * |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2524 | * @param[in] ctx Context for logging. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2525 | * @param[in] exp Expression to use. |
| 2526 | * @param[in] token Token to add. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2527 | * @param[in] tok_pos Token position in the XPath expression. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2528 | * @param[in] tok_len Token length in the XPath expression. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2529 | * @return LY_ERR |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2530 | */ |
| 2531 | static LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2532 | exp_add_token(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] | 2533 | { |
| 2534 | uint32_t prev; |
| 2535 | |
| 2536 | if (exp->used == exp->size) { |
| 2537 | prev = exp->size; |
| 2538 | exp->size += LYXP_EXPR_SIZE_STEP; |
| 2539 | if (prev > exp->size) { |
| 2540 | LOGINT(ctx); |
| 2541 | return LY_EINT; |
| 2542 | } |
| 2543 | |
| 2544 | exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens); |
| 2545 | LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM); |
| 2546 | exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos); |
| 2547 | LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM); |
| 2548 | exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len); |
| 2549 | LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM); |
| 2550 | } |
| 2551 | |
| 2552 | exp->tokens[exp->used] = token; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2553 | exp->tok_pos[exp->used] = tok_pos; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2554 | exp->tok_len[exp->used] = tok_len; |
| 2555 | ++exp->used; |
| 2556 | return LY_SUCCESS; |
| 2557 | } |
| 2558 | |
| 2559 | void |
| 2560 | lyxp_expr_free(struct ly_ctx *ctx, struct lyxp_expr *expr) |
| 2561 | { |
| 2562 | uint16_t i; |
| 2563 | |
| 2564 | if (!expr) { |
| 2565 | return; |
| 2566 | } |
| 2567 | |
| 2568 | lydict_remove(ctx, expr->expr); |
| 2569 | free(expr->tokens); |
| 2570 | free(expr->tok_pos); |
| 2571 | free(expr->tok_len); |
| 2572 | if (expr->repeat) { |
| 2573 | for (i = 0; i < expr->used; ++i) { |
| 2574 | free(expr->repeat[i]); |
| 2575 | } |
| 2576 | } |
| 2577 | free(expr->repeat); |
| 2578 | free(expr); |
| 2579 | } |
| 2580 | |
| 2581 | struct lyxp_expr * |
| 2582 | lyxp_expr_parse(struct ly_ctx *ctx, const char *expr) |
| 2583 | { |
| 2584 | struct lyxp_expr *ret; |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2585 | size_t parsed = 0, tok_len; |
| 2586 | long int ncname_len; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2587 | enum lyxp_token tok_type; |
| 2588 | int prev_function_check = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2589 | uint16_t exp_idx = 0; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2590 | |
| 2591 | if (strlen(expr) > UINT16_MAX) { |
| 2592 | LOGERR(ctx, LY_EINVAL, "XPath expression cannot be longer than %ud characters.", UINT16_MAX); |
| 2593 | return NULL; |
| 2594 | } |
| 2595 | |
| 2596 | /* init lyxp_expr structure */ |
| 2597 | ret = calloc(1, sizeof *ret); |
| 2598 | LY_CHECK_ERR_GOTO(!ret, LOGMEM(ctx), error); |
| 2599 | ret->expr = lydict_insert(ctx, expr, strlen(expr)); |
| 2600 | LY_CHECK_ERR_GOTO(!ret->expr, LOGMEM(ctx), error); |
| 2601 | ret->used = 0; |
| 2602 | ret->size = LYXP_EXPR_SIZE_START; |
| 2603 | ret->tokens = malloc(ret->size * sizeof *ret->tokens); |
| 2604 | LY_CHECK_ERR_GOTO(!ret->tokens, LOGMEM(ctx), error); |
| 2605 | |
| 2606 | ret->tok_pos = malloc(ret->size * sizeof *ret->tok_pos); |
| 2607 | LY_CHECK_ERR_GOTO(!ret->tok_pos, LOGMEM(ctx), error); |
| 2608 | |
| 2609 | ret->tok_len = malloc(ret->size * sizeof *ret->tok_len); |
| 2610 | LY_CHECK_ERR_GOTO(!ret->tok_len, LOGMEM(ctx), error); |
| 2611 | |
| 2612 | while (is_xmlws(expr[parsed])) { |
| 2613 | ++parsed; |
| 2614 | } |
| 2615 | |
| 2616 | do { |
| 2617 | if (expr[parsed] == '(') { |
| 2618 | |
| 2619 | /* '(' */ |
| 2620 | tok_len = 1; |
| 2621 | tok_type = LYXP_TOKEN_PAR1; |
| 2622 | |
| 2623 | if (prev_function_check && ret->used && (ret->tokens[ret->used - 1] == LYXP_TOKEN_NAMETEST)) { |
| 2624 | /* it is a NodeType/FunctionName after all */ |
| 2625 | if (((ret->tok_len[ret->used - 1] == 4) |
| 2626 | && (!strncmp(&expr[ret->tok_pos[ret->used - 1]], "node", 4) |
| 2627 | || !strncmp(&expr[ret->tok_pos[ret->used - 1]], "text", 4))) || |
| 2628 | ((ret->tok_len[ret->used - 1] == 7) |
| 2629 | && !strncmp(&expr[ret->tok_pos[ret->used - 1]], "comment", 7))) { |
| 2630 | ret->tokens[ret->used - 1] = LYXP_TOKEN_NODETYPE; |
| 2631 | } else { |
| 2632 | ret->tokens[ret->used - 1] = LYXP_TOKEN_FUNCNAME; |
| 2633 | } |
| 2634 | prev_function_check = 0; |
| 2635 | } |
| 2636 | |
| 2637 | } else if (expr[parsed] == ')') { |
| 2638 | |
| 2639 | /* ')' */ |
| 2640 | tok_len = 1; |
| 2641 | tok_type = LYXP_TOKEN_PAR2; |
| 2642 | |
| 2643 | } else if (expr[parsed] == '[') { |
| 2644 | |
| 2645 | /* '[' */ |
| 2646 | tok_len = 1; |
| 2647 | tok_type = LYXP_TOKEN_BRACK1; |
| 2648 | |
| 2649 | } else if (expr[parsed] == ']') { |
| 2650 | |
| 2651 | /* ']' */ |
| 2652 | tok_len = 1; |
| 2653 | tok_type = LYXP_TOKEN_BRACK2; |
| 2654 | |
| 2655 | } else if (!strncmp(&expr[parsed], "..", 2)) { |
| 2656 | |
| 2657 | /* '..' */ |
| 2658 | tok_len = 2; |
| 2659 | tok_type = LYXP_TOKEN_DDOT; |
| 2660 | |
| 2661 | } else if ((expr[parsed] == '.') && (!isdigit(expr[parsed + 1]))) { |
| 2662 | |
| 2663 | /* '.' */ |
| 2664 | tok_len = 1; |
| 2665 | tok_type = LYXP_TOKEN_DOT; |
| 2666 | |
| 2667 | } else if (expr[parsed] == '@') { |
| 2668 | |
| 2669 | /* '@' */ |
| 2670 | tok_len = 1; |
| 2671 | tok_type = LYXP_TOKEN_AT; |
| 2672 | |
| 2673 | } else if (expr[parsed] == ',') { |
| 2674 | |
| 2675 | /* ',' */ |
| 2676 | tok_len = 1; |
| 2677 | tok_type = LYXP_TOKEN_COMMA; |
| 2678 | |
| 2679 | } else if (expr[parsed] == '\'') { |
| 2680 | |
| 2681 | /* Literal with ' */ |
| 2682 | for (tok_len = 1; (expr[parsed + tok_len] != '\0') && (expr[parsed + tok_len] != '\''); ++tok_len); |
| 2683 | LY_CHECK_ERR_GOTO(expr[parsed + tok_len] == '\0', |
| 2684 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOE, expr[parsed], &expr[parsed]), error); |
| 2685 | ++tok_len; |
| 2686 | tok_type = LYXP_TOKEN_LITERAL; |
| 2687 | |
| 2688 | } else if (expr[parsed] == '\"') { |
| 2689 | |
| 2690 | /* Literal with " */ |
| 2691 | for (tok_len = 1; (expr[parsed + tok_len] != '\0') && (expr[parsed + tok_len] != '\"'); ++tok_len); |
| 2692 | LY_CHECK_ERR_GOTO(expr[parsed + tok_len] == '\0', |
| 2693 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOE, expr[parsed], &expr[parsed]), error); |
| 2694 | ++tok_len; |
| 2695 | tok_type = LYXP_TOKEN_LITERAL; |
| 2696 | |
| 2697 | } else if ((expr[parsed] == '.') || (isdigit(expr[parsed]))) { |
| 2698 | |
| 2699 | /* Number */ |
| 2700 | for (tok_len = 0; isdigit(expr[parsed + tok_len]); ++tok_len); |
| 2701 | if (expr[parsed + tok_len] == '.') { |
| 2702 | ++tok_len; |
| 2703 | for (; isdigit(expr[parsed + tok_len]); ++tok_len); |
| 2704 | } |
| 2705 | tok_type = LYXP_TOKEN_NUMBER; |
| 2706 | |
| 2707 | } else if (expr[parsed] == '/') { |
| 2708 | |
| 2709 | /* Operator '/', '//' */ |
| 2710 | if (!strncmp(&expr[parsed], "//", 2)) { |
| 2711 | tok_len = 2; |
| 2712 | } else { |
| 2713 | tok_len = 1; |
| 2714 | } |
| 2715 | tok_type = LYXP_TOKEN_OPERATOR_PATH; |
| 2716 | |
| 2717 | } else if (!strncmp(&expr[parsed], "!=", 2) || !strncmp(&expr[parsed], "<=", 2) |
| 2718 | || !strncmp(&expr[parsed], ">=", 2)) { |
| 2719 | |
| 2720 | /* Operator '!=', '<=', '>=' */ |
| 2721 | tok_len = 2; |
| 2722 | tok_type = LYXP_TOKEN_OPERATOR_COMP; |
| 2723 | |
| 2724 | } else if (expr[parsed] == '|') { |
| 2725 | |
| 2726 | /* Operator '|' */ |
| 2727 | tok_len = 1; |
| 2728 | tok_type = LYXP_TOKEN_OPERATOR_UNI; |
| 2729 | |
| 2730 | } else if ((expr[parsed] == '+') || (expr[parsed] == '-')) { |
| 2731 | |
| 2732 | /* Operator '+', '-' */ |
| 2733 | tok_len = 1; |
| 2734 | tok_type = LYXP_TOKEN_OPERATOR_MATH; |
| 2735 | |
| 2736 | } else if ((expr[parsed] == '=') || (expr[parsed] == '<') || (expr[parsed] == '>')) { |
| 2737 | |
| 2738 | /* Operator '=', '<', '>' */ |
| 2739 | tok_len = 1; |
| 2740 | tok_type = LYXP_TOKEN_OPERATOR_COMP; |
| 2741 | |
| 2742 | } else if (ret->used && (ret->tokens[ret->used - 1] != LYXP_TOKEN_AT) |
| 2743 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_PAR1) |
| 2744 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_BRACK1) |
| 2745 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_COMMA) |
| 2746 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPERATOR_LOG) |
| 2747 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPERATOR_COMP) |
| 2748 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPERATOR_MATH) |
| 2749 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPERATOR_UNI) |
| 2750 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPERATOR_PATH)) { |
| 2751 | |
| 2752 | /* Operator '*', 'or', 'and', 'mod', or 'div' */ |
| 2753 | if (expr[parsed] == '*') { |
| 2754 | tok_len = 1; |
| 2755 | tok_type = LYXP_TOKEN_OPERATOR_MATH; |
| 2756 | |
| 2757 | } else if (!strncmp(&expr[parsed], "or", 2)) { |
| 2758 | tok_len = 2; |
| 2759 | tok_type = LYXP_TOKEN_OPERATOR_LOG; |
| 2760 | |
| 2761 | } else if (!strncmp(&expr[parsed], "and", 3)) { |
| 2762 | tok_len = 3; |
| 2763 | tok_type = LYXP_TOKEN_OPERATOR_LOG; |
| 2764 | |
| 2765 | } else if (!strncmp(&expr[parsed], "mod", 3) || !strncmp(&expr[parsed], "div", 3)) { |
| 2766 | tok_len = 3; |
| 2767 | tok_type = LYXP_TOKEN_OPERATOR_MATH; |
| 2768 | |
| 2769 | } else if (prev_function_check) { |
Michal Vasko | 5307857 | 2019-05-24 08:50:15 +0200 | [diff] [blame] | 2770 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Invalid character 0x%x ('%c'), perhaps \"%.*s\" is supposed to be a function call.", |
| 2771 | 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] | 2772 | goto error; |
| 2773 | } else { |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2774 | 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] | 2775 | goto error; |
| 2776 | } |
| 2777 | } else if (expr[parsed] == '*') { |
| 2778 | |
| 2779 | /* NameTest '*' */ |
| 2780 | tok_len = 1; |
| 2781 | tok_type = LYXP_TOKEN_NAMETEST; |
| 2782 | |
| 2783 | } else { |
| 2784 | |
| 2785 | /* NameTest (NCName ':' '*' | QName) or NodeType/FunctionName */ |
| 2786 | ncname_len = parse_ncname(&expr[parsed]); |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2787 | 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] | 2788 | tok_len = ncname_len; |
| 2789 | |
| 2790 | if (expr[parsed + tok_len] == ':') { |
| 2791 | ++tok_len; |
| 2792 | if (expr[parsed + tok_len] == '*') { |
| 2793 | ++tok_len; |
| 2794 | } else { |
| 2795 | ncname_len = parse_ncname(&expr[parsed + tok_len]); |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2796 | 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] | 2797 | tok_len += ncname_len; |
| 2798 | } |
| 2799 | /* remove old flag to prevent ambiguities */ |
| 2800 | prev_function_check = 0; |
| 2801 | tok_type = LYXP_TOKEN_NAMETEST; |
| 2802 | } else { |
| 2803 | /* there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */ |
| 2804 | prev_function_check = 1; |
| 2805 | tok_type = LYXP_TOKEN_NAMETEST; |
| 2806 | } |
| 2807 | } |
| 2808 | |
| 2809 | /* store the token, move on to the next one */ |
| 2810 | LY_CHECK_GOTO(exp_add_token(ctx, ret, tok_type, parsed, tok_len), error); |
| 2811 | parsed += tok_len; |
| 2812 | while (is_xmlws(expr[parsed])) { |
| 2813 | ++parsed; |
| 2814 | } |
| 2815 | |
| 2816 | } while (expr[parsed]); |
| 2817 | |
| 2818 | /* prealloc repeat */ |
| 2819 | ret->repeat = calloc(ret->size, sizeof *ret->repeat); |
| 2820 | LY_CHECK_ERR_GOTO(!ret->repeat, LOGMEM(ctx), error); |
| 2821 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2822 | /* fill repeat */ |
| 2823 | LY_CHECK_GOTO(reparse_or_expr(ctx, ret, &exp_idx), error); |
| 2824 | if (ret->used > exp_idx) { |
| 2825 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, "Unknown", &ret->expr[ret->tok_pos[exp_idx]]); |
| 2826 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Unparsed characters \"%s\" left at the end of an XPath expression.", |
| 2827 | &ret->expr[ret->tok_pos[exp_idx]]); |
| 2828 | goto error; |
| 2829 | } |
| 2830 | |
| 2831 | print_expr_struct_debug(ret); |
| 2832 | |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2833 | return ret; |
| 2834 | |
| 2835 | error: |
| 2836 | lyxp_expr_free(ctx, ret); |
| 2837 | return NULL; |
| 2838 | } |
| 2839 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2840 | /* |
| 2841 | * warn functions |
| 2842 | * |
| 2843 | * Warn functions check specific reasonable conditions for schema XPath |
| 2844 | * and print a warning if they are not satisfied. |
| 2845 | */ |
| 2846 | |
| 2847 | /** |
| 2848 | * @brief Get the last-added schema node that is currently in the context. |
| 2849 | * |
| 2850 | * @param[in] set Set to search in. |
| 2851 | * @return Last-added schema context node, NULL if no node is in context. |
| 2852 | */ |
| 2853 | static struct lysc_node * |
| 2854 | warn_get_scnode_in_ctx(struct lyxp_set *set) |
| 2855 | { |
| 2856 | uint32_t i; |
| 2857 | |
| 2858 | if (!set || (set->type != LYXP_SET_SCNODE_SET)) { |
| 2859 | return NULL; |
| 2860 | } |
| 2861 | |
| 2862 | i = set->used; |
| 2863 | do { |
| 2864 | --i; |
| 2865 | if (set->val.scnodes[i].in_ctx == 1) { |
| 2866 | /* if there are more, simply return the first found (last added) */ |
| 2867 | return set->val.scnodes[i].scnode; |
| 2868 | } |
| 2869 | } while (i); |
| 2870 | |
| 2871 | return NULL; |
| 2872 | } |
| 2873 | |
| 2874 | /** |
| 2875 | * @brief Test whether a type is numeric - integer type or decimal64. |
| 2876 | * |
| 2877 | * @param[in] type Type to test. |
| 2878 | * @return 1 if numeric, 0 otherwise. |
| 2879 | */ |
| 2880 | static int |
| 2881 | warn_is_numeric_type(struct lysc_type *type) |
| 2882 | { |
| 2883 | struct lysc_type_union *uni; |
| 2884 | int ret; |
| 2885 | uint32_t i; |
| 2886 | |
| 2887 | switch (type->basetype) { |
| 2888 | case LY_TYPE_DEC64: |
| 2889 | case LY_TYPE_INT8: |
| 2890 | case LY_TYPE_UINT8: |
| 2891 | case LY_TYPE_INT16: |
| 2892 | case LY_TYPE_UINT16: |
| 2893 | case LY_TYPE_INT32: |
| 2894 | case LY_TYPE_UINT32: |
| 2895 | case LY_TYPE_INT64: |
| 2896 | case LY_TYPE_UINT64: |
| 2897 | return 1; |
| 2898 | case LY_TYPE_UNION: |
| 2899 | uni = (struct lysc_type_union *)type; |
| 2900 | LY_ARRAY_FOR(uni->types, i) { |
| 2901 | ret = warn_is_numeric_type(uni->types[i]); |
| 2902 | if (ret) { |
| 2903 | /* found a suitable type */ |
| 2904 | return 1; |
| 2905 | } |
| 2906 | } |
| 2907 | /* did not find any suitable type */ |
| 2908 | return 0; |
| 2909 | case LY_TYPE_LEAFREF: |
| 2910 | return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype); |
| 2911 | default: |
| 2912 | return 0; |
| 2913 | } |
| 2914 | } |
| 2915 | |
| 2916 | /** |
| 2917 | * @brief Test whether a type is string-like - no integers, decimal64 or binary. |
| 2918 | * |
| 2919 | * @param[in] type Type to test. |
| 2920 | * @return 1 if string, 0 otherwise. |
| 2921 | */ |
| 2922 | static int |
| 2923 | warn_is_string_type(struct lysc_type *type) |
| 2924 | { |
| 2925 | struct lysc_type_union *uni; |
| 2926 | int ret; |
| 2927 | uint32_t i; |
| 2928 | |
| 2929 | switch (type->basetype) { |
| 2930 | case LY_TYPE_BITS: |
| 2931 | case LY_TYPE_ENUM: |
| 2932 | case LY_TYPE_IDENT: |
| 2933 | case LY_TYPE_INST: |
| 2934 | case LY_TYPE_STRING: |
| 2935 | return 1; |
| 2936 | case LY_TYPE_UNION: |
| 2937 | uni = (struct lysc_type_union *)type; |
| 2938 | LY_ARRAY_FOR(uni->types, i) { |
| 2939 | ret = warn_is_string_type(uni->types[i]); |
| 2940 | if (ret) { |
| 2941 | /* found a suitable type */ |
| 2942 | return 1; |
| 2943 | } |
| 2944 | } |
| 2945 | /* did not find any suitable type */ |
| 2946 | return 0; |
| 2947 | case LY_TYPE_LEAFREF: |
| 2948 | return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype); |
| 2949 | default: |
| 2950 | return 0; |
| 2951 | } |
| 2952 | } |
| 2953 | |
| 2954 | /** |
| 2955 | * @brief Test whether a type is one specific type. |
| 2956 | * |
| 2957 | * @param[in] type Type to test. |
| 2958 | * @param[in] base Expected type. |
| 2959 | * @return 1 if it is, 0 otherwise. |
| 2960 | */ |
| 2961 | static int |
| 2962 | warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base) |
| 2963 | { |
| 2964 | struct lysc_type_union *uni; |
| 2965 | int ret; |
| 2966 | uint32_t i; |
| 2967 | |
| 2968 | if (type->basetype == base) { |
| 2969 | return 1; |
| 2970 | } else if (type->basetype == LY_TYPE_UNION) { |
| 2971 | uni = (struct lysc_type_union *)type; |
| 2972 | LY_ARRAY_FOR(uni->types, i) { |
| 2973 | ret = warn_is_specific_type(uni->types[i], base); |
| 2974 | if (ret) { |
| 2975 | /* found a suitable type */ |
| 2976 | return 1; |
| 2977 | } |
| 2978 | } |
| 2979 | /* did not find any suitable type */ |
| 2980 | return 0; |
| 2981 | } else if (type->basetype == LY_TYPE_LEAFREF) { |
| 2982 | return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base); |
| 2983 | } |
| 2984 | |
| 2985 | return 0; |
| 2986 | } |
| 2987 | |
| 2988 | /** |
| 2989 | * @brief Get next type of a (union) type. |
| 2990 | * |
| 2991 | * @param[in] type Base type. |
| 2992 | * @param[in] prev_type Previously returned type. |
| 2993 | * @return Next type or NULL. |
| 2994 | */ |
| 2995 | static struct lysc_type * |
| 2996 | warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type) |
| 2997 | { |
| 2998 | struct lysc_type_union *uni; |
| 2999 | int found = 0; |
| 3000 | uint32_t i; |
| 3001 | |
| 3002 | switch (type->basetype) { |
| 3003 | case LY_TYPE_UNION: |
| 3004 | uni = (struct lysc_type_union *)type; |
| 3005 | if (!prev_type) { |
| 3006 | return uni->types[0]; |
| 3007 | } |
| 3008 | LY_ARRAY_FOR(uni->types, i) { |
| 3009 | if (found) { |
| 3010 | return uni->types[i]; |
| 3011 | } |
| 3012 | if (prev_type == uni->types[i]) { |
| 3013 | found = 1; |
| 3014 | } |
| 3015 | } |
| 3016 | return NULL; |
| 3017 | default: |
| 3018 | if (prev_type) { |
| 3019 | assert(type == prev_type); |
| 3020 | return NULL; |
| 3021 | } else { |
| 3022 | return type; |
| 3023 | } |
| 3024 | } |
| 3025 | } |
| 3026 | |
| 3027 | /** |
| 3028 | * @brief Test whether 2 types have a common type. |
| 3029 | * |
| 3030 | * @param[in] type1 First type. |
| 3031 | * @param[in] type2 Second type. |
| 3032 | * @return 1 if they do, 0 otherwise. |
| 3033 | */ |
| 3034 | static int |
| 3035 | warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2) |
| 3036 | { |
| 3037 | struct lysc_type *t1, *rt1, *t2, *rt2; |
| 3038 | |
| 3039 | t1 = NULL; |
| 3040 | while ((t1 = warn_is_equal_type_next_type(type1, t1))) { |
| 3041 | if (t1->basetype == LY_TYPE_LEAFREF) { |
| 3042 | rt1 = ((struct lysc_type_leafref *)t1)->realtype; |
| 3043 | } else { |
| 3044 | rt1 = t1; |
| 3045 | } |
| 3046 | |
| 3047 | t2 = NULL; |
| 3048 | while ((t2 = warn_is_equal_type_next_type(type2, t2))) { |
| 3049 | if (t2->basetype == LY_TYPE_LEAFREF) { |
| 3050 | rt2 = ((struct lysc_type_leafref *)t2)->realtype; |
| 3051 | } else { |
| 3052 | rt2 = t2; |
| 3053 | } |
| 3054 | |
| 3055 | if (rt2->basetype == rt1->basetype) { |
| 3056 | /* match found */ |
| 3057 | return 1; |
| 3058 | } |
| 3059 | } |
| 3060 | } |
| 3061 | |
| 3062 | return 0; |
| 3063 | } |
| 3064 | |
| 3065 | /** |
| 3066 | * @brief Check both operands of comparison operators. |
| 3067 | * |
| 3068 | * @param[in] ctx Context for errors. |
| 3069 | * @param[in] set1 First operand set. |
| 3070 | * @param[in] set2 Second operand set. |
| 3071 | * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!='). |
| 3072 | * @param[in] expr Start of the expression to print with the warning. |
| 3073 | * @param[in] tok_pos Token position. |
| 3074 | */ |
| 3075 | static void |
| 3076 | warn_operands(struct ly_ctx *ctx, struct lyxp_set *set1, struct lyxp_set *set2, int numbers_only, const char *expr, uint16_t tok_pos) |
| 3077 | { |
| 3078 | struct lysc_node_leaf *node1, *node2; |
| 3079 | int leaves = 1, warning = 0; |
| 3080 | |
| 3081 | node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1); |
| 3082 | node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2); |
| 3083 | |
| 3084 | if (!node1 && !node2) { |
| 3085 | /* no node-sets involved, nothing to do */ |
| 3086 | return; |
| 3087 | } |
| 3088 | |
| 3089 | if (node1) { |
| 3090 | if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3091 | LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name); |
| 3092 | warning = 1; |
| 3093 | leaves = 0; |
| 3094 | } else if (numbers_only && !warn_is_numeric_type(node1->type)) { |
| 3095 | LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name); |
| 3096 | warning = 1; |
| 3097 | } |
| 3098 | } |
| 3099 | |
| 3100 | if (node2) { |
| 3101 | if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3102 | LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name); |
| 3103 | warning = 1; |
| 3104 | leaves = 0; |
| 3105 | } else if (numbers_only && !warn_is_numeric_type(node2->type)) { |
| 3106 | LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name); |
| 3107 | warning = 1; |
| 3108 | } |
| 3109 | } |
| 3110 | |
| 3111 | if (node1 && node2 && leaves && !numbers_only) { |
| 3112 | if ((warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type)) |
| 3113 | || (!warn_is_numeric_type(node1->type) && warn_is_numeric_type(node2->type)) |
| 3114 | || (!warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type) |
| 3115 | && !warn_is_equal_type(node1->type, node2->type))) { |
| 3116 | LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name); |
| 3117 | warning = 1; |
| 3118 | } |
| 3119 | } |
| 3120 | |
| 3121 | if (warning) { |
| 3122 | LOGWRN(ctx, "Previous warning generated by XPath subexpression[%u] \"%.20s\".", tok_pos, expr + tok_pos); |
| 3123 | } |
| 3124 | } |
| 3125 | |
| 3126 | /** |
| 3127 | * @brief Check that a value is valid for a leaf. If not applicable, does nothing. |
| 3128 | * |
| 3129 | * @param[in] exp Parsed XPath expression. |
| 3130 | * @param[in] set Set with the leaf/leaf-list. |
| 3131 | * @param[in] val_exp Index of the value (literal/number) in @p exp. |
| 3132 | * @param[in] equal_exp Index of the start of the equality expression in @p exp. |
| 3133 | * @param[in] last_equal_exp Index of the end of the equality expression in @p exp. |
| 3134 | */ |
| 3135 | static void |
| 3136 | warn_equality_value(struct lyxp_expr *exp, struct lyxp_set *set, uint16_t val_exp, uint16_t equal_exp, uint16_t last_equal_exp) |
| 3137 | { |
| 3138 | struct lysc_node *scnode; |
| 3139 | struct lysc_type *type; |
| 3140 | char *value; |
| 3141 | LY_ERR rc; |
| 3142 | struct ly_err_item *err = NULL; |
| 3143 | |
| 3144 | if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST)) |
| 3145 | && ((exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) || (exp->tokens[val_exp] == LYXP_TOKEN_NUMBER))) { |
| 3146 | /* check that the node can have the specified value */ |
| 3147 | if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) { |
| 3148 | value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2); |
| 3149 | } else { |
| 3150 | value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]); |
| 3151 | } |
| 3152 | if (!value) { |
| 3153 | LOGMEM(set->ctx); |
| 3154 | return; |
| 3155 | } |
| 3156 | |
| 3157 | if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) { |
| 3158 | LOGWRN(set->ctx, "Identityref \"%s\" comparison with identity \"%s\" without prefix, consider adding" |
| 3159 | " a prefix or best using \"derived-from(-or-self)()\" functions.", scnode->name, value); |
| 3160 | LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp], |
| 3161 | (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp], |
| 3162 | exp->expr + exp->tok_pos[equal_exp]); |
| 3163 | } |
| 3164 | |
| 3165 | type = ((struct lysc_node_leaf *)scnode)->type; |
| 3166 | if (type->basetype != LY_TYPE_IDENT) { |
| 3167 | rc = type->plugin->store(set->ctx, type, value, strlen(value), LY_TYPE_OPTS_SCHEMA, |
| 3168 | lys_resolve_prefix, (void *)type->dflt_mod, LYD_XML, NULL, NULL, NULL, NULL, &err); |
| 3169 | |
| 3170 | if (err) { |
| 3171 | LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg); |
| 3172 | ly_err_free(err); |
| 3173 | } else if (rc != LY_SUCCESS) { |
| 3174 | LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value); |
| 3175 | } |
| 3176 | if (rc != LY_SUCCESS) { |
| 3177 | LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp], |
| 3178 | (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp], |
| 3179 | exp->expr + exp->tok_pos[equal_exp]); |
| 3180 | } |
| 3181 | } |
| 3182 | free(value); |
| 3183 | } |
| 3184 | } |
| 3185 | |
| 3186 | /* |
| 3187 | * XPath functions |
| 3188 | */ |
| 3189 | |
| 3190 | /** |
| 3191 | * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN |
| 3192 | * depending on whether the first node bit value from the second argument is set. |
| 3193 | * |
| 3194 | * @param[in] args Array of arguments. |
| 3195 | * @param[in] arg_count Count of elements in @p args. |
| 3196 | * @param[in,out] set Context and result set at the same time. |
| 3197 | * @param[in] options XPath options. |
| 3198 | * @return LY_ERR (LY_EINVAL for wrong arguments on schema) |
| 3199 | */ |
| 3200 | static LY_ERR |
| 3201 | xpath_bit_is_set(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3202 | { |
| 3203 | struct lyd_node_term *leaf; |
| 3204 | struct lysc_node_leaf *sleaf; |
| 3205 | struct lysc_type_bits *bits; |
| 3206 | LY_ERR rc = LY_SUCCESS; |
| 3207 | uint32_t i; |
| 3208 | |
| 3209 | if (options & LYXP_SCNODE_ALL) { |
| 3210 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3211 | LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__); |
| 3212 | rc = LY_EINVAL; |
| 3213 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3214 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
| 3215 | rc = LY_EINVAL; |
| 3216 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) { |
| 3217 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"bits\".", __func__, sleaf->name); |
| 3218 | rc = LY_EINVAL; |
| 3219 | } |
| 3220 | |
| 3221 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 3222 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3223 | LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
| 3224 | rc = LY_EINVAL; |
| 3225 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3226 | LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name); |
| 3227 | rc = LY_EINVAL; |
| 3228 | } |
| 3229 | } |
| 3230 | set_scnode_clear_ctx(set); |
| 3231 | return rc; |
| 3232 | } |
| 3233 | |
| 3234 | if ((args[0]->type != LYXP_SET_NODE_SET) && (args[0]->type != LYXP_SET_EMPTY)) { |
| 3235 | 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)"); |
| 3236 | return LY_EVALID; |
| 3237 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3238 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3239 | LY_CHECK_RET(rc); |
| 3240 | |
| 3241 | set_fill_boolean(set, 0); |
| 3242 | if (args[0]->type == LYXP_SET_NODE_SET) { |
| 3243 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
| 3244 | if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) |
| 3245 | && (((struct lysc_node_leaf *)leaf->schema)->type->basetype == LY_TYPE_BITS)) { |
| 3246 | bits = (struct lysc_type_bits *)((struct lysc_node_leaf *)leaf->schema)->type; |
| 3247 | LY_ARRAY_FOR(bits->bits, i) { |
| 3248 | if (!strcmp(bits->bits[i].name, args[1]->val.str)) { |
| 3249 | set_fill_boolean(set, 1); |
| 3250 | break; |
| 3251 | } |
| 3252 | } |
| 3253 | } |
| 3254 | } |
| 3255 | |
| 3256 | return LY_SUCCESS; |
| 3257 | } |
| 3258 | |
| 3259 | /** |
| 3260 | * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN |
| 3261 | * with the argument converted to boolean. |
| 3262 | * |
| 3263 | * @param[in] args Array of arguments. |
| 3264 | * @param[in] arg_count Count of elements in @p args. |
| 3265 | * @param[in,out] set Context and result set at the same time. |
| 3266 | * @param[in] options XPath options. |
| 3267 | * @return LY_ERR (LY_EINVAL for wrong arguments on schema) |
| 3268 | */ |
| 3269 | static LY_ERR |
| 3270 | xpath_boolean(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3271 | { |
| 3272 | LY_ERR rc; |
| 3273 | |
| 3274 | if (options & LYXP_SCNODE_ALL) { |
| 3275 | set_scnode_clear_ctx(set); |
| 3276 | return LY_SUCCESS; |
| 3277 | } |
| 3278 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3279 | rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3280 | LY_CHECK_RET(rc); |
| 3281 | set_fill_set(set, args[0]); |
| 3282 | |
| 3283 | return LY_SUCCESS; |
| 3284 | } |
| 3285 | |
| 3286 | /** |
| 3287 | * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER |
| 3288 | * with the first argument rounded up to the nearest integer. |
| 3289 | * |
| 3290 | * @param[in] args Array of arguments. |
| 3291 | * @param[in] arg_count Count of elements in @p args. |
| 3292 | * @param[in,out] set Context and result set at the same time. |
| 3293 | * @param[in] options XPath options. |
| 3294 | * @return LY_ERR (LY_EINVAL for wrong arguments on schema) |
| 3295 | */ |
| 3296 | static LY_ERR |
| 3297 | xpath_ceiling(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3298 | { |
| 3299 | struct lysc_node_leaf *sleaf; |
| 3300 | LY_ERR rc = LY_SUCCESS; |
| 3301 | |
| 3302 | if (options & LYXP_SCNODE_ALL) { |
| 3303 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3304 | LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__); |
| 3305 | rc = LY_EINVAL; |
| 3306 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3307 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
| 3308 | rc = LY_EINVAL; |
| 3309 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) { |
| 3310 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name); |
| 3311 | rc = LY_EINVAL; |
| 3312 | } |
| 3313 | set_scnode_clear_ctx(set); |
| 3314 | return rc; |
| 3315 | } |
| 3316 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3317 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3318 | LY_CHECK_RET(rc); |
| 3319 | if ((long long)args[0]->val.num != args[0]->val.num) { |
| 3320 | set_fill_number(set, ((long long)args[0]->val.num) + 1); |
| 3321 | } else { |
| 3322 | set_fill_number(set, args[0]->val.num); |
| 3323 | } |
| 3324 | |
| 3325 | return LY_SUCCESS; |
| 3326 | } |
| 3327 | |
| 3328 | /** |
| 3329 | * @brief Execute the XPath concat(string, string, string*) function. |
| 3330 | * Returns LYXP_SET_STRING with the concatenation of all the arguments. |
| 3331 | * |
| 3332 | * @param[in] args Array of arguments. |
| 3333 | * @param[in] arg_count Count of elements in @p args. |
| 3334 | * @param[in,out] set Context and result set at the same time. |
| 3335 | * @param[in] options XPath options. |
| 3336 | * @return LY_ERR (LY_EINVAL for wrong arguments on schema) |
| 3337 | */ |
| 3338 | static LY_ERR |
| 3339 | xpath_concat(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options) |
| 3340 | { |
| 3341 | uint16_t i; |
| 3342 | char *str = NULL; |
| 3343 | size_t used = 1; |
| 3344 | LY_ERR rc = LY_SUCCESS; |
| 3345 | struct lysc_node_leaf *sleaf; |
| 3346 | |
| 3347 | if (options & LYXP_SCNODE_ALL) { |
| 3348 | for (i = 0; i < arg_count; ++i) { |
| 3349 | if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) { |
| 3350 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3351 | LOGWRN(set->ctx, "Argument #%u of %s is a %s node \"%s\".", |
| 3352 | i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
| 3353 | rc = LY_EINVAL; |
| 3354 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3355 | LOGWRN(set->ctx, "Argument #%u of %s is node \"%s\", not of string-type.", __func__, i + 1, sleaf->name); |
| 3356 | rc = LY_EINVAL; |
| 3357 | } |
| 3358 | } |
| 3359 | } |
| 3360 | set_scnode_clear_ctx(set); |
| 3361 | return rc; |
| 3362 | } |
| 3363 | |
| 3364 | for (i = 0; i < arg_count; ++i) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3365 | rc = lyxp_set_cast(args[i], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3366 | if (rc != LY_SUCCESS) { |
| 3367 | free(str); |
| 3368 | return rc; |
| 3369 | } |
| 3370 | |
| 3371 | str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char)); |
| 3372 | LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM); |
| 3373 | strcpy(str + used - 1, args[i]->val.str); |
| 3374 | used += strlen(args[i]->val.str); |
| 3375 | } |
| 3376 | |
| 3377 | /* free, kind of */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3378 | lyxp_set_cast(set, LYXP_SET_EMPTY); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3379 | set->type = LYXP_SET_STRING; |
| 3380 | set->val.str = str; |
| 3381 | |
| 3382 | return LY_SUCCESS; |
| 3383 | } |
| 3384 | |
| 3385 | /** |
| 3386 | * @brief Execute the XPath contains(string, string) function. |
| 3387 | * Returns LYXP_SET_BOOLEAN whether the second argument can |
| 3388 | * be found in the first or not. |
| 3389 | * |
| 3390 | * @param[in] args Array of arguments. |
| 3391 | * @param[in] arg_count Count of elements in @p args. |
| 3392 | * @param[in,out] set Context and result set at the same time. |
| 3393 | * @param[in] options XPath options. |
| 3394 | * @return LY_ERR (LY_EINVAL for wrong arguments on schema) |
| 3395 | */ |
| 3396 | static LY_ERR |
| 3397 | xpath_contains(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3398 | { |
| 3399 | struct lysc_node_leaf *sleaf; |
| 3400 | LY_ERR rc = LY_SUCCESS; |
| 3401 | |
| 3402 | if (options & LYXP_SCNODE_ALL) { |
| 3403 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3404 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3405 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
| 3406 | rc = LY_EINVAL; |
| 3407 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3408 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name); |
| 3409 | rc = LY_EINVAL; |
| 3410 | } |
| 3411 | } |
| 3412 | |
| 3413 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 3414 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3415 | LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
| 3416 | rc = LY_EINVAL; |
| 3417 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3418 | LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name); |
| 3419 | rc = LY_EINVAL; |
| 3420 | } |
| 3421 | } |
| 3422 | set_scnode_clear_ctx(set); |
| 3423 | return rc; |
| 3424 | } |
| 3425 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3426 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3427 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3428 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3429 | LY_CHECK_RET(rc); |
| 3430 | |
| 3431 | if (strstr(args[0]->val.str, args[1]->val.str)) { |
| 3432 | set_fill_boolean(set, 1); |
| 3433 | } else { |
| 3434 | set_fill_boolean(set, 0); |
| 3435 | } |
| 3436 | |
| 3437 | return LY_SUCCESS; |
| 3438 | } |
| 3439 | |
| 3440 | /** |
| 3441 | * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER |
| 3442 | * with the size of the node-set from the argument. |
| 3443 | * |
| 3444 | * @param[in] args Array of arguments. |
| 3445 | * @param[in] arg_count Count of elements in @p args. |
| 3446 | * @param[in,out] set Context and result set at the same time. |
| 3447 | * @param[in] options XPath options. |
| 3448 | * @return LY_ERR (LY_EINVAL for wrong arguments on schema) |
| 3449 | */ |
| 3450 | static LY_ERR |
| 3451 | xpath_count(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3452 | { |
| 3453 | struct lysc_node *scnode = NULL; |
| 3454 | LY_ERR rc = LY_SUCCESS; |
| 3455 | |
| 3456 | if (options & LYXP_SCNODE_ALL) { |
| 3457 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(scnode = warn_get_scnode_in_ctx(args[0]))) { |
| 3458 | LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__); |
| 3459 | rc = LY_EINVAL; |
| 3460 | } |
| 3461 | set_scnode_clear_ctx(set); |
| 3462 | return rc; |
| 3463 | } |
| 3464 | |
| 3465 | if (args[0]->type == LYXP_SET_EMPTY) { |
| 3466 | set_fill_number(set, 0); |
| 3467 | return LY_SUCCESS; |
| 3468 | } |
| 3469 | |
| 3470 | if (args[0]->type != LYXP_SET_NODE_SET) { |
| 3471 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "count(node-set)"); |
| 3472 | return LY_EVALID; |
| 3473 | } |
| 3474 | |
| 3475 | set_fill_number(set, args[0]->used); |
| 3476 | return LY_SUCCESS; |
| 3477 | } |
| 3478 | |
| 3479 | /** |
| 3480 | * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET |
| 3481 | * with the context with the intial node. |
| 3482 | * |
| 3483 | * @param[in] args Array of arguments. |
| 3484 | * @param[in] arg_count Count of elements in @p args. |
| 3485 | * @param[in,out] set Context and result set at the same time. |
| 3486 | * @param[in] options XPath options. |
| 3487 | * @return LY_ERR (LY_EINVAL for wrong arguments on schema) |
| 3488 | */ |
| 3489 | static LY_ERR |
| 3490 | xpath_current(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options) |
| 3491 | { |
| 3492 | if (arg_count || args) { |
| 3493 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGCOUNT, arg_count, "current()"); |
| 3494 | return LY_EVALID; |
| 3495 | } |
| 3496 | |
| 3497 | if (options & LYXP_SCNODE_ALL) { |
| 3498 | set_scnode_clear_ctx(set); |
| 3499 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 3500 | lyxp_set_scnode_insert_node(set, set->ctx_scnode, LYXP_NODE_ELEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3501 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3502 | lyxp_set_cast(set, LYXP_SET_EMPTY); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3503 | |
| 3504 | /* position is filled later */ |
| 3505 | set_insert_node(set, set->ctx_node, 0, LYXP_NODE_ELEM, 0); |
| 3506 | } |
| 3507 | |
| 3508 | return LY_SUCCESS; |
| 3509 | } |
| 3510 | |
| 3511 | /** |
| 3512 | * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either |
| 3513 | * leafref or instance-identifier target node(s). |
| 3514 | * |
| 3515 | * @param[in] args Array of arguments. |
| 3516 | * @param[in] arg_count Count of elements in @p args. |
| 3517 | * @param[in,out] set Context and result set at the same time. |
| 3518 | * @param[in] options XPath options. |
| 3519 | * @return LY_ERR (LY_EINVAL for wrong arguments on schema) |
| 3520 | */ |
| 3521 | static LY_ERR |
| 3522 | xpath_deref(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3523 | { |
Michal Vasko | ae9e4cb | 2019-09-25 08:43:05 +0200 | [diff] [blame] | 3524 | struct lysc_ctx cctx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3525 | struct lyd_node_term *leaf; |
| 3526 | struct lysc_node_leaf *sleaf; |
Michal Vasko | ae9e4cb | 2019-09-25 08:43:05 +0200 | [diff] [blame] | 3527 | const struct lysc_node *target; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3528 | const struct lyd_node *node; |
| 3529 | char *errmsg = NULL; |
| 3530 | const char *val; |
| 3531 | int dynamic; |
| 3532 | LY_ERR rc = LY_SUCCESS; |
| 3533 | |
| 3534 | if (options & LYXP_SCNODE_ALL) { |
| 3535 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3536 | LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__); |
| 3537 | rc = LY_EINVAL; |
| 3538 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3539 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
| 3540 | rc = LY_EINVAL; |
| 3541 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) && !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) { |
| 3542 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"leafref\" nor \"instance-identifier\".", |
| 3543 | __func__, sleaf->name); |
| 3544 | rc = LY_EINVAL; |
| 3545 | } |
| 3546 | set_scnode_clear_ctx(set); |
Michal Vasko | ae9e4cb | 2019-09-25 08:43:05 +0200 | [diff] [blame] | 3547 | if ((rc == LY_SUCCESS) && (sleaf->type->basetype == LY_TYPE_LEAFREF)) { |
| 3548 | cctx.ctx = set->ctx; |
| 3549 | rc = lys_compile_leafref_validate(&cctx, (struct lysc_node *)sleaf, (struct lysc_type_leafref *)sleaf->type, &target); |
| 3550 | /* it was already validated, it must succeed */ |
| 3551 | if (rc == LY_SUCCESS) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 3552 | lyxp_set_scnode_insert_node(set, target, LYXP_NODE_ELEM); |
Michal Vasko | ae9e4cb | 2019-09-25 08:43:05 +0200 | [diff] [blame] | 3553 | } |
| 3554 | } |
| 3555 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3556 | return rc; |
| 3557 | } |
| 3558 | |
| 3559 | if ((args[0]->type != LYXP_SET_NODE_SET) && (args[0]->type != LYXP_SET_EMPTY)) { |
| 3560 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "deref(node-set)"); |
| 3561 | return LY_EVALID; |
| 3562 | } |
| 3563 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3564 | lyxp_set_cast(set, LYXP_SET_EMPTY); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3565 | if (args[0]->type != LYXP_SET_EMPTY) { |
| 3566 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
| 3567 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 3568 | if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 3569 | if (sleaf->type->basetype == LY_TYPE_LEAFREF) { |
| 3570 | /* find leafref target */ |
| 3571 | val = lyd_value2str(leaf, &dynamic); |
| 3572 | node = ly_type_find_leafref(set->ctx, sleaf->type, val, strlen(val), (struct lyd_node *)leaf, |
| 3573 | set->trees, &leaf->value, &errmsg); |
| 3574 | if (dynamic) { |
| 3575 | free((char *)val); |
| 3576 | } |
| 3577 | if (!node) { |
| 3578 | LOGERR(set->ctx, LY_EINVAL, errmsg); |
| 3579 | free(errmsg); |
| 3580 | return LY_EINVAL; |
| 3581 | } |
| 3582 | |
| 3583 | /* insert it */ |
| 3584 | set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0); |
| 3585 | } else { |
| 3586 | assert(sleaf->type->basetype == LY_TYPE_INST); |
| 3587 | node = (struct lyd_node *)lyd_target(leaf->value.target, set->trees); |
| 3588 | if (!node) { |
| 3589 | val = lyd_value2str(leaf, &dynamic); |
| 3590 | LOGERR(set->ctx, LY_EVALID, "Invalid instance-identifier \"%s\" value - required instance not found.", val); |
| 3591 | if (dynamic) { |
| 3592 | free((char *)val); |
| 3593 | } |
| 3594 | return LY_EVALID; |
| 3595 | } |
| 3596 | } |
| 3597 | } |
| 3598 | } |
| 3599 | |
| 3600 | return LY_SUCCESS; |
| 3601 | } |
| 3602 | |
| 3603 | /** |
| 3604 | * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending |
| 3605 | * on whether the first argument nodes contain a node of an identity derived from the second |
| 3606 | * argument identity. |
| 3607 | * |
| 3608 | * @param[in] args Array of arguments. |
| 3609 | * @param[in] arg_count Count of elements in @p args. |
| 3610 | * @param[in,out] set Context and result set at the same time. |
| 3611 | * @param[in] options XPath options. |
| 3612 | * @return LY_ERR (LY_EINVAL for wrong arguments on schema) |
| 3613 | */ |
| 3614 | static LY_ERR |
| 3615 | xpath_derived_from(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3616 | { |
| 3617 | uint16_t i; |
| 3618 | struct lyd_node_term *leaf; |
| 3619 | struct lysc_node_leaf *sleaf; |
| 3620 | struct lyd_value data = {0}; |
| 3621 | struct ly_err_item *err = NULL; |
| 3622 | LY_ERR rc = LY_SUCCESS; |
| 3623 | int found; |
| 3624 | |
| 3625 | if (options & LYXP_SCNODE_ALL) { |
| 3626 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3627 | LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__); |
| 3628 | rc = LY_EINVAL; |
| 3629 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3630 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
| 3631 | rc = LY_EINVAL; |
| 3632 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) { |
| 3633 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", __func__, sleaf->name); |
| 3634 | rc = LY_EINVAL; |
| 3635 | } |
| 3636 | |
| 3637 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 3638 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3639 | LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
| 3640 | rc = LY_EINVAL; |
| 3641 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3642 | LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name); |
| 3643 | rc = LY_EINVAL; |
| 3644 | } |
| 3645 | } |
| 3646 | set_scnode_clear_ctx(set); |
| 3647 | return rc; |
| 3648 | } |
| 3649 | |
| 3650 | if ((args[0]->type != LYXP_SET_NODE_SET) && (args[0]->type != LYXP_SET_EMPTY)) { |
| 3651 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "derived-from(node-set, string)"); |
| 3652 | return LY_EVALID; |
| 3653 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3654 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3655 | LY_CHECK_RET(rc); |
| 3656 | |
| 3657 | set_fill_boolean(set, 0); |
| 3658 | if (args[0]->type != LYXP_SET_EMPTY) { |
| 3659 | found = 0; |
| 3660 | for (i = 0; i < args[0]->used; ++i) { |
| 3661 | leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node; |
| 3662 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 3663 | if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_IDENT)) { |
| 3664 | /* store args[1] into ident */ |
| 3665 | rc = sleaf->type->plugin->store(set->ctx, sleaf->type, args[1]->val.str, strlen(args[1]->val.str), |
| 3666 | LY_TYPE_OPTS_STORE, lys_resolve_prefix, (void *)sleaf->dflt_mod, set->format, |
| 3667 | (struct lyd_node *)leaf, set->trees, &data, NULL, &err); |
| 3668 | if (err) { |
| 3669 | ly_err_print(err); |
| 3670 | ly_err_free(err); |
| 3671 | } |
| 3672 | LY_CHECK_RET(rc); |
| 3673 | |
| 3674 | LY_ARRAY_FOR(data.ident->derived, i) { |
| 3675 | if (data.ident->derived[i] == leaf->value.ident) { |
| 3676 | set_fill_boolean(set, 1); |
| 3677 | found = 1; |
| 3678 | break; |
| 3679 | } |
| 3680 | } |
| 3681 | if (found) { |
| 3682 | break; |
| 3683 | } |
| 3684 | } |
| 3685 | } |
| 3686 | } |
| 3687 | |
| 3688 | return LY_SUCCESS; |
| 3689 | } |
| 3690 | |
| 3691 | /** |
| 3692 | * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending |
| 3693 | * on whether the first argument nodes contain a node of an identity that either is or is derived from |
| 3694 | * the second argument identity. |
| 3695 | * |
| 3696 | * @param[in] args Array of arguments. |
| 3697 | * @param[in] arg_count Count of elements in @p args. |
| 3698 | * @param[in,out] set Context and result set at the same time. |
| 3699 | * @param[in] options XPath options. |
| 3700 | * @return LY_ERR (LY_EINVAL for wrong arguments on schema) |
| 3701 | */ |
| 3702 | static LY_ERR |
| 3703 | xpath_derived_from_or_self(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3704 | { |
| 3705 | uint16_t i; |
| 3706 | struct lyd_node_term *leaf; |
| 3707 | struct lysc_node_leaf *sleaf; |
| 3708 | struct lyd_value data = {0}; |
| 3709 | struct ly_err_item *err = NULL; |
| 3710 | LY_ERR rc = LY_SUCCESS; |
| 3711 | int found; |
| 3712 | |
| 3713 | if (options & LYXP_SCNODE_ALL) { |
| 3714 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3715 | LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__); |
| 3716 | rc = LY_EINVAL; |
| 3717 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3718 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
| 3719 | rc = LY_EINVAL; |
| 3720 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) { |
| 3721 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", __func__, sleaf->name); |
| 3722 | rc = LY_EINVAL; |
| 3723 | } |
| 3724 | |
| 3725 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 3726 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3727 | LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
| 3728 | rc = LY_EINVAL; |
| 3729 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3730 | LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name); |
| 3731 | rc = LY_EINVAL; |
| 3732 | } |
| 3733 | } |
| 3734 | set_scnode_clear_ctx(set); |
| 3735 | return rc; |
| 3736 | } |
| 3737 | |
| 3738 | if ((args[0]->type != LYXP_SET_NODE_SET) && (args[0]->type != LYXP_SET_EMPTY)) { |
| 3739 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "derived-from-or-self(node-set, string)"); |
| 3740 | return LY_EVALID; |
| 3741 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3742 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3743 | LY_CHECK_RET(rc); |
| 3744 | |
| 3745 | set_fill_boolean(set, 0); |
| 3746 | if (args[0]->type != LYXP_SET_EMPTY) { |
| 3747 | found = 0; |
| 3748 | for (i = 0; i < args[0]->used; ++i) { |
| 3749 | leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node; |
| 3750 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 3751 | if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_IDENT)) { |
| 3752 | /* store args[1] into ident */ |
| 3753 | rc = sleaf->type->plugin->store(set->ctx, sleaf->type, args[1]->val.str, strlen(args[1]->val.str), |
| 3754 | LY_TYPE_OPTS_STORE, lys_resolve_prefix, (void *)sleaf->dflt_mod, set->format, |
| 3755 | (struct lyd_node *)leaf, set->trees, &data, NULL, &err); |
| 3756 | if (err) { |
| 3757 | ly_err_print(err); |
| 3758 | ly_err_free(err); |
| 3759 | } |
| 3760 | LY_CHECK_RET(rc); |
| 3761 | |
| 3762 | if (data.ident == leaf->value.ident) { |
| 3763 | set_fill_boolean(set, 1); |
| 3764 | break; |
| 3765 | } |
| 3766 | LY_ARRAY_FOR(data.ident->derived, i) { |
| 3767 | if (data.ident->derived[i] == leaf->value.ident) { |
| 3768 | set_fill_boolean(set, 1); |
| 3769 | found = 1; |
| 3770 | break; |
| 3771 | } |
| 3772 | } |
| 3773 | if (found) { |
| 3774 | break; |
| 3775 | } |
| 3776 | } |
| 3777 | } |
| 3778 | } |
| 3779 | |
| 3780 | return LY_SUCCESS; |
| 3781 | } |
| 3782 | |
| 3783 | /** |
| 3784 | * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER |
| 3785 | * with the integer value of the first node's enum value, otherwise NaN. |
| 3786 | * |
| 3787 | * @param[in] args Array of arguments. |
| 3788 | * @param[in] arg_count Count of elements in @p args. |
| 3789 | * @param[in,out] set Context and result set at the same time. |
| 3790 | * @param[in] options XPath options. |
| 3791 | * @return LY_ERR (LY_EINVAL for wrong arguments on schema) |
| 3792 | */ |
| 3793 | static LY_ERR |
| 3794 | xpath_enum_value(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3795 | { |
| 3796 | struct lyd_node_term *leaf; |
| 3797 | struct lysc_node_leaf *sleaf; |
| 3798 | LY_ERR rc = LY_SUCCESS; |
| 3799 | |
| 3800 | if (options & LYXP_SCNODE_ALL) { |
| 3801 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3802 | LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__); |
| 3803 | rc = LY_EINVAL; |
| 3804 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3805 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
| 3806 | rc = LY_EINVAL; |
| 3807 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) { |
| 3808 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"enumeration\".", __func__, sleaf->name); |
| 3809 | rc = LY_EINVAL; |
| 3810 | } |
| 3811 | set_scnode_clear_ctx(set); |
| 3812 | return rc; |
| 3813 | } |
| 3814 | |
| 3815 | if ((args[0]->type != LYXP_SET_NODE_SET) && (args[0]->type != LYXP_SET_EMPTY)) { |
| 3816 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "enum-value(node-set)"); |
| 3817 | return LY_EVALID; |
| 3818 | } |
| 3819 | |
| 3820 | set_fill_number(set, NAN); |
| 3821 | if (args[0]->type == LYXP_SET_NODE_SET) { |
| 3822 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
| 3823 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 3824 | if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) { |
| 3825 | set_fill_number(set, leaf->value.enum_item->value); |
| 3826 | } |
| 3827 | } |
| 3828 | |
| 3829 | return LY_SUCCESS; |
| 3830 | } |
| 3831 | |
| 3832 | /** |
| 3833 | * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN |
| 3834 | * with false value. |
| 3835 | * |
| 3836 | * @param[in] args Array of arguments. |
| 3837 | * @param[in] arg_count Count of elements in @p args. |
| 3838 | * @param[in,out] set Context and result set at the same time. |
| 3839 | * @param[in] options XPath options. |
| 3840 | * @return LY_ERR (LY_EINVAL for wrong arguments on schema) |
| 3841 | */ |
| 3842 | static LY_ERR |
| 3843 | xpath_false(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3844 | { |
| 3845 | if (options & LYXP_SCNODE_ALL) { |
| 3846 | set_scnode_clear_ctx(set); |
| 3847 | return LY_SUCCESS; |
| 3848 | } |
| 3849 | |
| 3850 | set_fill_boolean(set, 0); |
| 3851 | return LY_SUCCESS; |
| 3852 | } |
| 3853 | |
| 3854 | /** |
| 3855 | * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER |
| 3856 | * with the first argument floored (truncated). |
| 3857 | * |
| 3858 | * @param[in] args Array of arguments. |
| 3859 | * @param[in] arg_count Count of elements in @p args. |
| 3860 | * @param[in,out] set Context and result set at the same time. |
| 3861 | * @param[in] options XPath options. |
| 3862 | * @return LY_ERR (LY_EINVAL for wrong arguments on schema) |
| 3863 | */ |
| 3864 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3865 | 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] | 3866 | { |
| 3867 | LY_ERR rc; |
| 3868 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3869 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3870 | LY_CHECK_RET(rc); |
| 3871 | if (isfinite(args[0]->val.num)) { |
| 3872 | set_fill_number(set, (long long)args[0]->val.num); |
| 3873 | } |
| 3874 | |
| 3875 | return LY_SUCCESS; |
| 3876 | } |
| 3877 | |
| 3878 | /** |
| 3879 | * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN |
| 3880 | * whether the language of the text matches the one from the argument. |
| 3881 | * |
| 3882 | * @param[in] args Array of arguments. |
| 3883 | * @param[in] arg_count Count of elements in @p args. |
| 3884 | * @param[in,out] set Context and result set at the same time. |
| 3885 | * @param[in] options XPath options. |
| 3886 | * @return LY_ERR (LY_EINVAL for wrong arguments on schema) |
| 3887 | */ |
| 3888 | static LY_ERR |
| 3889 | xpath_lang(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3890 | { |
| 3891 | const struct lyd_node *node; |
| 3892 | struct lysc_node_leaf *sleaf; |
| 3893 | struct lyd_attr *attr = NULL; |
| 3894 | const char *val; |
| 3895 | int i, dynamic; |
| 3896 | LY_ERR rc = LY_SUCCESS; |
| 3897 | |
| 3898 | if (options & LYXP_SCNODE_ALL) { |
| 3899 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3900 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3901 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
| 3902 | rc = LY_EINVAL; |
| 3903 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3904 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name); |
| 3905 | rc = LY_EINVAL; |
| 3906 | } |
| 3907 | } |
| 3908 | set_scnode_clear_ctx(set); |
| 3909 | return rc; |
| 3910 | } |
| 3911 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3912 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3913 | LY_CHECK_RET(rc); |
| 3914 | |
| 3915 | if (set->type == LYXP_SET_EMPTY) { |
| 3916 | set_fill_boolean(set, 0); |
| 3917 | return LY_SUCCESS; |
| 3918 | } |
| 3919 | if (set->type != LYXP_SET_NODE_SET) { |
| 3920 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "lang(string)"); |
| 3921 | return LY_EVALID; |
| 3922 | } |
| 3923 | |
| 3924 | switch (set->val.nodes[0].type) { |
| 3925 | case LYXP_NODE_ELEM: |
| 3926 | case LYXP_NODE_TEXT: |
| 3927 | node = set->val.nodes[0].node; |
| 3928 | break; |
| 3929 | case LYXP_NODE_ATTR: |
| 3930 | node = set->val.attrs[0].attr->parent; |
| 3931 | break; |
| 3932 | default: |
| 3933 | /* nothing to do with roots */ |
| 3934 | set_fill_boolean(set, 0); |
| 3935 | return LY_SUCCESS; |
| 3936 | } |
| 3937 | |
| 3938 | /* find lang attribute */ |
| 3939 | for (; node; node = (struct lyd_node *)node->parent) { |
| 3940 | for (attr = node->attr; attr; attr = attr->next) { |
| 3941 | /* annotations */ |
| 3942 | if (attr->name && !strcmp(attr->name, "lang") && !strcmp(attr->annotation->module->name, "xml")) { |
| 3943 | break; |
| 3944 | } |
| 3945 | } |
| 3946 | |
| 3947 | if (attr) { |
| 3948 | break; |
| 3949 | } |
| 3950 | } |
| 3951 | |
| 3952 | /* compare languages */ |
| 3953 | if (!attr) { |
| 3954 | set_fill_boolean(set, 0); |
| 3955 | } else { |
| 3956 | val = lyd_attr2str(attr, &dynamic); |
| 3957 | for (i = 0; args[0]->val.str[i]; ++i) { |
| 3958 | if (tolower(args[0]->val.str[i]) != tolower(val[i])) { |
| 3959 | set_fill_boolean(set, 0); |
| 3960 | break; |
| 3961 | } |
| 3962 | } |
| 3963 | if (!args[0]->val.str[i]) { |
| 3964 | if (!val[i] || (val[i] == '-')) { |
| 3965 | set_fill_boolean(set, 1); |
| 3966 | } else { |
| 3967 | set_fill_boolean(set, 0); |
| 3968 | } |
| 3969 | } |
| 3970 | if (dynamic) { |
| 3971 | free((char *)val); |
| 3972 | } |
| 3973 | } |
| 3974 | |
| 3975 | return LY_SUCCESS; |
| 3976 | } |
| 3977 | |
| 3978 | /** |
| 3979 | * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER |
| 3980 | * with the context size. |
| 3981 | * |
| 3982 | * @param[in] args Array of arguments. |
| 3983 | * @param[in] arg_count Count of elements in @p args. |
| 3984 | * @param[in,out] set Context and result set at the same time. |
| 3985 | * @param[in] options XPath options. |
| 3986 | * @return LY_ERR (LY_EINVAL for wrong arguments on schema) |
| 3987 | */ |
| 3988 | static LY_ERR |
| 3989 | xpath_last(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3990 | { |
| 3991 | if (options & LYXP_SCNODE_ALL) { |
| 3992 | set_scnode_clear_ctx(set); |
| 3993 | return LY_SUCCESS; |
| 3994 | } |
| 3995 | |
| 3996 | if (set->type == LYXP_SET_EMPTY) { |
| 3997 | set_fill_number(set, 0); |
| 3998 | return LY_SUCCESS; |
| 3999 | } |
| 4000 | if (set->type != LYXP_SET_NODE_SET) { |
| 4001 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "last()"); |
| 4002 | return LY_EVALID; |
| 4003 | } |
| 4004 | |
| 4005 | set_fill_number(set, set->ctx_size); |
| 4006 | return LY_SUCCESS; |
| 4007 | } |
| 4008 | |
| 4009 | /** |
| 4010 | * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING |
| 4011 | * with the node name without namespace from the argument or the context. |
| 4012 | * |
| 4013 | * @param[in] args Array of arguments. |
| 4014 | * @param[in] arg_count Count of elements in @p args. |
| 4015 | * @param[in,out] set Context and result set at the same time. |
| 4016 | * @param[in] options XPath options. |
| 4017 | * @return LY_ERR (LY_EINVAL for wrong arguments on schema) |
| 4018 | */ |
| 4019 | static LY_ERR |
| 4020 | xpath_local_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options) |
| 4021 | { |
| 4022 | struct lyxp_set_node *item; |
| 4023 | /* suppress unused variable warning */ |
| 4024 | (void)options; |
| 4025 | |
| 4026 | if (options & LYXP_SCNODE_ALL) { |
| 4027 | set_scnode_clear_ctx(set); |
| 4028 | return LY_SUCCESS; |
| 4029 | } |
| 4030 | |
| 4031 | if (arg_count) { |
| 4032 | if (args[0]->type == LYXP_SET_EMPTY) { |
| 4033 | set_fill_string(set, "", 0); |
| 4034 | return LY_SUCCESS; |
| 4035 | } |
| 4036 | if (args[0]->type != LYXP_SET_NODE_SET) { |
| 4037 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "local-name(node-set?)"); |
| 4038 | return LY_EVALID; |
| 4039 | } |
| 4040 | |
| 4041 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4042 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4043 | |
| 4044 | item = &args[0]->val.nodes[0]; |
| 4045 | } else { |
| 4046 | if (set->type == LYXP_SET_EMPTY) { |
| 4047 | set_fill_string(set, "", 0); |
| 4048 | return LY_SUCCESS; |
| 4049 | } |
| 4050 | if (set->type != LYXP_SET_NODE_SET) { |
| 4051 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "local-name(node-set?)"); |
| 4052 | return LY_EVALID; |
| 4053 | } |
| 4054 | |
| 4055 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4056 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4057 | |
| 4058 | item = &set->val.nodes[0]; |
| 4059 | } |
| 4060 | |
| 4061 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4062 | case LYXP_NODE_NONE: |
| 4063 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4064 | case LYXP_NODE_ROOT: |
| 4065 | case LYXP_NODE_ROOT_CONFIG: |
| 4066 | case LYXP_NODE_TEXT: |
| 4067 | set_fill_string(set, "", 0); |
| 4068 | break; |
| 4069 | case LYXP_NODE_ELEM: |
| 4070 | set_fill_string(set, item->node->schema->name, strlen(item->node->schema->name)); |
| 4071 | break; |
| 4072 | case LYXP_NODE_ATTR: |
| 4073 | set_fill_string(set, ((struct lyd_attr *)item->node)->name, strlen(((struct lyd_attr *)item->node)->name)); |
| 4074 | break; |
| 4075 | } |
| 4076 | |
| 4077 | return LY_SUCCESS; |
| 4078 | } |
| 4079 | |
| 4080 | /** |
| 4081 | * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING |
| 4082 | * with the node name fully qualified (with namespace) from the argument or the context. |
| 4083 | * |
| 4084 | * @param[in] args Array of arguments. |
| 4085 | * @param[in] arg_count Count of elements in @p args. |
| 4086 | * @param[in,out] set Context and result set at the same time. |
| 4087 | * @param[in] options XPath options. |
| 4088 | * @return LY_ERR (LY_EINVAL for wrong arguments on schema) |
| 4089 | */ |
| 4090 | static LY_ERR |
| 4091 | xpath_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options) |
| 4092 | { |
| 4093 | struct lyxp_set_node *item; |
| 4094 | struct lys_module *mod; |
| 4095 | char *str; |
| 4096 | const char *name; |
| 4097 | int rc; |
| 4098 | |
| 4099 | if (options & LYXP_SCNODE_ALL) { |
| 4100 | set_scnode_clear_ctx(set); |
| 4101 | return LY_SUCCESS; |
| 4102 | } |
| 4103 | |
| 4104 | if (arg_count) { |
| 4105 | if (args[0]->type == LYXP_SET_EMPTY) { |
| 4106 | set_fill_string(set, "", 0); |
| 4107 | return LY_SUCCESS; |
| 4108 | } |
| 4109 | if (args[0]->type != LYXP_SET_NODE_SET) { |
| 4110 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "name(node-set?)"); |
| 4111 | return LY_EVALID; |
| 4112 | } |
| 4113 | |
| 4114 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4115 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4116 | |
| 4117 | item = &args[0]->val.nodes[0]; |
| 4118 | } else { |
| 4119 | if (set->type == LYXP_SET_EMPTY) { |
| 4120 | set_fill_string(set, "", 0); |
| 4121 | return LY_SUCCESS; |
| 4122 | } |
| 4123 | if (set->type != LYXP_SET_NODE_SET) { |
| 4124 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "name(node-set?)"); |
| 4125 | return LY_EVALID; |
| 4126 | } |
| 4127 | |
| 4128 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4129 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4130 | |
| 4131 | item = &set->val.nodes[0]; |
| 4132 | } |
| 4133 | |
| 4134 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4135 | case LYXP_NODE_NONE: |
| 4136 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4137 | case LYXP_NODE_ROOT: |
| 4138 | case LYXP_NODE_ROOT_CONFIG: |
| 4139 | case LYXP_NODE_TEXT: |
| 4140 | mod = NULL; |
| 4141 | name = NULL; |
| 4142 | break; |
| 4143 | case LYXP_NODE_ELEM: |
| 4144 | mod = item->node->schema->module; |
| 4145 | name = item->node->schema->name; |
| 4146 | break; |
| 4147 | case LYXP_NODE_ATTR: |
| 4148 | mod = ((struct lyd_attr *)item->node)->annotation->module; |
| 4149 | name = ((struct lyd_attr *)item->node)->name; |
| 4150 | break; |
| 4151 | } |
| 4152 | |
| 4153 | if (mod && name) { |
| 4154 | switch (set->format) { |
| 4155 | case LYD_UNKNOWN: |
| 4156 | rc = asprintf(&str, "%s:%s", lys_prefix_find_module(set->local_mod, mod), name); |
| 4157 | break; |
| 4158 | case LYD_JSON: |
| 4159 | rc = asprintf(&str, "%s:%s", mod->name, name); |
| 4160 | break; |
Michal Vasko | 9409ef6 | 2019-09-12 11:47:17 +0200 | [diff] [blame] | 4161 | default: |
| 4162 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4163 | } |
| 4164 | LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM); |
| 4165 | set_fill_string(set, str, strlen(str)); |
| 4166 | free(str); |
| 4167 | } else { |
| 4168 | set_fill_string(set, "", 0); |
| 4169 | } |
| 4170 | |
| 4171 | return LY_SUCCESS; |
| 4172 | } |
| 4173 | |
| 4174 | /** |
| 4175 | * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING |
| 4176 | * with the namespace of the node from the argument or the context. |
| 4177 | * |
| 4178 | * @param[in] args Array of arguments. |
| 4179 | * @param[in] arg_count Count of elements in @p args. |
| 4180 | * @param[in,out] set Context and result set at the same time. |
| 4181 | * @param[in] options XPath options. |
| 4182 | * @return LY_ERR (LY_EINVAL for wrong arguments on schema) |
| 4183 | */ |
| 4184 | static LY_ERR |
| 4185 | xpath_namespace_uri(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options) |
| 4186 | { |
| 4187 | struct lyxp_set_node *item; |
| 4188 | struct lys_module *mod; |
| 4189 | /* suppress unused variable warning */ |
| 4190 | (void)options; |
| 4191 | |
| 4192 | if (options & LYXP_SCNODE_ALL) { |
| 4193 | set_scnode_clear_ctx(set); |
| 4194 | return LY_SUCCESS; |
| 4195 | } |
| 4196 | |
| 4197 | if (arg_count) { |
| 4198 | if (args[0]->type == LYXP_SET_EMPTY) { |
| 4199 | set_fill_string(set, "", 0); |
| 4200 | return LY_SUCCESS; |
| 4201 | } |
| 4202 | if (args[0]->type != LYXP_SET_NODE_SET) { |
| 4203 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "namespace-uri(node-set?)"); |
| 4204 | return LY_EVALID; |
| 4205 | } |
| 4206 | |
| 4207 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4208 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4209 | |
| 4210 | item = &args[0]->val.nodes[0]; |
| 4211 | } else { |
| 4212 | if (set->type == LYXP_SET_EMPTY) { |
| 4213 | set_fill_string(set, "", 0); |
| 4214 | return LY_SUCCESS; |
| 4215 | } |
| 4216 | if (set->type != LYXP_SET_NODE_SET) { |
| 4217 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "namespace-uri(node-set?)"); |
| 4218 | return LY_EVALID; |
| 4219 | } |
| 4220 | |
| 4221 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4222 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4223 | |
| 4224 | item = &set->val.nodes[0]; |
| 4225 | } |
| 4226 | |
| 4227 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4228 | case LYXP_NODE_NONE: |
| 4229 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4230 | case LYXP_NODE_ROOT: |
| 4231 | case LYXP_NODE_ROOT_CONFIG: |
| 4232 | case LYXP_NODE_TEXT: |
| 4233 | set_fill_string(set, "", 0); |
| 4234 | break; |
| 4235 | case LYXP_NODE_ELEM: |
| 4236 | case LYXP_NODE_ATTR: |
| 4237 | if (item->type == LYXP_NODE_ELEM) { |
| 4238 | mod = item->node->schema->module; |
| 4239 | } else { /* LYXP_NODE_ATTR */ |
| 4240 | /* annotations */ |
| 4241 | mod = ((struct lyd_attr *)item->node)->annotation->module; |
| 4242 | } |
| 4243 | |
| 4244 | set_fill_string(set, mod->ns, strlen(mod->ns)); |
| 4245 | break; |
| 4246 | } |
| 4247 | |
| 4248 | return LY_SUCCESS; |
| 4249 | } |
| 4250 | |
| 4251 | /** |
| 4252 | * @brief Execute the XPath node() function (node type). Returns LYXP_SET_NODE_SET |
| 4253 | * with only nodes from the context. In practice it either leaves the context |
| 4254 | * as it is or returns an empty node set. |
| 4255 | * |
| 4256 | * @param[in] args Array of arguments. |
| 4257 | * @param[in] arg_count Count of elements in @p args. |
| 4258 | * @param[in,out] set Context and result set at the same time. |
| 4259 | * @param[in] options XPath options. |
| 4260 | * @return LY_ERR (LY_EINVAL for wrong arguments on schema) |
| 4261 | */ |
| 4262 | static LY_ERR |
| 4263 | xpath_node(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 4264 | { |
| 4265 | if (options & LYXP_SCNODE_ALL) { |
| 4266 | set_scnode_clear_ctx(set); |
| 4267 | return LY_SUCCESS; |
| 4268 | } |
| 4269 | |
| 4270 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4271 | lyxp_set_cast(set, LYXP_SET_EMPTY); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4272 | } |
| 4273 | return LY_SUCCESS; |
| 4274 | } |
| 4275 | |
| 4276 | /** |
| 4277 | * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING |
| 4278 | * with normalized value (no leading, trailing, double white spaces) of the node |
| 4279 | * from the argument or the context. |
| 4280 | * |
| 4281 | * @param[in] args Array of arguments. |
| 4282 | * @param[in] arg_count Count of elements in @p args. |
| 4283 | * @param[in,out] set Context and result set at the same time. |
| 4284 | * @param[in] options XPath options. |
| 4285 | * @return LY_ERR (LY_EINVAL for wrong arguments on schema) |
| 4286 | */ |
| 4287 | static LY_ERR |
| 4288 | xpath_normalize_space(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options) |
| 4289 | { |
| 4290 | uint16_t i, new_used; |
| 4291 | char *new; |
| 4292 | int have_spaces = 0, space_before = 0; |
| 4293 | struct lysc_node_leaf *sleaf; |
| 4294 | LY_ERR rc = LY_SUCCESS; |
| 4295 | |
| 4296 | if (options & LYXP_SCNODE_ALL) { |
| 4297 | if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4298 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4299 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
| 4300 | rc = LY_EINVAL; |
| 4301 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4302 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name); |
| 4303 | rc = LY_EINVAL; |
| 4304 | } |
| 4305 | } |
| 4306 | set_scnode_clear_ctx(set); |
| 4307 | return rc; |
| 4308 | } |
| 4309 | |
| 4310 | if (arg_count) { |
| 4311 | set_fill_set(set, args[0]); |
| 4312 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4313 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4314 | LY_CHECK_RET(rc); |
| 4315 | |
| 4316 | /* is there any normalization necessary? */ |
| 4317 | for (i = 0; set->val.str[i]; ++i) { |
| 4318 | if (is_xmlws(set->val.str[i])) { |
| 4319 | if ((i == 0) || space_before || (!set->val.str[i + 1])) { |
| 4320 | have_spaces = 1; |
| 4321 | break; |
| 4322 | } |
| 4323 | space_before = 1; |
| 4324 | } else { |
| 4325 | space_before = 0; |
| 4326 | } |
| 4327 | } |
| 4328 | |
| 4329 | /* yep, there is */ |
| 4330 | if (have_spaces) { |
| 4331 | /* it's enough, at least one character will go, makes space for ending '\0' */ |
| 4332 | new = malloc(strlen(set->val.str) * sizeof(char)); |
| 4333 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 4334 | new_used = 0; |
| 4335 | |
| 4336 | space_before = 0; |
| 4337 | for (i = 0; set->val.str[i]; ++i) { |
| 4338 | if (is_xmlws(set->val.str[i])) { |
| 4339 | if ((i == 0) || space_before) { |
| 4340 | space_before = 1; |
| 4341 | continue; |
| 4342 | } else { |
| 4343 | space_before = 1; |
| 4344 | } |
| 4345 | } else { |
| 4346 | space_before = 0; |
| 4347 | } |
| 4348 | |
| 4349 | new[new_used] = (space_before ? ' ' : set->val.str[i]); |
| 4350 | ++new_used; |
| 4351 | } |
| 4352 | |
| 4353 | /* at worst there is one trailing space now */ |
| 4354 | if (new_used && is_xmlws(new[new_used - 1])) { |
| 4355 | --new_used; |
| 4356 | } |
| 4357 | |
| 4358 | new = ly_realloc(new, (new_used + 1) * sizeof(char)); |
| 4359 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 4360 | new[new_used] = '\0'; |
| 4361 | |
| 4362 | free(set->val.str); |
| 4363 | set->val.str = new; |
| 4364 | } |
| 4365 | |
| 4366 | return LY_SUCCESS; |
| 4367 | } |
| 4368 | |
| 4369 | /** |
| 4370 | * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN |
| 4371 | * with the argument converted to boolean and logically inverted. |
| 4372 | * |
| 4373 | * @param[in] args Array of arguments. |
| 4374 | * @param[in] arg_count Count of elements in @p args. |
| 4375 | * @param[in,out] set Context and result set at the same time. |
| 4376 | * @param[in] options XPath options. |
| 4377 | * @return LY_ERR (LY_EINVAL for wrong arguments on schema) |
| 4378 | */ |
| 4379 | static LY_ERR |
| 4380 | xpath_not(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 4381 | { |
| 4382 | if (options & LYXP_SCNODE_ALL) { |
| 4383 | set_scnode_clear_ctx(set); |
| 4384 | return LY_SUCCESS; |
| 4385 | } |
| 4386 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4387 | lyxp_set_cast(args[0], LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4388 | if (args[0]->val.bool) { |
| 4389 | set_fill_boolean(set, 0); |
| 4390 | } else { |
| 4391 | set_fill_boolean(set, 1); |
| 4392 | } |
| 4393 | |
| 4394 | return LY_SUCCESS; |
| 4395 | } |
| 4396 | |
| 4397 | /** |
| 4398 | * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER |
| 4399 | * with the number representation of either the argument or the context. |
| 4400 | * |
| 4401 | * @param[in] args Array of arguments. |
| 4402 | * @param[in] arg_count Count of elements in @p args. |
| 4403 | * @param[in,out] set Context and result set at the same time. |
| 4404 | * @param[in] options XPath options. |
| 4405 | * @return LY_ERR (LY_EINVAL for wrong arguments on schema) |
| 4406 | */ |
| 4407 | static LY_ERR |
| 4408 | xpath_number(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options) |
| 4409 | { |
| 4410 | LY_ERR rc; |
| 4411 | |
| 4412 | if (options & LYXP_SCNODE_ALL) { |
| 4413 | set_scnode_clear_ctx(set); |
| 4414 | return LY_SUCCESS; |
| 4415 | } |
| 4416 | |
| 4417 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4418 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4419 | LY_CHECK_RET(rc); |
| 4420 | set_fill_set(set, args[0]); |
| 4421 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4422 | rc = lyxp_set_cast(set, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4423 | LY_CHECK_RET(rc); |
| 4424 | } |
| 4425 | |
| 4426 | return LY_SUCCESS; |
| 4427 | } |
| 4428 | |
| 4429 | /** |
| 4430 | * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER |
| 4431 | * with the context position. |
| 4432 | * |
| 4433 | * @param[in] args Array of arguments. |
| 4434 | * @param[in] arg_count Count of elements in @p args. |
| 4435 | * @param[in,out] set Context and result set at the same time. |
| 4436 | * @param[in] options XPath options. |
| 4437 | * @return LY_ERR (LY_EINVAL for wrong arguments on schema) |
| 4438 | */ |
| 4439 | static LY_ERR |
| 4440 | xpath_position(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 4441 | { |
| 4442 | if (options & LYXP_SCNODE_ALL) { |
| 4443 | set_scnode_clear_ctx(set); |
| 4444 | return LY_SUCCESS; |
| 4445 | } |
| 4446 | |
| 4447 | if (set->type == LYXP_SET_EMPTY) { |
| 4448 | set_fill_number(set, 0); |
| 4449 | return LY_SUCCESS; |
| 4450 | } |
| 4451 | if (set->type != LYXP_SET_NODE_SET) { |
| 4452 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "position()"); |
| 4453 | return LY_EVALID; |
| 4454 | } |
| 4455 | |
| 4456 | set_fill_number(set, set->ctx_pos); |
| 4457 | |
| 4458 | /* UNUSED in 'Release' build type */ |
| 4459 | (void)options; |
| 4460 | return LY_SUCCESS; |
| 4461 | } |
| 4462 | |
| 4463 | /** |
| 4464 | * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN |
| 4465 | * depending on whether the second argument regex matches the first argument string. For details refer to |
| 4466 | * YANG 1.1 RFC section 10.2.1. |
| 4467 | * |
| 4468 | * @param[in] args Array of arguments. |
| 4469 | * @param[in] arg_count Count of elements in @p args. |
| 4470 | * @param[in,out] set Context and result set at the same time. |
| 4471 | * @param[in] options XPath options. |
| 4472 | * @return LY_ERR (LY_EINVAL for wrong arguments on schema) |
| 4473 | */ |
| 4474 | static LY_ERR |
| 4475 | xpath_re_match(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 4476 | { |
| 4477 | struct lysc_pattern **patterns = NULL, **pattern; |
| 4478 | struct lysc_node_leaf *sleaf; |
| 4479 | char *path; |
| 4480 | LY_ERR rc = LY_SUCCESS; |
| 4481 | struct ly_err_item *err; |
| 4482 | |
| 4483 | if (options & LYXP_SCNODE_ALL) { |
| 4484 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4485 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4486 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
| 4487 | rc = LY_EINVAL; |
| 4488 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4489 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name); |
| 4490 | rc = LY_EINVAL; |
| 4491 | } |
| 4492 | } |
| 4493 | |
| 4494 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4495 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4496 | LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
| 4497 | rc = LY_EINVAL; |
| 4498 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4499 | LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name); |
| 4500 | rc = LY_EINVAL; |
| 4501 | } |
| 4502 | } |
| 4503 | set_scnode_clear_ctx(set); |
| 4504 | return rc; |
| 4505 | } |
| 4506 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4507 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4508 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4509 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4510 | LY_CHECK_RET(rc); |
| 4511 | |
| 4512 | LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM); |
| 4513 | *pattern = malloc(sizeof **pattern); |
| 4514 | path = lyd_path(set->ctx_node, LYD_PATH_LOG, NULL, 0); |
| 4515 | rc = lys_compile_type_pattern_check(set->ctx, path, args[1]->val.str, &(*pattern)->code); |
| 4516 | free(path); |
| 4517 | if (rc != LY_SUCCESS) { |
| 4518 | LY_ARRAY_FREE(patterns); |
| 4519 | return rc; |
| 4520 | } |
| 4521 | |
| 4522 | rc = ly_type_validate_patterns(patterns, args[0]->val.str, strlen(args[0]->val.str), &err); |
| 4523 | pcre2_code_free((*pattern)->code); |
| 4524 | free(*pattern); |
| 4525 | LY_ARRAY_FREE(patterns); |
| 4526 | if (rc && (rc != LY_EVALID)) { |
| 4527 | ly_err_print(err); |
| 4528 | ly_err_free(err); |
| 4529 | return rc; |
| 4530 | } |
| 4531 | |
| 4532 | if (rc == LY_EVALID) { |
| 4533 | ly_err_free(err); |
| 4534 | set_fill_boolean(set, 0); |
| 4535 | } else { |
| 4536 | set_fill_boolean(set, 1); |
| 4537 | } |
| 4538 | |
| 4539 | return LY_SUCCESS; |
| 4540 | } |
| 4541 | |
| 4542 | /** |
| 4543 | * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER |
| 4544 | * with the rounded first argument. For details refer to |
| 4545 | * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round. |
| 4546 | * |
| 4547 | * @param[in] args Array of arguments. |
| 4548 | * @param[in] arg_count Count of elements in @p args. |
| 4549 | * @param[in,out] set Context and result set at the same time. |
| 4550 | * @param[in] options XPath options. |
| 4551 | * @return LY_ERR (LY_EINVAL for wrong arguments on schema) |
| 4552 | */ |
| 4553 | static LY_ERR |
| 4554 | xpath_round(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 4555 | { |
| 4556 | struct lysc_node_leaf *sleaf; |
| 4557 | LY_ERR rc = LY_SUCCESS; |
| 4558 | |
| 4559 | if (options & LYXP_SCNODE_ALL) { |
| 4560 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4561 | LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__); |
| 4562 | rc = LY_EINVAL; |
| 4563 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4564 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
| 4565 | rc = LY_EINVAL; |
| 4566 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) { |
| 4567 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name); |
| 4568 | rc = LY_EINVAL; |
| 4569 | } |
| 4570 | set_scnode_clear_ctx(set); |
| 4571 | return rc; |
| 4572 | } |
| 4573 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4574 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4575 | LY_CHECK_RET(rc); |
| 4576 | |
| 4577 | /* cover only the cases where floor can't be used */ |
| 4578 | if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) { |
| 4579 | set_fill_number(set, -0.0f); |
| 4580 | } else { |
| 4581 | args[0]->val.num += 0.5; |
| 4582 | rc = xpath_floor(args, 1, args[0], options); |
| 4583 | LY_CHECK_RET(rc); |
| 4584 | set_fill_number(set, args[0]->val.num); |
| 4585 | } |
| 4586 | |
| 4587 | return LY_SUCCESS; |
| 4588 | } |
| 4589 | |
| 4590 | /** |
| 4591 | * @brief Execute the XPath starts-with(string, string) function. |
| 4592 | * Returns LYXP_SET_BOOLEAN whether the second argument is |
| 4593 | * the prefix of the first or not. |
| 4594 | * |
| 4595 | * @param[in] args Array of arguments. |
| 4596 | * @param[in] arg_count Count of elements in @p args. |
| 4597 | * @param[in,out] set Context and result set at the same time. |
| 4598 | * @param[in] options XPath options. |
| 4599 | * @return LY_ERR (LY_EINVAL for wrong arguments on schema) |
| 4600 | */ |
| 4601 | static LY_ERR |
| 4602 | xpath_starts_with(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 4603 | { |
| 4604 | struct lysc_node_leaf *sleaf; |
| 4605 | LY_ERR rc = LY_SUCCESS; |
| 4606 | |
| 4607 | if (options & LYXP_SCNODE_ALL) { |
| 4608 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4609 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4610 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
| 4611 | rc = LY_EINVAL; |
| 4612 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4613 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name); |
| 4614 | rc = LY_EINVAL; |
| 4615 | } |
| 4616 | } |
| 4617 | |
| 4618 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4619 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4620 | LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
| 4621 | rc = LY_EINVAL; |
| 4622 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4623 | LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name); |
| 4624 | rc = LY_EINVAL; |
| 4625 | } |
| 4626 | } |
| 4627 | set_scnode_clear_ctx(set); |
| 4628 | return rc; |
| 4629 | } |
| 4630 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4631 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4632 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4633 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4634 | LY_CHECK_RET(rc); |
| 4635 | |
| 4636 | if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) { |
| 4637 | set_fill_boolean(set, 0); |
| 4638 | } else { |
| 4639 | set_fill_boolean(set, 1); |
| 4640 | } |
| 4641 | |
| 4642 | return LY_SUCCESS; |
| 4643 | } |
| 4644 | |
| 4645 | /** |
| 4646 | * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING |
| 4647 | * with the string representation of either the argument or the context. |
| 4648 | * |
| 4649 | * @param[in] args Array of arguments. |
| 4650 | * @param[in] arg_count Count of elements in @p args. |
| 4651 | * @param[in,out] set Context and result set at the same time. |
| 4652 | * @param[in] options XPath options. |
| 4653 | * @return LY_ERR (LY_EINVAL for wrong arguments on schema) |
| 4654 | */ |
| 4655 | static LY_ERR |
| 4656 | xpath_string(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options) |
| 4657 | { |
| 4658 | LY_ERR rc; |
| 4659 | |
| 4660 | if (options & LYXP_SCNODE_ALL) { |
| 4661 | set_scnode_clear_ctx(set); |
| 4662 | return LY_SUCCESS; |
| 4663 | } |
| 4664 | |
| 4665 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4666 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4667 | LY_CHECK_RET(rc); |
| 4668 | set_fill_set(set, args[0]); |
| 4669 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4670 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4671 | LY_CHECK_RET(rc); |
| 4672 | } |
| 4673 | |
| 4674 | return LY_SUCCESS; |
| 4675 | } |
| 4676 | |
| 4677 | /** |
| 4678 | * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER |
| 4679 | * with the length of the string in either the argument or the context. |
| 4680 | * |
| 4681 | * @param[in] args Array of arguments. |
| 4682 | * @param[in] arg_count Count of elements in @p args. |
| 4683 | * @param[in,out] set Context and result set at the same time. |
| 4684 | * @param[in] options XPath options. |
| 4685 | * @return LY_ERR (LY_EINVAL for wrong arguments on schema) |
| 4686 | */ |
| 4687 | static LY_ERR |
| 4688 | xpath_string_length(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options) |
| 4689 | { |
| 4690 | struct lysc_node_leaf *sleaf; |
| 4691 | LY_ERR rc = LY_SUCCESS; |
| 4692 | |
| 4693 | if (options & LYXP_SCNODE_ALL) { |
| 4694 | if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4695 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4696 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
| 4697 | rc = LY_EINVAL; |
| 4698 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4699 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name); |
| 4700 | rc = LY_EINVAL; |
| 4701 | } |
| 4702 | } |
| 4703 | if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) { |
| 4704 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4705 | LOGWRN(set->ctx, "Argument #0 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
| 4706 | rc = LY_EINVAL; |
| 4707 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4708 | LOGWRN(set->ctx, "Argument #0 of %s is node \"%s\", not of string-type.", __func__, sleaf->name); |
| 4709 | rc = LY_EINVAL; |
| 4710 | } |
| 4711 | } |
| 4712 | set_scnode_clear_ctx(set); |
| 4713 | return rc; |
| 4714 | } |
| 4715 | |
| 4716 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4717 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4718 | LY_CHECK_RET(rc); |
| 4719 | set_fill_number(set, strlen(args[0]->val.str)); |
| 4720 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4721 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4722 | LY_CHECK_RET(rc); |
| 4723 | set_fill_number(set, strlen(set->val.str)); |
| 4724 | } |
| 4725 | |
| 4726 | return LY_SUCCESS; |
| 4727 | } |
| 4728 | |
| 4729 | /** |
| 4730 | * @brief Execute the XPath substring(string, number, number?) function. |
| 4731 | * Returns LYXP_SET_STRING substring of the first argument starting |
| 4732 | * on the second argument index ending on the third argument index, |
| 4733 | * indexed from 1. For exact definition refer to |
| 4734 | * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring. |
| 4735 | * |
| 4736 | * @param[in] args Array of arguments. |
| 4737 | * @param[in] arg_count Count of elements in @p args. |
| 4738 | * @param[in,out] set Context and result set at the same time. |
| 4739 | * @param[in] options XPath options. |
| 4740 | * @return LY_ERR (LY_EINVAL for wrong arguments on schema) |
| 4741 | */ |
| 4742 | static LY_ERR |
| 4743 | xpath_substring(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options) |
| 4744 | { |
| 4745 | int start, len; |
| 4746 | uint16_t str_start, str_len, pos; |
| 4747 | struct lysc_node_leaf *sleaf; |
| 4748 | LY_ERR rc = LY_SUCCESS; |
| 4749 | |
| 4750 | if (options & LYXP_SCNODE_ALL) { |
| 4751 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4752 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4753 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
| 4754 | rc = LY_EINVAL; |
| 4755 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4756 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name); |
| 4757 | rc = LY_EINVAL; |
| 4758 | } |
| 4759 | } |
| 4760 | |
| 4761 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4762 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4763 | LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
| 4764 | rc = LY_EINVAL; |
| 4765 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 4766 | LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name); |
| 4767 | rc = LY_EINVAL; |
| 4768 | } |
| 4769 | } |
| 4770 | |
| 4771 | if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET) |
| 4772 | && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) { |
| 4773 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4774 | LOGWRN(set->ctx, "Argument #3 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
| 4775 | rc = LY_EINVAL; |
| 4776 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 4777 | LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name); |
| 4778 | rc = LY_EINVAL; |
| 4779 | } |
| 4780 | } |
| 4781 | set_scnode_clear_ctx(set); |
| 4782 | return rc; |
| 4783 | } |
| 4784 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4785 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4786 | LY_CHECK_RET(rc); |
| 4787 | |
| 4788 | /* start */ |
| 4789 | if (xpath_round(&args[1], 1, args[1], options)) { |
| 4790 | return -1; |
| 4791 | } |
| 4792 | if (isfinite(args[1]->val.num)) { |
| 4793 | start = args[1]->val.num - 1; |
| 4794 | } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) { |
| 4795 | start = INT_MIN; |
| 4796 | } else { |
| 4797 | start = INT_MAX; |
| 4798 | } |
| 4799 | |
| 4800 | /* len */ |
| 4801 | if (arg_count == 3) { |
| 4802 | rc = xpath_round(&args[2], 1, args[2], options); |
| 4803 | LY_CHECK_RET(rc); |
| 4804 | if (isfinite(args[2]->val.num)) { |
| 4805 | len = args[2]->val.num; |
| 4806 | } else if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) { |
| 4807 | len = 0; |
| 4808 | } else { |
| 4809 | len = INT_MAX; |
| 4810 | } |
| 4811 | } else { |
| 4812 | len = INT_MAX; |
| 4813 | } |
| 4814 | |
| 4815 | /* find matching character positions */ |
| 4816 | str_start = 0; |
| 4817 | str_len = 0; |
| 4818 | for (pos = 0; args[0]->val.str[pos]; ++pos) { |
| 4819 | if (pos < start) { |
| 4820 | ++str_start; |
| 4821 | } else if (pos < start + len) { |
| 4822 | ++str_len; |
| 4823 | } else { |
| 4824 | break; |
| 4825 | } |
| 4826 | } |
| 4827 | |
| 4828 | set_fill_string(set, args[0]->val.str + str_start, str_len); |
| 4829 | return LY_SUCCESS; |
| 4830 | } |
| 4831 | |
| 4832 | /** |
| 4833 | * @brief Execute the XPath substring-after(string, string) function. |
| 4834 | * Returns LYXP_SET_STRING with the string succeeding the occurance |
| 4835 | * of the second argument in the first or an empty string. |
| 4836 | * |
| 4837 | * @param[in] args Array of arguments. |
| 4838 | * @param[in] arg_count Count of elements in @p args. |
| 4839 | * @param[in,out] set Context and result set at the same time. |
| 4840 | * @param[in] options XPath options. |
| 4841 | * @return LY_ERR (LY_EINVAL for wrong arguments on schema) |
| 4842 | */ |
| 4843 | static LY_ERR |
| 4844 | xpath_substring_after(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 4845 | { |
| 4846 | char *ptr; |
| 4847 | struct lysc_node_leaf *sleaf; |
| 4848 | LY_ERR rc = LY_SUCCESS; |
| 4849 | |
| 4850 | if (options & LYXP_SCNODE_ALL) { |
| 4851 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4852 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4853 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
| 4854 | rc = LY_EINVAL; |
| 4855 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4856 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name); |
| 4857 | rc = LY_EINVAL; |
| 4858 | } |
| 4859 | } |
| 4860 | |
| 4861 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4862 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4863 | LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
| 4864 | rc = LY_EINVAL; |
| 4865 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4866 | LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name); |
| 4867 | rc = LY_EINVAL; |
| 4868 | } |
| 4869 | } |
| 4870 | set_scnode_clear_ctx(set); |
| 4871 | return rc; |
| 4872 | } |
| 4873 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4874 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4875 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4876 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4877 | LY_CHECK_RET(rc); |
| 4878 | |
| 4879 | ptr = strstr(args[0]->val.str, args[1]->val.str); |
| 4880 | if (ptr) { |
| 4881 | set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str))); |
| 4882 | } else { |
| 4883 | set_fill_string(set, "", 0); |
| 4884 | } |
| 4885 | |
| 4886 | return LY_SUCCESS; |
| 4887 | } |
| 4888 | |
| 4889 | /** |
| 4890 | * @brief Execute the XPath substring-before(string, string) function. |
| 4891 | * Returns LYXP_SET_STRING with the string preceding the occurance |
| 4892 | * of the second argument in the first or an empty string. |
| 4893 | * |
| 4894 | * @param[in] args Array of arguments. |
| 4895 | * @param[in] arg_count Count of elements in @p args. |
| 4896 | * @param[in,out] set Context and result set at the same time. |
| 4897 | * @param[in] options XPath options. |
| 4898 | * @return LY_ERR (LY_EINVAL for wrong arguments on schema) |
| 4899 | */ |
| 4900 | static LY_ERR |
| 4901 | xpath_substring_before(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 4902 | { |
| 4903 | char *ptr; |
| 4904 | struct lysc_node_leaf *sleaf; |
| 4905 | LY_ERR rc = LY_SUCCESS; |
| 4906 | |
| 4907 | if (options & LYXP_SCNODE_ALL) { |
| 4908 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4909 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4910 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
| 4911 | rc = LY_EINVAL; |
| 4912 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4913 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name); |
| 4914 | rc = LY_EINVAL; |
| 4915 | } |
| 4916 | } |
| 4917 | |
| 4918 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4919 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4920 | LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
| 4921 | rc = LY_EINVAL; |
| 4922 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4923 | LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name); |
| 4924 | rc = LY_EINVAL; |
| 4925 | } |
| 4926 | } |
| 4927 | set_scnode_clear_ctx(set); |
| 4928 | return rc; |
| 4929 | } |
| 4930 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4931 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4932 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4933 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4934 | LY_CHECK_RET(rc); |
| 4935 | |
| 4936 | ptr = strstr(args[0]->val.str, args[1]->val.str); |
| 4937 | if (ptr) { |
| 4938 | set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str); |
| 4939 | } else { |
| 4940 | set_fill_string(set, "", 0); |
| 4941 | } |
| 4942 | |
| 4943 | return LY_SUCCESS; |
| 4944 | } |
| 4945 | |
| 4946 | /** |
| 4947 | * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER |
| 4948 | * with the sum of all the nodes in the context. |
| 4949 | * |
| 4950 | * @param[in] args Array of arguments. |
| 4951 | * @param[in] arg_count Count of elements in @p args. |
| 4952 | * @param[in,out] set Context and result set at the same time. |
| 4953 | * @param[in] options XPath options. |
| 4954 | * @return LY_ERR (LY_EINVAL for wrong arguments on schema) |
| 4955 | */ |
| 4956 | static LY_ERR |
| 4957 | xpath_sum(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 4958 | { |
| 4959 | long double num; |
| 4960 | char *str; |
| 4961 | uint16_t i; |
| 4962 | struct lyxp_set set_item; |
| 4963 | struct lysc_node_leaf *sleaf; |
| 4964 | LY_ERR rc = LY_SUCCESS; |
| 4965 | |
| 4966 | if (options & LYXP_SCNODE_ALL) { |
| 4967 | if (args[0]->type == LYXP_SET_SCNODE_SET) { |
| 4968 | for (i = 0; i < args[0]->used; ++i) { |
| 4969 | if (args[0]->val.scnodes[i].in_ctx == 1) { |
| 4970 | sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode; |
| 4971 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4972 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, |
| 4973 | lys_nodetype2str(sleaf->nodetype), sleaf->name); |
| 4974 | rc = LY_EINVAL; |
| 4975 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 4976 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name); |
| 4977 | rc = LY_EINVAL; |
| 4978 | } |
| 4979 | } |
| 4980 | } |
| 4981 | } |
| 4982 | set_scnode_clear_ctx(set); |
| 4983 | return rc; |
| 4984 | } |
| 4985 | |
| 4986 | set_fill_number(set, 0); |
| 4987 | if (args[0]->type == LYXP_SET_EMPTY) { |
| 4988 | return LY_SUCCESS; |
| 4989 | } |
| 4990 | |
| 4991 | if (args[0]->type != LYXP_SET_NODE_SET) { |
| 4992 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "sum(node-set)"); |
| 4993 | return LY_EVALID; |
| 4994 | } |
| 4995 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 4996 | set_init(&set_item, set); |
| 4997 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4998 | set_item.type = LYXP_SET_NODE_SET; |
| 4999 | set_item.val.nodes = malloc(sizeof *set_item.val.nodes); |
| 5000 | LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM); |
| 5001 | |
| 5002 | set_item.used = 1; |
| 5003 | set_item.size = 1; |
| 5004 | |
| 5005 | for (i = 0; i < args[0]->used; ++i) { |
| 5006 | set_item.val.nodes[0] = args[0]->val.nodes[i]; |
| 5007 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5008 | rc = cast_node_set_to_string(&set_item, &str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5009 | LY_CHECK_RET(rc); |
| 5010 | num = cast_string_to_number(str); |
| 5011 | free(str); |
| 5012 | set->val.num += num; |
| 5013 | } |
| 5014 | |
| 5015 | free(set_item.val.nodes); |
| 5016 | |
| 5017 | return LY_SUCCESS; |
| 5018 | } |
| 5019 | |
| 5020 | /** |
| 5021 | * @brief Execute the XPath text() function (node type). Returns LYXP_SET_NODE_SET |
| 5022 | * with the text content of the nodes in the context. |
| 5023 | * |
| 5024 | * @param[in] args Array of arguments. |
| 5025 | * @param[in] arg_count Count of elements in @p args. |
| 5026 | * @param[in,out] set Context and result set at the same time. |
| 5027 | * @param[in] options XPath options. |
| 5028 | * @return LY_ERR (LY_EINVAL for wrong arguments on schema) |
| 5029 | */ |
| 5030 | static LY_ERR |
| 5031 | xpath_text(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 5032 | { |
| 5033 | uint32_t i; |
| 5034 | |
| 5035 | if (options & LYXP_SCNODE_ALL) { |
| 5036 | set_scnode_clear_ctx(set); |
| 5037 | return LY_SUCCESS; |
| 5038 | } |
| 5039 | |
| 5040 | if (set->type == LYXP_SET_EMPTY) { |
| 5041 | return LY_SUCCESS; |
| 5042 | } |
| 5043 | if (set->type != LYXP_SET_NODE_SET) { |
| 5044 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "text()"); |
| 5045 | return LY_EVALID; |
| 5046 | } |
| 5047 | |
| 5048 | for (i = 0; i < set->used;) { |
| 5049 | switch (set->val.nodes[i].type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 5050 | case LYXP_NODE_NONE: |
| 5051 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5052 | case LYXP_NODE_ELEM: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5053 | if (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 5054 | set->val.nodes[i].type = LYXP_NODE_TEXT; |
| 5055 | ++i; |
| 5056 | break; |
| 5057 | } |
| 5058 | /* fall through */ |
| 5059 | case LYXP_NODE_ROOT: |
| 5060 | case LYXP_NODE_ROOT_CONFIG: |
| 5061 | case LYXP_NODE_TEXT: |
| 5062 | case LYXP_NODE_ATTR: |
| 5063 | set_remove_node(set, i); |
| 5064 | break; |
| 5065 | } |
| 5066 | } |
| 5067 | |
| 5068 | return LY_SUCCESS; |
| 5069 | } |
| 5070 | |
| 5071 | /** |
| 5072 | * @brief Execute the XPath translate(string, string, string) function. |
| 5073 | * Returns LYXP_SET_STRING with the first argument with the characters |
| 5074 | * from the second argument replaced by those on the corresponding |
| 5075 | * positions in the third argument. |
| 5076 | * |
| 5077 | * @param[in] args Array of arguments. |
| 5078 | * @param[in] arg_count Count of elements in @p args. |
| 5079 | * @param[in,out] set Context and result set at the same time. |
| 5080 | * @param[in] options XPath options. |
| 5081 | * @return LY_ERR (LY_EINVAL for wrong arguments on schema) |
| 5082 | */ |
| 5083 | static LY_ERR |
| 5084 | xpath_translate(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 5085 | { |
| 5086 | uint16_t i, j, new_used; |
| 5087 | char *new; |
| 5088 | int found, have_removed; |
| 5089 | struct lysc_node_leaf *sleaf; |
| 5090 | LY_ERR rc = LY_SUCCESS; |
| 5091 | |
| 5092 | if (options & LYXP_SCNODE_ALL) { |
| 5093 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 5094 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5095 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
| 5096 | rc = LY_EINVAL; |
| 5097 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5098 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name); |
| 5099 | rc = LY_EINVAL; |
| 5100 | } |
| 5101 | } |
| 5102 | |
| 5103 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 5104 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5105 | LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
| 5106 | rc = LY_EINVAL; |
| 5107 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5108 | LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name); |
| 5109 | rc = LY_EINVAL; |
| 5110 | } |
| 5111 | } |
| 5112 | |
| 5113 | if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) { |
| 5114 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5115 | LOGWRN(set->ctx, "Argument #3 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
| 5116 | rc = LY_EINVAL; |
| 5117 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5118 | LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of string-type.", __func__, sleaf->name); |
| 5119 | rc = LY_EINVAL; |
| 5120 | } |
| 5121 | } |
| 5122 | set_scnode_clear_ctx(set); |
| 5123 | return rc; |
| 5124 | } |
| 5125 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5126 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5127 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5128 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5129 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5130 | rc = lyxp_set_cast(args[2], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5131 | LY_CHECK_RET(rc); |
| 5132 | |
| 5133 | new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char)); |
| 5134 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 5135 | new_used = 0; |
| 5136 | |
| 5137 | have_removed = 0; |
| 5138 | for (i = 0; args[0]->val.str[i]; ++i) { |
| 5139 | found = 0; |
| 5140 | |
| 5141 | for (j = 0; args[1]->val.str[j]; ++j) { |
| 5142 | if (args[0]->val.str[i] == args[1]->val.str[j]) { |
| 5143 | /* removing this char */ |
| 5144 | if (j >= strlen(args[2]->val.str)) { |
| 5145 | have_removed = 1; |
| 5146 | found = 1; |
| 5147 | break; |
| 5148 | } |
| 5149 | /* replacing this char */ |
| 5150 | new[new_used] = args[2]->val.str[j]; |
| 5151 | ++new_used; |
| 5152 | found = 1; |
| 5153 | break; |
| 5154 | } |
| 5155 | } |
| 5156 | |
| 5157 | /* copying this char */ |
| 5158 | if (!found) { |
| 5159 | new[new_used] = args[0]->val.str[i]; |
| 5160 | ++new_used; |
| 5161 | } |
| 5162 | } |
| 5163 | |
| 5164 | if (have_removed) { |
| 5165 | new = ly_realloc(new, (new_used + 1) * sizeof(char)); |
| 5166 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 5167 | } |
| 5168 | new[new_used] = '\0'; |
| 5169 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5170 | lyxp_set_cast(set, LYXP_SET_EMPTY); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5171 | set->type = LYXP_SET_STRING; |
| 5172 | set->val.str = new; |
| 5173 | |
| 5174 | return LY_SUCCESS; |
| 5175 | } |
| 5176 | |
| 5177 | /** |
| 5178 | * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN |
| 5179 | * with true value. |
| 5180 | * |
| 5181 | * @param[in] args Array of arguments. |
| 5182 | * @param[in] arg_count Count of elements in @p args. |
| 5183 | * @param[in,out] set Context and result set at the same time. |
| 5184 | * @param[in] options XPath options. |
| 5185 | * @return LY_ERR (LY_EINVAL for wrong arguments on schema) |
| 5186 | */ |
| 5187 | static LY_ERR |
| 5188 | xpath_true(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 5189 | { |
| 5190 | if (options & LYXP_SCNODE_ALL) { |
| 5191 | set_scnode_clear_ctx(set); |
| 5192 | return LY_SUCCESS; |
| 5193 | } |
| 5194 | |
| 5195 | set_fill_boolean(set, 1); |
| 5196 | return LY_SUCCESS; |
| 5197 | } |
| 5198 | |
| 5199 | /* |
| 5200 | * moveto functions |
| 5201 | * |
| 5202 | * They and only they actually change the context (set). |
| 5203 | */ |
| 5204 | |
| 5205 | /** |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5206 | * @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] | 5207 | * |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5208 | * @param[in,out] qname Qualified node name. If includes prefix, it is skipped. |
| 5209 | * @param[in,out] qname_len Length of @p qname, is updated accordingly. |
| 5210 | * @param[in] set Set with XPath context. |
| 5211 | * @param[out] moveto_mod Expected module of a matching node. |
| 5212 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5213 | */ |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5214 | static LY_ERR |
| 5215 | 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] | 5216 | { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5217 | const struct lys_module *mod; |
| 5218 | const char *ptr; |
| 5219 | int pref_len; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5220 | char *str; |
| 5221 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5222 | if ((ptr = ly_strnchr(*qname, ':', *qname_len))) { |
| 5223 | /* specific module */ |
| 5224 | pref_len = ptr - *qname; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5225 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5226 | switch (set->format) { |
| 5227 | case LYD_UNKNOWN: |
| 5228 | /* schema, search all local module imports */ |
| 5229 | mod = lys_module_find_prefix(set->local_mod, *qname, pref_len); |
| 5230 | break; |
| 5231 | case LYD_JSON: |
| 5232 | /* JSON data, search in context */ |
| 5233 | str = strndup(*qname, pref_len); |
| 5234 | mod = ly_ctx_get_module(set->ctx, str, NULL); |
| 5235 | free(str); |
| 5236 | break; |
| 5237 | default: |
| 5238 | LOGINT_RET(set->ctx); |
| 5239 | } |
| 5240 | |
Juraj Vijtiuk | d75faa6 | 2019-11-26 14:10:10 +0100 | [diff] [blame] | 5241 | /* Check for errors and non-implemented modules, as they are not valid */ |
| 5242 | if (!mod || !mod->implemented) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5243 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INMOD, pref_len, *qname); |
| 5244 | return LY_EVALID; |
| 5245 | } |
Juraj Vijtiuk | d75faa6 | 2019-11-26 14:10:10 +0100 | [diff] [blame] | 5246 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5247 | *qname += pref_len + 1; |
| 5248 | *qname_len -= pref_len + 1; |
| 5249 | } else if (((*qname)[0] == '*') && (*qname_len == 1)) { |
| 5250 | /* all modules - special case */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5251 | mod = NULL; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5252 | } else { |
| 5253 | /* local module */ |
| 5254 | mod = set->local_mod; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5255 | } |
| 5256 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5257 | *moveto_mod = mod; |
| 5258 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5259 | } |
| 5260 | |
| 5261 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5262 | * @brief Move context @p set to the root. Handles absolute path. |
| 5263 | * Result is LYXP_SET_NODE_SET. |
| 5264 | * |
| 5265 | * @param[in,out] set Set to use. |
| 5266 | * @param[in] options Xpath options. |
| 5267 | */ |
| 5268 | static void |
| 5269 | moveto_root(struct lyxp_set *set, int options) |
| 5270 | { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5271 | if (!set) { |
| 5272 | return; |
| 5273 | } |
| 5274 | |
| 5275 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5276 | set_scnode_clear_ctx(set); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 5277 | lyxp_set_scnode_insert_node(set, NULL, set->root_type); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5278 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5279 | lyxp_set_cast(set, LYXP_SET_EMPTY); |
| 5280 | set_insert_node(set, NULL, 0, set->root_type, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5281 | } |
| 5282 | } |
| 5283 | |
| 5284 | /** |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 5285 | * @brief Check whether a node has some unresolved "when". |
| 5286 | * |
| 5287 | * @param[in] node Node to check. |
| 5288 | * @return LY_ERR value (LY_EINCOMPLETE if there are some unresolved "when") |
| 5289 | */ |
| 5290 | static LY_ERR |
| 5291 | moveto_when_check(const struct lyd_node *node) |
| 5292 | { |
| 5293 | const struct lysc_node *schema; |
| 5294 | |
| 5295 | if (!node) { |
| 5296 | return LY_SUCCESS; |
| 5297 | } |
| 5298 | |
| 5299 | schema = node->schema; |
| 5300 | do { |
| 5301 | if (schema->when && !(node->flags & LYD_WHEN_TRUE)) { |
| 5302 | return LY_EINCOMPLETE; |
| 5303 | } |
| 5304 | schema = schema->parent; |
| 5305 | } while (schema && (schema->nodetype & (LYS_CASE | LYS_CHOICE))); |
| 5306 | |
| 5307 | return LY_SUCCESS; |
| 5308 | } |
| 5309 | |
| 5310 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5311 | * @brief Check @p node as a part of NameTest processing. |
| 5312 | * |
| 5313 | * @param[in] node Node to check. |
| 5314 | * @param[in] root_type XPath root node type. |
| 5315 | * @param[in] node_name Node name to move to. Must be in the dictionary! |
| 5316 | * @param[in] moveto_mod Expected module of the node. |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5317 | * @return LY_ERR (LY_ENOT if node does not match, LY_EINCOMPLETE on unresolved when, |
| 5318 | * LY_EINVAL if netither node nor any children match) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5319 | */ |
| 5320 | static LY_ERR |
| 5321 | moveto_node_check(const struct lyd_node *node, enum lyxp_node_type root_type, const char *node_name, |
| 5322 | const struct lys_module *moveto_mod) |
| 5323 | { |
| 5324 | /* module check */ |
| 5325 | if (moveto_mod && (node->schema->module != moveto_mod)) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5326 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5327 | } |
| 5328 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5329 | /* context check */ |
| 5330 | 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] | 5331 | return LY_EINVAL; |
| 5332 | } |
| 5333 | |
| 5334 | /* name check */ |
Michal Vasko | 465a0e1 | 2019-11-07 11:11:58 +0100 | [diff] [blame] | 5335 | if (strcmp(node_name, "*") && (node->schema->name != node_name)) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5336 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5337 | } |
| 5338 | |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 5339 | /* when check */ |
| 5340 | if (moveto_when_check(node)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5341 | return LY_EINCOMPLETE; |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 5342 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5343 | |
| 5344 | /* match */ |
| 5345 | return LY_SUCCESS; |
| 5346 | } |
| 5347 | |
| 5348 | /** |
| 5349 | * @brief Check @p node as a part of schema NameTest processing. |
| 5350 | * |
| 5351 | * @param[in] node Schema node to check. |
| 5352 | * @param[in] root_type XPath root node type. |
| 5353 | * @param[in] node_name Node name to move to. Must be in the dictionary! |
| 5354 | * @param[in] moveto_mod Expected module of the node. |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5355 | * @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] | 5356 | */ |
| 5357 | static LY_ERR |
| 5358 | 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] | 5359 | const struct lys_module *moveto_mod) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5360 | { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5361 | /* module check */ |
| 5362 | if (strcmp(node_name, "*") && (node->module != moveto_mod)) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5363 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5364 | } |
| 5365 | |
| 5366 | /* context check */ |
| 5367 | if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) { |
| 5368 | return LY_EINVAL; |
| 5369 | } |
| 5370 | |
| 5371 | /* name check */ |
Michal Vasko | 465a0e1 | 2019-11-07 11:11:58 +0100 | [diff] [blame] | 5372 | if (strcmp(node_name, "*") && (node->name != node_name)) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5373 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5374 | } |
| 5375 | |
| 5376 | /* match */ |
| 5377 | return LY_SUCCESS; |
| 5378 | } |
| 5379 | |
| 5380 | /** |
| 5381 | * @brief Move context @p set to a node. Handles '/' and '*', 'NAME', 'PREFIX:*', or 'PREFIX:NAME'. |
| 5382 | * Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY). Context position aware. |
| 5383 | * |
| 5384 | * @param[in,out] set Set to use. |
| 5385 | * @param[in] qname Qualified node name to move to. |
| 5386 | * @param[in] qname_len Length of @p qname. |
| 5387 | * @param[in] options XPath options. |
| 5388 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 5389 | */ |
| 5390 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5391 | moveto_node(struct lyxp_set *set, const char *qname, uint16_t qname_len) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5392 | { |
Michal Vasko | 79bebfd | 2019-11-14 16:09:19 +0100 | [diff] [blame] | 5393 | uint32_t i, j; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5394 | int replaced; |
| 5395 | const char *name_dict = NULL; /* optimization - so we can do (==) instead (!strncmp(...)) in moveto_node_check() */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5396 | const struct lys_module *moveto_mod; |
| 5397 | const struct lyd_node *sub; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5398 | LY_ERR rc; |
| 5399 | |
| 5400 | if (!set || (set->type == LYXP_SET_EMPTY)) { |
| 5401 | return LY_SUCCESS; |
| 5402 | } |
| 5403 | |
| 5404 | if (set->type != LYXP_SET_NODE_SET) { |
| 5405 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 5406 | return LY_EVALID; |
| 5407 | } |
| 5408 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5409 | rc = moveto_resolve_model(&qname, &qname_len, set, &moveto_mod); |
| 5410 | LY_CHECK_RET(rc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5411 | |
| 5412 | /* name */ |
| 5413 | name_dict = lydict_insert(set->ctx, qname, qname_len); |
| 5414 | |
| 5415 | for (i = 0; i < set->used; ) { |
| 5416 | replaced = 0; |
| 5417 | |
| 5418 | 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] | 5419 | assert(!set->val.nodes[i].node); |
| 5420 | /* search in all the trees */ |
Michal Vasko | 79bebfd | 2019-11-14 16:09:19 +0100 | [diff] [blame] | 5421 | LY_ARRAY_FOR(set->trees, j) { |
| 5422 | for (sub = set->trees[j]; sub; sub = sub->next) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5423 | rc = moveto_node_check(sub, set->root_type, name_dict, moveto_mod); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5424 | if (rc == LY_SUCCESS) { |
| 5425 | /* pos filled later */ |
| 5426 | if (!replaced) { |
| 5427 | set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i); |
| 5428 | replaced = 1; |
| 5429 | } else { |
| 5430 | set_insert_node(set, sub, 0, LYXP_NODE_ELEM, i); |
| 5431 | } |
| 5432 | ++i; |
| 5433 | } else if (rc == LY_EINCOMPLETE) { |
| 5434 | lydict_remove(set->ctx, name_dict); |
| 5435 | return rc; |
| 5436 | } |
| 5437 | } |
| 5438 | } |
| 5439 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5440 | /* skip nodes without children - leaves, leaflists, anyxmls (ouput root will eval to true) */ |
| 5441 | } else if (!(set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5442 | |
| 5443 | for (sub = lyd_node_children(set->val.nodes[i].node); sub; sub = sub->next) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5444 | rc = moveto_node_check(sub, set->root_type, name_dict, moveto_mod); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5445 | if (rc == LY_SUCCESS) { |
| 5446 | if (!replaced) { |
| 5447 | set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i); |
| 5448 | replaced = 1; |
| 5449 | } else { |
| 5450 | set_insert_node(set, sub, 0, LYXP_NODE_ELEM, i); |
| 5451 | } |
| 5452 | ++i; |
| 5453 | } else if (rc == LY_EINCOMPLETE) { |
| 5454 | lydict_remove(set->ctx, name_dict); |
| 5455 | return rc; |
| 5456 | } |
| 5457 | } |
| 5458 | } |
| 5459 | |
| 5460 | if (!replaced) { |
| 5461 | /* no match */ |
| 5462 | set_remove_node(set, i); |
| 5463 | } |
| 5464 | } |
| 5465 | lydict_remove(set->ctx, name_dict); |
| 5466 | |
| 5467 | return LY_SUCCESS; |
| 5468 | } |
| 5469 | |
| 5470 | /** |
| 5471 | * @brief Move context @p set to a schema node. Handles '/' and '*', 'NAME', 'PREFIX:*', or 'PREFIX:NAME'. |
| 5472 | * Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY). |
| 5473 | * |
| 5474 | * @param[in,out] set Set to use. |
| 5475 | * @param[in] qname Qualified node name to move to. |
| 5476 | * @param[in] qname_len Length of @p qname. |
| 5477 | * @param[in] options XPath options. |
| 5478 | * @return LY_ERR |
| 5479 | */ |
| 5480 | static LY_ERR |
| 5481 | moveto_scnode(struct lyxp_set *set, const char *qname, uint16_t qname_len, int options) |
| 5482 | { |
Michal Vasko | cafad9d | 2019-11-07 15:20:03 +0100 | [diff] [blame] | 5483 | int i, orig_used, idx, temp_ctx = 0, getnext_opts; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5484 | uint32_t mod_idx; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5485 | const char *name_dict = NULL; /* optimization - so we can do (==) instead (!strncmp(...)) in moveto_node_check() */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5486 | const struct lys_module *moveto_mod; |
| 5487 | const struct lysc_node *sub, *start_parent; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5488 | LY_ERR rc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5489 | |
| 5490 | if (!set || (set->type == LYXP_SET_EMPTY)) { |
| 5491 | return LY_SUCCESS; |
| 5492 | } |
| 5493 | |
| 5494 | if (set->type != LYXP_SET_SCNODE_SET) { |
| 5495 | LOGVAL(set->ctx, LY_VLOG_LYS, set->ctx_scnode, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 5496 | return LY_EVALID; |
| 5497 | } |
| 5498 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5499 | rc = moveto_resolve_model(&qname, &qname_len, set, &moveto_mod); |
| 5500 | LY_CHECK_RET(rc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5501 | |
| 5502 | /* name */ |
| 5503 | name_dict = lydict_insert(set->ctx, qname, qname_len); |
| 5504 | |
Michal Vasko | cafad9d | 2019-11-07 15:20:03 +0100 | [diff] [blame] | 5505 | /* getnext opts */ |
| 5506 | getnext_opts = LYS_GETNEXT_NOSTATECHECK; |
| 5507 | if (options & LYXP_SCNODE_OUTPUT) { |
| 5508 | getnext_opts |= LYS_GETNEXT_OUTPUT; |
| 5509 | } |
| 5510 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5511 | orig_used = set->used; |
| 5512 | for (i = 0; i < orig_used; ++i) { |
| 5513 | if (set->val.scnodes[i].in_ctx != 1) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5514 | if (set->val.scnodes[i].in_ctx != -2) { |
| 5515 | continue; |
| 5516 | } |
| 5517 | |
| 5518 | /* remember context node */ |
| 5519 | set->val.scnodes[i].in_ctx = -1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5520 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5521 | |
| 5522 | start_parent = set->val.scnodes[i].scnode; |
| 5523 | |
| 5524 | if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) { |
| 5525 | /* it can actually be in any module, it's all <running>, but we know it's moveto_mod (if set), |
| 5526 | * so use it directly (root node itself is useless in this case) */ |
| 5527 | mod_idx = 0; |
| 5528 | while (moveto_mod || (moveto_mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) { |
| 5529 | sub = NULL; |
Michal Vasko | 509de4d | 2019-12-10 14:51:30 +0100 | [diff] [blame] | 5530 | /* module may not be implemented */ |
| 5531 | while (moveto_mod->implemented && (sub = lys_getnext(sub, NULL, moveto_mod->compiled, getnext_opts))) { |
Michal Vasko | cafad9d | 2019-11-07 15:20:03 +0100 | [diff] [blame] | 5532 | if (!moveto_scnode_check(sub, set->root_type, name_dict, moveto_mod)) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 5533 | idx = lyxp_set_scnode_insert_node(set, sub, LYXP_NODE_ELEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5534 | /* we need to prevent these nodes from being considered in this moveto */ |
| 5535 | if ((idx < orig_used) && (idx > i)) { |
| 5536 | set->val.scnodes[idx].in_ctx = 2; |
| 5537 | temp_ctx = 1; |
| 5538 | } |
| 5539 | } |
| 5540 | } |
| 5541 | |
| 5542 | if (!mod_idx) { |
| 5543 | /* moveto_mod was specified, we are not going through the whole context */ |
| 5544 | break; |
| 5545 | } |
| 5546 | /* next iteration */ |
| 5547 | moveto_mod = NULL; |
| 5548 | } |
| 5549 | |
| 5550 | /* skip nodes without children - leaves, leaflists, and anyxmls (ouput root will eval to true) */ |
| 5551 | } else if (!(start_parent->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) { |
| 5552 | sub = NULL; |
Michal Vasko | cafad9d | 2019-11-07 15:20:03 +0100 | [diff] [blame] | 5553 | while ((sub = lys_getnext(sub, start_parent, NULL, getnext_opts))) { |
| 5554 | if (!moveto_scnode_check(sub, set->root_type, name_dict, (moveto_mod ? moveto_mod : set->local_mod))) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 5555 | idx = lyxp_set_scnode_insert_node(set, sub, LYXP_NODE_ELEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5556 | if ((idx < orig_used) && (idx > i)) { |
| 5557 | set->val.scnodes[idx].in_ctx = 2; |
| 5558 | temp_ctx = 1; |
| 5559 | } |
| 5560 | } |
| 5561 | } |
| 5562 | } |
| 5563 | } |
| 5564 | lydict_remove(set->ctx, name_dict); |
| 5565 | |
| 5566 | /* correct temporary in_ctx values */ |
| 5567 | if (temp_ctx) { |
| 5568 | for (i = 0; i < orig_used; ++i) { |
| 5569 | if (set->val.scnodes[i].in_ctx == 2) { |
| 5570 | set->val.scnodes[i].in_ctx = 1; |
| 5571 | } |
| 5572 | } |
| 5573 | } |
| 5574 | |
| 5575 | return LY_SUCCESS; |
| 5576 | } |
| 5577 | |
| 5578 | /** |
| 5579 | * @brief Move context @p set to a node and all its descendants. Handles '//' and '*', 'NAME', |
| 5580 | * 'PREFIX:*', or 'PREFIX:NAME'. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY). |
| 5581 | * Context position aware. |
| 5582 | * |
| 5583 | * @param[in] set Set to use. |
| 5584 | * @param[in] qname Qualified node name to move to. |
| 5585 | * @param[in] qname_len Length of @p qname. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5586 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 5587 | */ |
| 5588 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5589 | moveto_node_alldesc(struct lyxp_set *set, const char *qname, uint16_t qname_len) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5590 | { |
| 5591 | uint32_t i; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5592 | const char *name_dict = NULL; /* optimization - so we can do (==) instead (!strncmp(...)) in moveto_node_check() */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5593 | const struct lyd_node *next, *elem, *start; |
| 5594 | const struct lys_module *moveto_mod; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5595 | struct lyxp_set ret_set; |
| 5596 | LY_ERR rc; |
| 5597 | |
| 5598 | if (!set || (set->type == LYXP_SET_EMPTY)) { |
| 5599 | return LY_SUCCESS; |
| 5600 | } |
| 5601 | |
| 5602 | if (set->type != LYXP_SET_NODE_SET) { |
| 5603 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 5604 | return LY_EVALID; |
| 5605 | } |
| 5606 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5607 | rc = moveto_resolve_model(&qname, &qname_len, set, &moveto_mod); |
| 5608 | LY_CHECK_RET(rc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5609 | |
| 5610 | /* replace the original nodes (and throws away all text and attr nodes, root is replaced by a child) */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5611 | rc = moveto_node(set, "*", 1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5612 | LY_CHECK_RET(rc); |
| 5613 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5614 | /* name */ |
| 5615 | name_dict = lydict_insert(set->ctx, qname, qname_len); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5616 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5617 | /* 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] | 5618 | set_init(&ret_set, set); |
| 5619 | for (i = 0; i < set->used; ++i) { |
| 5620 | |
| 5621 | /* TREE DFS */ |
| 5622 | start = set->val.nodes[i].node; |
| 5623 | for (elem = next = start; elem; elem = next) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5624 | rc = moveto_node_check(elem, set->root_type, name_dict, moveto_mod); |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5625 | if (!rc) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5626 | /* add matching node into result set */ |
| 5627 | set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used); |
| 5628 | if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) { |
| 5629 | /* the node is a duplicate, we'll process it later in the set */ |
| 5630 | goto skip_children; |
| 5631 | } |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5632 | } else if (rc == LY_EINCOMPLETE) { |
| 5633 | lydict_remove(set->ctx, name_dict); |
| 5634 | return rc; |
| 5635 | } else if (rc == LY_EINVAL) { |
| 5636 | goto skip_children; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5637 | } |
| 5638 | |
| 5639 | /* TREE DFS NEXT ELEM */ |
| 5640 | /* select element for the next run - children first */ |
| 5641 | if (elem->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
| 5642 | next = NULL; |
| 5643 | } else { |
| 5644 | next = lyd_node_children(elem); |
| 5645 | } |
| 5646 | if (!next) { |
| 5647 | skip_children: |
| 5648 | /* no children, so try siblings, but only if it's not the start, |
| 5649 | * that is considered to be the root and it's siblings are not traversed */ |
| 5650 | if (elem != start) { |
| 5651 | next = elem->next; |
| 5652 | } else { |
| 5653 | break; |
| 5654 | } |
| 5655 | } |
| 5656 | while (!next) { |
| 5657 | /* no siblings, go back through the parents */ |
| 5658 | if ((struct lyd_node *)elem->parent == start) { |
| 5659 | /* we are done, no next element to process */ |
| 5660 | break; |
| 5661 | } |
| 5662 | /* parent is already processed, go to its sibling */ |
| 5663 | elem = (struct lyd_node *)elem->parent; |
| 5664 | next = elem->next; |
| 5665 | } |
| 5666 | } |
| 5667 | } |
| 5668 | |
| 5669 | /* make the temporary set the current one */ |
| 5670 | ret_set.ctx_pos = set->ctx_pos; |
| 5671 | ret_set.ctx_size = set->ctx_size; |
| 5672 | set_free_content(set); |
| 5673 | memcpy(set, &ret_set, sizeof *set); |
| 5674 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5675 | lydict_remove(set->ctx, name_dict); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5676 | return LY_SUCCESS; |
| 5677 | } |
| 5678 | |
| 5679 | /** |
| 5680 | * @brief Move context @p set to a schema node and all its descendants. Handles '//' and '*', 'NAME', |
| 5681 | * 'PREFIX:*', or 'PREFIX:NAME'. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY). |
| 5682 | * |
| 5683 | * @param[in] set Set to use. |
| 5684 | * @param[in] qname Qualified node name to move to. |
| 5685 | * @param[in] qname_len Length of @p qname. |
| 5686 | * @param[in] options XPath options. |
| 5687 | * @return LY_ERR |
| 5688 | */ |
| 5689 | static LY_ERR |
| 5690 | moveto_scnode_alldesc(struct lyxp_set *set, const char *qname, uint16_t qname_len, int options) |
| 5691 | { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5692 | int i, orig_used, idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5693 | const struct lysc_node *next, *elem, *start; |
| 5694 | const struct lys_module *moveto_mod; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5695 | LY_ERR rc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5696 | |
| 5697 | if (!set || (set->type == LYXP_SET_EMPTY)) { |
| 5698 | return LY_SUCCESS; |
| 5699 | } |
| 5700 | |
| 5701 | if (set->type != LYXP_SET_SCNODE_SET) { |
| 5702 | LOGVAL(set->ctx, LY_VLOG_LYS, set->ctx_scnode, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 5703 | return LY_EVALID; |
| 5704 | } |
| 5705 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5706 | rc = moveto_resolve_model(&qname, &qname_len, set, &moveto_mod); |
| 5707 | LY_CHECK_RET(rc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5708 | |
| 5709 | orig_used = set->used; |
| 5710 | for (i = 0; i < orig_used; ++i) { |
| 5711 | if (set->val.scnodes[i].in_ctx != 1) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5712 | if (set->val.scnodes[i].in_ctx != -2) { |
| 5713 | continue; |
| 5714 | } |
| 5715 | |
| 5716 | /* remember context node */ |
| 5717 | set->val.scnodes[i].in_ctx = -1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5718 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5719 | |
| 5720 | /* TREE DFS */ |
| 5721 | start = set->val.scnodes[i].scnode; |
| 5722 | for (elem = next = start; elem; elem = next) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5723 | if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) { |
| 5724 | /* schema-only nodes, skip root */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5725 | goto next_iter; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5726 | } |
| 5727 | |
Michal Vasko | cafad9d | 2019-11-07 15:20:03 +0100 | [diff] [blame] | 5728 | rc = moveto_scnode_check(elem, set->root_type, qname, moveto_mod); |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5729 | if (!rc) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 5730 | 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] | 5731 | set->val.scnodes[idx].in_ctx = 1; |
| 5732 | if (idx > i) { |
| 5733 | /* we will process it later in the set */ |
| 5734 | goto skip_children; |
| 5735 | } |
| 5736 | } else { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 5737 | lyxp_set_scnode_insert_node(set, elem, LYXP_NODE_ELEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5738 | } |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5739 | } else if (rc == LY_EINVAL) { |
| 5740 | goto skip_children; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5741 | } |
| 5742 | |
| 5743 | next_iter: |
| 5744 | /* TREE DFS NEXT ELEM */ |
| 5745 | /* select element for the next run - children first */ |
| 5746 | next = lysc_node_children(elem, options & LYXP_SCNODE_OUTPUT ? LYS_CONFIG_R : LYS_CONFIG_W); |
| 5747 | if (elem->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
| 5748 | next = NULL; |
| 5749 | } |
| 5750 | if (!next) { |
| 5751 | skip_children: |
| 5752 | /* no children, so try siblings, but only if it's not the start, |
| 5753 | * that is considered to be the root and it's siblings are not traversed */ |
| 5754 | if (elem != start) { |
| 5755 | next = elem->next; |
| 5756 | } else { |
| 5757 | break; |
| 5758 | } |
| 5759 | } |
| 5760 | while (!next) { |
| 5761 | /* no siblings, go back through the parents */ |
| 5762 | if (elem->parent == start) { |
| 5763 | /* we are done, no next element to process */ |
| 5764 | break; |
| 5765 | } |
| 5766 | /* parent is already processed, go to its sibling */ |
| 5767 | elem = elem->parent; |
| 5768 | next = elem->next; |
| 5769 | } |
| 5770 | } |
| 5771 | } |
| 5772 | |
| 5773 | return LY_SUCCESS; |
| 5774 | } |
| 5775 | |
| 5776 | /** |
| 5777 | * @brief Move context @p set to an attribute. Handles '/' and '@*', '@NAME', '@PREFIX:*', |
| 5778 | * or '@PREFIX:NAME'. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY). |
| 5779 | * Indirectly context position aware. |
| 5780 | * |
| 5781 | * @param[in,out] set Set to use. |
| 5782 | * @param[in] qname Qualified node name to move to. |
| 5783 | * @param[in] qname_len Length of @p qname. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5784 | * @return LY_ERR |
| 5785 | */ |
| 5786 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5787 | moveto_attr(struct lyxp_set *set, const char *qname, uint16_t qname_len) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5788 | { |
| 5789 | uint32_t i; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5790 | int replaced, all = 0; |
| 5791 | const struct lys_module *moveto_mod; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5792 | struct lyd_attr *sub; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5793 | LY_ERR rc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5794 | |
| 5795 | if (!set || (set->type == LYXP_SET_EMPTY)) { |
| 5796 | return LY_SUCCESS; |
| 5797 | } |
| 5798 | |
| 5799 | if (set->type != LYXP_SET_NODE_SET) { |
| 5800 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 5801 | return LY_EVALID; |
| 5802 | } |
| 5803 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5804 | rc = moveto_resolve_model(&qname, &qname_len, set, &moveto_mod); |
| 5805 | LY_CHECK_RET(rc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5806 | |
| 5807 | if ((qname_len == 1) && (qname[0] == '*')) { |
| 5808 | all = 1; |
| 5809 | } |
| 5810 | |
| 5811 | for (i = 0; i < set->used; ) { |
| 5812 | replaced = 0; |
| 5813 | |
| 5814 | /* only attributes of an elem (not dummy) can be in the result, skip all the rest; |
| 5815 | * our attributes are always qualified */ |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5816 | if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5817 | for (sub = set->val.nodes[i].node->attr; sub; sub = sub->next) { |
| 5818 | |
| 5819 | /* check "namespace" */ |
| 5820 | if (moveto_mod && (sub->annotation->module != moveto_mod)) { |
| 5821 | continue; |
| 5822 | } |
| 5823 | |
| 5824 | if (all || (!strncmp(sub->name, qname, qname_len) && !sub->name[qname_len])) { |
| 5825 | /* match */ |
| 5826 | if (!replaced) { |
| 5827 | set->val.attrs[i].attr = sub; |
| 5828 | set->val.attrs[i].type = LYXP_NODE_ATTR; |
| 5829 | /* pos does not change */ |
| 5830 | replaced = 1; |
| 5831 | } else { |
| 5832 | set_insert_node(set, (struct lyd_node *)sub, set->val.nodes[i].pos, LYXP_NODE_ATTR, i + 1); |
| 5833 | } |
| 5834 | ++i; |
| 5835 | } |
| 5836 | } |
| 5837 | } |
| 5838 | |
| 5839 | if (!replaced) { |
| 5840 | /* no match */ |
| 5841 | set_remove_node(set, i); |
| 5842 | } |
| 5843 | } |
| 5844 | |
| 5845 | return LY_SUCCESS; |
| 5846 | } |
| 5847 | |
| 5848 | /** |
| 5849 | * @brief Move context @p set1 to union with @p set2. @p set2 is emptied afterwards. |
| 5850 | * Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY). Context position aware. |
| 5851 | * |
| 5852 | * @param[in,out] set1 Set to use for the result. |
| 5853 | * @param[in] set2 Set that is copied to @p set1. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5854 | * @return LY_ERR |
| 5855 | */ |
| 5856 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5857 | moveto_union(struct lyxp_set *set1, struct lyxp_set *set2) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5858 | { |
| 5859 | LY_ERR rc; |
| 5860 | |
| 5861 | if (((set1->type != LYXP_SET_NODE_SET) && (set1->type != LYXP_SET_EMPTY)) |
| 5862 | || ((set2->type != LYXP_SET_NODE_SET) && (set2->type != LYXP_SET_EMPTY))) { |
| 5863 | LOGVAL(set1->ctx, LY_VLOG_LYD, set1->ctx_node, LY_VCODE_XP_INOP_2, "union", print_set_type(set1), print_set_type(set2)); |
| 5864 | return LY_EVALID; |
| 5865 | } |
| 5866 | |
| 5867 | /* set2 is empty or both set1 and set2 */ |
| 5868 | if (set2->type == LYXP_SET_EMPTY) { |
| 5869 | return LY_SUCCESS; |
| 5870 | } |
| 5871 | |
| 5872 | if (set1->type == LYXP_SET_EMPTY) { |
| 5873 | memcpy(set1, set2, sizeof *set1); |
| 5874 | /* dynamic memory belongs to set1 now, do not free */ |
| 5875 | set2->type = LYXP_SET_EMPTY; |
| 5876 | return LY_SUCCESS; |
| 5877 | } |
| 5878 | |
| 5879 | /* we assume sets are sorted */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5880 | assert(!set_sort(set1) && !set_sort(set2)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5881 | |
| 5882 | /* sort, remove duplicates */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5883 | rc = set_sorted_merge(set1, set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5884 | LY_CHECK_RET(rc); |
| 5885 | |
| 5886 | /* final set must be sorted */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5887 | assert(!set_sort(set1)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5888 | |
| 5889 | return LY_SUCCESS; |
| 5890 | } |
| 5891 | |
| 5892 | /** |
| 5893 | * @brief Move context @p set to an attribute in any of the descendants. Handles '//' and '@*', |
| 5894 | * '@NAME', '@PREFIX:*', or '@PREFIX:NAME'. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY). |
| 5895 | * Context position aware. |
| 5896 | * |
| 5897 | * @param[in,out] set Set to use. |
| 5898 | * @param[in] qname Qualified node name to move to. |
| 5899 | * @param[in] qname_len Length of @p qname. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5900 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 5901 | */ |
| 5902 | static int |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5903 | moveto_attr_alldesc(struct lyxp_set *set, const char *qname, uint16_t qname_len) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5904 | { |
| 5905 | uint32_t i; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5906 | int replaced, all = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5907 | struct lyd_attr *sub; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5908 | const struct lys_module *moveto_mod; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5909 | struct lyxp_set *set_all_desc = NULL; |
| 5910 | LY_ERR rc; |
| 5911 | |
| 5912 | if (!set || (set->type == LYXP_SET_EMPTY)) { |
| 5913 | return LY_SUCCESS; |
| 5914 | } |
| 5915 | |
| 5916 | if (set->type != LYXP_SET_NODE_SET) { |
| 5917 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 5918 | return LY_EVALID; |
| 5919 | } |
| 5920 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5921 | rc = moveto_resolve_model(&qname, &qname_len, set, &moveto_mod); |
| 5922 | LY_CHECK_RET(rc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5923 | |
| 5924 | /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory, |
| 5925 | * but it likely won't be used much, so it's a waste of time */ |
| 5926 | /* copy the context */ |
| 5927 | set_all_desc = set_copy(set); |
| 5928 | /* get all descendant nodes (the original context nodes are removed) */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5929 | rc = moveto_node_alldesc(set_all_desc, "*", 1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5930 | if (rc != LY_SUCCESS) { |
| 5931 | lyxp_set_free(set_all_desc); |
| 5932 | return rc; |
| 5933 | } |
| 5934 | /* prepend the original context nodes */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5935 | rc = moveto_union(set, set_all_desc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5936 | if (rc != LY_SUCCESS) { |
| 5937 | lyxp_set_free(set_all_desc); |
| 5938 | return rc; |
| 5939 | } |
| 5940 | lyxp_set_free(set_all_desc); |
| 5941 | |
| 5942 | if ((qname_len == 1) && (qname[0] == '*')) { |
| 5943 | all = 1; |
| 5944 | } |
| 5945 | |
| 5946 | for (i = 0; i < set->used; ) { |
| 5947 | replaced = 0; |
| 5948 | |
| 5949 | /* only attributes of an elem can be in the result, skip all the rest, |
| 5950 | * we have all attributes qualified in lyd tree */ |
| 5951 | if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
| 5952 | for (sub = set->val.nodes[i].node->attr; sub; sub = sub->next) { |
| 5953 | /* check "namespace" */ |
| 5954 | if (moveto_mod && (sub->annotation->module != moveto_mod)) { |
| 5955 | continue; |
| 5956 | } |
| 5957 | |
| 5958 | if (all || (!strncmp(sub->name, qname, qname_len) && !sub->name[qname_len])) { |
| 5959 | /* match */ |
| 5960 | if (!replaced) { |
| 5961 | set->val.attrs[i].attr = sub; |
| 5962 | set->val.attrs[i].type = LYXP_NODE_ATTR; |
| 5963 | /* pos does not change */ |
| 5964 | replaced = 1; |
| 5965 | } else { |
| 5966 | set_insert_node(set, (struct lyd_node *)sub, set->val.attrs[i].pos, LYXP_NODE_ATTR, i + 1); |
| 5967 | } |
| 5968 | ++i; |
| 5969 | } |
| 5970 | } |
| 5971 | } |
| 5972 | |
| 5973 | if (!replaced) { |
| 5974 | /* no match */ |
| 5975 | set_remove_node(set, i); |
| 5976 | } |
| 5977 | } |
| 5978 | |
| 5979 | return LY_SUCCESS; |
| 5980 | } |
| 5981 | |
| 5982 | /** |
| 5983 | * @brief Move context @p set to self and al chilren, recursively. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET |
| 5984 | * (or LYXP_SET_EMPTY). Context position aware. |
| 5985 | * |
| 5986 | * @param[in] parent Current parent. |
| 5987 | * @param[in] parent_pos Position of @p parent. |
| 5988 | * @param[in] parent_type Node type of @p parent. |
| 5989 | * @param[in,out] to_set Set to use. |
| 5990 | * @param[in] dup_check_set Set for checking duplicities. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5991 | * @param[in] options XPath options. |
| 5992 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 5993 | */ |
| 5994 | static LY_ERR |
| 5995 | 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] | 5996 | 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] | 5997 | { |
| 5998 | const struct lyd_node *sub; |
| 5999 | LY_ERR rc; |
| 6000 | |
| 6001 | switch (parent_type) { |
| 6002 | case LYXP_NODE_ROOT: |
| 6003 | case LYXP_NODE_ROOT_CONFIG: |
| 6004 | /* add the same node but as an element */ |
| 6005 | if (!set_dup_node_check(dup_check_set, parent, LYXP_NODE_ELEM, -1)) { |
| 6006 | set_insert_node(to_set, parent, 0, LYXP_NODE_ELEM, to_set->used); |
| 6007 | |
| 6008 | /* skip anydata/anyxml and dummy nodes */ |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6009 | if (!(parent->schema->nodetype & LYS_ANYDATA)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6010 | /* also add all the children of this node, recursively */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6011 | rc = moveto_self_add_children_r(parent, 0, LYXP_NODE_ELEM, to_set, dup_check_set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6012 | LY_CHECK_RET(rc); |
| 6013 | } |
| 6014 | } |
| 6015 | break; |
| 6016 | case LYXP_NODE_ELEM: |
| 6017 | /* add all the children ... */ |
| 6018 | if (!(parent->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) { |
| 6019 | for (sub = lyd_node_children(parent); sub; sub = sub->next) { |
| 6020 | /* context check */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6021 | if ((to_set->root_type == LYXP_NODE_ROOT_CONFIG) && (sub->schema->flags & LYS_CONFIG_R)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6022 | continue; |
| 6023 | } |
| 6024 | |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 6025 | /* when check */ |
| 6026 | if (moveto_when_check(sub)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6027 | return LY_EINCOMPLETE; |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 6028 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6029 | |
| 6030 | if (!set_dup_node_check(dup_check_set, sub, LYXP_NODE_ELEM, -1)) { |
| 6031 | set_insert_node(to_set, sub, 0, LYXP_NODE_ELEM, to_set->used); |
| 6032 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6033 | /* skip anydata/anyxml nodes */ |
| 6034 | if (sub->schema->nodetype & LYS_ANYDATA) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6035 | continue; |
| 6036 | } |
| 6037 | |
| 6038 | /* also add all the children of this node, recursively */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6039 | rc = moveto_self_add_children_r(sub, 0, LYXP_NODE_ELEM, to_set, dup_check_set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6040 | LY_CHECK_RET(rc); |
| 6041 | } |
| 6042 | } |
| 6043 | |
| 6044 | /* ... or add their text node, ... */ |
| 6045 | } else { |
| 6046 | if (!set_dup_node_check(dup_check_set, parent, LYXP_NODE_TEXT, -1)) { |
| 6047 | set_insert_node(to_set, parent, parent_pos, LYXP_NODE_TEXT, to_set->used); |
| 6048 | } |
| 6049 | } |
| 6050 | break; |
| 6051 | default: |
| 6052 | LOGINT_RET(parent->schema->module->ctx); |
| 6053 | } |
| 6054 | |
| 6055 | return LY_SUCCESS; |
| 6056 | } |
| 6057 | |
| 6058 | /** |
| 6059 | * @brief Move context @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET |
| 6060 | * (or LYXP_SET_EMPTY). Context position aware. |
| 6061 | * |
| 6062 | * @param[in,out] set Set to use. |
| 6063 | * @param[in] all_desc Whether to go to all descendants ('//') or not ('/'). |
| 6064 | * @param[in] options XPath options. |
| 6065 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6066 | */ |
| 6067 | static LY_ERR |
| 6068 | moveto_self(struct lyxp_set *set, int all_desc, int options) |
| 6069 | { |
| 6070 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6071 | struct lyxp_set ret_set; |
| 6072 | LY_ERR rc; |
| 6073 | |
| 6074 | if (!set || (set->type == LYXP_SET_EMPTY)) { |
| 6075 | return LY_SUCCESS; |
| 6076 | } |
| 6077 | |
| 6078 | if (set->type != LYXP_SET_NODE_SET) { |
| 6079 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 6080 | return LY_EVALID; |
| 6081 | } |
| 6082 | |
| 6083 | /* nothing to do */ |
| 6084 | if (!all_desc) { |
| 6085 | return LY_SUCCESS; |
| 6086 | } |
| 6087 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6088 | /* add all the children, they get added recursively */ |
| 6089 | set_init(&ret_set, set); |
| 6090 | for (i = 0; i < set->used; ++i) { |
| 6091 | /* copy the current node to tmp */ |
| 6092 | set_insert_node(&ret_set, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, ret_set.used); |
| 6093 | |
| 6094 | /* do not touch attributes and text nodes */ |
| 6095 | if ((set->val.nodes[i].type == LYXP_NODE_TEXT) || (set->val.nodes[i].type == LYXP_NODE_ATTR)) { |
| 6096 | continue; |
| 6097 | } |
| 6098 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6099 | /* skip anydata/anyxml nodes */ |
| 6100 | if (set->val.nodes[i].node->schema->nodetype & LYS_ANYDATA) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6101 | continue; |
| 6102 | } |
| 6103 | |
| 6104 | /* add all the children */ |
| 6105 | 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] | 6106 | set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6107 | if (rc != LY_SUCCESS) { |
| 6108 | set_free_content(&ret_set); |
| 6109 | return rc; |
| 6110 | } |
| 6111 | } |
| 6112 | |
| 6113 | /* use the temporary set as the current one */ |
| 6114 | ret_set.ctx_pos = set->ctx_pos; |
| 6115 | ret_set.ctx_size = set->ctx_size; |
| 6116 | set_free_content(set); |
| 6117 | memcpy(set, &ret_set, sizeof *set); |
| 6118 | |
| 6119 | return LY_SUCCESS; |
| 6120 | } |
| 6121 | |
| 6122 | /** |
| 6123 | * @brief Move context schema @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_SCNODE_SET |
| 6124 | * (or LYXP_SET_EMPTY). |
| 6125 | * |
| 6126 | * @param[in,out] set Set to use. |
| 6127 | * @param[in] all_desc Whether to go to all descendants ('//') or not ('/'). |
| 6128 | * @param[in] options XPath options. |
| 6129 | * @return LY_ERR |
| 6130 | */ |
| 6131 | static LY_ERR |
| 6132 | moveto_scnode_self(struct lyxp_set *set, int all_desc, int options) |
| 6133 | { |
| 6134 | const struct lysc_node *sub; |
| 6135 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6136 | |
| 6137 | if (!set || (set->type == LYXP_SET_EMPTY)) { |
| 6138 | return LY_SUCCESS; |
| 6139 | } |
| 6140 | |
| 6141 | if (set->type != LYXP_SET_SCNODE_SET) { |
| 6142 | LOGVAL(set->ctx, LY_VLOG_LYS, set->ctx_scnode, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 6143 | return LY_EVALID; |
| 6144 | } |
| 6145 | |
| 6146 | /* nothing to do */ |
| 6147 | if (!all_desc) { |
| 6148 | return LY_SUCCESS; |
| 6149 | } |
| 6150 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6151 | /* add all the children, they get added recursively */ |
| 6152 | for (i = 0; i < set->used; ++i) { |
| 6153 | if (set->val.scnodes[i].in_ctx != 1) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6154 | if (set->val.scnodes[i].in_ctx != -2) { |
| 6155 | continue; |
| 6156 | } |
| 6157 | |
| 6158 | /* remember context node (it was traversed again so it changes to a normal node) */ |
| 6159 | set->val.scnodes[i].in_ctx = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6160 | } |
| 6161 | |
| 6162 | /* add all the children */ |
| 6163 | if (set->val.scnodes[i].scnode->nodetype & (LYS_LIST | LYS_CONTAINER)) { |
| 6164 | sub = NULL; |
| 6165 | while ((sub = lys_getnext(sub, set->val.scnodes[i].scnode, NULL, LYS_GETNEXT_NOSTATECHECK))) { |
| 6166 | /* RPC input/output check */ |
| 6167 | if (options & LYXP_SCNODE_OUTPUT) { |
| 6168 | if (sub->parent->nodetype == LYS_INPUT) { |
| 6169 | continue; |
| 6170 | } |
| 6171 | } else { |
| 6172 | if (sub->parent->nodetype == LYS_OUTPUT) { |
| 6173 | continue; |
| 6174 | } |
| 6175 | } |
| 6176 | |
| 6177 | /* context check */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6178 | if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (sub->flags & LYS_CONFIG_R)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6179 | continue; |
| 6180 | } |
| 6181 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6182 | lyxp_set_scnode_insert_node(set, sub, LYXP_NODE_ELEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6183 | /* throw away the insert index, we want to consider that node again, recursively */ |
| 6184 | } |
| 6185 | } |
| 6186 | } |
| 6187 | |
| 6188 | return LY_SUCCESS; |
| 6189 | } |
| 6190 | |
| 6191 | /** |
| 6192 | * @brief Move context @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_NODE_SET |
| 6193 | * (or LYXP_SET_EMPTY). Context position aware. |
| 6194 | * |
| 6195 | * @param[in] set Set to use. |
| 6196 | * @param[in] all_desc Whether to go to all descendants ('//') or not ('/'). |
| 6197 | * @param[in] options XPath options. |
| 6198 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6199 | */ |
| 6200 | static LY_ERR |
| 6201 | moveto_parent(struct lyxp_set *set, int all_desc, int options) |
| 6202 | { |
| 6203 | LY_ERR rc; |
| 6204 | uint32_t i; |
| 6205 | struct lyd_node *node, *new_node; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6206 | enum lyxp_node_type new_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6207 | |
| 6208 | if (!set || (set->type == LYXP_SET_EMPTY)) { |
| 6209 | return LY_SUCCESS; |
| 6210 | } |
| 6211 | |
| 6212 | if (set->type != LYXP_SET_NODE_SET) { |
| 6213 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 6214 | return LY_EVALID; |
| 6215 | } |
| 6216 | |
| 6217 | if (all_desc) { |
| 6218 | /* <path>//.. == <path>//./.. */ |
| 6219 | rc = moveto_self(set, 1, options); |
| 6220 | LY_CHECK_RET(rc); |
| 6221 | } |
| 6222 | |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 6223 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6224 | node = set->val.nodes[i].node; |
| 6225 | |
| 6226 | if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
| 6227 | new_node = (struct lyd_node *)node->parent; |
| 6228 | } else if (set->val.nodes[i].type == LYXP_NODE_TEXT) { |
| 6229 | new_node = node; |
| 6230 | } else if (set->val.nodes[i].type == LYXP_NODE_ATTR) { |
| 6231 | new_node = set->val.attrs[i].attr->parent; |
| 6232 | if (!new_node) { |
| 6233 | LOGINT_RET(set->ctx); |
| 6234 | } |
| 6235 | } else { |
| 6236 | /* root does not have a parent */ |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6237 | set_remove_node_none(set, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6238 | continue; |
| 6239 | } |
| 6240 | |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 6241 | /* when check */ |
| 6242 | if (moveto_when_check(new_node)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6243 | return LY_EINCOMPLETE; |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 6244 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6245 | |
| 6246 | /* node already there can also be the root */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6247 | if (!new_node) { |
| 6248 | new_type = set->root_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6249 | |
| 6250 | /* node has a standard parent (it can equal the root, it's not the root yet since they are fake) */ |
| 6251 | } else { |
| 6252 | new_type = LYXP_NODE_ELEM; |
| 6253 | } |
| 6254 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6255 | if (set_dup_node_check(set, new_node, new_type, -1)) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6256 | set_remove_node_none(set, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6257 | } else { |
| 6258 | set_replace_node(set, new_node, 0, new_type, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6259 | } |
| 6260 | } |
| 6261 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6262 | set_remove_nodes_none(set); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6263 | assert(!set_sort(set) && !set_sorted_dup_node_clean(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6264 | |
| 6265 | return LY_SUCCESS; |
| 6266 | } |
| 6267 | |
| 6268 | /** |
| 6269 | * @brief Move context schema @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_SCNODE_SET |
| 6270 | * (or LYXP_SET_EMPTY). |
| 6271 | * |
| 6272 | * @param[in] set Set to use. |
| 6273 | * @param[in] all_desc Whether to go to all descendants ('//') or not ('/'). |
| 6274 | * @param[in] options XPath options. |
| 6275 | * @return LY_ERR |
| 6276 | */ |
| 6277 | static LY_ERR |
| 6278 | moveto_scnode_parent(struct lyxp_set *set, int all_desc, int options) |
| 6279 | { |
| 6280 | int idx, i, orig_used, temp_ctx = 0; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6281 | const struct lysc_node *node, *new_node; |
| 6282 | enum lyxp_node_type new_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6283 | LY_ERR rc; |
| 6284 | |
| 6285 | if (!set || (set->type == LYXP_SET_EMPTY)) { |
| 6286 | return LY_SUCCESS; |
| 6287 | } |
| 6288 | |
| 6289 | if (set->type != LYXP_SET_SCNODE_SET) { |
| 6290 | LOGVAL(set->ctx, LY_VLOG_LYS, set->ctx_scnode, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 6291 | return LY_EVALID; |
| 6292 | } |
| 6293 | |
| 6294 | if (all_desc) { |
| 6295 | /* <path>//.. == <path>//./.. */ |
| 6296 | rc = moveto_scnode_self(set, 1, options); |
| 6297 | LY_CHECK_RET(rc); |
| 6298 | } |
| 6299 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6300 | orig_used = set->used; |
| 6301 | for (i = 0; i < orig_used; ++i) { |
| 6302 | if (set->val.scnodes[i].in_ctx != 1) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6303 | if (set->val.scnodes[i].in_ctx != -2) { |
| 6304 | continue; |
| 6305 | } |
| 6306 | |
| 6307 | /* remember context node */ |
| 6308 | set->val.scnodes[i].in_ctx = -1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6309 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6310 | |
| 6311 | node = set->val.scnodes[i].scnode; |
| 6312 | |
| 6313 | if (set->val.scnodes[i].type == LYXP_NODE_ELEM) { |
| 6314 | for (new_node = node->parent; |
| 6315 | new_node && (new_node->nodetype & (LYS_CHOICE | LYS_CASE)); |
| 6316 | new_node = new_node->parent); |
| 6317 | } else { |
| 6318 | /* root does not have a parent */ |
| 6319 | continue; |
| 6320 | } |
| 6321 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6322 | /* node has no parent */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6323 | if (!new_node) { |
| 6324 | new_type = set->root_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6325 | |
| 6326 | /* node has a standard parent (it can equal the root, it's not the root yet since they are fake) */ |
| 6327 | } else { |
| 6328 | new_type = LYXP_NODE_ELEM; |
| 6329 | } |
| 6330 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6331 | idx = lyxp_set_scnode_insert_node(set, new_node, new_type); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6332 | if ((idx < orig_used) && (idx > i)) { |
| 6333 | set->val.scnodes[idx].in_ctx = 2; |
| 6334 | temp_ctx = 1; |
| 6335 | } |
| 6336 | } |
| 6337 | |
| 6338 | if (temp_ctx) { |
| 6339 | for (i = 0; i < orig_used; ++i) { |
| 6340 | if (set->val.scnodes[i].in_ctx == 2) { |
| 6341 | set->val.scnodes[i].in_ctx = 1; |
| 6342 | } |
| 6343 | } |
| 6344 | } |
| 6345 | |
| 6346 | return LY_SUCCESS; |
| 6347 | } |
| 6348 | |
| 6349 | /** |
| 6350 | * @brief Move context @p set to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'. |
| 6351 | * Result is LYXP_SET_BOOLEAN. Indirectly context position aware. |
| 6352 | * |
| 6353 | * @param[in,out] set1 Set to use for the result. |
| 6354 | * @param[in] set2 Set acting as the second operand for @p op. |
| 6355 | * @param[in] op Comparison operator to process. |
| 6356 | * @param[in] options XPath options. |
| 6357 | * @return LY_ERR |
| 6358 | */ |
| 6359 | static LY_ERR |
| 6360 | moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op, int options) |
| 6361 | { |
| 6362 | /* |
| 6363 | * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING |
| 6364 | * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING) |
| 6365 | * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 6366 | * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 6367 | * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING |
| 6368 | * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6369 | * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 6370 | * |
| 6371 | * '=' or '!=' |
| 6372 | * BOOLEAN + BOOLEAN |
| 6373 | * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 6374 | * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 6375 | * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 6376 | * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 6377 | * NUMBER + NUMBER |
| 6378 | * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6379 | * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 6380 | * STRING + STRING |
| 6381 | * |
| 6382 | * '<=', '<', '>=', '>' |
| 6383 | * NUMBER + NUMBER |
| 6384 | * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 6385 | * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 6386 | * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 6387 | * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6388 | * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 6389 | * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 6390 | * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6391 | * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6392 | */ |
| 6393 | struct lyxp_set iter1, iter2; |
| 6394 | int result; |
| 6395 | int64_t i; |
| 6396 | LY_ERR rc; |
| 6397 | |
| 6398 | iter1.type = LYXP_SET_EMPTY; |
| 6399 | |
| 6400 | /* empty node-sets are always false */ |
| 6401 | if ((set1->type == LYXP_SET_EMPTY) || (set2->type == LYXP_SET_EMPTY)) { |
| 6402 | set_fill_boolean(set1, 0); |
| 6403 | return LY_SUCCESS; |
| 6404 | } |
| 6405 | |
| 6406 | /* iterative evaluation with node-sets */ |
| 6407 | if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) { |
| 6408 | if (set1->type == LYXP_SET_NODE_SET) { |
| 6409 | for (i = 0; i < set1->used; ++i) { |
| 6410 | switch (set2->type) { |
| 6411 | case LYXP_SET_NUMBER: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6412 | rc = set_comp_cast(&iter1, set1, LYXP_SET_NUMBER, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6413 | break; |
| 6414 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6415 | rc = set_comp_cast(&iter1, set1, LYXP_SET_BOOLEAN, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6416 | break; |
| 6417 | default: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6418 | rc = set_comp_cast(&iter1, set1, LYXP_SET_STRING, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6419 | break; |
| 6420 | } |
| 6421 | LY_CHECK_RET(rc); |
| 6422 | |
| 6423 | rc = moveto_op_comp(&iter1, set2, op, options); |
| 6424 | if (rc != LY_SUCCESS) { |
| 6425 | set_free_content(&iter1); |
| 6426 | return rc; |
| 6427 | } |
| 6428 | |
| 6429 | /* lazy evaluation until true */ |
| 6430 | if (iter1.val.bool) { |
| 6431 | set_fill_boolean(set1, 1); |
| 6432 | return LY_SUCCESS; |
| 6433 | } |
| 6434 | } |
| 6435 | } else { |
| 6436 | for (i = 0; i < set2->used; ++i) { |
| 6437 | switch (set1->type) { |
| 6438 | case LYXP_SET_NUMBER: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6439 | rc = set_comp_cast(&iter2, set2, LYXP_SET_NUMBER, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6440 | break; |
| 6441 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6442 | rc = set_comp_cast(&iter2, set2, LYXP_SET_BOOLEAN, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6443 | break; |
| 6444 | default: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6445 | rc = set_comp_cast(&iter2, set2, LYXP_SET_STRING, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6446 | break; |
| 6447 | } |
| 6448 | LY_CHECK_RET(rc); |
| 6449 | |
| 6450 | set_fill_set(&iter1, set1); |
| 6451 | |
| 6452 | rc = moveto_op_comp(&iter1, &iter2, op, options); |
| 6453 | if (rc != LY_SUCCESS) { |
| 6454 | set_free_content(&iter1); |
| 6455 | set_free_content(&iter2); |
| 6456 | return rc; |
| 6457 | } |
| 6458 | set_free_content(&iter2); |
| 6459 | |
| 6460 | /* lazy evaluation until true */ |
| 6461 | if (iter1.val.bool) { |
| 6462 | set_fill_boolean(set1, 1); |
| 6463 | return LY_SUCCESS; |
| 6464 | } |
| 6465 | } |
| 6466 | } |
| 6467 | |
| 6468 | /* false for all nodes */ |
| 6469 | set_fill_boolean(set1, 0); |
| 6470 | return LY_SUCCESS; |
| 6471 | } |
| 6472 | |
| 6473 | /* first convert properly */ |
| 6474 | if ((op[0] == '=') || (op[0] == '!')) { |
| 6475 | if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6476 | lyxp_set_cast(set1, LYXP_SET_BOOLEAN); |
| 6477 | lyxp_set_cast(set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6478 | } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6479 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6480 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6481 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6482 | LY_CHECK_RET(rc); |
| 6483 | } /* else we have 2 strings */ |
| 6484 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6485 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6486 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6487 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6488 | LY_CHECK_RET(rc); |
| 6489 | } |
| 6490 | |
| 6491 | assert(set1->type == set2->type); |
| 6492 | |
| 6493 | /* compute result */ |
| 6494 | if (op[0] == '=') { |
| 6495 | if (set1->type == LYXP_SET_BOOLEAN) { |
| 6496 | result = (set1->val.bool == set2->val.bool); |
| 6497 | } else if (set1->type == LYXP_SET_NUMBER) { |
| 6498 | result = (set1->val.num == set2->val.num); |
| 6499 | } else { |
| 6500 | assert(set1->type == LYXP_SET_STRING); |
Michal Vasko | ac6c72f | 2019-11-14 16:09:34 +0100 | [diff] [blame] | 6501 | result = !strcmp(set1->val.str, set2->val.str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6502 | } |
| 6503 | } else if (op[0] == '!') { |
| 6504 | if (set1->type == LYXP_SET_BOOLEAN) { |
| 6505 | result = (set1->val.bool != set2->val.bool); |
| 6506 | } else if (set1->type == LYXP_SET_NUMBER) { |
| 6507 | result = (set1->val.num != set2->val.num); |
| 6508 | } else { |
| 6509 | assert(set1->type == LYXP_SET_STRING); |
Michal Vasko | ac6c72f | 2019-11-14 16:09:34 +0100 | [diff] [blame] | 6510 | result = !strcmp(set1->val.str, set2->val.str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6511 | } |
| 6512 | } else { |
| 6513 | assert(set1->type == LYXP_SET_NUMBER); |
| 6514 | if (op[0] == '<') { |
| 6515 | if (op[1] == '=') { |
| 6516 | result = (set1->val.num <= set2->val.num); |
| 6517 | } else { |
| 6518 | result = (set1->val.num < set2->val.num); |
| 6519 | } |
| 6520 | } else { |
| 6521 | if (op[1] == '=') { |
| 6522 | result = (set1->val.num >= set2->val.num); |
| 6523 | } else { |
| 6524 | result = (set1->val.num > set2->val.num); |
| 6525 | } |
| 6526 | } |
| 6527 | } |
| 6528 | |
| 6529 | /* assign result */ |
| 6530 | if (result) { |
| 6531 | set_fill_boolean(set1, 1); |
| 6532 | } else { |
| 6533 | set_fill_boolean(set1, 0); |
| 6534 | } |
| 6535 | |
| 6536 | return LY_SUCCESS; |
| 6537 | } |
| 6538 | |
| 6539 | /** |
| 6540 | * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div', |
| 6541 | * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware. |
| 6542 | * |
| 6543 | * @param[in,out] set1 Set to use for the result. |
| 6544 | * @param[in] set2 Set acting as the second operand for @p op. |
| 6545 | * @param[in] op Operator to process. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6546 | * @return LY_ERR |
| 6547 | */ |
| 6548 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6549 | 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] | 6550 | { |
| 6551 | LY_ERR rc; |
| 6552 | |
| 6553 | /* unary '-' */ |
| 6554 | if (!set2 && (op[0] == '-')) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6555 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6556 | LY_CHECK_RET(rc); |
| 6557 | set1->val.num *= -1; |
| 6558 | lyxp_set_free(set2); |
| 6559 | return LY_SUCCESS; |
| 6560 | } |
| 6561 | |
| 6562 | assert(set1 && set2); |
| 6563 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6564 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6565 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6566 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6567 | LY_CHECK_RET(rc); |
| 6568 | |
| 6569 | switch (op[0]) { |
| 6570 | /* '+' */ |
| 6571 | case '+': |
| 6572 | set1->val.num += set2->val.num; |
| 6573 | break; |
| 6574 | |
| 6575 | /* '-' */ |
| 6576 | case '-': |
| 6577 | set1->val.num -= set2->val.num; |
| 6578 | break; |
| 6579 | |
| 6580 | /* '*' */ |
| 6581 | case '*': |
| 6582 | set1->val.num *= set2->val.num; |
| 6583 | break; |
| 6584 | |
| 6585 | /* 'div' */ |
| 6586 | case 'd': |
| 6587 | set1->val.num /= set2->val.num; |
| 6588 | break; |
| 6589 | |
| 6590 | /* 'mod' */ |
| 6591 | case 'm': |
| 6592 | set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num); |
| 6593 | break; |
| 6594 | |
| 6595 | default: |
| 6596 | LOGINT_RET(set1->ctx); |
| 6597 | } |
| 6598 | |
| 6599 | return LY_SUCCESS; |
| 6600 | } |
| 6601 | |
| 6602 | /* |
| 6603 | * eval functions |
| 6604 | * |
| 6605 | * They execute a parsed XPath expression on some data subtree. |
| 6606 | */ |
| 6607 | |
| 6608 | /** |
| 6609 | * @brief Evaluate Literal. Logs directly on error. |
| 6610 | * |
| 6611 | * @param[in] exp Parsed XPath expression. |
| 6612 | * @param[in] exp_idx Position in the expression @p exp. |
| 6613 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 6614 | */ |
| 6615 | static void |
| 6616 | eval_literal(struct lyxp_expr *exp, uint16_t *exp_idx, struct lyxp_set *set) |
| 6617 | { |
| 6618 | if (set) { |
| 6619 | if (exp->tok_len[*exp_idx] == 2) { |
| 6620 | set_fill_string(set, "", 0); |
| 6621 | } else { |
| 6622 | set_fill_string(set, &exp->expr[exp->tok_pos[*exp_idx] + 1], exp->tok_len[*exp_idx] - 2); |
| 6623 | } |
| 6624 | } |
| 6625 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
| 6626 | print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]); |
| 6627 | ++(*exp_idx); |
| 6628 | } |
| 6629 | |
| 6630 | /** |
| 6631 | * @brief Evaluate NodeTest. Logs directly on error. |
| 6632 | * |
| 6633 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 6634 | * |
| 6635 | * @param[in] exp Parsed XPath expression. |
| 6636 | * @param[in] exp_idx Position in the expression @p exp. |
| 6637 | * @param[in] attr_axis Whether to search attributes or standard nodes. |
| 6638 | * @param[in] all_desc Whether to search all the descendants or children only. |
| 6639 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 6640 | * @param[in] options XPath options. |
| 6641 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6642 | */ |
| 6643 | static int |
| 6644 | eval_node_test(struct lyxp_expr *exp, uint16_t *exp_idx, int attr_axis, int all_desc, |
| 6645 | struct lyxp_set *set, int options) |
| 6646 | { |
| 6647 | int i; |
| 6648 | char *path; |
| 6649 | LY_ERR rc; |
| 6650 | |
| 6651 | switch (exp->tokens[*exp_idx]) { |
| 6652 | case LYXP_TOKEN_NAMETEST: |
| 6653 | if (attr_axis) { |
| 6654 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 6655 | set_scnode_clear_ctx(set); |
| 6656 | rc = LY_SUCCESS; |
| 6657 | } else { |
| 6658 | if (all_desc) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6659 | rc = moveto_attr_alldesc(set, &exp->expr[exp->tok_pos[*exp_idx]], exp->tok_len[*exp_idx]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6660 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6661 | rc = moveto_attr(set, &exp->expr[exp->tok_pos[*exp_idx]], exp->tok_len[*exp_idx]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6662 | } |
| 6663 | } |
| 6664 | } else { |
| 6665 | if (all_desc) { |
| 6666 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 6667 | rc = moveto_scnode_alldesc(set, &exp->expr[exp->tok_pos[*exp_idx]], exp->tok_len[*exp_idx], options); |
| 6668 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6669 | rc = moveto_node_alldesc(set, &exp->expr[exp->tok_pos[*exp_idx]], exp->tok_len[*exp_idx]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6670 | } |
| 6671 | } else { |
| 6672 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 6673 | rc = moveto_scnode(set, &exp->expr[exp->tok_pos[*exp_idx]], exp->tok_len[*exp_idx], options); |
| 6674 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6675 | rc = moveto_node(set, &exp->expr[exp->tok_pos[*exp_idx]], exp->tok_len[*exp_idx]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6676 | } |
| 6677 | } |
| 6678 | |
| 6679 | if ((rc == LY_SUCCESS) && set && (options & LYXP_SCNODE_ALL)) { |
| 6680 | for (i = set->used - 1; i > -1; --i) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6681 | if (set->val.scnodes[i].in_ctx > 0) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6682 | break; |
| 6683 | } |
| 6684 | } |
| 6685 | if (i == -1) { |
| 6686 | path = lysc_path(set->ctx_scnode, LYSC_PATH_LOG, NULL, 0); |
| 6687 | LOGWRN(set->ctx, "Schema node \"%.*s\" not found (%.*s) with context node \"%s\".", |
| 6688 | exp->tok_len[*exp_idx], &exp->expr[exp->tok_pos[*exp_idx]], |
| 6689 | exp->tok_pos[*exp_idx] + exp->tok_len[*exp_idx], exp->expr, path); |
| 6690 | free(path); |
| 6691 | } |
| 6692 | } |
| 6693 | } |
| 6694 | LY_CHECK_RET(rc); |
| 6695 | |
| 6696 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
| 6697 | print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]); |
| 6698 | ++(*exp_idx); |
| 6699 | break; |
| 6700 | |
| 6701 | case LYXP_TOKEN_NODETYPE: |
| 6702 | if (set) { |
| 6703 | assert(exp->tok_len[*exp_idx] == 4); |
| 6704 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 6705 | set_scnode_clear_ctx(set); |
| 6706 | /* just for the debug message underneath */ |
| 6707 | set = NULL; |
| 6708 | } else { |
| 6709 | if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "node", 4)) { |
| 6710 | rc = xpath_node(NULL, 0, set, options); |
| 6711 | LY_CHECK_RET(rc); |
| 6712 | } else { |
| 6713 | assert(!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "text", 4)); |
| 6714 | rc = xpath_text(NULL, 0, set, options); |
| 6715 | LY_CHECK_RET(rc); |
| 6716 | } |
| 6717 | } |
| 6718 | } |
| 6719 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
| 6720 | print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]); |
| 6721 | ++(*exp_idx); |
| 6722 | |
| 6723 | /* '(' */ |
| 6724 | assert(exp->tokens[*exp_idx] == LYXP_TOKEN_PAR1); |
| 6725 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
| 6726 | print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]); |
| 6727 | ++(*exp_idx); |
| 6728 | |
| 6729 | /* ')' */ |
| 6730 | assert(exp->tokens[*exp_idx] == LYXP_TOKEN_PAR2); |
| 6731 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
| 6732 | print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]); |
| 6733 | ++(*exp_idx); |
| 6734 | break; |
| 6735 | |
| 6736 | default: |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 6737 | LOGINT_RET(set ? set->ctx : NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6738 | } |
| 6739 | |
| 6740 | return LY_SUCCESS; |
| 6741 | } |
| 6742 | |
| 6743 | /** |
| 6744 | * @brief Evaluate Predicate. Logs directly on error. |
| 6745 | * |
| 6746 | * [7] Predicate ::= '[' Expr ']' |
| 6747 | * |
| 6748 | * @param[in] exp Parsed XPath expression. |
| 6749 | * @param[in] exp_idx Position in the expression @p exp. |
| 6750 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 6751 | * @param[in] options XPath options. |
| 6752 | * @param[in] parent_pos_pred Whether parent predicate was a positional one. |
| 6753 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6754 | */ |
| 6755 | static LY_ERR |
| 6756 | eval_predicate(struct lyxp_expr *exp, uint16_t *exp_idx, struct lyxp_set *set, int options, int parent_pos_pred) |
| 6757 | { |
| 6758 | LY_ERR rc; |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 6759 | uint16_t i, orig_exp; |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6760 | uint32_t orig_pos, orig_size; |
| 6761 | int32_t pred_in_ctx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6762 | struct lyxp_set set2; |
| 6763 | struct lyd_node *orig_parent; |
| 6764 | |
| 6765 | /* '[' */ |
| 6766 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
| 6767 | print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]); |
| 6768 | ++(*exp_idx); |
| 6769 | |
| 6770 | if (!set) { |
| 6771 | only_parse: |
| 6772 | rc = eval_expr_select(exp, exp_idx, 0, NULL, options); |
| 6773 | LY_CHECK_RET(rc); |
| 6774 | } else if (set->type == LYXP_SET_NODE_SET) { |
| 6775 | /* 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] | 6776 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6777 | |
| 6778 | /* empty set, nothing to evaluate */ |
| 6779 | if (!set->used) { |
| 6780 | goto only_parse; |
| 6781 | } |
| 6782 | |
| 6783 | orig_exp = *exp_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6784 | orig_pos = 0; |
| 6785 | orig_size = set->used; |
| 6786 | orig_parent = NULL; |
| 6787 | for (i = 0; i < set->used; ) { |
| 6788 | set_init(&set2, set); |
| 6789 | set_insert_node(&set2, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, 0); |
| 6790 | /* remember the node context position for position() and context size for last(), |
| 6791 | * predicates should always be evaluated with respect to the child axis (since we do |
| 6792 | * not support explicit axes) so we assign positions based on their parents */ |
| 6793 | if (parent_pos_pred && ((struct lyd_node *)set->val.nodes[i].node->parent != orig_parent)) { |
| 6794 | orig_parent = (struct lyd_node *)set->val.nodes[i].node->parent; |
| 6795 | orig_pos = 1; |
| 6796 | } else { |
| 6797 | ++orig_pos; |
| 6798 | } |
| 6799 | |
| 6800 | set2.ctx_pos = orig_pos; |
| 6801 | set2.ctx_size = orig_size; |
| 6802 | *exp_idx = orig_exp; |
| 6803 | |
| 6804 | rc = eval_expr_select(exp, exp_idx, 0, &set2, options); |
| 6805 | if (rc != LY_SUCCESS) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6806 | lyxp_set_cast(&set2, LYXP_SET_EMPTY); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6807 | return rc; |
| 6808 | } |
| 6809 | |
| 6810 | /* number is a position */ |
| 6811 | if (set2.type == LYXP_SET_NUMBER) { |
| 6812 | if ((long long)set2.val.num == orig_pos) { |
| 6813 | set2.val.num = 1; |
| 6814 | } else { |
| 6815 | set2.val.num = 0; |
| 6816 | } |
| 6817 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6818 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6819 | |
| 6820 | /* predicate satisfied or not? */ |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 6821 | if (!set2.val.bool) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6822 | set_remove_node_none(set, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6823 | } |
| 6824 | } |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6825 | set_remove_nodes_none(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6826 | |
| 6827 | } else if (set->type == LYXP_SET_SCNODE_SET) { |
| 6828 | for (i = 0; i < set->used; ++i) { |
| 6829 | if (set->val.scnodes[i].in_ctx == 1) { |
| 6830 | /* there is a currently-valid node */ |
| 6831 | break; |
| 6832 | } |
| 6833 | } |
| 6834 | /* empty set, nothing to evaluate */ |
| 6835 | if (i == set->used) { |
| 6836 | goto only_parse; |
| 6837 | } |
| 6838 | |
| 6839 | orig_exp = *exp_idx; |
| 6840 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6841 | /* set special in_ctx to all the valid snodes */ |
| 6842 | pred_in_ctx = set_scnode_new_in_ctx(set); |
| 6843 | |
| 6844 | /* use the valid snodes one-by-one */ |
| 6845 | for (i = 0; i < set->used; ++i) { |
| 6846 | if (set->val.scnodes[i].in_ctx != pred_in_ctx) { |
| 6847 | continue; |
| 6848 | } |
| 6849 | set->val.scnodes[i].in_ctx = 1; |
| 6850 | |
| 6851 | *exp_idx = orig_exp; |
| 6852 | |
| 6853 | rc = eval_expr_select(exp, exp_idx, 0, set, options); |
| 6854 | LY_CHECK_RET(rc); |
| 6855 | |
| 6856 | set->val.scnodes[i].in_ctx = pred_in_ctx; |
| 6857 | } |
| 6858 | |
| 6859 | /* restore the state as it was before the predicate */ |
| 6860 | for (i = 0; i < set->used; ++i) { |
| 6861 | if (set->val.scnodes[i].in_ctx == 1) { |
| 6862 | set->val.scnodes[i].in_ctx = 0; |
| 6863 | } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) { |
| 6864 | set->val.scnodes[i].in_ctx = 1; |
| 6865 | } |
| 6866 | } |
| 6867 | |
| 6868 | } else { |
| 6869 | set2.type = LYXP_SET_EMPTY; |
| 6870 | set_fill_set(&set2, set); |
| 6871 | |
| 6872 | rc = eval_expr_select(exp, exp_idx, 0, &set2, options); |
| 6873 | if (rc != LY_SUCCESS) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6874 | lyxp_set_cast(&set2, LYXP_SET_EMPTY); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6875 | return rc; |
| 6876 | } |
| 6877 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6878 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6879 | if (!set2.val.bool) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6880 | lyxp_set_cast(set, LYXP_SET_EMPTY); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6881 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6882 | lyxp_set_cast(&set2, LYXP_SET_EMPTY); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6883 | } |
| 6884 | |
| 6885 | /* ']' */ |
| 6886 | assert(exp->tokens[*exp_idx] == LYXP_TOKEN_BRACK2); |
| 6887 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
| 6888 | print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]); |
| 6889 | ++(*exp_idx); |
| 6890 | |
| 6891 | return LY_SUCCESS; |
| 6892 | } |
| 6893 | |
| 6894 | /** |
| 6895 | * @brief Evaluate RelativeLocationPath. Logs directly on error. |
| 6896 | * |
| 6897 | * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step |
| 6898 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 6899 | * |
| 6900 | * @param[in] exp Parsed XPath expression. |
| 6901 | * @param[in] exp_idx Position in the expression @p exp. |
| 6902 | * @param[in] all_desc Whether to search all the descendants or children only. |
| 6903 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 6904 | * @param[in] options XPath options. |
| 6905 | * @return LY_ERR (YL_EINCOMPLETE on unresolved when) |
| 6906 | */ |
| 6907 | static LY_ERR |
| 6908 | eval_relative_location_path(struct lyxp_expr *exp, uint16_t *exp_idx, int all_desc, struct lyxp_set *set, int options) |
| 6909 | { |
| 6910 | int attr_axis; |
| 6911 | LY_ERR rc; |
| 6912 | |
| 6913 | goto step; |
| 6914 | do { |
| 6915 | /* evaluate '/' or '//' */ |
| 6916 | if (exp->tok_len[*exp_idx] == 1) { |
| 6917 | all_desc = 0; |
| 6918 | } else { |
| 6919 | assert(exp->tok_len[*exp_idx] == 2); |
| 6920 | all_desc = 1; |
| 6921 | } |
| 6922 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
| 6923 | print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]); |
| 6924 | ++(*exp_idx); |
| 6925 | |
| 6926 | step: |
| 6927 | /* Step */ |
| 6928 | attr_axis = 0; |
| 6929 | switch (exp->tokens[*exp_idx]) { |
| 6930 | case LYXP_TOKEN_DOT: |
| 6931 | /* evaluate '.' */ |
| 6932 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 6933 | rc = moveto_scnode_self(set, all_desc, options); |
| 6934 | } else { |
| 6935 | rc = moveto_self(set, all_desc, options); |
| 6936 | } |
| 6937 | LY_CHECK_RET(rc); |
| 6938 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
| 6939 | print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]); |
| 6940 | ++(*exp_idx); |
| 6941 | break; |
| 6942 | |
| 6943 | case LYXP_TOKEN_DDOT: |
| 6944 | /* evaluate '..' */ |
| 6945 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 6946 | rc = moveto_scnode_parent(set, all_desc, options); |
| 6947 | } else { |
| 6948 | rc = moveto_parent(set, all_desc, options); |
| 6949 | } |
| 6950 | LY_CHECK_RET(rc); |
| 6951 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
| 6952 | print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]); |
| 6953 | ++(*exp_idx); |
| 6954 | break; |
| 6955 | |
| 6956 | case LYXP_TOKEN_AT: |
| 6957 | /* evaluate '@' */ |
| 6958 | attr_axis = 1; |
| 6959 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
| 6960 | print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]); |
| 6961 | ++(*exp_idx); |
| 6962 | |
| 6963 | /* fall through */ |
| 6964 | case LYXP_TOKEN_NAMETEST: |
| 6965 | case LYXP_TOKEN_NODETYPE: |
| 6966 | rc = eval_node_test(exp, exp_idx, attr_axis, all_desc, set, options); |
| 6967 | LY_CHECK_RET(rc); |
| 6968 | |
| 6969 | while ((exp->used > *exp_idx) && (exp->tokens[*exp_idx] == LYXP_TOKEN_BRACK1)) { |
| 6970 | rc = eval_predicate(exp, exp_idx, set, options, 1); |
| 6971 | LY_CHECK_RET(rc); |
| 6972 | } |
| 6973 | break; |
| 6974 | |
| 6975 | default: |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 6976 | LOGINT_RET(set ? set->ctx : NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6977 | } |
| 6978 | } while ((exp->used > *exp_idx) && (exp->tokens[*exp_idx] == LYXP_TOKEN_OPERATOR_PATH)); |
| 6979 | |
| 6980 | return LY_SUCCESS; |
| 6981 | } |
| 6982 | |
| 6983 | /** |
| 6984 | * @brief Evaluate AbsoluteLocationPath. Logs directly on error. |
| 6985 | * |
| 6986 | * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath |
| 6987 | * |
| 6988 | * @param[in] exp Parsed XPath expression. |
| 6989 | * @param[in] exp_idx Position in the expression @p exp. |
| 6990 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 6991 | * @param[in] options XPath options. |
| 6992 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6993 | */ |
| 6994 | static LY_ERR |
| 6995 | eval_absolute_location_path(struct lyxp_expr *exp, uint16_t *exp_idx, struct lyxp_set *set, int options) |
| 6996 | { |
| 6997 | int all_desc; |
| 6998 | LY_ERR rc; |
| 6999 | |
| 7000 | if (set) { |
| 7001 | /* no matter what tokens follow, we need to be at the root */ |
| 7002 | moveto_root(set, options); |
| 7003 | } |
| 7004 | |
| 7005 | /* '/' RelativeLocationPath? */ |
| 7006 | if (exp->tok_len[*exp_idx] == 1) { |
| 7007 | /* evaluate '/' - deferred */ |
| 7008 | all_desc = 0; |
| 7009 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
| 7010 | print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]); |
| 7011 | ++(*exp_idx); |
| 7012 | |
Michal Vasko | 4b9e105 | 2019-09-13 11:25:37 +0200 | [diff] [blame] | 7013 | if (exp_check_token(set ? set->ctx : NULL, exp, *exp_idx, LYXP_TOKEN_NONE, 0)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7014 | return LY_SUCCESS; |
| 7015 | } |
| 7016 | switch (exp->tokens[*exp_idx]) { |
| 7017 | case LYXP_TOKEN_DOT: |
| 7018 | case LYXP_TOKEN_DDOT: |
| 7019 | case LYXP_TOKEN_AT: |
| 7020 | case LYXP_TOKEN_NAMETEST: |
| 7021 | case LYXP_TOKEN_NODETYPE: |
| 7022 | rc = eval_relative_location_path(exp, exp_idx, all_desc, set, options); |
| 7023 | LY_CHECK_RET(rc); |
| 7024 | break; |
| 7025 | default: |
| 7026 | break; |
| 7027 | } |
| 7028 | |
| 7029 | /* '//' RelativeLocationPath */ |
| 7030 | } else { |
| 7031 | /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */ |
| 7032 | all_desc = 1; |
| 7033 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
| 7034 | print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]); |
| 7035 | ++(*exp_idx); |
| 7036 | |
| 7037 | rc = eval_relative_location_path(exp, exp_idx, all_desc, set, options); |
| 7038 | LY_CHECK_RET(rc); |
| 7039 | } |
| 7040 | |
| 7041 | return LY_SUCCESS; |
| 7042 | } |
| 7043 | |
| 7044 | /** |
| 7045 | * @brief Evaluate FunctionCall. Logs directly on error. |
| 7046 | * |
| 7047 | * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')' |
| 7048 | * |
| 7049 | * @param[in] exp Parsed XPath expression. |
| 7050 | * @param[in] exp_idx Position in the expression @p exp. |
| 7051 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7052 | * @param[in] options XPath options. |
| 7053 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7054 | */ |
| 7055 | static LY_ERR |
| 7056 | eval_function_call(struct lyxp_expr *exp, uint16_t *exp_idx, struct lyxp_set *set, int options) |
| 7057 | { |
| 7058 | LY_ERR rc; |
| 7059 | LY_ERR (*xpath_func)(struct lyxp_set **, uint16_t, struct lyxp_set *, int) = NULL; |
| 7060 | uint16_t arg_count = 0, i, func_exp = *exp_idx; |
| 7061 | struct lyxp_set **args = NULL, **args_aux; |
| 7062 | |
| 7063 | if (set) { |
| 7064 | /* FunctionName */ |
| 7065 | switch (exp->tok_len[*exp_idx]) { |
| 7066 | case 3: |
| 7067 | if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "not", 3)) { |
| 7068 | xpath_func = &xpath_not; |
| 7069 | } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "sum", 3)) { |
| 7070 | xpath_func = &xpath_sum; |
| 7071 | } |
| 7072 | break; |
| 7073 | case 4: |
| 7074 | if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "lang", 4)) { |
| 7075 | xpath_func = &xpath_lang; |
| 7076 | } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "last", 4)) { |
| 7077 | xpath_func = &xpath_last; |
| 7078 | } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "name", 4)) { |
| 7079 | xpath_func = &xpath_name; |
| 7080 | } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "true", 4)) { |
| 7081 | xpath_func = &xpath_true; |
| 7082 | } |
| 7083 | break; |
| 7084 | case 5: |
| 7085 | if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "count", 5)) { |
| 7086 | xpath_func = &xpath_count; |
| 7087 | } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "false", 5)) { |
| 7088 | xpath_func = &xpath_false; |
| 7089 | } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "floor", 5)) { |
| 7090 | xpath_func = &xpath_floor; |
| 7091 | } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "round", 5)) { |
| 7092 | xpath_func = &xpath_round; |
| 7093 | } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "deref", 5)) { |
| 7094 | xpath_func = &xpath_deref; |
| 7095 | } |
| 7096 | break; |
| 7097 | case 6: |
| 7098 | if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "concat", 6)) { |
| 7099 | xpath_func = &xpath_concat; |
| 7100 | } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "number", 6)) { |
| 7101 | xpath_func = &xpath_number; |
| 7102 | } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "string", 6)) { |
| 7103 | xpath_func = &xpath_string; |
| 7104 | } |
| 7105 | break; |
| 7106 | case 7: |
| 7107 | if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "boolean", 7)) { |
| 7108 | xpath_func = &xpath_boolean; |
| 7109 | } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "ceiling", 7)) { |
| 7110 | xpath_func = &xpath_ceiling; |
| 7111 | } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "current", 7)) { |
| 7112 | xpath_func = &xpath_current; |
| 7113 | } |
| 7114 | break; |
| 7115 | case 8: |
| 7116 | if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "contains", 8)) { |
| 7117 | xpath_func = &xpath_contains; |
| 7118 | } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "position", 8)) { |
| 7119 | xpath_func = &xpath_position; |
| 7120 | } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "re-match", 8)) { |
| 7121 | xpath_func = &xpath_re_match; |
| 7122 | } |
| 7123 | break; |
| 7124 | case 9: |
| 7125 | if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "substring", 9)) { |
| 7126 | xpath_func = &xpath_substring; |
| 7127 | } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "translate", 9)) { |
| 7128 | xpath_func = &xpath_translate; |
| 7129 | } |
| 7130 | break; |
| 7131 | case 10: |
| 7132 | if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "local-name", 10)) { |
| 7133 | xpath_func = &xpath_local_name; |
| 7134 | } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "enum-value", 10)) { |
| 7135 | xpath_func = &xpath_enum_value; |
| 7136 | } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "bit-is-set", 10)) { |
| 7137 | xpath_func = &xpath_bit_is_set; |
| 7138 | } |
| 7139 | break; |
| 7140 | case 11: |
| 7141 | if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "starts-with", 11)) { |
| 7142 | xpath_func = &xpath_starts_with; |
| 7143 | } |
| 7144 | break; |
| 7145 | case 12: |
| 7146 | if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "derived-from", 12)) { |
| 7147 | xpath_func = &xpath_derived_from; |
| 7148 | } |
| 7149 | break; |
| 7150 | case 13: |
| 7151 | if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "namespace-uri", 13)) { |
| 7152 | xpath_func = &xpath_namespace_uri; |
| 7153 | } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "string-length", 13)) { |
| 7154 | xpath_func = &xpath_string_length; |
| 7155 | } |
| 7156 | break; |
| 7157 | case 15: |
| 7158 | if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "normalize-space", 15)) { |
| 7159 | xpath_func = &xpath_normalize_space; |
| 7160 | } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "substring-after", 15)) { |
| 7161 | xpath_func = &xpath_substring_after; |
| 7162 | } |
| 7163 | break; |
| 7164 | case 16: |
| 7165 | if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "substring-before", 16)) { |
| 7166 | xpath_func = &xpath_substring_before; |
| 7167 | } |
| 7168 | break; |
| 7169 | case 20: |
| 7170 | if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "derived-from-or-self", 20)) { |
| 7171 | xpath_func = &xpath_derived_from_or_self; |
| 7172 | } |
| 7173 | break; |
| 7174 | } |
| 7175 | |
| 7176 | if (!xpath_func) { |
| 7177 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*exp_idx]]); |
| 7178 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INFUNC, exp->tok_len[*exp_idx], &exp->expr[exp->tok_pos[*exp_idx]]); |
| 7179 | return LY_EVALID; |
| 7180 | } |
| 7181 | } |
| 7182 | |
| 7183 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
| 7184 | print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]); |
| 7185 | ++(*exp_idx); |
| 7186 | |
| 7187 | /* '(' */ |
| 7188 | assert(exp->tokens[*exp_idx] == LYXP_TOKEN_PAR1); |
| 7189 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
| 7190 | print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]); |
| 7191 | ++(*exp_idx); |
| 7192 | |
| 7193 | /* ( Expr ( ',' Expr )* )? */ |
| 7194 | if (exp->tokens[*exp_idx] != LYXP_TOKEN_PAR2) { |
| 7195 | if (set) { |
| 7196 | args = malloc(sizeof *args); |
| 7197 | LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup); |
| 7198 | arg_count = 1; |
| 7199 | args[0] = set_copy(set); |
| 7200 | if (!args[0]) { |
| 7201 | rc = LY_EMEM; |
| 7202 | goto cleanup; |
| 7203 | } |
| 7204 | |
| 7205 | rc = eval_expr_select(exp, exp_idx, 0, args[0], options); |
| 7206 | LY_CHECK_GOTO(rc, cleanup); |
| 7207 | } else { |
| 7208 | rc = eval_expr_select(exp, exp_idx, 0, NULL, options); |
| 7209 | LY_CHECK_GOTO(rc, cleanup); |
| 7210 | } |
| 7211 | } |
| 7212 | while ((exp->used > *exp_idx) && (exp->tokens[*exp_idx] == LYXP_TOKEN_COMMA)) { |
| 7213 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
| 7214 | print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]); |
| 7215 | ++(*exp_idx); |
| 7216 | |
| 7217 | if (set) { |
| 7218 | ++arg_count; |
| 7219 | args_aux = realloc(args, arg_count * sizeof *args); |
| 7220 | LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup); |
| 7221 | args = args_aux; |
| 7222 | args[arg_count - 1] = set_copy(set); |
| 7223 | if (!args[arg_count - 1]) { |
| 7224 | rc = LY_EMEM; |
| 7225 | goto cleanup; |
| 7226 | } |
| 7227 | |
| 7228 | rc = eval_expr_select(exp, exp_idx, 0, args[arg_count - 1], options); |
| 7229 | LY_CHECK_GOTO(rc, cleanup); |
| 7230 | } else { |
| 7231 | rc = eval_expr_select(exp, exp_idx, 0, NULL, options); |
| 7232 | LY_CHECK_GOTO(rc, cleanup); |
| 7233 | } |
| 7234 | } |
| 7235 | |
| 7236 | /* ')' */ |
| 7237 | assert(exp->tokens[*exp_idx] == LYXP_TOKEN_PAR2); |
| 7238 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
| 7239 | print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]); |
| 7240 | ++(*exp_idx); |
| 7241 | |
| 7242 | if (set) { |
| 7243 | /* evaluate function */ |
| 7244 | rc = xpath_func(args, arg_count, set, options); |
| 7245 | |
| 7246 | if (options & LYXP_SCNODE_ALL) { |
| 7247 | if (rc == LY_EINVAL) { |
| 7248 | /* some validation warning TODO log everything immediately? */ |
| 7249 | LOGWRN(set->ctx, "Previous warning generated by XPath function \"%.*s\".", |
| 7250 | (exp->tok_pos[*exp_idx - 1] - exp->tok_pos[func_exp]) + 1, &exp->expr[exp->tok_pos[func_exp]]); |
| 7251 | rc = LY_SUCCESS; |
| 7252 | } |
| 7253 | |
| 7254 | /* merge all nodes from arg evaluations */ |
| 7255 | for (i = 0; i < arg_count; ++i) { |
| 7256 | set_scnode_clear_ctx(args[i]); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7257 | lyxp_set_scnode_merge(set, args[i]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7258 | } |
| 7259 | } |
| 7260 | } else { |
| 7261 | rc = LY_SUCCESS; |
| 7262 | } |
| 7263 | |
| 7264 | cleanup: |
| 7265 | for (i = 0; i < arg_count; ++i) { |
| 7266 | lyxp_set_free(args[i]); |
| 7267 | } |
| 7268 | free(args); |
| 7269 | |
| 7270 | return rc; |
| 7271 | } |
| 7272 | |
| 7273 | /** |
| 7274 | * @brief Evaluate Number. Logs directly on error. |
| 7275 | * |
| 7276 | * @param[in] ctx Context for errors. |
| 7277 | * @param[in] exp Parsed XPath expression. |
| 7278 | * @param[in] exp_idx Position in the expression @p exp. |
| 7279 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7280 | * @return LY_ERR |
| 7281 | */ |
| 7282 | static LY_ERR |
| 7283 | eval_number(struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *exp_idx, struct lyxp_set *set) |
| 7284 | { |
| 7285 | long double num; |
| 7286 | char *endptr; |
| 7287 | |
| 7288 | if (set) { |
| 7289 | errno = 0; |
| 7290 | num = strtold(&exp->expr[exp->tok_pos[*exp_idx]], &endptr); |
| 7291 | if (errno) { |
| 7292 | LOGVAL(ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*exp_idx]]); |
| 7293 | LOGVAL(ctx, LY_VLOG_LYD, set->ctx_node, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double (%s).", |
| 7294 | exp->tok_len[*exp_idx], &exp->expr[exp->tok_pos[*exp_idx]], strerror(errno)); |
| 7295 | return LY_EVALID; |
| 7296 | } else if (endptr - &exp->expr[exp->tok_pos[*exp_idx]] != exp->tok_len[*exp_idx]) { |
| 7297 | LOGVAL(ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*exp_idx]]); |
| 7298 | LOGVAL(ctx, LY_VLOG_LYD, set->ctx_node, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double.", |
| 7299 | exp->tok_len[*exp_idx], &exp->expr[exp->tok_pos[*exp_idx]]); |
| 7300 | return LY_EVALID; |
| 7301 | } |
| 7302 | |
| 7303 | set_fill_number(set, num); |
| 7304 | } |
| 7305 | |
| 7306 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
| 7307 | print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]); |
| 7308 | ++(*exp_idx); |
| 7309 | return LY_SUCCESS; |
| 7310 | } |
| 7311 | |
| 7312 | /** |
| 7313 | * @brief Evaluate PathExpr. Logs directly on error. |
| 7314 | * |
| 7315 | * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate* |
| 7316 | * | PrimaryExpr Predicate* '/' RelativeLocationPath |
| 7317 | * | PrimaryExpr Predicate* '//' RelativeLocationPath |
| 7318 | * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath |
| 7319 | * [8] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall |
| 7320 | * |
| 7321 | * @param[in] exp Parsed XPath expression. |
| 7322 | * @param[in] exp_idx Position in the expression @p exp. |
| 7323 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7324 | * @param[in] options XPath options. |
| 7325 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7326 | */ |
| 7327 | static LY_ERR |
| 7328 | eval_path_expr(struct lyxp_expr *exp, uint16_t *exp_idx, struct lyxp_set *set, int options) |
| 7329 | { |
| 7330 | int all_desc, parent_pos_pred; |
| 7331 | LY_ERR rc; |
| 7332 | |
| 7333 | switch (exp->tokens[*exp_idx]) { |
| 7334 | case LYXP_TOKEN_PAR1: |
| 7335 | /* '(' Expr ')' */ |
| 7336 | |
| 7337 | /* '(' */ |
| 7338 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
| 7339 | print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]); |
| 7340 | ++(*exp_idx); |
| 7341 | |
| 7342 | /* Expr */ |
| 7343 | rc = eval_expr_select(exp, exp_idx, 0, set, options); |
| 7344 | LY_CHECK_RET(rc); |
| 7345 | |
| 7346 | /* ')' */ |
| 7347 | assert(exp->tokens[*exp_idx] == LYXP_TOKEN_PAR2); |
| 7348 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
| 7349 | print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]); |
| 7350 | ++(*exp_idx); |
| 7351 | |
| 7352 | parent_pos_pred = 0; |
| 7353 | goto predicate; |
| 7354 | |
| 7355 | case LYXP_TOKEN_DOT: |
| 7356 | case LYXP_TOKEN_DDOT: |
| 7357 | case LYXP_TOKEN_AT: |
| 7358 | case LYXP_TOKEN_NAMETEST: |
| 7359 | case LYXP_TOKEN_NODETYPE: |
| 7360 | /* RelativeLocationPath */ |
| 7361 | rc = eval_relative_location_path(exp, exp_idx, 0, set, options); |
| 7362 | LY_CHECK_RET(rc); |
| 7363 | break; |
| 7364 | |
| 7365 | case LYXP_TOKEN_FUNCNAME: |
| 7366 | /* FunctionCall */ |
| 7367 | if (!set) { |
| 7368 | rc = eval_function_call(exp, exp_idx, NULL, options); |
| 7369 | } else { |
| 7370 | rc = eval_function_call(exp, exp_idx, set, options); |
| 7371 | } |
| 7372 | LY_CHECK_RET(rc); |
| 7373 | |
| 7374 | parent_pos_pred = 1; |
| 7375 | goto predicate; |
| 7376 | |
| 7377 | case LYXP_TOKEN_OPERATOR_PATH: |
| 7378 | /* AbsoluteLocationPath */ |
| 7379 | rc = eval_absolute_location_path(exp, exp_idx, set, options); |
| 7380 | LY_CHECK_RET(rc); |
| 7381 | break; |
| 7382 | |
| 7383 | case LYXP_TOKEN_LITERAL: |
| 7384 | /* Literal */ |
| 7385 | if (!set || (options & LYXP_SCNODE_ALL)) { |
| 7386 | if (set) { |
| 7387 | set_scnode_clear_ctx(set); |
| 7388 | } |
| 7389 | eval_literal(exp, exp_idx, NULL); |
| 7390 | } else { |
| 7391 | eval_literal(exp, exp_idx, set); |
| 7392 | } |
| 7393 | |
| 7394 | parent_pos_pred = 1; |
| 7395 | goto predicate; |
| 7396 | |
| 7397 | case LYXP_TOKEN_NUMBER: |
| 7398 | /* Number */ |
| 7399 | if (!set || (options & LYXP_SCNODE_ALL)) { |
| 7400 | if (set) { |
| 7401 | set_scnode_clear_ctx(set); |
| 7402 | } |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 7403 | rc = eval_number(NULL, exp, exp_idx, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7404 | } else { |
| 7405 | rc = eval_number(set->ctx, exp, exp_idx, set); |
| 7406 | } |
| 7407 | LY_CHECK_RET(rc); |
| 7408 | |
| 7409 | parent_pos_pred = 1; |
| 7410 | goto predicate; |
| 7411 | |
| 7412 | default: |
| 7413 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INTOK, print_token(exp->tokens[*exp_idx]), |
| 7414 | &exp->expr[exp->tok_pos[*exp_idx]]); |
| 7415 | return LY_EVALID; |
| 7416 | } |
| 7417 | |
| 7418 | return LY_SUCCESS; |
| 7419 | |
| 7420 | predicate: |
| 7421 | /* Predicate* */ |
| 7422 | while ((exp->used > *exp_idx) && (exp->tokens[*exp_idx] == LYXP_TOKEN_BRACK1)) { |
| 7423 | rc = eval_predicate(exp, exp_idx, set, options, parent_pos_pred); |
| 7424 | LY_CHECK_RET(rc); |
| 7425 | } |
| 7426 | |
| 7427 | /* ('/' or '//') RelativeLocationPath */ |
| 7428 | if ((exp->used > *exp_idx) && (exp->tokens[*exp_idx] == LYXP_TOKEN_OPERATOR_PATH)) { |
| 7429 | |
| 7430 | /* evaluate '/' or '//' */ |
| 7431 | if (exp->tok_len[*exp_idx] == 1) { |
| 7432 | all_desc = 0; |
| 7433 | } else { |
| 7434 | assert(exp->tok_len[*exp_idx] == 2); |
| 7435 | all_desc = 1; |
| 7436 | } |
| 7437 | |
| 7438 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
| 7439 | print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]); |
| 7440 | ++(*exp_idx); |
| 7441 | |
| 7442 | rc = eval_relative_location_path(exp, exp_idx, all_desc, set, options); |
| 7443 | LY_CHECK_RET(rc); |
| 7444 | } |
| 7445 | |
| 7446 | return LY_SUCCESS; |
| 7447 | } |
| 7448 | |
| 7449 | /** |
| 7450 | * @brief Evaluate UnionExpr. Logs directly on error. |
| 7451 | * |
| 7452 | * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr |
| 7453 | * |
| 7454 | * @param[in] exp Parsed XPath expression. |
| 7455 | * @param[in] exp_idx Position in the expression @p exp. |
| 7456 | * @param[in] repeat How many times this expression is repeated. |
| 7457 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7458 | * @param[in] options XPath options. |
| 7459 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7460 | */ |
| 7461 | static LY_ERR |
| 7462 | eval_union_expr(struct lyxp_expr *exp, uint16_t *exp_idx, uint16_t repeat, struct lyxp_set *set, int options) |
| 7463 | { |
| 7464 | LY_ERR rc = LY_SUCCESS; |
| 7465 | struct lyxp_set orig_set, set2; |
| 7466 | uint16_t i; |
| 7467 | |
| 7468 | assert(repeat); |
| 7469 | |
| 7470 | set_init(&orig_set, set); |
| 7471 | set_init(&set2, set); |
| 7472 | |
| 7473 | set_fill_set(&orig_set, set); |
| 7474 | |
| 7475 | rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_UNION, set, options); |
| 7476 | LY_CHECK_GOTO(rc, cleanup); |
| 7477 | |
| 7478 | /* ('|' PathExpr)* */ |
| 7479 | for (i = 0; i < repeat; ++i) { |
| 7480 | assert(exp->tokens[*exp_idx] == LYXP_TOKEN_OPERATOR_UNI); |
| 7481 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
| 7482 | print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]); |
| 7483 | ++(*exp_idx); |
| 7484 | |
| 7485 | if (!set) { |
| 7486 | rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_UNION, NULL, options); |
| 7487 | LY_CHECK_GOTO(rc, cleanup); |
| 7488 | continue; |
| 7489 | } |
| 7490 | |
| 7491 | set_fill_set(&set2, &orig_set); |
| 7492 | rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_UNION, &set2, options); |
| 7493 | LY_CHECK_GOTO(rc, cleanup); |
| 7494 | |
| 7495 | /* eval */ |
| 7496 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7497 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7498 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7499 | rc = moveto_union(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7500 | LY_CHECK_GOTO(rc, cleanup); |
| 7501 | } |
| 7502 | } |
| 7503 | |
| 7504 | cleanup: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7505 | lyxp_set_cast(&orig_set, LYXP_SET_EMPTY); |
| 7506 | lyxp_set_cast(&set2, LYXP_SET_EMPTY); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7507 | return rc; |
| 7508 | } |
| 7509 | |
| 7510 | /** |
| 7511 | * @brief Evaluate UnaryExpr. Logs directly on error. |
| 7512 | * |
| 7513 | * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr |
| 7514 | * |
| 7515 | * @param[in] exp Parsed XPath expression. |
| 7516 | * @param[in] exp_idx Position in the expression @p exp. |
| 7517 | * @param[in] repeat How many times this expression is repeated. |
| 7518 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7519 | * @param[in] options XPath options. |
| 7520 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7521 | */ |
| 7522 | static LY_ERR |
| 7523 | eval_unary_expr(struct lyxp_expr *exp, uint16_t *exp_idx, uint16_t repeat, struct lyxp_set *set, int options) |
| 7524 | { |
| 7525 | LY_ERR rc; |
| 7526 | uint16_t this_op, i; |
| 7527 | |
| 7528 | assert(repeat); |
| 7529 | |
| 7530 | /* ('-')+ */ |
| 7531 | this_op = *exp_idx; |
| 7532 | for (i = 0; i < repeat; ++i) { |
| 7533 | assert(!exp_check_token(set->ctx, exp, *exp_idx, LYXP_TOKEN_OPERATOR_MATH, 0) && (exp->expr[exp->tok_pos[*exp_idx]] == '-')); |
| 7534 | |
| 7535 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
| 7536 | print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]); |
| 7537 | ++(*exp_idx); |
| 7538 | } |
| 7539 | |
| 7540 | rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_UNARY, set, options); |
| 7541 | LY_CHECK_RET(rc); |
| 7542 | |
| 7543 | if (set && (repeat % 2)) { |
| 7544 | if (options & LYXP_SCNODE_ALL) { |
| 7545 | warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]); |
| 7546 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7547 | 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] | 7548 | LY_CHECK_RET(rc); |
| 7549 | } |
| 7550 | } |
| 7551 | |
| 7552 | return LY_SUCCESS; |
| 7553 | } |
| 7554 | |
| 7555 | /** |
| 7556 | * @brief Evaluate MultiplicativeExpr. Logs directly on error. |
| 7557 | * |
| 7558 | * [16] MultiplicativeExpr ::= UnaryExpr |
| 7559 | * | MultiplicativeExpr '*' UnaryExpr |
| 7560 | * | MultiplicativeExpr 'div' UnaryExpr |
| 7561 | * | MultiplicativeExpr 'mod' UnaryExpr |
| 7562 | * |
| 7563 | * @param[in] exp Parsed XPath expression. |
| 7564 | * @param[in] exp_idx Position in the expression @p exp. |
| 7565 | * @param[in] repeat How many times this expression is repeated. |
| 7566 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7567 | * @param[in] options XPath options. |
| 7568 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7569 | */ |
| 7570 | static LY_ERR |
| 7571 | eval_multiplicative_expr(struct lyxp_expr *exp, uint16_t *exp_idx, uint16_t repeat, struct lyxp_set *set, int options) |
| 7572 | { |
| 7573 | LY_ERR rc; |
| 7574 | uint16_t this_op; |
| 7575 | struct lyxp_set orig_set, set2; |
| 7576 | uint16_t i; |
| 7577 | |
| 7578 | assert(repeat); |
| 7579 | |
| 7580 | set_init(&orig_set, set); |
| 7581 | set_init(&set2, set); |
| 7582 | |
| 7583 | set_fill_set(&orig_set, set); |
| 7584 | |
| 7585 | rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_MULTIPLICATIVE, set, options); |
| 7586 | LY_CHECK_GOTO(rc, cleanup); |
| 7587 | |
| 7588 | /* ('*' / 'div' / 'mod' UnaryExpr)* */ |
| 7589 | for (i = 0; i < repeat; ++i) { |
| 7590 | this_op = *exp_idx; |
| 7591 | |
| 7592 | assert(exp->tokens[*exp_idx] == LYXP_TOKEN_OPERATOR_MATH); |
| 7593 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
| 7594 | print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]); |
| 7595 | ++(*exp_idx); |
| 7596 | |
| 7597 | if (!set) { |
| 7598 | rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_MULTIPLICATIVE, NULL, options); |
| 7599 | LY_CHECK_GOTO(rc, cleanup); |
| 7600 | continue; |
| 7601 | } |
| 7602 | |
| 7603 | set_fill_set(&set2, &orig_set); |
| 7604 | rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options); |
| 7605 | LY_CHECK_GOTO(rc, cleanup); |
| 7606 | |
| 7607 | /* eval */ |
| 7608 | if (options & LYXP_SCNODE_ALL) { |
| 7609 | 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] | 7610 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7611 | set_scnode_clear_ctx(set); |
| 7612 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7613 | 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] | 7614 | LY_CHECK_GOTO(rc, cleanup); |
| 7615 | } |
| 7616 | } |
| 7617 | |
| 7618 | cleanup: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7619 | lyxp_set_cast(&orig_set, LYXP_SET_EMPTY); |
| 7620 | lyxp_set_cast(&set2, LYXP_SET_EMPTY); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7621 | return rc; |
| 7622 | } |
| 7623 | |
| 7624 | /** |
| 7625 | * @brief Evaluate AdditiveExpr. Logs directly on error. |
| 7626 | * |
| 7627 | * [15] AdditiveExpr ::= MultiplicativeExpr |
| 7628 | * | AdditiveExpr '+' MultiplicativeExpr |
| 7629 | * | AdditiveExpr '-' MultiplicativeExpr |
| 7630 | * |
| 7631 | * @param[in] exp Parsed XPath expression. |
| 7632 | * @param[in] exp_idx Position in the expression @p exp. |
| 7633 | * @param[in] repeat How many times this expression is repeated. |
| 7634 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7635 | * @param[in] options XPath options. |
| 7636 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7637 | */ |
| 7638 | static LY_ERR |
| 7639 | eval_additive_expr(struct lyxp_expr *exp, uint16_t *exp_idx, uint16_t repeat, struct lyxp_set *set, int options) |
| 7640 | { |
| 7641 | LY_ERR rc; |
| 7642 | uint16_t this_op; |
| 7643 | struct lyxp_set orig_set, set2; |
| 7644 | uint16_t i; |
| 7645 | |
| 7646 | assert(repeat); |
| 7647 | |
| 7648 | set_init(&orig_set, set); |
| 7649 | set_init(&set2, set); |
| 7650 | |
| 7651 | set_fill_set(&orig_set, set); |
| 7652 | |
| 7653 | rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_ADDITIVE, set, options); |
| 7654 | LY_CHECK_GOTO(rc, cleanup); |
| 7655 | |
| 7656 | /* ('+' / '-' MultiplicativeExpr)* */ |
| 7657 | for (i = 0; i < repeat; ++i) { |
| 7658 | this_op = *exp_idx; |
| 7659 | |
| 7660 | assert(exp->tokens[*exp_idx] == LYXP_TOKEN_OPERATOR_MATH); |
| 7661 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
| 7662 | print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]); |
| 7663 | ++(*exp_idx); |
| 7664 | |
| 7665 | if (!set) { |
| 7666 | rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_ADDITIVE, NULL, options); |
| 7667 | LY_CHECK_GOTO(rc, cleanup); |
| 7668 | continue; |
| 7669 | } |
| 7670 | |
| 7671 | set_fill_set(&set2, &orig_set); |
| 7672 | rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_ADDITIVE, &set2, options); |
| 7673 | LY_CHECK_GOTO(rc, cleanup); |
| 7674 | |
| 7675 | /* eval */ |
| 7676 | if (options & LYXP_SCNODE_ALL) { |
| 7677 | 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] | 7678 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7679 | set_scnode_clear_ctx(set); |
| 7680 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7681 | 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] | 7682 | LY_CHECK_GOTO(rc, cleanup); |
| 7683 | } |
| 7684 | } |
| 7685 | |
| 7686 | cleanup: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7687 | lyxp_set_cast(&orig_set, LYXP_SET_EMPTY); |
| 7688 | lyxp_set_cast(&set2, LYXP_SET_EMPTY); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7689 | return rc; |
| 7690 | } |
| 7691 | |
| 7692 | /** |
| 7693 | * @brief Evaluate RelationalExpr. Logs directly on error. |
| 7694 | * |
| 7695 | * [14] RelationalExpr ::= AdditiveExpr |
| 7696 | * | RelationalExpr '<' AdditiveExpr |
| 7697 | * | RelationalExpr '>' AdditiveExpr |
| 7698 | * | RelationalExpr '<=' AdditiveExpr |
| 7699 | * | RelationalExpr '>=' AdditiveExpr |
| 7700 | * |
| 7701 | * @param[in] exp Parsed XPath expression. |
| 7702 | * @param[in] exp_idx Position in the expression @p exp. |
| 7703 | * @param[in] repeat How many times this expression is repeated. |
| 7704 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7705 | * @param[in] options XPath options. |
| 7706 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7707 | */ |
| 7708 | static LY_ERR |
| 7709 | eval_relational_expr(struct lyxp_expr *exp, uint16_t *exp_idx, uint16_t repeat, struct lyxp_set *set, int options) |
| 7710 | { |
| 7711 | LY_ERR rc; |
| 7712 | uint16_t this_op; |
| 7713 | struct lyxp_set orig_set, set2; |
| 7714 | uint16_t i; |
| 7715 | |
| 7716 | assert(repeat); |
| 7717 | |
| 7718 | set_init(&orig_set, set); |
| 7719 | set_init(&set2, set); |
| 7720 | |
| 7721 | set_fill_set(&orig_set, set); |
| 7722 | |
| 7723 | rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_RELATIONAL, set, options); |
| 7724 | LY_CHECK_GOTO(rc, cleanup); |
| 7725 | |
| 7726 | /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */ |
| 7727 | for (i = 0; i < repeat; ++i) { |
| 7728 | this_op = *exp_idx; |
| 7729 | |
| 7730 | assert(exp->tokens[*exp_idx] == LYXP_TOKEN_OPERATOR_COMP); |
| 7731 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
| 7732 | print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]); |
| 7733 | ++(*exp_idx); |
| 7734 | |
| 7735 | if (!set) { |
| 7736 | rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_RELATIONAL, NULL, options); |
| 7737 | LY_CHECK_GOTO(rc, cleanup); |
| 7738 | continue; |
| 7739 | } |
| 7740 | |
| 7741 | set_fill_set(&set2, &orig_set); |
| 7742 | rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_RELATIONAL, &set2, options); |
| 7743 | LY_CHECK_GOTO(rc, cleanup); |
| 7744 | |
| 7745 | /* eval */ |
| 7746 | if (options & LYXP_SCNODE_ALL) { |
| 7747 | 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] | 7748 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7749 | set_scnode_clear_ctx(set); |
| 7750 | } else { |
| 7751 | rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options); |
| 7752 | LY_CHECK_GOTO(rc, cleanup); |
| 7753 | } |
| 7754 | } |
| 7755 | |
| 7756 | cleanup: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7757 | lyxp_set_cast(&orig_set, LYXP_SET_EMPTY); |
| 7758 | lyxp_set_cast(&set2, LYXP_SET_EMPTY); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7759 | return rc; |
| 7760 | } |
| 7761 | |
| 7762 | /** |
| 7763 | * @brief Evaluate EqualityExpr. Logs directly on error. |
| 7764 | * |
| 7765 | * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr |
| 7766 | * | EqualityExpr '!=' RelationalExpr |
| 7767 | * |
| 7768 | * @param[in] exp Parsed XPath expression. |
| 7769 | * @param[in] exp_idx Position in the expression @p exp. |
| 7770 | * @param[in] repeat How many times this expression is repeated. |
| 7771 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7772 | * @param[in] options XPath options. |
| 7773 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7774 | */ |
| 7775 | static LY_ERR |
| 7776 | eval_equality_expr(struct lyxp_expr *exp, uint16_t *exp_idx, uint16_t repeat, struct lyxp_set *set, int options) |
| 7777 | { |
| 7778 | LY_ERR rc; |
| 7779 | uint16_t this_op; |
| 7780 | struct lyxp_set orig_set, set2; |
| 7781 | uint16_t i; |
| 7782 | |
| 7783 | assert(repeat); |
| 7784 | |
| 7785 | set_init(&orig_set, set); |
| 7786 | set_init(&set2, set); |
| 7787 | |
| 7788 | set_fill_set(&orig_set, set); |
| 7789 | |
| 7790 | rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_EQUALITY, set, options); |
| 7791 | LY_CHECK_GOTO(rc, cleanup); |
| 7792 | |
| 7793 | /* ('=' / '!=' RelationalExpr)* */ |
| 7794 | for (i = 0; i < repeat; ++i) { |
| 7795 | this_op = *exp_idx; |
| 7796 | |
| 7797 | assert(exp->tokens[*exp_idx] == LYXP_TOKEN_OPERATOR_COMP); |
| 7798 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
| 7799 | print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]); |
| 7800 | ++(*exp_idx); |
| 7801 | |
| 7802 | if (!set) { |
| 7803 | rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_EQUALITY, NULL, options); |
| 7804 | LY_CHECK_GOTO(rc, cleanup); |
| 7805 | continue; |
| 7806 | } |
| 7807 | |
| 7808 | set_fill_set(&set2, &orig_set); |
| 7809 | rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_EQUALITY, &set2, options); |
| 7810 | LY_CHECK_GOTO(rc, cleanup); |
| 7811 | |
| 7812 | /* eval */ |
| 7813 | if (options & LYXP_SCNODE_ALL) { |
| 7814 | warn_operands(set->ctx, set, &set2, 0, exp->expr, exp->tok_pos[this_op - 1]); |
| 7815 | warn_equality_value(exp, set, *exp_idx - 1, this_op - 1, *exp_idx - 1); |
| 7816 | warn_equality_value(exp, &set2, this_op - 1, this_op - 1, *exp_idx - 1); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7817 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7818 | set_scnode_clear_ctx(set); |
| 7819 | } else { |
| 7820 | /* special handling of evaluations of identityref comparisons, always compare prefixed identites */ |
| 7821 | if ((set->type == LYXP_SET_NODE_SET) && (set->val.nodes[0].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) |
| 7822 | && (((struct lysc_node_leaf *)set->val.nodes[0].node->schema)->type->basetype == LY_TYPE_IDENT)) { |
| 7823 | /* left operand is identityref */ |
| 7824 | if ((set2.type == LYXP_SET_STRING) && !strchr(set2.val.str, ':')) { |
| 7825 | /* missing prefix in the right operand */ |
| 7826 | set2.val.str = ly_realloc(set2.val.str, strlen(set->local_mod->name) + 1 + strlen(set2.val.str) + 1); |
| 7827 | if (!set2.val.str) { |
| 7828 | goto cleanup; |
| 7829 | } |
| 7830 | |
| 7831 | memmove(set2.val.str + strlen(set->local_mod->name) + 1, set2.val.str, strlen(set2.val.str) + 1); |
| 7832 | memcpy(set2.val.str, set->local_mod->name, strlen(set->local_mod->name)); |
| 7833 | set2.val.str[strlen(set->local_mod->name)] = ':'; |
| 7834 | } |
| 7835 | } |
| 7836 | |
| 7837 | rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options); |
| 7838 | LY_CHECK_GOTO(rc, cleanup); |
| 7839 | } |
| 7840 | } |
| 7841 | |
| 7842 | cleanup: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7843 | lyxp_set_cast(&orig_set, LYXP_SET_EMPTY); |
| 7844 | lyxp_set_cast(&set2, LYXP_SET_EMPTY); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7845 | return rc; |
| 7846 | } |
| 7847 | |
| 7848 | /** |
| 7849 | * @brief Evaluate AndExpr. Logs directly on error. |
| 7850 | * |
| 7851 | * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr |
| 7852 | * |
| 7853 | * @param[in] exp Parsed XPath expression. |
| 7854 | * @param[in] exp_idx Position in the expression @p exp. |
| 7855 | * @param[in] repeat How many times this expression is repeated. |
| 7856 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7857 | * @param[in] options XPath options. |
| 7858 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7859 | */ |
| 7860 | static LY_ERR |
| 7861 | eval_and_expr(struct lyxp_expr *exp, uint16_t *exp_idx, uint16_t repeat, struct lyxp_set *set, int options) |
| 7862 | { |
| 7863 | LY_ERR rc; |
| 7864 | struct lyxp_set orig_set, set2; |
| 7865 | uint16_t i; |
| 7866 | |
| 7867 | assert(repeat); |
| 7868 | |
| 7869 | set_init(&orig_set, set); |
| 7870 | set_init(&set2, set); |
| 7871 | |
| 7872 | set_fill_set(&orig_set, set); |
| 7873 | |
| 7874 | rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_AND, set, options); |
| 7875 | LY_CHECK_GOTO(rc, cleanup); |
| 7876 | |
| 7877 | /* cast to boolean, we know that will be the final result */ |
| 7878 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 7879 | set_scnode_clear_ctx(set); |
| 7880 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7881 | lyxp_set_cast(set, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7882 | } |
| 7883 | |
| 7884 | /* ('and' EqualityExpr)* */ |
| 7885 | for (i = 0; i < repeat; ++i) { |
| 7886 | assert(exp->tokens[*exp_idx] == LYXP_TOKEN_OPERATOR_LOG); |
| 7887 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || !set->val.bool ? "skipped" : "parsed"), |
| 7888 | print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]); |
| 7889 | ++(*exp_idx); |
| 7890 | |
| 7891 | /* lazy evaluation */ |
| 7892 | if (!set || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bool)) { |
| 7893 | rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_AND, NULL, options); |
| 7894 | LY_CHECK_GOTO(rc, cleanup); |
| 7895 | continue; |
| 7896 | } |
| 7897 | |
| 7898 | set_fill_set(&set2, &orig_set); |
| 7899 | rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_AND, &set2, options); |
| 7900 | LY_CHECK_GOTO(rc, cleanup); |
| 7901 | |
| 7902 | /* eval - just get boolean value actually */ |
| 7903 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 7904 | set_scnode_clear_ctx(&set2); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7905 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7906 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7907 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7908 | set_fill_set(set, &set2); |
| 7909 | } |
| 7910 | } |
| 7911 | |
| 7912 | cleanup: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7913 | lyxp_set_cast(&orig_set, LYXP_SET_EMPTY); |
| 7914 | lyxp_set_cast(&set2, LYXP_SET_EMPTY); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7915 | return rc; |
| 7916 | } |
| 7917 | |
| 7918 | /** |
| 7919 | * @brief Evaluate OrExpr. Logs directly on error. |
| 7920 | * |
| 7921 | * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr |
| 7922 | * |
| 7923 | * @param[in] exp Parsed XPath expression. |
| 7924 | * @param[in] exp_idx Position in the expression @p exp. |
| 7925 | * @param[in] repeat How many times this expression is repeated. |
| 7926 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7927 | * @param[in] options XPath options. |
| 7928 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7929 | */ |
| 7930 | static LY_ERR |
| 7931 | eval_or_expr(struct lyxp_expr *exp, uint16_t *exp_idx, uint16_t repeat, struct lyxp_set *set, int options) |
| 7932 | { |
| 7933 | LY_ERR rc; |
| 7934 | struct lyxp_set orig_set, set2; |
| 7935 | uint16_t i; |
| 7936 | |
| 7937 | assert(repeat); |
| 7938 | |
| 7939 | set_init(&orig_set, set); |
| 7940 | set_init(&set2, set); |
| 7941 | |
| 7942 | set_fill_set(&orig_set, set); |
| 7943 | |
| 7944 | rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_OR, set, options); |
| 7945 | LY_CHECK_GOTO(rc, cleanup); |
| 7946 | |
| 7947 | /* cast to boolean, we know that will be the final result */ |
| 7948 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 7949 | set_scnode_clear_ctx(set); |
| 7950 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7951 | lyxp_set_cast(set, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7952 | } |
| 7953 | |
| 7954 | /* ('or' AndExpr)* */ |
| 7955 | for (i = 0; i < repeat; ++i) { |
| 7956 | assert(exp->tokens[*exp_idx] == LYXP_TOKEN_OPERATOR_LOG); |
| 7957 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || set->val.bool ? "skipped" : "parsed"), |
| 7958 | print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]); |
| 7959 | ++(*exp_idx); |
| 7960 | |
| 7961 | /* lazy evaluation */ |
| 7962 | if (!set || ((set->type == LYXP_SET_BOOLEAN) && set->val.bool)) { |
| 7963 | rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_OR, NULL, options); |
| 7964 | LY_CHECK_GOTO(rc, cleanup); |
| 7965 | continue; |
| 7966 | } |
| 7967 | |
| 7968 | set_fill_set(&set2, &orig_set); |
| 7969 | /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one), |
| 7970 | * but it does not matter */ |
| 7971 | rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_OR, &set2, options); |
| 7972 | LY_CHECK_GOTO(rc, cleanup); |
| 7973 | |
| 7974 | /* eval - just get boolean value actually */ |
| 7975 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 7976 | set_scnode_clear_ctx(&set2); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7977 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7978 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7979 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7980 | set_fill_set(set, &set2); |
| 7981 | } |
| 7982 | } |
| 7983 | |
| 7984 | cleanup: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7985 | lyxp_set_cast(&orig_set, LYXP_SET_EMPTY); |
| 7986 | lyxp_set_cast(&set2, LYXP_SET_EMPTY); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7987 | return rc; |
| 7988 | } |
| 7989 | |
| 7990 | /** |
| 7991 | * @brief Decide what expression is at the pointer @p exp_idx and evaluate it accordingly. |
| 7992 | * |
| 7993 | * @param[in] exp Parsed XPath expression. |
| 7994 | * @param[in] exp_idx Position in the expression @p exp. |
| 7995 | * @param[in] etype Expression type to evaluate. |
| 7996 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7997 | * @param[in] options XPath options. |
| 7998 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7999 | */ |
| 8000 | static LY_ERR |
| 8001 | eval_expr_select(struct lyxp_expr *exp, uint16_t *exp_idx, enum lyxp_expr_type etype, struct lyxp_set *set, int options) |
| 8002 | { |
| 8003 | uint16_t i, count; |
| 8004 | enum lyxp_expr_type next_etype; |
| 8005 | LY_ERR rc; |
| 8006 | |
| 8007 | /* process operator repeats */ |
| 8008 | if (!exp->repeat[*exp_idx]) { |
| 8009 | next_etype = LYXP_EXPR_NONE; |
| 8010 | } else { |
| 8011 | /* find etype repeat */ |
| 8012 | for (i = 0; exp->repeat[*exp_idx][i] > etype; ++i); |
| 8013 | |
| 8014 | /* select one-priority lower because etype expression called us */ |
| 8015 | if (i) { |
| 8016 | next_etype = exp->repeat[*exp_idx][i - 1]; |
| 8017 | /* count repeats for that expression */ |
| 8018 | for (count = 0; i && exp->repeat[*exp_idx][i - 1] == next_etype; ++count, --i); |
| 8019 | } else { |
| 8020 | next_etype = LYXP_EXPR_NONE; |
| 8021 | } |
| 8022 | } |
| 8023 | |
| 8024 | /* decide what expression are we parsing based on the repeat */ |
| 8025 | switch (next_etype) { |
| 8026 | case LYXP_EXPR_OR: |
| 8027 | rc = eval_or_expr(exp, exp_idx, count, set, options); |
| 8028 | break; |
| 8029 | case LYXP_EXPR_AND: |
| 8030 | rc = eval_and_expr(exp, exp_idx, count, set, options); |
| 8031 | break; |
| 8032 | case LYXP_EXPR_EQUALITY: |
| 8033 | rc = eval_equality_expr(exp, exp_idx, count, set, options); |
| 8034 | break; |
| 8035 | case LYXP_EXPR_RELATIONAL: |
| 8036 | rc = eval_relational_expr(exp, exp_idx, count, set, options); |
| 8037 | break; |
| 8038 | case LYXP_EXPR_ADDITIVE: |
| 8039 | rc = eval_additive_expr(exp, exp_idx, count, set, options); |
| 8040 | break; |
| 8041 | case LYXP_EXPR_MULTIPLICATIVE: |
| 8042 | rc = eval_multiplicative_expr(exp, exp_idx, count, set, options); |
| 8043 | break; |
| 8044 | case LYXP_EXPR_UNARY: |
| 8045 | rc = eval_unary_expr(exp, exp_idx, count, set, options); |
| 8046 | break; |
| 8047 | case LYXP_EXPR_UNION: |
| 8048 | rc = eval_union_expr(exp, exp_idx, count, set, options); |
| 8049 | break; |
| 8050 | case LYXP_EXPR_NONE: |
| 8051 | rc = eval_path_expr(exp, exp_idx, set, options); |
| 8052 | break; |
| 8053 | default: |
| 8054 | LOGINT_RET(set->ctx); |
| 8055 | } |
| 8056 | |
| 8057 | return rc; |
| 8058 | } |
| 8059 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8060 | /** |
| 8061 | * @brief Get root type. |
| 8062 | * |
| 8063 | * @param[in] ctx_node Context node. |
| 8064 | * @param[in] ctx_scnode Schema context node. |
| 8065 | * @param[in] options XPath options. |
| 8066 | * @return Root type. |
| 8067 | */ |
| 8068 | static enum lyxp_node_type |
| 8069 | lyxp_get_root_type(const struct lyd_node *ctx_node, const struct lysc_node *ctx_scnode, int options) |
| 8070 | { |
| 8071 | if (options & LYXP_SCNODE_ALL) { |
| 8072 | if (options & LYXP_SCNODE) { |
| 8073 | /* general root that can access everything */ |
| 8074 | return LYXP_NODE_ROOT; |
| 8075 | } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) { |
| 8076 | /* root context node can access only config data (because we said so, it is unspecified) */ |
| 8077 | return LYXP_NODE_ROOT_CONFIG; |
| 8078 | } else { |
| 8079 | return LYXP_NODE_ROOT; |
| 8080 | } |
| 8081 | } |
| 8082 | |
| 8083 | if (!ctx_node || (ctx_node->schema->flags & LYS_CONFIG_W)) { |
| 8084 | /* root context node can access only config data (because we said so, it is unspecified) */ |
| 8085 | return LYXP_NODE_ROOT_CONFIG; |
| 8086 | } |
| 8087 | |
| 8088 | return LYXP_NODE_ROOT; |
| 8089 | } |
| 8090 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8091 | LY_ERR |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 8092 | lyxp_eval(struct lyxp_expr *exp, LYD_FORMAT format, const struct lys_module *local_mod, const struct lyd_node *ctx_node, |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8093 | enum lyxp_node_type ctx_node_type, const struct lyd_node **trees, struct lyxp_set *set, int options) |
| 8094 | { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8095 | uint16_t exp_idx = 0; |
| 8096 | LY_ERR rc; |
| 8097 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 8098 | LY_CHECK_ARG_RET(NULL, exp, local_mod, set, LY_EINVAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8099 | |
| 8100 | /* prepare set for evaluation */ |
| 8101 | exp_idx = 0; |
| 8102 | memset(set, 0, sizeof *set); |
| 8103 | set->type = LYXP_SET_EMPTY; |
| 8104 | set_insert_node(set, (struct lyd_node *)ctx_node, 0, ctx_node_type, options); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 8105 | set->ctx = local_mod->ctx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8106 | set->ctx_node = ctx_node; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8107 | set->root_type = lyxp_get_root_type(ctx_node, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8108 | set->local_mod = local_mod; |
| 8109 | set->trees = trees; |
| 8110 | set->format = format; |
| 8111 | |
| 8112 | /* evaluate */ |
| 8113 | rc = eval_expr_select(exp, &exp_idx, 0, set, options); |
| 8114 | if (rc != LY_SUCCESS) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8115 | lyxp_set_cast(set, LYXP_SET_EMPTY); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8116 | } |
| 8117 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8118 | return rc; |
| 8119 | } |
| 8120 | |
| 8121 | #if 0 |
| 8122 | |
| 8123 | /* full xml printing of set elements, not used currently */ |
| 8124 | |
| 8125 | void |
| 8126 | lyxp_set_print_xml(FILE *f, struct lyxp_set *set) |
| 8127 | { |
| 8128 | uint32_t i; |
| 8129 | char *str_num; |
| 8130 | struct lyout out; |
| 8131 | |
| 8132 | memset(&out, 0, sizeof out); |
| 8133 | |
| 8134 | out.type = LYOUT_STREAM; |
| 8135 | out.method.f = f; |
| 8136 | |
| 8137 | switch (set->type) { |
| 8138 | case LYXP_SET_EMPTY: |
| 8139 | ly_print(&out, "Empty XPath set\n\n"); |
| 8140 | break; |
| 8141 | case LYXP_SET_BOOLEAN: |
| 8142 | ly_print(&out, "Boolean XPath set:\n"); |
| 8143 | ly_print(&out, "%s\n\n", set->value.bool ? "true" : "false"); |
| 8144 | break; |
| 8145 | case LYXP_SET_STRING: |
| 8146 | ly_print(&out, "String XPath set:\n"); |
| 8147 | ly_print(&out, "\"%s\"\n\n", set->value.str); |
| 8148 | break; |
| 8149 | case LYXP_SET_NUMBER: |
| 8150 | ly_print(&out, "Number XPath set:\n"); |
| 8151 | |
| 8152 | if (isnan(set->value.num)) { |
| 8153 | str_num = strdup("NaN"); |
| 8154 | } else if ((set->value.num == 0) || (set->value.num == -0.0f)) { |
| 8155 | str_num = strdup("0"); |
| 8156 | } else if (isinf(set->value.num) && !signbit(set->value.num)) { |
| 8157 | str_num = strdup("Infinity"); |
| 8158 | } else if (isinf(set->value.num) && signbit(set->value.num)) { |
| 8159 | str_num = strdup("-Infinity"); |
| 8160 | } else if ((long long)set->value.num == set->value.num) { |
| 8161 | if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) { |
| 8162 | str_num = NULL; |
| 8163 | } |
| 8164 | } else { |
| 8165 | if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) { |
| 8166 | str_num = NULL; |
| 8167 | } |
| 8168 | } |
| 8169 | if (!str_num) { |
| 8170 | LOGMEM; |
| 8171 | return; |
| 8172 | } |
| 8173 | ly_print(&out, "%s\n\n", str_num); |
| 8174 | free(str_num); |
| 8175 | break; |
| 8176 | case LYXP_SET_NODE_SET: |
| 8177 | ly_print(&out, "Node XPath set:\n"); |
| 8178 | |
| 8179 | for (i = 0; i < set->used; ++i) { |
| 8180 | ly_print(&out, "%d. ", i + 1); |
| 8181 | switch (set->node_type[i]) { |
| 8182 | case LYXP_NODE_ROOT_ALL: |
| 8183 | ly_print(&out, "ROOT all\n\n"); |
| 8184 | break; |
| 8185 | case LYXP_NODE_ROOT_CONFIG: |
| 8186 | ly_print(&out, "ROOT config\n\n"); |
| 8187 | break; |
| 8188 | case LYXP_NODE_ROOT_STATE: |
| 8189 | ly_print(&out, "ROOT state\n\n"); |
| 8190 | break; |
| 8191 | case LYXP_NODE_ROOT_NOTIF: |
| 8192 | ly_print(&out, "ROOT notification \"%s\"\n\n", set->value.nodes[i]->schema->name); |
| 8193 | break; |
| 8194 | case LYXP_NODE_ROOT_RPC: |
| 8195 | ly_print(&out, "ROOT rpc \"%s\"\n\n", set->value.nodes[i]->schema->name); |
| 8196 | break; |
| 8197 | case LYXP_NODE_ROOT_OUTPUT: |
| 8198 | ly_print(&out, "ROOT output \"%s\"\n\n", set->value.nodes[i]->schema->name); |
| 8199 | break; |
| 8200 | case LYXP_NODE_ELEM: |
| 8201 | ly_print(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name); |
| 8202 | xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT); |
| 8203 | ly_print(&out, "\n"); |
| 8204 | break; |
| 8205 | case LYXP_NODE_TEXT: |
| 8206 | ly_print(&out, "TEXT \"%s\"\n\n", ((struct lyd_node_leaf_list *)set->value.nodes[i])->value_str); |
| 8207 | break; |
| 8208 | case LYXP_NODE_ATTR: |
| 8209 | ly_print(&out, "ATTR \"%s\" = \"%s\"\n\n", set->value.attrs[i]->name, set->value.attrs[i]->value); |
| 8210 | break; |
| 8211 | } |
| 8212 | } |
| 8213 | break; |
| 8214 | } |
| 8215 | } |
| 8216 | |
| 8217 | #endif |
| 8218 | |
| 8219 | LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8220 | lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8221 | { |
| 8222 | long double num; |
| 8223 | char *str; |
| 8224 | LY_ERR rc; |
| 8225 | |
| 8226 | if (!set || (set->type == target)) { |
| 8227 | return LY_SUCCESS; |
| 8228 | } |
| 8229 | |
| 8230 | /* it's not possible to convert anything into a node set */ |
| 8231 | assert((target != LYXP_SET_NODE_SET) && ((set->type != LYXP_SET_SCNODE_SET) || (target == LYXP_SET_EMPTY))); |
| 8232 | |
| 8233 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 8234 | set_free_content(set); |
| 8235 | return LY_EINVAL; |
| 8236 | } |
| 8237 | |
| 8238 | /* to STRING */ |
| 8239 | if ((target == LYXP_SET_STRING) || ((target == LYXP_SET_NUMBER) |
| 8240 | && ((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_EMPTY)))) { |
| 8241 | switch (set->type) { |
| 8242 | case LYXP_SET_NUMBER: |
| 8243 | if (isnan(set->val.num)) { |
| 8244 | set->val.str = strdup("NaN"); |
| 8245 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 8246 | } else if ((set->val.num == 0) || (set->val.num == -0.0f)) { |
| 8247 | set->val.str = strdup("0"); |
| 8248 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 8249 | } else if (isinf(set->val.num) && !signbit(set->val.num)) { |
| 8250 | set->val.str = strdup("Infinity"); |
| 8251 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 8252 | } else if (isinf(set->val.num) && signbit(set->val.num)) { |
| 8253 | set->val.str = strdup("-Infinity"); |
| 8254 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 8255 | } else if ((long long)set->val.num == set->val.num) { |
| 8256 | if (asprintf(&str, "%lld", (long long)set->val.num) == -1) { |
| 8257 | LOGMEM_RET(set->ctx); |
| 8258 | } |
| 8259 | set->val.str = str; |
| 8260 | } else { |
| 8261 | if (asprintf(&str, "%03.1Lf", set->val.num) == -1) { |
| 8262 | LOGMEM_RET(set->ctx); |
| 8263 | } |
| 8264 | set->val.str = str; |
| 8265 | } |
| 8266 | break; |
| 8267 | case LYXP_SET_BOOLEAN: |
| 8268 | if (set->val.bool) { |
| 8269 | set->val.str = strdup("true"); |
| 8270 | } else { |
| 8271 | set->val.str = strdup("false"); |
| 8272 | } |
| 8273 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM); |
| 8274 | break; |
| 8275 | case LYXP_SET_NODE_SET: |
| 8276 | assert(set->used); |
| 8277 | |
| 8278 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8279 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8280 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8281 | rc = cast_node_set_to_string(set, &str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8282 | LY_CHECK_RET(rc); |
| 8283 | set_free_content(set); |
| 8284 | set->val.str = str; |
| 8285 | break; |
| 8286 | case LYXP_SET_EMPTY: |
| 8287 | set->val.str = strdup(""); |
| 8288 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM); |
| 8289 | break; |
| 8290 | default: |
| 8291 | LOGINT_RET(set->ctx); |
| 8292 | } |
| 8293 | set->type = LYXP_SET_STRING; |
| 8294 | } |
| 8295 | |
| 8296 | /* to NUMBER */ |
| 8297 | if (target == LYXP_SET_NUMBER) { |
| 8298 | switch (set->type) { |
| 8299 | case LYXP_SET_STRING: |
| 8300 | num = cast_string_to_number(set->val.str); |
| 8301 | set_free_content(set); |
| 8302 | set->val.num = num; |
| 8303 | break; |
| 8304 | case LYXP_SET_BOOLEAN: |
| 8305 | if (set->val.bool) { |
| 8306 | set->val.num = 1; |
| 8307 | } else { |
| 8308 | set->val.num = 0; |
| 8309 | } |
| 8310 | break; |
| 8311 | default: |
| 8312 | LOGINT_RET(set->ctx); |
| 8313 | } |
| 8314 | set->type = LYXP_SET_NUMBER; |
| 8315 | } |
| 8316 | |
| 8317 | /* to BOOLEAN */ |
| 8318 | if (target == LYXP_SET_BOOLEAN) { |
| 8319 | switch (set->type) { |
| 8320 | case LYXP_SET_NUMBER: |
| 8321 | if ((set->val.num == 0) || (set->val.num == -0.0f) || isnan(set->val.num)) { |
| 8322 | set->val.bool = 0; |
| 8323 | } else { |
| 8324 | set->val.bool = 1; |
| 8325 | } |
| 8326 | break; |
| 8327 | case LYXP_SET_STRING: |
| 8328 | if (set->val.str[0]) { |
| 8329 | set_free_content(set); |
| 8330 | set->val.bool = 1; |
| 8331 | } else { |
| 8332 | set_free_content(set); |
| 8333 | set->val.bool = 0; |
| 8334 | } |
| 8335 | break; |
| 8336 | case LYXP_SET_NODE_SET: |
| 8337 | set_free_content(set); |
| 8338 | |
| 8339 | assert(set->used); |
| 8340 | set->val.bool = 1; |
| 8341 | break; |
| 8342 | case LYXP_SET_EMPTY: |
| 8343 | set->val.bool = 0; |
| 8344 | break; |
| 8345 | default: |
| 8346 | LOGINT_RET(set->ctx); |
| 8347 | } |
| 8348 | set->type = LYXP_SET_BOOLEAN; |
| 8349 | } |
| 8350 | |
| 8351 | /* to EMPTY */ |
| 8352 | if (target == LYXP_SET_EMPTY) { |
| 8353 | set_free_content(set); |
| 8354 | set->type = LYXP_SET_EMPTY; |
| 8355 | } |
| 8356 | |
| 8357 | return LY_SUCCESS; |
| 8358 | } |
| 8359 | |
| 8360 | LY_ERR |
| 8361 | lyxp_atomize(struct lyxp_expr *exp, LYD_FORMAT format, const struct lys_module *local_mod, const struct lysc_node *ctx_scnode, |
| 8362 | enum lyxp_node_type ctx_scnode_type, struct lyxp_set *set, int options) |
| 8363 | { |
| 8364 | struct ly_ctx *ctx; |
| 8365 | uint16_t exp_idx = 0; |
| 8366 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8367 | LY_CHECK_ARG_RET(NULL, exp, local_mod, set, LY_EINVAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8368 | |
| 8369 | ctx = local_mod->ctx; |
| 8370 | |
| 8371 | /* prepare set for evaluation */ |
| 8372 | exp_idx = 0; |
| 8373 | memset(set, 0, sizeof *set); |
| 8374 | set->type = LYXP_SET_SCNODE_SET; |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 8375 | lyxp_set_scnode_insert_node(set, ctx_scnode, ctx_scnode_type); |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 8376 | set->val.scnodes[0].in_ctx = -2; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8377 | set->ctx = ctx; |
| 8378 | set->ctx_scnode = ctx_scnode; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8379 | set->root_type = lyxp_get_root_type(NULL, ctx_scnode, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8380 | set->local_mod = local_mod; |
| 8381 | set->format = format; |
| 8382 | |
| 8383 | /* evaluate */ |
| 8384 | return eval_expr_select(exp, &exp_idx, 0, set, options); |
| 8385 | } |