Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 1 | /** |
| 2 | * @file xpath.c |
| 3 | * @author Michal Vasko <mvasko@cesnet.cz> |
| 4 | * @brief YANG XPath evaluation functions |
| 5 | * |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6 | * Copyright (c) 2015 - 2020 CESNET, z.s.p.o. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 7 | * |
| 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * https://opensource.org/licenses/BSD-3-Clause |
| 13 | */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 14 | #define _GNU_SOURCE |
| 15 | |
| 16 | /* needed by libmath functions isfinite(), isinf(), isnan(), signbit(), ... */ |
| 17 | #define _ISOC99_SOURCE |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 18 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 19 | #include "xpath.h" |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 20 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 21 | #include <assert.h> |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 22 | #include <ctype.h> |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 23 | #include <errno.h> |
| 24 | #include <limits.h> |
| 25 | #include <math.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 26 | #include <stdint.h> |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 27 | #include <stdio.h> |
| 28 | #include <stdlib.h> |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 29 | #include <string.h> |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 30 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 31 | #include "common.h" |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 32 | #include "compat.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 33 | #include "context.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 34 | #include "dict.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 35 | #include "hash_table.h" |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 36 | #include "parser_data.h" |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 37 | #include "path.h" |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 38 | #include "plugins_types.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 39 | #include "printer.h" |
| 40 | #include "printer_data.h" |
| 41 | #include "tree.h" |
| 42 | #include "tree_data_internal.h" |
| 43 | #include "tree_schema_internal.h" |
| 44 | #include "xml.h" |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 45 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 46 | static LY_ERR reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx); |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 47 | static LY_ERR eval_expr_select(struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype, struct lyxp_set *set, uint32_t options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 48 | |
| 49 | /** |
| 50 | * @brief Print the type of an XPath \p set. |
| 51 | * |
| 52 | * @param[in] set Set to use. |
| 53 | * @return Set type string. |
| 54 | */ |
| 55 | static const char * |
| 56 | print_set_type(struct lyxp_set *set) |
| 57 | { |
| 58 | switch (set->type) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 59 | case LYXP_SET_NODE_SET: |
| 60 | return "node set"; |
| 61 | case LYXP_SET_SCNODE_SET: |
| 62 | return "schema node set"; |
| 63 | case LYXP_SET_BOOLEAN: |
| 64 | return "boolean"; |
| 65 | case LYXP_SET_NUMBER: |
| 66 | return "number"; |
| 67 | case LYXP_SET_STRING: |
| 68 | return "string"; |
| 69 | } |
| 70 | |
| 71 | return NULL; |
| 72 | } |
| 73 | |
Michal Vasko | 24cddf8 | 2020-06-01 08:17:01 +0200 | [diff] [blame] | 74 | const char * |
| 75 | lyxp_print_token(enum lyxp_token tok) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 76 | { |
| 77 | switch (tok) { |
| 78 | case LYXP_TOKEN_PAR1: |
| 79 | return "("; |
| 80 | case LYXP_TOKEN_PAR2: |
| 81 | return ")"; |
| 82 | case LYXP_TOKEN_BRACK1: |
| 83 | return "["; |
| 84 | case LYXP_TOKEN_BRACK2: |
| 85 | return "]"; |
| 86 | case LYXP_TOKEN_DOT: |
| 87 | return "."; |
| 88 | case LYXP_TOKEN_DDOT: |
| 89 | return ".."; |
| 90 | case LYXP_TOKEN_AT: |
| 91 | return "@"; |
| 92 | case LYXP_TOKEN_COMMA: |
| 93 | return ","; |
| 94 | case LYXP_TOKEN_NAMETEST: |
| 95 | return "NameTest"; |
| 96 | case LYXP_TOKEN_NODETYPE: |
| 97 | return "NodeType"; |
| 98 | case LYXP_TOKEN_FUNCNAME: |
| 99 | return "FunctionName"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 100 | case LYXP_TOKEN_OPER_LOG: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 101 | return "Operator(Logic)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 102 | case LYXP_TOKEN_OPER_EQUAL: |
| 103 | return "Operator(Equal)"; |
| 104 | case LYXP_TOKEN_OPER_NEQUAL: |
| 105 | return "Operator(Non-equal)"; |
| 106 | case LYXP_TOKEN_OPER_COMP: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 107 | return "Operator(Comparison)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 108 | case LYXP_TOKEN_OPER_MATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 109 | return "Operator(Math)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 110 | case LYXP_TOKEN_OPER_UNI: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 111 | return "Operator(Union)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 112 | case LYXP_TOKEN_OPER_PATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 113 | return "Operator(Path)"; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 114 | case LYXP_TOKEN_OPER_RPATH: |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 115 | return "Operator(Recursive Path)"; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 116 | case LYXP_TOKEN_LITERAL: |
| 117 | return "Literal"; |
| 118 | case LYXP_TOKEN_NUMBER: |
| 119 | return "Number"; |
| 120 | default: |
| 121 | LOGINT(NULL); |
| 122 | return ""; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * @brief Print the whole expression \p exp to debug output. |
| 128 | * |
| 129 | * @param[in] exp Expression to use. |
| 130 | */ |
| 131 | static void |
| 132 | print_expr_struct_debug(struct lyxp_expr *exp) |
| 133 | { |
| 134 | uint16_t i, j; |
| 135 | char tmp[128]; |
| 136 | |
| 137 | if (!exp || (ly_log_level < LY_LLDBG)) { |
| 138 | return; |
| 139 | } |
| 140 | |
| 141 | LOGDBG(LY_LDGXPATH, "expression \"%s\":", exp->expr); |
| 142 | for (i = 0; i < exp->used; ++i) { |
Michal Vasko | 24cddf8 | 2020-06-01 08:17:01 +0200 | [diff] [blame] | 143 | sprintf(tmp, "\ttoken %s, in expression \"%.*s\"", lyxp_print_token(exp->tokens[i]), exp->tok_len[i], |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 144 | &exp->expr[exp->tok_pos[i]]); |
| 145 | if (exp->repeat[i]) { |
| 146 | sprintf(tmp + strlen(tmp), " (repeat %d", exp->repeat[i][0]); |
| 147 | for (j = 1; exp->repeat[i][j]; ++j) { |
| 148 | sprintf(tmp + strlen(tmp), ", %d", exp->repeat[i][j]); |
| 149 | } |
| 150 | strcat(tmp, ")"); |
| 151 | } |
| 152 | LOGDBG(LY_LDGXPATH, tmp); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | #ifndef NDEBUG |
| 157 | |
| 158 | /** |
| 159 | * @brief Print XPath set content to debug output. |
| 160 | * |
| 161 | * @param[in] set Set to print. |
| 162 | */ |
| 163 | static void |
| 164 | print_set_debug(struct lyxp_set *set) |
| 165 | { |
| 166 | uint32_t i; |
| 167 | char *str; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 168 | struct lyxp_set_node *item; |
| 169 | struct lyxp_set_scnode *sitem; |
| 170 | |
| 171 | if (ly_log_level < LY_LLDBG) { |
| 172 | return; |
| 173 | } |
| 174 | |
| 175 | switch (set->type) { |
| 176 | case LYXP_SET_NODE_SET: |
| 177 | LOGDBG(LY_LDGXPATH, "set NODE SET:"); |
| 178 | for (i = 0; i < set->used; ++i) { |
| 179 | item = &set->val.nodes[i]; |
| 180 | |
| 181 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 182 | case LYXP_NODE_NONE: |
| 183 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): NONE", i + 1, item->pos); |
| 184 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 185 | case LYXP_NODE_ROOT: |
| 186 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT", i + 1, item->pos); |
| 187 | break; |
| 188 | case LYXP_NODE_ROOT_CONFIG: |
| 189 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT CONFIG", i + 1, item->pos); |
| 190 | break; |
| 191 | case LYXP_NODE_ELEM: |
| 192 | if ((item->node->schema->nodetype == LYS_LIST) |
| 193 | && (((struct lyd_node_inner *)item->node)->child->schema->nodetype == LYS_LEAF)) { |
| 194 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (1st child val: %s)", i + 1, item->pos, |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 195 | item->node->schema->name, LYD_CANON_VALUE(lyd_node_children(item->node, 0))); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 196 | } else if (((struct lyd_node_inner *)item->node)->schema->nodetype == LYS_LEAFLIST) { |
| 197 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (val: %s)", i + 1, item->pos, |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 198 | item->node->schema->name, LYD_CANON_VALUE(item->node)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 199 | } else { |
| 200 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s", i + 1, item->pos, item->node->schema->name); |
| 201 | } |
| 202 | break; |
| 203 | case LYXP_NODE_TEXT: |
| 204 | if (item->node->schema->nodetype & LYS_ANYDATA) { |
| 205 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT <%s>", i + 1, item->pos, |
| 206 | item->node->schema->nodetype == LYS_ANYXML ? "anyxml" : "anydata"); |
| 207 | } else { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 208 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT %s", i + 1, item->pos, LYD_CANON_VALUE(item->node)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 209 | } |
| 210 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 211 | case LYXP_NODE_META: |
| 212 | LOGDBG(LY_LDGXPATH, "\t%d (pos %u): META %s = %s", i + 1, item->pos, set->val.meta[i].meta->name, |
| 213 | set->val.meta[i].meta->value); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 214 | break; |
| 215 | } |
| 216 | } |
| 217 | break; |
| 218 | |
| 219 | case LYXP_SET_SCNODE_SET: |
| 220 | LOGDBG(LY_LDGXPATH, "set SCNODE SET:"); |
| 221 | for (i = 0; i < set->used; ++i) { |
| 222 | sitem = &set->val.scnodes[i]; |
| 223 | |
| 224 | switch (sitem->type) { |
| 225 | case LYXP_NODE_ROOT: |
| 226 | LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT", i + 1, sitem->in_ctx); |
| 227 | break; |
| 228 | case LYXP_NODE_ROOT_CONFIG: |
| 229 | LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT CONFIG", i + 1, sitem->in_ctx); |
| 230 | break; |
| 231 | case LYXP_NODE_ELEM: |
| 232 | LOGDBG(LY_LDGXPATH, "\t%d (%u): ELEM %s", i + 1, sitem->in_ctx, sitem->scnode->name); |
| 233 | break; |
| 234 | default: |
| 235 | LOGINT(NULL); |
| 236 | break; |
| 237 | } |
| 238 | } |
| 239 | break; |
| 240 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 241 | case LYXP_SET_BOOLEAN: |
| 242 | LOGDBG(LY_LDGXPATH, "set BOOLEAN"); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 243 | LOGDBG(LY_LDGXPATH, "\t%s", (set->val.bln ? "true" : "false")); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 244 | break; |
| 245 | |
| 246 | case LYXP_SET_STRING: |
| 247 | LOGDBG(LY_LDGXPATH, "set STRING"); |
| 248 | LOGDBG(LY_LDGXPATH, "\t%s", set->val.str); |
| 249 | break; |
| 250 | |
| 251 | case LYXP_SET_NUMBER: |
| 252 | LOGDBG(LY_LDGXPATH, "set NUMBER"); |
| 253 | |
| 254 | if (isnan(set->val.num)) { |
| 255 | str = strdup("NaN"); |
| 256 | } else if ((set->val.num == 0) || (set->val.num == -0.0f)) { |
| 257 | str = strdup("0"); |
| 258 | } else if (isinf(set->val.num) && !signbit(set->val.num)) { |
| 259 | str = strdup("Infinity"); |
| 260 | } else if (isinf(set->val.num) && signbit(set->val.num)) { |
| 261 | str = strdup("-Infinity"); |
| 262 | } else if ((long long)set->val.num == set->val.num) { |
| 263 | if (asprintf(&str, "%lld", (long long)set->val.num) == -1) { |
| 264 | str = NULL; |
| 265 | } |
| 266 | } else { |
| 267 | if (asprintf(&str, "%03.1Lf", set->val.num) == -1) { |
| 268 | str = NULL; |
| 269 | } |
| 270 | } |
| 271 | LY_CHECK_ERR_RET(!str, LOGMEM(NULL), ); |
| 272 | |
| 273 | LOGDBG(LY_LDGXPATH, "\t%s", str); |
| 274 | free(str); |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | #endif |
| 279 | |
| 280 | /** |
| 281 | * @brief Realloc the string \p str. |
| 282 | * |
| 283 | * @param[in] ctx libyang context for logging. |
| 284 | * @param[in] needed How much free space is required. |
| 285 | * @param[in,out] str Pointer to the string to use. |
| 286 | * @param[in,out] used Used bytes in \p str. |
| 287 | * @param[in,out] size Allocated bytes in \p str. |
| 288 | * @return LY_ERR |
| 289 | */ |
| 290 | static LY_ERR |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 291 | 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] | 292 | { |
| 293 | if (*size - *used < needed) { |
| 294 | do { |
| 295 | if ((UINT16_MAX - *size) < LYXP_STRING_CAST_SIZE_STEP) { |
| 296 | LOGERR(ctx, LY_EINVAL, "XPath string length limit (%u) reached.", UINT16_MAX); |
| 297 | return LY_EINVAL; |
| 298 | } |
| 299 | *size += LYXP_STRING_CAST_SIZE_STEP; |
| 300 | } while (*size - *used < needed); |
| 301 | *str = ly_realloc(*str, *size * sizeof(char)); |
| 302 | LY_CHECK_ERR_RET(!(*str), LOGMEM(ctx), LY_EMEM); |
| 303 | } |
| 304 | |
| 305 | return LY_SUCCESS; |
| 306 | } |
| 307 | |
| 308 | /** |
| 309 | * @brief Cast nodes recursively to one string @p str. |
| 310 | * |
| 311 | * @param[in] node Node to cast. |
| 312 | * @param[in] fake_cont Whether to put the data into a "fake" container. |
| 313 | * @param[in] root_type Type of the XPath root. |
| 314 | * @param[in] indent Current indent. |
| 315 | * @param[in,out] str Resulting string. |
| 316 | * @param[in,out] used Used bytes in @p str. |
| 317 | * @param[in,out] size Allocated bytes in @p str. |
| 318 | * @return LY_ERR |
| 319 | */ |
| 320 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 321 | cast_string_recursive(const struct lyd_node *node, uint8_t fake_cont, enum lyxp_node_type root_type, uint16_t indent, char **str, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 322 | uint16_t *used, uint16_t *size) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 323 | { |
Radek Krejci | 7f769d7 | 2020-07-11 23:13:56 +0200 | [diff] [blame] | 324 | char *buf, *line, *ptr = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 325 | const char *value_str; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 326 | const struct lyd_node *child; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 327 | struct lyd_node *tree; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 328 | struct lyd_node_any *any; |
| 329 | LY_ERR rc; |
| 330 | |
| 331 | if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) { |
| 332 | return LY_SUCCESS; |
| 333 | } |
| 334 | |
| 335 | if (fake_cont) { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 336 | rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 337 | LY_CHECK_RET(rc); |
| 338 | strcpy(*str + (*used - 1), "\n"); |
| 339 | ++(*used); |
| 340 | |
| 341 | ++indent; |
| 342 | } |
| 343 | |
| 344 | switch (node->schema->nodetype) { |
| 345 | case LYS_CONTAINER: |
| 346 | case LYS_LIST: |
| 347 | case LYS_RPC: |
| 348 | case LYS_NOTIF: |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 349 | rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 350 | LY_CHECK_RET(rc); |
| 351 | strcpy(*str + (*used - 1), "\n"); |
| 352 | ++(*used); |
| 353 | |
Michal Vasko | 5bfd4be | 2020-06-23 13:26:19 +0200 | [diff] [blame] | 354 | for (child = lyd_node_children(node, 0); child; child = child->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 355 | rc = cast_string_recursive(child, 0, root_type, indent + 1, str, used, size); |
| 356 | LY_CHECK_RET(rc); |
| 357 | } |
| 358 | |
| 359 | break; |
| 360 | |
| 361 | case LYS_LEAF: |
| 362 | case LYS_LEAFLIST: |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 363 | value_str = LYD_CANON_VALUE(node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 364 | |
| 365 | /* print indent */ |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 366 | LY_CHECK_RET(cast_string_realloc(LYD_CTX(node), indent * 2 + strlen(value_str) + 1, str, used, size)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 367 | memset(*str + (*used - 1), ' ', indent * 2); |
| 368 | *used += indent * 2; |
| 369 | |
| 370 | /* print value */ |
| 371 | if (*used == 1) { |
| 372 | sprintf(*str + (*used - 1), "%s", value_str); |
| 373 | *used += strlen(value_str); |
| 374 | } else { |
| 375 | sprintf(*str + (*used - 1), "%s\n", value_str); |
| 376 | *used += strlen(value_str) + 1; |
| 377 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 378 | |
| 379 | break; |
| 380 | |
| 381 | case LYS_ANYXML: |
| 382 | case LYS_ANYDATA: |
| 383 | any = (struct lyd_node_any *)node; |
| 384 | if (!(void *)any->value.tree) { |
| 385 | /* no content */ |
| 386 | buf = strdup(""); |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 387 | LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_CTX(node)), LY_EMEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 388 | } else { |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 389 | struct ly_out *out; |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 390 | |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 391 | if (any->value_type == LYD_ANYDATA_LYB) { |
| 392 | /* try to parse it into a data tree */ |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 393 | if (lyd_parse_data_mem((struct ly_ctx *)LYD_CTX(node), any->value.mem, LYD_LYB, LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, &tree) == LY_SUCCESS) { |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 394 | /* successfully parsed */ |
| 395 | free(any->value.mem); |
| 396 | any->value.tree = tree; |
| 397 | any->value_type = LYD_ANYDATA_DATATREE; |
| 398 | } |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 399 | /* error is covered by the following switch where LYD_ANYDATA_LYB causes failure */ |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 400 | } |
| 401 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 402 | switch (any->value_type) { |
| 403 | case LYD_ANYDATA_STRING: |
| 404 | case LYD_ANYDATA_XML: |
| 405 | case LYD_ANYDATA_JSON: |
| 406 | buf = strdup(any->value.json); |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 407 | LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_CTX(node)), LY_EMEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 408 | break; |
| 409 | case LYD_ANYDATA_DATATREE: |
Radek Krejci | 84ce7b1 | 2020-06-11 17:28:25 +0200 | [diff] [blame] | 410 | LY_CHECK_RET(ly_out_new_memory(&buf, 0, &out)); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 411 | rc = lyd_print_all(out, any->value.tree, LYD_XML, 0); |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 412 | ly_out_free(out, NULL, 0); |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 413 | LY_CHECK_RET(rc < 0, -rc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 414 | break; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 415 | case LYD_ANYDATA_LYB: |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 416 | LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot convert LYB anydata into string."); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 417 | return LY_EINVAL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 418 | } |
| 419 | } |
| 420 | |
| 421 | line = strtok_r(buf, "\n", &ptr); |
| 422 | do { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 423 | rc = cast_string_realloc(LYD_CTX(node), indent * 2 + strlen(line) + 1, str, used, size); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 424 | if (rc != LY_SUCCESS) { |
| 425 | free(buf); |
| 426 | return rc; |
| 427 | } |
| 428 | memset(*str + (*used - 1), ' ', indent * 2); |
| 429 | *used += indent * 2; |
| 430 | |
| 431 | strcpy(*str + (*used - 1), line); |
| 432 | *used += strlen(line); |
| 433 | |
| 434 | strcpy(*str + (*used - 1), "\n"); |
| 435 | *used += 1; |
| 436 | } while ((line = strtok_r(NULL, "\n", &ptr))); |
| 437 | |
| 438 | free(buf); |
| 439 | break; |
| 440 | |
| 441 | default: |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 442 | LOGINT_RET(LYD_CTX(node)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | if (fake_cont) { |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 446 | rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 447 | LY_CHECK_RET(rc); |
| 448 | strcpy(*str + (*used - 1), "\n"); |
| 449 | ++(*used); |
| 450 | |
| 451 | --indent; |
| 452 | } |
| 453 | |
| 454 | return LY_SUCCESS; |
| 455 | } |
| 456 | |
| 457 | /** |
| 458 | * @brief Cast an element into a string. |
| 459 | * |
| 460 | * @param[in] node Node to cast. |
| 461 | * @param[in] fake_cont Whether to put the data into a "fake" container. |
| 462 | * @param[in] root_type Type of the XPath root. |
| 463 | * @param[out] str Element cast to dynamically-allocated string. |
| 464 | * @return LY_ERR |
| 465 | */ |
| 466 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 467 | cast_string_elem(struct lyd_node *node, uint8_t fake_cont, enum lyxp_node_type root_type, char **str) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 468 | { |
| 469 | uint16_t used, size; |
| 470 | LY_ERR rc; |
| 471 | |
| 472 | *str = malloc(LYXP_STRING_CAST_SIZE_START * sizeof(char)); |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 473 | LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_CTX(node)), LY_EMEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 474 | (*str)[0] = '\0'; |
| 475 | used = 1; |
| 476 | size = LYXP_STRING_CAST_SIZE_START; |
| 477 | |
| 478 | rc = cast_string_recursive(node, fake_cont, root_type, 0, str, &used, &size); |
| 479 | if (rc != LY_SUCCESS) { |
| 480 | free(*str); |
| 481 | return rc; |
| 482 | } |
| 483 | |
| 484 | if (size > used) { |
| 485 | *str = ly_realloc(*str, used * sizeof(char)); |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 486 | LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_CTX(node)), LY_EMEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 487 | } |
| 488 | return LY_SUCCESS; |
| 489 | } |
| 490 | |
| 491 | /** |
| 492 | * @brief Cast a LYXP_SET_NODE_SET set into a string. |
| 493 | * Context position aware. |
| 494 | * |
| 495 | * @param[in] set Set to cast. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 496 | * @param[out] str Cast dynamically-allocated string. |
| 497 | * @return LY_ERR |
| 498 | */ |
| 499 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 500 | cast_node_set_to_string(struct lyxp_set *set, char **str) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 501 | { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 502 | switch (set->val.nodes[0].type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 503 | case LYXP_NODE_NONE: |
| 504 | /* invalid */ |
| 505 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 506 | case LYXP_NODE_ROOT: |
| 507 | case LYXP_NODE_ROOT_CONFIG: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 508 | 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] | 509 | case LYXP_NODE_ELEM: |
| 510 | case LYXP_NODE_TEXT: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 511 | 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] | 512 | case LYXP_NODE_META: |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 513 | *str = strdup(set->val.meta[0].meta->value.canonical); |
| 514 | if (!*str) { |
| 515 | LOGMEM_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 516 | } |
| 517 | return LY_SUCCESS; |
| 518 | } |
| 519 | |
| 520 | LOGINT_RET(set->ctx); |
| 521 | } |
| 522 | |
| 523 | /** |
| 524 | * @brief Cast a string into an XPath number. |
| 525 | * |
| 526 | * @param[in] str String to use. |
| 527 | * @return Cast number. |
| 528 | */ |
| 529 | static long double |
| 530 | cast_string_to_number(const char *str) |
| 531 | { |
| 532 | long double num; |
| 533 | char *ptr; |
| 534 | |
| 535 | errno = 0; |
| 536 | num = strtold(str, &ptr); |
| 537 | if (errno || *ptr) { |
| 538 | num = NAN; |
| 539 | } |
| 540 | return num; |
| 541 | } |
| 542 | |
| 543 | /** |
| 544 | * @brief Callback for checking value equality. |
| 545 | * |
| 546 | * @param[in] val1_p First value. |
| 547 | * @param[in] val2_p Second value. |
| 548 | * @param[in] mod Whether hash table is being modified. |
| 549 | * @param[in] cb_data Callback data. |
| 550 | * @return 0 if not equal, non-zero if equal. |
| 551 | */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 552 | static uint8_t |
| 553 | set_values_equal_cb(void *val1_p, void *val2_p, uint8_t UNUSED(mod), void *UNUSED(cb_data)) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 554 | { |
| 555 | struct lyxp_set_hash_node *val1, *val2; |
| 556 | |
| 557 | val1 = (struct lyxp_set_hash_node *)val1_p; |
| 558 | val2 = (struct lyxp_set_hash_node *)val2_p; |
| 559 | |
| 560 | if ((val1->node == val2->node) && (val1->type == val2->type)) { |
| 561 | return 1; |
| 562 | } |
| 563 | |
| 564 | return 0; |
| 565 | } |
| 566 | |
| 567 | /** |
| 568 | * @brief Insert node and its hash into set. |
| 569 | * |
| 570 | * @param[in] set et to insert to. |
| 571 | * @param[in] node Node with hash. |
| 572 | * @param[in] type Node type. |
| 573 | */ |
| 574 | static void |
| 575 | set_insert_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type) |
| 576 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 577 | LY_ERR r; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 578 | uint32_t i, hash; |
| 579 | struct lyxp_set_hash_node hnode; |
| 580 | |
| 581 | if (!set->ht && (set->used >= LYD_HT_MIN_ITEMS)) { |
| 582 | /* create hash table and add all the nodes */ |
| 583 | set->ht = lyht_new(1, sizeof(struct lyxp_set_hash_node), set_values_equal_cb, NULL, 1); |
| 584 | for (i = 0; i < set->used; ++i) { |
| 585 | hnode.node = set->val.nodes[i].node; |
| 586 | hnode.type = set->val.nodes[i].type; |
| 587 | |
| 588 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 589 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 590 | hash = dict_hash_multi(hash, NULL, 0); |
| 591 | |
| 592 | r = lyht_insert(set->ht, &hnode, hash, NULL); |
| 593 | assert(!r); |
| 594 | (void)r; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 595 | |
Michal Vasko | 9d6befd | 2019-12-11 14:56:56 +0100 | [diff] [blame] | 596 | if (hnode.node == node) { |
| 597 | /* it was just added, do not add it twice */ |
| 598 | node = NULL; |
| 599 | } |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | if (set->ht && node) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 604 | /* add the new node into hash table */ |
| 605 | hnode.node = node; |
| 606 | hnode.type = type; |
| 607 | |
| 608 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 609 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 610 | hash = dict_hash_multi(hash, NULL, 0); |
| 611 | |
| 612 | r = lyht_insert(set->ht, &hnode, hash, NULL); |
| 613 | assert(!r); |
| 614 | (void)r; |
| 615 | } |
| 616 | } |
| 617 | |
| 618 | /** |
| 619 | * @brief Remove node and its hash from set. |
| 620 | * |
| 621 | * @param[in] set Set to remove from. |
| 622 | * @param[in] node Node to remove. |
| 623 | * @param[in] type Node type. |
| 624 | */ |
| 625 | static void |
| 626 | set_remove_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type) |
| 627 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 628 | LY_ERR r; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 629 | struct lyxp_set_hash_node hnode; |
| 630 | uint32_t hash; |
| 631 | |
| 632 | if (set->ht) { |
| 633 | hnode.node = node; |
| 634 | hnode.type = type; |
| 635 | |
| 636 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 637 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 638 | hash = dict_hash_multi(hash, NULL, 0); |
| 639 | |
| 640 | r = lyht_remove(set->ht, &hnode, hash); |
| 641 | assert(!r); |
| 642 | (void)r; |
| 643 | |
| 644 | if (!set->ht->used) { |
| 645 | lyht_free(set->ht); |
| 646 | set->ht = NULL; |
| 647 | } |
| 648 | } |
| 649 | } |
| 650 | |
| 651 | /** |
| 652 | * @brief Check whether node is in set based on its hash. |
| 653 | * |
| 654 | * @param[in] set Set to search in. |
| 655 | * @param[in] node Node to search for. |
| 656 | * @param[in] type Node type. |
| 657 | * @param[in] skip_idx Index in @p set to skip. |
| 658 | * @return LY_ERR |
| 659 | */ |
| 660 | static LY_ERR |
| 661 | set_dup_node_hash_check(const struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type, int skip_idx) |
| 662 | { |
| 663 | struct lyxp_set_hash_node hnode, *match_p; |
| 664 | uint32_t hash; |
| 665 | |
| 666 | hnode.node = node; |
| 667 | hnode.type = type; |
| 668 | |
| 669 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 670 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 671 | hash = dict_hash_multi(hash, NULL, 0); |
| 672 | |
| 673 | if (!lyht_find(set->ht, &hnode, hash, (void **)&match_p)) { |
| 674 | if ((skip_idx > -1) && (set->val.nodes[skip_idx].node == match_p->node) && (set->val.nodes[skip_idx].type == match_p->type)) { |
| 675 | /* we found it on the index that should be skipped, find another */ |
| 676 | hnode = *match_p; |
| 677 | if (lyht_find_next(set->ht, &hnode, hash, (void **)&match_p)) { |
| 678 | /* none other found */ |
| 679 | return LY_SUCCESS; |
| 680 | } |
| 681 | } |
| 682 | |
| 683 | return LY_EEXIST; |
| 684 | } |
| 685 | |
| 686 | /* not found */ |
| 687 | return LY_SUCCESS; |
| 688 | } |
| 689 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 690 | void |
| 691 | lyxp_set_free_content(struct lyxp_set *set) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 692 | { |
| 693 | if (!set) { |
| 694 | return; |
| 695 | } |
| 696 | |
| 697 | if (set->type == LYXP_SET_NODE_SET) { |
| 698 | free(set->val.nodes); |
| 699 | lyht_free(set->ht); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 700 | } else if (set->type == LYXP_SET_SCNODE_SET) { |
| 701 | free(set->val.scnodes); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 702 | lyht_free(set->ht); |
| 703 | } else { |
| 704 | if (set->type == LYXP_SET_STRING) { |
| 705 | free(set->val.str); |
| 706 | } |
| 707 | set->type = LYXP_SET_NODE_SET; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 708 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 709 | |
| 710 | set->val.nodes = NULL; |
| 711 | set->used = 0; |
| 712 | set->size = 0; |
| 713 | set->ht = NULL; |
| 714 | set->ctx_pos = 0; |
| 715 | set->ctx_pos = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 716 | } |
| 717 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 718 | /** |
| 719 | * @brief Free dynamically-allocated set. |
| 720 | * |
| 721 | * @param[in] set Set to free. |
| 722 | */ |
| 723 | static void |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 724 | lyxp_set_free(struct lyxp_set *set) |
| 725 | { |
| 726 | if (!set) { |
| 727 | return; |
| 728 | } |
| 729 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 730 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 731 | free(set); |
| 732 | } |
| 733 | |
| 734 | /** |
| 735 | * @brief Initialize set context. |
| 736 | * |
| 737 | * @param[in] new Set to initialize. |
| 738 | * @param[in] set Arbitrary initialized set. |
| 739 | */ |
| 740 | static void |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 741 | set_init(struct lyxp_set *new, const struct lyxp_set *set) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 742 | { |
| 743 | memset(new, 0, sizeof *new); |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 744 | if (set) { |
| 745 | new->ctx = set->ctx; |
| 746 | new->ctx_node = set->ctx_node; |
Michal Vasko | 588112f | 2019-12-10 14:51:53 +0100 | [diff] [blame] | 747 | new->root_type = set->root_type; |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 748 | new->context_op = set->context_op; |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 749 | new->local_mod = set->local_mod; |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 750 | new->tree = set->tree; |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 751 | new->format = set->format; |
| 752 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 753 | } |
| 754 | |
| 755 | /** |
| 756 | * @brief Create a deep copy of a set. |
| 757 | * |
| 758 | * @param[in] set Set to copy. |
| 759 | * @return Copy of @p set. |
| 760 | */ |
| 761 | static struct lyxp_set * |
| 762 | set_copy(struct lyxp_set *set) |
| 763 | { |
| 764 | struct lyxp_set *ret; |
| 765 | uint16_t i; |
| 766 | |
| 767 | if (!set) { |
| 768 | return NULL; |
| 769 | } |
| 770 | |
| 771 | ret = malloc(sizeof *ret); |
| 772 | LY_CHECK_ERR_RET(!ret, LOGMEM(set->ctx), NULL); |
| 773 | set_init(ret, set); |
| 774 | |
| 775 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 776 | ret->type = set->type; |
| 777 | |
| 778 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | ba71654 | 2019-12-16 10:01:58 +0100 | [diff] [blame] | 779 | if ((set->val.scnodes[i].in_ctx == 1) || (set->val.scnodes[i].in_ctx == -2)) { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame^] | 780 | uint32_t idx; |
| 781 | LY_CHECK_ERR_RET(lyxp_set_scnode_insert_node(ret, set->val.scnodes[i].scnode, set->val.scnodes[i].type, &idx), |
| 782 | lyxp_set_free(ret), NULL); |
Michal Vasko | 3f27c52 | 2020-01-06 08:37:49 +0100 | [diff] [blame] | 783 | /* coverity seems to think scnodes can be NULL */ |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame^] | 784 | if (!ret->val.scnodes) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 785 | lyxp_set_free(ret); |
| 786 | return NULL; |
| 787 | } |
Michal Vasko | ba71654 | 2019-12-16 10:01:58 +0100 | [diff] [blame] | 788 | ret->val.scnodes[idx].in_ctx = set->val.scnodes[i].in_ctx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 789 | } |
| 790 | } |
| 791 | } else if (set->type == LYXP_SET_NODE_SET) { |
| 792 | ret->type = set->type; |
| 793 | ret->val.nodes = malloc(set->used * sizeof *ret->val.nodes); |
| 794 | LY_CHECK_ERR_RET(!ret->val.nodes, LOGMEM(set->ctx); free(ret), NULL); |
| 795 | memcpy(ret->val.nodes, set->val.nodes, set->used * sizeof *ret->val.nodes); |
| 796 | |
| 797 | ret->used = ret->size = set->used; |
| 798 | ret->ctx_pos = set->ctx_pos; |
| 799 | ret->ctx_size = set->ctx_size; |
| 800 | ret->ht = lyht_dup(set->ht); |
| 801 | } else { |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 802 | memcpy(ret, set, sizeof *ret); |
| 803 | if (set->type == LYXP_SET_STRING) { |
| 804 | ret->val.str = strdup(set->val.str); |
| 805 | LY_CHECK_ERR_RET(!ret->val.str, LOGMEM(set->ctx); free(ret), NULL); |
| 806 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 807 | } |
| 808 | |
| 809 | return ret; |
| 810 | } |
| 811 | |
| 812 | /** |
| 813 | * @brief Fill XPath set with a string. Any current data are disposed of. |
| 814 | * |
| 815 | * @param[in] set Set to fill. |
| 816 | * @param[in] string String to fill into \p set. |
| 817 | * @param[in] str_len Length of \p string. 0 is a valid value! |
| 818 | */ |
| 819 | static void |
| 820 | set_fill_string(struct lyxp_set *set, const char *string, uint16_t str_len) |
| 821 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 822 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 823 | |
| 824 | set->type = LYXP_SET_STRING; |
| 825 | if ((str_len == 0) && (string[0] != '\0')) { |
| 826 | string = ""; |
| 827 | } |
| 828 | set->val.str = strndup(string, str_len); |
| 829 | } |
| 830 | |
| 831 | /** |
| 832 | * @brief Fill XPath set with a number. Any current data are disposed of. |
| 833 | * |
| 834 | * @param[in] set Set to fill. |
| 835 | * @param[in] number Number to fill into \p set. |
| 836 | */ |
| 837 | static void |
| 838 | set_fill_number(struct lyxp_set *set, long double number) |
| 839 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 840 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 841 | |
| 842 | set->type = LYXP_SET_NUMBER; |
| 843 | set->val.num = number; |
| 844 | } |
| 845 | |
| 846 | /** |
| 847 | * @brief Fill XPath set with a boolean. Any current data are disposed of. |
| 848 | * |
| 849 | * @param[in] set Set to fill. |
| 850 | * @param[in] boolean Boolean to fill into \p set. |
| 851 | */ |
| 852 | static void |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 853 | set_fill_boolean(struct lyxp_set *set, uint8_t boolean) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 854 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 855 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 856 | |
| 857 | set->type = LYXP_SET_BOOLEAN; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 858 | set->val.bln = boolean; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 859 | } |
| 860 | |
| 861 | /** |
| 862 | * @brief Fill XPath set with the value from another set (deep assign). |
| 863 | * Any current data are disposed of. |
| 864 | * |
| 865 | * @param[in] trg Set to fill. |
| 866 | * @param[in] src Source set to copy into \p trg. |
| 867 | */ |
| 868 | static void |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 869 | set_fill_set(struct lyxp_set *trg, const struct lyxp_set *src) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 870 | { |
| 871 | if (!trg || !src) { |
| 872 | return; |
| 873 | } |
| 874 | |
| 875 | if (trg->type == LYXP_SET_NODE_SET) { |
| 876 | free(trg->val.nodes); |
| 877 | } else if (trg->type == LYXP_SET_STRING) { |
| 878 | free(trg->val.str); |
| 879 | } |
| 880 | set_init(trg, src); |
| 881 | |
| 882 | if (src->type == LYXP_SET_SCNODE_SET) { |
| 883 | trg->type = LYXP_SET_SCNODE_SET; |
| 884 | trg->used = src->used; |
| 885 | trg->size = src->used; |
| 886 | |
| 887 | trg->val.scnodes = ly_realloc(trg->val.scnodes, trg->size * sizeof *trg->val.scnodes); |
| 888 | LY_CHECK_ERR_RET(!trg->val.scnodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), ); |
| 889 | memcpy(trg->val.scnodes, src->val.scnodes, src->used * sizeof *src->val.scnodes); |
| 890 | } else if (src->type == LYXP_SET_BOOLEAN) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 891 | set_fill_boolean(trg, src->val.bln); |
Michal Vasko | 44f3d2c | 2020-08-24 09:49:38 +0200 | [diff] [blame] | 892 | } else if (src->type == LYXP_SET_NUMBER) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 893 | set_fill_number(trg, src->val.num); |
| 894 | } else if (src->type == LYXP_SET_STRING) { |
| 895 | set_fill_string(trg, src->val.str, strlen(src->val.str)); |
| 896 | } else { |
| 897 | if (trg->type == LYXP_SET_NODE_SET) { |
| 898 | free(trg->val.nodes); |
| 899 | } else if (trg->type == LYXP_SET_STRING) { |
| 900 | free(trg->val.str); |
| 901 | } |
| 902 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 903 | assert(src->type == LYXP_SET_NODE_SET); |
| 904 | |
| 905 | trg->type = LYXP_SET_NODE_SET; |
| 906 | trg->used = src->used; |
| 907 | trg->size = src->used; |
| 908 | trg->ctx_pos = src->ctx_pos; |
| 909 | trg->ctx_size = src->ctx_size; |
| 910 | |
| 911 | trg->val.nodes = malloc(trg->used * sizeof *trg->val.nodes); |
| 912 | LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), ); |
| 913 | memcpy(trg->val.nodes, src->val.nodes, src->used * sizeof *src->val.nodes); |
| 914 | if (src->ht) { |
| 915 | trg->ht = lyht_dup(src->ht); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 916 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 917 | trg->ht = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 918 | } |
| 919 | } |
| 920 | } |
| 921 | |
| 922 | /** |
| 923 | * @brief Clear context of all schema nodes. |
| 924 | * |
| 925 | * @param[in] set Set to clear. |
| 926 | */ |
| 927 | static void |
| 928 | set_scnode_clear_ctx(struct lyxp_set *set) |
| 929 | { |
| 930 | uint32_t i; |
| 931 | |
| 932 | for (i = 0; i < set->used; ++i) { |
| 933 | if (set->val.scnodes[i].in_ctx == 1) { |
| 934 | set->val.scnodes[i].in_ctx = 0; |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 935 | } else if (set->val.scnodes[i].in_ctx == -2) { |
| 936 | set->val.scnodes[i].in_ctx = -1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 937 | } |
| 938 | } |
| 939 | } |
| 940 | |
| 941 | /** |
| 942 | * @brief Remove a node from a set. Removing last node changes |
| 943 | * set into LYXP_SET_EMPTY. Context position aware. |
| 944 | * |
| 945 | * @param[in] set Set to use. |
| 946 | * @param[in] idx Index from @p set of the node to be removed. |
| 947 | */ |
| 948 | static void |
| 949 | set_remove_node(struct lyxp_set *set, uint32_t idx) |
| 950 | { |
| 951 | assert(set && (set->type == LYXP_SET_NODE_SET)); |
| 952 | assert(idx < set->used); |
| 953 | |
| 954 | set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
| 955 | |
| 956 | --set->used; |
| 957 | if (set->used) { |
| 958 | memmove(&set->val.nodes[idx], &set->val.nodes[idx + 1], |
| 959 | (set->used - idx) * sizeof *set->val.nodes); |
| 960 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 961 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 962 | } |
| 963 | } |
| 964 | |
| 965 | /** |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 966 | * @brief Remove a node from a set by setting its type to LYXP_NODE_NONE. |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 967 | * |
| 968 | * @param[in] set Set to use. |
| 969 | * @param[in] idx Index from @p set of the node to be removed. |
| 970 | */ |
| 971 | static void |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 972 | set_remove_node_none(struct lyxp_set *set, uint32_t idx) |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 973 | { |
| 974 | assert(set && (set->type == LYXP_SET_NODE_SET)); |
| 975 | assert(idx < set->used); |
| 976 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 977 | if (set->val.nodes[idx].type == LYXP_NODE_ELEM) { |
| 978 | set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
| 979 | } |
| 980 | set->val.nodes[idx].type = LYXP_NODE_NONE; |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 981 | } |
| 982 | |
| 983 | /** |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 984 | * @brief Remove all LYXP_NODE_NONE nodes from a set. Removing last node changes |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 985 | * set into LYXP_SET_EMPTY. Context position aware. |
| 986 | * |
| 987 | * @param[in] set Set to consolidate. |
| 988 | */ |
| 989 | static void |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 990 | set_remove_nodes_none(struct lyxp_set *set) |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 991 | { |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 992 | uint16_t i, orig_used, end = 0; |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 993 | int32_t start; |
| 994 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 995 | assert(set); |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 996 | |
| 997 | orig_used = set->used; |
| 998 | set->used = 0; |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 999 | for (i = 0; i < orig_used; ) { |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1000 | start = -1; |
| 1001 | do { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1002 | if ((set->val.nodes[i].type != LYXP_NODE_NONE) && (start == -1)) { |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1003 | start = i; |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1004 | } else if ((start > -1) && (set->val.nodes[i].type == LYXP_NODE_NONE)) { |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1005 | end = i; |
| 1006 | ++i; |
| 1007 | break; |
| 1008 | } |
| 1009 | |
| 1010 | ++i; |
| 1011 | if (i == orig_used) { |
| 1012 | end = i; |
| 1013 | } |
| 1014 | } while (i < orig_used); |
| 1015 | |
| 1016 | if (start > -1) { |
| 1017 | /* move the whole chunk of valid nodes together */ |
| 1018 | if (set->used != (unsigned)start) { |
| 1019 | memmove(&set->val.nodes[set->used], &set->val.nodes[start], (end - start) * sizeof *set->val.nodes); |
| 1020 | } |
| 1021 | set->used += end - start; |
| 1022 | } |
| 1023 | } |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1024 | } |
| 1025 | |
| 1026 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1027 | * @brief Check for duplicates in a node set. |
| 1028 | * |
| 1029 | * @param[in] set Set to check. |
| 1030 | * @param[in] node Node to look for in @p set. |
| 1031 | * @param[in] node_type Type of @p node. |
| 1032 | * @param[in] skip_idx Index from @p set to skip. |
| 1033 | * @return LY_ERR |
| 1034 | */ |
| 1035 | static LY_ERR |
| 1036 | set_dup_node_check(const struct lyxp_set *set, const struct lyd_node *node, enum lyxp_node_type node_type, int skip_idx) |
| 1037 | { |
| 1038 | uint32_t i; |
| 1039 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1040 | if (set->ht && node) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1041 | return set_dup_node_hash_check(set, (struct lyd_node *)node, node_type, skip_idx); |
| 1042 | } |
| 1043 | |
| 1044 | for (i = 0; i < set->used; ++i) { |
| 1045 | if ((skip_idx > -1) && (i == (unsigned)skip_idx)) { |
| 1046 | continue; |
| 1047 | } |
| 1048 | |
| 1049 | if ((set->val.nodes[i].node == node) && (set->val.nodes[i].type == node_type)) { |
| 1050 | return LY_EEXIST; |
| 1051 | } |
| 1052 | } |
| 1053 | |
| 1054 | return LY_SUCCESS; |
| 1055 | } |
| 1056 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame^] | 1057 | uint8_t |
| 1058 | lyxp_set_scnode_contains(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, int skip_idx, |
| 1059 | uint32_t *index_p) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1060 | { |
| 1061 | uint32_t i; |
| 1062 | |
| 1063 | for (i = 0; i < set->used; ++i) { |
| 1064 | if ((skip_idx > -1) && (i == (unsigned)skip_idx)) { |
| 1065 | continue; |
| 1066 | } |
| 1067 | |
| 1068 | if ((set->val.scnodes[i].scnode == node) && (set->val.scnodes[i].type == node_type)) { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame^] | 1069 | if (index_p) { |
| 1070 | *index_p = i; |
| 1071 | } |
| 1072 | return 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1073 | } |
| 1074 | } |
| 1075 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame^] | 1076 | return 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1077 | } |
| 1078 | |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 1079 | void |
| 1080 | lyxp_set_scnode_merge(struct lyxp_set *set1, struct lyxp_set *set2) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1081 | { |
| 1082 | uint32_t orig_used, i, j; |
| 1083 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1084 | 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] | 1085 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1086 | if (!set2->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1087 | return; |
| 1088 | } |
| 1089 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1090 | if (!set1->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1091 | memcpy(set1, set2, sizeof *set1); |
| 1092 | return; |
| 1093 | } |
| 1094 | |
| 1095 | if (set1->used + set2->used > set1->size) { |
| 1096 | set1->size = set1->used + set2->used; |
| 1097 | set1->val.scnodes = ly_realloc(set1->val.scnodes, set1->size * sizeof *set1->val.scnodes); |
| 1098 | LY_CHECK_ERR_RET(!set1->val.scnodes, LOGMEM(set1->ctx), ); |
| 1099 | } |
| 1100 | |
| 1101 | orig_used = set1->used; |
| 1102 | |
| 1103 | for (i = 0; i < set2->used; ++i) { |
| 1104 | for (j = 0; j < orig_used; ++j) { |
| 1105 | /* detect duplicities */ |
| 1106 | if (set1->val.scnodes[j].scnode == set2->val.scnodes[i].scnode) { |
| 1107 | break; |
| 1108 | } |
| 1109 | } |
| 1110 | |
| 1111 | if (j == orig_used) { |
| 1112 | memcpy(&set1->val.scnodes[set1->used], &set2->val.scnodes[i], sizeof *set2->val.scnodes); |
| 1113 | ++set1->used; |
| 1114 | } |
| 1115 | } |
| 1116 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1117 | lyxp_set_free_content(set2); |
| 1118 | set2->type = LYXP_SET_SCNODE_SET; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1119 | } |
| 1120 | |
| 1121 | /** |
| 1122 | * @brief Insert a node into a set. Context position aware. |
| 1123 | * |
| 1124 | * @param[in] set Set to use. |
| 1125 | * @param[in] node Node to insert to @p set. |
| 1126 | * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting. |
| 1127 | * @param[in] node_type Node type of @p node. |
| 1128 | * @param[in] idx Index in @p set to insert into. |
| 1129 | */ |
| 1130 | static void |
| 1131 | set_insert_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx) |
| 1132 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1133 | assert(set && (set->type == LYXP_SET_NODE_SET)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1134 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1135 | if (!set->size) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1136 | /* first item */ |
| 1137 | if (idx) { |
| 1138 | /* no real harm done, but it is a bug */ |
| 1139 | LOGINT(set->ctx); |
| 1140 | idx = 0; |
| 1141 | } |
| 1142 | set->val.nodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.nodes); |
| 1143 | LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), ); |
| 1144 | set->type = LYXP_SET_NODE_SET; |
| 1145 | set->used = 0; |
| 1146 | set->size = LYXP_SET_SIZE_START; |
| 1147 | set->ctx_pos = 1; |
| 1148 | set->ctx_size = 1; |
| 1149 | set->ht = NULL; |
| 1150 | } else { |
| 1151 | /* not an empty set */ |
| 1152 | if (set->used == set->size) { |
| 1153 | |
| 1154 | /* set is full */ |
| 1155 | set->val.nodes = ly_realloc(set->val.nodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.nodes); |
| 1156 | LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), ); |
| 1157 | set->size += LYXP_SET_SIZE_STEP; |
| 1158 | } |
| 1159 | |
| 1160 | if (idx > set->used) { |
| 1161 | LOGINT(set->ctx); |
| 1162 | idx = set->used; |
| 1163 | } |
| 1164 | |
| 1165 | /* make space for the new node */ |
| 1166 | if (idx < set->used) { |
| 1167 | memmove(&set->val.nodes[idx + 1], &set->val.nodes[idx], (set->used - idx) * sizeof *set->val.nodes); |
| 1168 | } |
| 1169 | } |
| 1170 | |
| 1171 | /* finally assign the value */ |
| 1172 | set->val.nodes[idx].node = (struct lyd_node *)node; |
| 1173 | set->val.nodes[idx].type = node_type; |
| 1174 | set->val.nodes[idx].pos = pos; |
| 1175 | ++set->used; |
| 1176 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1177 | if (set->val.nodes[idx].type == LYXP_NODE_ELEM) { |
| 1178 | set_insert_node_hash(set, (struct lyd_node *)node, node_type); |
| 1179 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1180 | } |
| 1181 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame^] | 1182 | LY_ERR |
| 1183 | lyxp_set_scnode_insert_node(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, uint32_t *index_p) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1184 | { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame^] | 1185 | uint32_t index; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1186 | |
| 1187 | assert(set->type == LYXP_SET_SCNODE_SET); |
| 1188 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame^] | 1189 | if (lyxp_set_scnode_contains(set, node, node_type, -1, &index)) { |
| 1190 | set->val.scnodes[index].in_ctx = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1191 | } else { |
| 1192 | if (set->used == set->size) { |
| 1193 | set->val.scnodes = ly_realloc(set->val.scnodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.scnodes); |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame^] | 1194 | LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), LY_EMEM); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1195 | set->size += LYXP_SET_SIZE_STEP; |
| 1196 | } |
| 1197 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame^] | 1198 | index = set->used; |
| 1199 | set->val.scnodes[index].scnode = (struct lysc_node *)node; |
| 1200 | set->val.scnodes[index].type = node_type; |
| 1201 | set->val.scnodes[index].in_ctx = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1202 | ++set->used; |
| 1203 | } |
| 1204 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame^] | 1205 | if (index_p) { |
| 1206 | *index_p = index; |
| 1207 | } |
| 1208 | |
| 1209 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1210 | } |
| 1211 | |
| 1212 | /** |
| 1213 | * @brief Replace a node in a set with another. Context position aware. |
| 1214 | * |
| 1215 | * @param[in] set Set to use. |
| 1216 | * @param[in] node Node to insert to @p set. |
| 1217 | * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting. |
| 1218 | * @param[in] node_type Node type of @p node. |
| 1219 | * @param[in] idx Index in @p set of the node to replace. |
| 1220 | */ |
| 1221 | static void |
| 1222 | set_replace_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx) |
| 1223 | { |
| 1224 | assert(set && (idx < set->used)); |
| 1225 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1226 | if (set->val.nodes[idx].type == LYXP_NODE_ELEM) { |
| 1227 | set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
| 1228 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1229 | set->val.nodes[idx].node = (struct lyd_node *)node; |
| 1230 | set->val.nodes[idx].type = node_type; |
| 1231 | set->val.nodes[idx].pos = pos; |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1232 | if (set->val.nodes[idx].type == LYXP_NODE_ELEM) { |
| 1233 | set_insert_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type); |
| 1234 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1235 | } |
| 1236 | |
| 1237 | /** |
| 1238 | * @brief Set all nodes with ctx 1 to a new unique context value. |
| 1239 | * |
| 1240 | * @param[in] set Set to modify. |
| 1241 | * @return New context value. |
| 1242 | */ |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 1243 | static int32_t |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1244 | set_scnode_new_in_ctx(struct lyxp_set *set) |
| 1245 | { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 1246 | uint32_t i; |
| 1247 | int32_t ret_ctx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1248 | |
| 1249 | assert(set->type == LYXP_SET_SCNODE_SET); |
| 1250 | |
| 1251 | ret_ctx = 3; |
| 1252 | retry: |
| 1253 | for (i = 0; i < set->used; ++i) { |
| 1254 | if (set->val.scnodes[i].in_ctx >= ret_ctx) { |
| 1255 | ret_ctx = set->val.scnodes[i].in_ctx + 1; |
| 1256 | goto retry; |
| 1257 | } |
| 1258 | } |
| 1259 | for (i = 0; i < set->used; ++i) { |
| 1260 | if (set->val.scnodes[i].in_ctx == 1) { |
| 1261 | set->val.scnodes[i].in_ctx = ret_ctx; |
| 1262 | } |
| 1263 | } |
| 1264 | |
| 1265 | return ret_ctx; |
| 1266 | } |
| 1267 | |
| 1268 | /** |
| 1269 | * @brief Get unique @p node position in the data. |
| 1270 | * |
| 1271 | * @param[in] node Node to find. |
| 1272 | * @param[in] node_type Node type of @p node. |
| 1273 | * @param[in] root Root node. |
| 1274 | * @param[in] root_type Type of the XPath @p root node. |
| 1275 | * @param[in] prev Node that we think is before @p node in DFS from @p root. Can optionally |
| 1276 | * be used to increase efficiency and start the DFS from this node. |
| 1277 | * @param[in] prev_pos Node @p prev position. Optional, but must be set if @p prev is set. |
| 1278 | * @return Node position. |
| 1279 | */ |
| 1280 | static uint32_t |
| 1281 | get_node_pos(const struct lyd_node *node, enum lyxp_node_type node_type, const struct lyd_node *root, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1282 | enum lyxp_node_type root_type, const struct lyd_node **prev, uint32_t *prev_pos) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1283 | { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1284 | const struct lyd_node *elem = NULL, *top_sibling; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1285 | uint32_t pos = 1; |
| 1286 | |
| 1287 | assert(prev && prev_pos && !root->prev->next); |
| 1288 | |
| 1289 | if ((node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ROOT_CONFIG)) { |
| 1290 | return 0; |
| 1291 | } |
| 1292 | |
| 1293 | if (*prev) { |
| 1294 | /* start from the previous element instead from the root */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1295 | pos = *prev_pos; |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 1296 | for (top_sibling = *prev; top_sibling->parent; top_sibling = (struct lyd_node *)top_sibling->parent) {} |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1297 | goto dfs_search; |
| 1298 | } |
| 1299 | |
| 1300 | for (top_sibling = root; top_sibling; top_sibling = top_sibling->next) { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1301 | LYD_TREE_DFS_BEGIN(top_sibling, elem) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1302 | dfs_search: |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1303 | if (*prev && !elem) { |
| 1304 | /* resume previous DFS */ |
| 1305 | elem = LYD_TREE_DFS_next = (struct lyd_node *)*prev; |
| 1306 | LYD_TREE_DFS_continue = 0; |
| 1307 | } |
| 1308 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1309 | if ((root_type == LYXP_NODE_ROOT_CONFIG) && (elem->schema->flags & LYS_CONFIG_R)) { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1310 | /* skip */ |
| 1311 | LYD_TREE_DFS_continue = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1312 | } else { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1313 | if (elem == node) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1314 | break; |
| 1315 | } |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1316 | ++pos; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1317 | } |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1318 | |
| 1319 | LYD_TREE_DFS_END(top_sibling, elem); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1320 | } |
| 1321 | |
| 1322 | /* node found */ |
| 1323 | if (elem) { |
| 1324 | break; |
| 1325 | } |
| 1326 | } |
| 1327 | |
| 1328 | if (!elem) { |
| 1329 | if (!(*prev)) { |
| 1330 | /* we went from root and failed to find it, cannot be */ |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 1331 | LOGINT(LYD_CTX(node)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1332 | return 0; |
| 1333 | } else { |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1334 | /* start the search again from the beginning */ |
| 1335 | *prev = root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1336 | |
Michal Vasko | 56daf73 | 2020-08-10 10:57:18 +0200 | [diff] [blame] | 1337 | top_sibling = root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1338 | pos = 1; |
| 1339 | goto dfs_search; |
| 1340 | } |
| 1341 | } |
| 1342 | |
| 1343 | /* remember the last found node for next time */ |
| 1344 | *prev = node; |
| 1345 | *prev_pos = pos; |
| 1346 | |
| 1347 | return pos; |
| 1348 | } |
| 1349 | |
| 1350 | /** |
| 1351 | * @brief Assign (fill) missing node positions. |
| 1352 | * |
| 1353 | * @param[in] set Set to fill positions in. |
| 1354 | * @param[in] root Context root node. |
| 1355 | * @param[in] root_type Context root type. |
| 1356 | * @return LY_ERR |
| 1357 | */ |
| 1358 | static LY_ERR |
| 1359 | set_assign_pos(struct lyxp_set *set, const struct lyd_node *root, enum lyxp_node_type root_type) |
| 1360 | { |
| 1361 | const struct lyd_node *prev = NULL, *tmp_node; |
| 1362 | uint32_t i, tmp_pos = 0; |
| 1363 | |
| 1364 | for (i = 0; i < set->used; ++i) { |
| 1365 | if (!set->val.nodes[i].pos) { |
| 1366 | tmp_node = NULL; |
| 1367 | switch (set->val.nodes[i].type) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1368 | case LYXP_NODE_META: |
| 1369 | tmp_node = set->val.meta[i].meta->parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1370 | if (!tmp_node) { |
| 1371 | LOGINT_RET(root->schema->module->ctx); |
| 1372 | } |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1373 | /* fallthrough */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1374 | case LYXP_NODE_ELEM: |
| 1375 | case LYXP_NODE_TEXT: |
| 1376 | if (!tmp_node) { |
| 1377 | tmp_node = set->val.nodes[i].node; |
| 1378 | } |
| 1379 | set->val.nodes[i].pos = get_node_pos(tmp_node, set->val.nodes[i].type, root, root_type, &prev, &tmp_pos); |
| 1380 | break; |
| 1381 | default: |
| 1382 | /* all roots have position 0 */ |
| 1383 | break; |
| 1384 | } |
| 1385 | } |
| 1386 | } |
| 1387 | |
| 1388 | return LY_SUCCESS; |
| 1389 | } |
| 1390 | |
| 1391 | /** |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1392 | * @brief Get unique @p meta position in the parent metadata. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1393 | * |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1394 | * @param[in] meta Metadata to use. |
| 1395 | * @return Metadata position. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1396 | */ |
| 1397 | static uint16_t |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1398 | get_meta_pos(struct lyd_meta *meta) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1399 | { |
| 1400 | uint16_t pos = 0; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1401 | struct lyd_meta *meta2; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1402 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1403 | for (meta2 = meta->parent->meta; meta2 && (meta2 != meta); meta2 = meta2->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1404 | ++pos; |
| 1405 | } |
| 1406 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1407 | assert(meta2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1408 | return pos; |
| 1409 | } |
| 1410 | |
| 1411 | /** |
| 1412 | * @brief Compare 2 nodes in respect to XPath document order. |
| 1413 | * |
| 1414 | * @param[in] item1 1st node. |
| 1415 | * @param[in] item2 2nd node. |
| 1416 | * @return If 1st > 2nd returns 1, 1st == 2nd returns 0, and 1st < 2nd returns -1. |
| 1417 | */ |
| 1418 | static int |
| 1419 | set_sort_compare(struct lyxp_set_node *item1, struct lyxp_set_node *item2) |
| 1420 | { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1421 | uint32_t meta_pos1 = 0, meta_pos2 = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1422 | |
| 1423 | if (item1->pos < item2->pos) { |
| 1424 | return -1; |
| 1425 | } |
| 1426 | |
| 1427 | if (item1->pos > item2->pos) { |
| 1428 | return 1; |
| 1429 | } |
| 1430 | |
| 1431 | /* node positions are equal, the fun case */ |
| 1432 | |
| 1433 | /* 1st ELEM - == - 2nd TEXT, 1st TEXT - == - 2nd ELEM */ |
| 1434 | /* special case since text nodes are actually saved as their parents */ |
| 1435 | if ((item1->node == item2->node) && (item1->type != item2->type)) { |
| 1436 | if (item1->type == LYXP_NODE_ELEM) { |
| 1437 | assert(item2->type == LYXP_NODE_TEXT); |
| 1438 | return -1; |
| 1439 | } else { |
| 1440 | assert((item1->type == LYXP_NODE_TEXT) && (item2->type == LYXP_NODE_ELEM)); |
| 1441 | return 1; |
| 1442 | } |
| 1443 | } |
| 1444 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1445 | /* we need meta positions now */ |
| 1446 | if (item1->type == LYXP_NODE_META) { |
| 1447 | meta_pos1 = get_meta_pos((struct lyd_meta *)item1->node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1448 | } |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1449 | if (item2->type == LYXP_NODE_META) { |
| 1450 | meta_pos2 = get_meta_pos((struct lyd_meta *)item2->node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1451 | } |
| 1452 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1453 | /* 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] | 1454 | /* check for duplicates */ |
| 1455 | if (item1->node == item2->node) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1456 | 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] | 1457 | return 0; |
| 1458 | } |
| 1459 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1460 | /* 1st ELEM - 2nd TEXT, 1st ELEM - any pos - 2nd META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1461 | /* elem is always first, 2nd node is after it */ |
| 1462 | if (item1->type == LYXP_NODE_ELEM) { |
| 1463 | assert(item2->type != LYXP_NODE_ELEM); |
| 1464 | return -1; |
| 1465 | } |
| 1466 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1467 | /* 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] | 1468 | /* 2nd is before 1st */ |
| 1469 | if (((item1->type == LYXP_NODE_TEXT) |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1470 | && ((item2->type == LYXP_NODE_ELEM) || (item2->type == LYXP_NODE_META))) |
| 1471 | || ((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_ELEM)) |
| 1472 | || (((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_META)) |
| 1473 | && (meta_pos1 > meta_pos2))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1474 | return 1; |
| 1475 | } |
| 1476 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 1477 | /* 1st META - any pos - 2nd TEXT, 1st META <pos< - 2nd META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1478 | /* 2nd is after 1st */ |
| 1479 | return -1; |
| 1480 | } |
| 1481 | |
| 1482 | /** |
| 1483 | * @brief Set cast for comparisons. |
| 1484 | * |
| 1485 | * @param[in] trg Target set to cast source into. |
| 1486 | * @param[in] src Source set. |
| 1487 | * @param[in] type Target set type. |
| 1488 | * @param[in] src_idx Source set node index. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1489 | * @return LY_ERR |
| 1490 | */ |
| 1491 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1492 | 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] | 1493 | { |
| 1494 | assert(src->type == LYXP_SET_NODE_SET); |
| 1495 | |
| 1496 | set_init(trg, src); |
| 1497 | |
| 1498 | /* insert node into target set */ |
| 1499 | set_insert_node(trg, src->val.nodes[src_idx].node, src->val.nodes[src_idx].pos, src->val.nodes[src_idx].type, 0); |
| 1500 | |
| 1501 | /* cast target set appropriately */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1502 | return lyxp_set_cast(trg, type); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1503 | } |
| 1504 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1505 | /** |
| 1506 | * @brief Set content canonization for comparisons. |
| 1507 | * |
| 1508 | * @param[in] trg Target set to put the canononized source into. |
| 1509 | * @param[in] src Source set. |
| 1510 | * @param[in] xp_node Source XPath node/meta to use for canonization. |
| 1511 | * @return LY_ERR |
| 1512 | */ |
| 1513 | static LY_ERR |
| 1514 | set_comp_canonize(struct lyxp_set *trg, const struct lyxp_set *src, const struct lyxp_set_node *xp_node) |
| 1515 | { |
| 1516 | struct lysc_type *type = NULL; |
| 1517 | struct lyd_value val; |
| 1518 | struct ly_err_item *err = NULL; |
| 1519 | char *str, *ptr; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 1520 | uint8_t dynamic; |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1521 | LY_ERR rc; |
| 1522 | |
| 1523 | /* is there anything to canonize even? */ |
| 1524 | if ((src->type == LYXP_SET_NUMBER) || (src->type == LYXP_SET_STRING)) { |
| 1525 | /* do we have a type to use for canonization? */ |
| 1526 | if ((xp_node->type == LYXP_NODE_ELEM) && (xp_node->node->schema->nodetype & LYD_NODE_TERM)) { |
| 1527 | type = ((struct lyd_node_term *)xp_node->node)->value.realtype; |
| 1528 | } else if (xp_node->type == LYXP_NODE_META) { |
| 1529 | type = ((struct lyd_meta *)xp_node->node)->value.realtype; |
| 1530 | } |
| 1531 | } |
| 1532 | if (!type) { |
| 1533 | goto fill; |
| 1534 | } |
| 1535 | |
| 1536 | if (src->type == LYXP_SET_NUMBER) { |
| 1537 | /* canonize number */ |
| 1538 | if (asprintf(&str, "%Lf", src->val.num) == -1) { |
| 1539 | LOGMEM(src->ctx); |
| 1540 | return LY_EMEM; |
| 1541 | } |
| 1542 | } else { |
| 1543 | /* canonize string */ |
| 1544 | str = strdup(src->val.str); |
| 1545 | } |
| 1546 | |
| 1547 | /* ignore errors, the value may not satisfy schema constraints */ |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 1548 | rc = type->plugin->store(src->ctx, type, str, strlen(str), LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_DYNAMIC, |
| 1549 | LY_PREF_JSON, NULL, NULL, NULL, &val, &err); |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1550 | ly_err_free(err); |
| 1551 | if (rc) { |
| 1552 | /* invalid value */ |
| 1553 | free(str); |
| 1554 | goto fill; |
| 1555 | } |
| 1556 | |
| 1557 | /* storing successful, now print the canonical value */ |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 1558 | str = (char *)type->plugin->print(&val, LY_PREF_JSON, NULL, &dynamic); |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 1559 | |
| 1560 | /* use the canonized value */ |
| 1561 | set_init(trg, src); |
| 1562 | trg->type = src->type; |
| 1563 | if (src->type == LYXP_SET_NUMBER) { |
| 1564 | trg->val.num = strtold(str, &ptr); |
| 1565 | if (dynamic) { |
| 1566 | free(str); |
| 1567 | } |
| 1568 | LY_CHECK_ERR_RET(ptr[0], LOGINT(src->ctx), LY_EINT); |
| 1569 | } else { |
| 1570 | trg->val.str = (dynamic ? str : strdup(str)); |
| 1571 | } |
| 1572 | type->plugin->free(src->ctx, &val); |
| 1573 | return LY_SUCCESS; |
| 1574 | |
| 1575 | fill: |
| 1576 | /* no canonization needed/possible */ |
| 1577 | set_fill_set(trg, src); |
| 1578 | return LY_SUCCESS; |
| 1579 | } |
| 1580 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1581 | #ifndef NDEBUG |
| 1582 | |
| 1583 | /** |
| 1584 | * @brief Bubble sort @p set into XPath document order. |
| 1585 | * Context position aware. Unused in the 'Release' build target. |
| 1586 | * |
| 1587 | * @param[in] set Set to sort. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1588 | * @return How many times the whole set was traversed - 1 (if set was sorted, returns 0). |
| 1589 | */ |
| 1590 | static int |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1591 | set_sort(struct lyxp_set *set) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1592 | { |
| 1593 | uint32_t i, j; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 1594 | int ret = 0, cmp; |
| 1595 | uint8_t inverted, change; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1596 | const struct lyd_node *root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1597 | struct lyxp_set_node item; |
| 1598 | struct lyxp_set_hash_node hnode; |
| 1599 | uint64_t hash; |
| 1600 | |
Michal Vasko | 3cf8fbf | 2020-05-27 15:21:21 +0200 | [diff] [blame] | 1601 | if ((set->type != LYXP_SET_NODE_SET) || (set->used < 2)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1602 | return 0; |
| 1603 | } |
| 1604 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1605 | /* find first top-level node to be used as anchor for positions */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 1606 | for (root = set->ctx_node; root->parent; root = (const struct lyd_node *)root->parent) {} |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 1607 | for ( ; root->prev->next; root = root->prev) {} |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1608 | |
| 1609 | /* fill positions */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1610 | if (set_assign_pos(set, root, set->root_type)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1611 | return -1; |
| 1612 | } |
| 1613 | |
| 1614 | LOGDBG(LY_LDGXPATH, "SORT BEGIN"); |
| 1615 | print_set_debug(set); |
| 1616 | |
| 1617 | for (i = 0; i < set->used; ++i) { |
| 1618 | inverted = 0; |
| 1619 | change = 0; |
| 1620 | |
| 1621 | for (j = 1; j < set->used - i; ++j) { |
| 1622 | /* compare node positions */ |
| 1623 | if (inverted) { |
| 1624 | cmp = set_sort_compare(&set->val.nodes[j], &set->val.nodes[j - 1]); |
| 1625 | } else { |
| 1626 | cmp = set_sort_compare(&set->val.nodes[j - 1], &set->val.nodes[j]); |
| 1627 | } |
| 1628 | |
| 1629 | /* swap if needed */ |
| 1630 | if ((inverted && (cmp < 0)) || (!inverted && (cmp > 0))) { |
| 1631 | change = 1; |
| 1632 | |
| 1633 | item = set->val.nodes[j - 1]; |
| 1634 | set->val.nodes[j - 1] = set->val.nodes[j]; |
| 1635 | set->val.nodes[j] = item; |
| 1636 | } else { |
| 1637 | /* whether node_pos1 should be smaller than node_pos2 or the other way around */ |
| 1638 | inverted = !inverted; |
| 1639 | } |
| 1640 | } |
| 1641 | |
| 1642 | ++ret; |
| 1643 | |
| 1644 | if (!change) { |
| 1645 | break; |
| 1646 | } |
| 1647 | } |
| 1648 | |
| 1649 | LOGDBG(LY_LDGXPATH, "SORT END %d", ret); |
| 1650 | print_set_debug(set); |
| 1651 | |
| 1652 | /* check node hashes */ |
| 1653 | if (set->used >= LYD_HT_MIN_ITEMS) { |
| 1654 | assert(set->ht); |
| 1655 | for (i = 0; i < set->used; ++i) { |
| 1656 | hnode.node = set->val.nodes[i].node; |
| 1657 | hnode.type = set->val.nodes[i].type; |
| 1658 | |
| 1659 | hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node); |
| 1660 | hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type); |
| 1661 | hash = dict_hash_multi(hash, NULL, 0); |
| 1662 | |
| 1663 | assert(!lyht_find(set->ht, &hnode, hash, NULL)); |
| 1664 | } |
| 1665 | } |
| 1666 | |
| 1667 | return ret - 1; |
| 1668 | } |
| 1669 | |
| 1670 | /** |
| 1671 | * @brief Remove duplicate entries in a sorted node set. |
| 1672 | * |
| 1673 | * @param[in] set Sorted set to check. |
| 1674 | * @return LY_ERR (LY_EEXIST if some duplicates are found) |
| 1675 | */ |
| 1676 | static LY_ERR |
| 1677 | set_sorted_dup_node_clean(struct lyxp_set *set) |
| 1678 | { |
| 1679 | uint32_t i = 0; |
| 1680 | LY_ERR ret = LY_SUCCESS; |
| 1681 | |
| 1682 | if (set->used > 1) { |
| 1683 | while (i < set->used - 1) { |
| 1684 | if ((set->val.nodes[i].node == set->val.nodes[i + 1].node) |
| 1685 | && (set->val.nodes[i].type == set->val.nodes[i + 1].type)) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1686 | set_remove_node_none(set, i + 1); |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1687 | ret = LY_EEXIST; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1688 | } |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 1689 | ++i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1690 | } |
| 1691 | } |
| 1692 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 1693 | set_remove_nodes_none(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1694 | return ret; |
| 1695 | } |
| 1696 | |
| 1697 | #endif |
| 1698 | |
| 1699 | /** |
| 1700 | * @brief Merge 2 sorted sets into one. |
| 1701 | * |
| 1702 | * @param[in,out] trg Set to merge into. Duplicates are removed. |
| 1703 | * @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] | 1704 | * @return LY_ERR |
| 1705 | */ |
| 1706 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1707 | set_sorted_merge(struct lyxp_set *trg, struct lyxp_set *src) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1708 | { |
| 1709 | uint32_t i, j, k, count, dup_count; |
| 1710 | int cmp; |
| 1711 | const struct lyd_node *root; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1712 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1713 | 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] | 1714 | return LY_EINVAL; |
| 1715 | } |
| 1716 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1717 | if (!src->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1718 | return LY_SUCCESS; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1719 | } else if (!trg->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1720 | set_fill_set(trg, src); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1721 | lyxp_set_free_content(src); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1722 | return LY_SUCCESS; |
| 1723 | } |
| 1724 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1725 | /* find first top-level node to be used as anchor for positions */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 1726 | for (root = trg->ctx_node; root->parent; root = (const struct lyd_node *)root->parent) {} |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 1727 | for ( ; root->prev->next; root = root->prev) {} |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1728 | |
| 1729 | /* fill positions */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 1730 | 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] | 1731 | return LY_EINT; |
| 1732 | } |
| 1733 | |
| 1734 | #ifndef NDEBUG |
| 1735 | LOGDBG(LY_LDGXPATH, "MERGE target"); |
| 1736 | print_set_debug(trg); |
| 1737 | LOGDBG(LY_LDGXPATH, "MERGE source"); |
| 1738 | print_set_debug(src); |
| 1739 | #endif |
| 1740 | |
| 1741 | /* make memory for the merge (duplicates are not detected yet, so space |
| 1742 | * will likely be wasted on them, too bad) */ |
| 1743 | if (trg->size - trg->used < src->used) { |
| 1744 | trg->size = trg->used + src->used; |
| 1745 | |
| 1746 | trg->val.nodes = ly_realloc(trg->val.nodes, trg->size * sizeof *trg->val.nodes); |
| 1747 | LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx), LY_EMEM); |
| 1748 | } |
| 1749 | |
| 1750 | i = 0; |
| 1751 | j = 0; |
| 1752 | count = 0; |
| 1753 | dup_count = 0; |
| 1754 | do { |
| 1755 | cmp = set_sort_compare(&src->val.nodes[i], &trg->val.nodes[j]); |
| 1756 | if (!cmp) { |
| 1757 | if (!count) { |
| 1758 | /* duplicate, just skip it */ |
| 1759 | ++i; |
| 1760 | ++j; |
| 1761 | } else { |
| 1762 | /* we are copying something already, so let's copy the duplicate too, |
| 1763 | * we are hoping that afterwards there are some more nodes to |
| 1764 | * copy and this way we can copy them all together */ |
| 1765 | ++count; |
| 1766 | ++dup_count; |
| 1767 | ++i; |
| 1768 | ++j; |
| 1769 | } |
| 1770 | } else if (cmp < 0) { |
| 1771 | /* inserting src node into trg, just remember it for now */ |
| 1772 | ++count; |
| 1773 | ++i; |
| 1774 | |
| 1775 | /* insert the hash now */ |
| 1776 | set_insert_node_hash(trg, src->val.nodes[i - 1].node, src->val.nodes[i - 1].type); |
| 1777 | } else if (count) { |
| 1778 | copy_nodes: |
| 1779 | /* time to actually copy the nodes, we have found the largest block of nodes */ |
| 1780 | memmove(&trg->val.nodes[j + (count - dup_count)], |
| 1781 | &trg->val.nodes[j], |
| 1782 | (trg->used - j) * sizeof *trg->val.nodes); |
| 1783 | memcpy(&trg->val.nodes[j - dup_count], &src->val.nodes[i - count], count * sizeof *src->val.nodes); |
| 1784 | |
| 1785 | trg->used += count - dup_count; |
| 1786 | /* do not change i, except the copying above, we are basically doing exactly what is in the else branch below */ |
| 1787 | j += count - dup_count; |
| 1788 | |
| 1789 | count = 0; |
| 1790 | dup_count = 0; |
| 1791 | } else { |
| 1792 | ++j; |
| 1793 | } |
| 1794 | } while ((i < src->used) && (j < trg->used)); |
| 1795 | |
| 1796 | if ((i < src->used) || count) { |
| 1797 | /* insert all the hashes first */ |
| 1798 | for (k = i; k < src->used; ++k) { |
| 1799 | set_insert_node_hash(trg, src->val.nodes[k].node, src->val.nodes[k].type); |
| 1800 | } |
| 1801 | |
| 1802 | /* loop ended, but we need to copy something at trg end */ |
| 1803 | count += src->used - i; |
| 1804 | i = src->used; |
| 1805 | goto copy_nodes; |
| 1806 | } |
| 1807 | |
| 1808 | /* we are inserting hashes before the actual node insert, which causes |
| 1809 | * situations when there were initially not enough items for a hash table, |
| 1810 | * but even after some were inserted, hash table was not created (during |
| 1811 | * insertion the number of items is not updated yet) */ |
| 1812 | if (!trg->ht && (trg->used >= LYD_HT_MIN_ITEMS)) { |
| 1813 | set_insert_node_hash(trg, NULL, 0); |
| 1814 | } |
| 1815 | |
| 1816 | #ifndef NDEBUG |
| 1817 | LOGDBG(LY_LDGXPATH, "MERGE result"); |
| 1818 | print_set_debug(trg); |
| 1819 | #endif |
| 1820 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 1821 | lyxp_set_free_content(src); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1822 | return LY_SUCCESS; |
| 1823 | } |
| 1824 | |
| 1825 | /* |
| 1826 | * (re)parse functions |
| 1827 | * |
| 1828 | * Parse functions parse the expression into |
| 1829 | * tokens (syntactic analysis). |
| 1830 | * |
| 1831 | * Reparse functions perform semantic analysis |
| 1832 | * (do not save the result, just a check) of |
| 1833 | * the expression and fill repeat indices. |
| 1834 | */ |
| 1835 | |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1836 | LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1837 | 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] | 1838 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1839 | if (exp->used == tok_idx) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1840 | if (ctx) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1841 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF); |
| 1842 | } |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1843 | return LY_EINCOMPLETE; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1844 | } |
| 1845 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1846 | if (want_tok && (exp->tokens[tok_idx] != want_tok)) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1847 | if (ctx) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1848 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1849 | lyxp_print_token(exp->tokens[tok_idx]), &exp->expr[exp->tok_pos[tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1850 | } |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1851 | return LY_ENOT; |
| 1852 | } |
| 1853 | |
| 1854 | return LY_SUCCESS; |
| 1855 | } |
| 1856 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1857 | LY_ERR |
| 1858 | lyxp_next_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok) |
| 1859 | { |
| 1860 | LY_CHECK_RET(lyxp_check_token(ctx, exp, *tok_idx, want_tok)); |
| 1861 | |
| 1862 | /* skip the token */ |
| 1863 | ++(*tok_idx); |
| 1864 | |
| 1865 | return LY_SUCCESS; |
| 1866 | } |
| 1867 | |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1868 | /* just like lyxp_check_token() but tests for 2 tokens */ |
| 1869 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1870 | exp_check_token2(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t tok_idx, enum lyxp_token want_tok1, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1871 | enum lyxp_token want_tok2) |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1872 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1873 | if (exp->used == tok_idx) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 1874 | if (ctx) { |
| 1875 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF); |
| 1876 | } |
| 1877 | return LY_EINCOMPLETE; |
| 1878 | } |
| 1879 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1880 | 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] | 1881 | if (ctx) { |
| 1882 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1883 | 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] | 1884 | } |
| 1885 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1886 | } |
| 1887 | |
| 1888 | return LY_SUCCESS; |
| 1889 | } |
| 1890 | |
| 1891 | /** |
| 1892 | * @brief Stack operation push on the repeat array. |
| 1893 | * |
| 1894 | * @param[in] exp Expression to use. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1895 | * @param[in] tok_idx Position in the expresion \p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1896 | * @param[in] repeat_op_idx Index from \p exp of the operator token. This value is pushed. |
| 1897 | */ |
| 1898 | static void |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1899 | 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] | 1900 | { |
| 1901 | uint16_t i; |
| 1902 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1903 | if (exp->repeat[tok_idx]) { |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 1904 | for (i = 0; exp->repeat[tok_idx][i]; ++i) {} |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1905 | exp->repeat[tok_idx] = realloc(exp->repeat[tok_idx], (i + 2) * sizeof *exp->repeat[tok_idx]); |
| 1906 | LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), ); |
| 1907 | exp->repeat[tok_idx][i] = repeat_op_idx; |
| 1908 | exp->repeat[tok_idx][i + 1] = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1909 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1910 | exp->repeat[tok_idx] = calloc(2, sizeof *exp->repeat[tok_idx]); |
| 1911 | LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), ); |
| 1912 | exp->repeat[tok_idx][0] = repeat_op_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1913 | } |
| 1914 | } |
| 1915 | |
| 1916 | /** |
| 1917 | * @brief Reparse Predicate. Logs directly on error. |
| 1918 | * |
| 1919 | * [7] Predicate ::= '[' Expr ']' |
| 1920 | * |
| 1921 | * @param[in] ctx Context for logging. |
| 1922 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1923 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1924 | * @return LY_ERR |
| 1925 | */ |
| 1926 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1927 | 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] | 1928 | { |
| 1929 | LY_ERR rc; |
| 1930 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1931 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1932 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1933 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1934 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1935 | rc = reparse_or_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1936 | LY_CHECK_RET(rc); |
| 1937 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1938 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK2); |
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 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1941 | |
| 1942 | return LY_SUCCESS; |
| 1943 | } |
| 1944 | |
| 1945 | /** |
| 1946 | * @brief Reparse RelativeLocationPath. Logs directly on error. |
| 1947 | * |
| 1948 | * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step |
| 1949 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 1950 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 1951 | * |
| 1952 | * @param[in] ctx Context for logging. |
| 1953 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1954 | * @param[in] tok_idx Position in the expression \p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1955 | * @return LY_ERR (LY_EINCOMPLETE on forward reference) |
| 1956 | */ |
| 1957 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1958 | 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] | 1959 | { |
| 1960 | LY_ERR rc; |
| 1961 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1962 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1963 | LY_CHECK_RET(rc); |
| 1964 | |
| 1965 | goto step; |
| 1966 | do { |
| 1967 | /* '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1968 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1969 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1970 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1971 | LY_CHECK_RET(rc); |
| 1972 | step: |
| 1973 | /* Step */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1974 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1975 | case LYXP_TOKEN_DOT: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1976 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1977 | break; |
| 1978 | |
| 1979 | case LYXP_TOKEN_DDOT: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1980 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1981 | break; |
| 1982 | |
| 1983 | case LYXP_TOKEN_AT: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1984 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1985 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1986 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1987 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1988 | 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] | 1989 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1990 | 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] | 1991 | return LY_EVALID; |
| 1992 | } |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 1993 | /* fall through */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1994 | case LYXP_TOKEN_NAMETEST: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 1995 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 1996 | goto reparse_predicate; |
| 1997 | break; |
| 1998 | |
| 1999 | case LYXP_TOKEN_NODETYPE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2000 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2001 | |
| 2002 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2003 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2004 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2005 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2006 | |
| 2007 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2008 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2009 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2010 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2011 | |
| 2012 | reparse_predicate: |
| 2013 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2014 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 2015 | rc = reparse_predicate(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2016 | LY_CHECK_RET(rc); |
| 2017 | } |
| 2018 | break; |
| 2019 | default: |
| 2020 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2021 | 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] | 2022 | return LY_EVALID; |
| 2023 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2024 | } 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] | 2025 | |
| 2026 | return LY_SUCCESS; |
| 2027 | } |
| 2028 | |
| 2029 | /** |
| 2030 | * @brief Reparse AbsoluteLocationPath. Logs directly on error. |
| 2031 | * |
| 2032 | * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath |
| 2033 | * |
| 2034 | * @param[in] ctx Context for logging. |
| 2035 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2036 | * @param[in] tok_idx Position in the expression \p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2037 | * @return LY_ERR |
| 2038 | */ |
| 2039 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2040 | 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] | 2041 | { |
| 2042 | LY_ERR rc; |
| 2043 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2044 | 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] | 2045 | |
| 2046 | /* '/' RelativeLocationPath? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2047 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2048 | /* '/' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2049 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2050 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2051 | if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2052 | return LY_SUCCESS; |
| 2053 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2054 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2055 | case LYXP_TOKEN_DOT: |
| 2056 | case LYXP_TOKEN_DDOT: |
| 2057 | case LYXP_TOKEN_AT: |
| 2058 | case LYXP_TOKEN_NAMETEST: |
| 2059 | case LYXP_TOKEN_NODETYPE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2060 | rc = reparse_relative_location_path(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2061 | LY_CHECK_RET(rc); |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 2062 | /* fall through */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2063 | default: |
| 2064 | break; |
| 2065 | } |
| 2066 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2067 | } else { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 2068 | /* '//' RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2069 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2070 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2071 | rc = reparse_relative_location_path(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2072 | LY_CHECK_RET(rc); |
| 2073 | } |
| 2074 | |
| 2075 | return LY_SUCCESS; |
| 2076 | } |
| 2077 | |
| 2078 | /** |
| 2079 | * @brief Reparse FunctionCall. Logs directly on error. |
| 2080 | * |
| 2081 | * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')' |
| 2082 | * |
| 2083 | * @param[in] ctx Context for logging. |
| 2084 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2085 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2086 | * @return LY_ERR |
| 2087 | */ |
| 2088 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2089 | 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] | 2090 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2091 | int8_t min_arg_count = -1; |
| 2092 | uint32_t arg_count, max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2093 | uint16_t func_tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2094 | LY_ERR rc; |
| 2095 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2096 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2097 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2098 | func_tok_idx = *tok_idx; |
| 2099 | switch (exp->tok_len[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2100 | case 3: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2101 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2102 | min_arg_count = 1; |
| 2103 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2104 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2105 | min_arg_count = 1; |
| 2106 | max_arg_count = 1; |
| 2107 | } |
| 2108 | break; |
| 2109 | case 4: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2110 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) { |
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]], "last", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2114 | min_arg_count = 0; |
| 2115 | max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2116 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2117 | min_arg_count = 0; |
| 2118 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2119 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2120 | min_arg_count = 0; |
| 2121 | max_arg_count = 0; |
| 2122 | } |
| 2123 | break; |
| 2124 | case 5: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2125 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2126 | min_arg_count = 1; |
| 2127 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2128 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2129 | min_arg_count = 0; |
| 2130 | max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2131 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2132 | min_arg_count = 1; |
| 2133 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2134 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2135 | min_arg_count = 1; |
| 2136 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2137 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2138 | min_arg_count = 1; |
| 2139 | max_arg_count = 1; |
| 2140 | } |
| 2141 | break; |
| 2142 | case 6: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2143 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2144 | min_arg_count = 2; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2145 | max_arg_count = UINT32_MAX; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2146 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2147 | min_arg_count = 0; |
| 2148 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2149 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2150 | min_arg_count = 0; |
| 2151 | max_arg_count = 1; |
| 2152 | } |
| 2153 | break; |
| 2154 | case 7: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2155 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2156 | min_arg_count = 1; |
| 2157 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2158 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2159 | min_arg_count = 1; |
| 2160 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2161 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2162 | min_arg_count = 0; |
| 2163 | max_arg_count = 0; |
| 2164 | } |
| 2165 | break; |
| 2166 | case 8: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2167 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2168 | min_arg_count = 2; |
| 2169 | max_arg_count = 2; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2170 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2171 | min_arg_count = 0; |
| 2172 | max_arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2173 | } 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] | 2174 | min_arg_count = 2; |
| 2175 | max_arg_count = 2; |
| 2176 | } |
| 2177 | break; |
| 2178 | case 9: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2179 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2180 | min_arg_count = 2; |
| 2181 | max_arg_count = 3; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2182 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2183 | min_arg_count = 3; |
| 2184 | max_arg_count = 3; |
| 2185 | } |
| 2186 | break; |
| 2187 | case 10: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2188 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2189 | min_arg_count = 0; |
| 2190 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2191 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2192 | min_arg_count = 1; |
| 2193 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2194 | } 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] | 2195 | min_arg_count = 2; |
| 2196 | max_arg_count = 2; |
| 2197 | } |
| 2198 | break; |
| 2199 | case 11: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2200 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2201 | min_arg_count = 2; |
| 2202 | max_arg_count = 2; |
| 2203 | } |
| 2204 | break; |
| 2205 | case 12: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2206 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2207 | min_arg_count = 2; |
| 2208 | max_arg_count = 2; |
| 2209 | } |
| 2210 | break; |
| 2211 | case 13: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2212 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2213 | min_arg_count = 0; |
| 2214 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2215 | } 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] | 2216 | min_arg_count = 0; |
| 2217 | max_arg_count = 1; |
| 2218 | } |
| 2219 | break; |
| 2220 | case 15: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2221 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2222 | min_arg_count = 0; |
| 2223 | max_arg_count = 1; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2224 | } 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] | 2225 | min_arg_count = 2; |
| 2226 | max_arg_count = 2; |
| 2227 | } |
| 2228 | break; |
| 2229 | case 16: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2230 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2231 | min_arg_count = 2; |
| 2232 | max_arg_count = 2; |
| 2233 | } |
| 2234 | break; |
| 2235 | case 20: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2236 | 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] | 2237 | min_arg_count = 2; |
| 2238 | max_arg_count = 2; |
| 2239 | } |
| 2240 | break; |
| 2241 | } |
| 2242 | if (min_arg_count == -1) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2243 | 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] | 2244 | return LY_EINVAL; |
| 2245 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2246 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2247 | |
| 2248 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2249 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2250 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2251 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2252 | |
| 2253 | /* ( Expr ( ',' Expr )* )? */ |
| 2254 | arg_count = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2255 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2256 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2257 | if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2258 | ++arg_count; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2259 | rc = reparse_or_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2260 | LY_CHECK_RET(rc); |
| 2261 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2262 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) { |
| 2263 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2264 | |
| 2265 | ++arg_count; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2266 | rc = reparse_or_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2267 | LY_CHECK_RET(rc); |
| 2268 | } |
| 2269 | |
| 2270 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2271 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2272 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2273 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2274 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2275 | if ((arg_count < (uint8_t)min_arg_count) || (arg_count > max_arg_count)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2276 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INARGCOUNT, arg_count, exp->tok_len[func_tok_idx], |
| 2277 | &exp->expr[exp->tok_pos[func_tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2278 | return LY_EVALID; |
| 2279 | } |
| 2280 | |
| 2281 | return LY_SUCCESS; |
| 2282 | } |
| 2283 | |
| 2284 | /** |
| 2285 | * @brief Reparse PathExpr. Logs directly on error. |
| 2286 | * |
| 2287 | * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate* |
| 2288 | * | PrimaryExpr Predicate* '/' RelativeLocationPath |
| 2289 | * | PrimaryExpr Predicate* '//' RelativeLocationPath |
| 2290 | * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath |
| 2291 | * [8] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall |
| 2292 | * |
| 2293 | * @param[in] ctx Context for logging. |
| 2294 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2295 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2296 | * @return LY_ERR |
| 2297 | */ |
| 2298 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2299 | 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] | 2300 | { |
| 2301 | LY_ERR rc; |
| 2302 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2303 | if (lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 2304 | return LY_EVALID; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2305 | } |
| 2306 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2307 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2308 | case LYXP_TOKEN_PAR1: |
| 2309 | /* '(' Expr ')' Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2310 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2311 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2312 | rc = reparse_or_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2313 | LY_CHECK_RET(rc); |
| 2314 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2315 | rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2316 | LY_CHECK_RET(rc); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2317 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2318 | goto predicate; |
| 2319 | break; |
| 2320 | case LYXP_TOKEN_DOT: |
| 2321 | case LYXP_TOKEN_DDOT: |
| 2322 | case LYXP_TOKEN_AT: |
| 2323 | case LYXP_TOKEN_NAMETEST: |
| 2324 | case LYXP_TOKEN_NODETYPE: |
| 2325 | /* RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2326 | rc = reparse_relative_location_path(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2327 | LY_CHECK_RET(rc); |
| 2328 | break; |
| 2329 | case LYXP_TOKEN_FUNCNAME: |
| 2330 | /* FunctionCall */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2331 | rc = reparse_function_call(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2332 | LY_CHECK_RET(rc); |
| 2333 | goto predicate; |
| 2334 | break; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2335 | case LYXP_TOKEN_OPER_PATH: |
| 2336 | case LYXP_TOKEN_OPER_RPATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2337 | /* AbsoluteLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2338 | rc = reparse_absolute_location_path(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2339 | LY_CHECK_RET(rc); |
| 2340 | break; |
| 2341 | case LYXP_TOKEN_LITERAL: |
| 2342 | /* Literal */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2343 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2344 | goto predicate; |
| 2345 | break; |
| 2346 | case LYXP_TOKEN_NUMBER: |
| 2347 | /* Number */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2348 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2349 | goto predicate; |
| 2350 | break; |
| 2351 | default: |
| 2352 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2353 | 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] | 2354 | return LY_EVALID; |
| 2355 | } |
| 2356 | |
| 2357 | return LY_SUCCESS; |
| 2358 | |
| 2359 | predicate: |
| 2360 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2361 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 2362 | rc = reparse_predicate(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2363 | LY_CHECK_RET(rc); |
| 2364 | } |
| 2365 | |
| 2366 | /* ('/' or '//') RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2367 | 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] | 2368 | |
| 2369 | /* '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2370 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2371 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2372 | rc = reparse_relative_location_path(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2373 | LY_CHECK_RET(rc); |
| 2374 | } |
| 2375 | |
| 2376 | return LY_SUCCESS; |
| 2377 | } |
| 2378 | |
| 2379 | /** |
| 2380 | * @brief Reparse UnaryExpr. Logs directly on error. |
| 2381 | * |
| 2382 | * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr |
| 2383 | * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr |
| 2384 | * |
| 2385 | * @param[in] ctx Context for logging. |
| 2386 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2387 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2388 | * @return LY_ERR |
| 2389 | */ |
| 2390 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2391 | 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] | 2392 | { |
| 2393 | uint16_t prev_exp; |
| 2394 | LY_ERR rc; |
| 2395 | |
| 2396 | /* ('-')* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2397 | prev_exp = *tok_idx; |
| 2398 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) |
| 2399 | && (exp->expr[exp->tok_pos[*tok_idx]] == '-')) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2400 | exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2401 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2402 | } |
| 2403 | |
| 2404 | /* PathExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2405 | prev_exp = *tok_idx; |
| 2406 | rc = reparse_path_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2407 | LY_CHECK_RET(rc); |
| 2408 | |
| 2409 | /* ('|' PathExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2410 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_UNI)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2411 | exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2412 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2413 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2414 | rc = reparse_path_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2415 | LY_CHECK_RET(rc); |
| 2416 | } |
| 2417 | |
| 2418 | return LY_SUCCESS; |
| 2419 | } |
| 2420 | |
| 2421 | /** |
| 2422 | * @brief Reparse AdditiveExpr. Logs directly on error. |
| 2423 | * |
| 2424 | * [15] AdditiveExpr ::= MultiplicativeExpr |
| 2425 | * | AdditiveExpr '+' MultiplicativeExpr |
| 2426 | * | AdditiveExpr '-' MultiplicativeExpr |
| 2427 | * [16] MultiplicativeExpr ::= UnaryExpr |
| 2428 | * | MultiplicativeExpr '*' UnaryExpr |
| 2429 | * | MultiplicativeExpr 'div' UnaryExpr |
| 2430 | * | MultiplicativeExpr 'mod' UnaryExpr |
| 2431 | * |
| 2432 | * @param[in] ctx Context for logging. |
| 2433 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2434 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2435 | * @return LY_ERR |
| 2436 | */ |
| 2437 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2438 | 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] | 2439 | { |
| 2440 | uint16_t prev_add_exp, prev_mul_exp; |
| 2441 | LY_ERR rc; |
| 2442 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2443 | prev_add_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2444 | goto reparse_multiplicative_expr; |
| 2445 | |
| 2446 | /* ('+' / '-' MultiplicativeExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2447 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) |
| 2448 | && ((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] | 2449 | exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2450 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2451 | |
| 2452 | reparse_multiplicative_expr: |
| 2453 | /* UnaryExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2454 | prev_mul_exp = *tok_idx; |
| 2455 | rc = reparse_unary_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2456 | LY_CHECK_RET(rc); |
| 2457 | |
| 2458 | /* ('*' / 'div' / 'mod' UnaryExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2459 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) |
| 2460 | && ((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] | 2461 | exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2462 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2463 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2464 | rc = reparse_unary_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2465 | LY_CHECK_RET(rc); |
| 2466 | } |
| 2467 | } |
| 2468 | |
| 2469 | return LY_SUCCESS; |
| 2470 | } |
| 2471 | |
| 2472 | /** |
| 2473 | * @brief Reparse EqualityExpr. Logs directly on error. |
| 2474 | * |
| 2475 | * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr |
| 2476 | * | EqualityExpr '!=' RelationalExpr |
| 2477 | * [14] RelationalExpr ::= AdditiveExpr |
| 2478 | * | RelationalExpr '<' AdditiveExpr |
| 2479 | * | RelationalExpr '>' AdditiveExpr |
| 2480 | * | RelationalExpr '<=' AdditiveExpr |
| 2481 | * | RelationalExpr '>=' AdditiveExpr |
| 2482 | * |
| 2483 | * @param[in] ctx Context for logging. |
| 2484 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2485 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2486 | * @return LY_ERR |
| 2487 | */ |
| 2488 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2489 | 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] | 2490 | { |
| 2491 | uint16_t prev_eq_exp, prev_rel_exp; |
| 2492 | LY_ERR rc; |
| 2493 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2494 | prev_eq_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2495 | goto reparse_additive_expr; |
| 2496 | |
| 2497 | /* ('=' / '!=' RelationalExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2498 | 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] | 2499 | exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2500 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2501 | |
| 2502 | reparse_additive_expr: |
| 2503 | /* AdditiveExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2504 | prev_rel_exp = *tok_idx; |
| 2505 | rc = reparse_additive_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2506 | LY_CHECK_RET(rc); |
| 2507 | |
| 2508 | /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2509 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_COMP)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2510 | exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2511 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2512 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2513 | rc = reparse_additive_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2514 | LY_CHECK_RET(rc); |
| 2515 | } |
| 2516 | } |
| 2517 | |
| 2518 | return LY_SUCCESS; |
| 2519 | } |
| 2520 | |
| 2521 | /** |
| 2522 | * @brief Reparse OrExpr. Logs directly on error. |
| 2523 | * |
| 2524 | * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr |
| 2525 | * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr |
| 2526 | * |
| 2527 | * @param[in] ctx Context for logging. |
| 2528 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2529 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2530 | * @return LY_ERR |
| 2531 | */ |
| 2532 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2533 | 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] | 2534 | { |
| 2535 | uint16_t prev_or_exp, prev_and_exp; |
| 2536 | LY_ERR rc; |
| 2537 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2538 | prev_or_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2539 | goto reparse_equality_expr; |
| 2540 | |
| 2541 | /* ('or' AndExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2542 | 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] | 2543 | exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2544 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2545 | |
| 2546 | reparse_equality_expr: |
| 2547 | /* EqualityExpr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2548 | prev_and_exp = *tok_idx; |
| 2549 | rc = reparse_equality_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2550 | LY_CHECK_RET(rc); |
| 2551 | |
| 2552 | /* ('and' EqualityExpr)* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2553 | 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] | 2554 | exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2555 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2556 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2557 | rc = reparse_equality_expr(ctx, exp, tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2558 | LY_CHECK_RET(rc); |
| 2559 | } |
| 2560 | } |
| 2561 | |
| 2562 | return LY_SUCCESS; |
| 2563 | } |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2564 | |
| 2565 | /** |
| 2566 | * @brief Parse NCName. |
| 2567 | * |
| 2568 | * @param[in] ncname Name to parse. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2569 | * @return Length of @p ncname valid bytes. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2570 | */ |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2571 | static long int |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2572 | parse_ncname(const char *ncname) |
| 2573 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2574 | uint32_t uc; |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2575 | size_t size; |
| 2576 | long int len = 0; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2577 | |
| 2578 | LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0); |
| 2579 | if (!is_xmlqnamestartchar(uc) || (uc == ':')) { |
| 2580 | return len; |
| 2581 | } |
| 2582 | |
| 2583 | do { |
| 2584 | len += size; |
Radek Krejci | 9a564c9 | 2019-01-07 14:53:57 +0100 | [diff] [blame] | 2585 | if (!*ncname) { |
| 2586 | break; |
| 2587 | } |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2588 | LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2589 | } while (is_xmlqnamechar(uc) && (uc != ':')); |
| 2590 | |
| 2591 | return len; |
| 2592 | } |
| 2593 | |
| 2594 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2595 | * @brief Add @p token into the expression @p exp. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2596 | * |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2597 | * @param[in] ctx Context for logging. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2598 | * @param[in] exp Expression to use. |
| 2599 | * @param[in] token Token to add. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2600 | * @param[in] tok_pos Token position in the XPath expression. |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2601 | * @param[in] tok_len Token length in the XPath expression. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2602 | * @return LY_ERR |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2603 | */ |
| 2604 | static LY_ERR |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 2605 | 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] | 2606 | { |
| 2607 | uint32_t prev; |
| 2608 | |
| 2609 | if (exp->used == exp->size) { |
| 2610 | prev = exp->size; |
| 2611 | exp->size += LYXP_EXPR_SIZE_STEP; |
| 2612 | if (prev > exp->size) { |
| 2613 | LOGINT(ctx); |
| 2614 | return LY_EINT; |
| 2615 | } |
| 2616 | |
| 2617 | exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens); |
| 2618 | LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM); |
| 2619 | exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos); |
| 2620 | LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM); |
| 2621 | exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len); |
| 2622 | LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM); |
| 2623 | } |
| 2624 | |
| 2625 | exp->tokens[exp->used] = token; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2626 | exp->tok_pos[exp->used] = tok_pos; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2627 | exp->tok_len[exp->used] = tok_len; |
| 2628 | ++exp->used; |
| 2629 | return LY_SUCCESS; |
| 2630 | } |
| 2631 | |
| 2632 | void |
Michal Vasko | 1467635 | 2020-05-29 11:35:55 +0200 | [diff] [blame] | 2633 | lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr) |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2634 | { |
| 2635 | uint16_t i; |
| 2636 | |
| 2637 | if (!expr) { |
| 2638 | return; |
| 2639 | } |
| 2640 | |
| 2641 | lydict_remove(ctx, expr->expr); |
| 2642 | free(expr->tokens); |
| 2643 | free(expr->tok_pos); |
| 2644 | free(expr->tok_len); |
| 2645 | if (expr->repeat) { |
| 2646 | for (i = 0; i < expr->used; ++i) { |
| 2647 | free(expr->repeat[i]); |
| 2648 | } |
| 2649 | } |
| 2650 | free(expr->repeat); |
| 2651 | free(expr); |
| 2652 | } |
| 2653 | |
| 2654 | struct lyxp_expr * |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2655 | lyxp_expr_parse(const struct ly_ctx *ctx, const char *expr, size_t expr_len, uint8_t reparse) |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2656 | { |
| 2657 | struct lyxp_expr *ret; |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2658 | size_t parsed = 0, tok_len; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2659 | enum lyxp_token tok_type; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2660 | uint8_t prev_function_check = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2661 | uint16_t tok_idx = 0; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2662 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2663 | if (!expr[0]) { |
| 2664 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF); |
| 2665 | return NULL; |
| 2666 | } |
| 2667 | |
| 2668 | if (!expr_len) { |
| 2669 | expr_len = strlen(expr); |
| 2670 | } |
| 2671 | if (expr_len > UINT16_MAX) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2672 | LOGERR(ctx, LY_EINVAL, "XPath expression cannot be longer than %ud characters.", UINT16_MAX); |
| 2673 | return NULL; |
| 2674 | } |
| 2675 | |
| 2676 | /* init lyxp_expr structure */ |
| 2677 | ret = calloc(1, sizeof *ret); |
| 2678 | LY_CHECK_ERR_GOTO(!ret, LOGMEM(ctx), error); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2679 | ret->expr = lydict_insert(ctx, expr, expr_len); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2680 | LY_CHECK_ERR_GOTO(!ret->expr, LOGMEM(ctx), error); |
| 2681 | ret->used = 0; |
| 2682 | ret->size = LYXP_EXPR_SIZE_START; |
| 2683 | ret->tokens = malloc(ret->size * sizeof *ret->tokens); |
| 2684 | LY_CHECK_ERR_GOTO(!ret->tokens, LOGMEM(ctx), error); |
| 2685 | |
| 2686 | ret->tok_pos = malloc(ret->size * sizeof *ret->tok_pos); |
| 2687 | LY_CHECK_ERR_GOTO(!ret->tok_pos, LOGMEM(ctx), error); |
| 2688 | |
| 2689 | ret->tok_len = malloc(ret->size * sizeof *ret->tok_len); |
| 2690 | LY_CHECK_ERR_GOTO(!ret->tok_len, LOGMEM(ctx), error); |
| 2691 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2692 | /* make expr 0-terminated */ |
| 2693 | expr = ret->expr; |
| 2694 | |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2695 | while (is_xmlws(expr[parsed])) { |
| 2696 | ++parsed; |
| 2697 | } |
| 2698 | |
| 2699 | do { |
| 2700 | if (expr[parsed] == '(') { |
| 2701 | |
| 2702 | /* '(' */ |
| 2703 | tok_len = 1; |
| 2704 | tok_type = LYXP_TOKEN_PAR1; |
| 2705 | |
| 2706 | if (prev_function_check && ret->used && (ret->tokens[ret->used - 1] == LYXP_TOKEN_NAMETEST)) { |
| 2707 | /* it is a NodeType/FunctionName after all */ |
| 2708 | if (((ret->tok_len[ret->used - 1] == 4) |
| 2709 | && (!strncmp(&expr[ret->tok_pos[ret->used - 1]], "node", 4) |
| 2710 | || !strncmp(&expr[ret->tok_pos[ret->used - 1]], "text", 4))) || |
| 2711 | ((ret->tok_len[ret->used - 1] == 7) |
| 2712 | && !strncmp(&expr[ret->tok_pos[ret->used - 1]], "comment", 7))) { |
| 2713 | ret->tokens[ret->used - 1] = LYXP_TOKEN_NODETYPE; |
| 2714 | } else { |
| 2715 | ret->tokens[ret->used - 1] = LYXP_TOKEN_FUNCNAME; |
| 2716 | } |
| 2717 | prev_function_check = 0; |
| 2718 | } |
| 2719 | |
| 2720 | } else if (expr[parsed] == ')') { |
| 2721 | |
| 2722 | /* ')' */ |
| 2723 | tok_len = 1; |
| 2724 | tok_type = LYXP_TOKEN_PAR2; |
| 2725 | |
| 2726 | } else if (expr[parsed] == '[') { |
| 2727 | |
| 2728 | /* '[' */ |
| 2729 | tok_len = 1; |
| 2730 | tok_type = LYXP_TOKEN_BRACK1; |
| 2731 | |
| 2732 | } else if (expr[parsed] == ']') { |
| 2733 | |
| 2734 | /* ']' */ |
| 2735 | tok_len = 1; |
| 2736 | tok_type = LYXP_TOKEN_BRACK2; |
| 2737 | |
| 2738 | } else if (!strncmp(&expr[parsed], "..", 2)) { |
| 2739 | |
| 2740 | /* '..' */ |
| 2741 | tok_len = 2; |
| 2742 | tok_type = LYXP_TOKEN_DDOT; |
| 2743 | |
| 2744 | } else if ((expr[parsed] == '.') && (!isdigit(expr[parsed + 1]))) { |
| 2745 | |
| 2746 | /* '.' */ |
| 2747 | tok_len = 1; |
| 2748 | tok_type = LYXP_TOKEN_DOT; |
| 2749 | |
| 2750 | } else if (expr[parsed] == '@') { |
| 2751 | |
| 2752 | /* '@' */ |
| 2753 | tok_len = 1; |
| 2754 | tok_type = LYXP_TOKEN_AT; |
| 2755 | |
| 2756 | } else if (expr[parsed] == ',') { |
| 2757 | |
| 2758 | /* ',' */ |
| 2759 | tok_len = 1; |
| 2760 | tok_type = LYXP_TOKEN_COMMA; |
| 2761 | |
| 2762 | } else if (expr[parsed] == '\'') { |
| 2763 | |
| 2764 | /* Literal with ' */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 2765 | for (tok_len = 1; (expr[parsed + tok_len] != '\0') && (expr[parsed + tok_len] != '\''); ++tok_len) {} |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2766 | LY_CHECK_ERR_GOTO(expr[parsed + tok_len] == '\0', |
| 2767 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOE, expr[parsed], &expr[parsed]), error); |
| 2768 | ++tok_len; |
| 2769 | tok_type = LYXP_TOKEN_LITERAL; |
| 2770 | |
| 2771 | } else if (expr[parsed] == '\"') { |
| 2772 | |
| 2773 | /* Literal with " */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 2774 | for (tok_len = 1; (expr[parsed + tok_len] != '\0') && (expr[parsed + tok_len] != '\"'); ++tok_len) {} |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2775 | LY_CHECK_ERR_GOTO(expr[parsed + tok_len] == '\0', |
| 2776 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOE, expr[parsed], &expr[parsed]), error); |
| 2777 | ++tok_len; |
| 2778 | tok_type = LYXP_TOKEN_LITERAL; |
| 2779 | |
| 2780 | } else if ((expr[parsed] == '.') || (isdigit(expr[parsed]))) { |
| 2781 | |
| 2782 | /* Number */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 2783 | for (tok_len = 0; isdigit(expr[parsed + tok_len]); ++tok_len) {} |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2784 | if (expr[parsed + tok_len] == '.') { |
| 2785 | ++tok_len; |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 2786 | for ( ; isdigit(expr[parsed + tok_len]); ++tok_len) {} |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2787 | } |
| 2788 | tok_type = LYXP_TOKEN_NUMBER; |
| 2789 | |
| 2790 | } else if (expr[parsed] == '/') { |
| 2791 | |
| 2792 | /* Operator '/', '//' */ |
| 2793 | if (!strncmp(&expr[parsed], "//", 2)) { |
| 2794 | tok_len = 2; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2795 | tok_type = LYXP_TOKEN_OPER_RPATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2796 | } else { |
| 2797 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2798 | tok_type = LYXP_TOKEN_OPER_PATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2799 | } |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2800 | |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 2801 | } else if (!strncmp(&expr[parsed], "!=", 2)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2802 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2803 | /* Operator '!=' */ |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2804 | tok_len = 2; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2805 | tok_type = LYXP_TOKEN_OPER_NEQUAL; |
| 2806 | |
| 2807 | } else if (!strncmp(&expr[parsed], "<=", 2) || !strncmp(&expr[parsed], ">=", 2)) { |
| 2808 | |
| 2809 | /* Operator '<=', '>=' */ |
| 2810 | tok_len = 2; |
| 2811 | tok_type = LYXP_TOKEN_OPER_COMP; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2812 | |
| 2813 | } else if (expr[parsed] == '|') { |
| 2814 | |
| 2815 | /* Operator '|' */ |
| 2816 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2817 | tok_type = LYXP_TOKEN_OPER_UNI; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2818 | |
| 2819 | } else if ((expr[parsed] == '+') || (expr[parsed] == '-')) { |
| 2820 | |
| 2821 | /* Operator '+', '-' */ |
| 2822 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2823 | tok_type = LYXP_TOKEN_OPER_MATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2824 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2825 | } else if (expr[parsed] == '=') { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2826 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2827 | /* Operator '=' */ |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2828 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2829 | tok_type = LYXP_TOKEN_OPER_EQUAL; |
| 2830 | |
| 2831 | } else if ((expr[parsed] == '<') || (expr[parsed] == '>')) { |
| 2832 | |
| 2833 | /* Operator '<', '>' */ |
| 2834 | tok_len = 1; |
| 2835 | tok_type = LYXP_TOKEN_OPER_COMP; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2836 | |
| 2837 | } else if (ret->used && (ret->tokens[ret->used - 1] != LYXP_TOKEN_AT) |
| 2838 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_PAR1) |
| 2839 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_BRACK1) |
| 2840 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_COMMA) |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2841 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_LOG) |
| 2842 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_EQUAL) |
| 2843 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_NEQUAL) |
| 2844 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_COMP) |
| 2845 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_MATH) |
| 2846 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_UNI) |
| 2847 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_PATH) |
| 2848 | && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_RPATH)) { |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2849 | |
| 2850 | /* Operator '*', 'or', 'and', 'mod', or 'div' */ |
| 2851 | if (expr[parsed] == '*') { |
| 2852 | tok_len = 1; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2853 | tok_type = LYXP_TOKEN_OPER_MATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2854 | |
| 2855 | } else if (!strncmp(&expr[parsed], "or", 2)) { |
| 2856 | tok_len = 2; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2857 | tok_type = LYXP_TOKEN_OPER_LOG; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2858 | |
| 2859 | } else if (!strncmp(&expr[parsed], "and", 3)) { |
| 2860 | tok_len = 3; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2861 | tok_type = LYXP_TOKEN_OPER_LOG; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2862 | |
| 2863 | } else if (!strncmp(&expr[parsed], "mod", 3) || !strncmp(&expr[parsed], "div", 3)) { |
| 2864 | tok_len = 3; |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 2865 | tok_type = LYXP_TOKEN_OPER_MATH; |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2866 | |
| 2867 | } else if (prev_function_check) { |
Michal Vasko | 5307857 | 2019-05-24 08:50:15 +0200 | [diff] [blame] | 2868 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Invalid character 0x%x ('%c'), perhaps \"%.*s\" is supposed to be a function call.", |
| 2869 | 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] | 2870 | goto error; |
| 2871 | } else { |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2872 | 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] | 2873 | goto error; |
| 2874 | } |
| 2875 | } else if (expr[parsed] == '*') { |
| 2876 | |
| 2877 | /* NameTest '*' */ |
| 2878 | tok_len = 1; |
| 2879 | tok_type = LYXP_TOKEN_NAMETEST; |
| 2880 | |
| 2881 | } else { |
| 2882 | |
| 2883 | /* NameTest (NCName ':' '*' | QName) or NodeType/FunctionName */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 2884 | long int ncname_len = parse_ncname(&expr[parsed]); |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2885 | 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] | 2886 | tok_len = ncname_len; |
| 2887 | |
| 2888 | if (expr[parsed + tok_len] == ':') { |
| 2889 | ++tok_len; |
| 2890 | if (expr[parsed + tok_len] == '*') { |
| 2891 | ++tok_len; |
| 2892 | } else { |
| 2893 | ncname_len = parse_ncname(&expr[parsed + tok_len]); |
Radek Krejci | d427026 | 2019-01-07 15:07:25 +0100 | [diff] [blame] | 2894 | 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] | 2895 | tok_len += ncname_len; |
| 2896 | } |
| 2897 | /* remove old flag to prevent ambiguities */ |
| 2898 | prev_function_check = 0; |
| 2899 | tok_type = LYXP_TOKEN_NAMETEST; |
| 2900 | } else { |
| 2901 | /* there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */ |
| 2902 | prev_function_check = 1; |
| 2903 | tok_type = LYXP_TOKEN_NAMETEST; |
| 2904 | } |
| 2905 | } |
| 2906 | |
| 2907 | /* store the token, move on to the next one */ |
| 2908 | LY_CHECK_GOTO(exp_add_token(ctx, ret, tok_type, parsed, tok_len), error); |
| 2909 | parsed += tok_len; |
| 2910 | while (is_xmlws(expr[parsed])) { |
| 2911 | ++parsed; |
| 2912 | } |
| 2913 | |
| 2914 | } while (expr[parsed]); |
| 2915 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2916 | if (reparse) { |
| 2917 | /* prealloc repeat */ |
| 2918 | ret->repeat = calloc(ret->size, sizeof *ret->repeat); |
| 2919 | LY_CHECK_ERR_GOTO(!ret->repeat, LOGMEM(ctx), error); |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2920 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2921 | /* fill repeat */ |
| 2922 | LY_CHECK_GOTO(reparse_or_expr(ctx, ret, &tok_idx), error); |
| 2923 | if (ret->used > tok_idx) { |
| 2924 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Unparsed characters \"%s\" left at the end of an XPath expression.", |
| 2925 | &ret->expr[ret->tok_pos[tok_idx]]); |
| 2926 | goto error; |
| 2927 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2928 | } |
| 2929 | |
| 2930 | print_expr_struct_debug(ret); |
| 2931 | |
Radek Krejci | b1646a9 | 2018-11-02 16:08:26 +0100 | [diff] [blame] | 2932 | return ret; |
| 2933 | |
| 2934 | error: |
| 2935 | lyxp_expr_free(ctx, ret); |
| 2936 | return NULL; |
| 2937 | } |
| 2938 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2939 | struct lyxp_expr * |
| 2940 | lyxp_expr_dup(const struct ly_ctx *ctx, const struct lyxp_expr *exp) |
| 2941 | { |
| 2942 | struct lyxp_expr *dup; |
| 2943 | uint32_t i, j; |
| 2944 | |
| 2945 | dup = calloc(1, sizeof *dup); |
| 2946 | LY_CHECK_ERR_GOTO(!dup, LOGMEM(ctx), error); |
| 2947 | |
| 2948 | dup->tokens = malloc(exp->used * sizeof *dup->tokens); |
| 2949 | LY_CHECK_ERR_GOTO(!dup->tokens, LOGMEM(ctx), error); |
| 2950 | memcpy(dup->tokens, exp->tokens, exp->used * sizeof *dup->tokens); |
| 2951 | |
| 2952 | dup->tok_pos = malloc(exp->used * sizeof *dup->tok_pos); |
| 2953 | LY_CHECK_ERR_GOTO(!dup->tok_pos, LOGMEM(ctx), error); |
| 2954 | memcpy(dup->tok_pos, exp->tok_pos, exp->used * sizeof *dup->tok_pos); |
| 2955 | |
| 2956 | dup->tok_len = malloc(exp->used * sizeof *dup->tok_len); |
| 2957 | LY_CHECK_ERR_GOTO(!dup->tok_len, LOGMEM(ctx), error); |
| 2958 | memcpy(dup->tok_len, exp->tok_len, exp->used * sizeof *dup->tok_len); |
| 2959 | |
| 2960 | dup->repeat = malloc(exp->used * sizeof *dup->repeat); |
| 2961 | LY_CHECK_ERR_GOTO(!dup->repeat, LOGMEM(ctx), error); |
| 2962 | for (i = 0; i < exp->used; ++i) { |
| 2963 | if (!exp->repeat[i]) { |
| 2964 | dup->repeat[i] = NULL; |
| 2965 | } else { |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 2966 | for (j = 0; exp->repeat[i][j]; ++j) {} |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2967 | /* the ending 0 as well */ |
| 2968 | ++j; |
| 2969 | |
Michal Vasko | 99c7164 | 2020-07-03 13:33:36 +0200 | [diff] [blame] | 2970 | dup->repeat[i] = malloc(j * sizeof **dup->repeat); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 2971 | LY_CHECK_ERR_GOTO(!dup->repeat[i], LOGMEM(ctx), error); |
| 2972 | memcpy(dup->repeat[i], exp->repeat[i], j * sizeof **dup->repeat); |
| 2973 | dup->repeat[i][j - 1] = 0; |
| 2974 | } |
| 2975 | } |
| 2976 | |
| 2977 | dup->used = exp->used; |
| 2978 | dup->size = exp->used; |
| 2979 | dup->expr = lydict_insert(ctx, exp->expr, 0); |
| 2980 | |
| 2981 | return dup; |
| 2982 | |
| 2983 | error: |
| 2984 | lyxp_expr_free(ctx, dup); |
| 2985 | return NULL; |
| 2986 | } |
| 2987 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 2988 | /* |
| 2989 | * warn functions |
| 2990 | * |
| 2991 | * Warn functions check specific reasonable conditions for schema XPath |
| 2992 | * and print a warning if they are not satisfied. |
| 2993 | */ |
| 2994 | |
| 2995 | /** |
| 2996 | * @brief Get the last-added schema node that is currently in the context. |
| 2997 | * |
| 2998 | * @param[in] set Set to search in. |
| 2999 | * @return Last-added schema context node, NULL if no node is in context. |
| 3000 | */ |
| 3001 | static struct lysc_node * |
| 3002 | warn_get_scnode_in_ctx(struct lyxp_set *set) |
| 3003 | { |
| 3004 | uint32_t i; |
| 3005 | |
| 3006 | if (!set || (set->type != LYXP_SET_SCNODE_SET)) { |
| 3007 | return NULL; |
| 3008 | } |
| 3009 | |
| 3010 | i = set->used; |
| 3011 | do { |
| 3012 | --i; |
| 3013 | if (set->val.scnodes[i].in_ctx == 1) { |
| 3014 | /* if there are more, simply return the first found (last added) */ |
| 3015 | return set->val.scnodes[i].scnode; |
| 3016 | } |
| 3017 | } while (i); |
| 3018 | |
| 3019 | return NULL; |
| 3020 | } |
| 3021 | |
| 3022 | /** |
| 3023 | * @brief Test whether a type is numeric - integer type or decimal64. |
| 3024 | * |
| 3025 | * @param[in] type Type to test. |
| 3026 | * @return 1 if numeric, 0 otherwise. |
| 3027 | */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3028 | static uint8_t |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3029 | warn_is_numeric_type(struct lysc_type *type) |
| 3030 | { |
| 3031 | struct lysc_type_union *uni; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3032 | uint8_t ret; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3033 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3034 | |
| 3035 | switch (type->basetype) { |
| 3036 | case LY_TYPE_DEC64: |
| 3037 | case LY_TYPE_INT8: |
| 3038 | case LY_TYPE_UINT8: |
| 3039 | case LY_TYPE_INT16: |
| 3040 | case LY_TYPE_UINT16: |
| 3041 | case LY_TYPE_INT32: |
| 3042 | case LY_TYPE_UINT32: |
| 3043 | case LY_TYPE_INT64: |
| 3044 | case LY_TYPE_UINT64: |
| 3045 | return 1; |
| 3046 | case LY_TYPE_UNION: |
| 3047 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3048 | LY_ARRAY_FOR(uni->types, u) { |
| 3049 | ret = warn_is_numeric_type(uni->types[u]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3050 | if (ret) { |
| 3051 | /* found a suitable type */ |
| 3052 | return 1; |
| 3053 | } |
| 3054 | } |
| 3055 | /* did not find any suitable type */ |
| 3056 | return 0; |
| 3057 | case LY_TYPE_LEAFREF: |
| 3058 | return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype); |
| 3059 | default: |
| 3060 | return 0; |
| 3061 | } |
| 3062 | } |
| 3063 | |
| 3064 | /** |
| 3065 | * @brief Test whether a type is string-like - no integers, decimal64 or binary. |
| 3066 | * |
| 3067 | * @param[in] type Type to test. |
| 3068 | * @return 1 if string, 0 otherwise. |
| 3069 | */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3070 | static uint8_t |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3071 | warn_is_string_type(struct lysc_type *type) |
| 3072 | { |
| 3073 | struct lysc_type_union *uni; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3074 | uint8_t ret; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3075 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3076 | |
| 3077 | switch (type->basetype) { |
| 3078 | case LY_TYPE_BITS: |
| 3079 | case LY_TYPE_ENUM: |
| 3080 | case LY_TYPE_IDENT: |
| 3081 | case LY_TYPE_INST: |
| 3082 | case LY_TYPE_STRING: |
| 3083 | return 1; |
| 3084 | case LY_TYPE_UNION: |
| 3085 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3086 | LY_ARRAY_FOR(uni->types, u) { |
| 3087 | ret = warn_is_string_type(uni->types[u]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3088 | if (ret) { |
| 3089 | /* found a suitable type */ |
| 3090 | return 1; |
| 3091 | } |
| 3092 | } |
| 3093 | /* did not find any suitable type */ |
| 3094 | return 0; |
| 3095 | case LY_TYPE_LEAFREF: |
| 3096 | return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype); |
| 3097 | default: |
| 3098 | return 0; |
| 3099 | } |
| 3100 | } |
| 3101 | |
| 3102 | /** |
| 3103 | * @brief Test whether a type is one specific type. |
| 3104 | * |
| 3105 | * @param[in] type Type to test. |
| 3106 | * @param[in] base Expected type. |
| 3107 | * @return 1 if it is, 0 otherwise. |
| 3108 | */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3109 | static uint8_t |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3110 | warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base) |
| 3111 | { |
| 3112 | struct lysc_type_union *uni; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3113 | uint8_t ret; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3114 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3115 | |
| 3116 | if (type->basetype == base) { |
| 3117 | return 1; |
| 3118 | } else if (type->basetype == LY_TYPE_UNION) { |
| 3119 | uni = (struct lysc_type_union *)type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3120 | LY_ARRAY_FOR(uni->types, u) { |
| 3121 | ret = warn_is_specific_type(uni->types[u], base); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3122 | if (ret) { |
| 3123 | /* found a suitable type */ |
| 3124 | return 1; |
| 3125 | } |
| 3126 | } |
| 3127 | /* did not find any suitable type */ |
| 3128 | return 0; |
| 3129 | } else if (type->basetype == LY_TYPE_LEAFREF) { |
| 3130 | return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base); |
| 3131 | } |
| 3132 | |
| 3133 | return 0; |
| 3134 | } |
| 3135 | |
| 3136 | /** |
| 3137 | * @brief Get next type of a (union) type. |
| 3138 | * |
| 3139 | * @param[in] type Base type. |
| 3140 | * @param[in] prev_type Previously returned type. |
| 3141 | * @return Next type or NULL. |
| 3142 | */ |
| 3143 | static struct lysc_type * |
| 3144 | warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type) |
| 3145 | { |
| 3146 | struct lysc_type_union *uni; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3147 | uint8_t found = 0; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3148 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3149 | |
| 3150 | switch (type->basetype) { |
| 3151 | case LY_TYPE_UNION: |
| 3152 | uni = (struct lysc_type_union *)type; |
| 3153 | if (!prev_type) { |
| 3154 | return uni->types[0]; |
| 3155 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3156 | LY_ARRAY_FOR(uni->types, u) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3157 | if (found) { |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3158 | return uni->types[u]; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3159 | } |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3160 | if (prev_type == uni->types[u]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3161 | found = 1; |
| 3162 | } |
| 3163 | } |
| 3164 | return NULL; |
| 3165 | default: |
| 3166 | if (prev_type) { |
| 3167 | assert(type == prev_type); |
| 3168 | return NULL; |
| 3169 | } else { |
| 3170 | return type; |
| 3171 | } |
| 3172 | } |
| 3173 | } |
| 3174 | |
| 3175 | /** |
| 3176 | * @brief Test whether 2 types have a common type. |
| 3177 | * |
| 3178 | * @param[in] type1 First type. |
| 3179 | * @param[in] type2 Second type. |
| 3180 | * @return 1 if they do, 0 otherwise. |
| 3181 | */ |
| 3182 | static int |
| 3183 | warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2) |
| 3184 | { |
| 3185 | struct lysc_type *t1, *rt1, *t2, *rt2; |
| 3186 | |
| 3187 | t1 = NULL; |
| 3188 | while ((t1 = warn_is_equal_type_next_type(type1, t1))) { |
| 3189 | if (t1->basetype == LY_TYPE_LEAFREF) { |
| 3190 | rt1 = ((struct lysc_type_leafref *)t1)->realtype; |
| 3191 | } else { |
| 3192 | rt1 = t1; |
| 3193 | } |
| 3194 | |
| 3195 | t2 = NULL; |
| 3196 | while ((t2 = warn_is_equal_type_next_type(type2, t2))) { |
| 3197 | if (t2->basetype == LY_TYPE_LEAFREF) { |
| 3198 | rt2 = ((struct lysc_type_leafref *)t2)->realtype; |
| 3199 | } else { |
| 3200 | rt2 = t2; |
| 3201 | } |
| 3202 | |
| 3203 | if (rt2->basetype == rt1->basetype) { |
| 3204 | /* match found */ |
| 3205 | return 1; |
| 3206 | } |
| 3207 | } |
| 3208 | } |
| 3209 | |
| 3210 | return 0; |
| 3211 | } |
| 3212 | |
| 3213 | /** |
| 3214 | * @brief Check both operands of comparison operators. |
| 3215 | * |
| 3216 | * @param[in] ctx Context for errors. |
| 3217 | * @param[in] set1 First operand set. |
| 3218 | * @param[in] set2 Second operand set. |
| 3219 | * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!='). |
| 3220 | * @param[in] expr Start of the expression to print with the warning. |
| 3221 | * @param[in] tok_pos Token position. |
| 3222 | */ |
| 3223 | static void |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3224 | warn_operands(struct ly_ctx *ctx, struct lyxp_set *set1, struct lyxp_set *set2, uint8_t numbers_only, const char *expr, uint16_t tok_pos) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3225 | { |
| 3226 | struct lysc_node_leaf *node1, *node2; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3227 | uint8_t leaves = 1, warning = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3228 | |
| 3229 | node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1); |
| 3230 | node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2); |
| 3231 | |
| 3232 | if (!node1 && !node2) { |
| 3233 | /* no node-sets involved, nothing to do */ |
| 3234 | return; |
| 3235 | } |
| 3236 | |
| 3237 | if (node1) { |
| 3238 | if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3239 | LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name); |
| 3240 | warning = 1; |
| 3241 | leaves = 0; |
| 3242 | } else if (numbers_only && !warn_is_numeric_type(node1->type)) { |
| 3243 | LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name); |
| 3244 | warning = 1; |
| 3245 | } |
| 3246 | } |
| 3247 | |
| 3248 | if (node2) { |
| 3249 | if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3250 | LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name); |
| 3251 | warning = 1; |
| 3252 | leaves = 0; |
| 3253 | } else if (numbers_only && !warn_is_numeric_type(node2->type)) { |
| 3254 | LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name); |
| 3255 | warning = 1; |
| 3256 | } |
| 3257 | } |
| 3258 | |
| 3259 | if (node1 && node2 && leaves && !numbers_only) { |
| 3260 | if ((warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type)) |
| 3261 | || (!warn_is_numeric_type(node1->type) && warn_is_numeric_type(node2->type)) |
| 3262 | || (!warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type) |
| 3263 | && !warn_is_equal_type(node1->type, node2->type))) { |
| 3264 | LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name); |
| 3265 | warning = 1; |
| 3266 | } |
| 3267 | } |
| 3268 | |
| 3269 | if (warning) { |
| 3270 | LOGWRN(ctx, "Previous warning generated by XPath subexpression[%u] \"%.20s\".", tok_pos, expr + tok_pos); |
| 3271 | } |
| 3272 | } |
| 3273 | |
| 3274 | /** |
| 3275 | * @brief Check that a value is valid for a leaf. If not applicable, does nothing. |
| 3276 | * |
| 3277 | * @param[in] exp Parsed XPath expression. |
| 3278 | * @param[in] set Set with the leaf/leaf-list. |
| 3279 | * @param[in] val_exp Index of the value (literal/number) in @p exp. |
| 3280 | * @param[in] equal_exp Index of the start of the equality expression in @p exp. |
| 3281 | * @param[in] last_equal_exp Index of the end of the equality expression in @p exp. |
| 3282 | */ |
| 3283 | static void |
| 3284 | warn_equality_value(struct lyxp_expr *exp, struct lyxp_set *set, uint16_t val_exp, uint16_t equal_exp, uint16_t last_equal_exp) |
| 3285 | { |
| 3286 | struct lysc_node *scnode; |
| 3287 | struct lysc_type *type; |
| 3288 | char *value; |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3289 | struct lyd_value storage; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3290 | LY_ERR rc; |
| 3291 | struct ly_err_item *err = NULL; |
| 3292 | |
| 3293 | if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST)) |
| 3294 | && ((exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) || (exp->tokens[val_exp] == LYXP_TOKEN_NUMBER))) { |
| 3295 | /* check that the node can have the specified value */ |
| 3296 | if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) { |
| 3297 | value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2); |
| 3298 | } else { |
| 3299 | value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]); |
| 3300 | } |
| 3301 | if (!value) { |
| 3302 | LOGMEM(set->ctx); |
| 3303 | return; |
| 3304 | } |
| 3305 | |
| 3306 | if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) { |
| 3307 | LOGWRN(set->ctx, "Identityref \"%s\" comparison with identity \"%s\" without prefix, consider adding" |
| 3308 | " a prefix or best using \"derived-from(-or-self)()\" functions.", scnode->name, value); |
| 3309 | LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp], |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 3310 | (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp], |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3311 | exp->expr + exp->tok_pos[equal_exp]); |
| 3312 | } |
| 3313 | |
| 3314 | type = ((struct lysc_node_leaf *)scnode)->type; |
| 3315 | if (type->basetype != LY_TYPE_IDENT) { |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 3316 | rc = type->plugin->store(set->ctx, type, value, strlen(value), LY_TYPE_OPTS_SCHEMA, LY_PREF_SCHEMA, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 3317 | (void *)set->local_mod, NULL, NULL, &storage, &err); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3318 | |
| 3319 | if (err) { |
| 3320 | LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg); |
| 3321 | ly_err_free(err); |
| 3322 | } else if (rc != LY_SUCCESS) { |
| 3323 | LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value); |
| 3324 | } |
| 3325 | if (rc != LY_SUCCESS) { |
| 3326 | LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp], |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 3327 | (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp], |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3328 | exp->expr + exp->tok_pos[equal_exp]); |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3329 | } else { |
| 3330 | type->plugin->free(set->ctx, &storage); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3331 | } |
| 3332 | } |
| 3333 | free(value); |
| 3334 | } |
| 3335 | } |
| 3336 | |
| 3337 | /* |
| 3338 | * XPath functions |
| 3339 | */ |
| 3340 | |
| 3341 | /** |
| 3342 | * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN |
| 3343 | * depending on whether the first node bit value from the second argument is set. |
| 3344 | * |
| 3345 | * @param[in] args Array of arguments. |
| 3346 | * @param[in] arg_count Count of elements in @p args. |
| 3347 | * @param[in,out] set Context and result set at the same time. |
| 3348 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3349 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3350 | */ |
| 3351 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3352 | xpath_bit_is_set(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3353 | { |
| 3354 | struct lyd_node_term *leaf; |
| 3355 | struct lysc_node_leaf *sleaf; |
| 3356 | struct lysc_type_bits *bits; |
| 3357 | LY_ERR rc = LY_SUCCESS; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3358 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3359 | |
| 3360 | if (options & LYXP_SCNODE_ALL) { |
| 3361 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3362 | 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] | 3363 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3364 | 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] | 3365 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) { |
| 3366 | 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] | 3367 | } |
| 3368 | |
| 3369 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 3370 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3371 | 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] | 3372 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3373 | 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] | 3374 | } |
| 3375 | } |
| 3376 | set_scnode_clear_ctx(set); |
| 3377 | return rc; |
| 3378 | } |
| 3379 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3380 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3381 | 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)"); |
| 3382 | return LY_EVALID; |
| 3383 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3384 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3385 | LY_CHECK_RET(rc); |
| 3386 | |
| 3387 | set_fill_boolean(set, 0); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3388 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3389 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
| 3390 | if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) |
| 3391 | && (((struct lysc_node_leaf *)leaf->schema)->type->basetype == LY_TYPE_BITS)) { |
| 3392 | bits = (struct lysc_type_bits *)((struct lysc_node_leaf *)leaf->schema)->type; |
Radek Krejci | 7eb54ba | 2020-05-18 16:30:04 +0200 | [diff] [blame] | 3393 | LY_ARRAY_FOR(bits->bits, u) { |
| 3394 | if (!strcmp(bits->bits[u].name, args[1]->val.str)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3395 | set_fill_boolean(set, 1); |
| 3396 | break; |
| 3397 | } |
| 3398 | } |
| 3399 | } |
| 3400 | } |
| 3401 | |
| 3402 | return LY_SUCCESS; |
| 3403 | } |
| 3404 | |
| 3405 | /** |
| 3406 | * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN |
| 3407 | * with the argument converted to boolean. |
| 3408 | * |
| 3409 | * @param[in] args Array of arguments. |
| 3410 | * @param[in] arg_count Count of elements in @p args. |
| 3411 | * @param[in,out] set Context and result set at the same time. |
| 3412 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3413 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3414 | */ |
| 3415 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3416 | xpath_boolean(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3417 | { |
| 3418 | LY_ERR rc; |
| 3419 | |
| 3420 | if (options & LYXP_SCNODE_ALL) { |
| 3421 | set_scnode_clear_ctx(set); |
| 3422 | return LY_SUCCESS; |
| 3423 | } |
| 3424 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3425 | rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3426 | LY_CHECK_RET(rc); |
| 3427 | set_fill_set(set, args[0]); |
| 3428 | |
| 3429 | return LY_SUCCESS; |
| 3430 | } |
| 3431 | |
| 3432 | /** |
| 3433 | * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER |
| 3434 | * with the first argument rounded up to the nearest integer. |
| 3435 | * |
| 3436 | * @param[in] args Array of arguments. |
| 3437 | * @param[in] arg_count Count of elements in @p args. |
| 3438 | * @param[in,out] set Context and result set at the same time. |
| 3439 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3440 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3441 | */ |
| 3442 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3443 | xpath_ceiling(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3444 | { |
| 3445 | struct lysc_node_leaf *sleaf; |
| 3446 | LY_ERR rc = LY_SUCCESS; |
| 3447 | |
| 3448 | if (options & LYXP_SCNODE_ALL) { |
| 3449 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3450 | 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] | 3451 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3452 | 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] | 3453 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) { |
| 3454 | 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] | 3455 | } |
| 3456 | set_scnode_clear_ctx(set); |
| 3457 | return rc; |
| 3458 | } |
| 3459 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3460 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3461 | LY_CHECK_RET(rc); |
| 3462 | if ((long long)args[0]->val.num != args[0]->val.num) { |
| 3463 | set_fill_number(set, ((long long)args[0]->val.num) + 1); |
| 3464 | } else { |
| 3465 | set_fill_number(set, args[0]->val.num); |
| 3466 | } |
| 3467 | |
| 3468 | return LY_SUCCESS; |
| 3469 | } |
| 3470 | |
| 3471 | /** |
| 3472 | * @brief Execute the XPath concat(string, string, string*) function. |
| 3473 | * Returns LYXP_SET_STRING with the concatenation of all the arguments. |
| 3474 | * |
| 3475 | * @param[in] args Array of arguments. |
| 3476 | * @param[in] arg_count Count of elements in @p args. |
| 3477 | * @param[in,out] set Context and result set at the same time. |
| 3478 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3479 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3480 | */ |
| 3481 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3482 | xpath_concat(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3483 | { |
| 3484 | uint16_t i; |
| 3485 | char *str = NULL; |
| 3486 | size_t used = 1; |
| 3487 | LY_ERR rc = LY_SUCCESS; |
| 3488 | struct lysc_node_leaf *sleaf; |
| 3489 | |
| 3490 | if (options & LYXP_SCNODE_ALL) { |
| 3491 | for (i = 0; i < arg_count; ++i) { |
| 3492 | if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) { |
| 3493 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3494 | LOGWRN(set->ctx, "Argument #%u of %s is a %s node \"%s\".", |
| 3495 | i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3496 | } else if (!warn_is_string_type(sleaf->type)) { |
Radek Krejci | 70124c8 | 2020-08-14 22:17:03 +0200 | [diff] [blame] | 3497 | LOGWRN(set->ctx, "Argument #%u of %s is node \"%s\", not of string-type.", i + 1, __func__, sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3498 | } |
| 3499 | } |
| 3500 | } |
| 3501 | set_scnode_clear_ctx(set); |
| 3502 | return rc; |
| 3503 | } |
| 3504 | |
| 3505 | for (i = 0; i < arg_count; ++i) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3506 | rc = lyxp_set_cast(args[i], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3507 | if (rc != LY_SUCCESS) { |
| 3508 | free(str); |
| 3509 | return rc; |
| 3510 | } |
| 3511 | |
| 3512 | str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char)); |
| 3513 | LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM); |
| 3514 | strcpy(str + used - 1, args[i]->val.str); |
| 3515 | used += strlen(args[i]->val.str); |
| 3516 | } |
| 3517 | |
| 3518 | /* free, kind of */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3519 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3520 | set->type = LYXP_SET_STRING; |
| 3521 | set->val.str = str; |
| 3522 | |
| 3523 | return LY_SUCCESS; |
| 3524 | } |
| 3525 | |
| 3526 | /** |
| 3527 | * @brief Execute the XPath contains(string, string) function. |
| 3528 | * Returns LYXP_SET_BOOLEAN whether the second argument can |
| 3529 | * be found in the first or not. |
| 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 |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3538 | xpath_contains(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3539 | { |
| 3540 | struct lysc_node_leaf *sleaf; |
| 3541 | LY_ERR rc = LY_SUCCESS; |
| 3542 | |
| 3543 | if (options & LYXP_SCNODE_ALL) { |
| 3544 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3545 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3546 | 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] | 3547 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3548 | 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] | 3549 | } |
| 3550 | } |
| 3551 | |
| 3552 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 3553 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3554 | 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] | 3555 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3556 | 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] | 3557 | } |
| 3558 | } |
| 3559 | set_scnode_clear_ctx(set); |
| 3560 | return rc; |
| 3561 | } |
| 3562 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3563 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3564 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3565 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3566 | LY_CHECK_RET(rc); |
| 3567 | |
| 3568 | if (strstr(args[0]->val.str, args[1]->val.str)) { |
| 3569 | set_fill_boolean(set, 1); |
| 3570 | } else { |
| 3571 | set_fill_boolean(set, 0); |
| 3572 | } |
| 3573 | |
| 3574 | return LY_SUCCESS; |
| 3575 | } |
| 3576 | |
| 3577 | /** |
| 3578 | * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER |
| 3579 | * with the size of the node-set from the argument. |
| 3580 | * |
| 3581 | * @param[in] args Array of arguments. |
| 3582 | * @param[in] arg_count Count of elements in @p args. |
| 3583 | * @param[in,out] set Context and result set at the same time. |
| 3584 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3585 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3586 | */ |
| 3587 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3588 | xpath_count(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3589 | { |
| 3590 | struct lysc_node *scnode = NULL; |
| 3591 | LY_ERR rc = LY_SUCCESS; |
| 3592 | |
| 3593 | if (options & LYXP_SCNODE_ALL) { |
| 3594 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(scnode = warn_get_scnode_in_ctx(args[0]))) { |
| 3595 | 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] | 3596 | } |
| 3597 | set_scnode_clear_ctx(set); |
| 3598 | return rc; |
| 3599 | } |
| 3600 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3601 | if (args[0]->type != LYXP_SET_NODE_SET) { |
| 3602 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "count(node-set)"); |
| 3603 | return LY_EVALID; |
| 3604 | } |
| 3605 | |
| 3606 | set_fill_number(set, args[0]->used); |
| 3607 | return LY_SUCCESS; |
| 3608 | } |
| 3609 | |
| 3610 | /** |
| 3611 | * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET |
| 3612 | * with the context with the intial node. |
| 3613 | * |
| 3614 | * @param[in] args Array of arguments. |
| 3615 | * @param[in] arg_count Count of elements in @p args. |
| 3616 | * @param[in,out] set Context and result set at the same time. |
| 3617 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3618 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3619 | */ |
| 3620 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3621 | xpath_current(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3622 | { |
| 3623 | if (arg_count || args) { |
| 3624 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGCOUNT, arg_count, "current()"); |
| 3625 | return LY_EVALID; |
| 3626 | } |
| 3627 | |
| 3628 | if (options & LYXP_SCNODE_ALL) { |
| 3629 | set_scnode_clear_ctx(set); |
| 3630 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame^] | 3631 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, set->ctx_scnode, LYXP_NODE_ELEM, NULL)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3632 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3633 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3634 | |
| 3635 | /* position is filled later */ |
| 3636 | set_insert_node(set, set->ctx_node, 0, LYXP_NODE_ELEM, 0); |
| 3637 | } |
| 3638 | |
| 3639 | return LY_SUCCESS; |
| 3640 | } |
| 3641 | |
| 3642 | /** |
| 3643 | * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either |
| 3644 | * leafref or instance-identifier target node(s). |
| 3645 | * |
| 3646 | * @param[in] args Array of arguments. |
| 3647 | * @param[in] arg_count Count of elements in @p args. |
| 3648 | * @param[in,out] set Context and result set at the same time. |
| 3649 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3650 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3651 | */ |
| 3652 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3653 | xpath_deref(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3654 | { |
| 3655 | struct lyd_node_term *leaf; |
Michal Vasko | 42e497c | 2020-01-06 08:38:25 +0100 | [diff] [blame] | 3656 | struct lysc_node_leaf *sleaf = NULL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3657 | struct lysc_type_leafref *lref; |
Michal Vasko | ae9e4cb | 2019-09-25 08:43:05 +0200 | [diff] [blame] | 3658 | const struct lysc_node *target; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3659 | struct ly_path *p; |
| 3660 | struct lyd_node *node; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3661 | char *errmsg = NULL; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 3662 | uint8_t oper; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3663 | LY_ERR rc = LY_SUCCESS; |
| 3664 | |
| 3665 | if (options & LYXP_SCNODE_ALL) { |
| 3666 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3667 | 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] | 3668 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3669 | 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] | 3670 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) && !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) { |
| 3671 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"leafref\" nor \"instance-identifier\".", |
| 3672 | __func__, sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3673 | } |
| 3674 | set_scnode_clear_ctx(set); |
Michal Vasko | 42e497c | 2020-01-06 08:38:25 +0100 | [diff] [blame] | 3675 | if (sleaf && (sleaf->type->basetype == LY_TYPE_LEAFREF)) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3676 | lref = (struct lysc_type_leafref *)sleaf->type; |
Michal Vasko | 00cbf53 | 2020-06-15 13:58:47 +0200 | [diff] [blame] | 3677 | 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] | 3678 | |
| 3679 | /* it was already evaluated on schema, it must succeed */ |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 3680 | rc = ly_path_compile(set->ctx, sleaf->module, (struct lysc_node *)sleaf, lref->path, LY_PATH_LREF_TRUE, |
| 3681 | oper, LY_PATH_TARGET_MANY, set->format, lref->path_context, &p); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3682 | assert(!rc); |
| 3683 | |
| 3684 | /* get the target node */ |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 3685 | target = p[LY_ARRAY_COUNT(p) - 1].node; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3686 | ly_path_free(set->ctx, p); |
| 3687 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame^] | 3688 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, target, LYXP_NODE_ELEM, NULL)); |
Michal Vasko | ae9e4cb | 2019-09-25 08:43:05 +0200 | [diff] [blame] | 3689 | } |
| 3690 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3691 | return rc; |
| 3692 | } |
| 3693 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3694 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3695 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "deref(node-set)"); |
| 3696 | return LY_EVALID; |
| 3697 | } |
| 3698 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3699 | lyxp_set_free_content(set); |
| 3700 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3701 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
| 3702 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 3703 | if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 3704 | if (sleaf->type->basetype == LY_TYPE_LEAFREF) { |
| 3705 | /* find leafref target */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3706 | if (ly_type_find_leafref((struct lysc_type_leafref *)sleaf->type, (struct lyd_node *)leaf, |
| 3707 | &leaf->value, set->tree, &node, &errmsg)) { |
| 3708 | LOGERR(set->ctx, LY_EVALID, errmsg); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3709 | free(errmsg); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3710 | return LY_EVALID; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3711 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3712 | } else { |
| 3713 | assert(sleaf->type->basetype == LY_TYPE_INST); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3714 | if (ly_path_eval(leaf->value.target, set->tree, &node)) { |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3715 | LOGERR(set->ctx, LY_EVALID, "Invalid instance-identifier \"%s\" value - required instance not found.", |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 3716 | LYD_CANON_VALUE(leaf)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3717 | return LY_EVALID; |
| 3718 | } |
| 3719 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 3720 | |
| 3721 | /* insert it */ |
| 3722 | set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3723 | } |
| 3724 | } |
| 3725 | |
| 3726 | return LY_SUCCESS; |
| 3727 | } |
| 3728 | |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3729 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3730 | xpath_derived_(struct lyxp_set **args, struct lyxp_set *set, uint32_t options, uint8_t self_match, const char *func) |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3731 | { |
| 3732 | uint16_t i; |
| 3733 | LY_ARRAY_COUNT_TYPE u; |
| 3734 | struct lyd_node_term *leaf; |
| 3735 | struct lysc_node_leaf *sleaf; |
| 3736 | struct lyd_meta *meta; |
| 3737 | struct lyd_value data = {0}, *val; |
| 3738 | struct ly_err_item *err = NULL; |
| 3739 | LY_ERR rc = LY_SUCCESS; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3740 | uint8_t found; |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3741 | |
| 3742 | if (options & LYXP_SCNODE_ALL) { |
| 3743 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3744 | LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", func); |
| 3745 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3746 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype), |
| 3747 | sleaf->name); |
| 3748 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) { |
| 3749 | LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", func, sleaf->name); |
| 3750 | } |
| 3751 | |
| 3752 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 3753 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3754 | LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype), |
| 3755 | sleaf->name); |
| 3756 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3757 | LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", func, sleaf->name); |
| 3758 | } |
| 3759 | } |
| 3760 | set_scnode_clear_ctx(set); |
| 3761 | return rc; |
| 3762 | } |
| 3763 | |
| 3764 | if (args[0]->type != LYXP_SET_NODE_SET) { |
| 3765 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), |
| 3766 | "derived-from(-or-self)(node-set, string)"); |
| 3767 | return LY_EVALID; |
| 3768 | } |
| 3769 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
| 3770 | LY_CHECK_RET(rc); |
| 3771 | |
| 3772 | set_fill_boolean(set, 0); |
| 3773 | found = 0; |
| 3774 | for (i = 0; i < args[0]->used; ++i) { |
| 3775 | if ((args[0]->val.nodes[i].type != LYXP_NODE_ELEM) && (args[0]->val.nodes[i].type != LYXP_NODE_META)) { |
| 3776 | continue; |
| 3777 | } |
| 3778 | |
| 3779 | if (args[0]->val.nodes[i].type == LYXP_NODE_ELEM) { |
| 3780 | leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node; |
| 3781 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 3782 | val = &leaf->value; |
| 3783 | if (!(sleaf->nodetype & LYD_NODE_TERM) || (leaf->value.realtype->basetype != LY_TYPE_IDENT)) { |
| 3784 | /* uninteresting */ |
| 3785 | continue; |
| 3786 | } |
| 3787 | |
| 3788 | /* store args[1] as ident */ |
| 3789 | rc = val->realtype->plugin->store(set->ctx, val->realtype, args[1]->val.str, strlen(args[1]->val.str), |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3790 | 0, set->format, (void *)set->local_mod, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 3791 | (struct lyd_node *)leaf, set->tree, &data, &err); |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3792 | } else { |
| 3793 | meta = args[0]->val.meta[i].meta; |
| 3794 | val = &meta->value; |
| 3795 | if (val->realtype->basetype != LY_TYPE_IDENT) { |
| 3796 | /* uninteresting */ |
| 3797 | continue; |
| 3798 | } |
| 3799 | |
| 3800 | /* store args[1] as ident */ |
| 3801 | rc = val->realtype->plugin->store(set->ctx, val->realtype, args[1]->val.str, strlen(args[1]->val.str), |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 3802 | 0, set->format, (void *)meta->annotation->module, |
| 3803 | meta->parent, set->tree, &data, &err); |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3804 | } |
| 3805 | |
| 3806 | if (err) { |
| 3807 | ly_err_print(err); |
| 3808 | ly_err_free(err); |
| 3809 | } |
| 3810 | LY_CHECK_RET(rc); |
| 3811 | |
| 3812 | /* finally check the identity itself */ |
| 3813 | if (self_match && (data.ident == val->ident)) { |
| 3814 | set_fill_boolean(set, 1); |
| 3815 | found = 1; |
| 3816 | } |
| 3817 | if (!found) { |
| 3818 | LY_ARRAY_FOR(data.ident->derived, u) { |
| 3819 | if (data.ident->derived[u] == val->ident) { |
| 3820 | set_fill_boolean(set, 1); |
| 3821 | found = 1; |
| 3822 | break; |
| 3823 | } |
| 3824 | } |
| 3825 | } |
| 3826 | |
| 3827 | /* free temporary value */ |
| 3828 | val->realtype->plugin->free(set->ctx, &data); |
| 3829 | if (found) { |
| 3830 | break; |
| 3831 | } |
| 3832 | } |
| 3833 | |
| 3834 | return LY_SUCCESS; |
| 3835 | } |
| 3836 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3837 | /** |
| 3838 | * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending |
| 3839 | * on whether the first argument nodes contain a node of an identity derived from the second |
| 3840 | * argument identity. |
| 3841 | * |
| 3842 | * @param[in] args Array of arguments. |
| 3843 | * @param[in] arg_count Count of elements in @p args. |
| 3844 | * @param[in,out] set Context and result set at the same time. |
| 3845 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3846 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3847 | */ |
| 3848 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3849 | xpath_derived_from(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3850 | { |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3851 | return xpath_derived_(args, set, options, 0, __func__); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3852 | } |
| 3853 | |
| 3854 | /** |
| 3855 | * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending |
| 3856 | * on whether the first argument nodes contain a node of an identity that either is or is derived from |
| 3857 | * the second argument identity. |
| 3858 | * |
| 3859 | * @param[in] args Array of arguments. |
| 3860 | * @param[in] arg_count Count of elements in @p args. |
| 3861 | * @param[in,out] set Context and result set at the same time. |
| 3862 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3863 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3864 | */ |
| 3865 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3866 | xpath_derived_from_or_self(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3867 | { |
Michal Vasko | e0dd59d | 2020-07-17 16:10:23 +0200 | [diff] [blame] | 3868 | return xpath_derived_(args, set, options, 1, __func__); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3869 | } |
| 3870 | |
| 3871 | /** |
| 3872 | * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER |
| 3873 | * with the integer value of the first node's enum value, otherwise NaN. |
| 3874 | * |
| 3875 | * @param[in] args Array of arguments. |
| 3876 | * @param[in] arg_count Count of elements in @p args. |
| 3877 | * @param[in,out] set Context and result set at the same time. |
| 3878 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3879 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3880 | */ |
| 3881 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3882 | xpath_enum_value(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3883 | { |
| 3884 | struct lyd_node_term *leaf; |
| 3885 | struct lysc_node_leaf *sleaf; |
| 3886 | LY_ERR rc = LY_SUCCESS; |
| 3887 | |
| 3888 | if (options & LYXP_SCNODE_ALL) { |
| 3889 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3890 | 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] | 3891 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3892 | 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] | 3893 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) { |
| 3894 | 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] | 3895 | } |
| 3896 | set_scnode_clear_ctx(set); |
| 3897 | return rc; |
| 3898 | } |
| 3899 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3900 | if (args[0]->type != LYXP_SET_NODE_SET) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3901 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "enum-value(node-set)"); |
| 3902 | return LY_EVALID; |
| 3903 | } |
| 3904 | |
| 3905 | set_fill_number(set, NAN); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 3906 | if (args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3907 | leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node; |
| 3908 | sleaf = (struct lysc_node_leaf *)leaf->schema; |
| 3909 | if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) { |
| 3910 | set_fill_number(set, leaf->value.enum_item->value); |
| 3911 | } |
| 3912 | } |
| 3913 | |
| 3914 | return LY_SUCCESS; |
| 3915 | } |
| 3916 | |
| 3917 | /** |
| 3918 | * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN |
| 3919 | * with false value. |
| 3920 | * |
| 3921 | * @param[in] args Array of arguments. |
| 3922 | * @param[in] arg_count Count of elements in @p args. |
| 3923 | * @param[in,out] set Context and result set at the same time. |
| 3924 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3925 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3926 | */ |
| 3927 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3928 | xpath_false(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3929 | { |
| 3930 | if (options & LYXP_SCNODE_ALL) { |
| 3931 | set_scnode_clear_ctx(set); |
| 3932 | return LY_SUCCESS; |
| 3933 | } |
| 3934 | |
| 3935 | set_fill_boolean(set, 0); |
| 3936 | return LY_SUCCESS; |
| 3937 | } |
| 3938 | |
| 3939 | /** |
| 3940 | * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER |
| 3941 | * with the first argument floored (truncated). |
| 3942 | * |
| 3943 | * @param[in] args Array of arguments. |
| 3944 | * @param[in] arg_count Count of elements in @p args. |
| 3945 | * @param[in,out] set Context and result set at the same time. |
| 3946 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3947 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3948 | */ |
| 3949 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3950 | xpath_floor(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t UNUSED(options)) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3951 | { |
| 3952 | LY_ERR rc; |
| 3953 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3954 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3955 | LY_CHECK_RET(rc); |
| 3956 | if (isfinite(args[0]->val.num)) { |
| 3957 | set_fill_number(set, (long long)args[0]->val.num); |
| 3958 | } |
| 3959 | |
| 3960 | return LY_SUCCESS; |
| 3961 | } |
| 3962 | |
| 3963 | /** |
| 3964 | * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN |
| 3965 | * whether the language of the text matches the one from the argument. |
| 3966 | * |
| 3967 | * @param[in] args Array of arguments. |
| 3968 | * @param[in] arg_count Count of elements in @p args. |
| 3969 | * @param[in,out] set Context and result set at the same time. |
| 3970 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 3971 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3972 | */ |
| 3973 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 3974 | xpath_lang(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3975 | { |
| 3976 | const struct lyd_node *node; |
| 3977 | struct lysc_node_leaf *sleaf; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 3978 | struct lyd_meta *meta = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3979 | const char *val; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3980 | LY_ERR rc = LY_SUCCESS; |
| 3981 | |
| 3982 | if (options & LYXP_SCNODE_ALL) { |
| 3983 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 3984 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 3985 | 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] | 3986 | } else if (!warn_is_string_type(sleaf->type)) { |
| 3987 | 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] | 3988 | } |
| 3989 | } |
| 3990 | set_scnode_clear_ctx(set); |
| 3991 | return rc; |
| 3992 | } |
| 3993 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 3994 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3995 | LY_CHECK_RET(rc); |
| 3996 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 3997 | if (set->type != LYXP_SET_NODE_SET) { |
| 3998 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "lang(string)"); |
| 3999 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4000 | } else if (!set->used) { |
| 4001 | set_fill_boolean(set, 0); |
| 4002 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4003 | } |
| 4004 | |
| 4005 | switch (set->val.nodes[0].type) { |
| 4006 | case LYXP_NODE_ELEM: |
| 4007 | case LYXP_NODE_TEXT: |
| 4008 | node = set->val.nodes[0].node; |
| 4009 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4010 | case LYXP_NODE_META: |
| 4011 | node = set->val.meta[0].meta->parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4012 | break; |
| 4013 | default: |
| 4014 | /* nothing to do with roots */ |
| 4015 | set_fill_boolean(set, 0); |
| 4016 | return LY_SUCCESS; |
| 4017 | } |
| 4018 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4019 | /* find lang metadata */ |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 4020 | for ( ; node; node = (struct lyd_node *)node->parent) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4021 | for (meta = node->meta; meta; meta = meta->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4022 | /* annotations */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4023 | 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] | 4024 | break; |
| 4025 | } |
| 4026 | } |
| 4027 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4028 | if (meta) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4029 | break; |
| 4030 | } |
| 4031 | } |
| 4032 | |
| 4033 | /* compare languages */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4034 | if (!meta) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4035 | set_fill_boolean(set, 0); |
| 4036 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4037 | uint64_t i; |
| 4038 | |
Michal Vasko | ba99a3e | 2020-08-18 15:50:05 +0200 | [diff] [blame] | 4039 | val = meta->value.canonical; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4040 | for (i = 0; args[0]->val.str[i]; ++i) { |
| 4041 | if (tolower(args[0]->val.str[i]) != tolower(val[i])) { |
| 4042 | set_fill_boolean(set, 0); |
| 4043 | break; |
| 4044 | } |
| 4045 | } |
| 4046 | if (!args[0]->val.str[i]) { |
| 4047 | if (!val[i] || (val[i] == '-')) { |
| 4048 | set_fill_boolean(set, 1); |
| 4049 | } else { |
| 4050 | set_fill_boolean(set, 0); |
| 4051 | } |
| 4052 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4053 | } |
| 4054 | |
| 4055 | return LY_SUCCESS; |
| 4056 | } |
| 4057 | |
| 4058 | /** |
| 4059 | * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER |
| 4060 | * with the context size. |
| 4061 | * |
| 4062 | * @param[in] args Array of arguments. |
| 4063 | * @param[in] arg_count Count of elements in @p args. |
| 4064 | * @param[in,out] set Context and result set at the same time. |
| 4065 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4066 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4067 | */ |
| 4068 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4069 | xpath_last(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4070 | { |
| 4071 | if (options & LYXP_SCNODE_ALL) { |
| 4072 | set_scnode_clear_ctx(set); |
| 4073 | return LY_SUCCESS; |
| 4074 | } |
| 4075 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4076 | if (set->type != LYXP_SET_NODE_SET) { |
| 4077 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "last()"); |
| 4078 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4079 | } else if (!set->used) { |
| 4080 | set_fill_number(set, 0); |
| 4081 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4082 | } |
| 4083 | |
| 4084 | set_fill_number(set, set->ctx_size); |
| 4085 | return LY_SUCCESS; |
| 4086 | } |
| 4087 | |
| 4088 | /** |
| 4089 | * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING |
| 4090 | * with the node name without namespace from the argument or the context. |
| 4091 | * |
| 4092 | * @param[in] args Array of arguments. |
| 4093 | * @param[in] arg_count Count of elements in @p args. |
| 4094 | * @param[in,out] set Context and result set at the same time. |
| 4095 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4096 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4097 | */ |
| 4098 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4099 | xpath_local_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4100 | { |
| 4101 | struct lyxp_set_node *item; |
| 4102 | /* suppress unused variable warning */ |
| 4103 | (void)options; |
| 4104 | |
| 4105 | if (options & LYXP_SCNODE_ALL) { |
| 4106 | set_scnode_clear_ctx(set); |
| 4107 | return LY_SUCCESS; |
| 4108 | } |
| 4109 | |
| 4110 | if (arg_count) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4111 | if (args[0]->type != LYXP_SET_NODE_SET) { |
| 4112 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "local-name(node-set?)"); |
| 4113 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4114 | } else if (!args[0]->used) { |
| 4115 | set_fill_string(set, "", 0); |
| 4116 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4117 | } |
| 4118 | |
| 4119 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4120 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4121 | |
| 4122 | item = &args[0]->val.nodes[0]; |
| 4123 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4124 | if (set->type != LYXP_SET_NODE_SET) { |
| 4125 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "local-name(node-set?)"); |
| 4126 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4127 | } else if (!set->used) { |
| 4128 | set_fill_string(set, "", 0); |
| 4129 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4130 | } |
| 4131 | |
| 4132 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4133 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4134 | |
| 4135 | item = &set->val.nodes[0]; |
| 4136 | } |
| 4137 | |
| 4138 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4139 | case LYXP_NODE_NONE: |
| 4140 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4141 | case LYXP_NODE_ROOT: |
| 4142 | case LYXP_NODE_ROOT_CONFIG: |
| 4143 | case LYXP_NODE_TEXT: |
| 4144 | set_fill_string(set, "", 0); |
| 4145 | break; |
| 4146 | case LYXP_NODE_ELEM: |
| 4147 | set_fill_string(set, item->node->schema->name, strlen(item->node->schema->name)); |
| 4148 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4149 | case LYXP_NODE_META: |
| 4150 | 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] | 4151 | break; |
| 4152 | } |
| 4153 | |
| 4154 | return LY_SUCCESS; |
| 4155 | } |
| 4156 | |
| 4157 | /** |
| 4158 | * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING |
| 4159 | * with the node name fully qualified (with namespace) from the argument or the context. |
| 4160 | * |
| 4161 | * @param[in] args Array of arguments. |
| 4162 | * @param[in] arg_count Count of elements in @p args. |
| 4163 | * @param[in,out] set Context and result set at the same time. |
| 4164 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4165 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4166 | */ |
| 4167 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4168 | xpath_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4169 | { |
| 4170 | struct lyxp_set_node *item; |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 4171 | struct lys_module *mod = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4172 | char *str; |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 4173 | const char *name = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4174 | |
| 4175 | if (options & LYXP_SCNODE_ALL) { |
| 4176 | set_scnode_clear_ctx(set); |
| 4177 | return LY_SUCCESS; |
| 4178 | } |
| 4179 | |
| 4180 | if (arg_count) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4181 | if (args[0]->type != LYXP_SET_NODE_SET) { |
| 4182 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "name(node-set?)"); |
| 4183 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4184 | } else if (!args[0]->used) { |
| 4185 | set_fill_string(set, "", 0); |
| 4186 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4187 | } |
| 4188 | |
| 4189 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4190 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4191 | |
| 4192 | item = &args[0]->val.nodes[0]; |
| 4193 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4194 | if (set->type != LYXP_SET_NODE_SET) { |
| 4195 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "name(node-set?)"); |
| 4196 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4197 | } else if (!set->used) { |
| 4198 | set_fill_string(set, "", 0); |
| 4199 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4200 | } |
| 4201 | |
| 4202 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4203 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4204 | |
| 4205 | item = &set->val.nodes[0]; |
| 4206 | } |
| 4207 | |
| 4208 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4209 | case LYXP_NODE_NONE: |
| 4210 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4211 | case LYXP_NODE_ROOT: |
| 4212 | case LYXP_NODE_ROOT_CONFIG: |
| 4213 | case LYXP_NODE_TEXT: |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 4214 | /* keep NULL */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4215 | break; |
| 4216 | case LYXP_NODE_ELEM: |
| 4217 | mod = item->node->schema->module; |
| 4218 | name = item->node->schema->name; |
| 4219 | break; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4220 | case LYXP_NODE_META: |
| 4221 | mod = ((struct lyd_meta *)item->node)->annotation->module; |
| 4222 | name = ((struct lyd_meta *)item->node)->name; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4223 | break; |
| 4224 | } |
| 4225 | |
| 4226 | if (mod && name) { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4227 | int rc = -1; |
| 4228 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4229 | switch (set->format) { |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 4230 | case LY_PREF_SCHEMA: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4231 | rc = asprintf(&str, "%s:%s", lys_prefix_find_module(set->local_mod, mod), name); |
| 4232 | break; |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 4233 | case LY_PREF_JSON: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4234 | rc = asprintf(&str, "%s:%s", mod->name, name); |
| 4235 | break; |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 4236 | case LY_PREF_XML: |
Michal Vasko | 9409ef6 | 2019-09-12 11:47:17 +0200 | [diff] [blame] | 4237 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4238 | } |
| 4239 | LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM); |
| 4240 | set_fill_string(set, str, strlen(str)); |
| 4241 | free(str); |
| 4242 | } else { |
| 4243 | set_fill_string(set, "", 0); |
| 4244 | } |
| 4245 | |
| 4246 | return LY_SUCCESS; |
| 4247 | } |
| 4248 | |
| 4249 | /** |
| 4250 | * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING |
| 4251 | * with the namespace of the node from the argument or the context. |
| 4252 | * |
| 4253 | * @param[in] args Array of arguments. |
| 4254 | * @param[in] arg_count Count of elements in @p args. |
| 4255 | * @param[in,out] set Context and result set at the same time. |
| 4256 | * @param[in] options XPath options. |
| 4257 | * @return LY_ERR (LY_EINVAL for wrong arguments on schema) |
| 4258 | */ |
| 4259 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4260 | xpath_namespace_uri(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4261 | { |
| 4262 | struct lyxp_set_node *item; |
| 4263 | struct lys_module *mod; |
| 4264 | /* suppress unused variable warning */ |
| 4265 | (void)options; |
| 4266 | |
| 4267 | if (options & LYXP_SCNODE_ALL) { |
| 4268 | set_scnode_clear_ctx(set); |
| 4269 | return LY_SUCCESS; |
| 4270 | } |
| 4271 | |
| 4272 | if (arg_count) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4273 | if (args[0]->type != LYXP_SET_NODE_SET) { |
| 4274 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), |
| 4275 | "namespace-uri(node-set?)"); |
| 4276 | return LY_EVALID; |
| 4277 | } else if (!args[0]->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4278 | set_fill_string(set, "", 0); |
| 4279 | return LY_SUCCESS; |
| 4280 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4281 | |
| 4282 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4283 | assert(!set_sort(args[0])); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4284 | |
| 4285 | item = &args[0]->val.nodes[0]; |
| 4286 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4287 | if (set->type != LYXP_SET_NODE_SET) { |
| 4288 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "namespace-uri(node-set?)"); |
| 4289 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4290 | } else if (!set->used) { |
| 4291 | set_fill_string(set, "", 0); |
| 4292 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4293 | } |
| 4294 | |
| 4295 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4296 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4297 | |
| 4298 | item = &set->val.nodes[0]; |
| 4299 | } |
| 4300 | |
| 4301 | switch (item->type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 4302 | case LYXP_NODE_NONE: |
| 4303 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4304 | case LYXP_NODE_ROOT: |
| 4305 | case LYXP_NODE_ROOT_CONFIG: |
| 4306 | case LYXP_NODE_TEXT: |
| 4307 | set_fill_string(set, "", 0); |
| 4308 | break; |
| 4309 | case LYXP_NODE_ELEM: |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4310 | case LYXP_NODE_META: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4311 | if (item->type == LYXP_NODE_ELEM) { |
| 4312 | mod = item->node->schema->module; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4313 | } else { /* LYXP_NODE_META */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4314 | /* annotations */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 4315 | mod = ((struct lyd_meta *)item->node)->annotation->module; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4316 | } |
| 4317 | |
| 4318 | set_fill_string(set, mod->ns, strlen(mod->ns)); |
| 4319 | break; |
| 4320 | } |
| 4321 | |
| 4322 | return LY_SUCCESS; |
| 4323 | } |
| 4324 | |
| 4325 | /** |
| 4326 | * @brief Execute the XPath node() function (node type). Returns LYXP_SET_NODE_SET |
| 4327 | * with only nodes from the context. In practice it either leaves the context |
| 4328 | * as it is or returns an empty node set. |
| 4329 | * |
| 4330 | * @param[in] args Array of arguments. |
| 4331 | * @param[in] arg_count Count of elements in @p args. |
| 4332 | * @param[in,out] set Context and result set at the same time. |
| 4333 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4334 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4335 | */ |
| 4336 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4337 | xpath_node(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4338 | { |
| 4339 | if (options & LYXP_SCNODE_ALL) { |
| 4340 | set_scnode_clear_ctx(set); |
| 4341 | return LY_SUCCESS; |
| 4342 | } |
| 4343 | |
| 4344 | if (set->type != LYXP_SET_NODE_SET) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4345 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4346 | } |
| 4347 | return LY_SUCCESS; |
| 4348 | } |
| 4349 | |
| 4350 | /** |
| 4351 | * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING |
| 4352 | * with normalized value (no leading, trailing, double white spaces) of the node |
| 4353 | * from the argument or the context. |
| 4354 | * |
| 4355 | * @param[in] args Array of arguments. |
| 4356 | * @param[in] arg_count Count of elements in @p args. |
| 4357 | * @param[in,out] set Context and result set at the same time. |
| 4358 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4359 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4360 | */ |
| 4361 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4362 | xpath_normalize_space(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4363 | { |
| 4364 | uint16_t i, new_used; |
| 4365 | char *new; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4366 | uint8_t have_spaces = 0, space_before = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4367 | struct lysc_node_leaf *sleaf; |
| 4368 | LY_ERR rc = LY_SUCCESS; |
| 4369 | |
| 4370 | if (options & LYXP_SCNODE_ALL) { |
| 4371 | if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4372 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4373 | 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] | 4374 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4375 | 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] | 4376 | } |
| 4377 | } |
| 4378 | set_scnode_clear_ctx(set); |
| 4379 | return rc; |
| 4380 | } |
| 4381 | |
| 4382 | if (arg_count) { |
| 4383 | set_fill_set(set, args[0]); |
| 4384 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4385 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4386 | LY_CHECK_RET(rc); |
| 4387 | |
| 4388 | /* is there any normalization necessary? */ |
| 4389 | for (i = 0; set->val.str[i]; ++i) { |
| 4390 | if (is_xmlws(set->val.str[i])) { |
| 4391 | if ((i == 0) || space_before || (!set->val.str[i + 1])) { |
| 4392 | have_spaces = 1; |
| 4393 | break; |
| 4394 | } |
| 4395 | space_before = 1; |
| 4396 | } else { |
| 4397 | space_before = 0; |
| 4398 | } |
| 4399 | } |
| 4400 | |
| 4401 | /* yep, there is */ |
| 4402 | if (have_spaces) { |
| 4403 | /* it's enough, at least one character will go, makes space for ending '\0' */ |
| 4404 | new = malloc(strlen(set->val.str) * sizeof(char)); |
| 4405 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 4406 | new_used = 0; |
| 4407 | |
| 4408 | space_before = 0; |
| 4409 | for (i = 0; set->val.str[i]; ++i) { |
| 4410 | if (is_xmlws(set->val.str[i])) { |
| 4411 | if ((i == 0) || space_before) { |
| 4412 | space_before = 1; |
| 4413 | continue; |
| 4414 | } else { |
| 4415 | space_before = 1; |
| 4416 | } |
| 4417 | } else { |
| 4418 | space_before = 0; |
| 4419 | } |
| 4420 | |
| 4421 | new[new_used] = (space_before ? ' ' : set->val.str[i]); |
| 4422 | ++new_used; |
| 4423 | } |
| 4424 | |
| 4425 | /* at worst there is one trailing space now */ |
| 4426 | if (new_used && is_xmlws(new[new_used - 1])) { |
| 4427 | --new_used; |
| 4428 | } |
| 4429 | |
| 4430 | new = ly_realloc(new, (new_used + 1) * sizeof(char)); |
| 4431 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 4432 | new[new_used] = '\0'; |
| 4433 | |
| 4434 | free(set->val.str); |
| 4435 | set->val.str = new; |
| 4436 | } |
| 4437 | |
| 4438 | return LY_SUCCESS; |
| 4439 | } |
| 4440 | |
| 4441 | /** |
| 4442 | * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN |
| 4443 | * with the argument converted to boolean and logically inverted. |
| 4444 | * |
| 4445 | * @param[in] args Array of arguments. |
| 4446 | * @param[in] arg_count Count of elements in @p args. |
| 4447 | * @param[in,out] set Context and result set at the same time. |
| 4448 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4449 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4450 | */ |
| 4451 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4452 | xpath_not(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4453 | { |
| 4454 | if (options & LYXP_SCNODE_ALL) { |
| 4455 | set_scnode_clear_ctx(set); |
| 4456 | return LY_SUCCESS; |
| 4457 | } |
| 4458 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4459 | lyxp_set_cast(args[0], LYXP_SET_BOOLEAN); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 4460 | if (args[0]->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4461 | set_fill_boolean(set, 0); |
| 4462 | } else { |
| 4463 | set_fill_boolean(set, 1); |
| 4464 | } |
| 4465 | |
| 4466 | return LY_SUCCESS; |
| 4467 | } |
| 4468 | |
| 4469 | /** |
| 4470 | * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER |
| 4471 | * with the number representation of either the argument or the context. |
| 4472 | * |
| 4473 | * @param[in] args Array of arguments. |
| 4474 | * @param[in] arg_count Count of elements in @p args. |
| 4475 | * @param[in,out] set Context and result set at the same time. |
| 4476 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4477 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4478 | */ |
| 4479 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4480 | xpath_number(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4481 | { |
| 4482 | LY_ERR rc; |
| 4483 | |
| 4484 | if (options & LYXP_SCNODE_ALL) { |
| 4485 | set_scnode_clear_ctx(set); |
| 4486 | return LY_SUCCESS; |
| 4487 | } |
| 4488 | |
| 4489 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4490 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4491 | LY_CHECK_RET(rc); |
| 4492 | set_fill_set(set, args[0]); |
| 4493 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4494 | rc = lyxp_set_cast(set, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4495 | LY_CHECK_RET(rc); |
| 4496 | } |
| 4497 | |
| 4498 | return LY_SUCCESS; |
| 4499 | } |
| 4500 | |
| 4501 | /** |
| 4502 | * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER |
| 4503 | * with the context position. |
| 4504 | * |
| 4505 | * @param[in] args Array of arguments. |
| 4506 | * @param[in] arg_count Count of elements in @p args. |
| 4507 | * @param[in,out] set Context and result set at the same time. |
| 4508 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4509 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4510 | */ |
| 4511 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4512 | xpath_position(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4513 | { |
| 4514 | if (options & LYXP_SCNODE_ALL) { |
| 4515 | set_scnode_clear_ctx(set); |
| 4516 | return LY_SUCCESS; |
| 4517 | } |
| 4518 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4519 | if (set->type != LYXP_SET_NODE_SET) { |
| 4520 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "position()"); |
| 4521 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 4522 | } else if (!set->used) { |
| 4523 | set_fill_number(set, 0); |
| 4524 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4525 | } |
| 4526 | |
| 4527 | set_fill_number(set, set->ctx_pos); |
| 4528 | |
| 4529 | /* UNUSED in 'Release' build type */ |
| 4530 | (void)options; |
| 4531 | return LY_SUCCESS; |
| 4532 | } |
| 4533 | |
| 4534 | /** |
| 4535 | * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN |
| 4536 | * depending on whether the second argument regex matches the first argument string. For details refer to |
| 4537 | * YANG 1.1 RFC section 10.2.1. |
| 4538 | * |
| 4539 | * @param[in] args Array of arguments. |
| 4540 | * @param[in] arg_count Count of elements in @p args. |
| 4541 | * @param[in,out] set Context and result set at the same time. |
| 4542 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4543 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4544 | */ |
| 4545 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4546 | xpath_re_match(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4547 | { |
| 4548 | struct lysc_pattern **patterns = NULL, **pattern; |
| 4549 | struct lysc_node_leaf *sleaf; |
| 4550 | char *path; |
| 4551 | LY_ERR rc = LY_SUCCESS; |
| 4552 | struct ly_err_item *err; |
| 4553 | |
| 4554 | if (options & LYXP_SCNODE_ALL) { |
| 4555 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4556 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4557 | 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] | 4558 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4559 | 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] | 4560 | } |
| 4561 | } |
| 4562 | |
| 4563 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4564 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4565 | 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] | 4566 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4567 | 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] | 4568 | } |
| 4569 | } |
| 4570 | set_scnode_clear_ctx(set); |
| 4571 | return rc; |
| 4572 | } |
| 4573 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4574 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4575 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4576 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4577 | LY_CHECK_RET(rc); |
| 4578 | |
| 4579 | LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM); |
| 4580 | *pattern = malloc(sizeof **pattern); |
| 4581 | path = lyd_path(set->ctx_node, LYD_PATH_LOG, NULL, 0); |
| 4582 | rc = lys_compile_type_pattern_check(set->ctx, path, args[1]->val.str, &(*pattern)->code); |
| 4583 | free(path); |
| 4584 | if (rc != LY_SUCCESS) { |
| 4585 | LY_ARRAY_FREE(patterns); |
| 4586 | return rc; |
| 4587 | } |
| 4588 | |
| 4589 | rc = ly_type_validate_patterns(patterns, args[0]->val.str, strlen(args[0]->val.str), &err); |
| 4590 | pcre2_code_free((*pattern)->code); |
| 4591 | free(*pattern); |
| 4592 | LY_ARRAY_FREE(patterns); |
| 4593 | if (rc && (rc != LY_EVALID)) { |
| 4594 | ly_err_print(err); |
| 4595 | ly_err_free(err); |
| 4596 | return rc; |
| 4597 | } |
| 4598 | |
| 4599 | if (rc == LY_EVALID) { |
| 4600 | ly_err_free(err); |
| 4601 | set_fill_boolean(set, 0); |
| 4602 | } else { |
| 4603 | set_fill_boolean(set, 1); |
| 4604 | } |
| 4605 | |
| 4606 | return LY_SUCCESS; |
| 4607 | } |
| 4608 | |
| 4609 | /** |
| 4610 | * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER |
| 4611 | * with the rounded first argument. For details refer to |
| 4612 | * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round. |
| 4613 | * |
| 4614 | * @param[in] args Array of arguments. |
| 4615 | * @param[in] arg_count Count of elements in @p args. |
| 4616 | * @param[in,out] set Context and result set at the same time. |
| 4617 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4618 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4619 | */ |
| 4620 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4621 | xpath_round(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4622 | { |
| 4623 | struct lysc_node_leaf *sleaf; |
| 4624 | LY_ERR rc = LY_SUCCESS; |
| 4625 | |
| 4626 | if (options & LYXP_SCNODE_ALL) { |
| 4627 | if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4628 | 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] | 4629 | } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4630 | 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] | 4631 | } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) { |
| 4632 | 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] | 4633 | } |
| 4634 | set_scnode_clear_ctx(set); |
| 4635 | return rc; |
| 4636 | } |
| 4637 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4638 | rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4639 | LY_CHECK_RET(rc); |
| 4640 | |
| 4641 | /* cover only the cases where floor can't be used */ |
| 4642 | if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) { |
| 4643 | set_fill_number(set, -0.0f); |
| 4644 | } else { |
| 4645 | args[0]->val.num += 0.5; |
| 4646 | rc = xpath_floor(args, 1, args[0], options); |
| 4647 | LY_CHECK_RET(rc); |
| 4648 | set_fill_number(set, args[0]->val.num); |
| 4649 | } |
| 4650 | |
| 4651 | return LY_SUCCESS; |
| 4652 | } |
| 4653 | |
| 4654 | /** |
| 4655 | * @brief Execute the XPath starts-with(string, string) function. |
| 4656 | * Returns LYXP_SET_BOOLEAN whether the second argument is |
| 4657 | * the prefix of the first or not. |
| 4658 | * |
| 4659 | * @param[in] args Array of arguments. |
| 4660 | * @param[in] arg_count Count of elements in @p args. |
| 4661 | * @param[in,out] set Context and result set at the same time. |
| 4662 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4663 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4664 | */ |
| 4665 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4666 | xpath_starts_with(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4667 | { |
| 4668 | struct lysc_node_leaf *sleaf; |
| 4669 | LY_ERR rc = LY_SUCCESS; |
| 4670 | |
| 4671 | if (options & LYXP_SCNODE_ALL) { |
| 4672 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4673 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4674 | 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] | 4675 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4676 | 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] | 4677 | } |
| 4678 | } |
| 4679 | |
| 4680 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4681 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4682 | 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] | 4683 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4684 | 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] | 4685 | } |
| 4686 | } |
| 4687 | set_scnode_clear_ctx(set); |
| 4688 | return rc; |
| 4689 | } |
| 4690 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4691 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4692 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4693 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4694 | LY_CHECK_RET(rc); |
| 4695 | |
| 4696 | if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) { |
| 4697 | set_fill_boolean(set, 0); |
| 4698 | } else { |
| 4699 | set_fill_boolean(set, 1); |
| 4700 | } |
| 4701 | |
| 4702 | return LY_SUCCESS; |
| 4703 | } |
| 4704 | |
| 4705 | /** |
| 4706 | * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING |
| 4707 | * with the string representation of either the argument or the context. |
| 4708 | * |
| 4709 | * @param[in] args Array of arguments. |
| 4710 | * @param[in] arg_count Count of elements in @p args. |
| 4711 | * @param[in,out] set Context and result set at the same time. |
| 4712 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4713 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4714 | */ |
| 4715 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4716 | xpath_string(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4717 | { |
| 4718 | LY_ERR rc; |
| 4719 | |
| 4720 | if (options & LYXP_SCNODE_ALL) { |
| 4721 | set_scnode_clear_ctx(set); |
| 4722 | return LY_SUCCESS; |
| 4723 | } |
| 4724 | |
| 4725 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4726 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4727 | LY_CHECK_RET(rc); |
| 4728 | set_fill_set(set, args[0]); |
| 4729 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4730 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4731 | LY_CHECK_RET(rc); |
| 4732 | } |
| 4733 | |
| 4734 | return LY_SUCCESS; |
| 4735 | } |
| 4736 | |
| 4737 | /** |
| 4738 | * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER |
| 4739 | * with the length of the string in either the argument or the context. |
| 4740 | * |
| 4741 | * @param[in] args Array of arguments. |
| 4742 | * @param[in] arg_count Count of elements in @p args. |
| 4743 | * @param[in,out] set Context and result set at the same time. |
| 4744 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4745 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4746 | */ |
| 4747 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4748 | xpath_string_length(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4749 | { |
| 4750 | struct lysc_node_leaf *sleaf; |
| 4751 | LY_ERR rc = LY_SUCCESS; |
| 4752 | |
| 4753 | if (options & LYXP_SCNODE_ALL) { |
| 4754 | if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4755 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4756 | 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] | 4757 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4758 | 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] | 4759 | } |
| 4760 | } |
| 4761 | if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) { |
| 4762 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4763 | 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] | 4764 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4765 | 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] | 4766 | } |
| 4767 | } |
| 4768 | set_scnode_clear_ctx(set); |
| 4769 | return rc; |
| 4770 | } |
| 4771 | |
| 4772 | if (arg_count) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4773 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4774 | LY_CHECK_RET(rc); |
| 4775 | set_fill_number(set, strlen(args[0]->val.str)); |
| 4776 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4777 | rc = lyxp_set_cast(set, LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4778 | LY_CHECK_RET(rc); |
| 4779 | set_fill_number(set, strlen(set->val.str)); |
| 4780 | } |
| 4781 | |
| 4782 | return LY_SUCCESS; |
| 4783 | } |
| 4784 | |
| 4785 | /** |
| 4786 | * @brief Execute the XPath substring(string, number, number?) function. |
| 4787 | * Returns LYXP_SET_STRING substring of the first argument starting |
| 4788 | * on the second argument index ending on the third argument index, |
| 4789 | * indexed from 1. For exact definition refer to |
| 4790 | * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring. |
| 4791 | * |
| 4792 | * @param[in] args Array of arguments. |
| 4793 | * @param[in] arg_count Count of elements in @p args. |
| 4794 | * @param[in,out] set Context and result set at the same time. |
| 4795 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4796 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4797 | */ |
| 4798 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4799 | xpath_substring(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4800 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4801 | int32_t start, len; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4802 | uint16_t str_start, str_len, pos; |
| 4803 | struct lysc_node_leaf *sleaf; |
| 4804 | LY_ERR rc = LY_SUCCESS; |
| 4805 | |
| 4806 | if (options & LYXP_SCNODE_ALL) { |
| 4807 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4808 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4809 | 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] | 4810 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4811 | 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] | 4812 | } |
| 4813 | } |
| 4814 | |
| 4815 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4816 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4817 | 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] | 4818 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 4819 | 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] | 4820 | } |
| 4821 | } |
| 4822 | |
| 4823 | if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET) |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 4824 | && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4825 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4826 | 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] | 4827 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 4828 | 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] | 4829 | } |
| 4830 | } |
| 4831 | set_scnode_clear_ctx(set); |
| 4832 | return rc; |
| 4833 | } |
| 4834 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4835 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4836 | LY_CHECK_RET(rc); |
| 4837 | |
| 4838 | /* start */ |
| 4839 | if (xpath_round(&args[1], 1, args[1], options)) { |
| 4840 | return -1; |
| 4841 | } |
| 4842 | if (isfinite(args[1]->val.num)) { |
| 4843 | start = args[1]->val.num - 1; |
| 4844 | } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4845 | start = INT32_MIN; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4846 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4847 | start = INT32_MAX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4848 | } |
| 4849 | |
| 4850 | /* len */ |
| 4851 | if (arg_count == 3) { |
| 4852 | rc = xpath_round(&args[2], 1, args[2], options); |
| 4853 | LY_CHECK_RET(rc); |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4854 | if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4855 | len = 0; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4856 | } else if (isfinite(args[2]->val.num)) { |
| 4857 | len = args[2]->val.num; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4858 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4859 | len = INT32_MAX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4860 | } |
| 4861 | } else { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4862 | len = INT32_MAX; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4863 | } |
| 4864 | |
| 4865 | /* find matching character positions */ |
| 4866 | str_start = 0; |
| 4867 | str_len = 0; |
| 4868 | for (pos = 0; args[0]->val.str[pos]; ++pos) { |
| 4869 | if (pos < start) { |
| 4870 | ++str_start; |
| 4871 | } else if (pos < start + len) { |
| 4872 | ++str_len; |
| 4873 | } else { |
| 4874 | break; |
| 4875 | } |
| 4876 | } |
| 4877 | |
| 4878 | set_fill_string(set, args[0]->val.str + str_start, str_len); |
| 4879 | return LY_SUCCESS; |
| 4880 | } |
| 4881 | |
| 4882 | /** |
| 4883 | * @brief Execute the XPath substring-after(string, string) function. |
| 4884 | * Returns LYXP_SET_STRING with the string succeeding the occurance |
| 4885 | * of the second argument in the first or an empty string. |
| 4886 | * |
| 4887 | * @param[in] args Array of arguments. |
| 4888 | * @param[in] arg_count Count of elements in @p args. |
| 4889 | * @param[in,out] set Context and result set at the same time. |
| 4890 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4891 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4892 | */ |
| 4893 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4894 | xpath_substring_after(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4895 | { |
| 4896 | char *ptr; |
| 4897 | struct lysc_node_leaf *sleaf; |
| 4898 | LY_ERR rc = LY_SUCCESS; |
| 4899 | |
| 4900 | if (options & LYXP_SCNODE_ALL) { |
| 4901 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4902 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4903 | 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] | 4904 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4905 | 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] | 4906 | } |
| 4907 | } |
| 4908 | |
| 4909 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4910 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4911 | 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] | 4912 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4913 | 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] | 4914 | } |
| 4915 | } |
| 4916 | set_scnode_clear_ctx(set); |
| 4917 | return rc; |
| 4918 | } |
| 4919 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4920 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4921 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4922 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4923 | LY_CHECK_RET(rc); |
| 4924 | |
| 4925 | ptr = strstr(args[0]->val.str, args[1]->val.str); |
| 4926 | if (ptr) { |
| 4927 | set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str))); |
| 4928 | } else { |
| 4929 | set_fill_string(set, "", 0); |
| 4930 | } |
| 4931 | |
| 4932 | return LY_SUCCESS; |
| 4933 | } |
| 4934 | |
| 4935 | /** |
| 4936 | * @brief Execute the XPath substring-before(string, string) function. |
| 4937 | * Returns LYXP_SET_STRING with the string preceding the occurance |
| 4938 | * of the second argument in the first or an empty string. |
| 4939 | * |
| 4940 | * @param[in] args Array of arguments. |
| 4941 | * @param[in] arg_count Count of elements in @p args. |
| 4942 | * @param[in,out] set Context and result set at the same time. |
| 4943 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4944 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4945 | */ |
| 4946 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4947 | xpath_substring_before(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4948 | { |
| 4949 | char *ptr; |
| 4950 | struct lysc_node_leaf *sleaf; |
| 4951 | LY_ERR rc = LY_SUCCESS; |
| 4952 | |
| 4953 | if (options & LYXP_SCNODE_ALL) { |
| 4954 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 4955 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4956 | 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] | 4957 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4958 | 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] | 4959 | } |
| 4960 | } |
| 4961 | |
| 4962 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 4963 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 4964 | 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] | 4965 | } else if (!warn_is_string_type(sleaf->type)) { |
| 4966 | 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] | 4967 | } |
| 4968 | } |
| 4969 | set_scnode_clear_ctx(set); |
| 4970 | return rc; |
| 4971 | } |
| 4972 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4973 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4974 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 4975 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4976 | LY_CHECK_RET(rc); |
| 4977 | |
| 4978 | ptr = strstr(args[0]->val.str, args[1]->val.str); |
| 4979 | if (ptr) { |
| 4980 | set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str); |
| 4981 | } else { |
| 4982 | set_fill_string(set, "", 0); |
| 4983 | } |
| 4984 | |
| 4985 | return LY_SUCCESS; |
| 4986 | } |
| 4987 | |
| 4988 | /** |
| 4989 | * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER |
| 4990 | * with the sum of all the nodes in the context. |
| 4991 | * |
| 4992 | * @param[in] args Array of arguments. |
| 4993 | * @param[in] arg_count Count of elements in @p args. |
| 4994 | * @param[in,out] set Context and result set at the same time. |
| 4995 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 4996 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 4997 | */ |
| 4998 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 4999 | xpath_sum(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5000 | { |
| 5001 | long double num; |
| 5002 | char *str; |
| 5003 | uint16_t i; |
| 5004 | struct lyxp_set set_item; |
| 5005 | struct lysc_node_leaf *sleaf; |
| 5006 | LY_ERR rc = LY_SUCCESS; |
| 5007 | |
| 5008 | if (options & LYXP_SCNODE_ALL) { |
| 5009 | if (args[0]->type == LYXP_SET_SCNODE_SET) { |
| 5010 | for (i = 0; i < args[0]->used; ++i) { |
| 5011 | if (args[0]->val.scnodes[i].in_ctx == 1) { |
| 5012 | sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode; |
| 5013 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5014 | LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, |
| 5015 | lys_nodetype2str(sleaf->nodetype), sleaf->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5016 | } else if (!warn_is_numeric_type(sleaf->type)) { |
| 5017 | 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] | 5018 | } |
| 5019 | } |
| 5020 | } |
| 5021 | } |
| 5022 | set_scnode_clear_ctx(set); |
| 5023 | return rc; |
| 5024 | } |
| 5025 | |
| 5026 | set_fill_number(set, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5027 | |
| 5028 | if (args[0]->type != LYXP_SET_NODE_SET) { |
| 5029 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "sum(node-set)"); |
| 5030 | return LY_EVALID; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5031 | } else if (!args[0]->used) { |
| 5032 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5033 | } |
| 5034 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5035 | set_init(&set_item, set); |
| 5036 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5037 | set_item.type = LYXP_SET_NODE_SET; |
| 5038 | set_item.val.nodes = malloc(sizeof *set_item.val.nodes); |
| 5039 | LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM); |
| 5040 | |
| 5041 | set_item.used = 1; |
| 5042 | set_item.size = 1; |
| 5043 | |
| 5044 | for (i = 0; i < args[0]->used; ++i) { |
| 5045 | set_item.val.nodes[0] = args[0]->val.nodes[i]; |
| 5046 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5047 | rc = cast_node_set_to_string(&set_item, &str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5048 | LY_CHECK_RET(rc); |
| 5049 | num = cast_string_to_number(str); |
| 5050 | free(str); |
| 5051 | set->val.num += num; |
| 5052 | } |
| 5053 | |
| 5054 | free(set_item.val.nodes); |
| 5055 | |
| 5056 | return LY_SUCCESS; |
| 5057 | } |
| 5058 | |
| 5059 | /** |
| 5060 | * @brief Execute the XPath text() function (node type). Returns LYXP_SET_NODE_SET |
| 5061 | * with the text content of the nodes in the context. |
| 5062 | * |
| 5063 | * @param[in] args Array of arguments. |
| 5064 | * @param[in] arg_count Count of elements in @p args. |
| 5065 | * @param[in,out] set Context and result set at the same time. |
| 5066 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5067 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5068 | */ |
| 5069 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5070 | xpath_text(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5071 | { |
| 5072 | uint32_t i; |
| 5073 | |
| 5074 | if (options & LYXP_SCNODE_ALL) { |
| 5075 | set_scnode_clear_ctx(set); |
| 5076 | return LY_SUCCESS; |
| 5077 | } |
| 5078 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5079 | if (set->type != LYXP_SET_NODE_SET) { |
| 5080 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "text()"); |
| 5081 | return LY_EVALID; |
| 5082 | } |
| 5083 | |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 5084 | for (i = 0; i < set->used; ) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5085 | switch (set->val.nodes[i].type) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 5086 | case LYXP_NODE_NONE: |
| 5087 | LOGINT_RET(set->ctx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5088 | case LYXP_NODE_ELEM: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5089 | if (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) { |
| 5090 | set->val.nodes[i].type = LYXP_NODE_TEXT; |
| 5091 | ++i; |
| 5092 | break; |
| 5093 | } |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 5094 | /* fall through */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5095 | case LYXP_NODE_ROOT: |
| 5096 | case LYXP_NODE_ROOT_CONFIG: |
| 5097 | case LYXP_NODE_TEXT: |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5098 | case LYXP_NODE_META: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5099 | set_remove_node(set, i); |
| 5100 | break; |
| 5101 | } |
| 5102 | } |
| 5103 | |
| 5104 | return LY_SUCCESS; |
| 5105 | } |
| 5106 | |
| 5107 | /** |
| 5108 | * @brief Execute the XPath translate(string, string, string) function. |
| 5109 | * Returns LYXP_SET_STRING with the first argument with the characters |
| 5110 | * from the second argument replaced by those on the corresponding |
| 5111 | * positions in the third argument. |
| 5112 | * |
| 5113 | * @param[in] args Array of arguments. |
| 5114 | * @param[in] arg_count Count of elements in @p args. |
| 5115 | * @param[in,out] set Context and result set at the same time. |
| 5116 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5117 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5118 | */ |
| 5119 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5120 | xpath_translate(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5121 | { |
| 5122 | uint16_t i, j, new_used; |
| 5123 | char *new; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5124 | uint8_t have_removed; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5125 | struct lysc_node_leaf *sleaf; |
| 5126 | LY_ERR rc = LY_SUCCESS; |
| 5127 | |
| 5128 | if (options & LYXP_SCNODE_ALL) { |
| 5129 | if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) { |
| 5130 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5131 | 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] | 5132 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5133 | 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] | 5134 | } |
| 5135 | } |
| 5136 | |
| 5137 | if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) { |
| 5138 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5139 | 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] | 5140 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5141 | 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] | 5142 | } |
| 5143 | } |
| 5144 | |
| 5145 | if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) { |
| 5146 | if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) { |
| 5147 | 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] | 5148 | } else if (!warn_is_string_type(sleaf->type)) { |
| 5149 | 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] | 5150 | } |
| 5151 | } |
| 5152 | set_scnode_clear_ctx(set); |
| 5153 | return rc; |
| 5154 | } |
| 5155 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5156 | rc = lyxp_set_cast(args[0], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5157 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5158 | rc = lyxp_set_cast(args[1], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5159 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5160 | rc = lyxp_set_cast(args[2], LYXP_SET_STRING); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5161 | LY_CHECK_RET(rc); |
| 5162 | |
| 5163 | new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char)); |
| 5164 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 5165 | new_used = 0; |
| 5166 | |
| 5167 | have_removed = 0; |
| 5168 | for (i = 0; args[0]->val.str[i]; ++i) { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5169 | uint8_t found = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5170 | |
| 5171 | for (j = 0; args[1]->val.str[j]; ++j) { |
| 5172 | if (args[0]->val.str[i] == args[1]->val.str[j]) { |
| 5173 | /* removing this char */ |
| 5174 | if (j >= strlen(args[2]->val.str)) { |
| 5175 | have_removed = 1; |
| 5176 | found = 1; |
| 5177 | break; |
| 5178 | } |
| 5179 | /* replacing this char */ |
| 5180 | new[new_used] = args[2]->val.str[j]; |
| 5181 | ++new_used; |
| 5182 | found = 1; |
| 5183 | break; |
| 5184 | } |
| 5185 | } |
| 5186 | |
| 5187 | /* copying this char */ |
| 5188 | if (!found) { |
| 5189 | new[new_used] = args[0]->val.str[i]; |
| 5190 | ++new_used; |
| 5191 | } |
| 5192 | } |
| 5193 | |
| 5194 | if (have_removed) { |
| 5195 | new = ly_realloc(new, (new_used + 1) * sizeof(char)); |
| 5196 | LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM); |
| 5197 | } |
| 5198 | new[new_used] = '\0'; |
| 5199 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5200 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5201 | set->type = LYXP_SET_STRING; |
| 5202 | set->val.str = new; |
| 5203 | |
| 5204 | return LY_SUCCESS; |
| 5205 | } |
| 5206 | |
| 5207 | /** |
| 5208 | * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN |
| 5209 | * with true value. |
| 5210 | * |
| 5211 | * @param[in] args Array of arguments. |
| 5212 | * @param[in] arg_count Count of elements in @p args. |
| 5213 | * @param[in,out] set Context and result set at the same time. |
| 5214 | * @param[in] options XPath options. |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 5215 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5216 | */ |
| 5217 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5218 | xpath_true(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5219 | { |
| 5220 | if (options & LYXP_SCNODE_ALL) { |
| 5221 | set_scnode_clear_ctx(set); |
| 5222 | return LY_SUCCESS; |
| 5223 | } |
| 5224 | |
| 5225 | set_fill_boolean(set, 1); |
| 5226 | return LY_SUCCESS; |
| 5227 | } |
| 5228 | |
| 5229 | /* |
| 5230 | * moveto functions |
| 5231 | * |
| 5232 | * They and only they actually change the context (set). |
| 5233 | */ |
| 5234 | |
| 5235 | /** |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5236 | * @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] | 5237 | * |
Michal Vasko | 2104e9f | 2020-03-06 08:23:25 +0100 | [diff] [blame] | 5238 | * XPath @p set is expected to be a (sc)node set! |
| 5239 | * |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5240 | * @param[in,out] qname Qualified node name. If includes prefix, it is skipped. |
| 5241 | * @param[in,out] qname_len Length of @p qname, is updated accordingly. |
| 5242 | * @param[in] set Set with XPath context. |
| 5243 | * @param[out] moveto_mod Expected module of a matching node. |
| 5244 | * @return LY_ERR |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5245 | */ |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5246 | static LY_ERR |
| 5247 | 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] | 5248 | { |
Michal Vasko | ed4fcfe | 2020-07-08 10:38:56 +0200 | [diff] [blame] | 5249 | const struct lys_module *mod = NULL; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5250 | const char *ptr; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5251 | size_t pref_len; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5252 | |
Michal Vasko | 2104e9f | 2020-03-06 08:23:25 +0100 | [diff] [blame] | 5253 | assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET)); |
| 5254 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5255 | if ((ptr = ly_strnchr(*qname, ':', *qname_len))) { |
| 5256 | /* specific module */ |
| 5257 | pref_len = ptr - *qname; |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 5258 | mod = ly_resolve_prefix(set->ctx, *qname, pref_len, set->format, (void *)set->local_mod); |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5259 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5260 | /* check for errors and non-implemented modules, as they are not valid */ |
Juraj Vijtiuk | d75faa6 | 2019-11-26 14:10:10 +0100 | [diff] [blame] | 5261 | if (!mod || !mod->implemented) { |
Michal Vasko | 2104e9f | 2020-03-06 08:23:25 +0100 | [diff] [blame] | 5262 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 5263 | LOGVAL(set->ctx, LY_VLOG_LYSC, set->ctx_scnode, LY_VCODE_XP_INMOD, pref_len, *qname); |
| 5264 | } else { |
| 5265 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INMOD, pref_len, *qname); |
| 5266 | } |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5267 | return LY_EVALID; |
| 5268 | } |
Juraj Vijtiuk | d75faa6 | 2019-11-26 14:10:10 +0100 | [diff] [blame] | 5269 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5270 | *qname += pref_len + 1; |
| 5271 | *qname_len -= pref_len + 1; |
| 5272 | } else if (((*qname)[0] == '*') && (*qname_len == 1)) { |
| 5273 | /* all modules - special case */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5274 | mod = NULL; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5275 | } else if (set->type == LYXP_SET_SCNODE_SET) { |
| 5276 | /* current node module */ |
| 5277 | mod = set->ctx_scnode->module; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5278 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5279 | /* current node module */ |
| 5280 | mod = set->ctx_node->schema->module; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5281 | } |
| 5282 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5283 | *moveto_mod = mod; |
| 5284 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5285 | } |
| 5286 | |
| 5287 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5288 | * @brief Move context @p set to the root. Handles absolute path. |
| 5289 | * Result is LYXP_SET_NODE_SET. |
| 5290 | * |
| 5291 | * @param[in,out] set Set to use. |
| 5292 | * @param[in] options Xpath options. |
| 5293 | */ |
| 5294 | static void |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5295 | moveto_root(struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5296 | { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5297 | if (!set) { |
| 5298 | return; |
| 5299 | } |
| 5300 | |
| 5301 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5302 | set_scnode_clear_ctx(set); |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame^] | 5303 | lyxp_set_scnode_insert_node(set, NULL, set->root_type, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5304 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5305 | set->type = LYXP_SET_NODE_SET; |
| 5306 | set->used = 0; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5307 | set_insert_node(set, NULL, 0, set->root_type, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5308 | } |
| 5309 | } |
| 5310 | |
| 5311 | /** |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 5312 | * @brief Check whether a node has some unresolved "when". |
| 5313 | * |
| 5314 | * @param[in] node Node to check. |
| 5315 | * @return LY_ERR value (LY_EINCOMPLETE if there are some unresolved "when") |
| 5316 | */ |
| 5317 | static LY_ERR |
| 5318 | moveto_when_check(const struct lyd_node *node) |
| 5319 | { |
| 5320 | const struct lysc_node *schema; |
| 5321 | |
| 5322 | if (!node) { |
| 5323 | return LY_SUCCESS; |
| 5324 | } |
| 5325 | |
| 5326 | schema = node->schema; |
| 5327 | do { |
| 5328 | if (schema->when && !(node->flags & LYD_WHEN_TRUE)) { |
| 5329 | return LY_EINCOMPLETE; |
| 5330 | } |
| 5331 | schema = schema->parent; |
| 5332 | } while (schema && (schema->nodetype & (LYS_CASE | LYS_CHOICE))); |
| 5333 | |
| 5334 | return LY_SUCCESS; |
| 5335 | } |
| 5336 | |
| 5337 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5338 | * @brief Check @p node as a part of NameTest processing. |
| 5339 | * |
| 5340 | * @param[in] node Node to check. |
| 5341 | * @param[in] root_type XPath root node type. |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5342 | * @param[in] context_op XPath operation parent. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5343 | * @param[in] node_name Node name in the dictionary to move to, NULL for any node. |
| 5344 | * @param[in] moveto_mod Expected module of the node, NULL for any. |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5345 | * @return LY_ERR (LY_ENOT if node does not match, LY_EINCOMPLETE on unresolved when, |
| 5346 | * LY_EINVAL if netither node nor any children match) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5347 | */ |
| 5348 | static LY_ERR |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5349 | moveto_node_check(const struct lyd_node *node, enum lyxp_node_type root_type, const struct lysc_node *context_op, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 5350 | const char *node_name, const struct lys_module *moveto_mod) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5351 | { |
| 5352 | /* module check */ |
| 5353 | if (moveto_mod && (node->schema->module != moveto_mod)) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5354 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5355 | } |
| 5356 | |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5357 | /* context check */ |
| 5358 | 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] | 5359 | return LY_EINVAL; |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5360 | } else if (context_op && (node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node->schema != context_op)) { |
| 5361 | return LY_EINVAL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5362 | } |
| 5363 | |
| 5364 | /* name check */ |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5365 | if (node_name && strcmp(node_name, "*") && (node->schema->name != node_name)) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5366 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5367 | } |
| 5368 | |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 5369 | /* when check */ |
| 5370 | if (moveto_when_check(node)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5371 | return LY_EINCOMPLETE; |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 5372 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5373 | |
| 5374 | /* match */ |
| 5375 | return LY_SUCCESS; |
| 5376 | } |
| 5377 | |
| 5378 | /** |
| 5379 | * @brief Check @p node as a part of schema NameTest processing. |
| 5380 | * |
| 5381 | * @param[in] node Schema node to check. |
| 5382 | * @param[in] root_type XPath root node type. |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5383 | * @param[in] context_op XPath operation parent. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5384 | * @param[in] node_name Node name in the dictionary to move to, NULL for any nodes. |
| 5385 | * @param[in] moveto_mod Expected module of the node, NULL for any. |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5386 | * @return LY_ERR (LY_ENOT if node does not match, LY_EINVAL if neither node nor any children match) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5387 | */ |
| 5388 | static LY_ERR |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5389 | moveto_scnode_check(const struct lysc_node *node, enum lyxp_node_type root_type, const struct lysc_node *context_op, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 5390 | const char *node_name, const struct lys_module *moveto_mod) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5391 | { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5392 | /* module check */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5393 | if (moveto_mod && (node->module != moveto_mod)) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5394 | return LY_ENOT; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5395 | } |
| 5396 | |
| 5397 | /* context check */ |
| 5398 | if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) { |
| 5399 | return LY_EINVAL; |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5400 | } else if (context_op && (node->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node != context_op)) { |
| 5401 | return LY_EINVAL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 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 | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5425 | const struct lyd_node *siblings, *sub; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5426 | LY_ERR rc; |
| 5427 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5428 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5429 | return LY_SUCCESS; |
| 5430 | } |
| 5431 | |
| 5432 | if (set->type != LYXP_SET_NODE_SET) { |
| 5433 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 5434 | return LY_EVALID; |
| 5435 | } |
| 5436 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5437 | for (i = 0; i < set->used; ) { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5438 | uint8_t replaced = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5439 | |
| 5440 | if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5441 | assert(!set->val.nodes[i].node); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5442 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5443 | /* search in all the trees */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5444 | siblings = set->tree; |
Michal Vasko | 5bfd4be | 2020-06-23 13:26:19 +0200 | [diff] [blame] | 5445 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5446 | /* search in children */ |
Michal Vasko | 5bfd4be | 2020-06-23 13:26:19 +0200 | [diff] [blame] | 5447 | siblings = lyd_node_children(set->val.nodes[i].node, 0); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5448 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5449 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5450 | for (sub = siblings; sub; sub = sub->next) { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5451 | rc = moveto_node_check(sub, set->root_type, set->context_op, ncname, mod); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5452 | if (rc == LY_SUCCESS) { |
| 5453 | if (!replaced) { |
| 5454 | set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i); |
| 5455 | replaced = 1; |
| 5456 | } else { |
| 5457 | set_insert_node(set, sub, 0, LYXP_NODE_ELEM, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5458 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5459 | ++i; |
| 5460 | } else if (rc == LY_EINCOMPLETE) { |
| 5461 | return rc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5462 | } |
| 5463 | } |
| 5464 | |
| 5465 | if (!replaced) { |
| 5466 | /* no match */ |
| 5467 | set_remove_node(set, i); |
| 5468 | } |
| 5469 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5470 | |
| 5471 | return LY_SUCCESS; |
| 5472 | } |
| 5473 | |
| 5474 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5475 | * @brief Move context @p set to a node using hashes. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY). |
| 5476 | * Context position aware. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5477 | * |
| 5478 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5479 | * @param[in] scnode Matching node schema. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5480 | * @param[in] predicates If @p scnode is ::LYS_LIST or ::LYS_LEAFLIST, the predicates specifying a single instance. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5481 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 5482 | */ |
| 5483 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5484 | moveto_node_hash(struct lyxp_set *set, const struct lysc_node *scnode, const struct ly_path_predicate *predicates) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5485 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5486 | LY_ERR ret = LY_SUCCESS; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5487 | uint32_t i; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5488 | const struct lyd_node *siblings; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5489 | struct lyd_node *sub, *inst = NULL; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5490 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5491 | assert(scnode && (!(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) || predicates)); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5492 | |
| 5493 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5494 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5495 | } |
| 5496 | |
| 5497 | if (set->type != LYXP_SET_NODE_SET) { |
| 5498 | 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] | 5499 | ret = LY_EVALID; |
| 5500 | goto cleanup; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5501 | } |
| 5502 | |
| 5503 | /* context check for all the nodes since we have the schema node */ |
| 5504 | if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) { |
| 5505 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5506 | goto cleanup; |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 5507 | } else if (set->context_op && (scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5508 | && (scnode != set->context_op)) { |
| 5509 | lyxp_set_free_content(set); |
| 5510 | goto cleanup; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5511 | } |
| 5512 | |
| 5513 | /* create specific data instance if needed */ |
| 5514 | if (scnode->nodetype == LYS_LIST) { |
| 5515 | LY_CHECK_GOTO(ret = lyd_create_list(scnode, predicates, &inst), cleanup); |
| 5516 | } else if (scnode->nodetype == LYS_LEAFLIST) { |
| 5517 | 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] | 5518 | } |
| 5519 | |
| 5520 | for (i = 0; i < set->used; ) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 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 */ |
Michal Vasko | 5bfd4be | 2020-06-23 13:26:19 +0200 | [diff] [blame] | 5530 | siblings = lyd_node_children(set->val.nodes[i].node, 0); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 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 */ |
Michal Vasko | cb1b7c0 | 2020-07-03 13:38:12 +0200 | [diff] [blame] | 5548 | set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5549 | ++i; |
Michal Vasko | cb1b7c0 | 2020-07-03 13:38:12 +0200 | [diff] [blame] | 5550 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5551 | /* no match */ |
| 5552 | set_remove_node(set, i); |
| 5553 | } |
| 5554 | } |
| 5555 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 5556 | cleanup: |
| 5557 | lyd_free_tree(inst); |
| 5558 | return ret; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5559 | } |
| 5560 | |
| 5561 | /** |
| 5562 | * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY). |
| 5563 | * |
| 5564 | * @param[in,out] set Set to use. |
| 5565 | * @param[in] mod Matching node module, NULL for any. |
| 5566 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5567 | * @param[in] options XPath options. |
| 5568 | * @return LY_ERR |
| 5569 | */ |
| 5570 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5571 | moveto_scnode(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5572 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5573 | uint8_t temp_ctx = 0; |
| 5574 | uint32_t getnext_opts; |
| 5575 | uint32_t orig_used, i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5576 | uint32_t mod_idx; |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 5577 | const struct lysc_node *iter, *start_parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5578 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5579 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5580 | return LY_SUCCESS; |
| 5581 | } |
| 5582 | |
| 5583 | if (set->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 5584 | 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] | 5585 | return LY_EVALID; |
| 5586 | } |
| 5587 | |
Michal Vasko | cafad9d | 2019-11-07 15:20:03 +0100 | [diff] [blame] | 5588 | /* getnext opts */ |
| 5589 | getnext_opts = LYS_GETNEXT_NOSTATECHECK; |
| 5590 | if (options & LYXP_SCNODE_OUTPUT) { |
| 5591 | getnext_opts |= LYS_GETNEXT_OUTPUT; |
| 5592 | } |
| 5593 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5594 | orig_used = set->used; |
| 5595 | for (i = 0; i < orig_used; ++i) { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame^] | 5596 | uint32_t idx; |
| 5597 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5598 | if (set->val.scnodes[i].in_ctx != 1) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5599 | if (set->val.scnodes[i].in_ctx != -2) { |
| 5600 | continue; |
| 5601 | } |
| 5602 | |
| 5603 | /* remember context node */ |
| 5604 | set->val.scnodes[i].in_ctx = -1; |
Michal Vasko | ec4df48 | 2019-12-16 10:02:18 +0100 | [diff] [blame] | 5605 | } else { |
| 5606 | set->val.scnodes[i].in_ctx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5607 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5608 | |
| 5609 | start_parent = set->val.scnodes[i].scnode; |
| 5610 | |
| 5611 | 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] | 5612 | /* 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] | 5613 | * so use it directly (root node itself is useless in this case) */ |
| 5614 | mod_idx = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5615 | 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] | 5616 | iter = NULL; |
Michal Vasko | 509de4d | 2019-12-10 14:51:30 +0100 | [diff] [blame] | 5617 | /* module may not be implemented */ |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 5618 | while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5619 | if (!moveto_scnode_check(iter, set->root_type, set->context_op, ncname, mod)) { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame^] | 5620 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx)); |
| 5621 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5622 | /* we need to prevent these nodes from being considered in this moveto */ |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame^] | 5623 | if ((idx < orig_used) && (idx > i)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5624 | set->val.scnodes[idx].in_ctx = 2; |
| 5625 | temp_ctx = 1; |
| 5626 | } |
| 5627 | } |
| 5628 | } |
| 5629 | |
| 5630 | if (!mod_idx) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5631 | /* mod was specified, we are not going through the whole context */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5632 | break; |
| 5633 | } |
| 5634 | /* next iteration */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5635 | mod = NULL; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5636 | } |
| 5637 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 5638 | } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) { |
| 5639 | iter = NULL; |
| 5640 | while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5641 | if (!moveto_scnode_check(iter, set->root_type, set->context_op, ncname, (mod ? mod : set->local_mod))) { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame^] | 5642 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx)); |
| 5643 | |
| 5644 | if ((idx < orig_used) && (idx > i)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5645 | set->val.scnodes[idx].in_ctx = 2; |
| 5646 | temp_ctx = 1; |
| 5647 | } |
| 5648 | } |
| 5649 | } |
| 5650 | } |
| 5651 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5652 | |
| 5653 | /* correct temporary in_ctx values */ |
| 5654 | if (temp_ctx) { |
| 5655 | for (i = 0; i < orig_used; ++i) { |
| 5656 | if (set->val.scnodes[i].in_ctx == 2) { |
| 5657 | set->val.scnodes[i].in_ctx = 1; |
| 5658 | } |
| 5659 | } |
| 5660 | } |
| 5661 | |
| 5662 | return LY_SUCCESS; |
| 5663 | } |
| 5664 | |
| 5665 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5666 | * @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] | 5667 | * Context position aware. |
| 5668 | * |
| 5669 | * @param[in] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5670 | * @param[in] mod Matching node module, NULL for any. |
| 5671 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5672 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 5673 | */ |
| 5674 | static LY_ERR |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5675 | 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] | 5676 | { |
| 5677 | uint32_t i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5678 | const struct lyd_node *next, *elem, *start; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5679 | struct lyxp_set ret_set; |
| 5680 | LY_ERR rc; |
| 5681 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5682 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5683 | return LY_SUCCESS; |
| 5684 | } |
| 5685 | |
| 5686 | if (set->type != LYXP_SET_NODE_SET) { |
| 5687 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 5688 | return LY_EVALID; |
| 5689 | } |
| 5690 | |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5691 | /* 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] | 5692 | rc = moveto_node(set, NULL, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5693 | LY_CHECK_RET(rc); |
| 5694 | |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5695 | /* 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] | 5696 | set_init(&ret_set, set); |
| 5697 | for (i = 0; i < set->used; ++i) { |
| 5698 | |
| 5699 | /* TREE DFS */ |
| 5700 | start = set->val.nodes[i].node; |
| 5701 | for (elem = next = start; elem; elem = next) { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5702 | rc = moveto_node_check(elem, set->root_type, set->context_op, ncname, mod); |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5703 | if (!rc) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5704 | /* add matching node into result set */ |
| 5705 | set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used); |
| 5706 | if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) { |
| 5707 | /* the node is a duplicate, we'll process it later in the set */ |
| 5708 | goto skip_children; |
| 5709 | } |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5710 | } else if (rc == LY_EINCOMPLETE) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5711 | return rc; |
| 5712 | } else if (rc == LY_EINVAL) { |
| 5713 | goto skip_children; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5714 | } |
| 5715 | |
| 5716 | /* TREE DFS NEXT ELEM */ |
| 5717 | /* select element for the next run - children first */ |
Michal Vasko | 5bfd4be | 2020-06-23 13:26:19 +0200 | [diff] [blame] | 5718 | next = lyd_node_children(elem, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5719 | if (!next) { |
| 5720 | skip_children: |
| 5721 | /* no children, so try siblings, but only if it's not the start, |
| 5722 | * that is considered to be the root and it's siblings are not traversed */ |
| 5723 | if (elem != start) { |
| 5724 | next = elem->next; |
| 5725 | } else { |
| 5726 | break; |
| 5727 | } |
| 5728 | } |
| 5729 | while (!next) { |
| 5730 | /* no siblings, go back through the parents */ |
| 5731 | if ((struct lyd_node *)elem->parent == start) { |
| 5732 | /* we are done, no next element to process */ |
| 5733 | break; |
| 5734 | } |
| 5735 | /* parent is already processed, go to its sibling */ |
| 5736 | elem = (struct lyd_node *)elem->parent; |
| 5737 | next = elem->next; |
| 5738 | } |
| 5739 | } |
| 5740 | } |
| 5741 | |
| 5742 | /* make the temporary set the current one */ |
| 5743 | ret_set.ctx_pos = set->ctx_pos; |
| 5744 | ret_set.ctx_size = set->ctx_size; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5745 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5746 | memcpy(set, &ret_set, sizeof *set); |
| 5747 | |
| 5748 | return LY_SUCCESS; |
| 5749 | } |
| 5750 | |
| 5751 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5752 | * @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] | 5753 | * |
| 5754 | * @param[in] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5755 | * @param[in] mod Matching node module, NULL for any. |
| 5756 | * @param[in] ncname Matching node name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5757 | * @param[in] options XPath options. |
| 5758 | * @return LY_ERR |
| 5759 | */ |
| 5760 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5761 | moveto_scnode_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5762 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5763 | uint32_t i, orig_used; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5764 | const struct lysc_node *next, *elem, *start; |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5765 | LY_ERR rc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5766 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5767 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5768 | return LY_SUCCESS; |
| 5769 | } |
| 5770 | |
| 5771 | if (set->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 5772 | 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] | 5773 | return LY_EVALID; |
| 5774 | } |
| 5775 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5776 | orig_used = set->used; |
| 5777 | for (i = 0; i < orig_used; ++i) { |
| 5778 | if (set->val.scnodes[i].in_ctx != 1) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5779 | if (set->val.scnodes[i].in_ctx != -2) { |
| 5780 | continue; |
| 5781 | } |
| 5782 | |
| 5783 | /* remember context node */ |
| 5784 | set->val.scnodes[i].in_ctx = -1; |
Michal Vasko | ec4df48 | 2019-12-16 10:02:18 +0100 | [diff] [blame] | 5785 | } else { |
| 5786 | set->val.scnodes[i].in_ctx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5787 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5788 | |
| 5789 | /* TREE DFS */ |
| 5790 | start = set->val.scnodes[i].scnode; |
| 5791 | for (elem = next = start; elem; elem = next) { |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5792 | if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) { |
| 5793 | /* schema-only nodes, skip root */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5794 | goto next_iter; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5795 | } |
| 5796 | |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 5797 | rc = moveto_scnode_check(elem, set->root_type, set->context_op, ncname, mod); |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5798 | if (!rc) { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame^] | 5799 | uint32_t idx; |
| 5800 | |
| 5801 | if (lyxp_set_scnode_contains(set, elem, LYXP_NODE_ELEM, i, &idx)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5802 | set->val.scnodes[idx].in_ctx = 1; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5803 | if ((uint32_t)idx > i) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5804 | /* we will process it later in the set */ |
| 5805 | goto skip_children; |
| 5806 | } |
| 5807 | } else { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame^] | 5808 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, elem, LYXP_NODE_ELEM, NULL)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5809 | } |
Michal Vasko | 6346ece | 2019-09-24 13:12:53 +0200 | [diff] [blame] | 5810 | } else if (rc == LY_EINVAL) { |
| 5811 | goto skip_children; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5812 | } |
| 5813 | |
| 5814 | next_iter: |
| 5815 | /* TREE DFS NEXT ELEM */ |
| 5816 | /* select element for the next run - children first */ |
| 5817 | 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] | 5818 | if (!next) { |
| 5819 | skip_children: |
| 5820 | /* no children, so try siblings, but only if it's not the start, |
| 5821 | * that is considered to be the root and it's siblings are not traversed */ |
| 5822 | if (elem != start) { |
| 5823 | next = elem->next; |
| 5824 | } else { |
| 5825 | break; |
| 5826 | } |
| 5827 | } |
| 5828 | while (!next) { |
| 5829 | /* no siblings, go back through the parents */ |
| 5830 | if (elem->parent == start) { |
| 5831 | /* we are done, no next element to process */ |
| 5832 | break; |
| 5833 | } |
| 5834 | /* parent is already processed, go to its sibling */ |
| 5835 | elem = elem->parent; |
| 5836 | next = elem->next; |
| 5837 | } |
| 5838 | } |
| 5839 | } |
| 5840 | |
| 5841 | return LY_SUCCESS; |
| 5842 | } |
| 5843 | |
| 5844 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5845 | * @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] | 5846 | * Indirectly context position aware. |
| 5847 | * |
| 5848 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5849 | * @param[in] mod Matching metadata module, NULL for any. |
| 5850 | * @param[in] ncname Matching metadata name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5851 | * @return LY_ERR |
| 5852 | */ |
| 5853 | static LY_ERR |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5854 | 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] | 5855 | { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5856 | struct lyd_meta *sub; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5857 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5858 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5859 | return LY_SUCCESS; |
| 5860 | } |
| 5861 | |
| 5862 | if (set->type != LYXP_SET_NODE_SET) { |
| 5863 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 5864 | return LY_EVALID; |
| 5865 | } |
| 5866 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5867 | for (uint32_t i = 0; i < set->used; ) { |
| 5868 | uint8_t replaced = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5869 | |
| 5870 | /* only attributes of an elem (not dummy) can be in the result, skip all the rest; |
| 5871 | * our attributes are always qualified */ |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 5872 | if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5873 | for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5874 | |
| 5875 | /* check "namespace" */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5876 | if (mod && (sub->annotation->module != mod)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5877 | continue; |
| 5878 | } |
| 5879 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5880 | if (!ncname || (sub->name == ncname)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5881 | /* match */ |
| 5882 | if (!replaced) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5883 | set->val.meta[i].meta = sub; |
| 5884 | set->val.meta[i].type = LYXP_NODE_META; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5885 | /* pos does not change */ |
| 5886 | replaced = 1; |
| 5887 | } else { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5888 | 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] | 5889 | } |
| 5890 | ++i; |
| 5891 | } |
| 5892 | } |
| 5893 | } |
| 5894 | |
| 5895 | if (!replaced) { |
| 5896 | /* no match */ |
| 5897 | set_remove_node(set, i); |
| 5898 | } |
| 5899 | } |
| 5900 | |
| 5901 | return LY_SUCCESS; |
| 5902 | } |
| 5903 | |
| 5904 | /** |
| 5905 | * @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] | 5906 | * Result is LYXP_SET_NODE_SET. Context position aware. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5907 | * |
| 5908 | * @param[in,out] set1 Set to use for the result. |
| 5909 | * @param[in] set2 Set that is copied to @p set1. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5910 | * @return LY_ERR |
| 5911 | */ |
| 5912 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5913 | moveto_union(struct lyxp_set *set1, struct lyxp_set *set2) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5914 | { |
| 5915 | LY_ERR rc; |
| 5916 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5917 | 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] | 5918 | LOGVAL(set1->ctx, LY_VLOG_LYD, set1->ctx_node, LY_VCODE_XP_INOP_2, "union", print_set_type(set1), print_set_type(set2)); |
| 5919 | return LY_EVALID; |
| 5920 | } |
| 5921 | |
| 5922 | /* set2 is empty or both set1 and set2 */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5923 | if (!set2->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5924 | return LY_SUCCESS; |
| 5925 | } |
| 5926 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5927 | if (!set1->used) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5928 | memcpy(set1, set2, sizeof *set1); |
| 5929 | /* dynamic memory belongs to set1 now, do not free */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5930 | memset(set2, 0, sizeof *set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5931 | return LY_SUCCESS; |
| 5932 | } |
| 5933 | |
| 5934 | /* we assume sets are sorted */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5935 | assert(!set_sort(set1) && !set_sort(set2)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5936 | |
| 5937 | /* sort, remove duplicates */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5938 | rc = set_sorted_merge(set1, set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5939 | LY_CHECK_RET(rc); |
| 5940 | |
| 5941 | /* final set must be sorted */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5942 | assert(!set_sort(set1)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5943 | |
| 5944 | return LY_SUCCESS; |
| 5945 | } |
| 5946 | |
| 5947 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 5948 | * @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] | 5949 | * Context position aware. |
| 5950 | * |
| 5951 | * @param[in,out] set Set to use. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5952 | * @param[in] mod Matching metadata module, NULL for any. |
| 5953 | * @param[in] ncname Matching metadata name in the dictionary, NULL for any. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5954 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 5955 | */ |
| 5956 | static int |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5957 | 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] | 5958 | { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5959 | struct lyd_meta *sub; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5960 | struct lyxp_set *set_all_desc = NULL; |
| 5961 | LY_ERR rc; |
| 5962 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5963 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5964 | return LY_SUCCESS; |
| 5965 | } |
| 5966 | |
| 5967 | if (set->type != LYXP_SET_NODE_SET) { |
| 5968 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 5969 | return LY_EVALID; |
| 5970 | } |
| 5971 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5972 | /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory, |
| 5973 | * but it likely won't be used much, so it's a waste of time */ |
| 5974 | /* copy the context */ |
| 5975 | set_all_desc = set_copy(set); |
| 5976 | /* get all descendant nodes (the original context nodes are removed) */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5977 | rc = moveto_node_alldesc(set_all_desc, NULL, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5978 | if (rc != LY_SUCCESS) { |
| 5979 | lyxp_set_free(set_all_desc); |
| 5980 | return rc; |
| 5981 | } |
| 5982 | /* prepend the original context nodes */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 5983 | rc = moveto_union(set, set_all_desc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5984 | if (rc != LY_SUCCESS) { |
| 5985 | lyxp_set_free(set_all_desc); |
| 5986 | return rc; |
| 5987 | } |
| 5988 | lyxp_set_free(set_all_desc); |
| 5989 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 5990 | for (uint32_t i = 0; i < set->used; ) { |
| 5991 | uint8_t replaced = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5992 | |
| 5993 | /* only attributes of an elem can be in the result, skip all the rest, |
| 5994 | * we have all attributes qualified in lyd tree */ |
| 5995 | if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 5996 | for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5997 | /* check "namespace" */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 5998 | if (mod && (sub->annotation->module != mod)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 5999 | continue; |
| 6000 | } |
| 6001 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6002 | if (!ncname || (sub->name == ncname)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6003 | /* match */ |
| 6004 | if (!replaced) { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6005 | set->val.meta[i].meta = sub; |
| 6006 | set->val.meta[i].type = LYXP_NODE_META; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6007 | /* pos does not change */ |
| 6008 | replaced = 1; |
| 6009 | } else { |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6010 | 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] | 6011 | } |
| 6012 | ++i; |
| 6013 | } |
| 6014 | } |
| 6015 | } |
| 6016 | |
| 6017 | if (!replaced) { |
| 6018 | /* no match */ |
| 6019 | set_remove_node(set, i); |
| 6020 | } |
| 6021 | } |
| 6022 | |
| 6023 | return LY_SUCCESS; |
| 6024 | } |
| 6025 | |
| 6026 | /** |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6027 | * @brief Move context @p set to self and al chilren, recursively. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET. |
| 6028 | * Context position aware. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6029 | * |
| 6030 | * @param[in] parent Current parent. |
| 6031 | * @param[in] parent_pos Position of @p parent. |
| 6032 | * @param[in] parent_type Node type of @p parent. |
| 6033 | * @param[in,out] to_set Set to use. |
| 6034 | * @param[in] dup_check_set Set for checking duplicities. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6035 | * @param[in] options XPath options. |
| 6036 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6037 | */ |
| 6038 | static LY_ERR |
| 6039 | moveto_self_add_children_r(const struct lyd_node *parent, uint32_t parent_pos, enum lyxp_node_type parent_type, |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6040 | struct lyxp_set *to_set, const struct lyxp_set *dup_check_set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6041 | { |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6042 | const struct lyd_node *iter, *first; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6043 | LY_ERR rc; |
| 6044 | |
| 6045 | switch (parent_type) { |
| 6046 | case LYXP_NODE_ROOT: |
| 6047 | case LYXP_NODE_ROOT_CONFIG: |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6048 | assert(!parent); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6049 | |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6050 | /* add all top-level nodes as elements */ |
| 6051 | first = to_set->tree; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6052 | break; |
| 6053 | case LYXP_NODE_ELEM: |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6054 | /* add just the text node of this term element node */ |
| 6055 | if (parent->schema->nodetype & (LYD_NODE_TERM | LYD_NODE_ANY)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6056 | if (!set_dup_node_check(dup_check_set, parent, LYXP_NODE_TEXT, -1)) { |
| 6057 | set_insert_node(to_set, parent, parent_pos, LYXP_NODE_TEXT, to_set->used); |
| 6058 | } |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6059 | return LY_SUCCESS; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6060 | } |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6061 | |
| 6062 | /* add all the children of this node */ |
Michal Vasko | 5bfd4be | 2020-06-23 13:26:19 +0200 | [diff] [blame] | 6063 | first = lyd_node_children(parent, 0); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6064 | break; |
| 6065 | default: |
| 6066 | LOGINT_RET(parent->schema->module->ctx); |
| 6067 | } |
| 6068 | |
Michal Vasko | 61ac2f6 | 2020-05-25 12:39:51 +0200 | [diff] [blame] | 6069 | /* add all top-level nodes as elements */ |
| 6070 | LY_LIST_FOR(first, iter) { |
| 6071 | /* context check */ |
| 6072 | if ((parent_type == LYXP_NODE_ROOT_CONFIG) && (iter->schema->flags & LYS_CONFIG_R)) { |
| 6073 | continue; |
| 6074 | } |
| 6075 | |
| 6076 | /* when check */ |
| 6077 | if (moveto_when_check(iter)) { |
| 6078 | return LY_EINCOMPLETE; |
| 6079 | } |
| 6080 | |
| 6081 | if (!set_dup_node_check(dup_check_set, iter, LYXP_NODE_ELEM, -1)) { |
| 6082 | set_insert_node(to_set, iter, 0, LYXP_NODE_ELEM, to_set->used); |
| 6083 | |
| 6084 | /* also add all the children of this node, recursively */ |
| 6085 | rc = moveto_self_add_children_r(iter, 0, LYXP_NODE_ELEM, to_set, dup_check_set, options); |
| 6086 | LY_CHECK_RET(rc); |
| 6087 | } |
| 6088 | } |
| 6089 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6090 | return LY_SUCCESS; |
| 6091 | } |
| 6092 | |
| 6093 | /** |
| 6094 | * @brief Move context @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET |
| 6095 | * (or LYXP_SET_EMPTY). Context position aware. |
| 6096 | * |
| 6097 | * @param[in,out] set Set to use. |
| 6098 | * @param[in] all_desc Whether to go to all descendants ('//') or not ('/'). |
| 6099 | * @param[in] options XPath options. |
| 6100 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6101 | */ |
| 6102 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6103 | moveto_self(struct lyxp_set *set, uint8_t all_desc, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6104 | { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6105 | struct lyxp_set ret_set; |
| 6106 | LY_ERR rc; |
| 6107 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6108 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6109 | return LY_SUCCESS; |
| 6110 | } |
| 6111 | |
| 6112 | if (set->type != LYXP_SET_NODE_SET) { |
| 6113 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 6114 | return LY_EVALID; |
| 6115 | } |
| 6116 | |
| 6117 | /* nothing to do */ |
| 6118 | if (!all_desc) { |
| 6119 | return LY_SUCCESS; |
| 6120 | } |
| 6121 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6122 | /* add all the children, they get added recursively */ |
| 6123 | set_init(&ret_set, set); |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6124 | for (uint32_t i = 0; i < set->used; ++i) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6125 | /* copy the current node to tmp */ |
| 6126 | set_insert_node(&ret_set, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, ret_set.used); |
| 6127 | |
| 6128 | /* do not touch attributes and text nodes */ |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6129 | 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] | 6130 | continue; |
| 6131 | } |
| 6132 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6133 | /* add all the children */ |
| 6134 | 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] | 6135 | set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6136 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6137 | lyxp_set_free_content(&ret_set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6138 | return rc; |
| 6139 | } |
| 6140 | } |
| 6141 | |
| 6142 | /* use the temporary set as the current one */ |
| 6143 | ret_set.ctx_pos = set->ctx_pos; |
| 6144 | ret_set.ctx_size = set->ctx_size; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6145 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6146 | memcpy(set, &ret_set, sizeof *set); |
| 6147 | |
| 6148 | return LY_SUCCESS; |
| 6149 | } |
| 6150 | |
| 6151 | /** |
| 6152 | * @brief Move context schema @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_SCNODE_SET |
| 6153 | * (or LYXP_SET_EMPTY). |
| 6154 | * |
| 6155 | * @param[in,out] set Set to use. |
| 6156 | * @param[in] all_desc Whether to go to all descendants ('//') or not ('/'). |
| 6157 | * @param[in] options XPath options. |
| 6158 | * @return LY_ERR |
| 6159 | */ |
| 6160 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6161 | moveto_scnode_self(struct lyxp_set *set, uint8_t all_desc, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6162 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6163 | uint32_t getnext_opts; |
| 6164 | uint32_t mod_idx; |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6165 | const struct lysc_node *iter, *start_parent; |
| 6166 | const struct lys_module *mod; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6167 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6168 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6169 | return LY_SUCCESS; |
| 6170 | } |
| 6171 | |
| 6172 | if (set->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 6173 | 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] | 6174 | return LY_EVALID; |
| 6175 | } |
| 6176 | |
| 6177 | /* nothing to do */ |
| 6178 | if (!all_desc) { |
| 6179 | return LY_SUCCESS; |
| 6180 | } |
| 6181 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6182 | /* getnext opts */ |
| 6183 | getnext_opts = LYS_GETNEXT_NOSTATECHECK; |
| 6184 | if (options & LYXP_SCNODE_OUTPUT) { |
| 6185 | getnext_opts |= LYS_GETNEXT_OUTPUT; |
| 6186 | } |
| 6187 | |
| 6188 | /* add all the children, recursively as they are being added into the same set */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6189 | for (uint32_t i = 0; i < set->used; ++i) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6190 | if (set->val.scnodes[i].in_ctx != 1) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6191 | if (set->val.scnodes[i].in_ctx != -2) { |
| 6192 | continue; |
| 6193 | } |
| 6194 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6195 | /* remember context node */ |
| 6196 | set->val.scnodes[i].in_ctx = -1; |
| 6197 | } else { |
| 6198 | set->val.scnodes[i].in_ctx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6199 | } |
| 6200 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6201 | start_parent = set->val.scnodes[i].scnode; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6202 | |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6203 | if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) { |
| 6204 | /* it can actually be in any module, it's all <running> */ |
| 6205 | mod_idx = 0; |
| 6206 | while ((mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) { |
| 6207 | iter = NULL; |
| 6208 | /* module may not be implemented */ |
| 6209 | while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) { |
| 6210 | /* context check */ |
| 6211 | if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) { |
| 6212 | continue; |
| 6213 | } |
| 6214 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame^] | 6215 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, NULL)); |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6216 | /* throw away the insert index, we want to consider that node again, recursively */ |
| 6217 | } |
| 6218 | } |
| 6219 | |
| 6220 | } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) { |
| 6221 | iter = NULL; |
| 6222 | while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6223 | /* context check */ |
Michal Vasko | 519fd60 | 2020-05-26 12:17:39 +0200 | [diff] [blame] | 6224 | 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] | 6225 | continue; |
| 6226 | } |
| 6227 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame^] | 6228 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, NULL)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6229 | } |
| 6230 | } |
| 6231 | } |
| 6232 | |
| 6233 | return LY_SUCCESS; |
| 6234 | } |
| 6235 | |
| 6236 | /** |
| 6237 | * @brief Move context @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_NODE_SET |
| 6238 | * (or LYXP_SET_EMPTY). Context position aware. |
| 6239 | * |
| 6240 | * @param[in] set Set to use. |
| 6241 | * @param[in] all_desc Whether to go to all descendants ('//') or not ('/'). |
| 6242 | * @param[in] options XPath options. |
| 6243 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6244 | */ |
| 6245 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6246 | moveto_parent(struct lyxp_set *set, uint8_t all_desc, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6247 | { |
| 6248 | LY_ERR rc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6249 | struct lyd_node *node, *new_node; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6250 | enum lyxp_node_type new_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6251 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6252 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6253 | return LY_SUCCESS; |
| 6254 | } |
| 6255 | |
| 6256 | if (set->type != LYXP_SET_NODE_SET) { |
| 6257 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set)); |
| 6258 | return LY_EVALID; |
| 6259 | } |
| 6260 | |
| 6261 | if (all_desc) { |
| 6262 | /* <path>//.. == <path>//./.. */ |
| 6263 | rc = moveto_self(set, 1, options); |
| 6264 | LY_CHECK_RET(rc); |
| 6265 | } |
| 6266 | |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6267 | for (uint32_t i = 0; i < set->used; ++i) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6268 | node = set->val.nodes[i].node; |
| 6269 | |
| 6270 | if (set->val.nodes[i].type == LYXP_NODE_ELEM) { |
| 6271 | new_node = (struct lyd_node *)node->parent; |
| 6272 | } else if (set->val.nodes[i].type == LYXP_NODE_TEXT) { |
| 6273 | new_node = node; |
Michal Vasko | 9f96a05 | 2020-03-10 09:41:45 +0100 | [diff] [blame] | 6274 | } else if (set->val.nodes[i].type == LYXP_NODE_META) { |
| 6275 | new_node = set->val.meta[i].meta->parent; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6276 | if (!new_node) { |
| 6277 | LOGINT_RET(set->ctx); |
| 6278 | } |
| 6279 | } else { |
| 6280 | /* root does not have a parent */ |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6281 | set_remove_node_none(set, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6282 | continue; |
| 6283 | } |
| 6284 | |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 6285 | /* when check */ |
| 6286 | if (moveto_when_check(new_node)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6287 | return LY_EINCOMPLETE; |
Michal Vasko | a142454 | 2019-11-14 16:08:52 +0100 | [diff] [blame] | 6288 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6289 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6290 | if (!new_node) { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 6291 | /* node already there can also be the root */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6292 | new_type = set->root_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6293 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6294 | } else { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 6295 | /* node has a standard parent (it can equal the root, it's not the root yet since they are fake) */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6296 | new_type = LYXP_NODE_ELEM; |
| 6297 | } |
| 6298 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6299 | if (set_dup_node_check(set, new_node, new_type, -1)) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6300 | set_remove_node_none(set, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6301 | } else { |
| 6302 | set_replace_node(set, new_node, 0, new_type, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6303 | } |
| 6304 | } |
| 6305 | |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6306 | set_remove_nodes_none(set); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6307 | assert(!set_sort(set) && !set_sorted_dup_node_clean(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6308 | |
| 6309 | return LY_SUCCESS; |
| 6310 | } |
| 6311 | |
| 6312 | /** |
| 6313 | * @brief Move context schema @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_SCNODE_SET |
| 6314 | * (or LYXP_SET_EMPTY). |
| 6315 | * |
| 6316 | * @param[in] set Set to use. |
| 6317 | * @param[in] all_desc Whether to go to all descendants ('//') or not ('/'). |
| 6318 | * @param[in] options XPath options. |
| 6319 | * @return LY_ERR |
| 6320 | */ |
| 6321 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6322 | moveto_scnode_parent(struct lyxp_set *set, uint8_t all_desc, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6323 | { |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame^] | 6324 | uint32_t i, orig_used, idx; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6325 | uint8_t temp_ctx = 0; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6326 | const struct lysc_node *node, *new_node; |
| 6327 | enum lyxp_node_type new_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6328 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6329 | if (!set) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6330 | return LY_SUCCESS; |
| 6331 | } |
| 6332 | |
| 6333 | if (set->type != LYXP_SET_SCNODE_SET) { |
Michal Vasko | f6e5188 | 2019-12-16 09:59:45 +0100 | [diff] [blame] | 6334 | 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] | 6335 | return LY_EVALID; |
| 6336 | } |
| 6337 | |
| 6338 | if (all_desc) { |
| 6339 | /* <path>//.. == <path>//./.. */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6340 | LY_CHECK_RET(moveto_scnode_self(set, 1, options)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6341 | } |
| 6342 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6343 | orig_used = set->used; |
| 6344 | for (i = 0; i < orig_used; ++i) { |
| 6345 | if (set->val.scnodes[i].in_ctx != 1) { |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6346 | if (set->val.scnodes[i].in_ctx != -2) { |
| 6347 | continue; |
| 6348 | } |
| 6349 | |
| 6350 | /* remember context node */ |
| 6351 | set->val.scnodes[i].in_ctx = -1; |
Michal Vasko | ec4df48 | 2019-12-16 10:02:18 +0100 | [diff] [blame] | 6352 | } else { |
| 6353 | set->val.scnodes[i].in_ctx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6354 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6355 | |
| 6356 | node = set->val.scnodes[i].scnode; |
| 6357 | |
| 6358 | if (set->val.scnodes[i].type == LYXP_NODE_ELEM) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6359 | new_node = lysc_data_parent(node); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6360 | } else { |
| 6361 | /* root does not have a parent */ |
| 6362 | continue; |
| 6363 | } |
| 6364 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6365 | if (!new_node) { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 6366 | /* node has no parent */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6367 | new_type = set->root_type; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6368 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6369 | } else { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 6370 | /* node has a standard parent (it can equal the root, it's not the root yet since they are fake) */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6371 | new_type = LYXP_NODE_ELEM; |
| 6372 | } |
| 6373 | |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame^] | 6374 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, new_node, new_type, &idx)); |
| 6375 | if ((idx < orig_used) && (idx > i)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6376 | set->val.scnodes[idx].in_ctx = 2; |
| 6377 | temp_ctx = 1; |
| 6378 | } |
| 6379 | } |
| 6380 | |
| 6381 | if (temp_ctx) { |
| 6382 | for (i = 0; i < orig_used; ++i) { |
| 6383 | if (set->val.scnodes[i].in_ctx == 2) { |
| 6384 | set->val.scnodes[i].in_ctx = 1; |
| 6385 | } |
| 6386 | } |
| 6387 | } |
| 6388 | |
| 6389 | return LY_SUCCESS; |
| 6390 | } |
| 6391 | |
| 6392 | /** |
| 6393 | * @brief Move context @p set to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'. |
| 6394 | * Result is LYXP_SET_BOOLEAN. Indirectly context position aware. |
| 6395 | * |
| 6396 | * @param[in,out] set1 Set to use for the result. |
| 6397 | * @param[in] set2 Set acting as the second operand for @p op. |
| 6398 | * @param[in] op Comparison operator to process. |
| 6399 | * @param[in] options XPath options. |
| 6400 | * @return LY_ERR |
| 6401 | */ |
| 6402 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6403 | moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6404 | { |
| 6405 | /* |
| 6406 | * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING |
| 6407 | * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING) |
| 6408 | * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 6409 | * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 6410 | * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING |
| 6411 | * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6412 | * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 6413 | * |
| 6414 | * '=' or '!=' |
| 6415 | * BOOLEAN + BOOLEAN |
| 6416 | * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 6417 | * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN |
| 6418 | * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 6419 | * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN) |
| 6420 | * NUMBER + NUMBER |
| 6421 | * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6422 | * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 6423 | * STRING + STRING |
| 6424 | * |
| 6425 | * '<=', '<', '>=', '>' |
| 6426 | * NUMBER + NUMBER |
| 6427 | * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 6428 | * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 6429 | * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 6430 | * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6431 | * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER |
| 6432 | * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER) |
| 6433 | * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6434 | * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER |
| 6435 | */ |
| 6436 | struct lyxp_set iter1, iter2; |
| 6437 | int result; |
| 6438 | int64_t i; |
| 6439 | LY_ERR rc; |
| 6440 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6441 | memset(&iter1, 0, sizeof iter1); |
| 6442 | memset(&iter2, 0, sizeof iter2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6443 | |
| 6444 | /* iterative evaluation with node-sets */ |
| 6445 | if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) { |
| 6446 | if (set1->type == LYXP_SET_NODE_SET) { |
| 6447 | for (i = 0; i < set1->used; ++i) { |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6448 | /* cast set1 */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6449 | switch (set2->type) { |
| 6450 | case LYXP_SET_NUMBER: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6451 | rc = set_comp_cast(&iter1, set1, LYXP_SET_NUMBER, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6452 | break; |
| 6453 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6454 | rc = set_comp_cast(&iter1, set1, LYXP_SET_BOOLEAN, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6455 | break; |
| 6456 | default: |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6457 | rc = set_comp_cast(&iter1, set1, LYXP_SET_STRING, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6458 | break; |
| 6459 | } |
| 6460 | LY_CHECK_RET(rc); |
| 6461 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6462 | /* canonize set2 */ |
| 6463 | LY_CHECK_ERR_RET(rc = set_comp_canonize(&iter2, set2, &set1->val.nodes[i]), lyxp_set_free_content(&iter1), rc); |
| 6464 | |
| 6465 | /* compare recursively */ |
| 6466 | rc = moveto_op_comp(&iter1, &iter2, op, options); |
| 6467 | lyxp_set_free_content(&iter2); |
| 6468 | LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6469 | |
| 6470 | /* lazy evaluation until true */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6471 | if (iter1.val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6472 | set_fill_boolean(set1, 1); |
| 6473 | return LY_SUCCESS; |
| 6474 | } |
| 6475 | } |
| 6476 | } else { |
| 6477 | for (i = 0; i < set2->used; ++i) { |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6478 | /* set set2 */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6479 | switch (set1->type) { |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6480 | case LYXP_SET_NUMBER: |
| 6481 | rc = set_comp_cast(&iter2, set2, LYXP_SET_NUMBER, i); |
| 6482 | break; |
| 6483 | case LYXP_SET_BOOLEAN: |
| 6484 | rc = set_comp_cast(&iter2, set2, LYXP_SET_BOOLEAN, i); |
| 6485 | break; |
| 6486 | default: |
| 6487 | rc = set_comp_cast(&iter2, set2, LYXP_SET_STRING, i); |
| 6488 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6489 | } |
| 6490 | LY_CHECK_RET(rc); |
| 6491 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6492 | /* canonize set1 */ |
| 6493 | LY_CHECK_ERR_RET(rc = set_comp_canonize(&iter1, set1, &set2->val.nodes[i]), lyxp_set_free_content(&iter2), rc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6494 | |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6495 | /* compare recursively */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6496 | rc = moveto_op_comp(&iter1, &iter2, op, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6497 | lyxp_set_free_content(&iter2); |
Michal Vasko | 4c7763f | 2020-07-27 17:40:37 +0200 | [diff] [blame] | 6498 | LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6499 | |
| 6500 | /* lazy evaluation until true */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6501 | if (iter1.val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6502 | set_fill_boolean(set1, 1); |
| 6503 | return LY_SUCCESS; |
| 6504 | } |
| 6505 | } |
| 6506 | } |
| 6507 | |
| 6508 | /* false for all nodes */ |
| 6509 | set_fill_boolean(set1, 0); |
| 6510 | return LY_SUCCESS; |
| 6511 | } |
| 6512 | |
| 6513 | /* first convert properly */ |
| 6514 | if ((op[0] == '=') || (op[0] == '!')) { |
| 6515 | if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6516 | lyxp_set_cast(set1, LYXP_SET_BOOLEAN); |
| 6517 | lyxp_set_cast(set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6518 | } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6519 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6520 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6521 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6522 | LY_CHECK_RET(rc); |
| 6523 | } /* else we have 2 strings */ |
| 6524 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6525 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6526 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6527 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6528 | LY_CHECK_RET(rc); |
| 6529 | } |
| 6530 | |
| 6531 | assert(set1->type == set2->type); |
| 6532 | |
| 6533 | /* compute result */ |
| 6534 | if (op[0] == '=') { |
| 6535 | if (set1->type == LYXP_SET_BOOLEAN) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6536 | result = (set1->val.bln == set2->val.bln); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6537 | } else if (set1->type == LYXP_SET_NUMBER) { |
| 6538 | result = (set1->val.num == set2->val.num); |
| 6539 | } else { |
| 6540 | assert(set1->type == LYXP_SET_STRING); |
Michal Vasko | ac6c72f | 2019-11-14 16:09:34 +0100 | [diff] [blame] | 6541 | result = !strcmp(set1->val.str, set2->val.str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6542 | } |
| 6543 | } else if (op[0] == '!') { |
| 6544 | if (set1->type == LYXP_SET_BOOLEAN) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6545 | result = (set1->val.bln != set2->val.bln); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6546 | } else if (set1->type == LYXP_SET_NUMBER) { |
| 6547 | result = (set1->val.num != set2->val.num); |
| 6548 | } else { |
| 6549 | assert(set1->type == LYXP_SET_STRING); |
Michal Vasko | ac6c72f | 2019-11-14 16:09:34 +0100 | [diff] [blame] | 6550 | result = !strcmp(set1->val.str, set2->val.str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6551 | } |
| 6552 | } else { |
| 6553 | assert(set1->type == LYXP_SET_NUMBER); |
| 6554 | if (op[0] == '<') { |
| 6555 | if (op[1] == '=') { |
| 6556 | result = (set1->val.num <= set2->val.num); |
| 6557 | } else { |
| 6558 | result = (set1->val.num < set2->val.num); |
| 6559 | } |
| 6560 | } else { |
| 6561 | if (op[1] == '=') { |
| 6562 | result = (set1->val.num >= set2->val.num); |
| 6563 | } else { |
| 6564 | result = (set1->val.num > set2->val.num); |
| 6565 | } |
| 6566 | } |
| 6567 | } |
| 6568 | |
| 6569 | /* assign result */ |
| 6570 | if (result) { |
| 6571 | set_fill_boolean(set1, 1); |
| 6572 | } else { |
| 6573 | set_fill_boolean(set1, 0); |
| 6574 | } |
| 6575 | |
| 6576 | return LY_SUCCESS; |
| 6577 | } |
| 6578 | |
| 6579 | /** |
| 6580 | * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div', |
| 6581 | * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware. |
| 6582 | * |
| 6583 | * @param[in,out] set1 Set to use for the result. |
| 6584 | * @param[in] set2 Set acting as the second operand for @p op. |
| 6585 | * @param[in] op Operator to process. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6586 | * @return LY_ERR |
| 6587 | */ |
| 6588 | static LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6589 | 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] | 6590 | { |
| 6591 | LY_ERR rc; |
| 6592 | |
| 6593 | /* unary '-' */ |
| 6594 | if (!set2 && (op[0] == '-')) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6595 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6596 | LY_CHECK_RET(rc); |
| 6597 | set1->val.num *= -1; |
| 6598 | lyxp_set_free(set2); |
| 6599 | return LY_SUCCESS; |
| 6600 | } |
| 6601 | |
| 6602 | assert(set1 && set2); |
| 6603 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6604 | rc = lyxp_set_cast(set1, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6605 | LY_CHECK_RET(rc); |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6606 | rc = lyxp_set_cast(set2, LYXP_SET_NUMBER); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6607 | LY_CHECK_RET(rc); |
| 6608 | |
| 6609 | switch (op[0]) { |
| 6610 | /* '+' */ |
| 6611 | case '+': |
| 6612 | set1->val.num += set2->val.num; |
| 6613 | break; |
| 6614 | |
| 6615 | /* '-' */ |
| 6616 | case '-': |
| 6617 | set1->val.num -= set2->val.num; |
| 6618 | break; |
| 6619 | |
| 6620 | /* '*' */ |
| 6621 | case '*': |
| 6622 | set1->val.num *= set2->val.num; |
| 6623 | break; |
| 6624 | |
| 6625 | /* 'div' */ |
| 6626 | case 'd': |
| 6627 | set1->val.num /= set2->val.num; |
| 6628 | break; |
| 6629 | |
| 6630 | /* 'mod' */ |
| 6631 | case 'm': |
| 6632 | set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num); |
| 6633 | break; |
| 6634 | |
| 6635 | default: |
| 6636 | LOGINT_RET(set1->ctx); |
| 6637 | } |
| 6638 | |
| 6639 | return LY_SUCCESS; |
| 6640 | } |
| 6641 | |
| 6642 | /* |
| 6643 | * eval functions |
| 6644 | * |
| 6645 | * They execute a parsed XPath expression on some data subtree. |
| 6646 | */ |
| 6647 | |
| 6648 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6649 | * @brief Evaluate Predicate. Logs directly on error. |
| 6650 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6651 | * [9] Predicate ::= '[' Expr ']' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6652 | * |
| 6653 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6654 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6655 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 6656 | * @param[in] options XPath options. |
| 6657 | * @param[in] parent_pos_pred Whether parent predicate was a positional one. |
| 6658 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6659 | */ |
| 6660 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6661 | eval_predicate(struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, uint32_t options, uint8_t parent_pos_pred) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6662 | { |
| 6663 | LY_ERR rc; |
Michal Vasko | 57eab13 | 2019-09-24 11:46:26 +0200 | [diff] [blame] | 6664 | uint16_t i, orig_exp; |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 6665 | uint32_t orig_pos, orig_size; |
| 6666 | int32_t pred_in_ctx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6667 | struct lyxp_set set2; |
| 6668 | struct lyd_node *orig_parent; |
| 6669 | |
| 6670 | /* '[' */ |
| 6671 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6672 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 6673 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6674 | |
| 6675 | if (!set) { |
| 6676 | only_parse: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6677 | rc = eval_expr_select(exp, tok_idx, 0, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6678 | LY_CHECK_RET(rc); |
| 6679 | } else if (set->type == LYXP_SET_NODE_SET) { |
| 6680 | /* 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] | 6681 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6682 | |
| 6683 | /* empty set, nothing to evaluate */ |
| 6684 | if (!set->used) { |
| 6685 | goto only_parse; |
| 6686 | } |
| 6687 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6688 | orig_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6689 | orig_pos = 0; |
| 6690 | orig_size = set->used; |
| 6691 | orig_parent = NULL; |
Michal Vasko | 39dbf35 | 2020-05-21 10:08:59 +0200 | [diff] [blame] | 6692 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6693 | set_init(&set2, set); |
| 6694 | set_insert_node(&set2, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, 0); |
| 6695 | /* remember the node context position for position() and context size for last(), |
| 6696 | * predicates should always be evaluated with respect to the child axis (since we do |
| 6697 | * not support explicit axes) so we assign positions based on their parents */ |
| 6698 | if (parent_pos_pred && ((struct lyd_node *)set->val.nodes[i].node->parent != orig_parent)) { |
| 6699 | orig_parent = (struct lyd_node *)set->val.nodes[i].node->parent; |
| 6700 | orig_pos = 1; |
| 6701 | } else { |
| 6702 | ++orig_pos; |
| 6703 | } |
| 6704 | |
| 6705 | set2.ctx_pos = orig_pos; |
| 6706 | set2.ctx_size = orig_size; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6707 | *tok_idx = orig_exp; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6708 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6709 | rc = eval_expr_select(exp, tok_idx, 0, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6710 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6711 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6712 | return rc; |
| 6713 | } |
| 6714 | |
| 6715 | /* number is a position */ |
| 6716 | if (set2.type == LYXP_SET_NUMBER) { |
| 6717 | if ((long long)set2.val.num == orig_pos) { |
| 6718 | set2.val.num = 1; |
| 6719 | } else { |
| 6720 | set2.val.num = 0; |
| 6721 | } |
| 6722 | } |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6723 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6724 | |
| 6725 | /* predicate satisfied or not? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6726 | if (!set2.val.bln) { |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6727 | set_remove_node_none(set, i); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6728 | } |
| 6729 | } |
Michal Vasko | 2caefc1 | 2019-11-14 16:07:56 +0100 | [diff] [blame] | 6730 | set_remove_nodes_none(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6731 | |
| 6732 | } else if (set->type == LYXP_SET_SCNODE_SET) { |
| 6733 | for (i = 0; i < set->used; ++i) { |
| 6734 | if (set->val.scnodes[i].in_ctx == 1) { |
| 6735 | /* there is a currently-valid node */ |
| 6736 | break; |
| 6737 | } |
| 6738 | } |
| 6739 | /* empty set, nothing to evaluate */ |
| 6740 | if (i == set->used) { |
| 6741 | goto only_parse; |
| 6742 | } |
| 6743 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6744 | orig_exp = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6745 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6746 | /* set special in_ctx to all the valid snodes */ |
| 6747 | pred_in_ctx = set_scnode_new_in_ctx(set); |
| 6748 | |
| 6749 | /* use the valid snodes one-by-one */ |
| 6750 | for (i = 0; i < set->used; ++i) { |
| 6751 | if (set->val.scnodes[i].in_ctx != pred_in_ctx) { |
| 6752 | continue; |
| 6753 | } |
| 6754 | set->val.scnodes[i].in_ctx = 1; |
| 6755 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6756 | *tok_idx = orig_exp; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6757 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6758 | rc = eval_expr_select(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6759 | LY_CHECK_RET(rc); |
| 6760 | |
| 6761 | set->val.scnodes[i].in_ctx = pred_in_ctx; |
| 6762 | } |
| 6763 | |
| 6764 | /* restore the state as it was before the predicate */ |
| 6765 | for (i = 0; i < set->used; ++i) { |
| 6766 | if (set->val.scnodes[i].in_ctx == 1) { |
| 6767 | set->val.scnodes[i].in_ctx = 0; |
| 6768 | } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) { |
| 6769 | set->val.scnodes[i].in_ctx = 1; |
| 6770 | } |
| 6771 | } |
| 6772 | |
| 6773 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6774 | set2.type = LYXP_SET_NODE_SET; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6775 | set_fill_set(&set2, set); |
| 6776 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6777 | rc = eval_expr_select(exp, tok_idx, 0, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6778 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6779 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6780 | return rc; |
| 6781 | } |
| 6782 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 6783 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6784 | if (!set2.val.bln) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6785 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6786 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6787 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6788 | } |
| 6789 | |
| 6790 | /* ']' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6791 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6792 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6793 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 6794 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 6795 | |
| 6796 | return LY_SUCCESS; |
| 6797 | } |
| 6798 | |
| 6799 | /** |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6800 | * @brief Evaluate Literal. Logs directly on error. |
| 6801 | * |
| 6802 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6803 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6804 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 6805 | */ |
| 6806 | static void |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6807 | 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] | 6808 | { |
| 6809 | if (set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6810 | if (exp->tok_len[*tok_idx] == 2) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6811 | set_fill_string(set, "", 0); |
| 6812 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6813 | 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] | 6814 | } |
| 6815 | } |
| 6816 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6817 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 6818 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6819 | } |
| 6820 | |
| 6821 | /** |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6822 | * @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] | 6823 | * |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6824 | * @param[in] exp Full parsed XPath expression. |
| 6825 | * @param[in,out] tok_idx Index in @p exp at the beginning of the predicate, is updated on success. |
| 6826 | * @param[in] scnode Found schema node as context for the predicate. |
| 6827 | * @param[in] format Format of any prefixes in key names/values. |
| 6828 | * @param[out] predicates Parsed predicates. |
| 6829 | * @param[out] pred_type Type of @p predicates. |
| 6830 | * @return LY_SUCCESS on success, |
| 6831 | * @return LY_ERR on any error. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6832 | */ |
| 6833 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6834 | eval_name_test_try_compile_predicates(struct lyxp_expr *exp, uint16_t *tok_idx, const struct lysc_node *scnode, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 6835 | LY_PREFIX_FORMAT format, struct ly_path_predicate **predicates, |
| 6836 | enum ly_path_pred_type *pred_type) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6837 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6838 | LY_ERR ret = LY_SUCCESS; |
| 6839 | uint16_t key_count, e_idx, pred_idx = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6840 | const struct lysc_node *key; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6841 | size_t pred_len; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6842 | uint32_t prev_lo; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6843 | struct lyxp_expr *exp2 = NULL; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6844 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6845 | assert(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6846 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6847 | if (scnode->nodetype == LYS_LIST) { |
| 6848 | /* get key count */ |
| 6849 | if (scnode->flags & LYS_KEYLESS) { |
| 6850 | return LY_EINVAL; |
| 6851 | } |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 6852 | for (key_count = 0, key = lysc_node_children(scnode, 0); key && (key->flags & LYS_KEY); key = key->next, ++key_count) {} |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6853 | assert(key_count); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6854 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6855 | /* learn where the predicates end */ |
| 6856 | e_idx = *tok_idx; |
| 6857 | while (key_count) { |
| 6858 | /* '[' */ |
| 6859 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) { |
| 6860 | return LY_EINVAL; |
| 6861 | } |
| 6862 | ++e_idx; |
| 6863 | |
| 6864 | /* ']' */ |
| 6865 | while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) { |
| 6866 | ++e_idx; |
| 6867 | } |
| 6868 | ++e_idx; |
| 6869 | |
| 6870 | /* another presumably key predicate parsed */ |
| 6871 | --key_count; |
| 6872 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6873 | } else { |
| 6874 | /* learn just where this single predicate ends */ |
| 6875 | e_idx = *tok_idx; |
| 6876 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6877 | /* '[' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6878 | if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) { |
| 6879 | return LY_EINVAL; |
| 6880 | } |
| 6881 | ++e_idx; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6882 | |
| 6883 | /* ']' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6884 | while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) { |
| 6885 | ++e_idx; |
| 6886 | } |
| 6887 | ++e_idx; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6888 | } |
| 6889 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6890 | /* get the joined length of all the keys predicates/of the single leaf-list predicate */ |
| 6891 | pred_len = (exp->tok_pos[e_idx - 1] + exp->tok_len[e_idx - 1]) - exp->tok_pos[*tok_idx]; |
| 6892 | |
| 6893 | /* turn logging off */ |
| 6894 | prev_lo = ly_log_options(0); |
| 6895 | |
| 6896 | /* parse the predicate(s) */ |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 6897 | LY_CHECK_GOTO(ret = ly_path_parse_predicate(scnode->module->ctx, scnode, exp->expr + exp->tok_pos[*tok_idx], pred_len, |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6898 | LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp2), cleanup); |
| 6899 | |
| 6900 | /* compile */ |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 6901 | ret = ly_path_compile_predicate(scnode->module->ctx, scnode, scnode->module, scnode, exp2, &pred_idx, |
| 6902 | format, scnode->module, predicates, pred_type); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6903 | LY_CHECK_GOTO(ret, cleanup); |
| 6904 | |
| 6905 | /* success, the predicate must include all the needed information for hash-based search */ |
| 6906 | *tok_idx = e_idx; |
| 6907 | |
| 6908 | cleanup: |
| 6909 | ly_log_options(prev_lo); |
| 6910 | lyxp_expr_free(scnode->module->ctx, exp2); |
| 6911 | return ret; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6912 | } |
| 6913 | |
| 6914 | /** |
| 6915 | * @brief Evaluate NameTest and any following Predicates. Logs directly on error. |
| 6916 | * |
| 6917 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 6918 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 6919 | * [7] NameTest ::= '*' | NCName ':' '*' | QName |
| 6920 | * |
| 6921 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6922 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6923 | * @param[in] attr_axis Whether to search attributes or standard nodes. |
| 6924 | * @param[in] all_desc Whether to search all the descendants or children only. |
| 6925 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 6926 | * @param[in] options XPath options. |
| 6927 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 6928 | */ |
| 6929 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6930 | eval_name_test_with_predicate(struct lyxp_expr *exp, uint16_t *tok_idx, uint8_t attr_axis, uint8_t all_desc, struct lyxp_set *set, |
| 6931 | uint32_t options) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6932 | { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6933 | char *path; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6934 | const char *ncname, *ncname_dict = NULL; |
| 6935 | uint16_t ncname_len; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6936 | const struct lys_module *moveto_mod; |
| 6937 | const struct lysc_node *scnode = NULL, *tmp; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6938 | struct ly_path_predicate *predicates = NULL; |
| 6939 | enum ly_path_pred_type pred_type = 0; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6940 | LY_ERR rc = LY_SUCCESS; |
| 6941 | |
| 6942 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6943 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 6944 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6945 | |
| 6946 | if (!set) { |
| 6947 | goto moveto; |
| 6948 | } |
| 6949 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6950 | ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]]; |
| 6951 | ncname_len = exp->tok_len[*tok_idx - 1]; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6952 | |
| 6953 | /* parse (and skip) module name */ |
| 6954 | rc = moveto_resolve_model(&ncname, &ncname_len, set, &moveto_mod); |
| 6955 | LY_CHECK_GOTO(rc, cleanup); |
| 6956 | |
| 6957 | if (moveto_mod && !attr_axis && !all_desc && (set->type == LYXP_SET_NODE_SET)) { |
| 6958 | /* find the matching schema node in some parent in the context */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 6959 | for (uint32_t i = 0; i < set->used; ++i) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6960 | if (set->val.nodes[i].type == set->root_type) { |
| 6961 | tmp = lys_find_child(NULL, moveto_mod, ncname, ncname_len, 0, 0); |
| 6962 | } else if ((set->val.nodes[i].type == LYXP_NODE_ELEM) |
| 6963 | && (!scnode || (lysc_data_parent(scnode) != set->val.nodes[i].node->schema))) { |
| 6964 | /* do not repeat the same search */ |
| 6965 | tmp = lys_find_child(set->val.nodes[i].node->schema, moveto_mod, ncname, ncname_len, 0, 0); |
Radek Krejci | b124784 | 2020-07-02 16:22:38 +0200 | [diff] [blame] | 6966 | } else { |
| 6967 | tmp = NULL; |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6968 | } |
| 6969 | |
| 6970 | /* additional context check */ |
| 6971 | if (tmp && (set->root_type == LYXP_NODE_ROOT_CONFIG) && (tmp->flags & LYS_CONFIG_R)) { |
| 6972 | tmp = NULL; |
| 6973 | } |
| 6974 | |
| 6975 | if (tmp) { |
| 6976 | if (scnode) { |
| 6977 | /* we found a schema node with the same name but at different level, give up, too complicated */ |
| 6978 | scnode = NULL; |
| 6979 | break; |
| 6980 | } else { |
| 6981 | /* remember the found schema node and continue to make sure it can be used */ |
| 6982 | scnode = tmp; |
| 6983 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6984 | } |
| 6985 | } |
| 6986 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6987 | if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) { |
| 6988 | /* try to create the predicates */ |
| 6989 | if (eval_name_test_try_compile_predicates(exp, tok_idx, scnode, set->format, &predicates, &pred_type)) { |
| 6990 | /* hashes cannot be used */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 6991 | scnode = NULL; |
| 6992 | } |
| 6993 | } |
| 6994 | } |
| 6995 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 6996 | if (!scnode && moveto_mod) { |
| 6997 | /* we are not able to match based on a schema node and not all the modules match, |
| 6998 | * use dictionary for efficient comparison */ |
| 6999 | ncname_dict = lydict_insert(set->ctx, ncname, ncname_len); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7000 | } |
| 7001 | |
| 7002 | moveto: |
| 7003 | /* move to the attribute(s), data node(s), or schema node(s) */ |
| 7004 | if (attr_axis) { |
| 7005 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 7006 | set_scnode_clear_ctx(set); |
| 7007 | } else { |
| 7008 | if (all_desc) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7009 | rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7010 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7011 | rc = moveto_attr(set, moveto_mod, ncname_dict); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7012 | } |
| 7013 | LY_CHECK_GOTO(rc, cleanup); |
| 7014 | } |
| 7015 | } else { |
| 7016 | if (set && (options & LYXP_SCNODE_ALL)) { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7017 | int64_t i; |
| 7018 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7019 | if (all_desc) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7020 | rc = moveto_scnode_alldesc(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7021 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7022 | rc = moveto_scnode(set, moveto_mod, ncname_dict, options); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7023 | } |
| 7024 | LY_CHECK_GOTO(rc, cleanup); |
| 7025 | |
| 7026 | for (i = set->used - 1; i > -1; --i) { |
| 7027 | if (set->val.scnodes[i].in_ctx > 0) { |
| 7028 | break; |
| 7029 | } |
| 7030 | } |
| 7031 | if (i == -1) { |
| 7032 | path = lysc_path(set->ctx_scnode, LYSC_PATH_LOG, NULL, 0); |
| 7033 | LOGWRN(set->ctx, "Schema node \"%.*s\" not found (%.*s) with context node \"%s\".", |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 7034 | ncname_len, ncname, ncname - exp->expr, exp->expr, path); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7035 | free(path); |
| 7036 | } |
| 7037 | } else { |
| 7038 | if (all_desc) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7039 | rc = moveto_node_alldesc(set, moveto_mod, ncname_dict); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7040 | } else { |
| 7041 | if (scnode) { |
| 7042 | /* we can find the nodes using hashes */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7043 | rc = moveto_node_hash(set, scnode, predicates); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7044 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7045 | rc = moveto_node(set, moveto_mod, ncname_dict); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7046 | } |
| 7047 | } |
| 7048 | LY_CHECK_GOTO(rc, cleanup); |
| 7049 | } |
| 7050 | } |
| 7051 | |
| 7052 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7053 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 7054 | rc = eval_predicate(exp, tok_idx, set, options, 1); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7055 | LY_CHECK_RET(rc); |
| 7056 | } |
| 7057 | |
| 7058 | cleanup: |
Michal Vasko | db51a8d | 2020-05-27 15:22:29 +0200 | [diff] [blame] | 7059 | if (set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7060 | lydict_remove(set->ctx, ncname_dict); |
| 7061 | ly_path_predicates_free(set->ctx, pred_type, scnode, predicates); |
Michal Vasko | db51a8d | 2020-05-27 15:22:29 +0200 | [diff] [blame] | 7062 | } |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7063 | return rc; |
| 7064 | } |
| 7065 | |
| 7066 | /** |
| 7067 | * @brief Evaluate NodeType and any following Predicates. Logs directly on error. |
| 7068 | * |
| 7069 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
| 7070 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
| 7071 | * [8] NodeType ::= 'text' | 'node' |
| 7072 | * |
| 7073 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7074 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7075 | * @param[in] attr_axis Whether to search attributes or standard nodes. |
| 7076 | * @param[in] all_desc Whether to search all the descendants or children only. |
| 7077 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7078 | * @param[in] options XPath options. |
| 7079 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7080 | */ |
| 7081 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7082 | eval_node_type_with_predicate(struct lyxp_expr *exp, uint16_t *tok_idx, uint8_t attr_axis, uint8_t all_desc, |
| 7083 | struct lyxp_set *set, uint32_t options) |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7084 | { |
| 7085 | LY_ERR rc; |
| 7086 | |
| 7087 | /* TODO */ |
| 7088 | (void)attr_axis; |
| 7089 | (void)all_desc; |
| 7090 | |
| 7091 | if (set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7092 | assert(exp->tok_len[*tok_idx] == 4); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7093 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 7094 | set_scnode_clear_ctx(set); |
| 7095 | /* just for the debug message below */ |
| 7096 | set = NULL; |
| 7097 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7098 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7099 | rc = xpath_node(NULL, 0, set, options); |
| 7100 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7101 | assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4)); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7102 | rc = xpath_text(NULL, 0, set, options); |
| 7103 | } |
| 7104 | LY_CHECK_RET(rc); |
| 7105 | } |
| 7106 | } |
| 7107 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7108 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7109 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7110 | |
| 7111 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7112 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7113 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7114 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7115 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7116 | |
| 7117 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7118 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7119 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7120 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7121 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7122 | |
| 7123 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7124 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 7125 | rc = eval_predicate(exp, tok_idx, set, options, 1); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7126 | LY_CHECK_RET(rc); |
| 7127 | } |
| 7128 | |
| 7129 | return LY_SUCCESS; |
| 7130 | } |
| 7131 | |
| 7132 | /** |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7133 | * @brief Evaluate RelativeLocationPath. Logs directly on error. |
| 7134 | * |
| 7135 | * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step |
| 7136 | * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..' |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7137 | * [6] NodeTest ::= NameTest | NodeType '(' ')' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7138 | * |
| 7139 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7140 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7141 | * @param[in] all_desc Whether to search all the descendants or children only. |
| 7142 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7143 | * @param[in] options XPath options. |
| 7144 | * @return LY_ERR (YL_EINCOMPLETE on unresolved when) |
| 7145 | */ |
| 7146 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7147 | eval_relative_location_path(struct lyxp_expr *exp, uint16_t *tok_idx, uint8_t all_desc, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7148 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7149 | uint8_t attr_axis; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7150 | LY_ERR rc; |
| 7151 | |
| 7152 | goto step; |
| 7153 | do { |
| 7154 | /* evaluate '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7155 | if (exp->tok_len[*tok_idx] == 1) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7156 | all_desc = 0; |
| 7157 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7158 | assert(exp->tok_len[*tok_idx] == 2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7159 | all_desc = 1; |
| 7160 | } |
| 7161 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7162 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7163 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7164 | |
| 7165 | step: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7166 | /* evaluate abbreviated axis '@'? if any */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7167 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7168 | attr_axis = 1; |
| 7169 | |
| 7170 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7171 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7172 | ++(*tok_idx); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7173 | } else { |
| 7174 | attr_axis = 0; |
| 7175 | } |
| 7176 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7177 | /* Step */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7178 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7179 | case LYXP_TOKEN_DOT: |
| 7180 | /* evaluate '.' */ |
| 7181 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 7182 | rc = moveto_scnode_self(set, all_desc, options); |
| 7183 | } else { |
| 7184 | rc = moveto_self(set, all_desc, options); |
| 7185 | } |
| 7186 | LY_CHECK_RET(rc); |
| 7187 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7188 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7189 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7190 | break; |
| 7191 | |
| 7192 | case LYXP_TOKEN_DDOT: |
| 7193 | /* evaluate '..' */ |
| 7194 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 7195 | rc = moveto_scnode_parent(set, all_desc, options); |
| 7196 | } else { |
| 7197 | rc = moveto_parent(set, all_desc, options); |
| 7198 | } |
| 7199 | LY_CHECK_RET(rc); |
| 7200 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7201 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7202 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7203 | break; |
| 7204 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7205 | case LYXP_TOKEN_NAMETEST: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7206 | /* evaluate NameTest Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7207 | 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] | 7208 | LY_CHECK_RET(rc); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7209 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7210 | |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7211 | case LYXP_TOKEN_NODETYPE: |
| 7212 | /* evaluate NodeType Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7213 | 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] | 7214 | LY_CHECK_RET(rc); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7215 | break; |
| 7216 | |
| 7217 | default: |
Michal Vasko | 02a7738 | 2019-09-12 11:47:35 +0200 | [diff] [blame] | 7218 | LOGINT_RET(set ? set->ctx : NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7219 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7220 | } 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] | 7221 | |
| 7222 | return LY_SUCCESS; |
| 7223 | } |
| 7224 | |
| 7225 | /** |
| 7226 | * @brief Evaluate AbsoluteLocationPath. Logs directly on error. |
| 7227 | * |
| 7228 | * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath |
| 7229 | * |
| 7230 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7231 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7232 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7233 | * @param[in] options XPath options. |
| 7234 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7235 | */ |
| 7236 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7237 | eval_absolute_location_path(struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7238 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7239 | uint8_t all_desc; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7240 | LY_ERR rc; |
| 7241 | |
| 7242 | if (set) { |
| 7243 | /* no matter what tokens follow, we need to be at the root */ |
| 7244 | moveto_root(set, options); |
| 7245 | } |
| 7246 | |
| 7247 | /* '/' RelativeLocationPath? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7248 | if (exp->tok_len[*tok_idx] == 1) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7249 | /* evaluate '/' - deferred */ |
| 7250 | all_desc = 0; |
| 7251 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7252 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7253 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7254 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7255 | if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7256 | return LY_SUCCESS; |
| 7257 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7258 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7259 | case LYXP_TOKEN_DOT: |
| 7260 | case LYXP_TOKEN_DDOT: |
| 7261 | case LYXP_TOKEN_AT: |
| 7262 | case LYXP_TOKEN_NAMETEST: |
| 7263 | case LYXP_TOKEN_NODETYPE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7264 | rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7265 | LY_CHECK_RET(rc); |
| 7266 | break; |
| 7267 | default: |
| 7268 | break; |
| 7269 | } |
| 7270 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7271 | } else { |
Radek Krejci | f6a1100 | 2020-08-21 13:29:07 +0200 | [diff] [blame] | 7272 | /* '//' RelativeLocationPath */ |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7273 | /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */ |
| 7274 | all_desc = 1; |
| 7275 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7276 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7277 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7278 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7279 | rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7280 | LY_CHECK_RET(rc); |
| 7281 | } |
| 7282 | |
| 7283 | return LY_SUCCESS; |
| 7284 | } |
| 7285 | |
| 7286 | /** |
| 7287 | * @brief Evaluate FunctionCall. Logs directly on error. |
| 7288 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7289 | * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')' |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7290 | * |
| 7291 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7292 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7293 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7294 | * @param[in] options XPath options. |
| 7295 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7296 | */ |
| 7297 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7298 | eval_function_call(struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7299 | { |
| 7300 | LY_ERR rc; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7301 | LY_ERR (*xpath_func)(struct lyxp_set **, uint16_t, struct lyxp_set *, uint32_t) = NULL; |
Michal Vasko | 0cbf54f | 2019-12-16 10:01:06 +0100 | [diff] [blame] | 7302 | uint16_t arg_count = 0, i; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7303 | struct lyxp_set **args = NULL, **args_aux; |
| 7304 | |
| 7305 | if (set) { |
| 7306 | /* FunctionName */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7307 | switch (exp->tok_len[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7308 | case 3: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7309 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7310 | xpath_func = &xpath_not; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7311 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7312 | xpath_func = &xpath_sum; |
| 7313 | } |
| 7314 | break; |
| 7315 | case 4: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7316 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7317 | xpath_func = &xpath_lang; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7318 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7319 | xpath_func = &xpath_last; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7320 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7321 | xpath_func = &xpath_name; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7322 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7323 | xpath_func = &xpath_true; |
| 7324 | } |
| 7325 | break; |
| 7326 | case 5: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7327 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7328 | xpath_func = &xpath_count; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7329 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7330 | xpath_func = &xpath_false; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7331 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7332 | xpath_func = &xpath_floor; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7333 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7334 | xpath_func = &xpath_round; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7335 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7336 | xpath_func = &xpath_deref; |
| 7337 | } |
| 7338 | break; |
| 7339 | case 6: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7340 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7341 | xpath_func = &xpath_concat; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7342 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7343 | xpath_func = &xpath_number; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7344 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7345 | xpath_func = &xpath_string; |
| 7346 | } |
| 7347 | break; |
| 7348 | case 7: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7349 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7350 | xpath_func = &xpath_boolean; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7351 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7352 | xpath_func = &xpath_ceiling; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7353 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7354 | xpath_func = &xpath_current; |
| 7355 | } |
| 7356 | break; |
| 7357 | case 8: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7358 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7359 | xpath_func = &xpath_contains; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7360 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7361 | xpath_func = &xpath_position; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7362 | } 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] | 7363 | xpath_func = &xpath_re_match; |
| 7364 | } |
| 7365 | break; |
| 7366 | case 9: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7367 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7368 | xpath_func = &xpath_substring; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7369 | } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7370 | xpath_func = &xpath_translate; |
| 7371 | } |
| 7372 | break; |
| 7373 | case 10: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7374 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7375 | xpath_func = &xpath_local_name; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7376 | } 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] | 7377 | xpath_func = &xpath_enum_value; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7378 | } 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] | 7379 | xpath_func = &xpath_bit_is_set; |
| 7380 | } |
| 7381 | break; |
| 7382 | case 11: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7383 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7384 | xpath_func = &xpath_starts_with; |
| 7385 | } |
| 7386 | break; |
| 7387 | case 12: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7388 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7389 | xpath_func = &xpath_derived_from; |
| 7390 | } |
| 7391 | break; |
| 7392 | case 13: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7393 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7394 | xpath_func = &xpath_namespace_uri; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7395 | } 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] | 7396 | xpath_func = &xpath_string_length; |
| 7397 | } |
| 7398 | break; |
| 7399 | case 15: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7400 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7401 | xpath_func = &xpath_normalize_space; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7402 | } 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] | 7403 | xpath_func = &xpath_substring_after; |
| 7404 | } |
| 7405 | break; |
| 7406 | case 16: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7407 | if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7408 | xpath_func = &xpath_substring_before; |
| 7409 | } |
| 7410 | break; |
| 7411 | case 20: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7412 | 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] | 7413 | xpath_func = &xpath_derived_from_or_self; |
| 7414 | } |
| 7415 | break; |
| 7416 | } |
| 7417 | |
| 7418 | if (!xpath_func) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7419 | 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] | 7420 | return LY_EVALID; |
| 7421 | } |
| 7422 | } |
| 7423 | |
| 7424 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7425 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7426 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7427 | |
| 7428 | /* '(' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7429 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7430 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7431 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7432 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7433 | |
| 7434 | /* ( Expr ( ',' Expr )* )? */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7435 | if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7436 | if (set) { |
| 7437 | args = malloc(sizeof *args); |
| 7438 | LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup); |
| 7439 | arg_count = 1; |
| 7440 | args[0] = set_copy(set); |
| 7441 | if (!args[0]) { |
| 7442 | rc = LY_EMEM; |
| 7443 | goto cleanup; |
| 7444 | } |
| 7445 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7446 | rc = eval_expr_select(exp, tok_idx, 0, args[0], options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7447 | LY_CHECK_GOTO(rc, cleanup); |
| 7448 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7449 | rc = eval_expr_select(exp, tok_idx, 0, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7450 | LY_CHECK_GOTO(rc, cleanup); |
| 7451 | } |
| 7452 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7453 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7454 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7455 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7456 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7457 | |
| 7458 | if (set) { |
| 7459 | ++arg_count; |
| 7460 | args_aux = realloc(args, arg_count * sizeof *args); |
| 7461 | LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup); |
| 7462 | args = args_aux; |
| 7463 | args[arg_count - 1] = set_copy(set); |
| 7464 | if (!args[arg_count - 1]) { |
| 7465 | rc = LY_EMEM; |
| 7466 | goto cleanup; |
| 7467 | } |
| 7468 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7469 | 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] | 7470 | LY_CHECK_GOTO(rc, cleanup); |
| 7471 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7472 | rc = eval_expr_select(exp, tok_idx, 0, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7473 | LY_CHECK_GOTO(rc, cleanup); |
| 7474 | } |
| 7475 | } |
| 7476 | |
| 7477 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7478 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7479 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7480 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7481 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7482 | |
| 7483 | if (set) { |
| 7484 | /* evaluate function */ |
| 7485 | rc = xpath_func(args, arg_count, set, options); |
| 7486 | |
| 7487 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7488 | /* merge all nodes from arg evaluations */ |
| 7489 | for (i = 0; i < arg_count; ++i) { |
| 7490 | set_scnode_clear_ctx(args[i]); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7491 | lyxp_set_scnode_merge(set, args[i]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7492 | } |
| 7493 | } |
| 7494 | } else { |
| 7495 | rc = LY_SUCCESS; |
| 7496 | } |
| 7497 | |
| 7498 | cleanup: |
| 7499 | for (i = 0; i < arg_count; ++i) { |
| 7500 | lyxp_set_free(args[i]); |
| 7501 | } |
| 7502 | free(args); |
| 7503 | |
| 7504 | return rc; |
| 7505 | } |
| 7506 | |
| 7507 | /** |
| 7508 | * @brief Evaluate Number. Logs directly on error. |
| 7509 | * |
| 7510 | * @param[in] ctx Context for errors. |
| 7511 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7512 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7513 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7514 | * @return LY_ERR |
| 7515 | */ |
| 7516 | static LY_ERR |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7517 | 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] | 7518 | { |
| 7519 | long double num; |
| 7520 | char *endptr; |
| 7521 | |
| 7522 | if (set) { |
| 7523 | errno = 0; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7524 | num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7525 | if (errno) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7526 | 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] | 7527 | 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] | 7528 | 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] | 7529 | return LY_EVALID; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7530 | } else if (endptr - &exp->expr[exp->tok_pos[*tok_idx]] != exp->tok_len[*tok_idx]) { |
| 7531 | 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] | 7532 | 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] | 7533 | exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7534 | return LY_EVALID; |
| 7535 | } |
| 7536 | |
| 7537 | set_fill_number(set, num); |
| 7538 | } |
| 7539 | |
| 7540 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7541 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7542 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7543 | return LY_SUCCESS; |
| 7544 | } |
| 7545 | |
| 7546 | /** |
| 7547 | * @brief Evaluate PathExpr. Logs directly on error. |
| 7548 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7549 | * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate* |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7550 | * | PrimaryExpr Predicate* '/' RelativeLocationPath |
| 7551 | * | PrimaryExpr Predicate* '//' RelativeLocationPath |
| 7552 | * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7553 | * [10] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7554 | * |
| 7555 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7556 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7557 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7558 | * @param[in] options XPath options. |
| 7559 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7560 | */ |
| 7561 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7562 | eval_path_expr(struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7563 | { |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7564 | uint8_t all_desc, parent_pos_pred; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7565 | LY_ERR rc; |
| 7566 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7567 | switch (exp->tokens[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7568 | case LYXP_TOKEN_PAR1: |
| 7569 | /* '(' Expr ')' */ |
| 7570 | |
| 7571 | /* '(' */ |
| 7572 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7573 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7574 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7575 | |
| 7576 | /* Expr */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7577 | rc = eval_expr_select(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7578 | LY_CHECK_RET(rc); |
| 7579 | |
| 7580 | /* ')' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7581 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7582 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7583 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7584 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7585 | |
| 7586 | parent_pos_pred = 0; |
| 7587 | goto predicate; |
| 7588 | |
| 7589 | case LYXP_TOKEN_DOT: |
| 7590 | case LYXP_TOKEN_DDOT: |
| 7591 | case LYXP_TOKEN_AT: |
| 7592 | case LYXP_TOKEN_NAMETEST: |
| 7593 | case LYXP_TOKEN_NODETYPE: |
| 7594 | /* RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7595 | rc = eval_relative_location_path(exp, tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7596 | LY_CHECK_RET(rc); |
| 7597 | break; |
| 7598 | |
| 7599 | case LYXP_TOKEN_FUNCNAME: |
| 7600 | /* FunctionCall */ |
| 7601 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7602 | rc = eval_function_call(exp, tok_idx, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7603 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7604 | rc = eval_function_call(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7605 | } |
| 7606 | LY_CHECK_RET(rc); |
| 7607 | |
| 7608 | parent_pos_pred = 1; |
| 7609 | goto predicate; |
| 7610 | |
Michal Vasko | 3e48bf3 | 2020-06-01 08:39:07 +0200 | [diff] [blame] | 7611 | case LYXP_TOKEN_OPER_PATH: |
| 7612 | case LYXP_TOKEN_OPER_RPATH: |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7613 | /* AbsoluteLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7614 | rc = eval_absolute_location_path(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7615 | LY_CHECK_RET(rc); |
| 7616 | break; |
| 7617 | |
| 7618 | case LYXP_TOKEN_LITERAL: |
| 7619 | /* Literal */ |
| 7620 | if (!set || (options & LYXP_SCNODE_ALL)) { |
| 7621 | if (set) { |
| 7622 | set_scnode_clear_ctx(set); |
| 7623 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7624 | eval_literal(exp, tok_idx, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7625 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7626 | eval_literal(exp, tok_idx, set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7627 | } |
| 7628 | |
| 7629 | parent_pos_pred = 1; |
| 7630 | goto predicate; |
| 7631 | |
| 7632 | case LYXP_TOKEN_NUMBER: |
| 7633 | /* Number */ |
| 7634 | if (!set || (options & LYXP_SCNODE_ALL)) { |
| 7635 | if (set) { |
| 7636 | set_scnode_clear_ctx(set); |
| 7637 | } |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7638 | rc = eval_number(NULL, exp, tok_idx, NULL); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7639 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7640 | rc = eval_number(set->ctx, exp, tok_idx, set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7641 | } |
| 7642 | LY_CHECK_RET(rc); |
| 7643 | |
| 7644 | parent_pos_pred = 1; |
| 7645 | goto predicate; |
| 7646 | |
| 7647 | default: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7648 | LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[*tok_idx]), |
| 7649 | &exp->expr[exp->tok_pos[*tok_idx]]); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7650 | return LY_EVALID; |
| 7651 | } |
| 7652 | |
| 7653 | return LY_SUCCESS; |
| 7654 | |
| 7655 | predicate: |
| 7656 | /* Predicate* */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7657 | while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) { |
| 7658 | rc = eval_predicate(exp, tok_idx, set, options, parent_pos_pred); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7659 | LY_CHECK_RET(rc); |
| 7660 | } |
| 7661 | |
| 7662 | /* ('/' or '//') RelativeLocationPath */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7663 | 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] | 7664 | |
| 7665 | /* evaluate '/' or '//' */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7666 | if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7667 | all_desc = 0; |
| 7668 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7669 | all_desc = 1; |
| 7670 | } |
| 7671 | |
| 7672 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7673 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7674 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7675 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7676 | rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7677 | LY_CHECK_RET(rc); |
| 7678 | } |
| 7679 | |
| 7680 | return LY_SUCCESS; |
| 7681 | } |
| 7682 | |
| 7683 | /** |
| 7684 | * @brief Evaluate UnionExpr. Logs directly on error. |
| 7685 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7686 | * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7687 | * |
| 7688 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7689 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7690 | * @param[in] repeat How many times this expression is repeated. |
| 7691 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7692 | * @param[in] options XPath options. |
| 7693 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7694 | */ |
| 7695 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7696 | eval_union_expr(struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7697 | { |
| 7698 | LY_ERR rc = LY_SUCCESS; |
| 7699 | struct lyxp_set orig_set, set2; |
| 7700 | uint16_t i; |
| 7701 | |
| 7702 | assert(repeat); |
| 7703 | |
| 7704 | set_init(&orig_set, set); |
| 7705 | set_init(&set2, set); |
| 7706 | |
| 7707 | set_fill_set(&orig_set, set); |
| 7708 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7709 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7710 | LY_CHECK_GOTO(rc, cleanup); |
| 7711 | |
| 7712 | /* ('|' PathExpr)* */ |
| 7713 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7714 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7715 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7716 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7717 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7718 | |
| 7719 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7720 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7721 | LY_CHECK_GOTO(rc, cleanup); |
| 7722 | continue; |
| 7723 | } |
| 7724 | |
| 7725 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7726 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7727 | LY_CHECK_GOTO(rc, cleanup); |
| 7728 | |
| 7729 | /* eval */ |
| 7730 | if (options & LYXP_SCNODE_ALL) { |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 7731 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7732 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7733 | rc = moveto_union(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7734 | LY_CHECK_GOTO(rc, cleanup); |
| 7735 | } |
| 7736 | } |
| 7737 | |
| 7738 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7739 | lyxp_set_free_content(&orig_set); |
| 7740 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7741 | return rc; |
| 7742 | } |
| 7743 | |
| 7744 | /** |
| 7745 | * @brief Evaluate UnaryExpr. Logs directly on error. |
| 7746 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7747 | * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7748 | * |
| 7749 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7750 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7751 | * @param[in] repeat How many times this expression is repeated. |
| 7752 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7753 | * @param[in] options XPath options. |
| 7754 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7755 | */ |
| 7756 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7757 | eval_unary_expr(struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7758 | { |
| 7759 | LY_ERR rc; |
| 7760 | uint16_t this_op, i; |
| 7761 | |
| 7762 | assert(repeat); |
| 7763 | |
| 7764 | /* ('-')+ */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7765 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7766 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7767 | 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] | 7768 | |
| 7769 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7770 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7771 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7772 | } |
| 7773 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7774 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7775 | LY_CHECK_RET(rc); |
| 7776 | |
| 7777 | if (set && (repeat % 2)) { |
| 7778 | if (options & LYXP_SCNODE_ALL) { |
| 7779 | warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]); |
| 7780 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7781 | 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] | 7782 | LY_CHECK_RET(rc); |
| 7783 | } |
| 7784 | } |
| 7785 | |
| 7786 | return LY_SUCCESS; |
| 7787 | } |
| 7788 | |
| 7789 | /** |
| 7790 | * @brief Evaluate MultiplicativeExpr. Logs directly on error. |
| 7791 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7792 | * [18] MultiplicativeExpr ::= UnaryExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7793 | * | MultiplicativeExpr '*' UnaryExpr |
| 7794 | * | MultiplicativeExpr 'div' UnaryExpr |
| 7795 | * | MultiplicativeExpr 'mod' UnaryExpr |
| 7796 | * |
| 7797 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7798 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7799 | * @param[in] repeat How many times this expression is repeated. |
| 7800 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7801 | * @param[in] options XPath options. |
| 7802 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7803 | */ |
| 7804 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7805 | eval_multiplicative_expr(struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7806 | { |
| 7807 | LY_ERR rc; |
| 7808 | uint16_t this_op; |
| 7809 | struct lyxp_set orig_set, set2; |
| 7810 | uint16_t i; |
| 7811 | |
| 7812 | assert(repeat); |
| 7813 | |
| 7814 | set_init(&orig_set, set); |
| 7815 | set_init(&set2, set); |
| 7816 | |
| 7817 | set_fill_set(&orig_set, set); |
| 7818 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7819 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7820 | LY_CHECK_GOTO(rc, cleanup); |
| 7821 | |
| 7822 | /* ('*' / 'div' / 'mod' UnaryExpr)* */ |
| 7823 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7824 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7825 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7826 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7827 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7828 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7829 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7830 | |
| 7831 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7832 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7833 | LY_CHECK_GOTO(rc, cleanup); |
| 7834 | continue; |
| 7835 | } |
| 7836 | |
| 7837 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7838 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7839 | LY_CHECK_GOTO(rc, cleanup); |
| 7840 | |
| 7841 | /* eval */ |
| 7842 | if (options & LYXP_SCNODE_ALL) { |
| 7843 | 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] | 7844 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7845 | set_scnode_clear_ctx(set); |
| 7846 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7847 | 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] | 7848 | LY_CHECK_GOTO(rc, cleanup); |
| 7849 | } |
| 7850 | } |
| 7851 | |
| 7852 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7853 | lyxp_set_free_content(&orig_set); |
| 7854 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7855 | return rc; |
| 7856 | } |
| 7857 | |
| 7858 | /** |
| 7859 | * @brief Evaluate AdditiveExpr. Logs directly on error. |
| 7860 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7861 | * [17] AdditiveExpr ::= MultiplicativeExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7862 | * | AdditiveExpr '+' MultiplicativeExpr |
| 7863 | * | AdditiveExpr '-' MultiplicativeExpr |
| 7864 | * |
| 7865 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7866 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7867 | * @param[in] repeat How many times this expression is repeated. |
| 7868 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7869 | * @param[in] options XPath options. |
| 7870 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7871 | */ |
| 7872 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7873 | eval_additive_expr(struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7874 | { |
| 7875 | LY_ERR rc; |
| 7876 | uint16_t this_op; |
| 7877 | struct lyxp_set orig_set, set2; |
| 7878 | uint16_t i; |
| 7879 | |
| 7880 | assert(repeat); |
| 7881 | |
| 7882 | set_init(&orig_set, set); |
| 7883 | set_init(&set2, set); |
| 7884 | |
| 7885 | set_fill_set(&orig_set, set); |
| 7886 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7887 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7888 | LY_CHECK_GOTO(rc, cleanup); |
| 7889 | |
| 7890 | /* ('+' / '-' MultiplicativeExpr)* */ |
| 7891 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7892 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7893 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7894 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7895 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7896 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7897 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7898 | |
| 7899 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7900 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7901 | LY_CHECK_GOTO(rc, cleanup); |
| 7902 | continue; |
| 7903 | } |
| 7904 | |
| 7905 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7906 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7907 | LY_CHECK_GOTO(rc, cleanup); |
| 7908 | |
| 7909 | /* eval */ |
| 7910 | if (options & LYXP_SCNODE_ALL) { |
| 7911 | 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] | 7912 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7913 | set_scnode_clear_ctx(set); |
| 7914 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 7915 | 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] | 7916 | LY_CHECK_GOTO(rc, cleanup); |
| 7917 | } |
| 7918 | } |
| 7919 | |
| 7920 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7921 | lyxp_set_free_content(&orig_set); |
| 7922 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7923 | return rc; |
| 7924 | } |
| 7925 | |
| 7926 | /** |
| 7927 | * @brief Evaluate RelationalExpr. Logs directly on error. |
| 7928 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7929 | * [16] RelationalExpr ::= AdditiveExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7930 | * | RelationalExpr '<' AdditiveExpr |
| 7931 | * | RelationalExpr '>' AdditiveExpr |
| 7932 | * | RelationalExpr '<=' AdditiveExpr |
| 7933 | * | RelationalExpr '>=' AdditiveExpr |
| 7934 | * |
| 7935 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7936 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7937 | * @param[in] repeat How many times this expression is repeated. |
| 7938 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 7939 | * @param[in] options XPath options. |
| 7940 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 7941 | */ |
| 7942 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 7943 | eval_relational_expr(struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7944 | { |
| 7945 | LY_ERR rc; |
| 7946 | uint16_t this_op; |
| 7947 | struct lyxp_set orig_set, set2; |
| 7948 | uint16_t i; |
| 7949 | |
| 7950 | assert(repeat); |
| 7951 | |
| 7952 | set_init(&orig_set, set); |
| 7953 | set_init(&set2, set); |
| 7954 | |
| 7955 | set_fill_set(&orig_set, set); |
| 7956 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7957 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7958 | LY_CHECK_GOTO(rc, cleanup); |
| 7959 | |
| 7960 | /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */ |
| 7961 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7962 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7963 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7964 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7965 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7966 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 7967 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7968 | |
| 7969 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7970 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7971 | LY_CHECK_GOTO(rc, cleanup); |
| 7972 | continue; |
| 7973 | } |
| 7974 | |
| 7975 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 7976 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7977 | LY_CHECK_GOTO(rc, cleanup); |
| 7978 | |
| 7979 | /* eval */ |
| 7980 | if (options & LYXP_SCNODE_ALL) { |
| 7981 | 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] | 7982 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7983 | set_scnode_clear_ctx(set); |
| 7984 | } else { |
| 7985 | rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options); |
| 7986 | LY_CHECK_GOTO(rc, cleanup); |
| 7987 | } |
| 7988 | } |
| 7989 | |
| 7990 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7991 | lyxp_set_free_content(&orig_set); |
| 7992 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 7993 | return rc; |
| 7994 | } |
| 7995 | |
| 7996 | /** |
| 7997 | * @brief Evaluate EqualityExpr. Logs directly on error. |
| 7998 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 7999 | * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8000 | * | EqualityExpr '!=' RelationalExpr |
| 8001 | * |
| 8002 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8003 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8004 | * @param[in] repeat How many times this expression is repeated. |
| 8005 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8006 | * @param[in] options XPath options. |
| 8007 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8008 | */ |
| 8009 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 8010 | eval_equality_expr(struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8011 | { |
| 8012 | LY_ERR rc; |
| 8013 | uint16_t this_op; |
| 8014 | struct lyxp_set orig_set, set2; |
| 8015 | uint16_t i; |
| 8016 | |
| 8017 | assert(repeat); |
| 8018 | |
| 8019 | set_init(&orig_set, set); |
| 8020 | set_init(&set2, set); |
| 8021 | |
| 8022 | set_fill_set(&orig_set, set); |
| 8023 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8024 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8025 | LY_CHECK_GOTO(rc, cleanup); |
| 8026 | |
| 8027 | /* ('=' / '!=' RelationalExpr)* */ |
| 8028 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8029 | this_op = *tok_idx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8030 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8031 | 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] | 8032 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"), |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8033 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 8034 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8035 | |
| 8036 | if (!set) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8037 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8038 | LY_CHECK_GOTO(rc, cleanup); |
| 8039 | continue; |
| 8040 | } |
| 8041 | |
| 8042 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8043 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8044 | LY_CHECK_GOTO(rc, cleanup); |
| 8045 | |
| 8046 | /* eval */ |
| 8047 | if (options & LYXP_SCNODE_ALL) { |
| 8048 | 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] | 8049 | warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1); |
| 8050 | 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] | 8051 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8052 | set_scnode_clear_ctx(set); |
| 8053 | } else { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8054 | rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options); |
| 8055 | LY_CHECK_GOTO(rc, cleanup); |
| 8056 | } |
| 8057 | } |
| 8058 | |
| 8059 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8060 | lyxp_set_free_content(&orig_set); |
| 8061 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8062 | return rc; |
| 8063 | } |
| 8064 | |
| 8065 | /** |
| 8066 | * @brief Evaluate AndExpr. Logs directly on error. |
| 8067 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8068 | * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8069 | * |
| 8070 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8071 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8072 | * @param[in] repeat How many times this expression is repeated. |
| 8073 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8074 | * @param[in] options XPath options. |
| 8075 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8076 | */ |
| 8077 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 8078 | eval_and_expr(struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8079 | { |
| 8080 | LY_ERR rc; |
| 8081 | struct lyxp_set orig_set, set2; |
| 8082 | uint16_t i; |
| 8083 | |
| 8084 | assert(repeat); |
| 8085 | |
| 8086 | set_init(&orig_set, set); |
| 8087 | set_init(&set2, set); |
| 8088 | |
| 8089 | set_fill_set(&orig_set, set); |
| 8090 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8091 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8092 | LY_CHECK_GOTO(rc, cleanup); |
| 8093 | |
| 8094 | /* cast to boolean, we know that will be the final result */ |
| 8095 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 8096 | set_scnode_clear_ctx(set); |
| 8097 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8098 | lyxp_set_cast(set, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8099 | } |
| 8100 | |
| 8101 | /* ('and' EqualityExpr)* */ |
| 8102 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8103 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG); |
| 8104 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || !set->val.bln ? "skipped" : "parsed"), |
| 8105 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 8106 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8107 | |
| 8108 | /* lazy evaluation */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8109 | if (!set || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) { |
| 8110 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8111 | LY_CHECK_GOTO(rc, cleanup); |
| 8112 | continue; |
| 8113 | } |
| 8114 | |
| 8115 | set_fill_set(&set2, &orig_set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8116 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8117 | LY_CHECK_GOTO(rc, cleanup); |
| 8118 | |
| 8119 | /* eval - just get boolean value actually */ |
| 8120 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 8121 | set_scnode_clear_ctx(&set2); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 8122 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8123 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8124 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8125 | set_fill_set(set, &set2); |
| 8126 | } |
| 8127 | } |
| 8128 | |
| 8129 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8130 | lyxp_set_free_content(&orig_set); |
| 8131 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8132 | return rc; |
| 8133 | } |
| 8134 | |
| 8135 | /** |
| 8136 | * @brief Evaluate OrExpr. Logs directly on error. |
| 8137 | * |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8138 | * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8139 | * |
| 8140 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8141 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8142 | * @param[in] repeat How many times this expression is repeated. |
| 8143 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8144 | * @param[in] options XPath options. |
| 8145 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8146 | */ |
| 8147 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 8148 | eval_or_expr(struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8149 | { |
| 8150 | LY_ERR rc; |
| 8151 | struct lyxp_set orig_set, set2; |
| 8152 | uint16_t i; |
| 8153 | |
| 8154 | assert(repeat); |
| 8155 | |
| 8156 | set_init(&orig_set, set); |
| 8157 | set_init(&set2, set); |
| 8158 | |
| 8159 | set_fill_set(&orig_set, set); |
| 8160 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8161 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8162 | LY_CHECK_GOTO(rc, cleanup); |
| 8163 | |
| 8164 | /* cast to boolean, we know that will be the final result */ |
| 8165 | if (set && (options & LYXP_SCNODE_ALL)) { |
| 8166 | set_scnode_clear_ctx(set); |
| 8167 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8168 | lyxp_set_cast(set, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8169 | } |
| 8170 | |
| 8171 | /* ('or' AndExpr)* */ |
| 8172 | for (i = 0; i < repeat; ++i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8173 | assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG); |
| 8174 | LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || set->val.bln ? "skipped" : "parsed"), |
| 8175 | lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]); |
| 8176 | ++(*tok_idx); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8177 | |
| 8178 | /* lazy evaluation */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8179 | if (!set || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) { |
| 8180 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, NULL, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8181 | LY_CHECK_GOTO(rc, cleanup); |
| 8182 | continue; |
| 8183 | } |
| 8184 | |
| 8185 | set_fill_set(&set2, &orig_set); |
| 8186 | /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one), |
| 8187 | * but it does not matter */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8188 | rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8189 | LY_CHECK_GOTO(rc, cleanup); |
| 8190 | |
| 8191 | /* eval - just get boolean value actually */ |
| 8192 | if (set->type == LYXP_SET_SCNODE_SET) { |
| 8193 | set_scnode_clear_ctx(&set2); |
Michal Vasko | ecd62de | 2019-11-13 12:35:11 +0100 | [diff] [blame] | 8194 | lyxp_set_scnode_merge(set, &set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8195 | } else { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8196 | lyxp_set_cast(&set2, LYXP_SET_BOOLEAN); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8197 | set_fill_set(set, &set2); |
| 8198 | } |
| 8199 | } |
| 8200 | |
| 8201 | cleanup: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8202 | lyxp_set_free_content(&orig_set); |
| 8203 | lyxp_set_free_content(&set2); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8204 | return rc; |
| 8205 | } |
| 8206 | |
| 8207 | /** |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8208 | * @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] | 8209 | * |
| 8210 | * @param[in] exp Parsed XPath expression. |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8211 | * @param[in] tok_idx Position in the expression @p exp. |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8212 | * @param[in] etype Expression type to evaluate. |
| 8213 | * @param[in,out] set Context and result set. On NULL the rule is only parsed. |
| 8214 | * @param[in] options XPath options. |
| 8215 | * @return LY_ERR (LY_EINCOMPLETE on unresolved when) |
| 8216 | */ |
| 8217 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 8218 | eval_expr_select(struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8219 | { |
| 8220 | uint16_t i, count; |
| 8221 | enum lyxp_expr_type next_etype; |
| 8222 | LY_ERR rc; |
| 8223 | |
| 8224 | /* process operator repeats */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8225 | if (!exp->repeat[*tok_idx]) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8226 | next_etype = LYXP_EXPR_NONE; |
| 8227 | } else { |
| 8228 | /* find etype repeat */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 8229 | for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i) {} |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8230 | |
| 8231 | /* select one-priority lower because etype expression called us */ |
| 8232 | if (i) { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8233 | next_etype = exp->repeat[*tok_idx][i - 1]; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8234 | /* count repeats for that expression */ |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 8235 | 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] | 8236 | } else { |
| 8237 | next_etype = LYXP_EXPR_NONE; |
| 8238 | } |
| 8239 | } |
| 8240 | |
| 8241 | /* decide what expression are we parsing based on the repeat */ |
| 8242 | switch (next_etype) { |
| 8243 | case LYXP_EXPR_OR: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8244 | rc = eval_or_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8245 | break; |
| 8246 | case LYXP_EXPR_AND: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8247 | rc = eval_and_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8248 | break; |
| 8249 | case LYXP_EXPR_EQUALITY: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8250 | rc = eval_equality_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8251 | break; |
| 8252 | case LYXP_EXPR_RELATIONAL: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8253 | rc = eval_relational_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8254 | break; |
| 8255 | case LYXP_EXPR_ADDITIVE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8256 | rc = eval_additive_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8257 | break; |
| 8258 | case LYXP_EXPR_MULTIPLICATIVE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8259 | rc = eval_multiplicative_expr(exp, tok_idx, count, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8260 | break; |
| 8261 | case LYXP_EXPR_UNARY: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8262 | rc = eval_unary_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_UNION: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8265 | rc = eval_union_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_NONE: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8268 | rc = eval_path_expr(exp, tok_idx, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8269 | break; |
| 8270 | default: |
| 8271 | LOGINT_RET(set->ctx); |
| 8272 | } |
| 8273 | |
| 8274 | return rc; |
| 8275 | } |
| 8276 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8277 | /** |
| 8278 | * @brief Get root type. |
| 8279 | * |
| 8280 | * @param[in] ctx_node Context node. |
| 8281 | * @param[in] ctx_scnode Schema context node. |
| 8282 | * @param[in] options XPath options. |
| 8283 | * @return Root type. |
| 8284 | */ |
| 8285 | static enum lyxp_node_type |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 8286 | lyxp_get_root_type(const struct lyd_node *ctx_node, const struct lysc_node *ctx_scnode, uint32_t options) |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8287 | { |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 8288 | const struct lysc_node *op; |
| 8289 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8290 | if (options & LYXP_SCNODE_ALL) { |
Radek Krejci | 1e008d2 | 2020-08-17 11:37:37 +0200 | [diff] [blame] | 8291 | for (op = ctx_scnode; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {} |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 8292 | |
| 8293 | if (op || (options & LYXP_SCNODE)) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8294 | /* general root that can access everything */ |
| 8295 | return LYXP_NODE_ROOT; |
| 8296 | } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) { |
| 8297 | /* root context node can access only config data (because we said so, it is unspecified) */ |
| 8298 | return LYXP_NODE_ROOT_CONFIG; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8299 | } |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 8300 | return LYXP_NODE_ROOT; |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8301 | } |
| 8302 | |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 8303 | op = ctx_node ? ctx_node->schema : NULL; |
Michal Vasko | d989ba0 | 2020-08-24 10:59:24 +0200 | [diff] [blame] | 8304 | for ( ; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {} |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 8305 | |
| 8306 | if (!ctx_node || (!op && (ctx_node->schema->flags & LYS_CONFIG_W))) { |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8307 | /* root context node can access only config data (because we said so, it is unspecified) */ |
| 8308 | return LYXP_NODE_ROOT_CONFIG; |
| 8309 | } |
| 8310 | |
| 8311 | return LYXP_NODE_ROOT; |
| 8312 | } |
| 8313 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8314 | LY_ERR |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 8315 | lyxp_eval(struct lyxp_expr *exp, LY_PREFIX_FORMAT format, const struct lys_module *local_mod, const struct lyd_node *ctx_node, |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 8316 | enum lyxp_node_type ctx_node_type, const struct lyd_node *tree, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8317 | { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8318 | uint16_t tok_idx = 0; |
| 8319 | const struct lyd_node *real_ctx_node; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8320 | LY_ERR rc; |
| 8321 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8322 | LY_CHECK_ARG_RET(NULL, exp, local_mod, ctx_node, set, LY_EINVAL); |
| 8323 | |
| 8324 | if ((ctx_node_type == LYXP_NODE_ROOT) || (ctx_node_type == LYXP_NODE_ROOT_CONFIG)) { |
| 8325 | /* we always need some context node because it is used for resolving unqualified names */ |
| 8326 | real_ctx_node = NULL; |
| 8327 | } else { |
| 8328 | real_ctx_node = ctx_node; |
| 8329 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8330 | |
| 8331 | /* prepare set for evaluation */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8332 | tok_idx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8333 | memset(set, 0, sizeof *set); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8334 | set->type = LYXP_SET_NODE_SET; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8335 | 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] | 8336 | set->ctx = local_mod->ctx; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8337 | set->ctx_node = ctx_node; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8338 | set->root_type = lyxp_get_root_type(real_ctx_node, NULL, options); |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 8339 | for (set->context_op = ctx_node->schema; |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 8340 | set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); |
| 8341 | set->context_op = set->context_op->parent) {} |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8342 | set->local_mod = local_mod; |
Michal Vasko | f03ed03 | 2020-03-04 13:31:44 +0100 | [diff] [blame] | 8343 | set->tree = tree; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8344 | set->format = format; |
| 8345 | |
| 8346 | /* evaluate */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8347 | rc = eval_expr_select(exp, &tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8348 | if (rc != LY_SUCCESS) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8349 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8350 | } |
| 8351 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8352 | return rc; |
| 8353 | } |
| 8354 | |
| 8355 | #if 0 |
| 8356 | |
| 8357 | /* full xml printing of set elements, not used currently */ |
| 8358 | |
| 8359 | void |
| 8360 | lyxp_set_print_xml(FILE *f, struct lyxp_set *set) |
| 8361 | { |
| 8362 | uint32_t i; |
| 8363 | char *str_num; |
| 8364 | struct lyout out; |
| 8365 | |
| 8366 | memset(&out, 0, sizeof out); |
| 8367 | |
| 8368 | out.type = LYOUT_STREAM; |
| 8369 | out.method.f = f; |
| 8370 | |
| 8371 | switch (set->type) { |
| 8372 | case LYXP_SET_EMPTY: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8373 | ly_print_(&out, "Empty XPath set\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8374 | break; |
| 8375 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8376 | ly_print_(&out, "Boolean XPath set:\n"); |
| 8377 | ly_print_(&out, "%s\n\n", set->value.bool ? "true" : "false"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8378 | break; |
| 8379 | case LYXP_SET_STRING: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8380 | ly_print_(&out, "String XPath set:\n"); |
| 8381 | ly_print_(&out, "\"%s\"\n\n", set->value.str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8382 | break; |
| 8383 | case LYXP_SET_NUMBER: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8384 | ly_print_(&out, "Number XPath set:\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8385 | |
| 8386 | if (isnan(set->value.num)) { |
| 8387 | str_num = strdup("NaN"); |
| 8388 | } else if ((set->value.num == 0) || (set->value.num == -0.0f)) { |
| 8389 | str_num = strdup("0"); |
| 8390 | } else if (isinf(set->value.num) && !signbit(set->value.num)) { |
| 8391 | str_num = strdup("Infinity"); |
| 8392 | } else if (isinf(set->value.num) && signbit(set->value.num)) { |
| 8393 | str_num = strdup("-Infinity"); |
| 8394 | } else if ((long long)set->value.num == set->value.num) { |
| 8395 | if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) { |
| 8396 | str_num = NULL; |
| 8397 | } |
| 8398 | } else { |
| 8399 | if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) { |
| 8400 | str_num = NULL; |
| 8401 | } |
| 8402 | } |
| 8403 | if (!str_num) { |
| 8404 | LOGMEM; |
| 8405 | return; |
| 8406 | } |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8407 | ly_print_(&out, "%s\n\n", str_num); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8408 | free(str_num); |
| 8409 | break; |
| 8410 | case LYXP_SET_NODE_SET: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8411 | ly_print_(&out, "Node XPath set:\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8412 | |
| 8413 | for (i = 0; i < set->used; ++i) { |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8414 | ly_print_(&out, "%d. ", i + 1); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8415 | switch (set->node_type[i]) { |
| 8416 | case LYXP_NODE_ROOT_ALL: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8417 | ly_print_(&out, "ROOT all\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8418 | break; |
| 8419 | case LYXP_NODE_ROOT_CONFIG: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8420 | ly_print_(&out, "ROOT config\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8421 | break; |
| 8422 | case LYXP_NODE_ROOT_STATE: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8423 | ly_print_(&out, "ROOT state\n\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8424 | break; |
| 8425 | case LYXP_NODE_ROOT_NOTIF: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8426 | ly_print_(&out, "ROOT notification \"%s\"\n\n", set->value.nodes[i]->schema->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8427 | break; |
| 8428 | case LYXP_NODE_ROOT_RPC: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8429 | ly_print_(&out, "ROOT rpc \"%s\"\n\n", set->value.nodes[i]->schema->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8430 | break; |
| 8431 | case LYXP_NODE_ROOT_OUTPUT: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8432 | ly_print_(&out, "ROOT output \"%s\"\n\n", set->value.nodes[i]->schema->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8433 | break; |
| 8434 | case LYXP_NODE_ELEM: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8435 | ly_print_(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8436 | xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT); |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8437 | ly_print_(&out, "\n"); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8438 | break; |
| 8439 | case LYXP_NODE_TEXT: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8440 | ly_print_(&out, "TEXT \"%s\"\n\n", ((struct lyd_node_leaf_list *)set->value.nodes[i])->value_str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8441 | break; |
| 8442 | case LYXP_NODE_ATTR: |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 8443 | ly_print_(&out, "ATTR \"%s\" = \"%s\"\n\n", set->value.attrs[i]->name, set->value.attrs[i]->value); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8444 | break; |
| 8445 | } |
| 8446 | } |
| 8447 | break; |
| 8448 | } |
| 8449 | } |
| 8450 | |
| 8451 | #endif |
| 8452 | |
| 8453 | LY_ERR |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8454 | lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8455 | { |
| 8456 | long double num; |
| 8457 | char *str; |
| 8458 | LY_ERR rc; |
| 8459 | |
| 8460 | if (!set || (set->type == target)) { |
| 8461 | return LY_SUCCESS; |
| 8462 | } |
| 8463 | |
| 8464 | /* it's not possible to convert anything into a node set */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8465 | assert(target != LYXP_SET_NODE_SET); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8466 | |
| 8467 | if (set->type == LYXP_SET_SCNODE_SET) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8468 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8469 | return LY_EINVAL; |
| 8470 | } |
| 8471 | |
| 8472 | /* to STRING */ |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8473 | 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] | 8474 | switch (set->type) { |
| 8475 | case LYXP_SET_NUMBER: |
| 8476 | if (isnan(set->val.num)) { |
| 8477 | set->val.str = strdup("NaN"); |
| 8478 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 8479 | } else if ((set->val.num == 0) || (set->val.num == -0.0f)) { |
| 8480 | set->val.str = strdup("0"); |
| 8481 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 8482 | } else if (isinf(set->val.num) && !signbit(set->val.num)) { |
| 8483 | set->val.str = strdup("Infinity"); |
| 8484 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 8485 | } else if (isinf(set->val.num) && signbit(set->val.num)) { |
| 8486 | set->val.str = strdup("-Infinity"); |
| 8487 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1); |
| 8488 | } else if ((long long)set->val.num == set->val.num) { |
| 8489 | if (asprintf(&str, "%lld", (long long)set->val.num) == -1) { |
| 8490 | LOGMEM_RET(set->ctx); |
| 8491 | } |
| 8492 | set->val.str = str; |
| 8493 | } else { |
| 8494 | if (asprintf(&str, "%03.1Lf", set->val.num) == -1) { |
| 8495 | LOGMEM_RET(set->ctx); |
| 8496 | } |
| 8497 | set->val.str = str; |
| 8498 | } |
| 8499 | break; |
| 8500 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8501 | if (set->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8502 | set->val.str = strdup("true"); |
| 8503 | } else { |
| 8504 | set->val.str = strdup("false"); |
| 8505 | } |
| 8506 | LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM); |
| 8507 | break; |
| 8508 | case LYXP_SET_NODE_SET: |
| 8509 | assert(set->used); |
| 8510 | |
| 8511 | /* we need the set sorted, it affects the result */ |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8512 | assert(!set_sort(set)); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8513 | |
Michal Vasko | 5e0e6eb | 2019-11-06 15:47:50 +0100 | [diff] [blame] | 8514 | rc = cast_node_set_to_string(set, &str); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8515 | LY_CHECK_RET(rc); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8516 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8517 | set->val.str = str; |
| 8518 | break; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8519 | default: |
| 8520 | LOGINT_RET(set->ctx); |
| 8521 | } |
| 8522 | set->type = LYXP_SET_STRING; |
| 8523 | } |
| 8524 | |
| 8525 | /* to NUMBER */ |
| 8526 | if (target == LYXP_SET_NUMBER) { |
| 8527 | switch (set->type) { |
| 8528 | case LYXP_SET_STRING: |
| 8529 | num = cast_string_to_number(set->val.str); |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8530 | lyxp_set_free_content(set); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8531 | set->val.num = num; |
| 8532 | break; |
| 8533 | case LYXP_SET_BOOLEAN: |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8534 | if (set->val.bln) { |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8535 | set->val.num = 1; |
| 8536 | } else { |
| 8537 | set->val.num = 0; |
| 8538 | } |
| 8539 | break; |
| 8540 | default: |
| 8541 | LOGINT_RET(set->ctx); |
| 8542 | } |
| 8543 | set->type = LYXP_SET_NUMBER; |
| 8544 | } |
| 8545 | |
| 8546 | /* to BOOLEAN */ |
| 8547 | if (target == LYXP_SET_BOOLEAN) { |
| 8548 | switch (set->type) { |
| 8549 | case LYXP_SET_NUMBER: |
| 8550 | 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] | 8551 | set->val.bln = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8552 | } else { |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8553 | set->val.bln = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8554 | } |
| 8555 | break; |
| 8556 | case LYXP_SET_STRING: |
| 8557 | if (set->val.str[0]) { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8558 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8559 | set->val.bln = 1; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8560 | } else { |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8561 | lyxp_set_free_content(set); |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8562 | set->val.bln = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8563 | } |
| 8564 | break; |
| 8565 | case LYXP_SET_NODE_SET: |
Michal Vasko | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8566 | if (set->used) { |
| 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 | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8569 | } else { |
| 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 | d367889 | 2020-05-21 10:06:58 +0200 | [diff] [blame] | 8572 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8573 | break; |
| 8574 | default: |
| 8575 | LOGINT_RET(set->ctx); |
| 8576 | } |
| 8577 | set->type = LYXP_SET_BOOLEAN; |
| 8578 | } |
| 8579 | |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8580 | return LY_SUCCESS; |
| 8581 | } |
| 8582 | |
| 8583 | LY_ERR |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 8584 | lyxp_atomize(struct lyxp_expr *exp, LY_PREFIX_FORMAT format, const struct lys_module *local_mod, |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 8585 | const struct lysc_node *ctx_scnode, enum lyxp_node_type ctx_scnode_type, struct lyxp_set *set, uint32_t options) |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8586 | { |
| 8587 | struct ly_ctx *ctx; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8588 | const struct lysc_node *real_ctx_scnode; |
| 8589 | uint16_t tok_idx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8590 | |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8591 | 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] | 8592 | |
| 8593 | ctx = local_mod->ctx; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8594 | if ((ctx_scnode_type == LYXP_NODE_ROOT) || (ctx_scnode_type == LYXP_NODE_ROOT_CONFIG)) { |
| 8595 | /* we always need some context node because it is used for resolving unqualified names */ |
| 8596 | real_ctx_scnode = NULL; |
| 8597 | } else { |
| 8598 | real_ctx_scnode = ctx_scnode; |
| 8599 | } |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8600 | |
| 8601 | /* prepare set for evaluation */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8602 | tok_idx = 0; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8603 | memset(set, 0, sizeof *set); |
| 8604 | set->type = LYXP_SET_SCNODE_SET; |
Radek Krejci | aa6b53f | 2020-08-27 15:19:03 +0200 | [diff] [blame^] | 8605 | LY_CHECK_RET(lyxp_set_scnode_insert_node(set, real_ctx_scnode, ctx_scnode_type, NULL)); |
Michal Vasko | 5c4e589 | 2019-11-14 12:31:38 +0100 | [diff] [blame] | 8606 | set->val.scnodes[0].in_ctx = -2; |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8607 | set->ctx = ctx; |
| 8608 | set->ctx_scnode = ctx_scnode; |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8609 | set->root_type = lyxp_get_root_type(NULL, real_ctx_scnode, options); |
Michal Vasko | 6b26e74 | 2020-07-17 15:02:10 +0200 | [diff] [blame] | 8610 | for (set->context_op = ctx_scnode; |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 8611 | set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); |
| 8612 | set->context_op = set->context_op->parent) {} |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8613 | set->local_mod = local_mod; |
| 8614 | set->format = format; |
| 8615 | |
| 8616 | /* evaluate */ |
Michal Vasko | 004d315 | 2020-06-11 19:59:22 +0200 | [diff] [blame] | 8617 | return eval_expr_select(exp, &tok_idx, 0, set, options); |
Michal Vasko | 03ff5a7 | 2019-09-11 13:49:33 +0200 | [diff] [blame] | 8618 | } |
Michal Vasko | d43d71a | 2020-08-07 14:54:58 +0200 | [diff] [blame] | 8619 | |
| 8620 | API const char * |
| 8621 | lyxp_get_expr(const struct lyxp_expr *path) |
| 8622 | { |
| 8623 | if (!path) { |
| 8624 | return NULL; |
| 8625 | } |
| 8626 | |
| 8627 | return path->expr; |
| 8628 | } |