Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 1 | /** |
| 2 | * @file xpath.c |
| 3 | * @author Michal Vasko <mvasko@cesnet.cz> |
| 4 | * @brief YANG XPath evaluation functions |
| 5 | * |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6 | * Copyright (c) 2015 - 2020 CESNET, z.s.p.o. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 7 | * |
| 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * https://opensource.org/licenses/BSD-3-Clause |
| 13 | */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 14 | #define _GNU_SOURCE |
| 15 | |
| 16 | /* needed by libmath functions isfinite(), isinf(), isnan(), signbit(), ... */ |
| 17 | #define _ISOC99_SOURCE |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 18 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 19 | #include "xpath.h" |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 20 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 21 | #include <assert.h> |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 22 | #include <ctype.h> |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 23 | #include <errno.h> |
| 24 | #include <limits.h> |
| 25 | #include <math.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 26 | #include <stdint.h> |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 27 | #include <stdio.h> |
| 28 | #include <stdlib.h> |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 29 | #include <string.h> |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 30 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 31 | #include "common.h" |
| 32 | #include "context.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 33 | #include "dict.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 34 | #include "hash_table.h" |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 35 | #include "path.h" |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 36 | #include "plugins_types.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 37 | #include "printer.h" |
| 38 | #include "printer_data.h" |
| 39 | #include "tree.h" |
| 40 | #include "tree_data_internal.h" |
| 41 | #include "tree_schema_internal.h" |
| 42 | #include "xml.h" |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 43 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 44 | static LY_ERR reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx); |
| 45 | static LY_ERR eval_expr_select(struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype, struct lyxp_set *set, int options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 46 | |
| 47 | /** |
| 48 | * @brief Print the type of an XPath \p set. |
| 49 | * |
| 50 | * @param[in] set Set to use. |
| 51 | * @return Set type string. |
| 52 | */ |
| 53 | static const char * |
| 54 | print_set_type(struct lyxp_set *set) |
| 55 | { |
| 56 | switch (set->type) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 57 | case LYXP_SET_NODE_SET: |
| 58 | return "node set"; |
| 59 | case LYXP_SET_SCNODE_SET: |
| 60 | return "schema node set"; |
| 61 | case LYXP_SET_BOOLEAN: |
| 62 | return "boolean"; |
| 63 | case LYXP_SET_NUMBER: |
| 64 | return "number"; |
| 65 | case LYXP_SET_STRING: |
| 66 | return "string"; |
| 67 | } |
| 68 | |
| 69 | return NULL; |
| 70 | } |
| 71 | |
Michal Vasko | 24cddf8 | 2020-06-01 08:17:01 +0200 | [diff] [blame] | 72 | const char * |
| 73 | lyxp_print_token(enum lyxp_token tok) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 74 | { |
| 75 | switch (tok) { |
| 76 | case LYXP_TOKEN_PAR1: |
| 77 | return "("; |
| 78 | case LYXP_TOKEN_PAR2: |
| 79 | return ")"; |
| 80 | case LYXP_TOKEN_BRACK1: |
| 81 | return "["; |
| 82 | case LYXP_TOKEN_BRACK2: |
| 83 | return "]"; |
| 84 | case LYXP_TOKEN_DOT: |
| 85 | return "."; |
| 86 | case LYXP_TOKEN_DDOT: |
| 87 | return ".."; |
| 88 | case LYXP_TOKEN_AT: |
| 89 | return "@"; |
| 90 | case LYXP_TOKEN_COMMA: |
| 91 | return ","; |
| 92 | case LYXP_TOKEN_NAMETEST: |
| 93 | return "NameTest"; |
| 94 | case LYXP_TOKEN_NODETYPE: |
| 95 | return "NodeType"; |
| 96 | case LYXP_TOKEN_FUNCNAME: |
| 97 | return "FunctionName"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 98 | case LYXP_TOKEN_OPER_LOG: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 99 | return "Operator(Logic)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 100 | case LYXP_TOKEN_OPER_EQUAL: |
| 101 | return "Operator(Equal)"; |
| 102 | case LYXP_TOKEN_OPER_NEQUAL: |
| 103 | return "Operator(Non-equal)"; |
| 104 | case LYXP_TOKEN_OPER_COMP: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 105 | return "Operator(Comparison)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 106 | case LYXP_TOKEN_OPER_MATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 107 | return "Operator(Math)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 108 | case LYXP_TOKEN_OPER_UNI: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 109 | return "Operator(Union)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 110 | case LYXP_TOKEN_OPER_PATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 111 | return "Operator(Path)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 112 | case LYXP_TOKEN_OPER_RPATH: |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 113 | return "Operator(Recursive Path)"; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 114 | case LYXP_TOKEN_LITERAL: |
| 115 | return "Literal"; |
| 116 | case LYXP_TOKEN_NUMBER: |
| 117 | return "Number"; |
| 118 | default: |
| 119 | LOGINT(NULL); |
| 120 | return ""; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * @brief Print the whole expression \p exp to debug output. |
| 126 | * |
| 127 | * @param[in] exp Expression to use. |
| 128 | */ |
| 129 | static void |
| 130 | print_expr_struct_debug(struct lyxp_expr *exp) |
| 131 | { |
| 132 | uint16_t i, j; |
| 133 | char tmp[128]; |
| 134 | |
| 135 | if (!exp || (ly_log_level < LY_LLDBG)) { |
| 136 | return; |
| 137 | } |
| 138 | |
| 139 | LOGDBG(LY_LDGXPATH, "expression \"%s\":", exp->expr); |
| 140 | for (i = 0; i < exp->used; ++i) { |
Michal Vasko | 24cddf8 | 2020-06-01 08:17:01 +0200 | [diff] [blame] | 141 | sprintf(tmp, "\ttoken %s, in expression \"%.*s\"", lyxp_print_token(exp->tokens[i]), exp->tok_len[i], |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 142 | &exp->expr[exp->tok_pos[i]]); |
| 143 | if (exp->repeat[i]) { |
| 144 | sprintf(tmp + strlen(tmp), " (repeat %d", exp->repeat[i][0]); |
| 145 | for (j = 1; exp->repeat[i][j]; ++j) { |
| 146 | sprintf(tmp + strlen(tmp), ", %d", exp->repeat[i][j]); |
| 147 | } |
| 148 | strcat(tmp, ")"); |
| 149 | } |
| 150 | LOGDBG(LY_LDGXPATH, tmp); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | #ifndef NDEBUG |
| 155 | |
| 156 | /** |
| 157 | * @brief Print XPath set content to debug output. |
| 158 | * |
| 159 | * @param[in] set Set to print. |
| 160 | */ |
| 161 | static void |
| 162 | print_set_debug(struct lyxp_set *set) |
| 163 | { |
| 164 | uint32_t i; |
| 165 | char *str; |
| 166 | int dynamic; |
| 167 | struct lyxp_set_node *item; |
| 168 | struct lyxp_set_scnode *sitem; |
| 169 | |
| 170 | if (ly_log_level < LY_LLDBG) { |
| 171 | return; |
| 172 | } |
| 173 | |
| 174 | switch (set->type) { |
| 175 | case LYXP_SET_NODE_SET: |
| 176 | LOGDBG(LY_LDGXPATH, "set NODE SET:"); |
| 177 | for (i = 0; i < set->used; ++i) { |
| 178 | item = &set->val.nodes[i]; |
| 179 | |
| 180 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 181 | case LYXP_NODE_NONE: |
| 182 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): NONE", i + 1, item->pos); |
| 183 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 184 | case LYXP_NODE_ROOT: |
| 185 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT", i + 1, item->pos); |
| 186 | break; |
| 187 | case LYXP_NODE_ROOT_CONFIG: |
| 188 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT CONFIG", i + 1, item->pos); |
| 189 | break; |
| 190 | case LYXP_NODE_ELEM: |
| 191 | if ((item->node->schema->nodetype == LYS_LIST) |
| 192 | && (((struct lyd_node_inner *)item->node)->child->schema->nodetype == LYS_LEAF)) { |
| 193 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (1st child val: %s)", i + 1, item->pos, |
| 194 | item->node->schema->name, |
| 195 | (str = (char *)lyd_value2str((struct lyd_node_term *)lyd_node_children(item->node), &dynamic))); |
| 196 | if (dynamic) { |
| 197 | free(str); |
| 198 | } |
| 199 | } else if (((struct lyd_node_inner *)item->node)->schema->nodetype == LYS_LEAFLIST) { |
| 200 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (val: %s)", i + 1, item->pos, |
| 201 | item->node->schema->name, |
| 202 | (str = (char *)lyd_value2str((struct lyd_node_term *)item->node, &dynamic))); |
| 203 | if (dynamic) { |
| 204 | free(str); |
| 205 | } |
| 206 | } else { |
| 207 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s", i + 1, item->pos, item->node->schema->name); |
| 208 | } |
| 209 | break; |
| 210 | case LYXP_NODE_TEXT: |
| 211 | if (item->node->schema->nodetype & LYS_ANYDATA) { |
| 212 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT <%s>", i + 1, item->pos, |
| 213 | item->node->schema->nodetype == LYS_ANYXML ? "anyxml" : "anydata"); |
| 214 | } else { |
| 215 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT %s", i + 1, item->pos, |
| 216 | (str = (char *)lyd_value2str((struct lyd_node_term *)item->node, &dynamic))); |
| 217 | if (dynamic) { |
| 218 | free(str); |
| 219 | } |
| 220 | } |
| 221 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 222 | case LYXP_NODE_META: |
| 223 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): META %s = %s", i + 1, item->pos, set->val.meta[i].meta->name, |
| 224 | set->val.meta[i].meta->value); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 225 | break; |
| 226 | } |
| 227 | } |
| 228 | break; |
| 229 | |
| 230 | case LYXP_SET_SCNODE_SET: |
| 231 | LOGDBG(LY_LDGXPATH, "set SCNODE SET:"); |
| 232 | for (i = 0; i < set->used; ++i) { |
| 233 | sitem = &set->val.scnodes[i]; |
| 234 | |
| 235 | switch (sitem->type) { |
| 236 | case LYXP_NODE_ROOT: |
| 237 | LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT", i + 1, sitem->in_ctx); |
| 238 | break; |
| 239 | case LYXP_NODE_ROOT_CONFIG: |
| 240 | LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT CONFIG", i + 1, sitem->in_ctx); |
| 241 | break; |
| 242 | case LYXP_NODE_ELEM: |
| 243 | LOGDBG(LY_LDGXPATH, "\t%d (%u): ELEM %s", i + 1, sitem->in_ctx, sitem->scnode->name); |
| 244 | break; |
| 245 | default: |
| 246 | LOGINT(NULL); |
| 247 | break; |
| 248 | } |
| 249 | } |
| 250 | break; |
| 251 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 252 | case LYXP_SET_BOOLEAN: |
| 253 | LOGDBG(LY_LDGXPATH, "set BOOLEAN"); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 254 | LOGDBG(LY_LDGXPATH, "\t%s", (set->val.bln ? "true" : "false")); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 255 | break; |
| 256 | |
| 257 | case LYXP_SET_STRING: |
| 258 | LOGDBG(LY_LDGXPATH, "set STRING"); |
| 259 | LOGDBG(LY_LDGXPATH, "\t%s", set->val.str); |
| 260 | break; |
| 261 | |
| 262 | case LYXP_SET_NUMBER: |
| 263 | LOGDBG(LY_LDGXPATH, "set NUMBER"); |
| 264 | |
| 265 | if (isnan(set->val.num)) { |
| 266 | str = strdup("NaN"); |
| 267 | } else if ((set->val.num == 0) || (set->val.num == -0.0f)) { |
| 268 | str = strdup("0"); |
| 269 | } else if (isinf(set->val.num) && !signbit(set->val.num)) { |
| 270 | str = strdup("Infinity"); |
| 271 | } else if (isinf(set->val.num) && signbit(set->val.num)) { |
| 272 | str = strdup("-Infinity"); |
| 273 | } else if ((long long)set->val.num == set->val.num) { |
| 274 | if (asprintf(&str, "%lld", (long long)set->val.num) == -1) { |
| 275 | str = NULL; |
| 276 | } |
| 277 | } else { |
| 278 | if (asprintf(&str, "%03.1Lf", set->val.num) == -1) { |
| 279 | str = NULL; |
| 280 | } |
| 281 | } |
| 282 | LY_CHECK_ERR_RET(!str, LOGMEM(NULL), ); |
| 283 | |
| 284 | LOGDBG(LY_LDGXPATH, "\t%s", str); |
| 285 | free(str); |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | #endif |
| 290 | |
| 291 | /** |
| 292 | * @brief Realloc the string \p str. |
| 293 | * |
| 294 | * @param[in] ctx libyang context for logging. |
| 295 | * @param[in] needed How much free space is required. |
| 296 | * @param[in,out] str Pointer to the string to use. |
| 297 | * @param[in,out] used Used bytes in \p str. |
| 298 | * @param[in,out] size Allocated bytes in \p str. |
| 299 | * @return LY_ERR |
| 300 | */ |
| 301 | static LY_ERR |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 302 | cast_string_realloc(const struct ly_ctx *ctx, uint16_t needed, char **str, uint16_t *used, uint16_t *size) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 303 | { |
| 304 | if (*size - *used < needed) { |
| 305 | do { |
| 306 | if ((UINT16_MAX - *size) < LYXP_STRING_CAST_SIZE_STEP) { |
| 307 | LOGERR(ctx, LY_EINVAL, "XPath string length limit (%u) reached.", UINT16_MAX); |
| 308 | return LY_EINVAL; |
| 309 | } |
| 310 | *size += LYXP_STRING_CAST_SIZE_STEP; |
| 311 | } while (*size - *used < needed); |
| 312 | *str = ly_realloc(*str, *size * sizeof(char)); |
| 313 | LY_CHECK_ERR_RET(!(*str), LOGMEM(ctx), LY_EMEM); |
| 314 | } |
| 315 | |
| 316 | return LY_SUCCESS; |
| 317 | } |
| 318 | |
| 319 | /** |
| 320 | * @brief Cast nodes recursively to one string @p str. |
| 321 | * |
| 322 | * @param[in] node Node to cast. |
| 323 | * @param[in] fake_cont Whether to put the data into a "fake" container. |
| 324 | * @param[in] root_type Type of the XPath root. |
| 325 | * @param[in] indent Current indent. |
| 326 | * @param[in,out] str Resulting string. |
| 327 | * @param[in,out] used Used bytes in @p str. |
| 328 | * @param[in,out] size Allocated bytes in @p str. |
| 329 | * @return LY_ERR |
| 330 | */ |
| 331 | static LY_ERR |
| 332 | cast_string_recursive(const struct lyd_node *node, int fake_cont, enum lyxp_node_type root_type, uint16_t indent, char **str, |
| 333 | uint16_t *used, uint16_t *size) |
| 334 | { |
| 335 | char *buf, *line, *ptr; |
| 336 | const char *value_str; |
| 337 | int dynamic; |
| 338 | const struct lyd_node *child; |
| 339 | struct lyd_node_any *any; |
| 340 | LY_ERR rc; |
| 341 | |
| 342 | if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) { |
| 343 | return LY_SUCCESS; |
| 344 | } |
| 345 | |
| 346 | if (fake_cont) { |
| 347 | rc = cast_string_realloc(LYD_NODE_CTX(node), 1, str, used, size); |
| 348 | LY_CHECK_RET(rc); |
| 349 | strcpy(*str + (*used - 1), "\n"); |
| 350 | ++(*used); |
| 351 | |
| 352 | ++indent; |
| 353 | } |
| 354 | |
| 355 | switch (node->schema->nodetype) { |
| 356 | case LYS_CONTAINER: |
| 357 | case LYS_LIST: |
| 358 | case LYS_RPC: |
| 359 | case LYS_NOTIF: |
| 360 | rc = cast_string_realloc(LYD_NODE_CTX(node), 1, str, used, size); |
| 361 | LY_CHECK_RET(rc); |
| 362 | strcpy(*str + (*used - 1), "\n"); |
| 363 | ++(*used); |
| 364 | |
| 365 | for (child = lyd_node_children(node); child; child = child->next) { |
| 366 | rc = cast_string_recursive(child, 0, root_type, indent + 1, str, used, size); |
| 367 | LY_CHECK_RET(rc); |
| 368 | } |
| 369 | |
| 370 | break; |
| 371 | |
| 372 | case LYS_LEAF: |
| 373 | case LYS_LEAFLIST: |
| 374 | value_str = lyd_value2str(((struct lyd_node_term *)node), &dynamic); |
| 375 | |
| 376 | /* print indent */ |
| 377 | rc = cast_string_realloc(LYD_NODE_CTX(node), indent * 2 + strlen(value_str) + 1, str, used, size); |
| 378 | if (rc != LY_SUCCESS) { |
| 379 | if (dynamic) { |
| 380 | free((char *)value_str); |
| 381 | } |
| 382 | return rc; |
| 383 | } |
| 384 | memset(*str + (*used - 1), ' ', indent * 2); |
| 385 | *used += indent * 2; |
| 386 | |
| 387 | /* print value */ |
| 388 | if (*used == 1) { |
| 389 | sprintf(*str + (*used - 1), "%s", value_str); |
| 390 | *used += strlen(value_str); |
| 391 | } else { |
| 392 | sprintf(*str + (*used - 1), "%s\n", value_str); |
| 393 | *used += strlen(value_str) + 1; |
| 394 | } |
| 395 | if (dynamic) { |
| 396 | free((char *)value_str); |
| 397 | } |
| 398 | |
| 399 | break; |
| 400 | |
| 401 | case LYS_ANYXML: |
| 402 | case LYS_ANYDATA: |
| 403 | any = (struct lyd_node_any *)node; |
| 404 | if (!(void *)any->value.tree) { |
| 405 | /* no content */ |
| 406 | buf = strdup(""); |
| 407 | LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_NODE_CTX(node)), LY_EMEM); |
| 408 | } else { |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 409 | struct ly_out *out; |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 410 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 411 | switch (any->value_type) { |
| 412 | case LYD_ANYDATA_STRING: |
| 413 | case LYD_ANYDATA_XML: |
| 414 | case LYD_ANYDATA_JSON: |
| 415 | buf = strdup(any->value.json); |
| 416 | LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_NODE_CTX(node)), LY_EMEM); |
| 417 | break; |
| 418 | case LYD_ANYDATA_DATATREE: |
Radek Krejci | 84ce7b1 | 2020-06-11 17:28:25 +0200 | [diff] [blame] | 419 | LY_CHECK_RET(ly_out_new_memory(&buf, 0, &out)); |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 420 | rc = lyd_print(out, any->value.tree, LYD_XML, LYDP_WITHSIBLINGS); |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 421 | ly_out_free(out, NULL, 0); |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 422 | LY_CHECK_RET(rc < 0, -rc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 423 | break; |
| 424 | /* TODO case LYD_ANYDATA_LYB: |
| 425 | LOGERR(LYD_NODE_CTX(node), LY_EINVAL, "Cannot convert LYB anydata into string."); |
| 426 | return -1;*/ |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | line = strtok_r(buf, "\n", &ptr); |
| 431 | do { |
| 432 | rc = cast_string_realloc(LYD_NODE_CTX(node), indent * 2 + strlen(line) + 1, str, used, size); |
| 433 | if (rc != LY_SUCCESS) { |
| 434 | free(buf); |
| 435 | return rc; |
| 436 | } |
| 437 | memset(*str + (*used - 1), ' ', indent * 2); |
| 438 | *used += indent * 2; |
| 439 | |
| 440 | strcpy(*str + (*used - 1), line); |
| 441 | *used += strlen(line); |
| 442 | |
| 443 | strcpy(*str + (*used - 1), "\n"); |
| 444 | *used += 1; |
| 445 | } while ((line = strtok_r(NULL, "\n", &ptr))); |
| 446 | |
| 447 | free(buf); |
| 448 | break; |
| 449 | |
| 450 | default: |
| 451 | LOGINT_RET(LYD_NODE_CTX(node)); |
| 452 | } |
| 453 | |
| 454 | if (fake_cont) { |
| 455 | rc = cast_string_realloc(LYD_NODE_CTX(node), 1, str, used, size); |
| 456 | LY_CHECK_RET(rc); |
| 457 | strcpy(*str + (*used - 1), "\n"); |
| 458 | ++(*used); |
| 459 | |
| 460 | --indent; |
| 461 | } |
| 462 | |
| 463 | return LY_SUCCESS; |
| 464 | } |
| 465 | |
| 466 | /** |
| 467 | * @brief Cast an element into a string. |
| 468 | * |
| 469 | * @param[in] node Node to cast. |
| 470 | * @param[in] fake_cont Whether to put the data into a "fake" container. |
| 471 | * @param[in] root_type Type of the XPath root. |
| 472 | * @param[out] str Element cast to dynamically-allocated string. |
| 473 | * @return LY_ERR |
| 474 | */ |
| 475 | static LY_ERR |
| 476 | cast_string_elem(struct lyd_node *node, int fake_cont, enum lyxp_node_type root_type, char **str) |
| 477 | { |
| 478 | uint16_t used, size; |
| 479 | LY_ERR rc; |
| 480 | |
| 481 | *str = malloc(LYXP_STRING_CAST_SIZE_START * sizeof(char)); |
| 482 | LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_NODE_CTX(node)), LY_EMEM); |
| 483 | (*str)[0] = '\0'; |
| 484 | used = 1; |
| 485 | size = LYXP_STRING_CAST_SIZE_START; |
| 486 | |
| 487 | rc = cast_string_recursive(node, fake_cont, root_type, 0, str, &used, &size); |
| 488 | if (rc != LY_SUCCESS) { |
| 489 | free(*str); |
| 490 | return rc; |
| 491 | } |
| 492 | |
| 493 | if (size > used) { |
| 494 | *str = ly_realloc(*str, used * sizeof(char)); |
| 495 | LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_NODE_CTX(node)), LY_EMEM); |
| 496 | } |
| 497 | return LY_SUCCESS; |
| 498 | } |
| 499 | |
| 500 | /** |
| 501 | * @brief Cast a LYXP_SET_NODE_SET set into a string. |
| 502 | * Context position aware. |
| 503 | * |
| 504 | * @param[in] set Set to cast. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 505 | * @param[out] str Cast dynamically-allocated string. |
| 506 | * @return LY_ERR |
| 507 | */ |
| 508 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 509 | cast_node_set_to_string(struct lyxp_set *set, char **str) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 510 | { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 511 | int dynamic; |
| 512 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 513 | switch (set->val.nodes[0].type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 514 | case LYXP_NODE_NONE: |
| 515 | /* invalid */ |
| 516 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 517 | case LYXP_NODE_ROOT: |
| 518 | case LYXP_NODE_ROOT_CONFIG: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 519 | return cast_string_elem(set->val.nodes[0].node, 1, set->root_type, str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 520 | case LYXP_NODE_ELEM: |
| 521 | case LYXP_NODE_TEXT: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 522 | return cast_string_elem(set->val.nodes[0].node, 0, set->root_type, str); |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 523 | case LYXP_NODE_META: |
| 524 | *str = (char *)lyd_meta2str(set->val.meta[0].meta, &dynamic); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 525 | if (!dynamic) { |
| 526 | *str = strdup(*str); |
| 527 | if (!*str) { |
| 528 | LOGMEM_RET(set->ctx); |
| 529 | } |
| 530 | } |
| 531 | return LY_SUCCESS; |
| 532 | } |
| 533 | |
| 534 | LOGINT_RET(set->ctx); |
| 535 | } |
| 536 | |
| 537 | /** |
| 538 | * @brief Cast a string into an XPath number. |
| 539 | * |
| 540 | * @param[in] str String to use. |
| 541 | * @return Cast number. |
| 542 | */ |
| 543 | static long double |
| 544 | cast_string_to_number(const char *str) |
| 545 | { |
| 546 | long double num; |
| 547 | char *ptr; |
| 548 | |
| 549 | errno = 0; |
| 550 | num = strtold(str, &ptr); |
| 551 | if (errno || *ptr) { |
| 552 | num = NAN; |
| 553 | } |
| 554 | return num; |
| 555 | } |
| 556 | |
| 557 | /** |
| 558 | * @brief Callback for checking value equality. |
| 559 | * |
| 560 | * @param[in] val1_p First value. |
| 561 | * @param[in] val2_p Second value. |
| 562 | * @param[in] mod Whether hash table is being modified. |
| 563 | * @param[in] cb_data Callback data. |
| 564 | * @return 0 if not equal, non-zero if equal. |
| 565 | */ |
| 566 | static int |
| 567 | set_values_equal_cb(void *val1_p, void *val2_p, int UNUSED(mod), void *UNUSED(cb_data)) |
| 568 | { |
| 569 | struct lyxp_set_hash_node *val1, *val2; |
| 570 | |
| 571 | val1 = (struct lyxp_set_hash_node *)val1_p; |
| 572 | val2 = (struct lyxp_set_hash_node *)val2_p; |
| 573 | |
| 574 | if ((val1->node == val2->node) && (val1->type == val2->type)) { |
| 575 | return 1; |
| 576 | } |
| 577 | |
| 578 | return 0; |
| 579 | } |
| 580 | |
| 581 | /** |
| 582 | * @brief Insert node and its hash into set. |
| 583 | * |
| 584 | * @param[in] set et to insert to. |
| 585 | * @param[in] node Node with hash. |
| 586 | * @param[in] type Node type. |
| 587 | */ |
| 588 | static void |
| 589 | set_insert_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type) |
| 590 | { |
| 591 | int r; |
| 592 | uint32_t i, hash; |
| 593 | struct lyxp_set_hash_node hnode; |
| 594 | |
| 595 | if (!set->ht && (set->used >= LYD_HT_MIN_ITEMS)) { |
| 596 | /* create hash table and add all the nodes */ |
| 597 | set->ht = lyht_new(1, sizeof(struct lyxp_set_hash_node), set_values_equal_cb, NULL, 1); |
| 598 | for (i = 0; i < set->used; ++i) { |
| 599 | hnode.node = set->val.nodes[i].node; |
| 600 | hnode.type = set->val.nodes[i].type; |
| 601 | |
| 602 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 603 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 604 | hash = dict_hash_multi(hash, NULL, 0); |
| 605 | |
| 606 | r = lyht_insert(set->ht, &hnode, hash, NULL); |
| 607 | assert(!r); |
| 608 | (void)r; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 609 | |
Michal Vasko | 9d6befd | 2019-12-11 14:56:56 +0100 | [diff] [blame] | 610 | if (hnode.node == node) { |
| 611 | /* it was just added, do not add it twice */ |
| 612 | node = NULL; |
| 613 | } |
| 614 | } |
| 615 | } |
| 616 | |
| 617 | if (set->ht && node) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 618 | /* add the new node into hash table */ |
| 619 | hnode.node = node; |
| 620 | hnode.type = type; |
| 621 | |
| 622 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 623 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 624 | hash = dict_hash_multi(hash, NULL, 0); |
| 625 | |
| 626 | r = lyht_insert(set->ht, &hnode, hash, NULL); |
| 627 | assert(!r); |
| 628 | (void)r; |
| 629 | } |
| 630 | } |
| 631 | |
| 632 | /** |
| 633 | * @brief Remove node and its hash from set. |
| 634 | * |
| 635 | * @param[in] set Set to remove from. |
| 636 | * @param[in] node Node to remove. |
| 637 | * @param[in] type Node type. |
| 638 | */ |
| 639 | static void |
| 640 | set_remove_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type) |
| 641 | { |
| 642 | int r; |
| 643 | struct lyxp_set_hash_node hnode; |
| 644 | uint32_t hash; |
| 645 | |
| 646 | if (set->ht) { |
| 647 | hnode.node = node; |
| 648 | hnode.type = type; |
| 649 | |
| 650 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 651 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 652 | hash = dict_hash_multi(hash, NULL, 0); |
| 653 | |
| 654 | r = lyht_remove(set->ht, &hnode, hash); |
| 655 | assert(!r); |
| 656 | (void)r; |
| 657 | |
| 658 | if (!set->ht->used) { |
| 659 | lyht_free(set->ht); |
| 660 | set->ht = NULL; |
| 661 | } |
| 662 | } |
| 663 | } |
| 664 | |
| 665 | /** |
| 666 | * @brief Check whether node is in set based on its hash. |
| 667 | * |
| 668 | * @param[in] set Set to search in. |
| 669 | * @param[in] node Node to search for. |
| 670 | * @param[in] type Node type. |
| 671 | * @param[in] skip_idx Index in @p set to skip. |
| 672 | * @return LY_ERR |
| 673 | */ |
| 674 | static LY_ERR |
| 675 | set_dup_node_hash_check(const struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type, int skip_idx) |
| 676 | { |
| 677 | struct lyxp_set_hash_node hnode, *match_p; |
| 678 | uint32_t hash; |
| 679 | |
| 680 | hnode.node = node; |
| 681 | hnode.type = type; |
| 682 | |
| 683 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 684 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 685 | hash = dict_hash_multi(hash, NULL, 0); |
| 686 | |
| 687 | if (!lyht_find(set->ht, &hnode, hash, (void **)&match_p)) { |
| 688 | if ((skip_idx > -1) && (set->val.nodes[skip_idx].node == match_p->node) && (set->val.nodes[skip_idx].type == match_p->type)) { |
| 689 | /* we found it on the index that should be skipped, find another */ |
| 690 | hnode = *match_p; |
| 691 | if (lyht_find_next(set->ht, &hnode, hash, (void **)&match_p)) { |
| 692 | /* none other found */ |
| 693 | return LY_SUCCESS; |
| 694 | } |
| 695 | } |
| 696 | |
| 697 | return LY_EEXIST; |
| 698 | } |
| 699 | |
| 700 | /* not found */ |
| 701 | return LY_SUCCESS; |
| 702 | } |
| 703 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 704 | void |
| 705 | lyxp_set_free_content(struct lyxp_set *set) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 706 | { |
| 707 | if (!set) { |
| 708 | return; |
| 709 | } |
| 710 | |
| 711 | if (set->type == LYXP_SET_NODE_SET) { |
| 712 | free(set->val.nodes); |
| 713 | lyht_free(set->ht); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 714 | } else if (set->type == LYXP_SET_SCNODE_SET) { |
| 715 | free(set->val.scnodes); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 716 | lyht_free(set->ht); |
| 717 | } else { |
| 718 | if (set->type == LYXP_SET_STRING) { |
| 719 | free(set->val.str); |
| 720 | } |
| 721 | set->type = LYXP_SET_NODE_SET; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 722 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 723 | |
| 724 | set->val.nodes = NULL; |
| 725 | set->used = 0; |
| 726 | set->size = 0; |
| 727 | set->ht = NULL; |
| 728 | set->ctx_pos = 0; |
| 729 | set->ctx_pos = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 730 | } |
| 731 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 732 | /** |
| 733 | * @brief Free dynamically-allocated set. |
| 734 | * |
| 735 | * @param[in] set Set to free. |
| 736 | */ |
| 737 | static void |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 738 | lyxp_set_free(struct lyxp_set *set) |
| 739 | { |
| 740 | if (!set) { |
| 741 | return; |
| 742 | } |
| 743 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 744 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 745 | free(set); |
| 746 | } |
| 747 | |
| 748 | /** |
| 749 | * @brief Initialize set context. |
| 750 | * |
| 751 | * @param[in] new Set to initialize. |
| 752 | * @param[in] set Arbitrary initialized set. |
| 753 | */ |
| 754 | static void |
| 755 | set_init(struct lyxp_set *new, struct lyxp_set *set) |
| 756 | { |
| 757 | memset(new, 0, sizeof *new); |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 758 | if (set) { |
| 759 | new->ctx = set->ctx; |
| 760 | new->ctx_node = set->ctx_node; |
Michal Vasko | 588112f | 2019-12-10 14:51:53 +0100 | [diff] [blame] | 761 | new->root_type = set->root_type; |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 762 | new->local_mod = set->local_mod; |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 763 | new->tree = set->tree; |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 764 | new->format = set->format; |
| 765 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 766 | } |
| 767 | |
| 768 | /** |
| 769 | * @brief Create a deep copy of a set. |
| 770 | * |
| 771 | * @param[in] set Set to copy. |
| 772 | * @return Copy of @p set. |
| 773 | */ |
| 774 | static struct lyxp_set * |
| 775 | set_copy(struct lyxp_set *set) |
| 776 | { |
| 777 | struct lyxp_set *ret; |
| 778 | uint16_t i; |
Michal Vasko | ba71654 | 2019-12-16 10:01:58 +0100 | [diff] [blame] | 779 | int idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 780 | |
| 781 | if (!set) { |
| 782 | return NULL; |
| 783 | } |
| 784 | |
| 785 | ret = malloc(sizeof *ret); |
| 786 | LY_CHECK_ERR_RET(!ret, LOGMEM(set->ctx), NULL); |
| 787 | set_init(ret, set); |
| 788 | |
| 789 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 790 | ret->type = set->type; |
| 791 | |
| 792 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | ba71654 | 2019-12-16 10:01:58 +0100 | [diff] [blame] | 793 | if ((set->val.scnodes[i].in_ctx == 1) || (set->val.scnodes[i].in_ctx == -2)) { |
| 794 | idx = lyxp_set_scnode_insert_node(ret, set->val.scnodes[i].scnode, set->val.scnodes[i].type); |
Michal Vasko | 3f27c52 | 2020-01-06 08:37:49 +0100 | [diff] [blame] | 795 | /* coverity seems to think scnodes can be NULL */ |
| 796 | if ((idx == -1) || !ret->val.scnodes) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 797 | lyxp_set_free(ret); |
| 798 | return NULL; |
| 799 | } |
Michal Vasko | ba71654 | 2019-12-16 10:01:58 +0100 | [diff] [blame] | 800 | ret->val.scnodes[idx].in_ctx = set->val.scnodes[i].in_ctx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 801 | } |
| 802 | } |
| 803 | } else if (set->type == LYXP_SET_NODE_SET) { |
| 804 | ret->type = set->type; |
| 805 | ret->val.nodes = malloc(set->used * sizeof *ret->val.nodes); |
| 806 | LY_CHECK_ERR_RET(!ret->val.nodes, LOGMEM(set->ctx); free(ret), NULL); |
| 807 | memcpy(ret->val.nodes, set->val.nodes, set->used * sizeof *ret->val.nodes); |
| 808 | |
| 809 | ret->used = ret->size = set->used; |
| 810 | ret->ctx_pos = set->ctx_pos; |
| 811 | ret->ctx_size = set->ctx_size; |
| 812 | ret->ht = lyht_dup(set->ht); |
| 813 | } else { |
| 814 | memcpy(ret, set, sizeof *ret); |
| 815 | if (set->type == LYXP_SET_STRING) { |
| 816 | ret->val.str = strdup(set->val.str); |
| 817 | LY_CHECK_ERR_RET(!ret->val.str, LOGMEM(set->ctx); free(ret), NULL); |
| 818 | } |
| 819 | } |
| 820 | |
| 821 | return ret; |
| 822 | } |
| 823 | |
| 824 | /** |
| 825 | * @brief Fill XPath set with a string. Any current data are disposed of. |
| 826 | * |
| 827 | * @param[in] set Set to fill. |
| 828 | * @param[in] string String to fill into \p set. |
| 829 | * @param[in] str_len Length of \p string. 0 is a valid value! |
| 830 | */ |
| 831 | static void |
| 832 | set_fill_string(struct lyxp_set *set, const char *string, uint16_t str_len) |
| 833 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 834 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 835 | |
| 836 | set->type = LYXP_SET_STRING; |
| 837 | if ((str_len == 0) && (string[0] != '\0')) { |
| 838 | string = ""; |
| 839 | } |
| 840 | set->val.str = strndup(string, str_len); |
| 841 | } |
| 842 | |
| 843 | /** |
| 844 | * @brief Fill XPath set with a number. Any current data are disposed of. |
| 845 | * |
| 846 | * @param[in] set Set to fill. |
| 847 | * @param[in] number Number to fill into \p set. |
| 848 | */ |
| 849 | static void |
| 850 | set_fill_number(struct lyxp_set *set, long double number) |
| 851 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 852 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 853 | |
| 854 | set->type = LYXP_SET_NUMBER; |
| 855 | set->val.num = number; |
| 856 | } |
| 857 | |
| 858 | /** |
| 859 | * @brief Fill XPath set with a boolean. Any current data are disposed of. |
| 860 | * |
| 861 | * @param[in] set Set to fill. |
| 862 | * @param[in] boolean Boolean to fill into \p set. |
| 863 | */ |
| 864 | static void |
| 865 | set_fill_boolean(struct lyxp_set *set, int boolean) |
| 866 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 867 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 868 | |
| 869 | set->type = LYXP_SET_BOOLEAN; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 870 | set->val.bln = boolean; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 871 | } |
| 872 | |
| 873 | /** |
| 874 | * @brief Fill XPath set with the value from another set (deep assign). |
| 875 | * Any current data are disposed of. |
| 876 | * |
| 877 | * @param[in] trg Set to fill. |
| 878 | * @param[in] src Source set to copy into \p trg. |
| 879 | */ |
| 880 | static void |
| 881 | set_fill_set(struct lyxp_set *trg, struct lyxp_set *src) |
| 882 | { |
| 883 | if (!trg || !src) { |
| 884 | return; |
| 885 | } |
| 886 | |
| 887 | if (trg->type == LYXP_SET_NODE_SET) { |
| 888 | free(trg->val.nodes); |
| 889 | } else if (trg->type == LYXP_SET_STRING) { |
| 890 | free(trg->val.str); |
| 891 | } |
| 892 | set_init(trg, src); |
| 893 | |
| 894 | if (src->type == LYXP_SET_SCNODE_SET) { |
| 895 | trg->type = LYXP_SET_SCNODE_SET; |
| 896 | trg->used = src->used; |
| 897 | trg->size = src->used; |
| 898 | |
| 899 | trg->val.scnodes = ly_realloc(trg->val.scnodes, trg->size * sizeof *trg->val.scnodes); |
| 900 | LY_CHECK_ERR_RET(!trg->val.scnodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), ); |
| 901 | memcpy(trg->val.scnodes, src->val.scnodes, src->used * sizeof *src->val.scnodes); |
| 902 | } else if (src->type == LYXP_SET_BOOLEAN) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 903 | set_fill_boolean(trg, src->val.bln); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 904 | } else if (src->type == LYXP_SET_NUMBER) { |
| 905 | set_fill_number(trg, src->val.num); |
| 906 | } else if (src->type == LYXP_SET_STRING) { |
| 907 | set_fill_string(trg, src->val.str, strlen(src->val.str)); |
| 908 | } else { |
| 909 | if (trg->type == LYXP_SET_NODE_SET) { |
| 910 | free(trg->val.nodes); |
| 911 | } else if (trg->type == LYXP_SET_STRING) { |
| 912 | free(trg->val.str); |
| 913 | } |
| 914 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 915 | assert(src->type == LYXP_SET_NODE_SET); |
| 916 | |
| 917 | trg->type = LYXP_SET_NODE_SET; |
| 918 | trg->used = src->used; |
| 919 | trg->size = src->used; |
| 920 | trg->ctx_pos = src->ctx_pos; |
| 921 | trg->ctx_size = src->ctx_size; |
| 922 | |
| 923 | trg->val.nodes = malloc(trg->used * sizeof *trg->val.nodes); |
| 924 | LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), ); |
| 925 | memcpy(trg->val.nodes, src->val.nodes, src->used * sizeof *src->val.nodes); |
| 926 | if (src->ht) { |
| 927 | trg->ht = lyht_dup(src->ht); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 928 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 929 | trg->ht = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 930 | } |
| 931 | } |
| 932 | } |
| 933 | |
| 934 | /** |
| 935 | * @brief Clear context of all schema nodes. |
| 936 | * |
| 937 | * @param[in] set Set to clear. |
| 938 | */ |
| 939 | static void |
| 940 | set_scnode_clear_ctx(struct lyxp_set *set) |
| 941 | { |
| 942 | uint32_t i; |
| 943 | |
| 944 | for (i = 0; i < set->used; ++i) { |
| 945 | if (set->val.scnodes[i].in_ctx == 1) { |
| 946 | set->val.scnodes[i].in_ctx = 0; |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 947 | } else if (set->val.scnodes[i].in_ctx == -2) { |
| 948 | set->val.scnodes[i].in_ctx = -1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 949 | } |
| 950 | } |
| 951 | } |
| 952 | |
| 953 | /** |
| 954 | * @brief Remove a node from a set. Removing last node changes |
| 955 | * set into LYXP_SET_EMPTY. Context position aware. |
| 956 | * |
| 957 | * @param[in] set Set to use. |
| 958 | * @param[in] idx Index from @p set of the node to be removed. |
| 959 | */ |
| 960 | static void |
| 961 | set_remove_node(struct lyxp_set *set, uint32_t idx) |
| 962 | { |
| 963 | assert(set && (set->type == LYXP_SET_NODE_SET)); |
| 964 | assert(idx < set->used); |
| 965 | |
| 966 | set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
| 967 | |
| 968 | --set->used; |
| 969 | if (set->used) { |
| 970 | memmove(&set->val.nodes[idx], &set->val.nodes[idx + 1], |
| 971 | (set->used - idx) * sizeof *set->val.nodes); |
| 972 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 973 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 974 | } |
| 975 | } |
| 976 | |
| 977 | /** |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 978 | * @brief Remove a node from a set by setting its type to LYXP_NODE_NONE. |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 979 | * |
| 980 | * @param[in] set Set to use. |
| 981 | * @param[in] idx Index from @p set of the node to be removed. |
| 982 | */ |
| 983 | static void |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 984 | set_remove_node_none(struct lyxp_set *set, uint32_t idx) |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 985 | { |
| 986 | assert(set && (set->type == LYXP_SET_NODE_SET)); |
| 987 | assert(idx < set->used); |
| 988 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 989 | if (set->val.nodes[idx].type == LYXP_NODE_ELEM) { |
| 990 | set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
| 991 | } |
| 992 | set->val.nodes[idx].type = LYXP_NODE_NONE; |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 993 | } |
| 994 | |
| 995 | /** |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 996 | * @brief Remove all LYXP_NODE_NONE nodes from a set. Removing last node changes |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 997 | * set into LYXP_SET_EMPTY. Context position aware. |
| 998 | * |
| 999 | * @param[in] set Set to consolidate. |
| 1000 | */ |
| 1001 | static void |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1002 | set_remove_nodes_none(struct lyxp_set *set) |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1003 | { |
| 1004 | uint16_t i, orig_used, end; |
| 1005 | int32_t start; |
| 1006 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1007 | assert(set); |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1008 | |
| 1009 | orig_used = set->used; |
| 1010 | set->used = 0; |
| 1011 | for (i = 0; i < orig_used;) { |
| 1012 | start = -1; |
| 1013 | do { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1014 | if ((set->val.nodes[i].type != LYXP_NODE_NONE) && (start == -1)) { |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1015 | start = i; |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1016 | } else if ((start > -1) && (set->val.nodes[i].type == LYXP_NODE_NONE)) { |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1017 | end = i; |
| 1018 | ++i; |
| 1019 | break; |
| 1020 | } |
| 1021 | |
| 1022 | ++i; |
| 1023 | if (i == orig_used) { |
| 1024 | end = i; |
| 1025 | } |
| 1026 | } while (i < orig_used); |
| 1027 | |
| 1028 | if (start > -1) { |
| 1029 | /* move the whole chunk of valid nodes together */ |
| 1030 | if (set->used != (unsigned)start) { |
| 1031 | memmove(&set->val.nodes[set->used], &set->val.nodes[start], (end - start) * sizeof *set->val.nodes); |
| 1032 | } |
| 1033 | set->used += end - start; |
| 1034 | } |
| 1035 | } |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1036 | } |
| 1037 | |
| 1038 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1039 | * @brief Check for duplicates in a node set. |
| 1040 | * |
| 1041 | * @param[in] set Set to check. |
| 1042 | * @param[in] node Node to look for in @p set. |
| 1043 | * @param[in] node_type Type of @p node. |
| 1044 | * @param[in] skip_idx Index from @p set to skip. |
| 1045 | * @return LY_ERR |
| 1046 | */ |
| 1047 | static LY_ERR |
| 1048 | set_dup_node_check(const struct lyxp_set *set, const struct lyd_node *node, enum lyxp_node_type node_type, int skip_idx) |
| 1049 | { |
| 1050 | uint32_t i; |
| 1051 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1052 | if (set->ht && node) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1053 | return set_dup_node_hash_check(set, (struct lyd_node *)node, node_type, skip_idx); |
| 1054 | } |
| 1055 | |
| 1056 | for (i = 0; i < set->used; ++i) { |
| 1057 | if ((skip_idx > -1) && (i == (unsigned)skip_idx)) { |
| 1058 | continue; |
| 1059 | } |
| 1060 | |
| 1061 | if ((set->val.nodes[i].node == node) && (set->val.nodes[i].type == node_type)) { |
| 1062 | return LY_EEXIST; |
| 1063 | } |
| 1064 | } |
| 1065 | |
| 1066 | return LY_SUCCESS; |
| 1067 | } |
| 1068 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 1069 | int |
| 1070 | lyxp_set_scnode_dup_node_check(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, int skip_idx) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1071 | { |
| 1072 | uint32_t i; |
| 1073 | |
| 1074 | for (i = 0; i < set->used; ++i) { |
| 1075 | if ((skip_idx > -1) && (i == (unsigned)skip_idx)) { |
| 1076 | continue; |
| 1077 | } |
| 1078 | |
| 1079 | if ((set->val.scnodes[i].scnode == node) && (set->val.scnodes[i].type == node_type)) { |
| 1080 | return i; |
| 1081 | } |
| 1082 | } |
| 1083 | |
| 1084 | return -1; |
| 1085 | } |
| 1086 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 1087 | void |
| 1088 | lyxp_set_scnode_merge(struct lyxp_set *set1, struct lyxp_set *set2) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1089 | { |
| 1090 | uint32_t orig_used, i, j; |
| 1091 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1092 | assert((set1->type == LYXP_SET_SCNODE_SET) && (set2->type == LYXP_SET_SCNODE_SET)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1093 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1094 | if (!set2->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1095 | return; |
| 1096 | } |
| 1097 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1098 | if (!set1->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1099 | memcpy(set1, set2, sizeof *set1); |
| 1100 | return; |
| 1101 | } |
| 1102 | |
| 1103 | if (set1->used + set2->used > set1->size) { |
| 1104 | set1->size = set1->used + set2->used; |
| 1105 | set1->val.scnodes = ly_realloc(set1->val.scnodes, set1->size * sizeof *set1->val.scnodes); |
| 1106 | LY_CHECK_ERR_RET(!set1->val.scnodes, LOGMEM(set1->ctx), ); |
| 1107 | } |
| 1108 | |
| 1109 | orig_used = set1->used; |
| 1110 | |
| 1111 | for (i = 0; i < set2->used; ++i) { |
| 1112 | for (j = 0; j < orig_used; ++j) { |
| 1113 | /* detect duplicities */ |
| 1114 | if (set1->val.scnodes[j].scnode == set2->val.scnodes[i].scnode) { |
| 1115 | break; |
| 1116 | } |
| 1117 | } |
| 1118 | |
| 1119 | if (j == orig_used) { |
| 1120 | memcpy(&set1->val.scnodes[set1->used], &set2->val.scnodes[i], sizeof *set2->val.scnodes); |
| 1121 | ++set1->used; |
| 1122 | } |
| 1123 | } |
| 1124 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1125 | lyxp_set_free_content(set2); |
| 1126 | set2->type = LYXP_SET_SCNODE_SET; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1127 | } |
| 1128 | |
| 1129 | /** |
| 1130 | * @brief Insert a node into a set. Context position aware. |
| 1131 | * |
| 1132 | * @param[in] set Set to use. |
| 1133 | * @param[in] node Node to insert to @p set. |
| 1134 | * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting. |
| 1135 | * @param[in] node_type Node type of @p node. |
| 1136 | * @param[in] idx Index in @p set to insert into. |
| 1137 | */ |
| 1138 | static void |
| 1139 | set_insert_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx) |
| 1140 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1141 | assert(set && (set->type == LYXP_SET_NODE_SET)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1142 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1143 | if (!set->size) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1144 | /* first item */ |
| 1145 | if (idx) { |
| 1146 | /* no real harm done, but it is a bug */ |
| 1147 | LOGINT(set->ctx); |
| 1148 | idx = 0; |
| 1149 | } |
| 1150 | set->val.nodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.nodes); |
| 1151 | LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), ); |
| 1152 | set->type = LYXP_SET_NODE_SET; |
| 1153 | set->used = 0; |
| 1154 | set->size = LYXP_SET_SIZE_START; |
| 1155 | set->ctx_pos = 1; |
| 1156 | set->ctx_size = 1; |
| 1157 | set->ht = NULL; |
| 1158 | } else { |
| 1159 | /* not an empty set */ |
| 1160 | if (set->used == set->size) { |
| 1161 | |
| 1162 | /* set is full */ |
| 1163 | set->val.nodes = ly_realloc(set->val.nodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.nodes); |
| 1164 | LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), ); |
| 1165 | set->size += LYXP_SET_SIZE_STEP; |
| 1166 | } |
| 1167 | |
| 1168 | if (idx > set->used) { |
| 1169 | LOGINT(set->ctx); |
| 1170 | idx = set->used; |
| 1171 | } |
| 1172 | |
| 1173 | /* make space for the new node */ |
| 1174 | if (idx < set->used) { |
| 1175 | memmove(&set->val.nodes[idx + 1], &set->val.nodes[idx], (set->used - idx) * sizeof *set->val.nodes); |
| 1176 | } |
| 1177 | } |
| 1178 | |
| 1179 | /* finally assign the value */ |
| 1180 | set->val.nodes[idx].node = (struct lyd_node *)node; |
| 1181 | set->val.nodes[idx].type = node_type; |
| 1182 | set->val.nodes[idx].pos = pos; |
| 1183 | ++set->used; |
| 1184 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1185 | if (set->val.nodes[idx].type == LYXP_NODE_ELEM) { |
| 1186 | set_insert_node_hash(set, (struct lyd_node *)node, node_type); |
| 1187 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1188 | } |
| 1189 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 1190 | int |
| 1191 | lyxp_set_scnode_insert_node(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1192 | { |
| 1193 | int ret; |
| 1194 | |
| 1195 | assert(set->type == LYXP_SET_SCNODE_SET); |
| 1196 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 1197 | ret = lyxp_set_scnode_dup_node_check(set, node, node_type, -1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1198 | if (ret > -1) { |
| 1199 | set->val.scnodes[ret].in_ctx = 1; |
| 1200 | } else { |
| 1201 | if (set->used == set->size) { |
| 1202 | set->val.scnodes = ly_realloc(set->val.scnodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.scnodes); |
| 1203 | LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), -1); |
| 1204 | set->size += LYXP_SET_SIZE_STEP; |
| 1205 | } |
| 1206 | |
| 1207 | ret = set->used; |
| 1208 | set->val.scnodes[ret].scnode = (struct lysc_node *)node; |
| 1209 | set->val.scnodes[ret].type = node_type; |
| 1210 | set->val.scnodes[ret].in_ctx = 1; |
| 1211 | ++set->used; |
| 1212 | } |
| 1213 | |
| 1214 | return ret; |
| 1215 | } |
| 1216 | |
| 1217 | /** |
| 1218 | * @brief Replace a node in a set with another. Context position aware. |
| 1219 | * |
| 1220 | * @param[in] set Set to use. |
| 1221 | * @param[in] node Node to insert to @p set. |
| 1222 | * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting. |
| 1223 | * @param[in] node_type Node type of @p node. |
| 1224 | * @param[in] idx Index in @p set of the node to replace. |
| 1225 | */ |
| 1226 | static void |
| 1227 | set_replace_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx) |
| 1228 | { |
| 1229 | assert(set && (idx < set->used)); |
| 1230 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1231 | if (set->val.nodes[idx].type == LYXP_NODE_ELEM) { |
| 1232 | set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
| 1233 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1234 | set->val.nodes[idx].node = (struct lyd_node *)node; |
| 1235 | set->val.nodes[idx].type = node_type; |
| 1236 | set->val.nodes[idx].pos = pos; |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1237 | if (set->val.nodes[idx].type == LYXP_NODE_ELEM) { |
| 1238 | set_insert_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
| 1239 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1240 | } |
| 1241 | |
| 1242 | /** |
| 1243 | * @brief Set all nodes with ctx 1 to a new unique context value. |
| 1244 | * |
| 1245 | * @param[in] set Set to modify. |
| 1246 | * @return New context value. |
| 1247 | */ |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 1248 | static int32_t |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1249 | set_scnode_new_in_ctx(struct lyxp_set *set) |
| 1250 | { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 1251 | uint32_t i; |
| 1252 | int32_t ret_ctx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1253 | |
| 1254 | assert(set->type == LYXP_SET_SCNODE_SET); |
| 1255 | |
| 1256 | ret_ctx = 3; |
| 1257 | retry: |
| 1258 | for (i = 0; i < set->used; ++i) { |
| 1259 | if (set->val.scnodes[i].in_ctx >= ret_ctx) { |
| 1260 | ret_ctx = set->val.scnodes[i].in_ctx + 1; |
| 1261 | goto retry; |
| 1262 | } |
| 1263 | } |
| 1264 | for (i = 0; i < set->used; ++i) { |
| 1265 | if (set->val.scnodes[i].in_ctx == 1) { |
| 1266 | set->val.scnodes[i].in_ctx = ret_ctx; |
| 1267 | } |
| 1268 | } |
| 1269 | |
| 1270 | return ret_ctx; |
| 1271 | } |
| 1272 | |
| 1273 | /** |
| 1274 | * @brief Get unique @p node position in the data. |
| 1275 | * |
| 1276 | * @param[in] node Node to find. |
| 1277 | * @param[in] node_type Node type of @p node. |
| 1278 | * @param[in] root Root node. |
| 1279 | * @param[in] root_type Type of the XPath @p root node. |
| 1280 | * @param[in] prev Node that we think is before @p node in DFS from @p root. Can optionally |
| 1281 | * be used to increase efficiency and start the DFS from this node. |
| 1282 | * @param[in] prev_pos Node @p prev position. Optional, but must be set if @p prev is set. |
| 1283 | * @return Node position. |
| 1284 | */ |
| 1285 | static uint32_t |
| 1286 | get_node_pos(const struct lyd_node *node, enum lyxp_node_type node_type, const struct lyd_node *root, |
| 1287 | enum lyxp_node_type root_type, const struct lyd_node **prev, uint32_t *prev_pos) |
| 1288 | { |
| 1289 | const struct lyd_node *next, *elem, *top_sibling; |
| 1290 | uint32_t pos = 1; |
| 1291 | |
| 1292 | assert(prev && prev_pos && !root->prev->next); |
| 1293 | |
| 1294 | if ((node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ROOT_CONFIG)) { |
| 1295 | return 0; |
| 1296 | } |
| 1297 | |
| 1298 | if (*prev) { |
| 1299 | /* start from the previous element instead from the root */ |
| 1300 | elem = next = *prev; |
| 1301 | pos = *prev_pos; |
| 1302 | for (top_sibling = elem; top_sibling->parent; top_sibling = (struct lyd_node *)top_sibling->parent); |
| 1303 | goto dfs_search; |
| 1304 | } |
| 1305 | |
| 1306 | for (top_sibling = root; top_sibling; top_sibling = top_sibling->next) { |
| 1307 | /* TREE DFS */ |
| 1308 | LYD_TREE_DFS_BEGIN(top_sibling, next, elem) { |
| 1309 | dfs_search: |
| 1310 | if ((root_type == LYXP_NODE_ROOT_CONFIG) && (elem->schema->flags & LYS_CONFIG_R)) { |
| 1311 | goto skip_children; |
| 1312 | } |
| 1313 | |
| 1314 | if (elem == node) { |
| 1315 | break; |
| 1316 | } |
| 1317 | ++pos; |
| 1318 | |
| 1319 | /* TREE DFS END */ |
| 1320 | /* select element for the next run - children first, |
| 1321 | * child exception for lyd_node_leaf and lyd_node_leaflist, but not the root */ |
| 1322 | if (elem->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) { |
| 1323 | next = NULL; |
| 1324 | } else { |
| 1325 | next = lyd_node_children(elem); |
| 1326 | } |
| 1327 | if (!next) { |
| 1328 | skip_children: |
| 1329 | /* no children */ |
| 1330 | if (elem == top_sibling) { |
| 1331 | /* we are done, root has no children */ |
| 1332 | elem = NULL; |
| 1333 | break; |
| 1334 | } |
| 1335 | /* try siblings */ |
| 1336 | next = elem->next; |
| 1337 | } |
| 1338 | while (!next) { |
| 1339 | /* no siblings, go back through parents */ |
| 1340 | if (elem->parent == top_sibling->parent) { |
| 1341 | /* we are done, no next element to process */ |
| 1342 | elem = NULL; |
| 1343 | break; |
| 1344 | } |
| 1345 | /* parent is already processed, go to its sibling */ |
| 1346 | elem = (struct lyd_node *)elem->parent; |
| 1347 | next = elem->next; |
| 1348 | } |
| 1349 | } |
| 1350 | |
| 1351 | /* node found */ |
| 1352 | if (elem) { |
| 1353 | break; |
| 1354 | } |
| 1355 | } |
| 1356 | |
| 1357 | if (!elem) { |
| 1358 | if (!(*prev)) { |
| 1359 | /* we went from root and failed to find it, cannot be */ |
| 1360 | LOGINT(node->schema->module->ctx); |
| 1361 | return 0; |
| 1362 | } else { |
| 1363 | *prev = NULL; |
| 1364 | *prev_pos = 0; |
| 1365 | |
| 1366 | elem = next = top_sibling = root; |
| 1367 | pos = 1; |
| 1368 | goto dfs_search; |
| 1369 | } |
| 1370 | } |
| 1371 | |
| 1372 | /* remember the last found node for next time */ |
| 1373 | *prev = node; |
| 1374 | *prev_pos = pos; |
| 1375 | |
| 1376 | return pos; |
| 1377 | } |
| 1378 | |
| 1379 | /** |
| 1380 | * @brief Assign (fill) missing node positions. |
| 1381 | * |
| 1382 | * @param[in] set Set to fill positions in. |
| 1383 | * @param[in] root Context root node. |
| 1384 | * @param[in] root_type Context root type. |
| 1385 | * @return LY_ERR |
| 1386 | */ |
| 1387 | static LY_ERR |
| 1388 | set_assign_pos(struct lyxp_set *set, const struct lyd_node *root, enum lyxp_node_type root_type) |
| 1389 | { |
| 1390 | const struct lyd_node *prev = NULL, *tmp_node; |
| 1391 | uint32_t i, tmp_pos = 0; |
| 1392 | |
| 1393 | for (i = 0; i < set->used; ++i) { |
| 1394 | if (!set->val.nodes[i].pos) { |
| 1395 | tmp_node = NULL; |
| 1396 | switch (set->val.nodes[i].type) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1397 | case LYXP_NODE_META: |
| 1398 | tmp_node = set->val.meta[i].meta->parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1399 | if (!tmp_node) { |
| 1400 | LOGINT_RET(root->schema->module->ctx); |
| 1401 | } |
| 1402 | /* fallthrough */ |
| 1403 | case LYXP_NODE_ELEM: |
| 1404 | case LYXP_NODE_TEXT: |
| 1405 | if (!tmp_node) { |
| 1406 | tmp_node = set->val.nodes[i].node; |
| 1407 | } |
| 1408 | set->val.nodes[i].pos = get_node_pos(tmp_node, set->val.nodes[i].type, root, root_type, &prev, &tmp_pos); |
| 1409 | break; |
| 1410 | default: |
| 1411 | /* all roots have position 0 */ |
| 1412 | break; |
| 1413 | } |
| 1414 | } |
| 1415 | } |
| 1416 | |
| 1417 | return LY_SUCCESS; |
| 1418 | } |
| 1419 | |
| 1420 | /** |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1421 | * @brief Get unique @p meta position in the parent metadata. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1422 | * |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1423 | * @param[in] meta Metadata to use. |
| 1424 | * @return Metadata position. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1425 | */ |
| 1426 | static uint16_t |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1427 | get_meta_pos(struct lyd_meta *meta) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1428 | { |
| 1429 | uint16_t pos = 0; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1430 | struct lyd_meta *meta2; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1431 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1432 | for (meta2 = meta->parent->meta; meta2 && (meta2 != meta); meta2 = meta2->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1433 | ++pos; |
| 1434 | } |
| 1435 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1436 | assert(meta2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1437 | return pos; |
| 1438 | } |
| 1439 | |
| 1440 | /** |
| 1441 | * @brief Compare 2 nodes in respect to XPath document order. |
| 1442 | * |
| 1443 | * @param[in] item1 1st node. |
| 1444 | * @param[in] item2 2nd node. |
| 1445 | * @return If 1st > 2nd returns 1, 1st == 2nd returns 0, and 1st < 2nd returns -1. |
| 1446 | */ |
| 1447 | static int |
| 1448 | set_sort_compare(struct lyxp_set_node *item1, struct lyxp_set_node *item2) |
| 1449 | { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1450 | uint32_t meta_pos1 = 0, meta_pos2 = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1451 | |
| 1452 | if (item1->pos < item2->pos) { |
| 1453 | return -1; |
| 1454 | } |
| 1455 | |
| 1456 | if (item1->pos > item2->pos) { |
| 1457 | return 1; |
| 1458 | } |
| 1459 | |
| 1460 | /* node positions are equal, the fun case */ |
| 1461 | |
| 1462 | /* 1st ELEM - == - 2nd TEXT, 1st TEXT - == - 2nd ELEM */ |
| 1463 | /* special case since text nodes are actually saved as their parents */ |
| 1464 | if ((item1->node == item2->node) && (item1->type != item2->type)) { |
| 1465 | if (item1->type == LYXP_NODE_ELEM) { |
| 1466 | assert(item2->type == LYXP_NODE_TEXT); |
| 1467 | return -1; |
| 1468 | } else { |
| 1469 | assert((item1->type == LYXP_NODE_TEXT) && (item2->type == LYXP_NODE_ELEM)); |
| 1470 | return 1; |
| 1471 | } |
| 1472 | } |
| 1473 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1474 | /* we need meta positions now */ |
| 1475 | if (item1->type == LYXP_NODE_META) { |
| 1476 | meta_pos1 = get_meta_pos((struct lyd_meta *)item1->node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1477 | } |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1478 | if (item2->type == LYXP_NODE_META) { |
| 1479 | meta_pos2 = get_meta_pos((struct lyd_meta *)item2->node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1480 | } |
| 1481 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1482 | /* 1st ROOT - 2nd ROOT, 1st ELEM - 2nd ELEM, 1st TEXT - 2nd TEXT, 1st META - =pos= - 2nd META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1483 | /* check for duplicates */ |
| 1484 | if (item1->node == item2->node) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1485 | assert((item1->type == item2->type) && ((item1->type != LYXP_NODE_META) || (meta_pos1 == meta_pos2))); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1486 | return 0; |
| 1487 | } |
| 1488 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1489 | /* 1st ELEM - 2nd TEXT, 1st ELEM - any pos - 2nd META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1490 | /* elem is always first, 2nd node is after it */ |
| 1491 | if (item1->type == LYXP_NODE_ELEM) { |
| 1492 | assert(item2->type != LYXP_NODE_ELEM); |
| 1493 | return -1; |
| 1494 | } |
| 1495 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1496 | /* 1st TEXT - 2nd ELEM, 1st TEXT - any pos - 2nd META, 1st META - any pos - 2nd ELEM, 1st META - >pos> - 2nd META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1497 | /* 2nd is before 1st */ |
| 1498 | if (((item1->type == LYXP_NODE_TEXT) |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1499 | && ((item2->type == LYXP_NODE_ELEM) || (item2->type == LYXP_NODE_META))) |
| 1500 | || ((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_ELEM)) |
| 1501 | || (((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_META)) |
| 1502 | && (meta_pos1 > meta_pos2))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1503 | return 1; |
| 1504 | } |
| 1505 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1506 | /* 1st META - any pos - 2nd TEXT, 1st META <pos< - 2nd META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1507 | /* 2nd is after 1st */ |
| 1508 | return -1; |
| 1509 | } |
| 1510 | |
| 1511 | /** |
| 1512 | * @brief Set cast for comparisons. |
| 1513 | * |
| 1514 | * @param[in] trg Target set to cast source into. |
| 1515 | * @param[in] src Source set. |
| 1516 | * @param[in] type Target set type. |
| 1517 | * @param[in] src_idx Source set node index. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1518 | * @return LY_ERR |
| 1519 | */ |
| 1520 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1521 | set_comp_cast(struct lyxp_set *trg, struct lyxp_set *src, enum lyxp_set_type type, uint32_t src_idx) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1522 | { |
| 1523 | assert(src->type == LYXP_SET_NODE_SET); |
| 1524 | |
| 1525 | set_init(trg, src); |
| 1526 | |
| 1527 | /* insert node into target set */ |
| 1528 | set_insert_node(trg, src->val.nodes[src_idx].node, src->val.nodes[src_idx].pos, src->val.nodes[src_idx].type, 0); |
| 1529 | |
| 1530 | /* cast target set appropriately */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1531 | return lyxp_set_cast(trg, type); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1532 | } |
| 1533 | |
| 1534 | #ifndef NDEBUG |
| 1535 | |
| 1536 | /** |
| 1537 | * @brief Bubble sort @p set into XPath document order. |
| 1538 | * Context position aware. Unused in the 'Release' build target. |
| 1539 | * |
| 1540 | * @param[in] set Set to sort. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1541 | * @return How many times the whole set was traversed - 1 (if set was sorted, returns 0). |
| 1542 | */ |
| 1543 | static int |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1544 | set_sort(struct lyxp_set *set) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1545 | { |
| 1546 | uint32_t i, j; |
| 1547 | int ret = 0, cmp, inverted, change; |
| 1548 | const struct lyd_node *root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1549 | struct lyxp_set_node item; |
| 1550 | struct lyxp_set_hash_node hnode; |
| 1551 | uint64_t hash; |
| 1552 | |
Michal Vasko | 3cf8fbf | 2020-05-27 15:21:21 +0200 | [diff] [blame] | 1553 | if ((set->type != LYXP_SET_NODE_SET) || (set->used < 2)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1554 | return 0; |
| 1555 | } |
| 1556 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1557 | /* find first top-level node to be used as anchor for positions */ |
| 1558 | for (root = set->ctx_node; root->parent; root = (const struct lyd_node *)root->parent); |
| 1559 | for (; root->prev->next; root = root->prev); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1560 | |
| 1561 | /* fill positions */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1562 | if (set_assign_pos(set, root, set->root_type)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1563 | return -1; |
| 1564 | } |
| 1565 | |
| 1566 | LOGDBG(LY_LDGXPATH, "SORT BEGIN"); |
| 1567 | print_set_debug(set); |
| 1568 | |
| 1569 | for (i = 0; i < set->used; ++i) { |
| 1570 | inverted = 0; |
| 1571 | change = 0; |
| 1572 | |
| 1573 | for (j = 1; j < set->used - i; ++j) { |
| 1574 | /* compare node positions */ |
| 1575 | if (inverted) { |
| 1576 | cmp = set_sort_compare(&set->val.nodes[j], &set->val.nodes[j - 1]); |
| 1577 | } else { |
| 1578 | cmp = set_sort_compare(&set->val.nodes[j - 1], &set->val.nodes[j]); |
| 1579 | } |
| 1580 | |
| 1581 | /* swap if needed */ |
| 1582 | if ((inverted && (cmp < 0)) || (!inverted && (cmp > 0))) { |
| 1583 | change = 1; |
| 1584 | |
| 1585 | item = set->val.nodes[j - 1]; |
| 1586 | set->val.nodes[j - 1] = set->val.nodes[j]; |
| 1587 | set->val.nodes[j] = item; |
| 1588 | } else { |
| 1589 | /* whether node_pos1 should be smaller than node_pos2 or the other way around */ |
| 1590 | inverted = !inverted; |
| 1591 | } |
| 1592 | } |
| 1593 | |
| 1594 | ++ret; |
| 1595 | |
| 1596 | if (!change) { |
| 1597 | break; |
| 1598 | } |
| 1599 | } |
| 1600 | |
| 1601 | LOGDBG(LY_LDGXPATH, "SORT END %d", ret); |
| 1602 | print_set_debug(set); |
| 1603 | |
| 1604 | /* check node hashes */ |
| 1605 | if (set->used >= LYD_HT_MIN_ITEMS) { |
| 1606 | assert(set->ht); |
| 1607 | for (i = 0; i < set->used; ++i) { |
| 1608 | hnode.node = set->val.nodes[i].node; |
| 1609 | hnode.type = set->val.nodes[i].type; |
| 1610 | |
| 1611 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 1612 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 1613 | hash = dict_hash_multi(hash, NULL, 0); |
| 1614 | |
| 1615 | assert(!lyht_find(set->ht, &hnode, hash, NULL)); |
| 1616 | } |
| 1617 | } |
| 1618 | |
| 1619 | return ret - 1; |
| 1620 | } |
| 1621 | |
| 1622 | /** |
| 1623 | * @brief Remove duplicate entries in a sorted node set. |
| 1624 | * |
| 1625 | * @param[in] set Sorted set to check. |
| 1626 | * @return LY_ERR (LY_EEXIST if some duplicates are found) |
| 1627 | */ |
| 1628 | static LY_ERR |
| 1629 | set_sorted_dup_node_clean(struct lyxp_set *set) |
| 1630 | { |
| 1631 | uint32_t i = 0; |
| 1632 | LY_ERR ret = LY_SUCCESS; |
| 1633 | |
| 1634 | if (set->used > 1) { |
| 1635 | while (i < set->used - 1) { |
| 1636 | if ((set->val.nodes[i].node == set->val.nodes[i + 1].node) |
| 1637 | && (set->val.nodes[i].type == set->val.nodes[i + 1].type)) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1638 | set_remove_node_none(set, i + 1); |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1639 | ret = LY_EEXIST; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1640 | } |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1641 | ++i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1642 | } |
| 1643 | } |
| 1644 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1645 | set_remove_nodes_none(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1646 | return ret; |
| 1647 | } |
| 1648 | |
| 1649 | #endif |
| 1650 | |
| 1651 | /** |
| 1652 | * @brief Merge 2 sorted sets into one. |
| 1653 | * |
| 1654 | * @param[in,out] trg Set to merge into. Duplicates are removed. |
| 1655 | * @param[in] src Set to be merged into @p trg. It is cast to #LYXP_SET_EMPTY on success. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1656 | * @return LY_ERR |
| 1657 | */ |
| 1658 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1659 | set_sorted_merge(struct lyxp_set *trg, struct lyxp_set *src) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1660 | { |
| 1661 | uint32_t i, j, k, count, dup_count; |
| 1662 | int cmp; |
| 1663 | const struct lyd_node *root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1664 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1665 | if ((trg->type != LYXP_SET_NODE_SET) || (src->type != LYXP_SET_NODE_SET)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1666 | return LY_EINVAL; |
| 1667 | } |
| 1668 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1669 | if (!src->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1670 | return LY_SUCCESS; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1671 | } else if (!trg->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1672 | set_fill_set(trg, src); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1673 | lyxp_set_free_content(src); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1674 | return LY_SUCCESS; |
| 1675 | } |
| 1676 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1677 | /* find first top-level node to be used as anchor for positions */ |
| 1678 | for (root = trg->ctx_node; root->parent; root = (const struct lyd_node *)root->parent); |
| 1679 | for (; root->prev->next; root = root->prev); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1680 | |
| 1681 | /* fill positions */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1682 | if (set_assign_pos(trg, root, trg->root_type) || set_assign_pos(src, root, src->root_type)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1683 | return LY_EINT; |
| 1684 | } |
| 1685 | |
| 1686 | #ifndef NDEBUG |
| 1687 | LOGDBG(LY_LDGXPATH, "MERGE target"); |
| 1688 | print_set_debug(trg); |
| 1689 | LOGDBG(LY_LDGXPATH, "MERGE source"); |
| 1690 | print_set_debug(src); |
| 1691 | #endif |
| 1692 | |
| 1693 | /* make memory for the merge (duplicates are not detected yet, so space |
| 1694 | * will likely be wasted on them, too bad) */ |
| 1695 | if (trg->size - trg->used < src->used) { |
| 1696 | trg->size = trg->used + src->used; |
| 1697 | |
| 1698 | trg->val.nodes = ly_realloc(trg->val.nodes, trg->size * sizeof *trg->val.nodes); |
| 1699 | LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx), LY_EMEM); |
| 1700 | } |
| 1701 | |
| 1702 | i = 0; |
| 1703 | j = 0; |
| 1704 | count = 0; |
| 1705 | dup_count = 0; |
| 1706 | do { |
| 1707 | cmp = set_sort_compare(&src->val.nodes[i], &trg->val.nodes[j]); |
| 1708 | if (!cmp) { |
| 1709 | if (!count) { |
| 1710 | /* duplicate, just skip it */ |
| 1711 | ++i; |
| 1712 | ++j; |
| 1713 | } else { |
| 1714 | /* we are copying something already, so let's copy the duplicate too, |
| 1715 | * we are hoping that afterwards there are some more nodes to |
| 1716 | * copy and this way we can copy them all together */ |
| 1717 | ++count; |
| 1718 | ++dup_count; |
| 1719 | ++i; |
| 1720 | ++j; |
| 1721 | } |
| 1722 | } else if (cmp < 0) { |
| 1723 | /* inserting src node into trg, just remember it for now */ |
| 1724 | ++count; |
| 1725 | ++i; |
| 1726 | |
| 1727 | /* insert the hash now */ |
| 1728 | set_insert_node_hash(trg, src->val.nodes[i - 1].node, src->val.nodes[i - 1].type); |
| 1729 | } else if (count) { |
| 1730 | copy_nodes: |
| 1731 | /* time to actually copy the nodes, we have found the largest block of nodes */ |
| 1732 | memmove(&trg->val.nodes[j + (count - dup_count)], |
| 1733 | &trg->val.nodes[j], |
| 1734 | (trg->used - j) * sizeof *trg->val.nodes); |
| 1735 | memcpy(&trg->val.nodes[j - dup_count], &src->val.nodes[i - count], count * sizeof *src->val.nodes); |
| 1736 | |
| 1737 | trg->used += count - dup_count; |
| 1738 | /* do not change i, except the copying above, we are basically doing exactly what is in the else branch below */ |
| 1739 | j += count - dup_count; |
| 1740 | |
| 1741 | count = 0; |
| 1742 | dup_count = 0; |
| 1743 | } else { |
| 1744 | ++j; |
| 1745 | } |
| 1746 | } while ((i < src->used) && (j < trg->used)); |
| 1747 | |
| 1748 | if ((i < src->used) || count) { |
| 1749 | /* insert all the hashes first */ |
| 1750 | for (k = i; k < src->used; ++k) { |
| 1751 | set_insert_node_hash(trg, src->val.nodes[k].node, src->val.nodes[k].type); |
| 1752 | } |
| 1753 | |
| 1754 | /* loop ended, but we need to copy something at trg end */ |
| 1755 | count += src->used - i; |
| 1756 | i = src->used; |
| 1757 | goto copy_nodes; |
| 1758 | } |
| 1759 | |
| 1760 | /* we are inserting hashes before the actual node insert, which causes |
| 1761 | * situations when there were initially not enough items for a hash table, |
| 1762 | * but even after some were inserted, hash table was not created (during |
| 1763 | * insertion the number of items is not updated yet) */ |
| 1764 | if (!trg->ht && (trg->used >= LYD_HT_MIN_ITEMS)) { |
| 1765 | set_insert_node_hash(trg, NULL, 0); |
| 1766 | } |
| 1767 | |
| 1768 | #ifndef NDEBUG |
| 1769 | LOGDBG(LY_LDGXPATH, "MERGE result"); |
| 1770 | print_set_debug(trg); |
| 1771 | #endif |
| 1772 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1773 | lyxp_set_free_content(src); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1774 | return LY_SUCCESS; |
| 1775 | } |
| 1776 | |
| 1777 | /* |
| 1778 | * (re)parse functions |
| 1779 | * |
| 1780 | * Parse functions parse the expression into |
| 1781 | * tokens (syntactic analysis). |
| 1782 | * |
| 1783 | * Reparse functions perform semantic analysis |
| 1784 | * (do not save the result, just a check) of |
| 1785 | * the expression and fill repeat indices. |
| 1786 | */ |
| 1787 | |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1788 | LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1789 | lyxp_check_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t tok_idx, enum lyxp_token want_tok) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1790 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1791 | if (exp->used == tok_idx) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1792 | if (ctx) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1793 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF); |
| 1794 | } |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1795 | return LY_EINCOMPLETE; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1796 | } |
| 1797 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1798 | if (want_tok && (exp->tokens[tok_idx] != want_tok)) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1799 | if (ctx) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1800 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1801 | lyxp_print_token(exp->tokens[tok_idx]), &exp->expr[exp->tok_pos[tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1802 | } |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1803 | return LY_ENOT; |
| 1804 | } |
| 1805 | |
| 1806 | return LY_SUCCESS; |
| 1807 | } |
| 1808 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1809 | LY_ERR |
| 1810 | lyxp_next_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok) |
| 1811 | { |
| 1812 | LY_CHECK_RET(lyxp_check_token(ctx, exp, *tok_idx, want_tok)); |
| 1813 | |
| 1814 | /* skip the token */ |
| 1815 | ++(*tok_idx); |
| 1816 | |
| 1817 | return LY_SUCCESS; |
| 1818 | } |
| 1819 | |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1820 | /* just like lyxp_check_token() but tests for 2 tokens */ |
| 1821 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1822 | exp_check_token2(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t tok_idx, enum lyxp_token want_tok1, |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1823 | enum lyxp_token want_tok2) |
| 1824 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1825 | if (exp->used == tok_idx) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1826 | if (ctx) { |
| 1827 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF); |
| 1828 | } |
| 1829 | return LY_EINCOMPLETE; |
| 1830 | } |
| 1831 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1832 | if ((exp->tokens[tok_idx] != want_tok1) && (exp->tokens[tok_idx] != want_tok2)) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1833 | if (ctx) { |
| 1834 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1835 | lyxp_print_token(exp->tokens[tok_idx]), &exp->expr[exp->tok_pos[tok_idx]]); |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1836 | } |
| 1837 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1838 | } |
| 1839 | |
| 1840 | return LY_SUCCESS; |
| 1841 | } |
| 1842 | |
| 1843 | /** |
| 1844 | * @brief Stack operation push on the repeat array. |
| 1845 | * |
| 1846 | * @param[in] exp Expression to use. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1847 | * @param[in] tok_idx Position in the expresion \p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1848 | * @param[in] repeat_op_idx Index from \p exp of the operator token. This value is pushed. |
| 1849 | */ |
| 1850 | static void |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1851 | exp_repeat_push(struct lyxp_expr *exp, uint16_t tok_idx, uint16_t repeat_op_idx) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1852 | { |
| 1853 | uint16_t i; |
| 1854 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1855 | if (exp->repeat[tok_idx]) { |
| 1856 | for (i = 0; exp->repeat[tok_idx][i]; ++i); |
| 1857 | exp->repeat[tok_idx] = realloc(exp->repeat[tok_idx], (i + 2) * sizeof *exp->repeat[tok_idx]); |
| 1858 | LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), ); |
| 1859 | exp->repeat[tok_idx][i] = repeat_op_idx; |
| 1860 | exp->repeat[tok_idx][i + 1] = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1861 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1862 | exp->repeat[tok_idx] = calloc(2, sizeof *exp->repeat[tok_idx]); |
| 1863 | LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), ); |
| 1864 | exp->repeat[tok_idx][0] = repeat_op_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1865 | } |
| 1866 | } |
| 1867 | |
| 1868 | /** |
| 1869 | * @brief Reparse Predicate. Logs directly on error. |
| 1870 | * |
| 1871 | * [7] Predicate ::= '[' Expr ']' |
| 1872 | * |
| 1873 | * @param[in] ctx Context for logging. |
| 1874 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1875 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1876 | * @return LY_ERR |
| 1877 | */ |
| 1878 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1879 | reparse_predicate(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1880 | { |
| 1881 | LY_ERR rc; |
| 1882 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1883 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1884 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1885 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1886 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1887 | rc = reparse_or_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1888 | LY_CHECK_RET(rc); |
| 1889 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1890 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1891 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1892 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1893 | |
| 1894 | return LY_SUCCESS; |
| 1895 | } |
| 1896 | |
| 1897 | /** |
| 1898 | * @brief Reparse RelativeLocationPath. Logs directly on error. |
| 1899 | * |
| 1900 | * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step |
| 1901 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 1902 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 1903 | * |
| 1904 | * @param[in] ctx Context for logging. |
| 1905 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1906 | * @param[in] tok_idx Position in the expression \p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1907 | * @return LY_ERR (LY_EINCOMPLETE on forward reference) |
| 1908 | */ |
| 1909 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1910 | reparse_relative_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1911 | { |
| 1912 | LY_ERR rc; |
| 1913 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1914 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1915 | LY_CHECK_RET(rc); |
| 1916 | |
| 1917 | goto step; |
| 1918 | do { |
| 1919 | /* '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1920 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1921 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1922 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1923 | LY_CHECK_RET(rc); |
| 1924 | step: |
| 1925 | /* Step */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1926 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1927 | case LYXP_TOKEN_DOT: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1928 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1929 | break; |
| 1930 | |
| 1931 | case LYXP_TOKEN_DDOT: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1932 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1933 | break; |
| 1934 | |
| 1935 | case LYXP_TOKEN_AT: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1936 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1937 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1938 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1939 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1940 | if ((exp->tokens[*tok_idx] != LYXP_TOKEN_NAMETEST) && (exp->tokens[*tok_idx] != LYXP_TOKEN_NODETYPE)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1941 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1942 | lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1943 | return LY_EVALID; |
| 1944 | } |
| 1945 | /* fall through */ |
| 1946 | case LYXP_TOKEN_NAMETEST: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1947 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1948 | goto reparse_predicate; |
| 1949 | break; |
| 1950 | |
| 1951 | case LYXP_TOKEN_NODETYPE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1952 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1953 | |
| 1954 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1955 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1956 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1957 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1958 | |
| 1959 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1960 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1961 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1962 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1963 | |
| 1964 | reparse_predicate: |
| 1965 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1966 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 1967 | rc = reparse_predicate(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1968 | LY_CHECK_RET(rc); |
| 1969 | } |
| 1970 | break; |
| 1971 | default: |
| 1972 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1973 | lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1974 | return LY_EVALID; |
| 1975 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1976 | } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1977 | |
| 1978 | return LY_SUCCESS; |
| 1979 | } |
| 1980 | |
| 1981 | /** |
| 1982 | * @brief Reparse AbsoluteLocationPath. Logs directly on error. |
| 1983 | * |
| 1984 | * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath |
| 1985 | * |
| 1986 | * @param[in] ctx Context for logging. |
| 1987 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1988 | * @param[in] tok_idx Position in the expression \p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1989 | * @return LY_ERR |
| 1990 | */ |
| 1991 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1992 | reparse_absolute_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1993 | { |
| 1994 | LY_ERR rc; |
| 1995 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1996 | LY_CHECK_RET(exp_check_token2(ctx, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1997 | |
| 1998 | /* '/' RelativeLocationPath? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1999 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2000 | /* '/' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2001 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2002 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2003 | if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2004 | return LY_SUCCESS; |
| 2005 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2006 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2007 | case LYXP_TOKEN_DOT: |
| 2008 | case LYXP_TOKEN_DDOT: |
| 2009 | case LYXP_TOKEN_AT: |
| 2010 | case LYXP_TOKEN_NAMETEST: |
| 2011 | case LYXP_TOKEN_NODETYPE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2012 | rc = reparse_relative_location_path(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2013 | LY_CHECK_RET(rc); |
| 2014 | /* fall through */ |
| 2015 | default: |
| 2016 | break; |
| 2017 | } |
| 2018 | |
| 2019 | /* '//' RelativeLocationPath */ |
| 2020 | } else { |
| 2021 | /* '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2022 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2023 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2024 | rc = reparse_relative_location_path(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2025 | LY_CHECK_RET(rc); |
| 2026 | } |
| 2027 | |
| 2028 | return LY_SUCCESS; |
| 2029 | } |
| 2030 | |
| 2031 | /** |
| 2032 | * @brief Reparse FunctionCall. Logs directly on error. |
| 2033 | * |
| 2034 | * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')' |
| 2035 | * |
| 2036 | * @param[in] ctx Context for logging. |
| 2037 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2038 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2039 | * @return LY_ERR |
| 2040 | */ |
| 2041 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2042 | reparse_function_call(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2043 | { |
| 2044 | int min_arg_count = -1, max_arg_count, arg_count; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2045 | uint16_t func_tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2046 | LY_ERR rc; |
| 2047 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2048 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2049 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2050 | func_tok_idx = *tok_idx; |
| 2051 | switch (exp->tok_len[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2052 | case 3: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2053 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2054 | min_arg_count = 1; |
| 2055 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2056 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2057 | min_arg_count = 1; |
| 2058 | max_arg_count = 1; |
| 2059 | } |
| 2060 | break; |
| 2061 | case 4: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2062 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2063 | min_arg_count = 1; |
| 2064 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2065 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2066 | min_arg_count = 0; |
| 2067 | max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2068 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2069 | min_arg_count = 0; |
| 2070 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2071 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2072 | min_arg_count = 0; |
| 2073 | max_arg_count = 0; |
| 2074 | } |
| 2075 | break; |
| 2076 | case 5: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2077 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2078 | min_arg_count = 1; |
| 2079 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2080 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2081 | min_arg_count = 0; |
| 2082 | max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2083 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2084 | min_arg_count = 1; |
| 2085 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2086 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2087 | min_arg_count = 1; |
| 2088 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2089 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2090 | min_arg_count = 1; |
| 2091 | max_arg_count = 1; |
| 2092 | } |
| 2093 | break; |
| 2094 | case 6: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2095 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2096 | min_arg_count = 2; |
Michal Vasko | be2e356 | 2019-10-15 15:35:35 +0200 | [diff] [blame] | 2097 | max_arg_count = INT_MAX; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2098 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2099 | min_arg_count = 0; |
| 2100 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2101 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2102 | min_arg_count = 0; |
| 2103 | max_arg_count = 1; |
| 2104 | } |
| 2105 | break; |
| 2106 | case 7: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2107 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2108 | min_arg_count = 1; |
| 2109 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2110 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2111 | min_arg_count = 1; |
| 2112 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2113 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2114 | min_arg_count = 0; |
| 2115 | max_arg_count = 0; |
| 2116 | } |
| 2117 | break; |
| 2118 | case 8: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2119 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2120 | min_arg_count = 2; |
| 2121 | max_arg_count = 2; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2122 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2123 | min_arg_count = 0; |
| 2124 | max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2125 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2126 | min_arg_count = 2; |
| 2127 | max_arg_count = 2; |
| 2128 | } |
| 2129 | break; |
| 2130 | case 9: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2131 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2132 | min_arg_count = 2; |
| 2133 | max_arg_count = 3; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2134 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2135 | min_arg_count = 3; |
| 2136 | max_arg_count = 3; |
| 2137 | } |
| 2138 | break; |
| 2139 | case 10: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2140 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2141 | min_arg_count = 0; |
| 2142 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2143 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2144 | min_arg_count = 1; |
| 2145 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2146 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2147 | min_arg_count = 2; |
| 2148 | max_arg_count = 2; |
| 2149 | } |
| 2150 | break; |
| 2151 | case 11: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2152 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2153 | min_arg_count = 2; |
| 2154 | max_arg_count = 2; |
| 2155 | } |
| 2156 | break; |
| 2157 | case 12: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2158 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2159 | min_arg_count = 2; |
| 2160 | max_arg_count = 2; |
| 2161 | } |
| 2162 | break; |
| 2163 | case 13: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2164 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2165 | min_arg_count = 0; |
| 2166 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2167 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2168 | min_arg_count = 0; |
| 2169 | max_arg_count = 1; |
| 2170 | } |
| 2171 | break; |
| 2172 | case 15: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2173 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2174 | min_arg_count = 0; |
| 2175 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2176 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2177 | min_arg_count = 2; |
| 2178 | max_arg_count = 2; |
| 2179 | } |
| 2180 | break; |
| 2181 | case 16: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2182 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2183 | min_arg_count = 2; |
| 2184 | max_arg_count = 2; |
| 2185 | } |
| 2186 | break; |
| 2187 | case 20: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2188 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2189 | min_arg_count = 2; |
| 2190 | max_arg_count = 2; |
| 2191 | } |
| 2192 | break; |
| 2193 | } |
| 2194 | if (min_arg_count == -1) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2195 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INFUNC, exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2196 | return LY_EINVAL; |
| 2197 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2198 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2199 | |
| 2200 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2201 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2202 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2203 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2204 | |
| 2205 | /* ( Expr ( ',' Expr )* )? */ |
| 2206 | arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2207 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2208 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2209 | if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2210 | ++arg_count; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2211 | rc = reparse_or_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2212 | LY_CHECK_RET(rc); |
| 2213 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2214 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) { |
| 2215 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2216 | |
| 2217 | ++arg_count; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2218 | rc = reparse_or_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2219 | LY_CHECK_RET(rc); |
| 2220 | } |
| 2221 | |
| 2222 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2223 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2224 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2225 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2226 | |
| 2227 | if ((arg_count < min_arg_count) || (arg_count > max_arg_count)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2228 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INARGCOUNT, arg_count, exp->tok_len[func_tok_idx], |
| 2229 | &exp->expr[exp->tok_pos[func_tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2230 | return LY_EVALID; |
| 2231 | } |
| 2232 | |
| 2233 | return LY_SUCCESS; |
| 2234 | } |
| 2235 | |
| 2236 | /** |
| 2237 | * @brief Reparse PathExpr. Logs directly on error. |
| 2238 | * |
| 2239 | * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate* |
| 2240 | * | PrimaryExpr Predicate* '/' RelativeLocationPath |
| 2241 | * | PrimaryExpr Predicate* '//' RelativeLocationPath |
| 2242 | * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath |
| 2243 | * [8] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall |
| 2244 | * |
| 2245 | * @param[in] ctx Context for logging. |
| 2246 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2247 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2248 | * @return LY_ERR |
| 2249 | */ |
| 2250 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2251 | reparse_path_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2252 | { |
| 2253 | LY_ERR rc; |
| 2254 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2255 | if (lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 2256 | return LY_EVALID; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2257 | } |
| 2258 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2259 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2260 | case LYXP_TOKEN_PAR1: |
| 2261 | /* '(' Expr ')' Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2262 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2263 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2264 | rc = reparse_or_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2265 | LY_CHECK_RET(rc); |
| 2266 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2267 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2268 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2269 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2270 | goto predicate; |
| 2271 | break; |
| 2272 | case LYXP_TOKEN_DOT: |
| 2273 | case LYXP_TOKEN_DDOT: |
| 2274 | case LYXP_TOKEN_AT: |
| 2275 | case LYXP_TOKEN_NAMETEST: |
| 2276 | case LYXP_TOKEN_NODETYPE: |
| 2277 | /* RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2278 | rc = reparse_relative_location_path(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2279 | LY_CHECK_RET(rc); |
| 2280 | break; |
| 2281 | case LYXP_TOKEN_FUNCNAME: |
| 2282 | /* FunctionCall */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2283 | rc = reparse_function_call(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2284 | LY_CHECK_RET(rc); |
| 2285 | goto predicate; |
| 2286 | break; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2287 | case LYXP_TOKEN_OPER_PATH: |
| 2288 | case LYXP_TOKEN_OPER_RPATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2289 | /* AbsoluteLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2290 | rc = reparse_absolute_location_path(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2291 | LY_CHECK_RET(rc); |
| 2292 | break; |
| 2293 | case LYXP_TOKEN_LITERAL: |
| 2294 | /* Literal */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2295 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2296 | goto predicate; |
| 2297 | break; |
| 2298 | case LYXP_TOKEN_NUMBER: |
| 2299 | /* Number */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2300 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2301 | goto predicate; |
| 2302 | break; |
| 2303 | default: |
| 2304 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2305 | lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2306 | return LY_EVALID; |
| 2307 | } |
| 2308 | |
| 2309 | return LY_SUCCESS; |
| 2310 | |
| 2311 | predicate: |
| 2312 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2313 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 2314 | rc = reparse_predicate(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2315 | LY_CHECK_RET(rc); |
| 2316 | } |
| 2317 | |
| 2318 | /* ('/' or '//') RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2319 | if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2320 | |
| 2321 | /* '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2322 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2323 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2324 | rc = reparse_relative_location_path(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2325 | LY_CHECK_RET(rc); |
| 2326 | } |
| 2327 | |
| 2328 | return LY_SUCCESS; |
| 2329 | } |
| 2330 | |
| 2331 | /** |
| 2332 | * @brief Reparse UnaryExpr. Logs directly on error. |
| 2333 | * |
| 2334 | * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr |
| 2335 | * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr |
| 2336 | * |
| 2337 | * @param[in] ctx Context for logging. |
| 2338 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2339 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2340 | * @return LY_ERR |
| 2341 | */ |
| 2342 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2343 | reparse_unary_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2344 | { |
| 2345 | uint16_t prev_exp; |
| 2346 | LY_ERR rc; |
| 2347 | |
| 2348 | /* ('-')* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2349 | prev_exp = *tok_idx; |
| 2350 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) |
| 2351 | && (exp->expr[exp->tok_pos[*tok_idx]] == '-')) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2352 | exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2353 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2354 | } |
| 2355 | |
| 2356 | /* PathExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2357 | prev_exp = *tok_idx; |
| 2358 | rc = reparse_path_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2359 | LY_CHECK_RET(rc); |
| 2360 | |
| 2361 | /* ('|' PathExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2362 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_UNI)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2363 | exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2364 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2365 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2366 | rc = reparse_path_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2367 | LY_CHECK_RET(rc); |
| 2368 | } |
| 2369 | |
| 2370 | return LY_SUCCESS; |
| 2371 | } |
| 2372 | |
| 2373 | /** |
| 2374 | * @brief Reparse AdditiveExpr. Logs directly on error. |
| 2375 | * |
| 2376 | * [15] AdditiveExpr ::= MultiplicativeExpr |
| 2377 | * | AdditiveExpr '+' MultiplicativeExpr |
| 2378 | * | AdditiveExpr '-' MultiplicativeExpr |
| 2379 | * [16] MultiplicativeExpr ::= UnaryExpr |
| 2380 | * | MultiplicativeExpr '*' UnaryExpr |
| 2381 | * | MultiplicativeExpr 'div' UnaryExpr |
| 2382 | * | MultiplicativeExpr 'mod' UnaryExpr |
| 2383 | * |
| 2384 | * @param[in] ctx Context for logging. |
| 2385 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2386 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2387 | * @return LY_ERR |
| 2388 | */ |
| 2389 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2390 | reparse_additive_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2391 | { |
| 2392 | uint16_t prev_add_exp, prev_mul_exp; |
| 2393 | LY_ERR rc; |
| 2394 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2395 | prev_add_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2396 | goto reparse_multiplicative_expr; |
| 2397 | |
| 2398 | /* ('+' / '-' MultiplicativeExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2399 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) |
| 2400 | && ((exp->expr[exp->tok_pos[*tok_idx]] == '+') || (exp->expr[exp->tok_pos[*tok_idx]] == '-'))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2401 | exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2402 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2403 | |
| 2404 | reparse_multiplicative_expr: |
| 2405 | /* UnaryExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2406 | prev_mul_exp = *tok_idx; |
| 2407 | rc = reparse_unary_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2408 | LY_CHECK_RET(rc); |
| 2409 | |
| 2410 | /* ('*' / 'div' / 'mod' UnaryExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2411 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) |
| 2412 | && ((exp->expr[exp->tok_pos[*tok_idx]] == '*') || (exp->tok_len[*tok_idx] == 3))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2413 | exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2414 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2415 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2416 | rc = reparse_unary_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2417 | LY_CHECK_RET(rc); |
| 2418 | } |
| 2419 | } |
| 2420 | |
| 2421 | return LY_SUCCESS; |
| 2422 | } |
| 2423 | |
| 2424 | /** |
| 2425 | * @brief Reparse EqualityExpr. Logs directly on error. |
| 2426 | * |
| 2427 | * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr |
| 2428 | * | EqualityExpr '!=' RelationalExpr |
| 2429 | * [14] RelationalExpr ::= AdditiveExpr |
| 2430 | * | RelationalExpr '<' AdditiveExpr |
| 2431 | * | RelationalExpr '>' AdditiveExpr |
| 2432 | * | RelationalExpr '<=' AdditiveExpr |
| 2433 | * | RelationalExpr '>=' AdditiveExpr |
| 2434 | * |
| 2435 | * @param[in] ctx Context for logging. |
| 2436 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2437 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2438 | * @return LY_ERR |
| 2439 | */ |
| 2440 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2441 | reparse_equality_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2442 | { |
| 2443 | uint16_t prev_eq_exp, prev_rel_exp; |
| 2444 | LY_ERR rc; |
| 2445 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2446 | prev_eq_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2447 | goto reparse_additive_expr; |
| 2448 | |
| 2449 | /* ('=' / '!=' RelationalExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2450 | while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_EQUAL, LYXP_TOKEN_OPER_NEQUAL)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2451 | exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2452 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2453 | |
| 2454 | reparse_additive_expr: |
| 2455 | /* AdditiveExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2456 | prev_rel_exp = *tok_idx; |
| 2457 | rc = reparse_additive_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2458 | LY_CHECK_RET(rc); |
| 2459 | |
| 2460 | /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2461 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_COMP)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2462 | exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2463 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2464 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2465 | rc = reparse_additive_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2466 | LY_CHECK_RET(rc); |
| 2467 | } |
| 2468 | } |
| 2469 | |
| 2470 | return LY_SUCCESS; |
| 2471 | } |
| 2472 | |
| 2473 | /** |
| 2474 | * @brief Reparse OrExpr. Logs directly on error. |
| 2475 | * |
| 2476 | * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr |
| 2477 | * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr |
| 2478 | * |
| 2479 | * @param[in] ctx Context for logging. |
| 2480 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2481 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2482 | * @return LY_ERR |
| 2483 | */ |
| 2484 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2485 | reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2486 | { |
| 2487 | uint16_t prev_or_exp, prev_and_exp; |
| 2488 | LY_ERR rc; |
| 2489 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2490 | prev_or_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2491 | goto reparse_equality_expr; |
| 2492 | |
| 2493 | /* ('or' AndExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2494 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_LOG) && (exp->tok_len[*tok_idx] == 2)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2495 | exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2496 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2497 | |
| 2498 | reparse_equality_expr: |
| 2499 | /* EqualityExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2500 | prev_and_exp = *tok_idx; |
| 2501 | rc = reparse_equality_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2502 | LY_CHECK_RET(rc); |
| 2503 | |
| 2504 | /* ('and' EqualityExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2505 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_LOG) && (exp->tok_len[*tok_idx] == 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2506 | exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2507 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2508 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2509 | rc = reparse_equality_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2510 | LY_CHECK_RET(rc); |
| 2511 | } |
| 2512 | } |
| 2513 | |
| 2514 | return LY_SUCCESS; |
| 2515 | } |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2516 | |
| 2517 | /** |
| 2518 | * @brief Parse NCName. |
| 2519 | * |
| 2520 | * @param[in] ncname Name to parse. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2521 | * @return Length of @p ncname valid bytes. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2522 | */ |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2523 | static long int |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2524 | parse_ncname(const char *ncname) |
| 2525 | { |
| 2526 | unsigned int uc; |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2527 | size_t size; |
| 2528 | long int len = 0; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2529 | |
| 2530 | LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0); |
| 2531 | if (!is_xmlqnamestartchar(uc) || (uc == ':')) { |
| 2532 | return len; |
| 2533 | } |
| 2534 | |
| 2535 | do { |
| 2536 | len += size; |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 2537 | if (!*ncname) { |
| 2538 | break; |
| 2539 | } |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2540 | LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2541 | } while (is_xmlqnamechar(uc) && (uc != ':')); |
| 2542 | |
| 2543 | return len; |
| 2544 | } |
| 2545 | |
| 2546 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2547 | * @brief Add @p token into the expression @p exp. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2548 | * |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2549 | * @param[in] ctx Context for logging. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2550 | * @param[in] exp Expression to use. |
| 2551 | * @param[in] token Token to add. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2552 | * @param[in] tok_pos Token position in the XPath expression. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2553 | * @param[in] tok_len Token length in the XPath expression. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2554 | * @return LY_ERR |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2555 | */ |
| 2556 | static LY_ERR |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 2557 | exp_add_token(const struct ly_ctx *ctx, struct lyxp_expr *exp, enum lyxp_token token, uint16_t tok_pos, uint16_t tok_len) |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2558 | { |
| 2559 | uint32_t prev; |
| 2560 | |
| 2561 | if (exp->used == exp->size) { |
| 2562 | prev = exp->size; |
| 2563 | exp->size += LYXP_EXPR_SIZE_STEP; |
| 2564 | if (prev > exp->size) { |
| 2565 | LOGINT(ctx); |
| 2566 | return LY_EINT; |
| 2567 | } |
| 2568 | |
| 2569 | exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens); |
| 2570 | LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM); |
| 2571 | exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos); |
| 2572 | LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM); |
| 2573 | exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len); |
| 2574 | LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM); |
| 2575 | } |
| 2576 | |
| 2577 | exp->tokens[exp->used] = token; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2578 | exp->tok_pos[exp->used] = tok_pos; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2579 | exp->tok_len[exp->used] = tok_len; |
| 2580 | ++exp->used; |
| 2581 | return LY_SUCCESS; |
| 2582 | } |
| 2583 | |
| 2584 | void |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 2585 | lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr) |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2586 | { |
| 2587 | uint16_t i; |
| 2588 | |
| 2589 | if (!expr) { |
| 2590 | return; |
| 2591 | } |
| 2592 | |
| 2593 | lydict_remove(ctx, expr->expr); |
| 2594 | free(expr->tokens); |
| 2595 | free(expr->tok_pos); |
| 2596 | free(expr->tok_len); |
| 2597 | if (expr->repeat) { |
| 2598 | for (i = 0; i < expr->used; ++i) { |
| 2599 | free(expr->repeat[i]); |
| 2600 | } |
| 2601 | } |
| 2602 | free(expr->repeat); |
| 2603 | free(expr); |
| 2604 | } |
| 2605 | |
| 2606 | struct lyxp_expr * |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2607 | lyxp_expr_parse(const struct ly_ctx *ctx, const char *expr, size_t expr_len, int reparse) |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2608 | { |
| 2609 | struct lyxp_expr *ret; |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2610 | size_t parsed = 0, tok_len; |
| 2611 | long int ncname_len; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2612 | enum lyxp_token tok_type; |
| 2613 | int prev_function_check = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2614 | uint16_t tok_idx = 0; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2615 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2616 | if (!expr[0]) { |
| 2617 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF); |
| 2618 | return NULL; |
| 2619 | } |
| 2620 | |
| 2621 | if (!expr_len) { |
| 2622 | expr_len = strlen(expr); |
| 2623 | } |
| 2624 | if (expr_len > UINT16_MAX) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2625 | LOGERR(ctx, LY_EINVAL, "XPath expression cannot be longer than %ud characters.", UINT16_MAX); |
| 2626 | return NULL; |
| 2627 | } |
| 2628 | |
| 2629 | /* init lyxp_expr structure */ |
| 2630 | ret = calloc(1, sizeof *ret); |
| 2631 | LY_CHECK_ERR_GOTO(!ret, LOGMEM(ctx), error); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2632 | ret->expr = lydict_insert(ctx, expr, expr_len); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2633 | LY_CHECK_ERR_GOTO(!ret->expr, LOGMEM(ctx), error); |
| 2634 | ret->used = 0; |
| 2635 | ret->size = LYXP_EXPR_SIZE_START; |
| 2636 | ret->tokens = malloc(ret->size * sizeof *ret->tokens); |
| 2637 | LY_CHECK_ERR_GOTO(!ret->tokens, LOGMEM(ctx), error); |
| 2638 | |
| 2639 | ret->tok_pos = malloc(ret->size * sizeof *ret->tok_pos); |
| 2640 | LY_CHECK_ERR_GOTO(!ret->tok_pos, LOGMEM(ctx), error); |
| 2641 | |
| 2642 | ret->tok_len = malloc(ret->size * sizeof *ret->tok_len); |
| 2643 | LY_CHECK_ERR_GOTO(!ret->tok_len, LOGMEM(ctx), error); |
| 2644 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2645 | /* make expr 0-terminated */ |
| 2646 | expr = ret->expr; |
| 2647 | |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2648 | while (is_xmlws(expr[parsed])) { |
| 2649 | ++parsed; |
| 2650 | } |
| 2651 | |
| 2652 | do { |
| 2653 | if (expr[parsed] == '(') { |
| 2654 | |
| 2655 | /* '(' */ |
| 2656 | tok_len = 1; |
| 2657 | tok_type = LYXP_TOKEN_PAR1; |
| 2658 | |
| 2659 | if (prev_function_check && ret->used && (ret->tokens[ret->used - 1] == LYXP_TOKEN_NAMETEST)) { |
| 2660 | /* it is a NodeType/FunctionName after all */ |
| 2661 | if (((ret->tok_len[ret->used - 1] == 4) |
| 2662 | && (!strncmp(&expr[ret->tok_pos[ret->used - 1]], "node", 4) |
| 2663 | || !strncmp(&expr[ret->tok_pos[ret->used - 1]], "text", 4))) || |
| 2664 | ((ret->tok_len[ret->used - 1] == 7) |
| 2665 | && !strncmp(&expr[ret->tok_pos[ret->used - 1]], "comment", 7))) { |
| 2666 | ret->tokens[ret->used - 1] = LYXP_TOKEN_NODETYPE; |
| 2667 | } else { |
| 2668 | ret->tokens[ret->used - 1] = LYXP_TOKEN_FUNCNAME; |
| 2669 | } |
| 2670 | prev_function_check = 0; |
| 2671 | } |
| 2672 | |
| 2673 | } else if (expr[parsed] == ')') { |
| 2674 | |
| 2675 | /* ')' */ |
| 2676 | tok_len = 1; |
| 2677 | tok_type = LYXP_TOKEN_PAR2; |
| 2678 | |
| 2679 | } else if (expr[parsed] == '[') { |
| 2680 | |
| 2681 | /* '[' */ |
| 2682 | tok_len = 1; |
| 2683 | tok_type = LYXP_TOKEN_BRACK1; |
| 2684 | |
| 2685 | } else if (expr[parsed] == ']') { |
| 2686 | |
| 2687 | /* ']' */ |
| 2688 | tok_len = 1; |
| 2689 | tok_type = LYXP_TOKEN_BRACK2; |
| 2690 | |
| 2691 | } else if (!strncmp(&expr[parsed], "..", 2)) { |
| 2692 | |
| 2693 | /* '..' */ |
| 2694 | tok_len = 2; |
| 2695 | tok_type = LYXP_TOKEN_DDOT; |
| 2696 | |
| 2697 | } else if ((expr[parsed] == '.') && (!isdigit(expr[parsed + 1]))) { |
| 2698 | |
| 2699 | /* '.' */ |
| 2700 | tok_len = 1; |
| 2701 | tok_type = LYXP_TOKEN_DOT; |
| 2702 | |
| 2703 | } else if (expr[parsed] == '@') { |
| 2704 | |
| 2705 | /* '@' */ |
| 2706 | tok_len = 1; |
| 2707 | tok_type = LYXP_TOKEN_AT; |
| 2708 | |
| 2709 | } else if (expr[parsed] == ',') { |
| 2710 | |
| 2711 | /* ',' */ |
| 2712 | tok_len = 1; |
| 2713 | tok_type = LYXP_TOKEN_COMMA; |
| 2714 | |
| 2715 | } else if (expr[parsed] == '\'') { |
| 2716 | |
| 2717 | /* Literal with ' */ |
| 2718 | for (tok_len = 1; (expr[parsed + tok_len] != '\0') && (expr[parsed + tok_len] != '\''); ++tok_len); |
| 2719 | LY_CHECK_ERR_GOTO(expr[parsed + tok_len] == '\0', |
| 2720 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOE, expr[parsed], &expr[parsed]), error); |
| 2721 | ++tok_len; |
| 2722 | tok_type = LYXP_TOKEN_LITERAL; |
| 2723 | |
| 2724 | } else if (expr[parsed] == '\"') { |
| 2725 | |
| 2726 | /* Literal with " */ |
| 2727 | for (tok_len = 1; (expr[parsed + tok_len] != '\0') && (expr[parsed + tok_len] != '\"'); ++tok_len); |
| 2728 | LY_CHECK_ERR_GOTO(expr[parsed + tok_len] == '\0', |
| 2729 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOE, expr[parsed], &expr[parsed]), error); |
| 2730 | ++tok_len; |
| 2731 | tok_type = LYXP_TOKEN_LITERAL; |
| 2732 | |
| 2733 | } else if ((expr[parsed] == '.') || (isdigit(expr[parsed]))) { |
| 2734 | |
| 2735 | /* Number */ |
| 2736 | for (tok_len = 0; isdigit(expr[parsed + tok_len]); ++tok_len); |
| 2737 | if (expr[parsed + tok_len] == '.') { |
| 2738 | ++tok_len; |
| 2739 | for (; isdigit(expr[parsed + tok_len]); ++tok_len); |
| 2740 | } |
| 2741 | tok_type = LYXP_TOKEN_NUMBER; |
| 2742 | |
| 2743 | } else if (expr[parsed] == '/') { |
| 2744 | |
| 2745 | /* Operator '/', '//' */ |
| 2746 | if (!strncmp(&expr[parsed], "//", 2)) { |
| 2747 | tok_len = 2; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2748 | tok_type = LYXP_TOKEN_OPER_RPATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2749 | } else { |
| 2750 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2751 | tok_type = LYXP_TOKEN_OPER_PATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2752 | } |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2753 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2754 | } else if (!strncmp(&expr[parsed], "!=", 2)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2755 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2756 | /* Operator '!=' */ |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2757 | tok_len = 2; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2758 | tok_type = LYXP_TOKEN_OPER_NEQUAL; |
| 2759 | |
| 2760 | } else if (!strncmp(&expr[parsed], "<=", 2) || !strncmp(&expr[parsed], ">=", 2)) { |
| 2761 | |
| 2762 | /* Operator '<=', '>=' */ |
| 2763 | tok_len = 2; |
| 2764 | tok_type = LYXP_TOKEN_OPER_COMP; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2765 | |
| 2766 | } else if (expr[parsed] == '|') { |
| 2767 | |
| 2768 | /* Operator '|' */ |
| 2769 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2770 | tok_type = LYXP_TOKEN_OPER_UNI; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2771 | |
| 2772 | } else if ((expr[parsed] == '+') || (expr[parsed] == '-')) { |
| 2773 | |
| 2774 | /* Operator '+', '-' */ |
| 2775 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2776 | tok_type = LYXP_TOKEN_OPER_MATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2777 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2778 | } else if (expr[parsed] == '=') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2779 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2780 | /* Operator '=' */ |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2781 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2782 | tok_type = LYXP_TOKEN_OPER_EQUAL; |
| 2783 | |
| 2784 | } else if ((expr[parsed] == '<') || (expr[parsed] == '>')) { |
| 2785 | |
| 2786 | /* Operator '<', '>' */ |
| 2787 | tok_len = 1; |
| 2788 | tok_type = LYXP_TOKEN_OPER_COMP; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2789 | |
| 2790 | } else if (ret->used && (ret->tokens[ret->used - 1] != LYXP_TOKEN_AT) |
| 2791 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_PAR1) |
| 2792 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_BRACK1) |
| 2793 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_COMMA) |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2794 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_LOG) |
| 2795 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_EQUAL) |
| 2796 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_NEQUAL) |
| 2797 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_COMP) |
| 2798 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_MATH) |
| 2799 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_UNI) |
| 2800 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_PATH) |
| 2801 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_RPATH)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2802 | |
| 2803 | /* Operator '*', 'or', 'and', 'mod', or 'div' */ |
| 2804 | if (expr[parsed] == '*') { |
| 2805 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2806 | tok_type = LYXP_TOKEN_OPER_MATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2807 | |
| 2808 | } else if (!strncmp(&expr[parsed], "or", 2)) { |
| 2809 | tok_len = 2; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2810 | tok_type = LYXP_TOKEN_OPER_LOG; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2811 | |
| 2812 | } else if (!strncmp(&expr[parsed], "and", 3)) { |
| 2813 | tok_len = 3; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2814 | tok_type = LYXP_TOKEN_OPER_LOG; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2815 | |
| 2816 | } else if (!strncmp(&expr[parsed], "mod", 3) || !strncmp(&expr[parsed], "div", 3)) { |
| 2817 | tok_len = 3; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2818 | tok_type = LYXP_TOKEN_OPER_MATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2819 | |
| 2820 | } else if (prev_function_check) { |
Michal Vasko | 5307857 | 2019-05-24 08:50:15 +0200 | [diff] [blame] | 2821 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Invalid character 0x%x ('%c'), perhaps \"%.*s\" is supposed to be a function call.", |
| 2822 | expr[parsed], expr[parsed], ret->tok_len[ret->used - 1], &ret->expr[ret->tok_pos[ret->used - 1]]); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2823 | goto error; |
| 2824 | } else { |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2825 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INEXPR, parsed + 1, expr); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2826 | goto error; |
| 2827 | } |
| 2828 | } else if (expr[parsed] == '*') { |
| 2829 | |
| 2830 | /* NameTest '*' */ |
| 2831 | tok_len = 1; |
| 2832 | tok_type = LYXP_TOKEN_NAMETEST; |
| 2833 | |
| 2834 | } else { |
| 2835 | |
| 2836 | /* NameTest (NCName ':' '*' | QName) or NodeType/FunctionName */ |
| 2837 | ncname_len = parse_ncname(&expr[parsed]); |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2838 | LY_CHECK_ERR_GOTO(ncname_len < 0, LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INEXPR, parsed - ncname_len + 1, expr), error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2839 | tok_len = ncname_len; |
| 2840 | |
| 2841 | if (expr[parsed + tok_len] == ':') { |
| 2842 | ++tok_len; |
| 2843 | if (expr[parsed + tok_len] == '*') { |
| 2844 | ++tok_len; |
| 2845 | } else { |
| 2846 | ncname_len = parse_ncname(&expr[parsed + tok_len]); |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2847 | LY_CHECK_ERR_GOTO(ncname_len < 0, LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INEXPR, parsed - ncname_len + 1, expr), error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2848 | tok_len += ncname_len; |
| 2849 | } |
| 2850 | /* remove old flag to prevent ambiguities */ |
| 2851 | prev_function_check = 0; |
| 2852 | tok_type = LYXP_TOKEN_NAMETEST; |
| 2853 | } else { |
| 2854 | /* there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */ |
| 2855 | prev_function_check = 1; |
| 2856 | tok_type = LYXP_TOKEN_NAMETEST; |
| 2857 | } |
| 2858 | } |
| 2859 | |
| 2860 | /* store the token, move on to the next one */ |
| 2861 | LY_CHECK_GOTO(exp_add_token(ctx, ret, tok_type, parsed, tok_len), error); |
| 2862 | parsed += tok_len; |
| 2863 | while (is_xmlws(expr[parsed])) { |
| 2864 | ++parsed; |
| 2865 | } |
| 2866 | |
| 2867 | } while (expr[parsed]); |
| 2868 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2869 | if (reparse) { |
| 2870 | /* prealloc repeat */ |
| 2871 | ret->repeat = calloc(ret->size, sizeof *ret->repeat); |
| 2872 | LY_CHECK_ERR_GOTO(!ret->repeat, LOGMEM(ctx), error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2873 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2874 | /* fill repeat */ |
| 2875 | LY_CHECK_GOTO(reparse_or_expr(ctx, ret, &tok_idx), error); |
| 2876 | if (ret->used > tok_idx) { |
| 2877 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Unparsed characters \"%s\" left at the end of an XPath expression.", |
| 2878 | &ret->expr[ret->tok_pos[tok_idx]]); |
| 2879 | goto error; |
| 2880 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2881 | } |
| 2882 | |
| 2883 | print_expr_struct_debug(ret); |
| 2884 | |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2885 | return ret; |
| 2886 | |
| 2887 | error: |
| 2888 | lyxp_expr_free(ctx, ret); |
| 2889 | return NULL; |
| 2890 | } |
| 2891 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2892 | struct lyxp_expr * |
| 2893 | lyxp_expr_dup(const struct ly_ctx *ctx, const struct lyxp_expr *exp) |
| 2894 | { |
| 2895 | struct lyxp_expr *dup; |
| 2896 | uint32_t i, j; |
| 2897 | |
| 2898 | dup = calloc(1, sizeof *dup); |
| 2899 | LY_CHECK_ERR_GOTO(!dup, LOGMEM(ctx), error); |
| 2900 | |
| 2901 | dup->tokens = malloc(exp->used * sizeof *dup->tokens); |
| 2902 | LY_CHECK_ERR_GOTO(!dup->tokens, LOGMEM(ctx), error); |
| 2903 | memcpy(dup->tokens, exp->tokens, exp->used * sizeof *dup->tokens); |
| 2904 | |
| 2905 | dup->tok_pos = malloc(exp->used * sizeof *dup->tok_pos); |
| 2906 | LY_CHECK_ERR_GOTO(!dup->tok_pos, LOGMEM(ctx), error); |
| 2907 | memcpy(dup->tok_pos, exp->tok_pos, exp->used * sizeof *dup->tok_pos); |
| 2908 | |
| 2909 | dup->tok_len = malloc(exp->used * sizeof *dup->tok_len); |
| 2910 | LY_CHECK_ERR_GOTO(!dup->tok_len, LOGMEM(ctx), error); |
| 2911 | memcpy(dup->tok_len, exp->tok_len, exp->used * sizeof *dup->tok_len); |
| 2912 | |
| 2913 | dup->repeat = malloc(exp->used * sizeof *dup->repeat); |
| 2914 | LY_CHECK_ERR_GOTO(!dup->repeat, LOGMEM(ctx), error); |
| 2915 | for (i = 0; i < exp->used; ++i) { |
| 2916 | if (!exp->repeat[i]) { |
| 2917 | dup->repeat[i] = NULL; |
| 2918 | } else { |
| 2919 | for (j = 0; exp->repeat[i][j]; ++j); |
| 2920 | /* the ending 0 as well */ |
| 2921 | ++j; |
| 2922 | |
| 2923 | dup->repeat[i] = malloc(j * sizeof *dup->repeat); |
| 2924 | LY_CHECK_ERR_GOTO(!dup->repeat[i], LOGMEM(ctx), error); |
| 2925 | memcpy(dup->repeat[i], exp->repeat[i], j * sizeof **dup->repeat); |
| 2926 | dup->repeat[i][j - 1] = 0; |
| 2927 | } |
| 2928 | } |
| 2929 | |
| 2930 | dup->used = exp->used; |
| 2931 | dup->size = exp->used; |
| 2932 | dup->expr = lydict_insert(ctx, exp->expr, 0); |
| 2933 | |
| 2934 | return dup; |
| 2935 | |
| 2936 | error: |
| 2937 | lyxp_expr_free(ctx, dup); |
| 2938 | return NULL; |
| 2939 | } |
| 2940 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2941 | /* |
| 2942 | * warn functions |
| 2943 | * |
| 2944 | * Warn functions check specific reasonable conditions for schema XPath |
| 2945 | * and print a warning if they are not satisfied. |
| 2946 | */ |
| 2947 | |
| 2948 | /** |
| 2949 | * @brief Get the last-added schema node that is currently in the context. |
| 2950 | * |
| 2951 | * @param[in] set Set to search in. |
| 2952 | * @return Last-added schema context node, NULL if no node is in context. |
| 2953 | */ |
| 2954 | static struct lysc_node * |
| 2955 | warn_get_scnode_in_ctx(struct lyxp_set *set) |
| 2956 | { |
| 2957 | uint32_t i; |
| 2958 | |
| 2959 | if (!set || (set->type != LYXP_SET_SCNODE_SET)) { |
| 2960 | return NULL; |
| 2961 | } |
| 2962 | |
| 2963 | i = set->used; |
| 2964 | do { |
| 2965 | --i; |
| 2966 | if (set->val.scnodes[i].in_ctx == 1) { |
| 2967 | /* if there are more, simply return the first found (last added) */ |
| 2968 | return set->val.scnodes[i].scnode; |
| 2969 | } |
| 2970 | } while (i); |
| 2971 | |
| 2972 | return NULL; |
| 2973 | } |
| 2974 | |
| 2975 | /** |
| 2976 | * @brief Test whether a type is numeric - integer type or decimal64. |
| 2977 | * |
| 2978 | * @param[in] type Type to test. |
| 2979 | * @return 1 if numeric, 0 otherwise. |
| 2980 | */ |
| 2981 | static int |
| 2982 | warn_is_numeric_type(struct lysc_type *type) |
| 2983 | { |
| 2984 | struct lysc_type_union *uni; |
| 2985 | int ret; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 2986 | LY_ARRAY_SIZE_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2987 | |
| 2988 | switch (type->basetype) { |
| 2989 | case LY_TYPE_DEC64: |
| 2990 | case LY_TYPE_INT8: |
| 2991 | case LY_TYPE_UINT8: |
| 2992 | case LY_TYPE_INT16: |
| 2993 | case LY_TYPE_UINT16: |
| 2994 | case LY_TYPE_INT32: |
| 2995 | case LY_TYPE_UINT32: |
| 2996 | case LY_TYPE_INT64: |
| 2997 | case LY_TYPE_UINT64: |
| 2998 | return 1; |
| 2999 | case LY_TYPE_UNION: |
| 3000 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3001 | LY_ARRAY_FOR(uni->types, u) { |
| 3002 | ret = warn_is_numeric_type(uni->types[u]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3003 | if (ret) { |
| 3004 | /* found a suitable type */ |
| 3005 | return 1; |
| 3006 | } |
| 3007 | } |
| 3008 | /* did not find any suitable type */ |
| 3009 | return 0; |
| 3010 | case LY_TYPE_LEAFREF: |
| 3011 | return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype); |
| 3012 | default: |
| 3013 | return 0; |
| 3014 | } |
| 3015 | } |
| 3016 | |
| 3017 | /** |
| 3018 | * @brief Test whether a type is string-like - no integers, decimal64 or binary. |
| 3019 | * |
| 3020 | * @param[in] type Type to test. |
| 3021 | * @return 1 if string, 0 otherwise. |
| 3022 | */ |
| 3023 | static int |
| 3024 | warn_is_string_type(struct lysc_type *type) |
| 3025 | { |
| 3026 | struct lysc_type_union *uni; |
| 3027 | int ret; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3028 | LY_ARRAY_SIZE_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3029 | |
| 3030 | switch (type->basetype) { |
| 3031 | case LY_TYPE_BITS: |
| 3032 | case LY_TYPE_ENUM: |
| 3033 | case LY_TYPE_IDENT: |
| 3034 | case LY_TYPE_INST: |
| 3035 | case LY_TYPE_STRING: |
| 3036 | return 1; |
| 3037 | case LY_TYPE_UNION: |
| 3038 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3039 | LY_ARRAY_FOR(uni->types, u) { |
| 3040 | ret = warn_is_string_type(uni->types[u]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3041 | if (ret) { |
| 3042 | /* found a suitable type */ |
| 3043 | return 1; |
| 3044 | } |
| 3045 | } |
| 3046 | /* did not find any suitable type */ |
| 3047 | return 0; |
| 3048 | case LY_TYPE_LEAFREF: |
| 3049 | return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype); |
| 3050 | default: |
| 3051 | return 0; |
| 3052 | } |
| 3053 | } |
| 3054 | |
| 3055 | /** |
| 3056 | * @brief Test whether a type is one specific type. |
| 3057 | * |
| 3058 | * @param[in] type Type to test. |
| 3059 | * @param[in] base Expected type. |
| 3060 | * @return 1 if it is, 0 otherwise. |
| 3061 | */ |
| 3062 | static int |
| 3063 | warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base) |
| 3064 | { |
| 3065 | struct lysc_type_union *uni; |
| 3066 | int ret; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3067 | LY_ARRAY_SIZE_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3068 | |
| 3069 | if (type->basetype == base) { |
| 3070 | return 1; |
| 3071 | } else if (type->basetype == LY_TYPE_UNION) { |
| 3072 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3073 | LY_ARRAY_FOR(uni->types, u) { |
| 3074 | ret = warn_is_specific_type(uni->types[u], base); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3075 | if (ret) { |
| 3076 | /* found a suitable type */ |
| 3077 | return 1; |
| 3078 | } |
| 3079 | } |
| 3080 | /* did not find any suitable type */ |
| 3081 | return 0; |
| 3082 | } else if (type->basetype == LY_TYPE_LEAFREF) { |
| 3083 | return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base); |
| 3084 | } |
| 3085 | |
| 3086 | return 0; |
| 3087 | } |
| 3088 | |
| 3089 | /** |
| 3090 | * @brief Get next type of a (union) type. |
| 3091 | * |
| 3092 | * @param[in] type Base type. |
| 3093 | * @param[in] prev_type Previously returned type. |
| 3094 | * @return Next type or NULL. |
| 3095 | */ |
| 3096 | static struct lysc_type * |
| 3097 | warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type) |
| 3098 | { |
| 3099 | struct lysc_type_union *uni; |
| 3100 | int found = 0; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3101 | LY_ARRAY_SIZE_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3102 | |
| 3103 | switch (type->basetype) { |
| 3104 | case LY_TYPE_UNION: |
| 3105 | uni = (struct lysc_type_union *)type; |
| 3106 | if (!prev_type) { |
| 3107 | return uni->types[0]; |
| 3108 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3109 | LY_ARRAY_FOR(uni->types, u) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3110 | if (found) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3111 | return uni->types[u]; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3112 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3113 | if (prev_type == uni->types[u]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3114 | found = 1; |
| 3115 | } |
| 3116 | } |
| 3117 | return NULL; |
| 3118 | default: |
| 3119 | if (prev_type) { |
| 3120 | assert(type == prev_type); |
| 3121 | return NULL; |
| 3122 | } else { |
| 3123 | return type; |
| 3124 | } |
| 3125 | } |
| 3126 | } |
| 3127 | |
| 3128 | /** |
| 3129 | * @brief Test whether 2 types have a common type. |
| 3130 | * |
| 3131 | * @param[in] type1 First type. |
| 3132 | * @param[in] type2 Second type. |
| 3133 | * @return 1 if they do, 0 otherwise. |
| 3134 | */ |
| 3135 | static int |
| 3136 | warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2) |
| 3137 | { |
| 3138 | struct lysc_type *t1, *rt1, *t2, *rt2; |
| 3139 | |
| 3140 | t1 = NULL; |
| 3141 | while ((t1 = warn_is_equal_type_next_type(type1, t1))) { |
| 3142 | if (t1->basetype == LY_TYPE_LEAFREF) { |
| 3143 | rt1 = ((struct lysc_type_leafref *)t1)->realtype; |
| 3144 | } else { |
| 3145 | rt1 = t1; |
| 3146 | } |
| 3147 | |
| 3148 | t2 = NULL; |
| 3149 | while ((t2 = warn_is_equal_type_next_type(type2, t2))) { |
| 3150 | if (t2->basetype == LY_TYPE_LEAFREF) { |
| 3151 | rt2 = ((struct lysc_type_leafref *)t2)->realtype; |
| 3152 | } else { |
| 3153 | rt2 = t2; |
| 3154 | } |
| 3155 | |
| 3156 | if (rt2->basetype == rt1->basetype) { |
| 3157 | /* match found */ |
| 3158 | return 1; |
| 3159 | } |
| 3160 | } |
| 3161 | } |
| 3162 | |
| 3163 | return 0; |
| 3164 | } |
| 3165 | |
| 3166 | /** |
| 3167 | * @brief Check both operands of comparison operators. |
| 3168 | * |
| 3169 | * @param[in] ctx Context for errors. |
| 3170 | * @param[in] set1 First operand set. |
| 3171 | * @param[in] set2 Second operand set. |
| 3172 | * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!='). |
| 3173 | * @param[in] expr Start of the expression to print with the warning. |
| 3174 | * @param[in] tok_pos Token position. |
| 3175 | */ |
| 3176 | static void |
| 3177 | warn_operands(struct ly_ctx *ctx, struct lyxp_set *set1, struct lyxp_set *set2, int numbers_only, const char *expr, uint16_t tok_pos) |
| 3178 | { |
| 3179 | struct lysc_node_leaf *node1, *node2; |
| 3180 | int leaves = 1, warning = 0; |
| 3181 | |
| 3182 | node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1); |
| 3183 | node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2); |
| 3184 | |
| 3185 | if (!node1 && !node2) { |
| 3186 | /* no node-sets involved, nothing to do */ |
| 3187 | return; |
| 3188 | } |
| 3189 | |
| 3190 | if (node1) { |
| 3191 | if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3192 | LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name); |
| 3193 | warning = 1; |
| 3194 | leaves = 0; |
| 3195 | } else if (numbers_only && !warn_is_numeric_type(node1->type)) { |
| 3196 | LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name); |
| 3197 | warning = 1; |
| 3198 | } |
| 3199 | } |
| 3200 | |
| 3201 | if (node2) { |
| 3202 | if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3203 | LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name); |
| 3204 | warning = 1; |
| 3205 | leaves = 0; |
| 3206 | } else if (numbers_only && !warn_is_numeric_type(node2->type)) { |
| 3207 | LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name); |
| 3208 | warning = 1; |
| 3209 | } |
| 3210 | } |
| 3211 | |
| 3212 | if (node1 && node2 && leaves && !numbers_only) { |
| 3213 | if ((warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type)) |
| 3214 | || (!warn_is_numeric_type(node1->type) && warn_is_numeric_type(node2->type)) |
| 3215 | || (!warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type) |
| 3216 | && !warn_is_equal_type(node1->type, node2->type))) { |
| 3217 | LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name); |
| 3218 | warning = 1; |
| 3219 | } |
| 3220 | } |
| 3221 | |
| 3222 | if (warning) { |
| 3223 | LOGWRN(ctx, "Previous warning generated by XPath subexpression[%u] \"%.20s\".", tok_pos, expr + tok_pos); |
| 3224 | } |
| 3225 | } |
| 3226 | |
| 3227 | /** |
| 3228 | * @brief Check that a value is valid for a leaf. If not applicable, does nothing. |
| 3229 | * |
| 3230 | * @param[in] exp Parsed XPath expression. |
| 3231 | * @param[in] set Set with the leaf/leaf-list. |
| 3232 | * @param[in] val_exp Index of the value (literal/number) in @p exp. |
| 3233 | * @param[in] equal_exp Index of the start of the equality expression in @p exp. |
| 3234 | * @param[in] last_equal_exp Index of the end of the equality expression in @p exp. |
| 3235 | */ |
| 3236 | static void |
| 3237 | warn_equality_value(struct lyxp_expr *exp, struct lyxp_set *set, uint16_t val_exp, uint16_t equal_exp, uint16_t last_equal_exp) |
| 3238 | { |
| 3239 | struct lysc_node *scnode; |
| 3240 | struct lysc_type *type; |
| 3241 | char *value; |
| 3242 | LY_ERR rc; |
| 3243 | struct ly_err_item *err = NULL; |
| 3244 | |
| 3245 | if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST)) |
| 3246 | && ((exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) || (exp->tokens[val_exp] == LYXP_TOKEN_NUMBER))) { |
| 3247 | /* check that the node can have the specified value */ |
| 3248 | if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) { |
| 3249 | value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2); |
| 3250 | } else { |
| 3251 | value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]); |
| 3252 | } |
| 3253 | if (!value) { |
| 3254 | LOGMEM(set->ctx); |
| 3255 | return; |
| 3256 | } |
| 3257 | |
| 3258 | if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) { |
| 3259 | LOGWRN(set->ctx, "Identityref \"%s\" comparison with identity \"%s\" without prefix, consider adding" |
| 3260 | " a prefix or best using \"derived-from(-or-self)()\" functions.", scnode->name, value); |
| 3261 | LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp], |
| 3262 | (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp], |
| 3263 | exp->expr + exp->tok_pos[equal_exp]); |
| 3264 | } |
| 3265 | |
| 3266 | type = ((struct lysc_node_leaf *)scnode)->type; |
| 3267 | if (type->basetype != LY_TYPE_IDENT) { |
| 3268 | rc = type->plugin->store(set->ctx, type, value, strlen(value), LY_TYPE_OPTS_SCHEMA, |
| 3269 | lys_resolve_prefix, (void *)type->dflt_mod, LYD_XML, NULL, NULL, NULL, NULL, &err); |
| 3270 | |
| 3271 | if (err) { |
| 3272 | LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg); |
| 3273 | ly_err_free(err); |
| 3274 | } else if (rc != LY_SUCCESS) { |
| 3275 | LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value); |
| 3276 | } |
| 3277 | if (rc != LY_SUCCESS) { |
| 3278 | LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp], |
| 3279 | (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp], |
| 3280 | exp->expr + exp->tok_pos[equal_exp]); |
| 3281 | } |
| 3282 | } |
| 3283 | free(value); |
| 3284 | } |
| 3285 | } |
| 3286 | |
| 3287 | /* |
| 3288 | * XPath functions |
| 3289 | */ |
| 3290 | |
| 3291 | /** |
| 3292 | * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN |
| 3293 | * depending on whether the first node bit value from the second argument is set. |
| 3294 | * |
| 3295 | * @param[in] args Array of arguments. |
| 3296 | * @param[in] arg_count Count of elements in @p args. |
| 3297 | * @param[in,out] set Context and result set at the same time. |
| 3298 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3299 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3300 | */ |
| 3301 | static LY_ERR |
| 3302 | xpath_bit_is_set(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3303 | { |
| 3304 | struct lyd_node_term *leaf; |
| 3305 | struct lysc_node_leaf *sleaf; |
| 3306 | struct lysc_type_bits *bits; |
| 3307 | LY_ERR rc = LY_SUCCESS; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3308 | LY_ARRAY_SIZE_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3309 | |
| 3310 | if (options & LYXP_SCNODE_ALL) { |
| 3311 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3312 | LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3313 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3314 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3315 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) { |
| 3316 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"bits\".", __func__, sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3317 | } |
| 3318 | |
| 3319 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 3320 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3321 | LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3322 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3323 | LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3324 | } |
| 3325 | } |
| 3326 | set_scnode_clear_ctx(set); |
| 3327 | return rc; |
| 3328 | } |
| 3329 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3330 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3331 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "bit-is-set(node-set, string)"); |
| 3332 | return LY_EVALID; |
| 3333 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3334 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3335 | LY_CHECK_RET(rc); |
| 3336 | |
| 3337 | set_fill_boolean(set, 0); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3338 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3339 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
| 3340 | if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) |
| 3341 | && (((struct lysc_node_leaf *)leaf->schema)->type->basetype == LY_TYPE_BITS)) { |
| 3342 | bits = (struct lysc_type_bits *)((struct lysc_node_leaf *)leaf->schema)->type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3343 | LY_ARRAY_FOR(bits->bits, u) { |
| 3344 | if (!strcmp(bits->bits[u].name, args[1]->val.str)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3345 | set_fill_boolean(set, 1); |
| 3346 | break; |
| 3347 | } |
| 3348 | } |
| 3349 | } |
| 3350 | } |
| 3351 | |
| 3352 | return LY_SUCCESS; |
| 3353 | } |
| 3354 | |
| 3355 | /** |
| 3356 | * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN |
| 3357 | * with the argument converted to boolean. |
| 3358 | * |
| 3359 | * @param[in] args Array of arguments. |
| 3360 | * @param[in] arg_count Count of elements in @p args. |
| 3361 | * @param[in,out] set Context and result set at the same time. |
| 3362 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3363 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3364 | */ |
| 3365 | static LY_ERR |
| 3366 | xpath_boolean(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3367 | { |
| 3368 | LY_ERR rc; |
| 3369 | |
| 3370 | if (options & LYXP_SCNODE_ALL) { |
| 3371 | set_scnode_clear_ctx(set); |
| 3372 | return LY_SUCCESS; |
| 3373 | } |
| 3374 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3375 | rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3376 | LY_CHECK_RET(rc); |
| 3377 | set_fill_set(set, args[0]); |
| 3378 | |
| 3379 | return LY_SUCCESS; |
| 3380 | } |
| 3381 | |
| 3382 | /** |
| 3383 | * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER |
| 3384 | * with the first argument rounded up to the nearest integer. |
| 3385 | * |
| 3386 | * @param[in] args Array of arguments. |
| 3387 | * @param[in] arg_count Count of elements in @p args. |
| 3388 | * @param[in,out] set Context and result set at the same time. |
| 3389 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3390 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3391 | */ |
| 3392 | static LY_ERR |
| 3393 | xpath_ceiling(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3394 | { |
| 3395 | struct lysc_node_leaf *sleaf; |
| 3396 | LY_ERR rc = LY_SUCCESS; |
| 3397 | |
| 3398 | if (options & LYXP_SCNODE_ALL) { |
| 3399 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3400 | LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3401 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3402 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3403 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) { |
| 3404 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3405 | } |
| 3406 | set_scnode_clear_ctx(set); |
| 3407 | return rc; |
| 3408 | } |
| 3409 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3410 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3411 | LY_CHECK_RET(rc); |
| 3412 | if ((long long)args[0]->val.num != args[0]->val.num) { |
| 3413 | set_fill_number(set, ((long long)args[0]->val.num) + 1); |
| 3414 | } else { |
| 3415 | set_fill_number(set, args[0]->val.num); |
| 3416 | } |
| 3417 | |
| 3418 | return LY_SUCCESS; |
| 3419 | } |
| 3420 | |
| 3421 | /** |
| 3422 | * @brief Execute the XPath concat(string, string, string*) function. |
| 3423 | * Returns LYXP_SET_STRING with the concatenation of all the arguments. |
| 3424 | * |
| 3425 | * @param[in] args Array of arguments. |
| 3426 | * @param[in] arg_count Count of elements in @p args. |
| 3427 | * @param[in,out] set Context and result set at the same time. |
| 3428 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3429 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3430 | */ |
| 3431 | static LY_ERR |
| 3432 | xpath_concat(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options) |
| 3433 | { |
| 3434 | uint16_t i; |
| 3435 | char *str = NULL; |
| 3436 | size_t used = 1; |
| 3437 | LY_ERR rc = LY_SUCCESS; |
| 3438 | struct lysc_node_leaf *sleaf; |
| 3439 | |
| 3440 | if (options & LYXP_SCNODE_ALL) { |
| 3441 | for (i = 0; i < arg_count; ++i) { |
| 3442 | if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) { |
| 3443 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3444 | LOGWRN(set->ctx, "Argument #%u of %s is a %s node \"%s\".", |
| 3445 | i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3446 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3447 | LOGWRN(set->ctx, "Argument #%u of %s is node \"%s\", not of string-type.", __func__, i + 1, sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3448 | } |
| 3449 | } |
| 3450 | } |
| 3451 | set_scnode_clear_ctx(set); |
| 3452 | return rc; |
| 3453 | } |
| 3454 | |
| 3455 | for (i = 0; i < arg_count; ++i) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3456 | rc = lyxp_set_cast(args[i], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3457 | if (rc != LY_SUCCESS) { |
| 3458 | free(str); |
| 3459 | return rc; |
| 3460 | } |
| 3461 | |
| 3462 | str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char)); |
| 3463 | LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM); |
| 3464 | strcpy(str + used - 1, args[i]->val.str); |
| 3465 | used += strlen(args[i]->val.str); |
| 3466 | } |
| 3467 | |
| 3468 | /* free, kind of */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3469 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3470 | set->type = LYXP_SET_STRING; |
| 3471 | set->val.str = str; |
| 3472 | |
| 3473 | return LY_SUCCESS; |
| 3474 | } |
| 3475 | |
| 3476 | /** |
| 3477 | * @brief Execute the XPath contains(string, string) function. |
| 3478 | * Returns LYXP_SET_BOOLEAN whether the second argument can |
| 3479 | * be found in the first or not. |
| 3480 | * |
| 3481 | * @param[in] args Array of arguments. |
| 3482 | * @param[in] arg_count Count of elements in @p args. |
| 3483 | * @param[in,out] set Context and result set at the same time. |
| 3484 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3485 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3486 | */ |
| 3487 | static LY_ERR |
| 3488 | xpath_contains(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3489 | { |
| 3490 | struct lysc_node_leaf *sleaf; |
| 3491 | LY_ERR rc = LY_SUCCESS; |
| 3492 | |
| 3493 | if (options & LYXP_SCNODE_ALL) { |
| 3494 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3495 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3496 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3497 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3498 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3499 | } |
| 3500 | } |
| 3501 | |
| 3502 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 3503 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3504 | LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3505 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3506 | LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3507 | } |
| 3508 | } |
| 3509 | set_scnode_clear_ctx(set); |
| 3510 | return rc; |
| 3511 | } |
| 3512 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3513 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3514 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3515 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3516 | LY_CHECK_RET(rc); |
| 3517 | |
| 3518 | if (strstr(args[0]->val.str, args[1]->val.str)) { |
| 3519 | set_fill_boolean(set, 1); |
| 3520 | } else { |
| 3521 | set_fill_boolean(set, 0); |
| 3522 | } |
| 3523 | |
| 3524 | return LY_SUCCESS; |
| 3525 | } |
| 3526 | |
| 3527 | /** |
| 3528 | * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER |
| 3529 | * with the size of the node-set from the argument. |
| 3530 | * |
| 3531 | * @param[in] args Array of arguments. |
| 3532 | * @param[in] arg_count Count of elements in @p args. |
| 3533 | * @param[in,out] set Context and result set at the same time. |
| 3534 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3535 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3536 | */ |
| 3537 | static LY_ERR |
| 3538 | xpath_count(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3539 | { |
| 3540 | struct lysc_node *scnode = NULL; |
| 3541 | LY_ERR rc = LY_SUCCESS; |
| 3542 | |
| 3543 | if (options & LYXP_SCNODE_ALL) { |
| 3544 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(scnode = warn_get_scnode_in_ctx(args[0]))) { |
| 3545 | LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3546 | } |
| 3547 | set_scnode_clear_ctx(set); |
| 3548 | return rc; |
| 3549 | } |
| 3550 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3551 | if (args[0]->type != LYXP_SET_NODE_SET) { |
| 3552 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "count(node-set)"); |
| 3553 | return LY_EVALID; |
| 3554 | } |
| 3555 | |
| 3556 | set_fill_number(set, args[0]->used); |
| 3557 | return LY_SUCCESS; |
| 3558 | } |
| 3559 | |
| 3560 | /** |
| 3561 | * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET |
| 3562 | * with the context with the intial node. |
| 3563 | * |
| 3564 | * @param[in] args Array of arguments. |
| 3565 | * @param[in] arg_count Count of elements in @p args. |
| 3566 | * @param[in,out] set Context and result set at the same time. |
| 3567 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3568 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3569 | */ |
| 3570 | static LY_ERR |
| 3571 | xpath_current(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options) |
| 3572 | { |
| 3573 | if (arg_count || args) { |
| 3574 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGCOUNT, arg_count, "current()"); |
| 3575 | return LY_EVALID; |
| 3576 | } |
| 3577 | |
| 3578 | if (options & LYXP_SCNODE_ALL) { |
| 3579 | set_scnode_clear_ctx(set); |
| 3580 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 3581 | lyxp_set_scnode_insert_node(set, set->ctx_scnode, LYXP_NODE_ELEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3582 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3583 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3584 | |
| 3585 | /* position is filled later */ |
| 3586 | set_insert_node(set, set->ctx_node, 0, LYXP_NODE_ELEM, 0); |
| 3587 | } |
| 3588 | |
| 3589 | return LY_SUCCESS; |
| 3590 | } |
| 3591 | |
| 3592 | /** |
| 3593 | * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either |
| 3594 | * leafref or instance-identifier target node(s). |
| 3595 | * |
| 3596 | * @param[in] args Array of arguments. |
| 3597 | * @param[in] arg_count Count of elements in @p args. |
| 3598 | * @param[in,out] set Context and result set at the same time. |
| 3599 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3600 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3601 | */ |
| 3602 | static LY_ERR |
| 3603 | xpath_deref(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3604 | { |
| 3605 | struct lyd_node_term *leaf; |
Michal Vasko | 42e497c | 2020-01-06 08:38:25 +0100 | [diff] [blame] | 3606 | struct lysc_node_leaf *sleaf = NULL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3607 | struct lysc_type_leafref *lref; |
Michal Vasko | ae9e4cb | 2019-09-25 08:43:05 +0200 | [diff] [blame] | 3608 | const struct lysc_node *target; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3609 | struct ly_path *p; |
| 3610 | struct lyd_node *node; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3611 | char *errmsg = NULL; |
| 3612 | const char *val; |
| 3613 | int dynamic; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame^] | 3614 | uint8_t oper; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3615 | LY_ERR rc = LY_SUCCESS; |
| 3616 | |
| 3617 | if (options & LYXP_SCNODE_ALL) { |
| 3618 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3619 | LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3620 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3621 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3622 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) && !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) { |
| 3623 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"leafref\" nor \"instance-identifier\".", |
| 3624 | __func__, sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3625 | } |
| 3626 | set_scnode_clear_ctx(set); |
Michal Vasko | 42e497c | 2020-01-06 08:38:25 +0100 | [diff] [blame] | 3627 | if (sleaf && (sleaf->type->basetype == LY_TYPE_LEAFREF)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3628 | lref = (struct lysc_type_leafref *)sleaf->type; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame^] | 3629 | oper = lysc_is_output((struct lysc_node *)sleaf) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3630 | |
| 3631 | /* it was already evaluated on schema, it must succeed */ |
| 3632 | if (set->format == LYD_JSON) { |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame^] | 3633 | rc = ly_path_compile(set->ctx, sleaf->module, (struct lysc_node *)sleaf, lref->path, LY_PATH_LREF_TRUE, |
| 3634 | oper, LY_PATH_TARGET_MANY, lydjson_resolve_prefix, NULL, LYD_JSON, &p); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3635 | } else { |
| 3636 | assert(set->format == LYD_SCHEMA); |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame^] | 3637 | rc = ly_path_compile(set->ctx, sleaf->module, (struct lysc_node *)sleaf, lref->path, LY_PATH_LREF_TRUE, |
| 3638 | oper, LY_PATH_TARGET_MANY, lys_resolve_prefix, lref->path_context, LYD_SCHEMA, &p); |
Michal Vasko | ae9e4cb | 2019-09-25 08:43:05 +0200 | [diff] [blame] | 3639 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3640 | assert(!rc); |
| 3641 | |
| 3642 | /* get the target node */ |
| 3643 | target = p[LY_ARRAY_SIZE(p) - 1].node; |
| 3644 | ly_path_free(set->ctx, p); |
| 3645 | |
| 3646 | lyxp_set_scnode_insert_node(set, target, LYXP_NODE_ELEM); |
Michal Vasko | ae9e4cb | 2019-09-25 08:43:05 +0200 | [diff] [blame] | 3647 | } |
| 3648 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3649 | return rc; |
| 3650 | } |
| 3651 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3652 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3653 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "deref(node-set)"); |
| 3654 | return LY_EVALID; |
| 3655 | } |
| 3656 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3657 | lyxp_set_free_content(set); |
| 3658 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3659 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
| 3660 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 3661 | if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 3662 | if (sleaf->type->basetype == LY_TYPE_LEAFREF) { |
| 3663 | /* find leafref target */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3664 | if (ly_type_find_leafref((struct lysc_type_leafref *)sleaf->type, (struct lyd_node *)leaf, |
| 3665 | &leaf->value, set->tree, &node, &errmsg)) { |
| 3666 | LOGERR(set->ctx, LY_EVALID, errmsg); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3667 | free(errmsg); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3668 | return LY_EVALID; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3669 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3670 | } else { |
| 3671 | assert(sleaf->type->basetype == LY_TYPE_INST); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3672 | if (ly_path_eval(leaf->value.target, set->tree, &node)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3673 | val = lyd_value2str(leaf, &dynamic); |
| 3674 | LOGERR(set->ctx, LY_EVALID, "Invalid instance-identifier \"%s\" value - required instance not found.", val); |
| 3675 | if (dynamic) { |
| 3676 | free((char *)val); |
| 3677 | } |
| 3678 | return LY_EVALID; |
| 3679 | } |
| 3680 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3681 | |
| 3682 | /* insert it */ |
| 3683 | set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3684 | } |
| 3685 | } |
| 3686 | |
| 3687 | return LY_SUCCESS; |
| 3688 | } |
| 3689 | |
| 3690 | /** |
| 3691 | * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending |
| 3692 | * on whether the first argument nodes contain a node of an identity derived from the second |
| 3693 | * argument identity. |
| 3694 | * |
| 3695 | * @param[in] args Array of arguments. |
| 3696 | * @param[in] arg_count Count of elements in @p args. |
| 3697 | * @param[in,out] set Context and result set at the same time. |
| 3698 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3699 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3700 | */ |
| 3701 | static LY_ERR |
| 3702 | xpath_derived_from(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3703 | { |
| 3704 | uint16_t i; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3705 | LY_ARRAY_SIZE_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 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__); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3716 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3717 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3718 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) { |
| 3719 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", __func__, sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3720 | } |
| 3721 | |
| 3722 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 3723 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3724 | LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3725 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3726 | LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3727 | } |
| 3728 | } |
| 3729 | set_scnode_clear_ctx(set); |
| 3730 | return rc; |
| 3731 | } |
| 3732 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3733 | if (args[0]->type != LYXP_SET_NODE_SET) { |
| 3734 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), |
| 3735 | "derived-from(node-set, string)"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3736 | return LY_EVALID; |
| 3737 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3738 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3739 | LY_CHECK_RET(rc); |
| 3740 | |
| 3741 | set_fill_boolean(set, 0); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3742 | found = 0; |
| 3743 | for (i = 0; i < args[0]->used; ++i) { |
| 3744 | leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node; |
| 3745 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 3746 | if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_IDENT)) { |
| 3747 | /* store args[1] into ident */ |
| 3748 | rc = sleaf->type->plugin->store(set->ctx, sleaf->type, args[1]->val.str, strlen(args[1]->val.str), |
| 3749 | LY_TYPE_OPTS_STORE, lys_resolve_prefix, (void *)sleaf->dflt_mod, set->format, |
| 3750 | (struct lyd_node *)leaf, set->tree, &data, NULL, &err); |
| 3751 | if (err) { |
| 3752 | ly_err_print(err); |
| 3753 | ly_err_free(err); |
| 3754 | } |
| 3755 | LY_CHECK_RET(rc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3756 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3757 | LY_ARRAY_FOR(data.ident->derived, u) { |
| 3758 | if (data.ident->derived[u] == leaf->value.ident) { |
| 3759 | set_fill_boolean(set, 1); |
| 3760 | found = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3761 | break; |
| 3762 | } |
| 3763 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3764 | if (found) { |
| 3765 | break; |
| 3766 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3767 | } |
| 3768 | } |
| 3769 | |
| 3770 | return LY_SUCCESS; |
| 3771 | } |
| 3772 | |
| 3773 | /** |
| 3774 | * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending |
| 3775 | * on whether the first argument nodes contain a node of an identity that either is or is derived from |
| 3776 | * the second argument identity. |
| 3777 | * |
| 3778 | * @param[in] args Array of arguments. |
| 3779 | * @param[in] arg_count Count of elements in @p args. |
| 3780 | * @param[in,out] set Context and result set at the same time. |
| 3781 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3782 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3783 | */ |
| 3784 | static LY_ERR |
| 3785 | xpath_derived_from_or_self(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3786 | { |
| 3787 | uint16_t i; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3788 | LY_ARRAY_SIZE_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3789 | struct lyd_node_term *leaf; |
| 3790 | struct lysc_node_leaf *sleaf; |
| 3791 | struct lyd_value data = {0}; |
| 3792 | struct ly_err_item *err = NULL; |
| 3793 | LY_ERR rc = LY_SUCCESS; |
| 3794 | int found; |
| 3795 | |
| 3796 | if (options & LYXP_SCNODE_ALL) { |
| 3797 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3798 | LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3799 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3800 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3801 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) { |
| 3802 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", __func__, sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3803 | } |
| 3804 | |
| 3805 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 3806 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3807 | LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3808 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3809 | LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3810 | } |
| 3811 | } |
| 3812 | set_scnode_clear_ctx(set); |
| 3813 | return rc; |
| 3814 | } |
| 3815 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3816 | if (args[0]->type != LYXP_SET_NODE_SET) { |
| 3817 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), |
| 3818 | "derived-from-or-self(node-set, string)"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3819 | return LY_EVALID; |
| 3820 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3821 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3822 | LY_CHECK_RET(rc); |
| 3823 | |
| 3824 | set_fill_boolean(set, 0); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3825 | found = 0; |
| 3826 | for (i = 0; i < args[0]->used; ++i) { |
| 3827 | leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node; |
| 3828 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 3829 | if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_IDENT)) { |
| 3830 | /* store args[1] into ident */ |
| 3831 | rc = sleaf->type->plugin->store(set->ctx, sleaf->type, args[1]->val.str, strlen(args[1]->val.str), |
| 3832 | LY_TYPE_OPTS_STORE, lys_resolve_prefix, (void *)sleaf->dflt_mod, set->format, |
| 3833 | (struct lyd_node *)leaf, set->tree, &data, NULL, &err); |
| 3834 | if (err) { |
| 3835 | ly_err_print(err); |
| 3836 | ly_err_free(err); |
| 3837 | } |
| 3838 | LY_CHECK_RET(rc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3839 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3840 | if (data.ident == leaf->value.ident) { |
| 3841 | set_fill_boolean(set, 1); |
| 3842 | break; |
| 3843 | } |
| 3844 | LY_ARRAY_FOR(data.ident->derived, u) { |
| 3845 | if (data.ident->derived[u] == leaf->value.ident) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3846 | set_fill_boolean(set, 1); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3847 | found = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3848 | break; |
| 3849 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3850 | } |
| 3851 | if (found) { |
| 3852 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3853 | } |
| 3854 | } |
| 3855 | } |
| 3856 | |
| 3857 | return LY_SUCCESS; |
| 3858 | } |
| 3859 | |
| 3860 | /** |
| 3861 | * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER |
| 3862 | * with the integer value of the first node's enum value, otherwise NaN. |
| 3863 | * |
| 3864 | * @param[in] args Array of arguments. |
| 3865 | * @param[in] arg_count Count of elements in @p args. |
| 3866 | * @param[in,out] set Context and result set at the same time. |
| 3867 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3868 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3869 | */ |
| 3870 | static LY_ERR |
| 3871 | xpath_enum_value(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3872 | { |
| 3873 | struct lyd_node_term *leaf; |
| 3874 | struct lysc_node_leaf *sleaf; |
| 3875 | LY_ERR rc = LY_SUCCESS; |
| 3876 | |
| 3877 | if (options & LYXP_SCNODE_ALL) { |
| 3878 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3879 | LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3880 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3881 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3882 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) { |
| 3883 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"enumeration\".", __func__, sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3884 | } |
| 3885 | set_scnode_clear_ctx(set); |
| 3886 | return rc; |
| 3887 | } |
| 3888 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3889 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3890 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "enum-value(node-set)"); |
| 3891 | return LY_EVALID; |
| 3892 | } |
| 3893 | |
| 3894 | set_fill_number(set, NAN); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3895 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3896 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
| 3897 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 3898 | if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) { |
| 3899 | set_fill_number(set, leaf->value.enum_item->value); |
| 3900 | } |
| 3901 | } |
| 3902 | |
| 3903 | return LY_SUCCESS; |
| 3904 | } |
| 3905 | |
| 3906 | /** |
| 3907 | * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN |
| 3908 | * with false value. |
| 3909 | * |
| 3910 | * @param[in] args Array of arguments. |
| 3911 | * @param[in] arg_count Count of elements in @p args. |
| 3912 | * @param[in,out] set Context and result set at the same time. |
| 3913 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3914 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3915 | */ |
| 3916 | static LY_ERR |
| 3917 | xpath_false(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3918 | { |
| 3919 | if (options & LYXP_SCNODE_ALL) { |
| 3920 | set_scnode_clear_ctx(set); |
| 3921 | return LY_SUCCESS; |
| 3922 | } |
| 3923 | |
| 3924 | set_fill_boolean(set, 0); |
| 3925 | return LY_SUCCESS; |
| 3926 | } |
| 3927 | |
| 3928 | /** |
| 3929 | * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER |
| 3930 | * with the first argument floored (truncated). |
| 3931 | * |
| 3932 | * @param[in] args Array of arguments. |
| 3933 | * @param[in] arg_count Count of elements in @p args. |
| 3934 | * @param[in,out] set Context and result set at the same time. |
| 3935 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3936 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3937 | */ |
| 3938 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3939 | 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] | 3940 | { |
| 3941 | LY_ERR rc; |
| 3942 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3943 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3944 | LY_CHECK_RET(rc); |
| 3945 | if (isfinite(args[0]->val.num)) { |
| 3946 | set_fill_number(set, (long long)args[0]->val.num); |
| 3947 | } |
| 3948 | |
| 3949 | return LY_SUCCESS; |
| 3950 | } |
| 3951 | |
| 3952 | /** |
| 3953 | * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN |
| 3954 | * whether the language of the text matches the one from the argument. |
| 3955 | * |
| 3956 | * @param[in] args Array of arguments. |
| 3957 | * @param[in] arg_count Count of elements in @p args. |
| 3958 | * @param[in,out] set Context and result set at the same time. |
| 3959 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3960 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3961 | */ |
| 3962 | static LY_ERR |
| 3963 | xpath_lang(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 3964 | { |
| 3965 | const struct lyd_node *node; |
| 3966 | struct lysc_node_leaf *sleaf; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 3967 | struct lyd_meta *meta = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3968 | const char *val; |
| 3969 | int i, dynamic; |
| 3970 | LY_ERR rc = LY_SUCCESS; |
| 3971 | |
| 3972 | if (options & LYXP_SCNODE_ALL) { |
| 3973 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3974 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3975 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3976 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3977 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3978 | } |
| 3979 | } |
| 3980 | set_scnode_clear_ctx(set); |
| 3981 | return rc; |
| 3982 | } |
| 3983 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3984 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3985 | LY_CHECK_RET(rc); |
| 3986 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3987 | if (set->type != LYXP_SET_NODE_SET) { |
| 3988 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "lang(string)"); |
| 3989 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3990 | } else if (!set->used) { |
| 3991 | set_fill_boolean(set, 0); |
| 3992 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3993 | } |
| 3994 | |
| 3995 | switch (set->val.nodes[0].type) { |
| 3996 | case LYXP_NODE_ELEM: |
| 3997 | case LYXP_NODE_TEXT: |
| 3998 | node = set->val.nodes[0].node; |
| 3999 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4000 | case LYXP_NODE_META: |
| 4001 | node = set->val.meta[0].meta->parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4002 | break; |
| 4003 | default: |
| 4004 | /* nothing to do with roots */ |
| 4005 | set_fill_boolean(set, 0); |
| 4006 | return LY_SUCCESS; |
| 4007 | } |
| 4008 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4009 | /* find lang metadata */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4010 | for (; node; node = (struct lyd_node *)node->parent) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4011 | for (meta = node->meta; meta; meta = meta->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4012 | /* annotations */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4013 | if (meta->name && !strcmp(meta->name, "lang") && !strcmp(meta->annotation->module->name, "xml")) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4014 | break; |
| 4015 | } |
| 4016 | } |
| 4017 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4018 | if (meta) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4019 | break; |
| 4020 | } |
| 4021 | } |
| 4022 | |
| 4023 | /* compare languages */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4024 | if (!meta) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4025 | set_fill_boolean(set, 0); |
| 4026 | } else { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4027 | val = lyd_meta2str(meta, &dynamic); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4028 | for (i = 0; args[0]->val.str[i]; ++i) { |
| 4029 | if (tolower(args[0]->val.str[i]) != tolower(val[i])) { |
| 4030 | set_fill_boolean(set, 0); |
| 4031 | break; |
| 4032 | } |
| 4033 | } |
| 4034 | if (!args[0]->val.str[i]) { |
| 4035 | if (!val[i] || (val[i] == '-')) { |
| 4036 | set_fill_boolean(set, 1); |
| 4037 | } else { |
| 4038 | set_fill_boolean(set, 0); |
| 4039 | } |
| 4040 | } |
| 4041 | if (dynamic) { |
| 4042 | free((char *)val); |
| 4043 | } |
| 4044 | } |
| 4045 | |
| 4046 | return LY_SUCCESS; |
| 4047 | } |
| 4048 | |
| 4049 | /** |
| 4050 | * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER |
| 4051 | * with the context size. |
| 4052 | * |
| 4053 | * @param[in] args Array of arguments. |
| 4054 | * @param[in] arg_count Count of elements in @p args. |
| 4055 | * @param[in,out] set Context and result set at the same time. |
| 4056 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4057 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4058 | */ |
| 4059 | static LY_ERR |
| 4060 | xpath_last(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 4061 | { |
| 4062 | if (options & LYXP_SCNODE_ALL) { |
| 4063 | set_scnode_clear_ctx(set); |
| 4064 | return LY_SUCCESS; |
| 4065 | } |
| 4066 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4067 | if (set->type != LYXP_SET_NODE_SET) { |
| 4068 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "last()"); |
| 4069 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4070 | } else if (!set->used) { |
| 4071 | set_fill_number(set, 0); |
| 4072 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4073 | } |
| 4074 | |
| 4075 | set_fill_number(set, set->ctx_size); |
| 4076 | return LY_SUCCESS; |
| 4077 | } |
| 4078 | |
| 4079 | /** |
| 4080 | * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING |
| 4081 | * with the node name without namespace from the argument or the context. |
| 4082 | * |
| 4083 | * @param[in] args Array of arguments. |
| 4084 | * @param[in] arg_count Count of elements in @p args. |
| 4085 | * @param[in,out] set Context and result set at the same time. |
| 4086 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4087 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4088 | */ |
| 4089 | static LY_ERR |
| 4090 | xpath_local_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options) |
| 4091 | { |
| 4092 | struct lyxp_set_node *item; |
| 4093 | /* suppress unused variable warning */ |
| 4094 | (void)options; |
| 4095 | |
| 4096 | if (options & LYXP_SCNODE_ALL) { |
| 4097 | set_scnode_clear_ctx(set); |
| 4098 | return LY_SUCCESS; |
| 4099 | } |
| 4100 | |
| 4101 | if (arg_count) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4102 | if (args[0]->type != LYXP_SET_NODE_SET) { |
| 4103 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "local-name(node-set?)"); |
| 4104 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4105 | } else if (!args[0]->used) { |
| 4106 | set_fill_string(set, "", 0); |
| 4107 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4108 | } |
| 4109 | |
| 4110 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4111 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4112 | |
| 4113 | item = &args[0]->val.nodes[0]; |
| 4114 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4115 | if (set->type != LYXP_SET_NODE_SET) { |
| 4116 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "local-name(node-set?)"); |
| 4117 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4118 | } else if (!set->used) { |
| 4119 | set_fill_string(set, "", 0); |
| 4120 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4121 | } |
| 4122 | |
| 4123 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4124 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4125 | |
| 4126 | item = &set->val.nodes[0]; |
| 4127 | } |
| 4128 | |
| 4129 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4130 | case LYXP_NODE_NONE: |
| 4131 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4132 | case LYXP_NODE_ROOT: |
| 4133 | case LYXP_NODE_ROOT_CONFIG: |
| 4134 | case LYXP_NODE_TEXT: |
| 4135 | set_fill_string(set, "", 0); |
| 4136 | break; |
| 4137 | case LYXP_NODE_ELEM: |
| 4138 | set_fill_string(set, item->node->schema->name, strlen(item->node->schema->name)); |
| 4139 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4140 | case LYXP_NODE_META: |
| 4141 | set_fill_string(set, ((struct lyd_meta *)item->node)->name, strlen(((struct lyd_meta *)item->node)->name)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4142 | break; |
| 4143 | } |
| 4144 | |
| 4145 | return LY_SUCCESS; |
| 4146 | } |
| 4147 | |
| 4148 | /** |
| 4149 | * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING |
| 4150 | * with the node name fully qualified (with namespace) from the argument or the context. |
| 4151 | * |
| 4152 | * @param[in] args Array of arguments. |
| 4153 | * @param[in] arg_count Count of elements in @p args. |
| 4154 | * @param[in,out] set Context and result set at the same time. |
| 4155 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4156 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4157 | */ |
| 4158 | static LY_ERR |
| 4159 | xpath_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options) |
| 4160 | { |
| 4161 | struct lyxp_set_node *item; |
| 4162 | struct lys_module *mod; |
| 4163 | char *str; |
| 4164 | const char *name; |
| 4165 | int rc; |
| 4166 | |
| 4167 | if (options & LYXP_SCNODE_ALL) { |
| 4168 | set_scnode_clear_ctx(set); |
| 4169 | return LY_SUCCESS; |
| 4170 | } |
| 4171 | |
| 4172 | if (arg_count) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4173 | if (args[0]->type != LYXP_SET_NODE_SET) { |
| 4174 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "name(node-set?)"); |
| 4175 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4176 | } else if (!args[0]->used) { |
| 4177 | set_fill_string(set, "", 0); |
| 4178 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4179 | } |
| 4180 | |
| 4181 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4182 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4183 | |
| 4184 | item = &args[0]->val.nodes[0]; |
| 4185 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4186 | if (set->type != LYXP_SET_NODE_SET) { |
| 4187 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "name(node-set?)"); |
| 4188 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4189 | } else if (!set->used) { |
| 4190 | set_fill_string(set, "", 0); |
| 4191 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4192 | } |
| 4193 | |
| 4194 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4195 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4196 | |
| 4197 | item = &set->val.nodes[0]; |
| 4198 | } |
| 4199 | |
| 4200 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4201 | case LYXP_NODE_NONE: |
| 4202 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4203 | case LYXP_NODE_ROOT: |
| 4204 | case LYXP_NODE_ROOT_CONFIG: |
| 4205 | case LYXP_NODE_TEXT: |
| 4206 | mod = NULL; |
| 4207 | name = NULL; |
| 4208 | break; |
| 4209 | case LYXP_NODE_ELEM: |
| 4210 | mod = item->node->schema->module; |
| 4211 | name = item->node->schema->name; |
| 4212 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4213 | case LYXP_NODE_META: |
| 4214 | mod = ((struct lyd_meta *)item->node)->annotation->module; |
| 4215 | name = ((struct lyd_meta *)item->node)->name; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4216 | break; |
| 4217 | } |
| 4218 | |
| 4219 | if (mod && name) { |
| 4220 | switch (set->format) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 4221 | case LYD_SCHEMA: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4222 | rc = asprintf(&str, "%s:%s", lys_prefix_find_module(set->local_mod, mod), name); |
| 4223 | break; |
| 4224 | case LYD_JSON: |
| 4225 | rc = asprintf(&str, "%s:%s", mod->name, name); |
| 4226 | break; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 4227 | case LYD_XML: |
Michal Vasko | 9409ef6 | 2019-09-12 11:47:17 +0200 | [diff] [blame] | 4228 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4229 | } |
| 4230 | LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM); |
| 4231 | set_fill_string(set, str, strlen(str)); |
| 4232 | free(str); |
| 4233 | } else { |
| 4234 | set_fill_string(set, "", 0); |
| 4235 | } |
| 4236 | |
| 4237 | return LY_SUCCESS; |
| 4238 | } |
| 4239 | |
| 4240 | /** |
| 4241 | * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING |
| 4242 | * with the namespace of the node from the argument or the context. |
| 4243 | * |
| 4244 | * @param[in] args Array of arguments. |
| 4245 | * @param[in] arg_count Count of elements in @p args. |
| 4246 | * @param[in,out] set Context and result set at the same time. |
| 4247 | * @param[in] options XPath options. |
| 4248 | * @return LY_ERR (LY_EINVAL for wrong arguments on schema) |
| 4249 | */ |
| 4250 | static LY_ERR |
| 4251 | xpath_namespace_uri(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options) |
| 4252 | { |
| 4253 | struct lyxp_set_node *item; |
| 4254 | struct lys_module *mod; |
| 4255 | /* suppress unused variable warning */ |
| 4256 | (void)options; |
| 4257 | |
| 4258 | if (options & LYXP_SCNODE_ALL) { |
| 4259 | set_scnode_clear_ctx(set); |
| 4260 | return LY_SUCCESS; |
| 4261 | } |
| 4262 | |
| 4263 | if (arg_count) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4264 | if (args[0]->type != LYXP_SET_NODE_SET) { |
| 4265 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), |
| 4266 | "namespace-uri(node-set?)"); |
| 4267 | return LY_EVALID; |
| 4268 | } else if (!args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4269 | set_fill_string(set, "", 0); |
| 4270 | return LY_SUCCESS; |
| 4271 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4272 | |
| 4273 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4274 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4275 | |
| 4276 | item = &args[0]->val.nodes[0]; |
| 4277 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4278 | if (set->type != LYXP_SET_NODE_SET) { |
| 4279 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "namespace-uri(node-set?)"); |
| 4280 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4281 | } else if (!set->used) { |
| 4282 | set_fill_string(set, "", 0); |
| 4283 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4284 | } |
| 4285 | |
| 4286 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4287 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4288 | |
| 4289 | item = &set->val.nodes[0]; |
| 4290 | } |
| 4291 | |
| 4292 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4293 | case LYXP_NODE_NONE: |
| 4294 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4295 | case LYXP_NODE_ROOT: |
| 4296 | case LYXP_NODE_ROOT_CONFIG: |
| 4297 | case LYXP_NODE_TEXT: |
| 4298 | set_fill_string(set, "", 0); |
| 4299 | break; |
| 4300 | case LYXP_NODE_ELEM: |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4301 | case LYXP_NODE_META: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4302 | if (item->type == LYXP_NODE_ELEM) { |
| 4303 | mod = item->node->schema->module; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4304 | } else { /* LYXP_NODE_META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4305 | /* annotations */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4306 | mod = ((struct lyd_meta *)item->node)->annotation->module; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4307 | } |
| 4308 | |
| 4309 | set_fill_string(set, mod->ns, strlen(mod->ns)); |
| 4310 | break; |
| 4311 | } |
| 4312 | |
| 4313 | return LY_SUCCESS; |
| 4314 | } |
| 4315 | |
| 4316 | /** |
| 4317 | * @brief Execute the XPath node() function (node type). Returns LYXP_SET_NODE_SET |
| 4318 | * with only nodes from the context. In practice it either leaves the context |
| 4319 | * as it is or returns an empty node set. |
| 4320 | * |
| 4321 | * @param[in] args Array of arguments. |
| 4322 | * @param[in] arg_count Count of elements in @p args. |
| 4323 | * @param[in,out] set Context and result set at the same time. |
| 4324 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4325 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4326 | */ |
| 4327 | static LY_ERR |
| 4328 | xpath_node(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 4329 | { |
| 4330 | if (options & LYXP_SCNODE_ALL) { |
| 4331 | set_scnode_clear_ctx(set); |
| 4332 | return LY_SUCCESS; |
| 4333 | } |
| 4334 | |
| 4335 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4336 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4337 | } |
| 4338 | return LY_SUCCESS; |
| 4339 | } |
| 4340 | |
| 4341 | /** |
| 4342 | * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING |
| 4343 | * with normalized value (no leading, trailing, double white spaces) of the node |
| 4344 | * from the argument or the context. |
| 4345 | * |
| 4346 | * @param[in] args Array of arguments. |
| 4347 | * @param[in] arg_count Count of elements in @p args. |
| 4348 | * @param[in,out] set Context and result set at the same time. |
| 4349 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4350 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4351 | */ |
| 4352 | static LY_ERR |
| 4353 | xpath_normalize_space(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options) |
| 4354 | { |
| 4355 | uint16_t i, new_used; |
| 4356 | char *new; |
| 4357 | int have_spaces = 0, space_before = 0; |
| 4358 | struct lysc_node_leaf *sleaf; |
| 4359 | LY_ERR rc = LY_SUCCESS; |
| 4360 | |
| 4361 | if (options & LYXP_SCNODE_ALL) { |
| 4362 | if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4363 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4364 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4365 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4366 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4367 | } |
| 4368 | } |
| 4369 | set_scnode_clear_ctx(set); |
| 4370 | return rc; |
| 4371 | } |
| 4372 | |
| 4373 | if (arg_count) { |
| 4374 | set_fill_set(set, args[0]); |
| 4375 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4376 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4377 | LY_CHECK_RET(rc); |
| 4378 | |
| 4379 | /* is there any normalization necessary? */ |
| 4380 | for (i = 0; set->val.str[i]; ++i) { |
| 4381 | if (is_xmlws(set->val.str[i])) { |
| 4382 | if ((i == 0) || space_before || (!set->val.str[i + 1])) { |
| 4383 | have_spaces = 1; |
| 4384 | break; |
| 4385 | } |
| 4386 | space_before = 1; |
| 4387 | } else { |
| 4388 | space_before = 0; |
| 4389 | } |
| 4390 | } |
| 4391 | |
| 4392 | /* yep, there is */ |
| 4393 | if (have_spaces) { |
| 4394 | /* it's enough, at least one character will go, makes space for ending '\0' */ |
| 4395 | new = malloc(strlen(set->val.str) * sizeof(char)); |
| 4396 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 4397 | new_used = 0; |
| 4398 | |
| 4399 | space_before = 0; |
| 4400 | for (i = 0; set->val.str[i]; ++i) { |
| 4401 | if (is_xmlws(set->val.str[i])) { |
| 4402 | if ((i == 0) || space_before) { |
| 4403 | space_before = 1; |
| 4404 | continue; |
| 4405 | } else { |
| 4406 | space_before = 1; |
| 4407 | } |
| 4408 | } else { |
| 4409 | space_before = 0; |
| 4410 | } |
| 4411 | |
| 4412 | new[new_used] = (space_before ? ' ' : set->val.str[i]); |
| 4413 | ++new_used; |
| 4414 | } |
| 4415 | |
| 4416 | /* at worst there is one trailing space now */ |
| 4417 | if (new_used && is_xmlws(new[new_used - 1])) { |
| 4418 | --new_used; |
| 4419 | } |
| 4420 | |
| 4421 | new = ly_realloc(new, (new_used + 1) * sizeof(char)); |
| 4422 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 4423 | new[new_used] = '\0'; |
| 4424 | |
| 4425 | free(set->val.str); |
| 4426 | set->val.str = new; |
| 4427 | } |
| 4428 | |
| 4429 | return LY_SUCCESS; |
| 4430 | } |
| 4431 | |
| 4432 | /** |
| 4433 | * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN |
| 4434 | * with the argument converted to boolean and logically inverted. |
| 4435 | * |
| 4436 | * @param[in] args Array of arguments. |
| 4437 | * @param[in] arg_count Count of elements in @p args. |
| 4438 | * @param[in,out] set Context and result set at the same time. |
| 4439 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4440 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4441 | */ |
| 4442 | static LY_ERR |
| 4443 | xpath_not(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 4444 | { |
| 4445 | if (options & LYXP_SCNODE_ALL) { |
| 4446 | set_scnode_clear_ctx(set); |
| 4447 | return LY_SUCCESS; |
| 4448 | } |
| 4449 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4450 | lyxp_set_cast(args[0], LYXP_SET_BOOLEAN); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4451 | if (args[0]->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4452 | set_fill_boolean(set, 0); |
| 4453 | } else { |
| 4454 | set_fill_boolean(set, 1); |
| 4455 | } |
| 4456 | |
| 4457 | return LY_SUCCESS; |
| 4458 | } |
| 4459 | |
| 4460 | /** |
| 4461 | * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER |
| 4462 | * with the number representation of either the argument or the context. |
| 4463 | * |
| 4464 | * @param[in] args Array of arguments. |
| 4465 | * @param[in] arg_count Count of elements in @p args. |
| 4466 | * @param[in,out] set Context and result set at the same time. |
| 4467 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4468 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4469 | */ |
| 4470 | static LY_ERR |
| 4471 | xpath_number(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options) |
| 4472 | { |
| 4473 | LY_ERR rc; |
| 4474 | |
| 4475 | if (options & LYXP_SCNODE_ALL) { |
| 4476 | set_scnode_clear_ctx(set); |
| 4477 | return LY_SUCCESS; |
| 4478 | } |
| 4479 | |
| 4480 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4481 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4482 | LY_CHECK_RET(rc); |
| 4483 | set_fill_set(set, args[0]); |
| 4484 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4485 | rc = lyxp_set_cast(set, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4486 | LY_CHECK_RET(rc); |
| 4487 | } |
| 4488 | |
| 4489 | return LY_SUCCESS; |
| 4490 | } |
| 4491 | |
| 4492 | /** |
| 4493 | * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER |
| 4494 | * with the context position. |
| 4495 | * |
| 4496 | * @param[in] args Array of arguments. |
| 4497 | * @param[in] arg_count Count of elements in @p args. |
| 4498 | * @param[in,out] set Context and result set at the same time. |
| 4499 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4500 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4501 | */ |
| 4502 | static LY_ERR |
| 4503 | xpath_position(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 4504 | { |
| 4505 | if (options & LYXP_SCNODE_ALL) { |
| 4506 | set_scnode_clear_ctx(set); |
| 4507 | return LY_SUCCESS; |
| 4508 | } |
| 4509 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4510 | if (set->type != LYXP_SET_NODE_SET) { |
| 4511 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "position()"); |
| 4512 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4513 | } else if (!set->used) { |
| 4514 | set_fill_number(set, 0); |
| 4515 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4516 | } |
| 4517 | |
| 4518 | set_fill_number(set, set->ctx_pos); |
| 4519 | |
| 4520 | /* UNUSED in 'Release' build type */ |
| 4521 | (void)options; |
| 4522 | return LY_SUCCESS; |
| 4523 | } |
| 4524 | |
| 4525 | /** |
| 4526 | * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN |
| 4527 | * depending on whether the second argument regex matches the first argument string. For details refer to |
| 4528 | * YANG 1.1 RFC section 10.2.1. |
| 4529 | * |
| 4530 | * @param[in] args Array of arguments. |
| 4531 | * @param[in] arg_count Count of elements in @p args. |
| 4532 | * @param[in,out] set Context and result set at the same time. |
| 4533 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4534 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4535 | */ |
| 4536 | static LY_ERR |
| 4537 | xpath_re_match(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 4538 | { |
| 4539 | struct lysc_pattern **patterns = NULL, **pattern; |
| 4540 | struct lysc_node_leaf *sleaf; |
| 4541 | char *path; |
| 4542 | LY_ERR rc = LY_SUCCESS; |
| 4543 | struct ly_err_item *err; |
| 4544 | |
| 4545 | if (options & LYXP_SCNODE_ALL) { |
| 4546 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4547 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4548 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4549 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4550 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4551 | } |
| 4552 | } |
| 4553 | |
| 4554 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4555 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4556 | LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4557 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4558 | LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4559 | } |
| 4560 | } |
| 4561 | set_scnode_clear_ctx(set); |
| 4562 | return rc; |
| 4563 | } |
| 4564 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4565 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4566 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4567 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4568 | LY_CHECK_RET(rc); |
| 4569 | |
| 4570 | LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM); |
| 4571 | *pattern = malloc(sizeof **pattern); |
| 4572 | path = lyd_path(set->ctx_node, LYD_PATH_LOG, NULL, 0); |
| 4573 | rc = lys_compile_type_pattern_check(set->ctx, path, args[1]->val.str, &(*pattern)->code); |
| 4574 | free(path); |
| 4575 | if (rc != LY_SUCCESS) { |
| 4576 | LY_ARRAY_FREE(patterns); |
| 4577 | return rc; |
| 4578 | } |
| 4579 | |
| 4580 | rc = ly_type_validate_patterns(patterns, args[0]->val.str, strlen(args[0]->val.str), &err); |
| 4581 | pcre2_code_free((*pattern)->code); |
| 4582 | free(*pattern); |
| 4583 | LY_ARRAY_FREE(patterns); |
| 4584 | if (rc && (rc != LY_EVALID)) { |
| 4585 | ly_err_print(err); |
| 4586 | ly_err_free(err); |
| 4587 | return rc; |
| 4588 | } |
| 4589 | |
| 4590 | if (rc == LY_EVALID) { |
| 4591 | ly_err_free(err); |
| 4592 | set_fill_boolean(set, 0); |
| 4593 | } else { |
| 4594 | set_fill_boolean(set, 1); |
| 4595 | } |
| 4596 | |
| 4597 | return LY_SUCCESS; |
| 4598 | } |
| 4599 | |
| 4600 | /** |
| 4601 | * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER |
| 4602 | * with the rounded first argument. For details refer to |
| 4603 | * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round. |
| 4604 | * |
| 4605 | * @param[in] args Array of arguments. |
| 4606 | * @param[in] arg_count Count of elements in @p args. |
| 4607 | * @param[in,out] set Context and result set at the same time. |
| 4608 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4609 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4610 | */ |
| 4611 | static LY_ERR |
| 4612 | xpath_round(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 4613 | { |
| 4614 | struct lysc_node_leaf *sleaf; |
| 4615 | LY_ERR rc = LY_SUCCESS; |
| 4616 | |
| 4617 | if (options & LYXP_SCNODE_ALL) { |
| 4618 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4619 | LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4620 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4621 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4622 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) { |
| 4623 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4624 | } |
| 4625 | set_scnode_clear_ctx(set); |
| 4626 | return rc; |
| 4627 | } |
| 4628 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4629 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4630 | LY_CHECK_RET(rc); |
| 4631 | |
| 4632 | /* cover only the cases where floor can't be used */ |
| 4633 | if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) { |
| 4634 | set_fill_number(set, -0.0f); |
| 4635 | } else { |
| 4636 | args[0]->val.num += 0.5; |
| 4637 | rc = xpath_floor(args, 1, args[0], options); |
| 4638 | LY_CHECK_RET(rc); |
| 4639 | set_fill_number(set, args[0]->val.num); |
| 4640 | } |
| 4641 | |
| 4642 | return LY_SUCCESS; |
| 4643 | } |
| 4644 | |
| 4645 | /** |
| 4646 | * @brief Execute the XPath starts-with(string, string) function. |
| 4647 | * Returns LYXP_SET_BOOLEAN whether the second argument is |
| 4648 | * the prefix of the first or not. |
| 4649 | * |
| 4650 | * @param[in] args Array of arguments. |
| 4651 | * @param[in] arg_count Count of elements in @p args. |
| 4652 | * @param[in,out] set Context and result set at the same time. |
| 4653 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4654 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4655 | */ |
| 4656 | static LY_ERR |
| 4657 | xpath_starts_with(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 4658 | { |
| 4659 | struct lysc_node_leaf *sleaf; |
| 4660 | LY_ERR rc = LY_SUCCESS; |
| 4661 | |
| 4662 | if (options & LYXP_SCNODE_ALL) { |
| 4663 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4664 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4665 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4666 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4667 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4668 | } |
| 4669 | } |
| 4670 | |
| 4671 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4672 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4673 | LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4674 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4675 | LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4676 | } |
| 4677 | } |
| 4678 | set_scnode_clear_ctx(set); |
| 4679 | return rc; |
| 4680 | } |
| 4681 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4682 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4683 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4684 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4685 | LY_CHECK_RET(rc); |
| 4686 | |
| 4687 | if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) { |
| 4688 | set_fill_boolean(set, 0); |
| 4689 | } else { |
| 4690 | set_fill_boolean(set, 1); |
| 4691 | } |
| 4692 | |
| 4693 | return LY_SUCCESS; |
| 4694 | } |
| 4695 | |
| 4696 | /** |
| 4697 | * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING |
| 4698 | * with the string representation of either the argument or the context. |
| 4699 | * |
| 4700 | * @param[in] args Array of arguments. |
| 4701 | * @param[in] arg_count Count of elements in @p args. |
| 4702 | * @param[in,out] set Context and result set at the same time. |
| 4703 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4704 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4705 | */ |
| 4706 | static LY_ERR |
| 4707 | xpath_string(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options) |
| 4708 | { |
| 4709 | LY_ERR rc; |
| 4710 | |
| 4711 | if (options & LYXP_SCNODE_ALL) { |
| 4712 | set_scnode_clear_ctx(set); |
| 4713 | return LY_SUCCESS; |
| 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_set(set, args[0]); |
| 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 | } |
| 4724 | |
| 4725 | return LY_SUCCESS; |
| 4726 | } |
| 4727 | |
| 4728 | /** |
| 4729 | * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER |
| 4730 | * with the length of the string in either the argument or the context. |
| 4731 | * |
| 4732 | * @param[in] args Array of arguments. |
| 4733 | * @param[in] arg_count Count of elements in @p args. |
| 4734 | * @param[in,out] set Context and result set at the same time. |
| 4735 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4736 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4737 | */ |
| 4738 | static LY_ERR |
| 4739 | xpath_string_length(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options) |
| 4740 | { |
| 4741 | struct lysc_node_leaf *sleaf; |
| 4742 | LY_ERR rc = LY_SUCCESS; |
| 4743 | |
| 4744 | if (options & LYXP_SCNODE_ALL) { |
| 4745 | if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4746 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4747 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4748 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4749 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4750 | } |
| 4751 | } |
| 4752 | if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) { |
| 4753 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4754 | LOGWRN(set->ctx, "Argument #0 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4755 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4756 | LOGWRN(set->ctx, "Argument #0 of %s is node \"%s\", not of string-type.", __func__, sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4757 | } |
| 4758 | } |
| 4759 | set_scnode_clear_ctx(set); |
| 4760 | return rc; |
| 4761 | } |
| 4762 | |
| 4763 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4764 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4765 | LY_CHECK_RET(rc); |
| 4766 | set_fill_number(set, strlen(args[0]->val.str)); |
| 4767 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4768 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4769 | LY_CHECK_RET(rc); |
| 4770 | set_fill_number(set, strlen(set->val.str)); |
| 4771 | } |
| 4772 | |
| 4773 | return LY_SUCCESS; |
| 4774 | } |
| 4775 | |
| 4776 | /** |
| 4777 | * @brief Execute the XPath substring(string, number, number?) function. |
| 4778 | * Returns LYXP_SET_STRING substring of the first argument starting |
| 4779 | * on the second argument index ending on the third argument index, |
| 4780 | * indexed from 1. For exact definition refer to |
| 4781 | * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring. |
| 4782 | * |
| 4783 | * @param[in] args Array of arguments. |
| 4784 | * @param[in] arg_count Count of elements in @p args. |
| 4785 | * @param[in,out] set Context and result set at the same time. |
| 4786 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4787 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4788 | */ |
| 4789 | static LY_ERR |
| 4790 | xpath_substring(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options) |
| 4791 | { |
| 4792 | int start, len; |
| 4793 | uint16_t str_start, str_len, pos; |
| 4794 | struct lysc_node_leaf *sleaf; |
| 4795 | LY_ERR rc = LY_SUCCESS; |
| 4796 | |
| 4797 | if (options & LYXP_SCNODE_ALL) { |
| 4798 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4799 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4800 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4801 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4802 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4803 | } |
| 4804 | } |
| 4805 | |
| 4806 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4807 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4808 | LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4809 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 4810 | LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4811 | } |
| 4812 | } |
| 4813 | |
| 4814 | if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET) |
| 4815 | && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) { |
| 4816 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4817 | LOGWRN(set->ctx, "Argument #3 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4818 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 4819 | LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4820 | } |
| 4821 | } |
| 4822 | set_scnode_clear_ctx(set); |
| 4823 | return rc; |
| 4824 | } |
| 4825 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4826 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4827 | LY_CHECK_RET(rc); |
| 4828 | |
| 4829 | /* start */ |
| 4830 | if (xpath_round(&args[1], 1, args[1], options)) { |
| 4831 | return -1; |
| 4832 | } |
| 4833 | if (isfinite(args[1]->val.num)) { |
| 4834 | start = args[1]->val.num - 1; |
| 4835 | } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) { |
| 4836 | start = INT_MIN; |
| 4837 | } else { |
| 4838 | start = INT_MAX; |
| 4839 | } |
| 4840 | |
| 4841 | /* len */ |
| 4842 | if (arg_count == 3) { |
| 4843 | rc = xpath_round(&args[2], 1, args[2], options); |
| 4844 | LY_CHECK_RET(rc); |
| 4845 | if (isfinite(args[2]->val.num)) { |
| 4846 | len = args[2]->val.num; |
| 4847 | } else if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) { |
| 4848 | len = 0; |
| 4849 | } else { |
| 4850 | len = INT_MAX; |
| 4851 | } |
| 4852 | } else { |
| 4853 | len = INT_MAX; |
| 4854 | } |
| 4855 | |
| 4856 | /* find matching character positions */ |
| 4857 | str_start = 0; |
| 4858 | str_len = 0; |
| 4859 | for (pos = 0; args[0]->val.str[pos]; ++pos) { |
| 4860 | if (pos < start) { |
| 4861 | ++str_start; |
| 4862 | } else if (pos < start + len) { |
| 4863 | ++str_len; |
| 4864 | } else { |
| 4865 | break; |
| 4866 | } |
| 4867 | } |
| 4868 | |
| 4869 | set_fill_string(set, args[0]->val.str + str_start, str_len); |
| 4870 | return LY_SUCCESS; |
| 4871 | } |
| 4872 | |
| 4873 | /** |
| 4874 | * @brief Execute the XPath substring-after(string, string) function. |
| 4875 | * Returns LYXP_SET_STRING with the string succeeding the occurance |
| 4876 | * of the second argument in the first or an empty string. |
| 4877 | * |
| 4878 | * @param[in] args Array of arguments. |
| 4879 | * @param[in] arg_count Count of elements in @p args. |
| 4880 | * @param[in,out] set Context and result set at the same time. |
| 4881 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4882 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4883 | */ |
| 4884 | static LY_ERR |
| 4885 | xpath_substring_after(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 4886 | { |
| 4887 | char *ptr; |
| 4888 | struct lysc_node_leaf *sleaf; |
| 4889 | LY_ERR rc = LY_SUCCESS; |
| 4890 | |
| 4891 | if (options & LYXP_SCNODE_ALL) { |
| 4892 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4893 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4894 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4895 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4896 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4897 | } |
| 4898 | } |
| 4899 | |
| 4900 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4901 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4902 | LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4903 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4904 | LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4905 | } |
| 4906 | } |
| 4907 | set_scnode_clear_ctx(set); |
| 4908 | return rc; |
| 4909 | } |
| 4910 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4911 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4912 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4913 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4914 | LY_CHECK_RET(rc); |
| 4915 | |
| 4916 | ptr = strstr(args[0]->val.str, args[1]->val.str); |
| 4917 | if (ptr) { |
| 4918 | set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str))); |
| 4919 | } else { |
| 4920 | set_fill_string(set, "", 0); |
| 4921 | } |
| 4922 | |
| 4923 | return LY_SUCCESS; |
| 4924 | } |
| 4925 | |
| 4926 | /** |
| 4927 | * @brief Execute the XPath substring-before(string, string) function. |
| 4928 | * Returns LYXP_SET_STRING with the string preceding the occurance |
| 4929 | * of the second argument in the first or an empty string. |
| 4930 | * |
| 4931 | * @param[in] args Array of arguments. |
| 4932 | * @param[in] arg_count Count of elements in @p args. |
| 4933 | * @param[in,out] set Context and result set at the same time. |
| 4934 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4935 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4936 | */ |
| 4937 | static LY_ERR |
| 4938 | xpath_substring_before(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 4939 | { |
| 4940 | char *ptr; |
| 4941 | struct lysc_node_leaf *sleaf; |
| 4942 | LY_ERR rc = LY_SUCCESS; |
| 4943 | |
| 4944 | if (options & LYXP_SCNODE_ALL) { |
| 4945 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4946 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4947 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4948 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4949 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4950 | } |
| 4951 | } |
| 4952 | |
| 4953 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4954 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4955 | LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4956 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4957 | LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4958 | } |
| 4959 | } |
| 4960 | set_scnode_clear_ctx(set); |
| 4961 | return rc; |
| 4962 | } |
| 4963 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4964 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4965 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4966 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4967 | LY_CHECK_RET(rc); |
| 4968 | |
| 4969 | ptr = strstr(args[0]->val.str, args[1]->val.str); |
| 4970 | if (ptr) { |
| 4971 | set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str); |
| 4972 | } else { |
| 4973 | set_fill_string(set, "", 0); |
| 4974 | } |
| 4975 | |
| 4976 | return LY_SUCCESS; |
| 4977 | } |
| 4978 | |
| 4979 | /** |
| 4980 | * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER |
| 4981 | * with the sum of all the nodes in the context. |
| 4982 | * |
| 4983 | * @param[in] args Array of arguments. |
| 4984 | * @param[in] arg_count Count of elements in @p args. |
| 4985 | * @param[in,out] set Context and result set at the same time. |
| 4986 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4987 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4988 | */ |
| 4989 | static LY_ERR |
| 4990 | xpath_sum(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 4991 | { |
| 4992 | long double num; |
| 4993 | char *str; |
| 4994 | uint16_t i; |
| 4995 | struct lyxp_set set_item; |
| 4996 | struct lysc_node_leaf *sleaf; |
| 4997 | LY_ERR rc = LY_SUCCESS; |
| 4998 | |
| 4999 | if (options & LYXP_SCNODE_ALL) { |
| 5000 | if (args[0]->type == LYXP_SET_SCNODE_SET) { |
| 5001 | for (i = 0; i < args[0]->used; ++i) { |
| 5002 | if (args[0]->val.scnodes[i].in_ctx == 1) { |
| 5003 | sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode; |
| 5004 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5005 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, |
| 5006 | lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5007 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 5008 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5009 | } |
| 5010 | } |
| 5011 | } |
| 5012 | } |
| 5013 | set_scnode_clear_ctx(set); |
| 5014 | return rc; |
| 5015 | } |
| 5016 | |
| 5017 | set_fill_number(set, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5018 | |
| 5019 | if (args[0]->type != LYXP_SET_NODE_SET) { |
| 5020 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "sum(node-set)"); |
| 5021 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5022 | } else if (!args[0]->used) { |
| 5023 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5024 | } |
| 5025 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5026 | set_init(&set_item, set); |
| 5027 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5028 | set_item.type = LYXP_SET_NODE_SET; |
| 5029 | set_item.val.nodes = malloc(sizeof *set_item.val.nodes); |
| 5030 | LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM); |
| 5031 | |
| 5032 | set_item.used = 1; |
| 5033 | set_item.size = 1; |
| 5034 | |
| 5035 | for (i = 0; i < args[0]->used; ++i) { |
| 5036 | set_item.val.nodes[0] = args[0]->val.nodes[i]; |
| 5037 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5038 | rc = cast_node_set_to_string(&set_item, &str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5039 | LY_CHECK_RET(rc); |
| 5040 | num = cast_string_to_number(str); |
| 5041 | free(str); |
| 5042 | set->val.num += num; |
| 5043 | } |
| 5044 | |
| 5045 | free(set_item.val.nodes); |
| 5046 | |
| 5047 | return LY_SUCCESS; |
| 5048 | } |
| 5049 | |
| 5050 | /** |
| 5051 | * @brief Execute the XPath text() function (node type). Returns LYXP_SET_NODE_SET |
| 5052 | * with the text content of the nodes in the context. |
| 5053 | * |
| 5054 | * @param[in] args Array of arguments. |
| 5055 | * @param[in] arg_count Count of elements in @p args. |
| 5056 | * @param[in,out] set Context and result set at the same time. |
| 5057 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5058 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5059 | */ |
| 5060 | static LY_ERR |
| 5061 | xpath_text(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 5062 | { |
| 5063 | uint32_t i; |
| 5064 | |
| 5065 | if (options & LYXP_SCNODE_ALL) { |
| 5066 | set_scnode_clear_ctx(set); |
| 5067 | return LY_SUCCESS; |
| 5068 | } |
| 5069 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5070 | if (set->type != LYXP_SET_NODE_SET) { |
| 5071 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "text()"); |
| 5072 | return LY_EVALID; |
| 5073 | } |
| 5074 | |
| 5075 | for (i = 0; i < set->used;) { |
| 5076 | switch (set->val.nodes[i].type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 5077 | case LYXP_NODE_NONE: |
| 5078 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5079 | case LYXP_NODE_ELEM: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5080 | if (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 5081 | set->val.nodes[i].type = LYXP_NODE_TEXT; |
| 5082 | ++i; |
| 5083 | break; |
| 5084 | } |
| 5085 | /* fall through */ |
| 5086 | case LYXP_NODE_ROOT: |
| 5087 | case LYXP_NODE_ROOT_CONFIG: |
| 5088 | case LYXP_NODE_TEXT: |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5089 | case LYXP_NODE_META: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5090 | set_remove_node(set, i); |
| 5091 | break; |
| 5092 | } |
| 5093 | } |
| 5094 | |
| 5095 | return LY_SUCCESS; |
| 5096 | } |
| 5097 | |
| 5098 | /** |
| 5099 | * @brief Execute the XPath translate(string, string, string) function. |
| 5100 | * Returns LYXP_SET_STRING with the first argument with the characters |
| 5101 | * from the second argument replaced by those on the corresponding |
| 5102 | * positions in the third argument. |
| 5103 | * |
| 5104 | * @param[in] args Array of arguments. |
| 5105 | * @param[in] arg_count Count of elements in @p args. |
| 5106 | * @param[in,out] set Context and result set at the same time. |
| 5107 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5108 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5109 | */ |
| 5110 | static LY_ERR |
| 5111 | xpath_translate(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 5112 | { |
| 5113 | uint16_t i, j, new_used; |
| 5114 | char *new; |
| 5115 | int found, have_removed; |
| 5116 | struct lysc_node_leaf *sleaf; |
| 5117 | LY_ERR rc = LY_SUCCESS; |
| 5118 | |
| 5119 | if (options & LYXP_SCNODE_ALL) { |
| 5120 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 5121 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5122 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5123 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5124 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5125 | } |
| 5126 | } |
| 5127 | |
| 5128 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 5129 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5130 | LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5131 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5132 | LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5133 | } |
| 5134 | } |
| 5135 | |
| 5136 | if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) { |
| 5137 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5138 | LOGWRN(set->ctx, "Argument #3 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5139 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5140 | LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of string-type.", __func__, sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5141 | } |
| 5142 | } |
| 5143 | set_scnode_clear_ctx(set); |
| 5144 | return rc; |
| 5145 | } |
| 5146 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5147 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5148 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5149 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5150 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5151 | rc = lyxp_set_cast(args[2], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5152 | LY_CHECK_RET(rc); |
| 5153 | |
| 5154 | new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char)); |
| 5155 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 5156 | new_used = 0; |
| 5157 | |
| 5158 | have_removed = 0; |
| 5159 | for (i = 0; args[0]->val.str[i]; ++i) { |
| 5160 | found = 0; |
| 5161 | |
| 5162 | for (j = 0; args[1]->val.str[j]; ++j) { |
| 5163 | if (args[0]->val.str[i] == args[1]->val.str[j]) { |
| 5164 | /* removing this char */ |
| 5165 | if (j >= strlen(args[2]->val.str)) { |
| 5166 | have_removed = 1; |
| 5167 | found = 1; |
| 5168 | break; |
| 5169 | } |
| 5170 | /* replacing this char */ |
| 5171 | new[new_used] = args[2]->val.str[j]; |
| 5172 | ++new_used; |
| 5173 | found = 1; |
| 5174 | break; |
| 5175 | } |
| 5176 | } |
| 5177 | |
| 5178 | /* copying this char */ |
| 5179 | if (!found) { |
| 5180 | new[new_used] = args[0]->val.str[i]; |
| 5181 | ++new_used; |
| 5182 | } |
| 5183 | } |
| 5184 | |
| 5185 | if (have_removed) { |
| 5186 | new = ly_realloc(new, (new_used + 1) * sizeof(char)); |
| 5187 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 5188 | } |
| 5189 | new[new_used] = '\0'; |
| 5190 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5191 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5192 | set->type = LYXP_SET_STRING; |
| 5193 | set->val.str = new; |
| 5194 | |
| 5195 | return LY_SUCCESS; |
| 5196 | } |
| 5197 | |
| 5198 | /** |
| 5199 | * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN |
| 5200 | * with true value. |
| 5201 | * |
| 5202 | * @param[in] args Array of arguments. |
| 5203 | * @param[in] arg_count Count of elements in @p args. |
| 5204 | * @param[in,out] set Context and result set at the same time. |
| 5205 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5206 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5207 | */ |
| 5208 | static LY_ERR |
| 5209 | xpath_true(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options) |
| 5210 | { |
| 5211 | if (options & LYXP_SCNODE_ALL) { |
| 5212 | set_scnode_clear_ctx(set); |
| 5213 | return LY_SUCCESS; |
| 5214 | } |
| 5215 | |
| 5216 | set_fill_boolean(set, 1); |
| 5217 | return LY_SUCCESS; |
| 5218 | } |
| 5219 | |
| 5220 | /* |
| 5221 | * moveto functions |
| 5222 | * |
| 5223 | * They and only they actually change the context (set). |
| 5224 | */ |
| 5225 | |
| 5226 | /** |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5227 | * @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] | 5228 | * |
Michal Vasko | 2104e9f | 2020-03-06 08:23:25 +0100 | [diff] [blame] | 5229 | * XPath @p set is expected to be a (sc)node set! |
| 5230 | * |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5231 | * @param[in,out] qname Qualified node name. If includes prefix, it is skipped. |
| 5232 | * @param[in,out] qname_len Length of @p qname, is updated accordingly. |
| 5233 | * @param[in] set Set with XPath context. |
| 5234 | * @param[out] moveto_mod Expected module of a matching node. |
| 5235 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5236 | */ |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5237 | static LY_ERR |
| 5238 | 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] | 5239 | { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5240 | const struct lys_module *mod; |
| 5241 | const char *ptr; |
| 5242 | int pref_len; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5243 | char *str; |
| 5244 | |
Michal Vasko | 2104e9f | 2020-03-06 08:23:25 +0100 | [diff] [blame] | 5245 | assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET)); |
| 5246 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5247 | if ((ptr = ly_strnchr(*qname, ':', *qname_len))) { |
| 5248 | /* specific module */ |
| 5249 | pref_len = ptr - *qname; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5250 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5251 | switch (set->format) { |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 5252 | case LYD_SCHEMA: |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5253 | /* schema, search all local module imports */ |
| 5254 | mod = lys_module_find_prefix(set->local_mod, *qname, pref_len); |
| 5255 | break; |
| 5256 | case LYD_JSON: |
| 5257 | /* JSON data, search in context */ |
| 5258 | str = strndup(*qname, pref_len); |
Michal Vasko | 97e76fd | 2020-05-27 15:22:01 +0200 | [diff] [blame] | 5259 | mod = ly_ctx_get_module_implemented(set->ctx, str); |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5260 | free(str); |
| 5261 | break; |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 5262 | case LYD_XML: |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5263 | LOGINT_RET(set->ctx); |
| 5264 | } |
| 5265 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5266 | /* check for errors and non-implemented modules, as they are not valid */ |
Juraj Vijtiuk | d75faa6 | 2019-11-26 14:10:10 +0100 | [diff] [blame] | 5267 | if (!mod || !mod->implemented) { |
Michal Vasko | 2104e9f | 2020-03-06 08:23:25 +0100 | [diff] [blame] | 5268 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 5269 | LOGVAL(set->ctx, LY_VLOG_LYSC, set->ctx_scnode, LY_VCODE_XP_INMOD, pref_len, *qname); |
| 5270 | } else { |
| 5271 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INMOD, pref_len, *qname); |
| 5272 | } |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5273 | return LY_EVALID; |
| 5274 | } |
Juraj Vijtiuk | d75faa6 | 2019-11-26 14:10:10 +0100 | [diff] [blame] | 5275 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5276 | *qname += pref_len + 1; |
| 5277 | *qname_len -= pref_len + 1; |
| 5278 | } else if (((*qname)[0] == '*') && (*qname_len == 1)) { |
| 5279 | /* all modules - special case */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5280 | mod = NULL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5281 | } else if (set->type == LYXP_SET_SCNODE_SET) { |
| 5282 | /* current node module */ |
| 5283 | mod = set->ctx_scnode->module; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5284 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5285 | /* current node module */ |
| 5286 | mod = set->ctx_node->schema->module; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5287 | } |
| 5288 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5289 | *moveto_mod = mod; |
| 5290 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5291 | } |
| 5292 | |
| 5293 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5294 | * @brief Move context @p set to the root. Handles absolute path. |
| 5295 | * Result is LYXP_SET_NODE_SET. |
| 5296 | * |
| 5297 | * @param[in,out] set Set to use. |
| 5298 | * @param[in] options Xpath options. |
| 5299 | */ |
| 5300 | static void |
| 5301 | moveto_root(struct lyxp_set *set, int options) |
| 5302 | { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5303 | if (!set) { |
| 5304 | return; |
| 5305 | } |
| 5306 | |
| 5307 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5308 | set_scnode_clear_ctx(set); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 5309 | lyxp_set_scnode_insert_node(set, NULL, set->root_type); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5310 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5311 | set->type = LYXP_SET_NODE_SET; |
| 5312 | set->used = 0; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5313 | set_insert_node(set, NULL, 0, set->root_type, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5314 | } |
| 5315 | } |
| 5316 | |
| 5317 | /** |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 5318 | * @brief Check whether a node has some unresolved "when". |
| 5319 | * |
| 5320 | * @param[in] node Node to check. |
| 5321 | * @return LY_ERR value (LY_EINCOMPLETE if there are some unresolved "when") |
| 5322 | */ |
| 5323 | static LY_ERR |
| 5324 | moveto_when_check(const struct lyd_node *node) |
| 5325 | { |
| 5326 | const struct lysc_node *schema; |
| 5327 | |
| 5328 | if (!node) { |
| 5329 | return LY_SUCCESS; |
| 5330 | } |
| 5331 | |
| 5332 | schema = node->schema; |
| 5333 | do { |
| 5334 | if (schema->when && !(node->flags & LYD_WHEN_TRUE)) { |
| 5335 | return LY_EINCOMPLETE; |
| 5336 | } |
| 5337 | schema = schema->parent; |
| 5338 | } while (schema && (schema->nodetype & (LYS_CASE | LYS_CHOICE))); |
| 5339 | |
| 5340 | return LY_SUCCESS; |
| 5341 | } |
| 5342 | |
| 5343 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5344 | * @brief Check @p node as a part of NameTest processing. |
| 5345 | * |
| 5346 | * @param[in] node Node to check. |
| 5347 | * @param[in] root_type XPath root node type. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5348 | * @param[in] node_name Node name in the dictionary to move to, NULL for any node. |
| 5349 | * @param[in] moveto_mod Expected module of the node, NULL for any. |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5350 | * @return LY_ERR (LY_ENOT if node does not match, LY_EINCOMPLETE on unresolved when, |
| 5351 | * LY_EINVAL if netither node nor any children match) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5352 | */ |
| 5353 | static LY_ERR |
| 5354 | moveto_node_check(const struct lyd_node *node, enum lyxp_node_type root_type, const char *node_name, |
| 5355 | const struct lys_module *moveto_mod) |
| 5356 | { |
| 5357 | /* module check */ |
| 5358 | if (moveto_mod && (node->schema->module != moveto_mod)) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5359 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5360 | } |
| 5361 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5362 | /* context check */ |
| 5363 | 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] | 5364 | return LY_EINVAL; |
| 5365 | } |
| 5366 | |
| 5367 | /* name check */ |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5368 | if (node_name && strcmp(node_name, "*") && (node->schema->name != node_name)) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5369 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5370 | } |
| 5371 | |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 5372 | /* when check */ |
| 5373 | if (moveto_when_check(node)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5374 | return LY_EINCOMPLETE; |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 5375 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5376 | |
| 5377 | /* match */ |
| 5378 | return LY_SUCCESS; |
| 5379 | } |
| 5380 | |
| 5381 | /** |
| 5382 | * @brief Check @p node as a part of schema NameTest processing. |
| 5383 | * |
| 5384 | * @param[in] node Schema node to check. |
| 5385 | * @param[in] root_type XPath root node type. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5386 | * @param[in] node_name Node name in the dictionary to move to, NULL for any nodes. |
| 5387 | * @param[in] moveto_mod Expected module of the node, NULL for any. |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5388 | * @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] | 5389 | */ |
| 5390 | static LY_ERR |
| 5391 | 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] | 5392 | const struct lys_module *moveto_mod) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5393 | { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5394 | /* module check */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5395 | if (moveto_mod && (node->module != moveto_mod)) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5396 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5397 | } |
| 5398 | |
| 5399 | /* context check */ |
| 5400 | if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) { |
| 5401 | return LY_EINVAL; |
| 5402 | } |
| 5403 | |
| 5404 | /* name check */ |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5405 | if (node_name && strcmp(node_name, "*") && (node->name != node_name)) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5406 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5407 | } |
| 5408 | |
| 5409 | /* match */ |
| 5410 | return LY_SUCCESS; |
| 5411 | } |
| 5412 | |
| 5413 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5414 | * @brief Move context @p set to a node. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY). Context position aware. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5415 | * |
| 5416 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5417 | * @param[in] mod Matching node module, NULL for any. |
| 5418 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5419 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 5420 | */ |
| 5421 | static LY_ERR |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5422 | moveto_node(struct lyxp_set *set, const struct lys_module *mod, const char *ncname) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5423 | { |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 5424 | uint32_t i; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5425 | int replaced; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5426 | const struct lyd_node *siblings, *sub; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5427 | LY_ERR rc; |
| 5428 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5429 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5430 | return LY_SUCCESS; |
| 5431 | } |
| 5432 | |
| 5433 | if (set->type != LYXP_SET_NODE_SET) { |
| 5434 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 5435 | return LY_EVALID; |
| 5436 | } |
| 5437 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5438 | for (i = 0; i < set->used; ) { |
| 5439 | replaced = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5440 | siblings = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5441 | |
| 5442 | 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] | 5443 | assert(!set->val.nodes[i].node); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5444 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5445 | /* search in all the trees */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5446 | siblings = set->tree; |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5447 | } else if (!(set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5448 | /* search in children */ |
| 5449 | siblings = lyd_node_children(set->val.nodes[i].node); |
| 5450 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5451 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5452 | for (sub = siblings; sub; sub = sub->next) { |
| 5453 | rc = moveto_node_check(sub, set->root_type, ncname, mod); |
| 5454 | if (rc == LY_SUCCESS) { |
| 5455 | if (!replaced) { |
| 5456 | set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i); |
| 5457 | replaced = 1; |
| 5458 | } else { |
| 5459 | set_insert_node(set, sub, 0, LYXP_NODE_ELEM, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5460 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5461 | ++i; |
| 5462 | } else if (rc == LY_EINCOMPLETE) { |
| 5463 | return rc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5464 | } |
| 5465 | } |
| 5466 | |
| 5467 | if (!replaced) { |
| 5468 | /* no match */ |
| 5469 | set_remove_node(set, i); |
| 5470 | } |
| 5471 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5472 | |
| 5473 | return LY_SUCCESS; |
| 5474 | } |
| 5475 | |
| 5476 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5477 | * @brief Move context @p set to a node using hashes. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY). |
| 5478 | * Context position aware. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5479 | * |
| 5480 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5481 | * @param[in] scnode Matching node schema. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5482 | * @param[in] predicates If @p scnode is ::LYS_LIST or ::LYS_LEAFLIST, the predicates specifying a single instance. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5483 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 5484 | */ |
| 5485 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5486 | moveto_node_hash(struct lyxp_set *set, const struct lysc_node *scnode, const struct ly_path_predicate *predicates) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5487 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5488 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5489 | uint32_t i; |
| 5490 | int replaced; |
| 5491 | const struct lyd_node *siblings; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5492 | struct lyd_node *sub, *inst = NULL; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5493 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5494 | assert(scnode && (!(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) || predicates)); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5495 | |
| 5496 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5497 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5498 | } |
| 5499 | |
| 5500 | if (set->type != LYXP_SET_NODE_SET) { |
| 5501 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5502 | ret = LY_EVALID; |
| 5503 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5504 | } |
| 5505 | |
| 5506 | /* context check for all the nodes since we have the schema node */ |
| 5507 | if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) { |
| 5508 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5509 | goto cleanup; |
| 5510 | } |
| 5511 | |
| 5512 | /* create specific data instance if needed */ |
| 5513 | if (scnode->nodetype == LYS_LIST) { |
| 5514 | LY_CHECK_GOTO(ret = lyd_create_list(scnode, predicates, &inst), cleanup); |
| 5515 | } else if (scnode->nodetype == LYS_LEAFLIST) { |
| 5516 | LY_CHECK_GOTO(ret = lyd_create_term2(scnode, &predicates[0].value, &inst), cleanup); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5517 | } |
| 5518 | |
| 5519 | for (i = 0; i < set->used; ) { |
| 5520 | replaced = 0; |
| 5521 | siblings = NULL; |
| 5522 | |
| 5523 | if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) { |
| 5524 | assert(!set->val.nodes[i].node); |
| 5525 | |
| 5526 | /* search in all the trees */ |
| 5527 | siblings = set->tree; |
| 5528 | } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
| 5529 | /* search in children */ |
| 5530 | siblings = lyd_node_children(set->val.nodes[i].node); |
| 5531 | } |
| 5532 | |
| 5533 | /* find the node using hashes */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5534 | if (inst) { |
| 5535 | lyd_find_sibling_first(siblings, inst, &sub); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5536 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5537 | lyd_find_sibling_val(siblings, scnode, NULL, 0, &sub); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5538 | } |
| 5539 | |
| 5540 | /* when check */ |
| 5541 | if (sub && moveto_when_check(sub)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5542 | ret = LY_EINCOMPLETE; |
| 5543 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5544 | } |
| 5545 | |
| 5546 | if (sub) { |
| 5547 | /* pos filled later */ |
| 5548 | if (!replaced) { |
| 5549 | set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i); |
| 5550 | replaced = 1; |
| 5551 | } else { |
| 5552 | set_insert_node(set, sub, 0, LYXP_NODE_ELEM, i); |
| 5553 | } |
| 5554 | ++i; |
| 5555 | } |
| 5556 | |
| 5557 | if (!replaced) { |
| 5558 | /* no match */ |
| 5559 | set_remove_node(set, i); |
| 5560 | } |
| 5561 | } |
| 5562 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5563 | cleanup: |
| 5564 | lyd_free_tree(inst); |
| 5565 | return ret; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5566 | } |
| 5567 | |
| 5568 | /** |
| 5569 | * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY). |
| 5570 | * |
| 5571 | * @param[in,out] set Set to use. |
| 5572 | * @param[in] mod Matching node module, NULL for any. |
| 5573 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5574 | * @param[in] options XPath options. |
| 5575 | * @return LY_ERR |
| 5576 | */ |
| 5577 | static LY_ERR |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5578 | moveto_scnode(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, int options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5579 | { |
Michal Vasko | cafad9d | 2019-11-07 15:20:03 +0100 | [diff] [blame] | 5580 | int i, orig_used, idx, temp_ctx = 0, getnext_opts; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5581 | uint32_t mod_idx; |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 5582 | const struct lysc_node *iter, *start_parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5583 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5584 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5585 | return LY_SUCCESS; |
| 5586 | } |
| 5587 | |
| 5588 | if (set->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 5589 | LOGVAL(set->ctx, LY_VLOG_LYSC, set->ctx_scnode, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5590 | return LY_EVALID; |
| 5591 | } |
| 5592 | |
Michal Vasko | cafad9d | 2019-11-07 15:20:03 +0100 | [diff] [blame] | 5593 | /* getnext opts */ |
| 5594 | getnext_opts = LYS_GETNEXT_NOSTATECHECK; |
| 5595 | if (options & LYXP_SCNODE_OUTPUT) { |
| 5596 | getnext_opts |= LYS_GETNEXT_OUTPUT; |
| 5597 | } |
| 5598 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5599 | orig_used = set->used; |
| 5600 | for (i = 0; i < orig_used; ++i) { |
| 5601 | if (set->val.scnodes[i].in_ctx != 1) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5602 | if (set->val.scnodes[i].in_ctx != -2) { |
| 5603 | continue; |
| 5604 | } |
| 5605 | |
| 5606 | /* remember context node */ |
| 5607 | set->val.scnodes[i].in_ctx = -1; |
Michal Vasko | ec4df48 | 2019-12-16 10:02:18 +0100 | [diff] [blame] | 5608 | } else { |
| 5609 | set->val.scnodes[i].in_ctx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5610 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5611 | |
| 5612 | start_parent = set->val.scnodes[i].scnode; |
| 5613 | |
| 5614 | if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5615 | /* it can actually be in any module, it's all <running>, but we know it's mod (if set), |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5616 | * so use it directly (root node itself is useless in this case) */ |
| 5617 | mod_idx = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5618 | while (mod || (mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) { |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 5619 | iter = NULL; |
Michal Vasko | 509de4d | 2019-12-10 14:51:30 +0100 | [diff] [blame] | 5620 | /* module may not be implemented */ |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 5621 | while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) { |
| 5622 | if (!moveto_scnode_check(iter, set->root_type, ncname, mod)) { |
| 5623 | idx = lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5624 | /* we need to prevent these nodes from being considered in this moveto */ |
| 5625 | if ((idx < orig_used) && (idx > i)) { |
| 5626 | set->val.scnodes[idx].in_ctx = 2; |
| 5627 | temp_ctx = 1; |
| 5628 | } |
| 5629 | } |
| 5630 | } |
| 5631 | |
| 5632 | if (!mod_idx) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5633 | /* mod was specified, we are not going through the whole context */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5634 | break; |
| 5635 | } |
| 5636 | /* next iteration */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5637 | mod = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5638 | } |
| 5639 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 5640 | } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) { |
| 5641 | iter = NULL; |
| 5642 | while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) { |
| 5643 | if (!moveto_scnode_check(iter, set->root_type, ncname, (mod ? mod : set->local_mod))) { |
| 5644 | idx = lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5645 | if ((idx < orig_used) && (idx > i)) { |
| 5646 | set->val.scnodes[idx].in_ctx = 2; |
| 5647 | temp_ctx = 1; |
| 5648 | } |
| 5649 | } |
| 5650 | } |
| 5651 | } |
| 5652 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5653 | |
| 5654 | /* correct temporary in_ctx values */ |
| 5655 | if (temp_ctx) { |
| 5656 | for (i = 0; i < orig_used; ++i) { |
| 5657 | if (set->val.scnodes[i].in_ctx == 2) { |
| 5658 | set->val.scnodes[i].in_ctx = 1; |
| 5659 | } |
| 5660 | } |
| 5661 | } |
| 5662 | |
| 5663 | return LY_SUCCESS; |
| 5664 | } |
| 5665 | |
| 5666 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5667 | * @brief Move context @p set to a node and all its descendants. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY). |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5668 | * Context position aware. |
| 5669 | * |
| 5670 | * @param[in] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5671 | * @param[in] mod Matching node module, NULL for any. |
| 5672 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5673 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 5674 | */ |
| 5675 | static LY_ERR |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5676 | moveto_node_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5677 | { |
| 5678 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5679 | const struct lyd_node *next, *elem, *start; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5680 | struct lyxp_set ret_set; |
| 5681 | LY_ERR rc; |
| 5682 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5683 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5684 | return LY_SUCCESS; |
| 5685 | } |
| 5686 | |
| 5687 | if (set->type != LYXP_SET_NODE_SET) { |
| 5688 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 5689 | return LY_EVALID; |
| 5690 | } |
| 5691 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5692 | /* replace the original nodes (and throws away all text and meta nodes, root is replaced by a child) */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5693 | rc = moveto_node(set, NULL, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5694 | LY_CHECK_RET(rc); |
| 5695 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5696 | /* 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] | 5697 | set_init(&ret_set, set); |
| 5698 | for (i = 0; i < set->used; ++i) { |
| 5699 | |
| 5700 | /* TREE DFS */ |
| 5701 | start = set->val.nodes[i].node; |
| 5702 | for (elem = next = start; elem; elem = next) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5703 | rc = moveto_node_check(elem, set->root_type, ncname, mod); |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5704 | if (!rc) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5705 | /* add matching node into result set */ |
| 5706 | set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used); |
| 5707 | if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) { |
| 5708 | /* the node is a duplicate, we'll process it later in the set */ |
| 5709 | goto skip_children; |
| 5710 | } |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5711 | } else if (rc == LY_EINCOMPLETE) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5712 | return rc; |
| 5713 | } else if (rc == LY_EINVAL) { |
| 5714 | goto skip_children; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5715 | } |
| 5716 | |
| 5717 | /* TREE DFS NEXT ELEM */ |
| 5718 | /* select element for the next run - children first */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5719 | next = lyd_node_children(elem); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5720 | if (!next) { |
| 5721 | skip_children: |
| 5722 | /* no children, so try siblings, but only if it's not the start, |
| 5723 | * that is considered to be the root and it's siblings are not traversed */ |
| 5724 | if (elem != start) { |
| 5725 | next = elem->next; |
| 5726 | } else { |
| 5727 | break; |
| 5728 | } |
| 5729 | } |
| 5730 | while (!next) { |
| 5731 | /* no siblings, go back through the parents */ |
| 5732 | if ((struct lyd_node *)elem->parent == start) { |
| 5733 | /* we are done, no next element to process */ |
| 5734 | break; |
| 5735 | } |
| 5736 | /* parent is already processed, go to its sibling */ |
| 5737 | elem = (struct lyd_node *)elem->parent; |
| 5738 | next = elem->next; |
| 5739 | } |
| 5740 | } |
| 5741 | } |
| 5742 | |
| 5743 | /* make the temporary set the current one */ |
| 5744 | ret_set.ctx_pos = set->ctx_pos; |
| 5745 | ret_set.ctx_size = set->ctx_size; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5746 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5747 | memcpy(set, &ret_set, sizeof *set); |
| 5748 | |
| 5749 | return LY_SUCCESS; |
| 5750 | } |
| 5751 | |
| 5752 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5753 | * @brief Move context @p set to a schema node and all its descendants. Result is LYXP_SET_NODE_SET. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5754 | * |
| 5755 | * @param[in] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5756 | * @param[in] mod Matching node module, NULL for any. |
| 5757 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5758 | * @param[in] options XPath options. |
| 5759 | * @return LY_ERR |
| 5760 | */ |
| 5761 | static LY_ERR |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5762 | moveto_scnode_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, int options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5763 | { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5764 | int i, orig_used, idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5765 | const struct lysc_node *next, *elem, *start; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5766 | LY_ERR rc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5767 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5768 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5769 | return LY_SUCCESS; |
| 5770 | } |
| 5771 | |
| 5772 | if (set->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 5773 | LOGVAL(set->ctx, LY_VLOG_LYSC, set->ctx_scnode, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5774 | return LY_EVALID; |
| 5775 | } |
| 5776 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5777 | orig_used = set->used; |
| 5778 | for (i = 0; i < orig_used; ++i) { |
| 5779 | if (set->val.scnodes[i].in_ctx != 1) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5780 | if (set->val.scnodes[i].in_ctx != -2) { |
| 5781 | continue; |
| 5782 | } |
| 5783 | |
| 5784 | /* remember context node */ |
| 5785 | set->val.scnodes[i].in_ctx = -1; |
Michal Vasko | ec4df48 | 2019-12-16 10:02:18 +0100 | [diff] [blame] | 5786 | } else { |
| 5787 | set->val.scnodes[i].in_ctx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5788 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5789 | |
| 5790 | /* TREE DFS */ |
| 5791 | start = set->val.scnodes[i].scnode; |
| 5792 | for (elem = next = start; elem; elem = next) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5793 | if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) { |
| 5794 | /* schema-only nodes, skip root */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5795 | goto next_iter; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5796 | } |
| 5797 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5798 | rc = moveto_scnode_check(elem, set->root_type, ncname, mod); |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5799 | if (!rc) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 5800 | 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] | 5801 | set->val.scnodes[idx].in_ctx = 1; |
| 5802 | if (idx > i) { |
| 5803 | /* we will process it later in the set */ |
| 5804 | goto skip_children; |
| 5805 | } |
| 5806 | } else { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 5807 | lyxp_set_scnode_insert_node(set, elem, LYXP_NODE_ELEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5808 | } |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5809 | } else if (rc == LY_EINVAL) { |
| 5810 | goto skip_children; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5811 | } |
| 5812 | |
| 5813 | next_iter: |
| 5814 | /* TREE DFS NEXT ELEM */ |
| 5815 | /* select element for the next run - children first */ |
| 5816 | next = lysc_node_children(elem, options & LYXP_SCNODE_OUTPUT ? LYS_CONFIG_R : LYS_CONFIG_W); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5817 | if (!next) { |
| 5818 | skip_children: |
| 5819 | /* no children, so try siblings, but only if it's not the start, |
| 5820 | * that is considered to be the root and it's siblings are not traversed */ |
| 5821 | if (elem != start) { |
| 5822 | next = elem->next; |
| 5823 | } else { |
| 5824 | break; |
| 5825 | } |
| 5826 | } |
| 5827 | while (!next) { |
| 5828 | /* no siblings, go back through the parents */ |
| 5829 | if (elem->parent == start) { |
| 5830 | /* we are done, no next element to process */ |
| 5831 | break; |
| 5832 | } |
| 5833 | /* parent is already processed, go to its sibling */ |
| 5834 | elem = elem->parent; |
| 5835 | next = elem->next; |
| 5836 | } |
| 5837 | } |
| 5838 | } |
| 5839 | |
| 5840 | return LY_SUCCESS; |
| 5841 | } |
| 5842 | |
| 5843 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5844 | * @brief Move context @p set to an attribute. Result is LYXP_SET_NODE_SET. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5845 | * Indirectly context position aware. |
| 5846 | * |
| 5847 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5848 | * @param[in] mod Matching metadata module, NULL for any. |
| 5849 | * @param[in] ncname Matching metadata name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5850 | * @return LY_ERR |
| 5851 | */ |
| 5852 | static LY_ERR |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5853 | moveto_attr(struct lyxp_set *set, const struct lys_module *mod, const char *ncname) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5854 | { |
| 5855 | uint32_t i; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5856 | int replaced; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5857 | struct lyd_meta *sub; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5858 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5859 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5860 | return LY_SUCCESS; |
| 5861 | } |
| 5862 | |
| 5863 | if (set->type != LYXP_SET_NODE_SET) { |
| 5864 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 5865 | return LY_EVALID; |
| 5866 | } |
| 5867 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5868 | for (i = 0; i < set->used; ) { |
| 5869 | replaced = 0; |
| 5870 | |
| 5871 | /* only attributes of an elem (not dummy) can be in the result, skip all the rest; |
| 5872 | * our attributes are always qualified */ |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5873 | if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5874 | for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5875 | |
| 5876 | /* check "namespace" */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5877 | if (mod && (sub->annotation->module != mod)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5878 | continue; |
| 5879 | } |
| 5880 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5881 | if (!ncname || (sub->name == ncname)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5882 | /* match */ |
| 5883 | if (!replaced) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5884 | set->val.meta[i].meta = sub; |
| 5885 | set->val.meta[i].type = LYXP_NODE_META; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5886 | /* pos does not change */ |
| 5887 | replaced = 1; |
| 5888 | } else { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5889 | set_insert_node(set, (struct lyd_node *)sub, set->val.nodes[i].pos, LYXP_NODE_META, i + 1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5890 | } |
| 5891 | ++i; |
| 5892 | } |
| 5893 | } |
| 5894 | } |
| 5895 | |
| 5896 | if (!replaced) { |
| 5897 | /* no match */ |
| 5898 | set_remove_node(set, i); |
| 5899 | } |
| 5900 | } |
| 5901 | |
| 5902 | return LY_SUCCESS; |
| 5903 | } |
| 5904 | |
| 5905 | /** |
| 5906 | * @brief Move context @p set1 to union with @p set2. @p set2 is emptied afterwards. |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5907 | * Result is LYXP_SET_NODE_SET. Context position aware. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5908 | * |
| 5909 | * @param[in,out] set1 Set to use for the result. |
| 5910 | * @param[in] set2 Set that is copied to @p set1. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5911 | * @return LY_ERR |
| 5912 | */ |
| 5913 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5914 | moveto_union(struct lyxp_set *set1, struct lyxp_set *set2) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5915 | { |
| 5916 | LY_ERR rc; |
| 5917 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5918 | if ((set1->type != LYXP_SET_NODE_SET) || (set2->type != LYXP_SET_NODE_SET)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5919 | LOGVAL(set1->ctx, LY_VLOG_LYD, set1->ctx_node, LY_VCODE_XP_INOP_2, "union", print_set_type(set1), print_set_type(set2)); |
| 5920 | return LY_EVALID; |
| 5921 | } |
| 5922 | |
| 5923 | /* set2 is empty or both set1 and set2 */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5924 | if (!set2->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5925 | return LY_SUCCESS; |
| 5926 | } |
| 5927 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5928 | if (!set1->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5929 | memcpy(set1, set2, sizeof *set1); |
| 5930 | /* dynamic memory belongs to set1 now, do not free */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5931 | memset(set2, 0, sizeof *set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5932 | return LY_SUCCESS; |
| 5933 | } |
| 5934 | |
| 5935 | /* we assume sets are sorted */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5936 | assert(!set_sort(set1) && !set_sort(set2)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5937 | |
| 5938 | /* sort, remove duplicates */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5939 | rc = set_sorted_merge(set1, set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5940 | LY_CHECK_RET(rc); |
| 5941 | |
| 5942 | /* final set must be sorted */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5943 | assert(!set_sort(set1)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5944 | |
| 5945 | return LY_SUCCESS; |
| 5946 | } |
| 5947 | |
| 5948 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5949 | * @brief Move context @p set to an attribute in any of the descendants. Result is LYXP_SET_NODE_SET. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5950 | * Context position aware. |
| 5951 | * |
| 5952 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5953 | * @param[in] mod Matching metadata module, NULL for any. |
| 5954 | * @param[in] ncname Matching metadata name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5955 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 5956 | */ |
| 5957 | static int |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5958 | moveto_attr_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5959 | { |
| 5960 | uint32_t i; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5961 | int replaced; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5962 | struct lyd_meta *sub; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5963 | struct lyxp_set *set_all_desc = NULL; |
| 5964 | LY_ERR rc; |
| 5965 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5966 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5967 | return LY_SUCCESS; |
| 5968 | } |
| 5969 | |
| 5970 | if (set->type != LYXP_SET_NODE_SET) { |
| 5971 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 5972 | return LY_EVALID; |
| 5973 | } |
| 5974 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5975 | /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory, |
| 5976 | * but it likely won't be used much, so it's a waste of time */ |
| 5977 | /* copy the context */ |
| 5978 | set_all_desc = set_copy(set); |
| 5979 | /* get all descendant nodes (the original context nodes are removed) */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5980 | rc = moveto_node_alldesc(set_all_desc, NULL, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5981 | if (rc != LY_SUCCESS) { |
| 5982 | lyxp_set_free(set_all_desc); |
| 5983 | return rc; |
| 5984 | } |
| 5985 | /* prepend the original context nodes */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5986 | rc = moveto_union(set, set_all_desc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5987 | if (rc != LY_SUCCESS) { |
| 5988 | lyxp_set_free(set_all_desc); |
| 5989 | return rc; |
| 5990 | } |
| 5991 | lyxp_set_free(set_all_desc); |
| 5992 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5993 | for (i = 0; i < set->used; ) { |
| 5994 | replaced = 0; |
| 5995 | |
| 5996 | /* only attributes of an elem can be in the result, skip all the rest, |
| 5997 | * we have all attributes qualified in lyd tree */ |
| 5998 | if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5999 | for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6000 | /* check "namespace" */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6001 | if (mod && (sub->annotation->module != mod)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6002 | continue; |
| 6003 | } |
| 6004 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6005 | if (!ncname || (sub->name == ncname)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6006 | /* match */ |
| 6007 | if (!replaced) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6008 | set->val.meta[i].meta = sub; |
| 6009 | set->val.meta[i].type = LYXP_NODE_META; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6010 | /* pos does not change */ |
| 6011 | replaced = 1; |
| 6012 | } else { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6013 | set_insert_node(set, (struct lyd_node *)sub, set->val.meta[i].pos, LYXP_NODE_META, i + 1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6014 | } |
| 6015 | ++i; |
| 6016 | } |
| 6017 | } |
| 6018 | } |
| 6019 | |
| 6020 | if (!replaced) { |
| 6021 | /* no match */ |
| 6022 | set_remove_node(set, i); |
| 6023 | } |
| 6024 | } |
| 6025 | |
| 6026 | return LY_SUCCESS; |
| 6027 | } |
| 6028 | |
| 6029 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6030 | * @brief Move context @p set to self and al chilren, recursively. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET. |
| 6031 | * Context position aware. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6032 | * |
| 6033 | * @param[in] parent Current parent. |
| 6034 | * @param[in] parent_pos Position of @p parent. |
| 6035 | * @param[in] parent_type Node type of @p parent. |
| 6036 | * @param[in,out] to_set Set to use. |
| 6037 | * @param[in] dup_check_set Set for checking duplicities. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6038 | * @param[in] options XPath options. |
| 6039 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6040 | */ |
| 6041 | static LY_ERR |
| 6042 | 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] | 6043 | 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] | 6044 | { |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6045 | const struct lyd_node *iter, *first; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6046 | LY_ERR rc; |
| 6047 | |
| 6048 | switch (parent_type) { |
| 6049 | case LYXP_NODE_ROOT: |
| 6050 | case LYXP_NODE_ROOT_CONFIG: |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6051 | assert(!parent); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6052 | |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6053 | /* add all top-level nodes as elements */ |
| 6054 | first = to_set->tree; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6055 | break; |
| 6056 | case LYXP_NODE_ELEM: |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6057 | /* add just the text node of this term element node */ |
| 6058 | if (parent->schema->nodetype & (LYD_NODE_TERM | LYD_NODE_ANY)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6059 | if (!set_dup_node_check(dup_check_set, parent, LYXP_NODE_TEXT, -1)) { |
| 6060 | set_insert_node(to_set, parent, parent_pos, LYXP_NODE_TEXT, to_set->used); |
| 6061 | } |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6062 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6063 | } |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6064 | |
| 6065 | /* add all the children of this node */ |
| 6066 | first = lyd_node_children(parent); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6067 | break; |
| 6068 | default: |
| 6069 | LOGINT_RET(parent->schema->module->ctx); |
| 6070 | } |
| 6071 | |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6072 | /* add all top-level nodes as elements */ |
| 6073 | LY_LIST_FOR(first, iter) { |
| 6074 | /* context check */ |
| 6075 | if ((parent_type == LYXP_NODE_ROOT_CONFIG) && (iter->schema->flags & LYS_CONFIG_R)) { |
| 6076 | continue; |
| 6077 | } |
| 6078 | |
| 6079 | /* when check */ |
| 6080 | if (moveto_when_check(iter)) { |
| 6081 | return LY_EINCOMPLETE; |
| 6082 | } |
| 6083 | |
| 6084 | if (!set_dup_node_check(dup_check_set, iter, LYXP_NODE_ELEM, -1)) { |
| 6085 | set_insert_node(to_set, iter, 0, LYXP_NODE_ELEM, to_set->used); |
| 6086 | |
| 6087 | /* also add all the children of this node, recursively */ |
| 6088 | rc = moveto_self_add_children_r(iter, 0, LYXP_NODE_ELEM, to_set, dup_check_set, options); |
| 6089 | LY_CHECK_RET(rc); |
| 6090 | } |
| 6091 | } |
| 6092 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6093 | return LY_SUCCESS; |
| 6094 | } |
| 6095 | |
| 6096 | /** |
| 6097 | * @brief Move context @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET |
| 6098 | * (or LYXP_SET_EMPTY). Context position aware. |
| 6099 | * |
| 6100 | * @param[in,out] set Set to use. |
| 6101 | * @param[in] all_desc Whether to go to all descendants ('//') or not ('/'). |
| 6102 | * @param[in] options XPath options. |
| 6103 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6104 | */ |
| 6105 | static LY_ERR |
| 6106 | moveto_self(struct lyxp_set *set, int all_desc, int options) |
| 6107 | { |
| 6108 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6109 | struct lyxp_set ret_set; |
| 6110 | LY_ERR rc; |
| 6111 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6112 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6113 | return LY_SUCCESS; |
| 6114 | } |
| 6115 | |
| 6116 | if (set->type != LYXP_SET_NODE_SET) { |
| 6117 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 6118 | return LY_EVALID; |
| 6119 | } |
| 6120 | |
| 6121 | /* nothing to do */ |
| 6122 | if (!all_desc) { |
| 6123 | return LY_SUCCESS; |
| 6124 | } |
| 6125 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6126 | /* add all the children, they get added recursively */ |
| 6127 | set_init(&ret_set, set); |
| 6128 | for (i = 0; i < set->used; ++i) { |
| 6129 | /* copy the current node to tmp */ |
| 6130 | set_insert_node(&ret_set, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, ret_set.used); |
| 6131 | |
| 6132 | /* do not touch attributes and text nodes */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6133 | if ((set->val.nodes[i].type == LYXP_NODE_TEXT) || (set->val.nodes[i].type == LYXP_NODE_META)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6134 | continue; |
| 6135 | } |
| 6136 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6137 | /* add all the children */ |
| 6138 | 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] | 6139 | set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6140 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6141 | lyxp_set_free_content(&ret_set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6142 | return rc; |
| 6143 | } |
| 6144 | } |
| 6145 | |
| 6146 | /* use the temporary set as the current one */ |
| 6147 | ret_set.ctx_pos = set->ctx_pos; |
| 6148 | ret_set.ctx_size = set->ctx_size; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6149 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6150 | memcpy(set, &ret_set, sizeof *set); |
| 6151 | |
| 6152 | return LY_SUCCESS; |
| 6153 | } |
| 6154 | |
| 6155 | /** |
| 6156 | * @brief Move context schema @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_SCNODE_SET |
| 6157 | * (or LYXP_SET_EMPTY). |
| 6158 | * |
| 6159 | * @param[in,out] set Set to use. |
| 6160 | * @param[in] all_desc Whether to go to all descendants ('//') or not ('/'). |
| 6161 | * @param[in] options XPath options. |
| 6162 | * @return LY_ERR |
| 6163 | */ |
| 6164 | static LY_ERR |
| 6165 | moveto_scnode_self(struct lyxp_set *set, int all_desc, int options) |
| 6166 | { |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6167 | int getnext_opts; |
| 6168 | uint32_t i, mod_idx; |
| 6169 | const struct lysc_node *iter, *start_parent; |
| 6170 | const struct lys_module *mod; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6171 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6172 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6173 | return LY_SUCCESS; |
| 6174 | } |
| 6175 | |
| 6176 | if (set->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 6177 | LOGVAL(set->ctx, LY_VLOG_LYSC, set->ctx_scnode, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6178 | return LY_EVALID; |
| 6179 | } |
| 6180 | |
| 6181 | /* nothing to do */ |
| 6182 | if (!all_desc) { |
| 6183 | return LY_SUCCESS; |
| 6184 | } |
| 6185 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6186 | /* getnext opts */ |
| 6187 | getnext_opts = LYS_GETNEXT_NOSTATECHECK; |
| 6188 | if (options & LYXP_SCNODE_OUTPUT) { |
| 6189 | getnext_opts |= LYS_GETNEXT_OUTPUT; |
| 6190 | } |
| 6191 | |
| 6192 | /* add all the children, recursively as they are being added into the same set */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6193 | for (i = 0; i < set->used; ++i) { |
| 6194 | if (set->val.scnodes[i].in_ctx != 1) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6195 | if (set->val.scnodes[i].in_ctx != -2) { |
| 6196 | continue; |
| 6197 | } |
| 6198 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6199 | /* remember context node */ |
| 6200 | set->val.scnodes[i].in_ctx = -1; |
| 6201 | } else { |
| 6202 | set->val.scnodes[i].in_ctx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6203 | } |
| 6204 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6205 | start_parent = set->val.scnodes[i].scnode; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6206 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6207 | if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) { |
| 6208 | /* it can actually be in any module, it's all <running> */ |
| 6209 | mod_idx = 0; |
| 6210 | while ((mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) { |
| 6211 | iter = NULL; |
| 6212 | /* module may not be implemented */ |
| 6213 | while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) { |
| 6214 | /* context check */ |
| 6215 | if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) { |
| 6216 | continue; |
| 6217 | } |
| 6218 | |
| 6219 | lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM); |
| 6220 | /* throw away the insert index, we want to consider that node again, recursively */ |
| 6221 | } |
| 6222 | } |
| 6223 | |
| 6224 | } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) { |
| 6225 | iter = NULL; |
| 6226 | while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6227 | /* context check */ |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6228 | if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6229 | continue; |
| 6230 | } |
| 6231 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6232 | lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6233 | } |
| 6234 | } |
| 6235 | } |
| 6236 | |
| 6237 | return LY_SUCCESS; |
| 6238 | } |
| 6239 | |
| 6240 | /** |
| 6241 | * @brief Move context @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_NODE_SET |
| 6242 | * (or LYXP_SET_EMPTY). Context position aware. |
| 6243 | * |
| 6244 | * @param[in] set Set to use. |
| 6245 | * @param[in] all_desc Whether to go to all descendants ('//') or not ('/'). |
| 6246 | * @param[in] options XPath options. |
| 6247 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6248 | */ |
| 6249 | static LY_ERR |
| 6250 | moveto_parent(struct lyxp_set *set, int all_desc, int options) |
| 6251 | { |
| 6252 | LY_ERR rc; |
| 6253 | uint32_t i; |
| 6254 | struct lyd_node *node, *new_node; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6255 | enum lyxp_node_type new_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6256 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6257 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6258 | return LY_SUCCESS; |
| 6259 | } |
| 6260 | |
| 6261 | if (set->type != LYXP_SET_NODE_SET) { |
| 6262 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 6263 | return LY_EVALID; |
| 6264 | } |
| 6265 | |
| 6266 | if (all_desc) { |
| 6267 | /* <path>//.. == <path>//./.. */ |
| 6268 | rc = moveto_self(set, 1, options); |
| 6269 | LY_CHECK_RET(rc); |
| 6270 | } |
| 6271 | |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 6272 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6273 | node = set->val.nodes[i].node; |
| 6274 | |
| 6275 | if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
| 6276 | new_node = (struct lyd_node *)node->parent; |
| 6277 | } else if (set->val.nodes[i].type == LYXP_NODE_TEXT) { |
| 6278 | new_node = node; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6279 | } else if (set->val.nodes[i].type == LYXP_NODE_META) { |
| 6280 | new_node = set->val.meta[i].meta->parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6281 | if (!new_node) { |
| 6282 | LOGINT_RET(set->ctx); |
| 6283 | } |
| 6284 | } else { |
| 6285 | /* root does not have a parent */ |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6286 | set_remove_node_none(set, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6287 | continue; |
| 6288 | } |
| 6289 | |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 6290 | /* when check */ |
| 6291 | if (moveto_when_check(new_node)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6292 | return LY_EINCOMPLETE; |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 6293 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6294 | |
| 6295 | /* node already there can also be the root */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6296 | if (!new_node) { |
| 6297 | new_type = set->root_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6298 | |
| 6299 | /* node has a standard parent (it can equal the root, it's not the root yet since they are fake) */ |
| 6300 | } else { |
| 6301 | new_type = LYXP_NODE_ELEM; |
| 6302 | } |
| 6303 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6304 | if (set_dup_node_check(set, new_node, new_type, -1)) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6305 | set_remove_node_none(set, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6306 | } else { |
| 6307 | set_replace_node(set, new_node, 0, new_type, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6308 | } |
| 6309 | } |
| 6310 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6311 | set_remove_nodes_none(set); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6312 | assert(!set_sort(set) && !set_sorted_dup_node_clean(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6313 | |
| 6314 | return LY_SUCCESS; |
| 6315 | } |
| 6316 | |
| 6317 | /** |
| 6318 | * @brief Move context schema @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_SCNODE_SET |
| 6319 | * (or LYXP_SET_EMPTY). |
| 6320 | * |
| 6321 | * @param[in] set Set to use. |
| 6322 | * @param[in] all_desc Whether to go to all descendants ('//') or not ('/'). |
| 6323 | * @param[in] options XPath options. |
| 6324 | * @return LY_ERR |
| 6325 | */ |
| 6326 | static LY_ERR |
| 6327 | moveto_scnode_parent(struct lyxp_set *set, int all_desc, int options) |
| 6328 | { |
| 6329 | int idx, i, orig_used, temp_ctx = 0; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6330 | const struct lysc_node *node, *new_node; |
| 6331 | enum lyxp_node_type new_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6332 | LY_ERR rc; |
| 6333 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6334 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6335 | return LY_SUCCESS; |
| 6336 | } |
| 6337 | |
| 6338 | if (set->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 6339 | LOGVAL(set->ctx, LY_VLOG_LYSC, set->ctx_scnode, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6340 | return LY_EVALID; |
| 6341 | } |
| 6342 | |
| 6343 | if (all_desc) { |
| 6344 | /* <path>//.. == <path>//./.. */ |
| 6345 | rc = moveto_scnode_self(set, 1, options); |
| 6346 | LY_CHECK_RET(rc); |
| 6347 | } |
| 6348 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6349 | orig_used = set->used; |
| 6350 | for (i = 0; i < orig_used; ++i) { |
| 6351 | if (set->val.scnodes[i].in_ctx != 1) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6352 | if (set->val.scnodes[i].in_ctx != -2) { |
| 6353 | continue; |
| 6354 | } |
| 6355 | |
| 6356 | /* remember context node */ |
| 6357 | set->val.scnodes[i].in_ctx = -1; |
Michal Vasko | ec4df48 | 2019-12-16 10:02:18 +0100 | [diff] [blame] | 6358 | } else { |
| 6359 | set->val.scnodes[i].in_ctx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6360 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6361 | |
| 6362 | node = set->val.scnodes[i].scnode; |
| 6363 | |
| 6364 | if (set->val.scnodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6365 | new_node = lysc_data_parent(node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6366 | } else { |
| 6367 | /* root does not have a parent */ |
| 6368 | continue; |
| 6369 | } |
| 6370 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6371 | /* node has no parent */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6372 | if (!new_node) { |
| 6373 | new_type = set->root_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6374 | |
| 6375 | /* node has a standard parent (it can equal the root, it's not the root yet since they are fake) */ |
| 6376 | } else { |
| 6377 | new_type = LYXP_NODE_ELEM; |
| 6378 | } |
| 6379 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 6380 | idx = lyxp_set_scnode_insert_node(set, new_node, new_type); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6381 | if ((idx < orig_used) && (idx > i)) { |
| 6382 | set->val.scnodes[idx].in_ctx = 2; |
| 6383 | temp_ctx = 1; |
| 6384 | } |
| 6385 | } |
| 6386 | |
| 6387 | if (temp_ctx) { |
| 6388 | for (i = 0; i < orig_used; ++i) { |
| 6389 | if (set->val.scnodes[i].in_ctx == 2) { |
| 6390 | set->val.scnodes[i].in_ctx = 1; |
| 6391 | } |
| 6392 | } |
| 6393 | } |
| 6394 | |
| 6395 | return LY_SUCCESS; |
| 6396 | } |
| 6397 | |
| 6398 | /** |
| 6399 | * @brief Move context @p set to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'. |
| 6400 | * Result is LYXP_SET_BOOLEAN. Indirectly context position aware. |
| 6401 | * |
| 6402 | * @param[in,out] set1 Set to use for the result. |
| 6403 | * @param[in] set2 Set acting as the second operand for @p op. |
| 6404 | * @param[in] op Comparison operator to process. |
| 6405 | * @param[in] options XPath options. |
| 6406 | * @return LY_ERR |
| 6407 | */ |
| 6408 | static LY_ERR |
| 6409 | moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op, int options) |
| 6410 | { |
| 6411 | /* |
| 6412 | * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING |
| 6413 | * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING) |
| 6414 | * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 6415 | * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 6416 | * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING |
| 6417 | * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6418 | * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 6419 | * |
| 6420 | * '=' or '!=' |
| 6421 | * BOOLEAN + BOOLEAN |
| 6422 | * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 6423 | * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 6424 | * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 6425 | * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 6426 | * NUMBER + NUMBER |
| 6427 | * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6428 | * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 6429 | * STRING + STRING |
| 6430 | * |
| 6431 | * '<=', '<', '>=', '>' |
| 6432 | * NUMBER + NUMBER |
| 6433 | * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 6434 | * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 6435 | * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 6436 | * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6437 | * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 6438 | * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 6439 | * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6440 | * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6441 | */ |
| 6442 | struct lyxp_set iter1, iter2; |
| 6443 | int result; |
| 6444 | int64_t i; |
| 6445 | LY_ERR rc; |
| 6446 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6447 | memset(&iter1, 0, sizeof iter1); |
| 6448 | memset(&iter2, 0, sizeof iter2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6449 | |
| 6450 | /* iterative evaluation with node-sets */ |
| 6451 | if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) { |
| 6452 | if (set1->type == LYXP_SET_NODE_SET) { |
| 6453 | for (i = 0; i < set1->used; ++i) { |
| 6454 | switch (set2->type) { |
| 6455 | case LYXP_SET_NUMBER: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6456 | rc = set_comp_cast(&iter1, set1, LYXP_SET_NUMBER, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6457 | break; |
| 6458 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6459 | rc = set_comp_cast(&iter1, set1, LYXP_SET_BOOLEAN, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6460 | break; |
| 6461 | default: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6462 | rc = set_comp_cast(&iter1, set1, LYXP_SET_STRING, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6463 | break; |
| 6464 | } |
| 6465 | LY_CHECK_RET(rc); |
| 6466 | |
| 6467 | rc = moveto_op_comp(&iter1, set2, op, options); |
| 6468 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6469 | lyxp_set_free_content(&iter1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6470 | return rc; |
| 6471 | } |
| 6472 | |
| 6473 | /* lazy evaluation until true */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6474 | if (iter1.val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6475 | set_fill_boolean(set1, 1); |
| 6476 | return LY_SUCCESS; |
| 6477 | } |
| 6478 | } |
| 6479 | } else { |
| 6480 | for (i = 0; i < set2->used; ++i) { |
| 6481 | switch (set1->type) { |
| 6482 | case LYXP_SET_NUMBER: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6483 | rc = set_comp_cast(&iter2, set2, LYXP_SET_NUMBER, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6484 | break; |
| 6485 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6486 | rc = set_comp_cast(&iter2, set2, LYXP_SET_BOOLEAN, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6487 | break; |
| 6488 | default: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6489 | rc = set_comp_cast(&iter2, set2, LYXP_SET_STRING, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6490 | break; |
| 6491 | } |
| 6492 | LY_CHECK_RET(rc); |
| 6493 | |
| 6494 | set_fill_set(&iter1, set1); |
| 6495 | |
| 6496 | rc = moveto_op_comp(&iter1, &iter2, op, options); |
| 6497 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6498 | lyxp_set_free_content(&iter1); |
| 6499 | lyxp_set_free_content(&iter2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6500 | return rc; |
| 6501 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6502 | lyxp_set_free_content(&iter2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6503 | |
| 6504 | /* lazy evaluation until true */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6505 | if (iter1.val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6506 | set_fill_boolean(set1, 1); |
| 6507 | return LY_SUCCESS; |
| 6508 | } |
| 6509 | } |
| 6510 | } |
| 6511 | |
| 6512 | /* false for all nodes */ |
| 6513 | set_fill_boolean(set1, 0); |
| 6514 | return LY_SUCCESS; |
| 6515 | } |
| 6516 | |
| 6517 | /* first convert properly */ |
| 6518 | if ((op[0] == '=') || (op[0] == '!')) { |
| 6519 | if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6520 | lyxp_set_cast(set1, LYXP_SET_BOOLEAN); |
| 6521 | lyxp_set_cast(set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6522 | } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6523 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6524 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6525 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6526 | LY_CHECK_RET(rc); |
| 6527 | } /* else we have 2 strings */ |
| 6528 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6529 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6530 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6531 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6532 | LY_CHECK_RET(rc); |
| 6533 | } |
| 6534 | |
| 6535 | assert(set1->type == set2->type); |
| 6536 | |
| 6537 | /* compute result */ |
| 6538 | if (op[0] == '=') { |
| 6539 | if (set1->type == LYXP_SET_BOOLEAN) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6540 | result = (set1->val.bln == set2->val.bln); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6541 | } else if (set1->type == LYXP_SET_NUMBER) { |
| 6542 | result = (set1->val.num == set2->val.num); |
| 6543 | } else { |
| 6544 | assert(set1->type == LYXP_SET_STRING); |
Michal Vasko | ac6c72f | 2019-11-14 16:09:34 +0100 | [diff] [blame] | 6545 | result = !strcmp(set1->val.str, set2->val.str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6546 | } |
| 6547 | } else if (op[0] == '!') { |
| 6548 | if (set1->type == LYXP_SET_BOOLEAN) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6549 | result = (set1->val.bln != set2->val.bln); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6550 | } else if (set1->type == LYXP_SET_NUMBER) { |
| 6551 | result = (set1->val.num != set2->val.num); |
| 6552 | } else { |
| 6553 | assert(set1->type == LYXP_SET_STRING); |
Michal Vasko | ac6c72f | 2019-11-14 16:09:34 +0100 | [diff] [blame] | 6554 | result = !strcmp(set1->val.str, set2->val.str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6555 | } |
| 6556 | } else { |
| 6557 | assert(set1->type == LYXP_SET_NUMBER); |
| 6558 | if (op[0] == '<') { |
| 6559 | if (op[1] == '=') { |
| 6560 | result = (set1->val.num <= set2->val.num); |
| 6561 | } else { |
| 6562 | result = (set1->val.num < set2->val.num); |
| 6563 | } |
| 6564 | } else { |
| 6565 | if (op[1] == '=') { |
| 6566 | result = (set1->val.num >= set2->val.num); |
| 6567 | } else { |
| 6568 | result = (set1->val.num > set2->val.num); |
| 6569 | } |
| 6570 | } |
| 6571 | } |
| 6572 | |
| 6573 | /* assign result */ |
| 6574 | if (result) { |
| 6575 | set_fill_boolean(set1, 1); |
| 6576 | } else { |
| 6577 | set_fill_boolean(set1, 0); |
| 6578 | } |
| 6579 | |
| 6580 | return LY_SUCCESS; |
| 6581 | } |
| 6582 | |
| 6583 | /** |
| 6584 | * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div', |
| 6585 | * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware. |
| 6586 | * |
| 6587 | * @param[in,out] set1 Set to use for the result. |
| 6588 | * @param[in] set2 Set acting as the second operand for @p op. |
| 6589 | * @param[in] op Operator to process. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6590 | * @return LY_ERR |
| 6591 | */ |
| 6592 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6593 | 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] | 6594 | { |
| 6595 | LY_ERR rc; |
| 6596 | |
| 6597 | /* unary '-' */ |
| 6598 | if (!set2 && (op[0] == '-')) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6599 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6600 | LY_CHECK_RET(rc); |
| 6601 | set1->val.num *= -1; |
| 6602 | lyxp_set_free(set2); |
| 6603 | return LY_SUCCESS; |
| 6604 | } |
| 6605 | |
| 6606 | assert(set1 && set2); |
| 6607 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6608 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6609 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6610 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6611 | LY_CHECK_RET(rc); |
| 6612 | |
| 6613 | switch (op[0]) { |
| 6614 | /* '+' */ |
| 6615 | case '+': |
| 6616 | set1->val.num += set2->val.num; |
| 6617 | break; |
| 6618 | |
| 6619 | /* '-' */ |
| 6620 | case '-': |
| 6621 | set1->val.num -= set2->val.num; |
| 6622 | break; |
| 6623 | |
| 6624 | /* '*' */ |
| 6625 | case '*': |
| 6626 | set1->val.num *= set2->val.num; |
| 6627 | break; |
| 6628 | |
| 6629 | /* 'div' */ |
| 6630 | case 'd': |
| 6631 | set1->val.num /= set2->val.num; |
| 6632 | break; |
| 6633 | |
| 6634 | /* 'mod' */ |
| 6635 | case 'm': |
| 6636 | set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num); |
| 6637 | break; |
| 6638 | |
| 6639 | default: |
| 6640 | LOGINT_RET(set1->ctx); |
| 6641 | } |
| 6642 | |
| 6643 | return LY_SUCCESS; |
| 6644 | } |
| 6645 | |
| 6646 | /* |
| 6647 | * eval functions |
| 6648 | * |
| 6649 | * They execute a parsed XPath expression on some data subtree. |
| 6650 | */ |
| 6651 | |
| 6652 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6653 | * @brief Evaluate Predicate. Logs directly on error. |
| 6654 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6655 | * [9] Predicate ::= '[' Expr ']' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6656 | * |
| 6657 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6658 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6659 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 6660 | * @param[in] options XPath options. |
| 6661 | * @param[in] parent_pos_pred Whether parent predicate was a positional one. |
| 6662 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6663 | */ |
| 6664 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6665 | eval_predicate(struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, int options, int parent_pos_pred) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6666 | { |
| 6667 | LY_ERR rc; |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 6668 | uint16_t i, orig_exp; |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6669 | uint32_t orig_pos, orig_size; |
| 6670 | int32_t pred_in_ctx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6671 | struct lyxp_set set2; |
| 6672 | struct lyd_node *orig_parent; |
| 6673 | |
| 6674 | /* '[' */ |
| 6675 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6676 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 6677 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6678 | |
| 6679 | if (!set) { |
| 6680 | only_parse: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6681 | rc = eval_expr_select(exp, tok_idx, 0, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6682 | LY_CHECK_RET(rc); |
| 6683 | } else if (set->type == LYXP_SET_NODE_SET) { |
| 6684 | /* 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] | 6685 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6686 | |
| 6687 | /* empty set, nothing to evaluate */ |
| 6688 | if (!set->used) { |
| 6689 | goto only_parse; |
| 6690 | } |
| 6691 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6692 | orig_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6693 | orig_pos = 0; |
| 6694 | orig_size = set->used; |
| 6695 | orig_parent = NULL; |
Michal Vasko | 39dbf35 | 2020-05-21 10:08:59 +0200 | [diff] [blame] | 6696 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6697 | set_init(&set2, set); |
| 6698 | set_insert_node(&set2, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, 0); |
| 6699 | /* remember the node context position for position() and context size for last(), |
| 6700 | * predicates should always be evaluated with respect to the child axis (since we do |
| 6701 | * not support explicit axes) so we assign positions based on their parents */ |
| 6702 | if (parent_pos_pred && ((struct lyd_node *)set->val.nodes[i].node->parent != orig_parent)) { |
| 6703 | orig_parent = (struct lyd_node *)set->val.nodes[i].node->parent; |
| 6704 | orig_pos = 1; |
| 6705 | } else { |
| 6706 | ++orig_pos; |
| 6707 | } |
| 6708 | |
| 6709 | set2.ctx_pos = orig_pos; |
| 6710 | set2.ctx_size = orig_size; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6711 | *tok_idx = orig_exp; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6712 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6713 | rc = eval_expr_select(exp, tok_idx, 0, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6714 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6715 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6716 | return rc; |
| 6717 | } |
| 6718 | |
| 6719 | /* number is a position */ |
| 6720 | if (set2.type == LYXP_SET_NUMBER) { |
| 6721 | if ((long long)set2.val.num == orig_pos) { |
| 6722 | set2.val.num = 1; |
| 6723 | } else { |
| 6724 | set2.val.num = 0; |
| 6725 | } |
| 6726 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6727 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6728 | |
| 6729 | /* predicate satisfied or not? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6730 | if (!set2.val.bln) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6731 | set_remove_node_none(set, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6732 | } |
| 6733 | } |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6734 | set_remove_nodes_none(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6735 | |
| 6736 | } else if (set->type == LYXP_SET_SCNODE_SET) { |
| 6737 | for (i = 0; i < set->used; ++i) { |
| 6738 | if (set->val.scnodes[i].in_ctx == 1) { |
| 6739 | /* there is a currently-valid node */ |
| 6740 | break; |
| 6741 | } |
| 6742 | } |
| 6743 | /* empty set, nothing to evaluate */ |
| 6744 | if (i == set->used) { |
| 6745 | goto only_parse; |
| 6746 | } |
| 6747 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6748 | orig_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6749 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6750 | /* set special in_ctx to all the valid snodes */ |
| 6751 | pred_in_ctx = set_scnode_new_in_ctx(set); |
| 6752 | |
| 6753 | /* use the valid snodes one-by-one */ |
| 6754 | for (i = 0; i < set->used; ++i) { |
| 6755 | if (set->val.scnodes[i].in_ctx != pred_in_ctx) { |
| 6756 | continue; |
| 6757 | } |
| 6758 | set->val.scnodes[i].in_ctx = 1; |
| 6759 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6760 | *tok_idx = orig_exp; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6761 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6762 | rc = eval_expr_select(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6763 | LY_CHECK_RET(rc); |
| 6764 | |
| 6765 | set->val.scnodes[i].in_ctx = pred_in_ctx; |
| 6766 | } |
| 6767 | |
| 6768 | /* restore the state as it was before the predicate */ |
| 6769 | for (i = 0; i < set->used; ++i) { |
| 6770 | if (set->val.scnodes[i].in_ctx == 1) { |
| 6771 | set->val.scnodes[i].in_ctx = 0; |
| 6772 | } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) { |
| 6773 | set->val.scnodes[i].in_ctx = 1; |
| 6774 | } |
| 6775 | } |
| 6776 | |
| 6777 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6778 | set2.type = LYXP_SET_NODE_SET; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6779 | set_fill_set(&set2, set); |
| 6780 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6781 | rc = eval_expr_select(exp, tok_idx, 0, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6782 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6783 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6784 | return rc; |
| 6785 | } |
| 6786 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6787 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6788 | if (!set2.val.bln) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6789 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6790 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6791 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6792 | } |
| 6793 | |
| 6794 | /* ']' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6795 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6796 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6797 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 6798 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6799 | |
| 6800 | return LY_SUCCESS; |
| 6801 | } |
| 6802 | |
| 6803 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6804 | * @brief Evaluate Literal. Logs directly on error. |
| 6805 | * |
| 6806 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6807 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6808 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 6809 | */ |
| 6810 | static void |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6811 | eval_literal(struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6812 | { |
| 6813 | if (set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6814 | if (exp->tok_len[*tok_idx] == 2) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6815 | set_fill_string(set, "", 0); |
| 6816 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6817 | set_fill_string(set, &exp->expr[exp->tok_pos[*tok_idx] + 1], exp->tok_len[*tok_idx] - 2); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6818 | } |
| 6819 | } |
| 6820 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6821 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 6822 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6823 | } |
| 6824 | |
| 6825 | /** |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6826 | * @brief Try to compile list or leaf-list predicate in the known format to be used for hash-based instance search. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6827 | * |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6828 | * @param[in] exp Full parsed XPath expression. |
| 6829 | * @param[in,out] tok_idx Index in @p exp at the beginning of the predicate, is updated on success. |
| 6830 | * @param[in] scnode Found schema node as context for the predicate. |
| 6831 | * @param[in] format Format of any prefixes in key names/values. |
| 6832 | * @param[out] predicates Parsed predicates. |
| 6833 | * @param[out] pred_type Type of @p predicates. |
| 6834 | * @return LY_SUCCESS on success, |
| 6835 | * @return LY_ERR on any error. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6836 | */ |
| 6837 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6838 | eval_name_test_try_compile_predicates(struct lyxp_expr *exp, uint16_t *tok_idx, const struct lysc_node *scnode, |
| 6839 | LYD_FORMAT format, struct ly_path_predicate **predicates, enum ly_path_pred_type *pred_type) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6840 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6841 | LY_ERR ret = LY_SUCCESS; |
| 6842 | uint16_t key_count, e_idx, pred_idx = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6843 | const struct lysc_node *key; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6844 | size_t pred_len; |
| 6845 | int prev_lo; |
| 6846 | struct lyxp_expr *exp2 = NULL; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6847 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6848 | assert(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6849 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6850 | if (scnode->nodetype == LYS_LIST) { |
| 6851 | /* get key count */ |
| 6852 | if (scnode->flags & LYS_KEYLESS) { |
| 6853 | return LY_EINVAL; |
| 6854 | } |
| 6855 | for (key_count = 0, key = lysc_node_children(scnode, 0); key && (key->flags & LYS_KEY); key = key->next, ++key_count); |
| 6856 | assert(key_count); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6857 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6858 | /* learn where the predicates end */ |
| 6859 | e_idx = *tok_idx; |
| 6860 | while (key_count) { |
| 6861 | /* '[' */ |
| 6862 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) { |
| 6863 | return LY_EINVAL; |
| 6864 | } |
| 6865 | ++e_idx; |
| 6866 | |
| 6867 | /* ']' */ |
| 6868 | while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) { |
| 6869 | ++e_idx; |
| 6870 | } |
| 6871 | ++e_idx; |
| 6872 | |
| 6873 | /* another presumably key predicate parsed */ |
| 6874 | --key_count; |
| 6875 | } |
| 6876 | |
| 6877 | if (key_count) { |
| 6878 | /* some keys missing for sure */ |
| 6879 | return LY_EINVAL; |
| 6880 | } |
| 6881 | } else { |
| 6882 | /* learn just where this single predicate ends */ |
| 6883 | e_idx = *tok_idx; |
| 6884 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6885 | /* '[' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6886 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) { |
| 6887 | return LY_EINVAL; |
| 6888 | } |
| 6889 | ++e_idx; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6890 | |
| 6891 | /* ']' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6892 | while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) { |
| 6893 | ++e_idx; |
| 6894 | } |
| 6895 | ++e_idx; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6896 | } |
| 6897 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6898 | /* get the joined length of all the keys predicates/of the single leaf-list predicate */ |
| 6899 | pred_len = (exp->tok_pos[e_idx - 1] + exp->tok_len[e_idx - 1]) - exp->tok_pos[*tok_idx]; |
| 6900 | |
| 6901 | /* turn logging off */ |
| 6902 | prev_lo = ly_log_options(0); |
| 6903 | |
| 6904 | /* parse the predicate(s) */ |
| 6905 | LY_CHECK_GOTO(ret = ly_path_parse_predicate(scnode->module->ctx, exp->expr + exp->tok_pos[*tok_idx], pred_len, |
| 6906 | LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp2), cleanup); |
| 6907 | |
| 6908 | /* compile */ |
| 6909 | switch (format) { |
| 6910 | case LYD_SCHEMA: |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame^] | 6911 | ret = ly_path_compile_predicate(scnode->module->ctx, scnode->module, scnode, exp2, &pred_idx, lys_resolve_prefix, |
| 6912 | scnode->module, LYD_SCHEMA, predicates, pred_type); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6913 | break; |
| 6914 | case LYD_JSON: |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame^] | 6915 | ret = ly_path_compile_predicate(scnode->module->ctx, scnode->module, scnode, exp2, &pred_idx, |
| 6916 | lydjson_resolve_prefix, NULL, LYD_JSON, predicates, pred_type); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6917 | break; |
| 6918 | case LYD_XML: |
| 6919 | ret = LY_EINT; |
| 6920 | goto cleanup; |
| 6921 | } |
| 6922 | LY_CHECK_GOTO(ret, cleanup); |
| 6923 | |
| 6924 | /* success, the predicate must include all the needed information for hash-based search */ |
| 6925 | *tok_idx = e_idx; |
| 6926 | |
| 6927 | cleanup: |
| 6928 | ly_log_options(prev_lo); |
| 6929 | lyxp_expr_free(scnode->module->ctx, exp2); |
| 6930 | return ret; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6931 | } |
| 6932 | |
| 6933 | /** |
| 6934 | * @brief Evaluate NameTest and any following Predicates. Logs directly on error. |
| 6935 | * |
| 6936 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 6937 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 6938 | * [7] NameTest ::= '*' | NCName ':' '*' | QName |
| 6939 | * |
| 6940 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6941 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6942 | * @param[in] attr_axis Whether to search attributes or standard nodes. |
| 6943 | * @param[in] all_desc Whether to search all the descendants or children only. |
| 6944 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 6945 | * @param[in] options XPath options. |
| 6946 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6947 | */ |
| 6948 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6949 | eval_name_test_with_predicate(struct lyxp_expr *exp, uint16_t *tok_idx, int attr_axis, int all_desc, struct lyxp_set *set, |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6950 | int options) |
| 6951 | { |
| 6952 | int i; |
| 6953 | char *path; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6954 | const char *ncname, *ncname_dict = NULL; |
| 6955 | uint16_t ncname_len; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6956 | const struct lys_module *moveto_mod; |
| 6957 | const struct lysc_node *scnode = NULL, *tmp; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6958 | struct ly_path_predicate *predicates = NULL; |
| 6959 | enum ly_path_pred_type pred_type = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6960 | LY_ERR rc = LY_SUCCESS; |
| 6961 | |
| 6962 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6963 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 6964 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6965 | |
| 6966 | if (!set) { |
| 6967 | goto moveto; |
| 6968 | } |
| 6969 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6970 | ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]]; |
| 6971 | ncname_len = exp->tok_len[*tok_idx - 1]; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6972 | |
| 6973 | /* parse (and skip) module name */ |
| 6974 | rc = moveto_resolve_model(&ncname, &ncname_len, set, &moveto_mod); |
| 6975 | LY_CHECK_GOTO(rc, cleanup); |
| 6976 | |
| 6977 | if (moveto_mod && !attr_axis && !all_desc && (set->type == LYXP_SET_NODE_SET)) { |
| 6978 | /* find the matching schema node in some parent in the context */ |
| 6979 | for (i = 0; i < (signed)set->used; ++i) { |
| 6980 | if (set->val.nodes[i].type == set->root_type) { |
| 6981 | tmp = lys_find_child(NULL, moveto_mod, ncname, ncname_len, 0, 0); |
| 6982 | } else if ((set->val.nodes[i].type == LYXP_NODE_ELEM) |
| 6983 | && (!scnode || (lysc_data_parent(scnode) != set->val.nodes[i].node->schema))) { |
| 6984 | /* do not repeat the same search */ |
| 6985 | tmp = lys_find_child(set->val.nodes[i].node->schema, moveto_mod, ncname, ncname_len, 0, 0); |
| 6986 | } |
| 6987 | |
| 6988 | /* additional context check */ |
| 6989 | if (tmp && (set->root_type == LYXP_NODE_ROOT_CONFIG) && (tmp->flags & LYS_CONFIG_R)) { |
| 6990 | tmp = NULL; |
| 6991 | } |
| 6992 | |
| 6993 | if (tmp) { |
| 6994 | if (scnode) { |
| 6995 | /* we found a schema node with the same name but at different level, give up, too complicated */ |
| 6996 | scnode = NULL; |
| 6997 | break; |
| 6998 | } else { |
| 6999 | /* remember the found schema node and continue to make sure it can be used */ |
| 7000 | scnode = tmp; |
| 7001 | } |
| 7002 | tmp = NULL; |
| 7003 | } |
| 7004 | } |
| 7005 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7006 | if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) { |
| 7007 | /* try to create the predicates */ |
| 7008 | if (eval_name_test_try_compile_predicates(exp, tok_idx, scnode, set->format, &predicates, &pred_type)) { |
| 7009 | /* hashes cannot be used */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7010 | scnode = NULL; |
| 7011 | } |
| 7012 | } |
| 7013 | } |
| 7014 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7015 | if (!scnode && moveto_mod) { |
| 7016 | /* we are not able to match based on a schema node and not all the modules match, |
| 7017 | * use dictionary for efficient comparison */ |
| 7018 | ncname_dict = lydict_insert(set->ctx, ncname, ncname_len); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7019 | } |
| 7020 | |
| 7021 | moveto: |
| 7022 | /* move to the attribute(s), data node(s), or schema node(s) */ |
| 7023 | if (attr_axis) { |
| 7024 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 7025 | set_scnode_clear_ctx(set); |
| 7026 | } else { |
| 7027 | if (all_desc) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7028 | rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7029 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7030 | rc = moveto_attr(set, moveto_mod, ncname_dict); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7031 | } |
| 7032 | LY_CHECK_GOTO(rc, cleanup); |
| 7033 | } |
| 7034 | } else { |
| 7035 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 7036 | if (all_desc) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7037 | rc = moveto_scnode_alldesc(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7038 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7039 | rc = moveto_scnode(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7040 | } |
| 7041 | LY_CHECK_GOTO(rc, cleanup); |
| 7042 | |
| 7043 | for (i = set->used - 1; i > -1; --i) { |
| 7044 | if (set->val.scnodes[i].in_ctx > 0) { |
| 7045 | break; |
| 7046 | } |
| 7047 | } |
| 7048 | if (i == -1) { |
| 7049 | path = lysc_path(set->ctx_scnode, LYSC_PATH_LOG, NULL, 0); |
| 7050 | LOGWRN(set->ctx, "Schema node \"%.*s\" not found (%.*s) with context node \"%s\".", |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7051 | exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]], |
| 7052 | exp->tok_pos[*tok_idx] + exp->tok_len[*tok_idx], exp->expr, path); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7053 | free(path); |
| 7054 | } |
| 7055 | } else { |
| 7056 | if (all_desc) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7057 | rc = moveto_node_alldesc(set, moveto_mod, ncname_dict); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7058 | } else { |
| 7059 | if (scnode) { |
| 7060 | /* we can find the nodes using hashes */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7061 | rc = moveto_node_hash(set, scnode, predicates); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7062 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7063 | rc = moveto_node(set, moveto_mod, ncname_dict); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7064 | } |
| 7065 | } |
| 7066 | LY_CHECK_GOTO(rc, cleanup); |
| 7067 | } |
| 7068 | } |
| 7069 | |
| 7070 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7071 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 7072 | rc = eval_predicate(exp, tok_idx, set, options, 1); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7073 | LY_CHECK_RET(rc); |
| 7074 | } |
| 7075 | |
| 7076 | cleanup: |
Michal Vasko | db51a8d | 2020-05-27 15:22:29 +0200 | [diff] [blame] | 7077 | if (set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7078 | lydict_remove(set->ctx, ncname_dict); |
| 7079 | ly_path_predicates_free(set->ctx, pred_type, scnode, predicates); |
Michal Vasko | db51a8d | 2020-05-27 15:22:29 +0200 | [diff] [blame] | 7080 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7081 | return rc; |
| 7082 | } |
| 7083 | |
| 7084 | /** |
| 7085 | * @brief Evaluate NodeType and any following Predicates. Logs directly on error. |
| 7086 | * |
| 7087 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 7088 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 7089 | * [8] NodeType ::= 'text' | 'node' |
| 7090 | * |
| 7091 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7092 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7093 | * @param[in] attr_axis Whether to search attributes or standard nodes. |
| 7094 | * @param[in] all_desc Whether to search all the descendants or children only. |
| 7095 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7096 | * @param[in] options XPath options. |
| 7097 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7098 | */ |
| 7099 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7100 | eval_node_type_with_predicate(struct lyxp_expr *exp, uint16_t *tok_idx, int attr_axis, int all_desc, |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7101 | struct lyxp_set *set, int options) |
| 7102 | { |
| 7103 | LY_ERR rc; |
| 7104 | |
| 7105 | /* TODO */ |
| 7106 | (void)attr_axis; |
| 7107 | (void)all_desc; |
| 7108 | |
| 7109 | if (set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7110 | assert(exp->tok_len[*tok_idx] == 4); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7111 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 7112 | set_scnode_clear_ctx(set); |
| 7113 | /* just for the debug message below */ |
| 7114 | set = NULL; |
| 7115 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7116 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7117 | rc = xpath_node(NULL, 0, set, options); |
| 7118 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7119 | assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4)); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7120 | rc = xpath_text(NULL, 0, set, options); |
| 7121 | } |
| 7122 | LY_CHECK_RET(rc); |
| 7123 | } |
| 7124 | } |
| 7125 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7126 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7127 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7128 | |
| 7129 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7130 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7131 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7132 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7133 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7134 | |
| 7135 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7136 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7137 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7138 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7139 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7140 | |
| 7141 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7142 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 7143 | rc = eval_predicate(exp, tok_idx, set, options, 1); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7144 | LY_CHECK_RET(rc); |
| 7145 | } |
| 7146 | |
| 7147 | return LY_SUCCESS; |
| 7148 | } |
| 7149 | |
| 7150 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7151 | * @brief Evaluate RelativeLocationPath. Logs directly on error. |
| 7152 | * |
| 7153 | * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step |
| 7154 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7155 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7156 | * |
| 7157 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7158 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7159 | * @param[in] all_desc Whether to search all the descendants or children only. |
| 7160 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7161 | * @param[in] options XPath options. |
| 7162 | * @return LY_ERR (YL_EINCOMPLETE on unresolved when) |
| 7163 | */ |
| 7164 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7165 | eval_relative_location_path(struct lyxp_expr *exp, uint16_t *tok_idx, int all_desc, struct lyxp_set *set, int options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7166 | { |
| 7167 | int attr_axis; |
| 7168 | LY_ERR rc; |
| 7169 | |
| 7170 | goto step; |
| 7171 | do { |
| 7172 | /* evaluate '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7173 | if (exp->tok_len[*tok_idx] == 1) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7174 | all_desc = 0; |
| 7175 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7176 | assert(exp->tok_len[*tok_idx] == 2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7177 | all_desc = 1; |
| 7178 | } |
| 7179 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7180 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7181 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7182 | |
| 7183 | step: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7184 | /* evaluate abbreviated axis '@'? if any */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7185 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7186 | attr_axis = 1; |
| 7187 | |
| 7188 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7189 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7190 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7191 | } else { |
| 7192 | attr_axis = 0; |
| 7193 | } |
| 7194 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7195 | /* Step */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7196 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7197 | case LYXP_TOKEN_DOT: |
| 7198 | /* evaluate '.' */ |
| 7199 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 7200 | rc = moveto_scnode_self(set, all_desc, options); |
| 7201 | } else { |
| 7202 | rc = moveto_self(set, all_desc, options); |
| 7203 | } |
| 7204 | LY_CHECK_RET(rc); |
| 7205 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7206 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7207 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7208 | break; |
| 7209 | |
| 7210 | case LYXP_TOKEN_DDOT: |
| 7211 | /* evaluate '..' */ |
| 7212 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 7213 | rc = moveto_scnode_parent(set, all_desc, options); |
| 7214 | } else { |
| 7215 | rc = moveto_parent(set, all_desc, options); |
| 7216 | } |
| 7217 | LY_CHECK_RET(rc); |
| 7218 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7219 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7220 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7221 | break; |
| 7222 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7223 | case LYXP_TOKEN_NAMETEST: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7224 | /* evaluate NameTest Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7225 | rc = eval_name_test_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7226 | LY_CHECK_RET(rc); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7227 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7228 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7229 | case LYXP_TOKEN_NODETYPE: |
| 7230 | /* evaluate NodeType Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7231 | rc = eval_node_type_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7232 | LY_CHECK_RET(rc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7233 | break; |
| 7234 | |
| 7235 | default: |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 7236 | LOGINT_RET(set ? set->ctx : NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7237 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7238 | } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7239 | |
| 7240 | return LY_SUCCESS; |
| 7241 | } |
| 7242 | |
| 7243 | /** |
| 7244 | * @brief Evaluate AbsoluteLocationPath. Logs directly on error. |
| 7245 | * |
| 7246 | * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath |
| 7247 | * |
| 7248 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7249 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7250 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7251 | * @param[in] options XPath options. |
| 7252 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7253 | */ |
| 7254 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7255 | eval_absolute_location_path(struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, int options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7256 | { |
| 7257 | int all_desc; |
| 7258 | LY_ERR rc; |
| 7259 | |
| 7260 | if (set) { |
| 7261 | /* no matter what tokens follow, we need to be at the root */ |
| 7262 | moveto_root(set, options); |
| 7263 | } |
| 7264 | |
| 7265 | /* '/' RelativeLocationPath? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7266 | if (exp->tok_len[*tok_idx] == 1) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7267 | /* evaluate '/' - deferred */ |
| 7268 | all_desc = 0; |
| 7269 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7270 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7271 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7272 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7273 | if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7274 | return LY_SUCCESS; |
| 7275 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7276 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7277 | case LYXP_TOKEN_DOT: |
| 7278 | case LYXP_TOKEN_DDOT: |
| 7279 | case LYXP_TOKEN_AT: |
| 7280 | case LYXP_TOKEN_NAMETEST: |
| 7281 | case LYXP_TOKEN_NODETYPE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7282 | rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7283 | LY_CHECK_RET(rc); |
| 7284 | break; |
| 7285 | default: |
| 7286 | break; |
| 7287 | } |
| 7288 | |
| 7289 | /* '//' RelativeLocationPath */ |
| 7290 | } else { |
| 7291 | /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */ |
| 7292 | all_desc = 1; |
| 7293 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7294 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7295 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7296 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7297 | rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7298 | LY_CHECK_RET(rc); |
| 7299 | } |
| 7300 | |
| 7301 | return LY_SUCCESS; |
| 7302 | } |
| 7303 | |
| 7304 | /** |
| 7305 | * @brief Evaluate FunctionCall. Logs directly on error. |
| 7306 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7307 | * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7308 | * |
| 7309 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7310 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7311 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7312 | * @param[in] options XPath options. |
| 7313 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7314 | */ |
| 7315 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7316 | eval_function_call(struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, int options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7317 | { |
| 7318 | LY_ERR rc; |
| 7319 | LY_ERR (*xpath_func)(struct lyxp_set **, uint16_t, struct lyxp_set *, int) = NULL; |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 7320 | uint16_t arg_count = 0, i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7321 | struct lyxp_set **args = NULL, **args_aux; |
| 7322 | |
| 7323 | if (set) { |
| 7324 | /* FunctionName */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7325 | switch (exp->tok_len[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7326 | case 3: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7327 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7328 | xpath_func = &xpath_not; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7329 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7330 | xpath_func = &xpath_sum; |
| 7331 | } |
| 7332 | break; |
| 7333 | case 4: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7334 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7335 | xpath_func = &xpath_lang; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7336 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7337 | xpath_func = &xpath_last; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7338 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7339 | xpath_func = &xpath_name; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7340 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7341 | xpath_func = &xpath_true; |
| 7342 | } |
| 7343 | break; |
| 7344 | case 5: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7345 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7346 | xpath_func = &xpath_count; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7347 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7348 | xpath_func = &xpath_false; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7349 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7350 | xpath_func = &xpath_floor; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7351 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7352 | xpath_func = &xpath_round; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7353 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7354 | xpath_func = &xpath_deref; |
| 7355 | } |
| 7356 | break; |
| 7357 | case 6: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7358 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7359 | xpath_func = &xpath_concat; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7360 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7361 | xpath_func = &xpath_number; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7362 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7363 | xpath_func = &xpath_string; |
| 7364 | } |
| 7365 | break; |
| 7366 | case 7: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7367 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7368 | xpath_func = &xpath_boolean; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7369 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7370 | xpath_func = &xpath_ceiling; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7371 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7372 | xpath_func = &xpath_current; |
| 7373 | } |
| 7374 | break; |
| 7375 | case 8: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7376 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7377 | xpath_func = &xpath_contains; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7378 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7379 | xpath_func = &xpath_position; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7380 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7381 | xpath_func = &xpath_re_match; |
| 7382 | } |
| 7383 | break; |
| 7384 | case 9: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7385 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7386 | xpath_func = &xpath_substring; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7387 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7388 | xpath_func = &xpath_translate; |
| 7389 | } |
| 7390 | break; |
| 7391 | case 10: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7392 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7393 | xpath_func = &xpath_local_name; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7394 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7395 | xpath_func = &xpath_enum_value; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7396 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7397 | xpath_func = &xpath_bit_is_set; |
| 7398 | } |
| 7399 | break; |
| 7400 | case 11: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7401 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7402 | xpath_func = &xpath_starts_with; |
| 7403 | } |
| 7404 | break; |
| 7405 | case 12: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7406 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7407 | xpath_func = &xpath_derived_from; |
| 7408 | } |
| 7409 | break; |
| 7410 | case 13: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7411 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7412 | xpath_func = &xpath_namespace_uri; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7413 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7414 | xpath_func = &xpath_string_length; |
| 7415 | } |
| 7416 | break; |
| 7417 | case 15: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7418 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7419 | xpath_func = &xpath_normalize_space; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7420 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7421 | xpath_func = &xpath_substring_after; |
| 7422 | } |
| 7423 | break; |
| 7424 | case 16: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7425 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7426 | xpath_func = &xpath_substring_before; |
| 7427 | } |
| 7428 | break; |
| 7429 | case 20: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7430 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7431 | xpath_func = &xpath_derived_from_or_self; |
| 7432 | } |
| 7433 | break; |
| 7434 | } |
| 7435 | |
| 7436 | if (!xpath_func) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7437 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INFUNC, exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7438 | return LY_EVALID; |
| 7439 | } |
| 7440 | } |
| 7441 | |
| 7442 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7443 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7444 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7445 | |
| 7446 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7447 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7448 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7449 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7450 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7451 | |
| 7452 | /* ( Expr ( ',' Expr )* )? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7453 | if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7454 | if (set) { |
| 7455 | args = malloc(sizeof *args); |
| 7456 | LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup); |
| 7457 | arg_count = 1; |
| 7458 | args[0] = set_copy(set); |
| 7459 | if (!args[0]) { |
| 7460 | rc = LY_EMEM; |
| 7461 | goto cleanup; |
| 7462 | } |
| 7463 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7464 | rc = eval_expr_select(exp, tok_idx, 0, args[0], options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7465 | LY_CHECK_GOTO(rc, cleanup); |
| 7466 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7467 | rc = eval_expr_select(exp, tok_idx, 0, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7468 | LY_CHECK_GOTO(rc, cleanup); |
| 7469 | } |
| 7470 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7471 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7472 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7473 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7474 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7475 | |
| 7476 | if (set) { |
| 7477 | ++arg_count; |
| 7478 | args_aux = realloc(args, arg_count * sizeof *args); |
| 7479 | LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup); |
| 7480 | args = args_aux; |
| 7481 | args[arg_count - 1] = set_copy(set); |
| 7482 | if (!args[arg_count - 1]) { |
| 7483 | rc = LY_EMEM; |
| 7484 | goto cleanup; |
| 7485 | } |
| 7486 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7487 | rc = eval_expr_select(exp, tok_idx, 0, args[arg_count - 1], options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7488 | LY_CHECK_GOTO(rc, cleanup); |
| 7489 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7490 | rc = eval_expr_select(exp, tok_idx, 0, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7491 | LY_CHECK_GOTO(rc, cleanup); |
| 7492 | } |
| 7493 | } |
| 7494 | |
| 7495 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7496 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7497 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7498 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7499 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7500 | |
| 7501 | if (set) { |
| 7502 | /* evaluate function */ |
| 7503 | rc = xpath_func(args, arg_count, set, options); |
| 7504 | |
| 7505 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7506 | /* merge all nodes from arg evaluations */ |
| 7507 | for (i = 0; i < arg_count; ++i) { |
| 7508 | set_scnode_clear_ctx(args[i]); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7509 | lyxp_set_scnode_merge(set, args[i]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7510 | } |
| 7511 | } |
| 7512 | } else { |
| 7513 | rc = LY_SUCCESS; |
| 7514 | } |
| 7515 | |
| 7516 | cleanup: |
| 7517 | for (i = 0; i < arg_count; ++i) { |
| 7518 | lyxp_set_free(args[i]); |
| 7519 | } |
| 7520 | free(args); |
| 7521 | |
| 7522 | return rc; |
| 7523 | } |
| 7524 | |
| 7525 | /** |
| 7526 | * @brief Evaluate Number. Logs directly on error. |
| 7527 | * |
| 7528 | * @param[in] ctx Context for errors. |
| 7529 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7530 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7531 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7532 | * @return LY_ERR |
| 7533 | */ |
| 7534 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7535 | eval_number(struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7536 | { |
| 7537 | long double num; |
| 7538 | char *endptr; |
| 7539 | |
| 7540 | if (set) { |
| 7541 | errno = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7542 | num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7543 | if (errno) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7544 | LOGVAL(ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7545 | LOGVAL(ctx, LY_VLOG_LYD, set->ctx_node, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double (%s).", |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7546 | exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]], strerror(errno)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7547 | return LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7548 | } else if (endptr - &exp->expr[exp->tok_pos[*tok_idx]] != exp->tok_len[*tok_idx]) { |
| 7549 | LOGVAL(ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7550 | LOGVAL(ctx, LY_VLOG_LYD, set->ctx_node, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double.", |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7551 | exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7552 | return LY_EVALID; |
| 7553 | } |
| 7554 | |
| 7555 | set_fill_number(set, num); |
| 7556 | } |
| 7557 | |
| 7558 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7559 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7560 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7561 | return LY_SUCCESS; |
| 7562 | } |
| 7563 | |
| 7564 | /** |
| 7565 | * @brief Evaluate PathExpr. Logs directly on error. |
| 7566 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7567 | * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate* |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7568 | * | PrimaryExpr Predicate* '/' RelativeLocationPath |
| 7569 | * | PrimaryExpr Predicate* '//' RelativeLocationPath |
| 7570 | * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7571 | * [10] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7572 | * |
| 7573 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7574 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7575 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7576 | * @param[in] options XPath options. |
| 7577 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7578 | */ |
| 7579 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7580 | eval_path_expr(struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, int options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7581 | { |
| 7582 | int all_desc, parent_pos_pred; |
| 7583 | LY_ERR rc; |
| 7584 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7585 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7586 | case LYXP_TOKEN_PAR1: |
| 7587 | /* '(' Expr ')' */ |
| 7588 | |
| 7589 | /* '(' */ |
| 7590 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7591 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7592 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7593 | |
| 7594 | /* Expr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7595 | rc = eval_expr_select(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7596 | LY_CHECK_RET(rc); |
| 7597 | |
| 7598 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7599 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7600 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7601 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7602 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7603 | |
| 7604 | parent_pos_pred = 0; |
| 7605 | goto predicate; |
| 7606 | |
| 7607 | case LYXP_TOKEN_DOT: |
| 7608 | case LYXP_TOKEN_DDOT: |
| 7609 | case LYXP_TOKEN_AT: |
| 7610 | case LYXP_TOKEN_NAMETEST: |
| 7611 | case LYXP_TOKEN_NODETYPE: |
| 7612 | /* RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7613 | rc = eval_relative_location_path(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7614 | LY_CHECK_RET(rc); |
| 7615 | break; |
| 7616 | |
| 7617 | case LYXP_TOKEN_FUNCNAME: |
| 7618 | /* FunctionCall */ |
| 7619 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7620 | rc = eval_function_call(exp, tok_idx, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7621 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7622 | rc = eval_function_call(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7623 | } |
| 7624 | LY_CHECK_RET(rc); |
| 7625 | |
| 7626 | parent_pos_pred = 1; |
| 7627 | goto predicate; |
| 7628 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 7629 | case LYXP_TOKEN_OPER_PATH: |
| 7630 | case LYXP_TOKEN_OPER_RPATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7631 | /* AbsoluteLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7632 | rc = eval_absolute_location_path(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7633 | LY_CHECK_RET(rc); |
| 7634 | break; |
| 7635 | |
| 7636 | case LYXP_TOKEN_LITERAL: |
| 7637 | /* Literal */ |
| 7638 | if (!set || (options & LYXP_SCNODE_ALL)) { |
| 7639 | if (set) { |
| 7640 | set_scnode_clear_ctx(set); |
| 7641 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7642 | eval_literal(exp, tok_idx, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7643 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7644 | eval_literal(exp, tok_idx, set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7645 | } |
| 7646 | |
| 7647 | parent_pos_pred = 1; |
| 7648 | goto predicate; |
| 7649 | |
| 7650 | case LYXP_TOKEN_NUMBER: |
| 7651 | /* Number */ |
| 7652 | if (!set || (options & LYXP_SCNODE_ALL)) { |
| 7653 | if (set) { |
| 7654 | set_scnode_clear_ctx(set); |
| 7655 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7656 | rc = eval_number(NULL, exp, tok_idx, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7657 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7658 | rc = eval_number(set->ctx, exp, tok_idx, set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7659 | } |
| 7660 | LY_CHECK_RET(rc); |
| 7661 | |
| 7662 | parent_pos_pred = 1; |
| 7663 | goto predicate; |
| 7664 | |
| 7665 | default: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7666 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[*tok_idx]), |
| 7667 | &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7668 | return LY_EVALID; |
| 7669 | } |
| 7670 | |
| 7671 | return LY_SUCCESS; |
| 7672 | |
| 7673 | predicate: |
| 7674 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7675 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 7676 | rc = eval_predicate(exp, tok_idx, set, options, parent_pos_pred); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7677 | LY_CHECK_RET(rc); |
| 7678 | } |
| 7679 | |
| 7680 | /* ('/' or '//') RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7681 | if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7682 | |
| 7683 | /* evaluate '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7684 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7685 | all_desc = 0; |
| 7686 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7687 | all_desc = 1; |
| 7688 | } |
| 7689 | |
| 7690 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7691 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7692 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7693 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7694 | rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7695 | LY_CHECK_RET(rc); |
| 7696 | } |
| 7697 | |
| 7698 | return LY_SUCCESS; |
| 7699 | } |
| 7700 | |
| 7701 | /** |
| 7702 | * @brief Evaluate UnionExpr. Logs directly on error. |
| 7703 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7704 | * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7705 | * |
| 7706 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7707 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7708 | * @param[in] repeat How many times this expression is repeated. |
| 7709 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7710 | * @param[in] options XPath options. |
| 7711 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7712 | */ |
| 7713 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7714 | eval_union_expr(struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, int options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7715 | { |
| 7716 | LY_ERR rc = LY_SUCCESS; |
| 7717 | struct lyxp_set orig_set, set2; |
| 7718 | uint16_t i; |
| 7719 | |
| 7720 | assert(repeat); |
| 7721 | |
| 7722 | set_init(&orig_set, set); |
| 7723 | set_init(&set2, set); |
| 7724 | |
| 7725 | set_fill_set(&orig_set, set); |
| 7726 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7727 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7728 | LY_CHECK_GOTO(rc, cleanup); |
| 7729 | |
| 7730 | /* ('|' PathExpr)* */ |
| 7731 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7732 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7733 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7734 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7735 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7736 | |
| 7737 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7738 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7739 | LY_CHECK_GOTO(rc, cleanup); |
| 7740 | continue; |
| 7741 | } |
| 7742 | |
| 7743 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7744 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7745 | LY_CHECK_GOTO(rc, cleanup); |
| 7746 | |
| 7747 | /* eval */ |
| 7748 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7749 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7750 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7751 | rc = moveto_union(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7752 | LY_CHECK_GOTO(rc, cleanup); |
| 7753 | } |
| 7754 | } |
| 7755 | |
| 7756 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7757 | lyxp_set_free_content(&orig_set); |
| 7758 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7759 | return rc; |
| 7760 | } |
| 7761 | |
| 7762 | /** |
| 7763 | * @brief Evaluate UnaryExpr. Logs directly on error. |
| 7764 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7765 | * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7766 | * |
| 7767 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7768 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7769 | * @param[in] repeat How many times this expression is repeated. |
| 7770 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7771 | * @param[in] options XPath options. |
| 7772 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7773 | */ |
| 7774 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7775 | eval_unary_expr(struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, int options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7776 | { |
| 7777 | LY_ERR rc; |
| 7778 | uint16_t this_op, i; |
| 7779 | |
| 7780 | assert(repeat); |
| 7781 | |
| 7782 | /* ('-')+ */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7783 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7784 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7785 | assert(!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) && (exp->expr[exp->tok_pos[*tok_idx]] == '-')); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7786 | |
| 7787 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7788 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7789 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7790 | } |
| 7791 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7792 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7793 | LY_CHECK_RET(rc); |
| 7794 | |
| 7795 | if (set && (repeat % 2)) { |
| 7796 | if (options & LYXP_SCNODE_ALL) { |
| 7797 | warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]); |
| 7798 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7799 | 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] | 7800 | LY_CHECK_RET(rc); |
| 7801 | } |
| 7802 | } |
| 7803 | |
| 7804 | return LY_SUCCESS; |
| 7805 | } |
| 7806 | |
| 7807 | /** |
| 7808 | * @brief Evaluate MultiplicativeExpr. Logs directly on error. |
| 7809 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7810 | * [18] MultiplicativeExpr ::= UnaryExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7811 | * | MultiplicativeExpr '*' UnaryExpr |
| 7812 | * | MultiplicativeExpr 'div' UnaryExpr |
| 7813 | * | MultiplicativeExpr 'mod' UnaryExpr |
| 7814 | * |
| 7815 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7816 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7817 | * @param[in] repeat How many times this expression is repeated. |
| 7818 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7819 | * @param[in] options XPath options. |
| 7820 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7821 | */ |
| 7822 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7823 | eval_multiplicative_expr(struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, int options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7824 | { |
| 7825 | LY_ERR rc; |
| 7826 | uint16_t this_op; |
| 7827 | struct lyxp_set orig_set, set2; |
| 7828 | uint16_t i; |
| 7829 | |
| 7830 | assert(repeat); |
| 7831 | |
| 7832 | set_init(&orig_set, set); |
| 7833 | set_init(&set2, set); |
| 7834 | |
| 7835 | set_fill_set(&orig_set, set); |
| 7836 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7837 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7838 | LY_CHECK_GOTO(rc, cleanup); |
| 7839 | |
| 7840 | /* ('*' / 'div' / 'mod' UnaryExpr)* */ |
| 7841 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7842 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7843 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7844 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7845 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7846 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7847 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7848 | |
| 7849 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7850 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7851 | LY_CHECK_GOTO(rc, cleanup); |
| 7852 | continue; |
| 7853 | } |
| 7854 | |
| 7855 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7856 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7857 | LY_CHECK_GOTO(rc, cleanup); |
| 7858 | |
| 7859 | /* eval */ |
| 7860 | if (options & LYXP_SCNODE_ALL) { |
| 7861 | 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] | 7862 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7863 | set_scnode_clear_ctx(set); |
| 7864 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7865 | 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] | 7866 | LY_CHECK_GOTO(rc, cleanup); |
| 7867 | } |
| 7868 | } |
| 7869 | |
| 7870 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7871 | lyxp_set_free_content(&orig_set); |
| 7872 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7873 | return rc; |
| 7874 | } |
| 7875 | |
| 7876 | /** |
| 7877 | * @brief Evaluate AdditiveExpr. Logs directly on error. |
| 7878 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7879 | * [17] AdditiveExpr ::= MultiplicativeExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7880 | * | AdditiveExpr '+' MultiplicativeExpr |
| 7881 | * | AdditiveExpr '-' MultiplicativeExpr |
| 7882 | * |
| 7883 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7884 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7885 | * @param[in] repeat How many times this expression is repeated. |
| 7886 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7887 | * @param[in] options XPath options. |
| 7888 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7889 | */ |
| 7890 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7891 | eval_additive_expr(struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, int options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7892 | { |
| 7893 | LY_ERR rc; |
| 7894 | uint16_t this_op; |
| 7895 | struct lyxp_set orig_set, set2; |
| 7896 | uint16_t i; |
| 7897 | |
| 7898 | assert(repeat); |
| 7899 | |
| 7900 | set_init(&orig_set, set); |
| 7901 | set_init(&set2, set); |
| 7902 | |
| 7903 | set_fill_set(&orig_set, set); |
| 7904 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7905 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7906 | LY_CHECK_GOTO(rc, cleanup); |
| 7907 | |
| 7908 | /* ('+' / '-' MultiplicativeExpr)* */ |
| 7909 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7910 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7911 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7912 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7913 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7914 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7915 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7916 | |
| 7917 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7918 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7919 | LY_CHECK_GOTO(rc, cleanup); |
| 7920 | continue; |
| 7921 | } |
| 7922 | |
| 7923 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7924 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7925 | LY_CHECK_GOTO(rc, cleanup); |
| 7926 | |
| 7927 | /* eval */ |
| 7928 | if (options & LYXP_SCNODE_ALL) { |
| 7929 | 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] | 7930 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7931 | set_scnode_clear_ctx(set); |
| 7932 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7933 | 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] | 7934 | LY_CHECK_GOTO(rc, cleanup); |
| 7935 | } |
| 7936 | } |
| 7937 | |
| 7938 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7939 | lyxp_set_free_content(&orig_set); |
| 7940 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7941 | return rc; |
| 7942 | } |
| 7943 | |
| 7944 | /** |
| 7945 | * @brief Evaluate RelationalExpr. Logs directly on error. |
| 7946 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7947 | * [16] RelationalExpr ::= AdditiveExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7948 | * | RelationalExpr '<' AdditiveExpr |
| 7949 | * | RelationalExpr '>' AdditiveExpr |
| 7950 | * | RelationalExpr '<=' AdditiveExpr |
| 7951 | * | RelationalExpr '>=' AdditiveExpr |
| 7952 | * |
| 7953 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7954 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7955 | * @param[in] repeat How many times this expression is repeated. |
| 7956 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7957 | * @param[in] options XPath options. |
| 7958 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7959 | */ |
| 7960 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7961 | eval_relational_expr(struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, int options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7962 | { |
| 7963 | LY_ERR rc; |
| 7964 | uint16_t this_op; |
| 7965 | struct lyxp_set orig_set, set2; |
| 7966 | uint16_t i; |
| 7967 | |
| 7968 | assert(repeat); |
| 7969 | |
| 7970 | set_init(&orig_set, set); |
| 7971 | set_init(&set2, set); |
| 7972 | |
| 7973 | set_fill_set(&orig_set, set); |
| 7974 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7975 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7976 | LY_CHECK_GOTO(rc, cleanup); |
| 7977 | |
| 7978 | /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */ |
| 7979 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7980 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7981 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7982 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7983 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7984 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7985 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7986 | |
| 7987 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7988 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7989 | LY_CHECK_GOTO(rc, cleanup); |
| 7990 | continue; |
| 7991 | } |
| 7992 | |
| 7993 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7994 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7995 | LY_CHECK_GOTO(rc, cleanup); |
| 7996 | |
| 7997 | /* eval */ |
| 7998 | if (options & LYXP_SCNODE_ALL) { |
| 7999 | 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] | 8000 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8001 | set_scnode_clear_ctx(set); |
| 8002 | } else { |
| 8003 | rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options); |
| 8004 | LY_CHECK_GOTO(rc, cleanup); |
| 8005 | } |
| 8006 | } |
| 8007 | |
| 8008 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8009 | lyxp_set_free_content(&orig_set); |
| 8010 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8011 | return rc; |
| 8012 | } |
| 8013 | |
| 8014 | /** |
| 8015 | * @brief Evaluate EqualityExpr. Logs directly on error. |
| 8016 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8017 | * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8018 | * | EqualityExpr '!=' RelationalExpr |
| 8019 | * |
| 8020 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8021 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8022 | * @param[in] repeat How many times this expression is repeated. |
| 8023 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8024 | * @param[in] options XPath options. |
| 8025 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8026 | */ |
| 8027 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8028 | eval_equality_expr(struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, int options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8029 | { |
| 8030 | LY_ERR rc; |
| 8031 | uint16_t this_op; |
| 8032 | struct lyxp_set orig_set, set2; |
| 8033 | uint16_t i; |
| 8034 | |
| 8035 | assert(repeat); |
| 8036 | |
| 8037 | set_init(&orig_set, set); |
| 8038 | set_init(&set2, set); |
| 8039 | |
| 8040 | set_fill_set(&orig_set, set); |
| 8041 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8042 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8043 | LY_CHECK_GOTO(rc, cleanup); |
| 8044 | |
| 8045 | /* ('=' / '!=' RelationalExpr)* */ |
| 8046 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8047 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8048 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8049 | assert((exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL) || (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_NEQUAL)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8050 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8051 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 8052 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8053 | |
| 8054 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8055 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8056 | LY_CHECK_GOTO(rc, cleanup); |
| 8057 | continue; |
| 8058 | } |
| 8059 | |
| 8060 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8061 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8062 | LY_CHECK_GOTO(rc, cleanup); |
| 8063 | |
| 8064 | /* eval */ |
| 8065 | if (options & LYXP_SCNODE_ALL) { |
| 8066 | warn_operands(set->ctx, set, &set2, 0, exp->expr, exp->tok_pos[this_op - 1]); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8067 | warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1); |
| 8068 | warn_equality_value(exp, &set2, this_op - 1, this_op - 1, *tok_idx - 1); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 8069 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8070 | set_scnode_clear_ctx(set); |
| 8071 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8072 | rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options); |
| 8073 | LY_CHECK_GOTO(rc, cleanup); |
| 8074 | } |
| 8075 | } |
| 8076 | |
| 8077 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8078 | lyxp_set_free_content(&orig_set); |
| 8079 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8080 | return rc; |
| 8081 | } |
| 8082 | |
| 8083 | /** |
| 8084 | * @brief Evaluate AndExpr. Logs directly on error. |
| 8085 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8086 | * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8087 | * |
| 8088 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8089 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8090 | * @param[in] repeat How many times this expression is repeated. |
| 8091 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8092 | * @param[in] options XPath options. |
| 8093 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8094 | */ |
| 8095 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8096 | eval_and_expr(struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, int options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8097 | { |
| 8098 | LY_ERR rc; |
| 8099 | struct lyxp_set orig_set, set2; |
| 8100 | uint16_t i; |
| 8101 | |
| 8102 | assert(repeat); |
| 8103 | |
| 8104 | set_init(&orig_set, set); |
| 8105 | set_init(&set2, set); |
| 8106 | |
| 8107 | set_fill_set(&orig_set, set); |
| 8108 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8109 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8110 | LY_CHECK_GOTO(rc, cleanup); |
| 8111 | |
| 8112 | /* cast to boolean, we know that will be the final result */ |
| 8113 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 8114 | set_scnode_clear_ctx(set); |
| 8115 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8116 | lyxp_set_cast(set, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8117 | } |
| 8118 | |
| 8119 | /* ('and' EqualityExpr)* */ |
| 8120 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8121 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG); |
| 8122 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || !set->val.bln ? "skipped" : "parsed"), |
| 8123 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 8124 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8125 | |
| 8126 | /* lazy evaluation */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8127 | if (!set || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) { |
| 8128 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8129 | LY_CHECK_GOTO(rc, cleanup); |
| 8130 | continue; |
| 8131 | } |
| 8132 | |
| 8133 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8134 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8135 | LY_CHECK_GOTO(rc, cleanup); |
| 8136 | |
| 8137 | /* eval - just get boolean value actually */ |
| 8138 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 8139 | set_scnode_clear_ctx(&set2); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 8140 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8141 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8142 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8143 | set_fill_set(set, &set2); |
| 8144 | } |
| 8145 | } |
| 8146 | |
| 8147 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8148 | lyxp_set_free_content(&orig_set); |
| 8149 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8150 | return rc; |
| 8151 | } |
| 8152 | |
| 8153 | /** |
| 8154 | * @brief Evaluate OrExpr. Logs directly on error. |
| 8155 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8156 | * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8157 | * |
| 8158 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8159 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8160 | * @param[in] repeat How many times this expression is repeated. |
| 8161 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8162 | * @param[in] options XPath options. |
| 8163 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8164 | */ |
| 8165 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8166 | eval_or_expr(struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, int options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8167 | { |
| 8168 | LY_ERR rc; |
| 8169 | struct lyxp_set orig_set, set2; |
| 8170 | uint16_t i; |
| 8171 | |
| 8172 | assert(repeat); |
| 8173 | |
| 8174 | set_init(&orig_set, set); |
| 8175 | set_init(&set2, set); |
| 8176 | |
| 8177 | set_fill_set(&orig_set, set); |
| 8178 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8179 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8180 | LY_CHECK_GOTO(rc, cleanup); |
| 8181 | |
| 8182 | /* cast to boolean, we know that will be the final result */ |
| 8183 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 8184 | set_scnode_clear_ctx(set); |
| 8185 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8186 | lyxp_set_cast(set, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8187 | } |
| 8188 | |
| 8189 | /* ('or' AndExpr)* */ |
| 8190 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8191 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG); |
| 8192 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || set->val.bln ? "skipped" : "parsed"), |
| 8193 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 8194 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8195 | |
| 8196 | /* lazy evaluation */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8197 | if (!set || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) { |
| 8198 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8199 | LY_CHECK_GOTO(rc, cleanup); |
| 8200 | continue; |
| 8201 | } |
| 8202 | |
| 8203 | set_fill_set(&set2, &orig_set); |
| 8204 | /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one), |
| 8205 | * but it does not matter */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8206 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8207 | LY_CHECK_GOTO(rc, cleanup); |
| 8208 | |
| 8209 | /* eval - just get boolean value actually */ |
| 8210 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 8211 | set_scnode_clear_ctx(&set2); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 8212 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8213 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8214 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8215 | set_fill_set(set, &set2); |
| 8216 | } |
| 8217 | } |
| 8218 | |
| 8219 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8220 | lyxp_set_free_content(&orig_set); |
| 8221 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8222 | return rc; |
| 8223 | } |
| 8224 | |
| 8225 | /** |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8226 | * @brief Decide what expression is at the pointer @p tok_idx and evaluate it accordingly. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8227 | * |
| 8228 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8229 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8230 | * @param[in] etype Expression type to evaluate. |
| 8231 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8232 | * @param[in] options XPath options. |
| 8233 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8234 | */ |
| 8235 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8236 | eval_expr_select(struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype, struct lyxp_set *set, int options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8237 | { |
| 8238 | uint16_t i, count; |
| 8239 | enum lyxp_expr_type next_etype; |
| 8240 | LY_ERR rc; |
| 8241 | |
| 8242 | /* process operator repeats */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8243 | if (!exp->repeat[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8244 | next_etype = LYXP_EXPR_NONE; |
| 8245 | } else { |
| 8246 | /* find etype repeat */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8247 | for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8248 | |
| 8249 | /* select one-priority lower because etype expression called us */ |
| 8250 | if (i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8251 | next_etype = exp->repeat[*tok_idx][i - 1]; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8252 | /* count repeats for that expression */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8253 | for (count = 0; i && exp->repeat[*tok_idx][i - 1] == next_etype; ++count, --i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8254 | } else { |
| 8255 | next_etype = LYXP_EXPR_NONE; |
| 8256 | } |
| 8257 | } |
| 8258 | |
| 8259 | /* decide what expression are we parsing based on the repeat */ |
| 8260 | switch (next_etype) { |
| 8261 | case LYXP_EXPR_OR: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8262 | rc = eval_or_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8263 | break; |
| 8264 | case LYXP_EXPR_AND: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8265 | rc = eval_and_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8266 | break; |
| 8267 | case LYXP_EXPR_EQUALITY: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8268 | rc = eval_equality_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8269 | break; |
| 8270 | case LYXP_EXPR_RELATIONAL: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8271 | rc = eval_relational_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8272 | break; |
| 8273 | case LYXP_EXPR_ADDITIVE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8274 | rc = eval_additive_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8275 | break; |
| 8276 | case LYXP_EXPR_MULTIPLICATIVE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8277 | rc = eval_multiplicative_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8278 | break; |
| 8279 | case LYXP_EXPR_UNARY: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8280 | rc = eval_unary_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8281 | break; |
| 8282 | case LYXP_EXPR_UNION: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8283 | rc = eval_union_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8284 | break; |
| 8285 | case LYXP_EXPR_NONE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8286 | rc = eval_path_expr(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8287 | break; |
| 8288 | default: |
| 8289 | LOGINT_RET(set->ctx); |
| 8290 | } |
| 8291 | |
| 8292 | return rc; |
| 8293 | } |
| 8294 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8295 | /** |
| 8296 | * @brief Get root type. |
| 8297 | * |
| 8298 | * @param[in] ctx_node Context node. |
| 8299 | * @param[in] ctx_scnode Schema context node. |
| 8300 | * @param[in] options XPath options. |
| 8301 | * @return Root type. |
| 8302 | */ |
| 8303 | static enum lyxp_node_type |
| 8304 | lyxp_get_root_type(const struct lyd_node *ctx_node, const struct lysc_node *ctx_scnode, int options) |
| 8305 | { |
| 8306 | if (options & LYXP_SCNODE_ALL) { |
| 8307 | if (options & LYXP_SCNODE) { |
| 8308 | /* general root that can access everything */ |
| 8309 | return LYXP_NODE_ROOT; |
| 8310 | } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) { |
| 8311 | /* root context node can access only config data (because we said so, it is unspecified) */ |
| 8312 | return LYXP_NODE_ROOT_CONFIG; |
| 8313 | } else { |
| 8314 | return LYXP_NODE_ROOT; |
| 8315 | } |
| 8316 | } |
| 8317 | |
| 8318 | if (!ctx_node || (ctx_node->schema->flags & LYS_CONFIG_W)) { |
| 8319 | /* root context node can access only config data (because we said so, it is unspecified) */ |
| 8320 | return LYXP_NODE_ROOT_CONFIG; |
| 8321 | } |
| 8322 | |
| 8323 | return LYXP_NODE_ROOT; |
| 8324 | } |
| 8325 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8326 | LY_ERR |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 8327 | lyxp_eval(struct lyxp_expr *exp, LYD_FORMAT format, const struct lys_module *local_mod, const struct lyd_node *ctx_node, |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 8328 | enum lyxp_node_type ctx_node_type, const struct lyd_node *tree, struct lyxp_set *set, int options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8329 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8330 | uint16_t tok_idx = 0; |
| 8331 | const struct lyd_node *real_ctx_node; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8332 | LY_ERR rc; |
| 8333 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8334 | LY_CHECK_ARG_RET(NULL, exp, local_mod, ctx_node, set, LY_EINVAL); |
| 8335 | |
| 8336 | if ((ctx_node_type == LYXP_NODE_ROOT) || (ctx_node_type == LYXP_NODE_ROOT_CONFIG)) { |
| 8337 | /* we always need some context node because it is used for resolving unqualified names */ |
| 8338 | real_ctx_node = NULL; |
| 8339 | } else { |
| 8340 | real_ctx_node = ctx_node; |
| 8341 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8342 | |
| 8343 | /* prepare set for evaluation */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8344 | tok_idx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8345 | memset(set, 0, sizeof *set); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8346 | set->type = LYXP_SET_NODE_SET; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8347 | set_insert_node(set, (struct lyd_node *)real_ctx_node, 0, ctx_node_type, 0); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 8348 | set->ctx = local_mod->ctx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8349 | set->ctx_node = ctx_node; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8350 | set->root_type = lyxp_get_root_type(real_ctx_node, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8351 | set->local_mod = local_mod; |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 8352 | set->tree = tree; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8353 | set->format = format; |
| 8354 | |
| 8355 | /* evaluate */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8356 | rc = eval_expr_select(exp, &tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8357 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8358 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8359 | } |
| 8360 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8361 | return rc; |
| 8362 | } |
| 8363 | |
| 8364 | #if 0 |
| 8365 | |
| 8366 | /* full xml printing of set elements, not used currently */ |
| 8367 | |
| 8368 | void |
| 8369 | lyxp_set_print_xml(FILE *f, struct lyxp_set *set) |
| 8370 | { |
| 8371 | uint32_t i; |
| 8372 | char *str_num; |
| 8373 | struct lyout out; |
| 8374 | |
| 8375 | memset(&out, 0, sizeof out); |
| 8376 | |
| 8377 | out.type = LYOUT_STREAM; |
| 8378 | out.method.f = f; |
| 8379 | |
| 8380 | switch (set->type) { |
| 8381 | case LYXP_SET_EMPTY: |
| 8382 | ly_print(&out, "Empty XPath set\n\n"); |
| 8383 | break; |
| 8384 | case LYXP_SET_BOOLEAN: |
| 8385 | ly_print(&out, "Boolean XPath set:\n"); |
| 8386 | ly_print(&out, "%s\n\n", set->value.bool ? "true" : "false"); |
| 8387 | break; |
| 8388 | case LYXP_SET_STRING: |
| 8389 | ly_print(&out, "String XPath set:\n"); |
| 8390 | ly_print(&out, "\"%s\"\n\n", set->value.str); |
| 8391 | break; |
| 8392 | case LYXP_SET_NUMBER: |
| 8393 | ly_print(&out, "Number XPath set:\n"); |
| 8394 | |
| 8395 | if (isnan(set->value.num)) { |
| 8396 | str_num = strdup("NaN"); |
| 8397 | } else if ((set->value.num == 0) || (set->value.num == -0.0f)) { |
| 8398 | str_num = strdup("0"); |
| 8399 | } else if (isinf(set->value.num) && !signbit(set->value.num)) { |
| 8400 | str_num = strdup("Infinity"); |
| 8401 | } else if (isinf(set->value.num) && signbit(set->value.num)) { |
| 8402 | str_num = strdup("-Infinity"); |
| 8403 | } else if ((long long)set->value.num == set->value.num) { |
| 8404 | if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) { |
| 8405 | str_num = NULL; |
| 8406 | } |
| 8407 | } else { |
| 8408 | if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) { |
| 8409 | str_num = NULL; |
| 8410 | } |
| 8411 | } |
| 8412 | if (!str_num) { |
| 8413 | LOGMEM; |
| 8414 | return; |
| 8415 | } |
| 8416 | ly_print(&out, "%s\n\n", str_num); |
| 8417 | free(str_num); |
| 8418 | break; |
| 8419 | case LYXP_SET_NODE_SET: |
| 8420 | ly_print(&out, "Node XPath set:\n"); |
| 8421 | |
| 8422 | for (i = 0; i < set->used; ++i) { |
| 8423 | ly_print(&out, "%d. ", i + 1); |
| 8424 | switch (set->node_type[i]) { |
| 8425 | case LYXP_NODE_ROOT_ALL: |
| 8426 | ly_print(&out, "ROOT all\n\n"); |
| 8427 | break; |
| 8428 | case LYXP_NODE_ROOT_CONFIG: |
| 8429 | ly_print(&out, "ROOT config\n\n"); |
| 8430 | break; |
| 8431 | case LYXP_NODE_ROOT_STATE: |
| 8432 | ly_print(&out, "ROOT state\n\n"); |
| 8433 | break; |
| 8434 | case LYXP_NODE_ROOT_NOTIF: |
| 8435 | ly_print(&out, "ROOT notification \"%s\"\n\n", set->value.nodes[i]->schema->name); |
| 8436 | break; |
| 8437 | case LYXP_NODE_ROOT_RPC: |
| 8438 | ly_print(&out, "ROOT rpc \"%s\"\n\n", set->value.nodes[i]->schema->name); |
| 8439 | break; |
| 8440 | case LYXP_NODE_ROOT_OUTPUT: |
| 8441 | ly_print(&out, "ROOT output \"%s\"\n\n", set->value.nodes[i]->schema->name); |
| 8442 | break; |
| 8443 | case LYXP_NODE_ELEM: |
| 8444 | ly_print(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name); |
| 8445 | xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT); |
| 8446 | ly_print(&out, "\n"); |
| 8447 | break; |
| 8448 | case LYXP_NODE_TEXT: |
| 8449 | ly_print(&out, "TEXT \"%s\"\n\n", ((struct lyd_node_leaf_list *)set->value.nodes[i])->value_str); |
| 8450 | break; |
| 8451 | case LYXP_NODE_ATTR: |
| 8452 | ly_print(&out, "ATTR \"%s\" = \"%s\"\n\n", set->value.attrs[i]->name, set->value.attrs[i]->value); |
| 8453 | break; |
| 8454 | } |
| 8455 | } |
| 8456 | break; |
| 8457 | } |
| 8458 | } |
| 8459 | |
| 8460 | #endif |
| 8461 | |
| 8462 | LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8463 | lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8464 | { |
| 8465 | long double num; |
| 8466 | char *str; |
| 8467 | LY_ERR rc; |
| 8468 | |
| 8469 | if (!set || (set->type == target)) { |
| 8470 | return LY_SUCCESS; |
| 8471 | } |
| 8472 | |
| 8473 | /* it's not possible to convert anything into a node set */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8474 | assert(target != LYXP_SET_NODE_SET); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8475 | |
| 8476 | if (set->type == LYXP_SET_SCNODE_SET) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8477 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8478 | return LY_EINVAL; |
| 8479 | } |
| 8480 | |
| 8481 | /* to STRING */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8482 | if ((target == LYXP_SET_STRING) || ((target == LYXP_SET_NUMBER) && (set->type == LYXP_SET_NODE_SET))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8483 | switch (set->type) { |
| 8484 | case LYXP_SET_NUMBER: |
| 8485 | if (isnan(set->val.num)) { |
| 8486 | set->val.str = strdup("NaN"); |
| 8487 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 8488 | } else if ((set->val.num == 0) || (set->val.num == -0.0f)) { |
| 8489 | set->val.str = strdup("0"); |
| 8490 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 8491 | } else if (isinf(set->val.num) && !signbit(set->val.num)) { |
| 8492 | set->val.str = strdup("Infinity"); |
| 8493 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 8494 | } else if (isinf(set->val.num) && signbit(set->val.num)) { |
| 8495 | set->val.str = strdup("-Infinity"); |
| 8496 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 8497 | } else if ((long long)set->val.num == set->val.num) { |
| 8498 | if (asprintf(&str, "%lld", (long long)set->val.num) == -1) { |
| 8499 | LOGMEM_RET(set->ctx); |
| 8500 | } |
| 8501 | set->val.str = str; |
| 8502 | } else { |
| 8503 | if (asprintf(&str, "%03.1Lf", set->val.num) == -1) { |
| 8504 | LOGMEM_RET(set->ctx); |
| 8505 | } |
| 8506 | set->val.str = str; |
| 8507 | } |
| 8508 | break; |
| 8509 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8510 | if (set->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8511 | set->val.str = strdup("true"); |
| 8512 | } else { |
| 8513 | set->val.str = strdup("false"); |
| 8514 | } |
| 8515 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM); |
| 8516 | break; |
| 8517 | case LYXP_SET_NODE_SET: |
| 8518 | assert(set->used); |
| 8519 | |
| 8520 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8521 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8522 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8523 | rc = cast_node_set_to_string(set, &str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8524 | LY_CHECK_RET(rc); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8525 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8526 | set->val.str = str; |
| 8527 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8528 | default: |
| 8529 | LOGINT_RET(set->ctx); |
| 8530 | } |
| 8531 | set->type = LYXP_SET_STRING; |
| 8532 | } |
| 8533 | |
| 8534 | /* to NUMBER */ |
| 8535 | if (target == LYXP_SET_NUMBER) { |
| 8536 | switch (set->type) { |
| 8537 | case LYXP_SET_STRING: |
| 8538 | num = cast_string_to_number(set->val.str); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8539 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8540 | set->val.num = num; |
| 8541 | break; |
| 8542 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8543 | if (set->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8544 | set->val.num = 1; |
| 8545 | } else { |
| 8546 | set->val.num = 0; |
| 8547 | } |
| 8548 | break; |
| 8549 | default: |
| 8550 | LOGINT_RET(set->ctx); |
| 8551 | } |
| 8552 | set->type = LYXP_SET_NUMBER; |
| 8553 | } |
| 8554 | |
| 8555 | /* to BOOLEAN */ |
| 8556 | if (target == LYXP_SET_BOOLEAN) { |
| 8557 | switch (set->type) { |
| 8558 | case LYXP_SET_NUMBER: |
| 8559 | if ((set->val.num == 0) || (set->val.num == -0.0f) || isnan(set->val.num)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8560 | set->val.bln = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8561 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8562 | set->val.bln = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8563 | } |
| 8564 | break; |
| 8565 | case LYXP_SET_STRING: |
| 8566 | if (set->val.str[0]) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8567 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8568 | set->val.bln = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8569 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8570 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8571 | set->val.bln = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8572 | } |
| 8573 | break; |
| 8574 | case LYXP_SET_NODE_SET: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8575 | if (set->used) { |
| 8576 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8577 | set->val.bln = 1; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8578 | } else { |
| 8579 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8580 | set->val.bln = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8581 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8582 | break; |
| 8583 | default: |
| 8584 | LOGINT_RET(set->ctx); |
| 8585 | } |
| 8586 | set->type = LYXP_SET_BOOLEAN; |
| 8587 | } |
| 8588 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8589 | return LY_SUCCESS; |
| 8590 | } |
| 8591 | |
| 8592 | LY_ERR |
| 8593 | lyxp_atomize(struct lyxp_expr *exp, LYD_FORMAT format, const struct lys_module *local_mod, const struct lysc_node *ctx_scnode, |
| 8594 | enum lyxp_node_type ctx_scnode_type, struct lyxp_set *set, int options) |
| 8595 | { |
| 8596 | struct ly_ctx *ctx; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8597 | const struct lysc_node *real_ctx_scnode; |
| 8598 | uint16_t tok_idx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8599 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8600 | LY_CHECK_ARG_RET(NULL, exp, local_mod, ctx_scnode, set, LY_EINVAL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8601 | |
| 8602 | ctx = local_mod->ctx; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8603 | if ((ctx_scnode_type == LYXP_NODE_ROOT) || (ctx_scnode_type == LYXP_NODE_ROOT_CONFIG)) { |
| 8604 | /* we always need some context node because it is used for resolving unqualified names */ |
| 8605 | real_ctx_scnode = NULL; |
| 8606 | } else { |
| 8607 | real_ctx_scnode = ctx_scnode; |
| 8608 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8609 | |
| 8610 | /* prepare set for evaluation */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8611 | tok_idx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8612 | memset(set, 0, sizeof *set); |
| 8613 | set->type = LYXP_SET_SCNODE_SET; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8614 | lyxp_set_scnode_insert_node(set, real_ctx_scnode, ctx_scnode_type); |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 8615 | set->val.scnodes[0].in_ctx = -2; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8616 | set->ctx = ctx; |
| 8617 | set->ctx_scnode = ctx_scnode; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8618 | set->root_type = lyxp_get_root_type(NULL, real_ctx_scnode, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8619 | set->local_mod = local_mod; |
| 8620 | set->format = format; |
| 8621 | |
| 8622 | /* evaluate */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8623 | return eval_expr_select(exp, &tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8624 | } |